WMGraphics.Mod 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. MODULE WMGraphics; (** AUTHOR "TF"; PURPOSE "Generic Graphic Support"; *)
  2. IMPORT
  3. Kernel, Rectangles := WMRectangles, Raster, KernelLog, UTF8Strings, Strings, RasterScale := WMRasterScale,
  4. Codecs, Files, Streams;
  5. CONST
  6. (** Copy Modes *)
  7. ModeCopy* = RasterScale.ModeCopy; ModeSrcOverDst* = RasterScale.ModeSrcOverDst;
  8. (** Scale Modes *)
  9. ScaleBox* = RasterScale.ScaleBox; ScaleBilinear* = RasterScale.ScaleBilinear;
  10. (** Clip Modes *)
  11. ClipNone* = 0; ClipRect* = 1; (*ClipStencil* = 2;*)
  12. (** FontStyles *)
  13. FontBold* = 0; FontItalic* = 1;
  14. Black* = 0FFH; White* = LONGINT(0FFFFFFFFH);
  15. Gray*=LONGINT(0777777FFH);
  16. Red* = LONGINT(0FF0000FFH);
  17. Green* = 000FF00FFH; Blue* = 0FFFFH;
  18. Yellow* = LONGINT(0FFFF00FFH);
  19. Magenta* = LONGINT(0FF00FFFFH);
  20. Cyan* = 00FFFFFFH;
  21. TYPE
  22. Char32 = LONGINT;
  23. Point2d* = RECORD x*, y* : LONGINT END;
  24. Image* = OBJECT(Raster.Image)
  25. VAR
  26. key* : POINTER TO ARRAY OF CHAR;
  27. END Image;
  28. Rectangle* = Rectangles.Rectangle;
  29. Color* = LONGINT;
  30. GlyphSpacings* = RECORD
  31. bearing* : Rectangle;
  32. width*, height*, ascent*, descent* : LONGINT;
  33. dx*, dy* : LONGINT; (** Delta position where the bitmap returned by GetGlyphMap has to be placed relatively to
  34. x, y on the base line *)
  35. END;
  36. (* Bearings are the blank spaces left an right of a character.
  37. bearing.l is the left, bearing.r is the right, bearing.t top and bearing.b the bottom side - bearing of the character
  38. hadvance = bearing.l + width + bearing.r --> the distance to the next character on the line without --> kerning
  39. vadvance = bearing.t + height + bearing.b --> the baseline to baseline distance of two lines of this font
  40. When rendering a character at the position (x, y), y refers to the y position of the baseline, x refers to .
  41. --> Kerning pairs
  42. *)
  43. (* ascent is the height of the font above the base line in units of the destination canvas *)
  44. (* descent is the height of the font below the base line in units of the destination canvas *)
  45. (* basetobasedist is the suggested distance between two lines of this font *)
  46. Font* = OBJECT
  47. VAR
  48. ascent*, descent* : LONGINT;
  49. name* : ARRAY 256 OF CHAR;
  50. size* : LONGINT;
  51. style* : SET;
  52. PROCEDURE &Init*;
  53. END Init;
  54. PROCEDURE GetHeight*():LONGINT;
  55. BEGIN
  56. RETURN ascent + descent
  57. END GetHeight;
  58. PROCEDURE GetAscent*():LONGINT;
  59. BEGIN
  60. RETURN ascent
  61. END GetAscent;
  62. PROCEDURE GetDescent*():LONGINT;
  63. BEGIN
  64. RETURN descent
  65. END GetDescent;
  66. (* return TRUE if the font can render the character *)
  67. PROCEDURE HasChar*(char : Char32) : BOOLEAN;
  68. BEGIN
  69. RETURN FALSE
  70. END HasChar;
  71. (** Render an UTF8 string to a canvas *)
  72. PROCEDURE RenderString*(canvas : Canvas ; x, y : REAL; CONST text : ARRAY OF CHAR);
  73. VAR i, len, code : LONGINT; g : GlyphSpacings;
  74. BEGIN
  75. len := LEN(text); i := 0;
  76. WHILE (i < len) & (text[i] # 0X) DO
  77. IF UTF8Strings.DecodeChar(text, i, code) THEN
  78. IF HasChar(code) THEN
  79. GetGlyphSpacings(code, g);
  80. RenderChar(canvas, x, y, code)
  81. ELSE
  82. FBGetGlyphSpacings(code, g);
  83. FBRenderChar(canvas, x, y, code)
  84. END;
  85. x := x + g.bearing.l + g.width + g.bearing.r
  86. ELSE INC(i) (* avoid endless loop *)
  87. END
  88. END
  89. END RenderString;
  90. (** Render an UTF8 string to a canvas *)
  91. PROCEDURE GetStringSize*(CONST text : ARRAY OF CHAR; VAR dx, dy : LONGINT);
  92. VAR i, len, code : LONGINT; g : GlyphSpacings;
  93. BEGIN
  94. len := LEN(text); i := 0; dx := 0; dy := GetHeight();
  95. WHILE (i < len) & (text[i] # 0X) DO
  96. IF UTF8Strings.DecodeChar(text, i, code) THEN
  97. IF HasChar(code) THEN GetGlyphSpacings(code, g);
  98. ELSE FBGetGlyphSpacings(code, g)
  99. END;
  100. dy := Strings.Max(dy, g.height);
  101. dx := dx + g.bearing.l + g.width + g.bearing.r
  102. ELSE INC(i) (* avoid endless loop *)
  103. END
  104. END
  105. END GetStringSize;
  106. (** Render character char to canvas at x, y (baseline) *)
  107. PROCEDURE RenderChar*(canvas : Canvas ; x, y : REAL; char : Char32);
  108. VAR g : GlyphSpacings; img : Image;
  109. BEGIN
  110. GetGlyphSpacings(char, g);
  111. GetGlyphMap(char, img);
  112. canvas.DrawImage(ENTIER(x + g.bearing.l) + g.dx, ENTIER(y - ascent) + g.dy, img, ModeSrcOverDst)
  113. END RenderChar;
  114. (** return a bitmap of character code *)
  115. PROCEDURE GetGlyphMap*(code : LONGINT; VAR map : Image);
  116. END GetGlyphMap;
  117. (** return spacing of character code *)
  118. PROCEDURE GetGlyphSpacings*(code : LONGINT; VAR glyphSpacings : GlyphSpacings);
  119. END GetGlyphSpacings;
  120. END Font;
  121. FontManager* = OBJECT
  122. PROCEDURE GetFont*(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : Font;
  123. BEGIN
  124. RETURN NIL
  125. END GetFont;
  126. END FontManager;
  127. CanvasState* = RECORD
  128. clipMode : SET;
  129. clipRect : Rectangle;
  130. limits : Rectangle;
  131. dx, dy : LONGINT;
  132. font : Font;
  133. color : LONGINT;
  134. END;
  135. Canvas* = OBJECT
  136. VAR
  137. limits*, (* The limits to which the clip Rect can be set *)
  138. clipRect* : Rectangle; (* The current clip rectangle *)
  139. dx*, dy*, color* : LONGINT;
  140. clipMode* : SET;
  141. generator*: Strings.String;
  142. font- : Font;
  143. (** IF cs is NIL a new canvas state object is created for this canvas, otherwise cs is reused *)
  144. PROCEDURE SaveState*(VAR cs : CanvasState);
  145. BEGIN
  146. cs.clipMode := clipMode;
  147. cs.limits := limits;
  148. cs.dx := dx; cs.dy := dy;
  149. cs.font := font; cs.color := color;
  150. GetClipRect(cs.clipRect)
  151. END SaveState;
  152. (** Restore a previously saved canvas state *)
  153. PROCEDURE RestoreState*(CONST cs : CanvasState);
  154. BEGIN
  155. clipMode := cs.clipMode;
  156. limits := cs.limits;
  157. dx := cs.dx; dy := cs.dy;
  158. font := cs.font; color := cs.color;
  159. SetClipRect(cs.clipRect)
  160. END RestoreState;
  161. (** set the current clipping rectangle as the limit for new SetClipRect operations.
  162. ddx and ddy specify a coordinate shift. *)
  163. PROCEDURE ClipRectAsNewLimits*(ddx, ddy : LONGINT);
  164. BEGIN
  165. limits := clipRect;
  166. SetDelta(dx + ddx, dy + ddy)
  167. END ClipRectAsNewLimits;
  168. (** in user coordinates *)
  169. PROCEDURE SetClipRect*(rect : Rectangle);
  170. BEGIN
  171. INCL(clipMode, ClipRect);
  172. rect.r := Max(rect.r, rect.l); rect.b := Max(rect.b, rect.t);
  173. Rectangles.MoveRel(rect, dx, dy);
  174. Rectangles.ClipRect(rect, limits);
  175. clipRect := rect
  176. END SetClipRect;
  177. (** return the current Clipping rectangle in user coordinates; Clients may use this to avoid drawing that is
  178. clipped away for sure *)
  179. PROCEDURE GetClipRect*(VAR rect : Rectangle);
  180. BEGIN
  181. rect := clipRect;
  182. Rectangles.MoveRel(rect, -dx, -dy)
  183. END GetClipRect;
  184. (** *)
  185. PROCEDURE SetClipMode*(mode : SET);
  186. BEGIN
  187. clipMode := mode
  188. END SetClipMode;
  189. (** Set color for fonts *)
  190. PROCEDURE SetColor*(x : Color);
  191. BEGIN
  192. color := x
  193. END SetColor;
  194. PROCEDURE GetColor*() : LONGINT;
  195. BEGIN
  196. RETURN color;
  197. END GetColor;
  198. (** Set the current font. IF f is NIL, GetFont will search for the system default font. *)
  199. PROCEDURE SetFont*(f: Font);
  200. BEGIN
  201. font := f
  202. END SetFont;
  203. (** Return the font currently set for this canvas. If no font is set, return the system default font. If no
  204. system default font is set, block until a default font is set *)
  205. PROCEDURE GetFont*():Font;
  206. BEGIN
  207. IF font = NIL THEN font := GetDefaultFont() END;
  208. RETURN font
  209. END GetFont;
  210. (** Draw an UTF8 String at position x, y (base line)
  211. The currently set font and color is used
  212. *)
  213. PROCEDURE DrawString*(x, y: LONGINT; CONST text : ARRAY OF CHAR);
  214. BEGIN
  215. IF font # NIL THEN
  216. font.RenderString(SELF, x, y, text)
  217. END
  218. END DrawString;
  219. PROCEDURE SetLineWidth*(w:REAL);
  220. BEGIN
  221. (* Dummy. But is implemented in WMGraphicsGfx *)
  222. END SetLineWidth;
  223. (** draw a line within the current clipping rectangle *)
  224. (** Override for improved speed *)
  225. PROCEDURE Line*(x0, y0, x1, y1 : LONGINT; color : Color; mode : LONGINT);
  226. VAR t, xi, mi, xf, mf, dt2 : LONGINT;
  227. BEGIN
  228. IF y0 = y1 THEN (* horizontal case *)
  229. IF x0 > x1 THEN t := x0; x0 := x1; x1 := t END;
  230. Fill(Rectangles.MakeRect(x0, y0, x1 + 1, y0 + 1), color, mode)
  231. ELSIF x0 = x1 THEN (* vertical case *)
  232. IF y0 > y1 THEN t := y0; y0 := y1; y1 := t END;
  233. Fill(Rectangles.MakeRect(x0, y0, x0 + 1, y1 + 1), color, mode)
  234. ELSE (* general case *)
  235. IF ABS(y1 - y0) > ABS(x1 - x0) THEN
  236. IF y0 > y1 THEN t := y0; y0 := y1; y1 := t; t := x0; x0 := x1; x1 := t END;
  237. xi := x0; xf := y0 - y1; mi := (x1 - x0) DIV (y1 - y0); mf := 2 * ( (x1 - x0) MOD (y1 - y0)); dt2 := 2 * (y1 - y0);
  238. FOR t := y0 TO y1 DO
  239. SetPixel(xi, t, color, mode);
  240. INC(xi, mi); INC(xf, mf);
  241. IF xf > 0 THEN INC(xi); DEC(xf, dt2) END
  242. END
  243. ELSE
  244. IF x0 > x1 THEN t := y0; y0 := y1; y1 := t; t := x0; x0 := x1; x1 := t END;
  245. xi := y0; xf := x0 - x1; mi := (y1 - y0) DIV (x1 - x0); mf := 2 * ( (y1 - y0) MOD (x1 - x0)); dt2 := 2 * (x1 - x0);
  246. FOR t := x0 TO x1 DO
  247. SetPixel(t, xi, color, mode);
  248. INC(xi, mi); INC(xf, mf);
  249. IF xf > 0 THEN INC(xi); DEC(xf, dt2) END
  250. END
  251. END
  252. END
  253. END Line;
  254. (** set a pixel within the current clipping rectangle *)
  255. PROCEDURE SetPixel*(x, y : LONGINT; color : Color; mode : LONGINT);
  256. BEGIN
  257. Fill(MakeRectangle(x, y, x + 1, y + 1), color, mode)
  258. END SetPixel;
  259. (** fill a rectangle within the current clipping rectangle *)
  260. PROCEDURE Fill*(rect : Rectangle; color : Color; mode : LONGINT);
  261. END Fill;
  262. (** fill a polygon given by points *)
  263. PROCEDURE FillPolygonFlat*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; color : Color; mode : LONGINT);
  264. END FillPolygonFlat;
  265. PROCEDURE FillPolygonCB*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; callBack : FillLineCallBack);
  266. END FillPolygonCB;
  267. PROCEDURE PolyLine*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; closed : BOOLEAN; color : Color; mode : LONGINT);
  268. VAR i : LONGINT;
  269. BEGIN
  270. FOR i := 1 TO nofPoints - 1 DO
  271. Line(points[i-1].x, points[i-1].y, points[i].x, points[i].y, color, mode)
  272. END;
  273. IF closed THEN
  274. Line(points[nofPoints-1].x, points[nofPoints-1].y, points[0].x, points[0].y, color, mode)
  275. END
  276. END PolyLine;
  277. (** draw an image within the current clipping rectangle *)
  278. PROCEDURE DrawImage*(x, y: LONGINT; image: Raster.Image; mode : LONGINT);
  279. END DrawImage;
  280. PROCEDURE ScaleImage*(src : Raster.Image; sr, dr : Rectangle; copyMode, scaleMode : LONGINT);
  281. END ScaleImage;
  282. (** Set coordinate shift *)
  283. PROCEDURE SetDelta*(dx, dy: LONGINT);
  284. BEGIN
  285. SELF.dx := dx; SELF.dy := dy
  286. END SetDelta;
  287. (** Set the available range in the super drawing space *)
  288. PROCEDURE SetLimits*(r : Rectangle);
  289. BEGIN
  290. limits := r
  291. END SetLimits;
  292. (** Get the avalilable range in the super drawing space, like the range set but clipped *)
  293. PROCEDURE GetLimits*(): Rectangle;
  294. BEGIN
  295. RETURN limits
  296. END GetLimits;
  297. END Canvas;
  298. TYPE
  299. FillPosEntry = RECORD pos, next : LONGINT END;
  300. FillHeap = POINTER TO ARRAY OF FillPosEntry;
  301. FillLineCallBack* = PROCEDURE {DELEGATE} (canvas : Canvas; y, x0, x1 : LONGINT);
  302. CanvasGenerator* = PROCEDURE(img:Raster.Image):BufferCanvas;
  303. TYPE
  304. BufferCanvas* = OBJECT(Canvas)
  305. VAR img- : Raster.Image;
  306. bounds : Rectangle; (* real limiting img bounds *)
  307. (* filling *)
  308. fillHeap : FillHeap;
  309. heapSize, topHeap : LONGINT;
  310. height : LONGINT;
  311. edges : POINTER TO ARRAY OF LONGINT;
  312. PROCEDURE &New*(img : Raster.Image);
  313. BEGIN
  314. SELF.img := img;
  315. bounds := MakeRectangle(0, 0, img.width, img.height);
  316. SetLimits(bounds);
  317. clipRect := bounds;
  318. clipMode := { ClipRect };
  319. (* filling *)
  320. height := img.height; NEW(edges, height);
  321. SetFont(GetDefaultFont());
  322. generator:=Strings.NewString("WMGraphics.GenCanvas");
  323. END New;
  324. (* Not thread-safe!!! *)
  325. PROCEDURE GetImage*() : Raster.Image;
  326. BEGIN
  327. RETURN img;
  328. END GetImage;
  329. PROCEDURE SetLimits*(r : Rectangle);
  330. BEGIN
  331. r.r := Max(r.r, r.l); r.b := Max(r.t, r.b);
  332. Rectangles.ClipRect(r, bounds); SetLimits^(r)
  333. END SetLimits;
  334. (* PROCEDURE Line*(x0, y0, x1, y1 : LONGINT; color : Color; mode : LONGINT);
  335. BEGIN
  336. END Line; *)
  337. PROCEDURE Fill*(rect : Rectangle; color : Color; mode : LONGINT);
  338. VAR rm : Raster.Mode; pix : Raster.Pixel;
  339. BEGIN
  340. (* convert to super coordinates *)
  341. Rectangles.MoveRel(rect, dx, dy);
  342. IF ClipRect IN clipMode THEN Rectangles.ClipRect(rect, clipRect) END;
  343. Rectangles.ClipRect(rect, limits);
  344. IF ~Rectangles.RectEmpty(rect) THEN
  345. Raster.SetRGBA(pix, ((color DIV 65536) DIV 256) MOD 256, (color DIV 65536) MOD 256,
  346. (color DIV 256) MOD 256, color MOD 256);
  347. IF mode = ModeCopy THEN Raster.InitMode(rm, Raster.srcCopy) ELSE Raster.InitMode(rm, Raster.srcOverDst) END;
  348. Raster.Fill(SELF.img, rect.l, rect.t, rect.r, rect.b, pix, rm);
  349. END
  350. END Fill;
  351. (* Polygon filling *)
  352. (** fill a polygon given by points *)
  353. PROCEDURE FillPolygonFlat*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; color : Color; mode : LONGINT);
  354. VAR i : LONGINT;
  355. BEGIN
  356. IF nofPoints < 3 THEN RETURN END;
  357. ASSERT(nofPoints <= LEN(points));
  358. ClearHeap;
  359. FOR i := 1 TO nofPoints - 1 DO AddLine(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y) END;
  360. AddLine(points[nofPoints - 1].x, points[nofPoints - 1].y, points[0].x, points[0].y);
  361. FillFlat(color, mode)
  362. END FillPolygonFlat;
  363. (** fill a polygon given by points *)
  364. PROCEDURE FillPolygonCB*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; callBack : FillLineCallBack);
  365. VAR i : LONGINT;
  366. BEGIN
  367. IF nofPoints < 3 THEN RETURN END;
  368. ASSERT(nofPoints <= LEN(points));
  369. ClearHeap;
  370. FOR i := 1 TO nofPoints - 1 DO AddLine(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y) END;
  371. AddLine(points[nofPoints - 1].x, points[nofPoints - 1].y, points[0].x, points[0].y);
  372. FillCB(callBack)
  373. END FillPolygonCB;
  374. PROCEDURE ClearHeap;
  375. VAR i : LONGINT;
  376. BEGIN
  377. topHeap := 0;
  378. FOR i := 0 TO height - 1 DO edges[i] := 0 END;
  379. IF fillHeap = NIL THEN NEW(fillHeap, 1024); heapSize := 1024 END
  380. END ClearHeap;
  381. PROCEDURE NewFillPos(pos : LONGINT) : LONGINT;
  382. VAR newHeap : FillHeap;
  383. i : LONGINT;
  384. BEGIN
  385. INC(topHeap);
  386. IF topHeap >= heapSize THEN (* grow heap *)
  387. NEW(newHeap, heapSize * 2);
  388. FOR i := 0 TO heapSize - 1 DO newHeap[i] := fillHeap[i] END;
  389. heapSize := heapSize * 2;
  390. fillHeap := newHeap
  391. END;
  392. fillHeap[topHeap].pos := pos;
  393. fillHeap[topHeap].next := 0;
  394. RETURN topHeap
  395. END NewFillPos;
  396. PROCEDURE AddIntersection(y, pos : LONGINT);
  397. VAR new, cur : LONGINT;
  398. BEGIN
  399. IF (y < 0) OR (y >= height) THEN RETURN END;
  400. new := NewFillPos(pos);
  401. IF edges[y] = 0 THEN edges[y] := new
  402. ELSE
  403. cur := edges[y];
  404. IF fillHeap[cur].pos > pos THEN
  405. fillHeap[new].next := cur;
  406. edges[y] := new
  407. ELSE
  408. WHILE (fillHeap[cur].next # 0) & (fillHeap[fillHeap[cur].next].pos < pos) DO cur := fillHeap[cur].next END;
  409. fillHeap[new].next := fillHeap[cur].next;
  410. fillHeap[cur].next := new
  411. END;
  412. END;
  413. END AddIntersection;
  414. PROCEDURE AddLine(x0, y0, x1, y1 : LONGINT);
  415. VAR t, xi, xf, mi, mf, dt2 : LONGINT ;
  416. BEGIN
  417. IF (y0 = y1) THEN RETURN END;
  418. IF y0 > y1 THEN t := y0; y0 := y1; y1 := t; t := x0; x0 := x1; x1 := t END;
  419. xi := x0; xf := y0 - y1; mi := (x1 - x0) DIV (y1 - y0); mf := 2 * ( (x1 - x0) MOD (y1 - y0)); dt2 := 2 * (y1 - y0);
  420. FOR t := y0 TO y1 - 1 DO
  421. AddIntersection(t, xi);
  422. INC(xi, mi); INC(xf, mf);
  423. IF xf > 0 THEN INC(xi); DEC(xf, dt2) END
  424. END
  425. END AddLine;
  426. PROCEDURE FillFlat(color, mode : LONGINT);
  427. VAR i, sp, cur : LONGINT;
  428. in : BOOLEAN;
  429. BEGIN
  430. FOR i := 0 TO height - 1 DO
  431. cur := edges[i];
  432. in := FALSE;
  433. WHILE cur # 0 DO
  434. in := ~in;
  435. IF in THEN sp := fillHeap[cur].pos
  436. ELSE Fill(Rectangles.MakeRect(sp, i, fillHeap[cur].pos, i + 1), color, mode)
  437. END;
  438. cur := fillHeap[cur].next
  439. END
  440. END
  441. END FillFlat;
  442. PROCEDURE FillCB(cb : FillLineCallBack);
  443. VAR i, sp, cur : LONGINT;
  444. in : BOOLEAN;
  445. BEGIN
  446. FOR i := 0 TO height - 1 DO
  447. cur := edges[i];
  448. in := FALSE;
  449. WHILE cur # 0 DO
  450. in := ~in;
  451. IF in THEN sp := fillHeap[cur].pos
  452. ELSE cb(SELF, i, sp, fillHeap[cur].pos)
  453. END;
  454. cur := fillHeap[cur].next
  455. END
  456. END
  457. END FillCB;
  458. PROCEDURE DrawImage*(x, y: LONGINT; img: Raster.Image; mode : LONGINT);
  459. VAR imgBounds : Rectangle;
  460. rm : Raster.Mode;
  461. BEGIN
  462. IF img = NIL THEN RETURN END;
  463. imgBounds := MakeRectangle(0, 0, img.width, img.height);
  464. (* to super coordinates *)
  465. Rectangles.MoveRel(imgBounds, x + dx, y + dy);
  466. IF ClipRect IN clipMode THEN Rectangles.ClipRect(imgBounds, clipRect) END;
  467. Rectangles.ClipRect(imgBounds, limits);
  468. IF ~Rectangles.RectEmpty(imgBounds) THEN
  469. IF mode = ModeCopy THEN Raster.InitMode(rm, Raster.srcCopy) ELSE Raster.InitMode(rm, Raster.srcOverDst) END;
  470. Raster.SetRGBA(rm.col, (color DIV 1000000H) MOD 100H, (color DIV 10000H) MOD 100H,
  471. (color DIV 100H) MOD 100H, color MOD 100H);
  472. IF imgBounds.l - (x + dx) < 0 THEN
  473. KernelLog.String("Error...");
  474. KernelLog.String("x + dx = "); KernelLog.Int(x + dx, 4); KernelLog.Ln;
  475. KernelLog.String("x = "); KernelLog.Int(x, 4); KernelLog.Ln;
  476. KernelLog.String("dx = "); KernelLog.Int(dx, 4); KernelLog.Ln;
  477. KernelLog.String("clip = "); KernelLog.Int(clipRect.l, 4); KernelLog.Int(clipRect.t, 4);
  478. KernelLog.Int(clipRect.r, 4); KernelLog.Int(clipRect.b, 4);KernelLog.Ln;
  479. KernelLog.String("imgBounds = ");
  480. KernelLog.Int(imgBounds.l, 4); KernelLog.Int(imgBounds.t, 4); KernelLog.Int(imgBounds.r, 4); KernelLog.Int(imgBounds.b, 4);KernelLog.Ln;
  481. KernelLog.String("limits = "); KernelLog.Int(limits.l, 4); KernelLog.Int(limits.t, 4);
  482. KernelLog.Int(limits.r, 4); KernelLog.Int(limits.b, 4);KernelLog.Ln;
  483. RETURN
  484. END;
  485. Raster.Copy(img, SELF.img, imgBounds.l - (x + dx), imgBounds.t - (y + dy),
  486. imgBounds.r - imgBounds.l + (imgBounds.l - (x + dx)), imgBounds.b - imgBounds.t + (imgBounds.t - (y + dy)),
  487. imgBounds.l, imgBounds.t, rm);
  488. END;
  489. END DrawImage;
  490. PROCEDURE ScaleImage*(src : Raster.Image; sr , dr : Rectangle; copyMode, scaleMode : LONGINT);
  491. BEGIN
  492. Rectangles.MoveRel(dr, dx, dy);
  493. RasterScale.Scale(src, sr, img, dr, clipRect, copyMode, scaleMode);
  494. END ScaleImage;
  495. END BufferCanvas;
  496. VAR imgCache : Kernel.FinalizedCollection;
  497. searchName : ARRAY 128 OF CHAR;
  498. foundImg : Image;
  499. defaultFont : Font;
  500. fontManager : FontManager;
  501. fallbackFonts* : ARRAY 5 OF Font;
  502. nofFallbackFonts : LONGINT;
  503. CONST
  504. AlignLeft* = 0; AlignCenter* = 1; AlignRight* = 2;
  505. AlignTop* = 0; AlignBottom* = 2;
  506. PROCEDURE Max(a, b:LONGINT):LONGINT;
  507. BEGIN
  508. IF a>b THEN RETURN a ELSE RETURN b END
  509. END Max;
  510. (* Tool Functions *)
  511. PROCEDURE MakeRectangle*(l, t, r, b: LONGINT):Rectangle;
  512. VAR result : Rectangle;
  513. BEGIN
  514. result.l := l; result.t := t; result.r := r; result.b := b; RETURN result
  515. END MakeRectangle;
  516. PROCEDURE ColorToRGBA*(color : Color; VAR r, g, b, a : LONGINT);
  517. BEGIN
  518. r := (color DIV 1000000H) MOD 100H;
  519. g := (color DIV 10000H) MOD 100H;
  520. b := (color DIV 100H) MOD 100H;
  521. a := color MOD 100H
  522. END ColorToRGBA;
  523. PROCEDURE RGBAToColor*(r, g, b, a: LONGINT): Color;
  524. BEGIN
  525. RETURN r * 1000000H + g * 10000H + b * 100H + a
  526. END RGBAToColor;
  527. PROCEDURE Dark*(color:Color):Color;
  528. BEGIN
  529. RETURN LONGINT((color MOD 100000000H+0FFH) DIV 2);
  530. END Dark;
  531. PROCEDURE Light*(color:Color):Color;
  532. BEGIN
  533. RETURN LONGINT((color MOD 100000000H + 0FFFFFFFFH MOD 100000000H) DIV 2);
  534. END Light;
  535. PROCEDURE CheckImage(obj: ANY; VAR cont: BOOLEAN);
  536. BEGIN
  537. IF obj IS Image THEN
  538. IF obj(Image).key # NIL THEN
  539. IF obj(Image).key^ = searchName THEN
  540. foundImg := obj(Image);
  541. cont := FALSE
  542. END
  543. END
  544. END
  545. END CheckImage;
  546. PROCEDURE GetExtension (CONST name : ARRAY OF CHAR;VAR ext: ARRAY OF CHAR);
  547. VAR i, j: LONGINT; ch: CHAR;
  548. BEGIN
  549. i := 0; j := 0;
  550. WHILE name[i] # 0X DO
  551. IF name[i] = "." THEN j := i+1 END;
  552. INC(i)
  553. END;
  554. i := 0;
  555. REPEAT
  556. ch := name[j]; ext[i] := ch; INC(i); INC(j)
  557. UNTIL (ch = 0X) OR (i = LEN(ext));
  558. ext[i-1] := 0X
  559. END GetExtension;
  560. (** loads an image and returns a BGRA8888 bitmap if successful, NIL otherwise.
  561. If shared is TRUE, the image will not be reloaded if it is already in memory.
  562. *)
  563. PROCEDURE LoadImage*(CONST name : ARRAY OF CHAR; shared : BOOLEAN): Image;
  564. VAR img : Image;
  565. res, w, h, x : LONGINT;
  566. decoder : Codecs.ImageDecoder;
  567. in : Streams.Reader;
  568. ext : ARRAY 16 OF CHAR;
  569. BEGIN
  570. IF name = "" THEN RETURN NIL END;
  571. BEGIN {EXCLUSIVE}
  572. IF shared THEN
  573. foundImg := NIL; COPY(name, searchName);
  574. imgCache.Enumerate(CheckImage);
  575. IF foundImg # NIL THEN RETURN foundImg END
  576. END;
  577. END;
  578. GetExtension(name, ext);
  579. Strings.UpperCase(ext);
  580. decoder := Codecs.GetImageDecoder(ext);
  581. IF decoder = NIL THEN
  582. KernelLog.String("No decoder found for "); KernelLog.String(ext); KernelLog.Ln;
  583. RETURN NIL
  584. END;
  585. in := Codecs.OpenInputStream(name);
  586. IF in # NIL THEN
  587. decoder.Open(in, res);
  588. IF res = 0 THEN
  589. decoder.GetImageInfo(w, h, x, x);
  590. NEW(img);
  591. Raster.Create(img, w, h, Raster.BGRA8888);
  592. decoder.Render(img);
  593. NEW(img.key, LEN(name)); COPY(name, img.key^);
  594. IF shared THEN imgCache.Add(img, NIL) END
  595. END
  596. END;
  597. RETURN img
  598. END LoadImage;
  599. PROCEDURE StoreImage*(img : Raster.Image; CONST name : ARRAY OF CHAR; VAR res : LONGINT);
  600. VAR encoder : Codecs.ImageEncoder;
  601. f : Files.File;
  602. w : Files.Writer;
  603. ext : ARRAY 16 OF CHAR;
  604. BEGIN
  605. res := -1;
  606. GetExtension(name, ext);
  607. Strings.UpperCase(ext);
  608. encoder := Codecs.GetImageEncoder(ext);
  609. IF encoder = NIL THEN
  610. KernelLog.String("No encoder found for "); KernelLog.String(ext); KernelLog.Ln;
  611. RETURN
  612. END;
  613. f := Files.New(name);
  614. IF f # NIL THEN
  615. Files.OpenWriter(w, f, 0);
  616. END;
  617. IF w # NIL THEN
  618. encoder.Open(w);
  619. encoder.WriteImage(img, res);
  620. Files.Register(f);
  621. END
  622. END StoreImage;
  623. (** Draw an UTF8 String in a rectangle *)
  624. PROCEDURE DrawStringInRect*(canvas : Canvas; rect : Rectangle; wrap : BOOLEAN; hAlign, vAlign : LONGINT;
  625. CONST text : ARRAY OF CHAR);
  626. VAR tw, th, xPos, yPos : LONGINT;
  627. font : Font;
  628. BEGIN
  629. font := canvas.GetFont();
  630. IF font # NIL THEN
  631. font.GetStringSize(text, tw, th);
  632. END;
  633. xPos := rect.l; yPos := rect.t + font.GetAscent();
  634. IF ~wrap THEN
  635. IF hAlign = AlignCenter THEN xPos := ((rect.l + rect.r) - tw) DIV 2
  636. ELSIF hAlign = AlignRight THEN xPos := rect.r - tw
  637. END;
  638. IF vAlign = AlignCenter THEN yPos := (rect.t + rect.b - font.GetDescent() - font.GetAscent() ) DIV 2 + font.GetAscent() ;
  639. ELSIF vAlign = AlignBottom THEN yPos := rect.b - font.GetDescent();
  640. END;
  641. canvas.DrawString(xPos, yPos, text);
  642. ELSE
  643. (* not implemented *)
  644. END
  645. END DrawStringInRect;
  646. PROCEDURE GenCanvas*(img:Raster.Image):BufferCanvas;
  647. VAR c:BufferCanvas;
  648. BEGIN
  649. NEW(c,img); RETURN c
  650. END GenCanvas;
  651. PROCEDURE InstallDefaultFont*(f : Font);
  652. BEGIN { EXCLUSIVE }
  653. defaultFont := f;
  654. fallbackFonts[0] := defaultFont
  655. END InstallDefaultFont;
  656. PROCEDURE GetDefaultFont*() : Font;
  657. BEGIN { EXCLUSIVE }
  658. AWAIT(defaultFont # NIL);
  659. RETURN defaultFont
  660. END GetDefaultFont;
  661. PROCEDURE InstallFontManager*(fm : FontManager);
  662. BEGIN { EXCLUSIVE }
  663. fontManager := fm;
  664. IF fontManager # NIL THEN
  665. fallbackFonts[1] := fontManager.GetFont("Single", 20, {});
  666. END
  667. END InstallFontManager;
  668. PROCEDURE GetFont*(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : Font;
  669. VAR f : Font;
  670. BEGIN { EXCLUSIVE }
  671. f := NIL;
  672. IF fontManager # NIL THEN f := fontManager.GetFont(name, size, style) END;
  673. IF f = NIL THEN AWAIT(defaultFont # NIL); f := defaultFont END;
  674. RETURN f
  675. END GetFont;
  676. (** Render the fallback case of the character char to canvas at x, y (baseline) *)
  677. PROCEDURE FBRenderChar*(canvas : Canvas ; x, y : REAL; char : Char32);
  678. VAR i, w, h : LONGINT; f : Font; found : BOOLEAN; str : ARRAY 16 OF CHAR; r: Rectangles.Rectangle;
  679. BEGIN
  680. i := 0; found := FALSE;
  681. WHILE ~found & (i < nofFallbackFonts) DO
  682. f := fallbackFonts[i];
  683. IF (f # NIL) & f.HasChar(char) THEN found := TRUE END;
  684. INC(i)
  685. END;
  686. IF f # NIL THEN f.RenderChar(canvas, x, y, char)
  687. ELSE
  688. f := GetDefaultFont();
  689. Strings.IntToStr(char,str); Strings.Concat("U", str, str);
  690. f.GetStringSize(str, w, h);
  691. r := Rectangles.MakeRect(ENTIER(x), ENTIER(y) - f.ascent, ENTIER(x) + w, ENTIER(y) + f.descent);
  692. canvas.Fill(r, LONGINT(0CCCC00FFH), ModeCopy);
  693. f.RenderString(canvas, x, y, str)
  694. END
  695. END FBRenderChar;
  696. (** return the fallback spacing of character code *)
  697. PROCEDURE FBGetGlyphSpacings*(code : LONGINT; VAR glyphSpacings : GlyphSpacings);
  698. VAR i : LONGINT; f : Font; found : BOOLEAN; str : ARRAY 16 OF CHAR;
  699. BEGIN
  700. i := 0; found := FALSE;
  701. WHILE ~found & (i < nofFallbackFonts) DO
  702. f := fallbackFonts[i];
  703. IF (f # NIL) & f.HasChar(code) THEN found := TRUE END;
  704. INC(i)
  705. END;
  706. IF f # NIL THEN f.GetGlyphSpacings(code, glyphSpacings)
  707. ELSE
  708. f := GetDefaultFont();
  709. Strings.IntToStr(code, str); Strings.Concat("U", str, str);
  710. glyphSpacings.bearing := Rectangles.MakeRect(0, 0, 0, 0);
  711. f.GetStringSize(str, glyphSpacings.width, glyphSpacings.height);
  712. glyphSpacings.ascent := f.ascent; glyphSpacings.descent := f.descent;
  713. glyphSpacings.dx := 0; glyphSpacings.dy := 0
  714. END
  715. END FBGetGlyphSpacings;
  716. (** Tools *)
  717. (* Return true if the alpha value at pos x, y in img is >= threshold. Returns false if x, y are out of image *)
  718. PROCEDURE IsBitmapHit*(x, y, threshold: LONGINT; img: Raster.Image) : BOOLEAN;
  719. VAR pix : Raster.Pixel;
  720. mode : Raster.Mode;
  721. BEGIN
  722. IF (img # NIL) & (x >= 0) & (y >= 0) & (x < img.width) & (y < img.height) THEN
  723. Raster.InitMode(mode, Raster.srcCopy);
  724. Raster.Get(img, x, y, pix, mode);
  725. RETURN (ORD(pix[Raster.a]) >= threshold)
  726. ELSE RETURN FALSE
  727. END
  728. END IsBitmapHit;
  729. PROCEDURE IsScaledBitmapHit*(x,y,w,h,threshold: LONGINT; img: Raster.Image): BOOLEAN;
  730. BEGIN
  731. RETURN IsBitmapHit(x*img.width DIV w, y*img.height DIV h, threshold,img);
  732. END IsScaledBitmapHit;
  733. PROCEDURE ClearCache*;
  734. BEGIN
  735. imgCache.Clear;
  736. END ClearCache;
  737. BEGIN
  738. nofFallbackFonts := 3;
  739. NEW(imgCache)
  740. END WMGraphics.