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. Rectangles.Normalize(rect);
  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. Rectangles.Normalize(r);
  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. Rectangles.Normalize(rect);
  341. (* convert to super coordinates *)
  342. Rectangles.MoveRel(rect, dx, dy);
  343. IF ClipRect IN clipMode THEN Rectangles.ClipRect(rect, clipRect) END;
  344. Rectangles.ClipRect(rect, limits);
  345. IF ~Rectangles.RectEmpty(rect) THEN
  346. Raster.SetRGBA(pix, ((color DIV 65536) DIV 256) MOD 256, (color DIV 65536) MOD 256,
  347. (color DIV 256) MOD 256, color MOD 256);
  348. IF mode = ModeCopy THEN Raster.InitMode(rm, Raster.srcCopy) ELSE Raster.InitMode(rm, Raster.srcOverDst) END;
  349. Raster.Fill(SELF.img, rect.l, rect.t, rect.r, rect.b, pix, rm);
  350. END
  351. END Fill;
  352. (* Polygon filling *)
  353. (** fill a polygon given by points *)
  354. PROCEDURE FillPolygonFlat*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; color : Color; mode : LONGINT);
  355. VAR i : LONGINT;
  356. BEGIN
  357. IF nofPoints < 3 THEN RETURN END;
  358. ASSERT(nofPoints <= LEN(points));
  359. ClearHeap;
  360. FOR i := 1 TO nofPoints - 1 DO AddLine(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y) END;
  361. AddLine(points[nofPoints - 1].x, points[nofPoints - 1].y, points[0].x, points[0].y);
  362. FillFlat(color, mode)
  363. END FillPolygonFlat;
  364. (** fill a polygon given by points *)
  365. PROCEDURE FillPolygonCB*(CONST points : ARRAY OF Point2d; nofPoints : LONGINT; callBack : FillLineCallBack);
  366. VAR i : LONGINT;
  367. BEGIN
  368. IF nofPoints < 3 THEN RETURN END;
  369. ASSERT(nofPoints <= LEN(points));
  370. ClearHeap;
  371. FOR i := 1 TO nofPoints - 1 DO AddLine(points[i - 1].x, points[i - 1].y, points[i].x, points[i].y) END;
  372. AddLine(points[nofPoints - 1].x, points[nofPoints - 1].y, points[0].x, points[0].y);
  373. FillCB(callBack)
  374. END FillPolygonCB;
  375. PROCEDURE ClearHeap;
  376. VAR i : LONGINT;
  377. BEGIN
  378. topHeap := 0;
  379. FOR i := 0 TO height - 1 DO edges[i] := 0 END;
  380. IF fillHeap = NIL THEN NEW(fillHeap, 1024); heapSize := 1024 END
  381. END ClearHeap;
  382. PROCEDURE NewFillPos(pos : LONGINT) : LONGINT;
  383. VAR newHeap : FillHeap;
  384. i : LONGINT;
  385. BEGIN
  386. INC(topHeap);
  387. IF topHeap >= heapSize THEN (* grow heap *)
  388. NEW(newHeap, heapSize * 2);
  389. FOR i := 0 TO heapSize - 1 DO newHeap[i] := fillHeap[i] END;
  390. heapSize := heapSize * 2;
  391. fillHeap := newHeap
  392. END;
  393. fillHeap[topHeap].pos := pos;
  394. fillHeap[topHeap].next := 0;
  395. RETURN topHeap
  396. END NewFillPos;
  397. PROCEDURE AddIntersection(y, pos : LONGINT);
  398. VAR new, cur : LONGINT;
  399. BEGIN
  400. IF (y < 0) OR (y >= height) THEN RETURN END;
  401. new := NewFillPos(pos);
  402. IF edges[y] = 0 THEN edges[y] := new
  403. ELSE
  404. cur := edges[y];
  405. IF fillHeap[cur].pos > pos THEN
  406. fillHeap[new].next := cur;
  407. edges[y] := new
  408. ELSE
  409. WHILE (fillHeap[cur].next # 0) & (fillHeap[fillHeap[cur].next].pos < pos) DO cur := fillHeap[cur].next END;
  410. fillHeap[new].next := fillHeap[cur].next;
  411. fillHeap[cur].next := new
  412. END;
  413. END;
  414. END AddIntersection;
  415. PROCEDURE AddLine(x0, y0, x1, y1 : LONGINT);
  416. VAR t, xi, xf, mi, mf, dt2 : LONGINT ;
  417. BEGIN
  418. IF (y0 = y1) THEN RETURN END;
  419. IF y0 > y1 THEN t := y0; y0 := y1; y1 := t; t := x0; x0 := x1; x1 := t END;
  420. xi := x0; xf := y0 - y1; mi := (x1 - x0) DIV (y1 - y0); mf := 2 * ( (x1 - x0) MOD (y1 - y0)); dt2 := 2 * (y1 - y0);
  421. FOR t := y0 TO y1 - 1 DO
  422. AddIntersection(t, xi);
  423. INC(xi, mi); INC(xf, mf);
  424. IF xf > 0 THEN INC(xi); DEC(xf, dt2) END
  425. END
  426. END AddLine;
  427. PROCEDURE FillFlat(color, mode : LONGINT);
  428. VAR i, sp, cur : LONGINT;
  429. in : BOOLEAN;
  430. BEGIN
  431. FOR i := 0 TO height - 1 DO
  432. cur := edges[i];
  433. in := FALSE;
  434. WHILE cur # 0 DO
  435. in := ~in;
  436. IF in THEN sp := fillHeap[cur].pos
  437. ELSE Fill(Rectangles.MakeRect(sp, i, fillHeap[cur].pos, i + 1), color, mode)
  438. END;
  439. cur := fillHeap[cur].next
  440. END
  441. END
  442. END FillFlat;
  443. PROCEDURE FillCB(cb : FillLineCallBack);
  444. VAR i, sp, cur : LONGINT;
  445. in : BOOLEAN;
  446. BEGIN
  447. FOR i := 0 TO height - 1 DO
  448. cur := edges[i];
  449. in := FALSE;
  450. WHILE cur # 0 DO
  451. in := ~in;
  452. IF in THEN sp := fillHeap[cur].pos
  453. ELSE cb(SELF, i, sp, fillHeap[cur].pos)
  454. END;
  455. cur := fillHeap[cur].next
  456. END
  457. END
  458. END FillCB;
  459. PROCEDURE DrawImage*(x, y: LONGINT; img: Raster.Image; mode : LONGINT);
  460. VAR imgBounds : Rectangle;
  461. rm : Raster.Mode;
  462. BEGIN
  463. IF img = NIL THEN RETURN END;
  464. imgBounds := MakeRectangle(0, 0, img.width, img.height);
  465. (* to super coordinates *)
  466. Rectangles.MoveRel(imgBounds, x + dx, y + dy);
  467. IF ClipRect IN clipMode THEN Rectangles.ClipRect(imgBounds, clipRect) END;
  468. Rectangles.ClipRect(imgBounds, limits);
  469. IF ~Rectangles.RectEmpty(imgBounds) THEN
  470. IF mode = ModeCopy THEN Raster.InitMode(rm, Raster.srcCopy) ELSE Raster.InitMode(rm, Raster.srcOverDst) END;
  471. Raster.SetRGBA(rm.col, (color DIV 1000000H) MOD 100H, (color DIV 10000H) MOD 100H,
  472. (color DIV 100H) MOD 100H, color MOD 100H);
  473. IF imgBounds.l - (x + dx) < 0 THEN
  474. KernelLog.String("Error...");
  475. KernelLog.String("x + dx = "); KernelLog.Int(x + dx, 4); KernelLog.Ln;
  476. KernelLog.String("x = "); KernelLog.Int(x, 4); KernelLog.Ln;
  477. KernelLog.String("dx = "); KernelLog.Int(dx, 4); KernelLog.Ln;
  478. KernelLog.String("clip = "); KernelLog.Int(clipRect.l, 4); KernelLog.Int(clipRect.t, 4);
  479. KernelLog.Int(clipRect.r, 4); KernelLog.Int(clipRect.b, 4);KernelLog.Ln;
  480. KernelLog.String("imgBounds = ");
  481. KernelLog.Int(imgBounds.l, 4); KernelLog.Int(imgBounds.t, 4); KernelLog.Int(imgBounds.r, 4); KernelLog.Int(imgBounds.b, 4);KernelLog.Ln;
  482. KernelLog.String("limits = "); KernelLog.Int(limits.l, 4); KernelLog.Int(limits.t, 4);
  483. KernelLog.Int(limits.r, 4); KernelLog.Int(limits.b, 4);KernelLog.Ln;
  484. RETURN
  485. END;
  486. Raster.Copy(img, SELF.img, imgBounds.l - (x + dx), imgBounds.t - (y + dy),
  487. imgBounds.r - imgBounds.l + (imgBounds.l - (x + dx)), imgBounds.b - imgBounds.t + (imgBounds.t - (y + dy)),
  488. imgBounds.l, imgBounds.t, rm);
  489. END;
  490. END DrawImage;
  491. PROCEDURE ScaleImage*(src : Raster.Image; sr , dr : Rectangle; copyMode, scaleMode : LONGINT);
  492. BEGIN
  493. Rectangles.MoveRel(dr, dx, dy);
  494. RasterScale.Scale(src, sr, img, dr, clipRect, copyMode, scaleMode);
  495. END ScaleImage;
  496. END BufferCanvas;
  497. VAR imgCache : Kernel.FinalizedCollection;
  498. searchName : ARRAY 128 OF CHAR;
  499. foundImg : Image;
  500. defaultFont : Font;
  501. fontManager : FontManager;
  502. fallbackFonts* : ARRAY 5 OF Font;
  503. nofFallbackFonts : LONGINT;
  504. CONST
  505. AlignLeft* = 0; AlignCenter* = 1; AlignRight* = 2;
  506. AlignTop* = 0; AlignBottom* = 2;
  507. PROCEDURE Max(a, b:LONGINT):LONGINT;
  508. BEGIN
  509. IF a>b THEN RETURN a ELSE RETURN b END
  510. END Max;
  511. (* Tool Functions *)
  512. PROCEDURE MakeRectangle*(l, t, r, b: LONGINT):Rectangle;
  513. VAR result : Rectangle;
  514. BEGIN
  515. result.l := l; result.t := t; result.r := r; result.b := b; RETURN result
  516. END MakeRectangle;
  517. PROCEDURE ColorToRGBA*(color : Color; VAR r, g, b, a : LONGINT);
  518. BEGIN
  519. r := (color DIV 1000000H) MOD 100H;
  520. g := (color DIV 10000H) MOD 100H;
  521. b := (color DIV 100H) MOD 100H;
  522. a := color MOD 100H
  523. END ColorToRGBA;
  524. PROCEDURE RGBAToColor*(r, g, b, a: LONGINT): Color;
  525. BEGIN
  526. RETURN r * 1000000H + g * 10000H + b * 100H + a
  527. END RGBAToColor;
  528. PROCEDURE Dark*(color:Color):Color;
  529. BEGIN
  530. RETURN LONGINT((color MOD 100000000H+0FFH) DIV 2);
  531. END Dark;
  532. PROCEDURE Light*(color:Color):Color;
  533. BEGIN
  534. RETURN LONGINT((color MOD 100000000H + 0FFFFFFFFH MOD 100000000H) DIV 2);
  535. END Light;
  536. PROCEDURE CheckImage(obj: ANY; VAR cont: BOOLEAN);
  537. BEGIN
  538. IF obj IS Image THEN
  539. IF obj(Image).key # NIL THEN
  540. IF obj(Image).key^ = searchName THEN
  541. foundImg := obj(Image);
  542. cont := FALSE
  543. END
  544. END
  545. END
  546. END CheckImage;
  547. PROCEDURE GetExtension (CONST name : ARRAY OF CHAR;VAR ext: ARRAY OF CHAR);
  548. VAR i, j: LONGINT; ch: CHAR;
  549. BEGIN
  550. i := 0; j := 0;
  551. WHILE name[i] # 0X DO
  552. IF name[i] = "." THEN j := i+1 END;
  553. INC(i)
  554. END;
  555. i := 0;
  556. REPEAT
  557. ch := name[j]; ext[i] := ch; INC(i); INC(j)
  558. UNTIL (ch = 0X) OR (i = LEN(ext));
  559. ext[i-1] := 0X
  560. END GetExtension;
  561. (** loads an image and returns a BGRA8888 bitmap if successful, NIL otherwise.
  562. If shared is TRUE, the image will not be reloaded if it is already in memory.
  563. *)
  564. PROCEDURE LoadImage*(CONST name : ARRAY OF CHAR; shared : BOOLEAN): Image;
  565. VAR img : Image;
  566. res, w, h, x : LONGINT;
  567. decoder : Codecs.ImageDecoder;
  568. in : Streams.Reader;
  569. ext : ARRAY 16 OF CHAR;
  570. BEGIN
  571. IF name = "" THEN RETURN NIL END;
  572. BEGIN {EXCLUSIVE}
  573. IF shared THEN
  574. foundImg := NIL; COPY(name, searchName);
  575. imgCache.Enumerate(CheckImage);
  576. IF foundImg # NIL THEN RETURN foundImg END
  577. END;
  578. END;
  579. GetExtension(name, ext);
  580. Strings.UpperCase(ext);
  581. decoder := Codecs.GetImageDecoder(ext);
  582. IF decoder = NIL THEN
  583. KernelLog.String("No decoder found for "); KernelLog.String(ext); KernelLog.Ln;
  584. RETURN NIL
  585. END;
  586. in := Codecs.OpenInputStream(name);
  587. IF in # NIL THEN
  588. decoder.Open(in, res);
  589. IF res = 0 THEN
  590. decoder.GetImageInfo(w, h, x, x);
  591. NEW(img);
  592. Raster.Create(img, w, h, Raster.BGRA8888);
  593. decoder.Render(img);
  594. NEW(img.key, LEN(name)); COPY(name, img.key^);
  595. IF shared THEN imgCache.Add(img, NIL) END
  596. END
  597. END;
  598. RETURN img
  599. END LoadImage;
  600. PROCEDURE StoreImage*(img : Raster.Image; CONST name : ARRAY OF CHAR; VAR res : LONGINT);
  601. VAR encoder : Codecs.ImageEncoder;
  602. f : Files.File;
  603. w : Files.Writer;
  604. ext : ARRAY 16 OF CHAR;
  605. BEGIN
  606. res := -1;
  607. GetExtension(name, ext);
  608. Strings.UpperCase(ext);
  609. encoder := Codecs.GetImageEncoder(ext);
  610. IF encoder = NIL THEN
  611. KernelLog.String("No encoder found for "); KernelLog.String(ext); KernelLog.Ln;
  612. RETURN
  613. END;
  614. f := Files.New(name);
  615. IF f # NIL THEN
  616. Files.OpenWriter(w, f, 0);
  617. END;
  618. IF w # NIL THEN
  619. encoder.Open(w);
  620. encoder.WriteImage(img, res);
  621. Files.Register(f);
  622. END
  623. END StoreImage;
  624. (** Draw an UTF8 String in a rectangle *)
  625. PROCEDURE DrawStringInRect*(canvas : Canvas; rect : Rectangle; wrap : BOOLEAN; hAlign, vAlign : LONGINT;
  626. CONST text : ARRAY OF CHAR);
  627. VAR tw, th, xPos, yPos : LONGINT;
  628. font : Font;
  629. BEGIN
  630. font := canvas.GetFont();
  631. IF font # NIL THEN
  632. font.GetStringSize(text, tw, th);
  633. END;
  634. xPos := rect.l; yPos := rect.t + font.GetAscent();
  635. IF ~wrap THEN
  636. IF hAlign = AlignCenter THEN xPos := ((rect.l + rect.r) - tw) DIV 2
  637. ELSIF hAlign = AlignRight THEN xPos := rect.r - tw
  638. END;
  639. IF vAlign = AlignCenter THEN yPos := (rect.t + rect.b - font.GetDescent() - font.GetAscent() ) DIV 2 + font.GetAscent() ;
  640. ELSIF vAlign = AlignBottom THEN yPos := rect.b - font.GetDescent();
  641. END;
  642. canvas.DrawString(xPos, yPos, text);
  643. ELSE
  644. (* not implemented *)
  645. END
  646. END DrawStringInRect;
  647. PROCEDURE GenCanvas*(img:Raster.Image):BufferCanvas;
  648. VAR c:BufferCanvas;
  649. BEGIN
  650. NEW(c,img); RETURN c
  651. END GenCanvas;
  652. PROCEDURE InstallDefaultFont*(f : Font);
  653. BEGIN { EXCLUSIVE }
  654. defaultFont := f;
  655. fallbackFonts[0] := defaultFont
  656. END InstallDefaultFont;
  657. PROCEDURE GetDefaultFont*() : Font;
  658. BEGIN { EXCLUSIVE }
  659. AWAIT(defaultFont # NIL);
  660. RETURN defaultFont
  661. END GetDefaultFont;
  662. PROCEDURE InstallFontManager*(fm : FontManager);
  663. BEGIN { EXCLUSIVE }
  664. fontManager := fm;
  665. IF fontManager # NIL THEN
  666. fallbackFonts[1] := fontManager.GetFont("Single", 20, {});
  667. END
  668. END InstallFontManager;
  669. PROCEDURE GetFont*(CONST name : ARRAY OF CHAR; size : LONGINT; style : SET) : Font;
  670. VAR f : Font;
  671. BEGIN { EXCLUSIVE }
  672. f := NIL;
  673. IF fontManager # NIL THEN f := fontManager.GetFont(name, size, style) END;
  674. IF f = NIL THEN AWAIT(defaultFont # NIL); f := defaultFont END;
  675. RETURN f
  676. END GetFont;
  677. (** Render the fallback case of the character char to canvas at x, y (baseline) *)
  678. PROCEDURE FBRenderChar*(canvas : Canvas ; x, y : REAL; char : Char32);
  679. VAR i, w, h : LONGINT; f : Font; found : BOOLEAN; str : ARRAY 16 OF CHAR; r: Rectangles.Rectangle;
  680. BEGIN
  681. i := 0; found := FALSE;
  682. WHILE ~found & (i < nofFallbackFonts) DO
  683. f := fallbackFonts[i];
  684. IF (f # NIL) & f.HasChar(char) THEN found := TRUE END;
  685. INC(i)
  686. END;
  687. IF f # NIL THEN f.RenderChar(canvas, x, y, char)
  688. ELSE
  689. f := GetDefaultFont();
  690. Strings.IntToStr(char,str); Strings.Concat("U", str, str);
  691. f.GetStringSize(str, w, h);
  692. r := Rectangles.MakeRect(ENTIER(x), ENTIER(y) - f.ascent, ENTIER(x) + w, ENTIER(y) + f.descent);
  693. canvas.Fill(r, LONGINT(0CCCC00FFH), ModeCopy);
  694. f.RenderString(canvas, x, y, str)
  695. END
  696. END FBRenderChar;
  697. (** return the fallback spacing of character code *)
  698. PROCEDURE FBGetGlyphSpacings*(code : LONGINT; VAR glyphSpacings : GlyphSpacings);
  699. VAR i : LONGINT; f : Font; found : BOOLEAN; str : ARRAY 16 OF CHAR;
  700. BEGIN
  701. i := 0; found := FALSE;
  702. WHILE ~found & (i < nofFallbackFonts) DO
  703. f := fallbackFonts[i];
  704. IF (f # NIL) & f.HasChar(code) THEN found := TRUE END;
  705. INC(i)
  706. END;
  707. IF f # NIL THEN f.GetGlyphSpacings(code, glyphSpacings)
  708. ELSE
  709. f := GetDefaultFont();
  710. Strings.IntToStr(code, str); Strings.Concat("U", str, str);
  711. glyphSpacings.bearing := Rectangles.MakeRect(0, 0, 0, 0);
  712. f.GetStringSize(str, glyphSpacings.width, glyphSpacings.height);
  713. glyphSpacings.ascent := f.ascent; glyphSpacings.descent := f.descent;
  714. glyphSpacings.dx := 0; glyphSpacings.dy := 0
  715. END
  716. END FBGetGlyphSpacings;
  717. (** Tools *)
  718. (* Return true if the alpha value at pos x, y in img is >= threshold. Returns false if x, y are out of image *)
  719. PROCEDURE IsBitmapHit*(x, y, threshold: LONGINT; img: Raster.Image) : BOOLEAN;
  720. VAR pix : Raster.Pixel;
  721. mode : Raster.Mode;
  722. BEGIN
  723. IF (img # NIL) & (x >= 0) & (y >= 0) & (x < img.width) & (y < img.height) THEN
  724. Raster.InitMode(mode, Raster.srcCopy);
  725. Raster.Get(img, x, y, pix, mode);
  726. RETURN (ORD(pix[Raster.a]) >= threshold)
  727. ELSE RETURN FALSE
  728. END
  729. END IsBitmapHit;
  730. PROCEDURE IsScaledBitmapHit*(x,y,w,h,threshold: LONGINT; img: Raster.Image): BOOLEAN;
  731. BEGIN
  732. RETURN IsBitmapHit(x*img.width DIV w, y*img.height DIV h, threshold,img);
  733. END IsScaledBitmapHit;
  734. PROCEDURE ClearCache*;
  735. BEGIN
  736. imgCache.Clear;
  737. END ClearCache;
  738. BEGIN
  739. nofFallbackFonts := 3;
  740. NEW(imgCache)
  741. END WMGraphics.