WMGraphics.Mod 25 KB

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