RasterWorks.ob07 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (*
  2. Copyright 2016, 2018, 2022 KolibriOS team
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *)
  14. MODULE RasterWorks;
  15. IMPORT sys := SYSTEM, KOSAPI;
  16. CONST
  17. (* flags *)
  18. bold *= 1;
  19. italic *= 2;
  20. underline *= 4;
  21. strike_through *= 8;
  22. align_right *= 16;
  23. align_center *= 32;
  24. bpp32 *= 128;
  25. (* encoding *)
  26. cp866 *= 1;
  27. utf16le *= 2;
  28. utf8 *= 3;
  29. VAR
  30. // draw text on 24bpp or 32bpp image
  31. // autofits text between 'x' and 'xSize'
  32. drawText *: PROCEDURE (canvas, x, y, string, charQuantity, fontColor, params: INTEGER): INTEGER;
  33. (*
  34. [canvas]:
  35. xSize dd ?
  36. ySize dd ?
  37. picture rb xSize * ySize * bpp
  38. fontColor dd AARRGGBB
  39. AA = alpha channel ; 0 = transparent, FF = non transparent
  40. params dd ffeewwhh
  41. hh = char height
  42. ww = char width ; 0 = auto (proportional)
  43. ee = encoding ; 1 = cp866, 2 = UTF-16LE, 3 = UTF-8
  44. ff = flags ; 0001 = bold, 0010 = italic
  45. ; 0100 = underline, 1000 = strike-through
  46. 00010000 = align right, 00100000 = align center
  47. 01000000 = set text area between higher and lower halfs of 'x'
  48. 10000000 = 32bpp canvas insted of 24bpp
  49. all flags combinable, except align right + align center
  50. returns: char width (0 = error)
  51. *)
  52. // calculate amount of valid chars in UTF-8 string
  53. // supports zero terminated string (set byteQuantity = -1)
  54. countUTF8Z *: PROCEDURE (string, byteQuantity: INTEGER): INTEGER;
  55. // calculate amount of chars that fits given width
  56. charsFit *: PROCEDURE (areaWidth, charHeight: INTEGER): INTEGER;
  57. // calculate string width in pixels
  58. strWidth *: PROCEDURE (charQuantity, charHeight: INTEGER): INTEGER;
  59. PROCEDURE params* (charHeight, charWidth, encoding, flags: INTEGER): INTEGER;
  60. (*
  61. hh = char height
  62. ww = char width ; 0 = auto (proportional)
  63. ee = encoding ; 1 = cp866, 2 = UTF-16LE, 3 = UTF-8
  64. ff = flags ; 0001 = bold, 0010 = italic
  65. ; 0100 = underline, 1000 = strike-through
  66. 00010000 = align right, 00100000 = align center
  67. 01000000 = set text area between higher and lower halfs of 'x'
  68. 10000000 = 32bpp canvas insted of 24bpp
  69. all flags combinable, except align right + align center
  70. *)
  71. RETURN charHeight + LSL(charWidth, 8) + LSL(encoding, 16) + LSL(flags, 24)
  72. END params;
  73. PROCEDURE main;
  74. VAR Lib: INTEGER;
  75. PROCEDURE GetProc(Lib, v: INTEGER; name: ARRAY OF CHAR);
  76. VAR a: INTEGER;
  77. BEGIN
  78. a := KOSAPI.GetProcAdr(name, Lib);
  79. ASSERT(a # 0);
  80. sys.PUT(v, a)
  81. END GetProc;
  82. BEGIN
  83. Lib := KOSAPI.LoadLib("/sys/lib/RasterWorks.obj");
  84. ASSERT(Lib # 0);
  85. GetProc(Lib, sys.ADR(drawText), "drawText");
  86. GetProc(Lib, sys.ADR(countUTF8Z), "countUTF8Z");
  87. GetProc(Lib, sys.ADR(charsFit), "charsFit");
  88. GetProc(Lib, sys.ADR(strWidth), "strWidth");
  89. END main;
  90. BEGIN
  91. main
  92. END RasterWorks.