GR32_Text_VCL.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. unit GR32_Text_VCL;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Vectorial Polygon Rasterizer for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Mattias Andersson <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$I GR32.inc}
  35. uses
  36. Windows, Types, GR32, GR32_Paths, Math;
  37. procedure TextToPath(Font: HFONT; Path: TCustomPath; const ARect: TFloatRect; const Text: string; Flags: Cardinal = 0);
  38. function TextToPolyPolygon(Font: HFONT; const ARect: TFloatRect; const Text: string; Flags: Cardinal = 0): TArrayOfArrayOfFloatPoint;
  39. function MeasureTextDC(DC: HDC; const ARect: TFloatRect; const Text: string; Flags: Cardinal = 0): TFloatRect; overload;
  40. function MeasureText(Font: HFONT; const ARect: TFloatRect; const Text: string; Flags: Cardinal = 0): TFloatRect;
  41. type
  42. TTextHinting = (thNone, thNoHorz, thHinting);
  43. TKerningPairArray = array [0..0] of TKerningPair;
  44. PKerningPairArray = ^TKerningPairArray;
  45. procedure SetHinting(Value: TTextHinting);
  46. function GetHinting: TTextHinting;
  47. const
  48. DT_LEFT = 0; //See also Window's DrawText() flags ...
  49. DT_CENTER = 1; //http://msdn.microsoft.com/en-us/library/ms901121.aspx
  50. DT_RIGHT = 2;
  51. DT_VCENTER = 4;
  52. DT_BOTTOM = 8;
  53. DT_WORDBREAK = $10;
  54. DT_SINGLELINE = $20;
  55. DT_NOCLIP = $100;
  56. DT_JUSTIFY = 3; //Graphics32 additions ...
  57. DT_HORZ_ALIGN_MASK = 3;
  58. implementation
  59. uses
  60. {$IFDEF USESTACKALLOC}
  61. GR32_LowLevel,
  62. {$ENDIF}
  63. SysUtils;
  64. var
  65. UseHinting: Boolean;
  66. HorzStretch: Integer; // stretching factor when calling GetGlyphOutline()
  67. HorzStretch_Inv: Single;
  68. VertFlip_mat2: TMat2;
  69. const
  70. GGO_UNHINTED = $0100;
  71. GGODefaultFlags: array [Boolean] of Integer = (GGO_NATIVE or GGO_UNHINTED, GGO_NATIVE);
  72. TT_PRIM_CSPLINE = 3;
  73. MaxSingle = 3.4e+38;
  74. // import GetKerningPairs from gdi32 library
  75. function GetKerningPairs(DC: HDC; Count: DWORD; P: PKerningPair): DWORD;
  76. stdcall; external gdi32 name 'GetKerningPairs';
  77. function PointFXtoPointF(const Point: tagPointFX): TFloatPoint; {$IFDEF UseInlining} inline; {$ENDIF}
  78. begin
  79. Result.X := Point.X.Value + Point.X.Fract * FixedToFloat;
  80. Result.Y := Point.Y.Value + Point.Y.Fract * FixedToFloat;
  81. end;
  82. {$IFDEF USESTACKALLOC}
  83. {$W+}
  84. {$ENDIF}
  85. function GlyphOutlineToPath(Handle: HDC; Path: TCustomPath;
  86. DstX, MaxX, DstY: Single;
  87. const Glyph: Integer; out Metrics: TGlyphMetrics): Boolean;
  88. var
  89. I, K, S: Integer;
  90. Res: DWORD;
  91. GlyphMemPtr, BufferPtr: PTTPolygonHeader;
  92. CurvePtr: PTTPolyCurve;
  93. P1, P2, P3: TFloatPoint;
  94. begin
  95. Result := False;
  96. Res := GetGlyphOutline(Handle, Glyph, GGODefaultFlags[UseHinting], Metrics,
  97. 0, nil, VertFlip_mat2);
  98. if (Res = 0) then Exit;
  99. Result := DstX + Metrics.gmCellIncX <= MaxX;
  100. if not Result or not Assigned(Path) then Exit;
  101. {$IFDEF USESTACKALLOC}
  102. GlyphMemPtr := StackAlloc(Res);
  103. {$ELSE}
  104. GetMem(GlyphMemPtr, Res);
  105. {$ENDIF}
  106. BufferPtr := GlyphMemPtr;
  107. Res := GetGlyphOutline(Handle, Glyph, GGODefaultFlags[UseHinting], Metrics,
  108. Res, BufferPtr, VertFlip_mat2);
  109. if (Res = GDI_ERROR) or (BufferPtr^.dwType <> TT_POLYGON_TYPE) then
  110. begin
  111. {$IFDEF USESTACKALLOC}
  112. StackFree(GlyphMemPtr);
  113. {$ELSE}
  114. FreeMem(GlyphMemPtr);
  115. {$ENDIF}
  116. Exit;
  117. end;
  118. // Batch each glyph so we're sure that the polygons are rendered as a whole (no pun...)
  119. // and not as individual independent polygons.
  120. // We're doing this here for completeness but since the path will also be batched at
  121. // an outer level it isn't really necessary here.
  122. Path.BeginUpdate;
  123. while Res > 0 do
  124. begin
  125. S := BufferPtr.cb - SizeOf(TTTPolygonHeader);
  126. {$IFDEF HAS_NATIVEINT}
  127. NativeInt(CurvePtr) := NativeInt(BufferPtr) + SizeOf(TTTPolygonHeader);
  128. {$ELSE}
  129. Integer(CurvePtr) := Integer(BufferPtr) + SizeOf(TTTPolygonHeader);
  130. {$ENDIF}
  131. P1 := PointFXtoPointF(BufferPtr.pfxStart);
  132. Path.MoveTo(P1.X + DstX, P1.Y + DstY);
  133. while S > 0 do
  134. begin
  135. case CurvePtr.wType of
  136. TT_PRIM_LINE:
  137. for I := 0 to CurvePtr.cpfx - 1 do
  138. begin
  139. P1 := PointFXtoPointF(CurvePtr.apfx[I]);
  140. Path.LineTo(P1.X + DstX, P1.Y + DstY);
  141. end;
  142. TT_PRIM_QSPLINE:
  143. begin
  144. for I := 0 to CurvePtr.cpfx - 2 do
  145. begin
  146. P1 := PointFXtoPointF(CurvePtr.apfx[I]);
  147. if I < CurvePtr.cpfx - 2 then
  148. with PointFXtoPointF(CurvePtr.apfx[I + 1]) do
  149. begin
  150. P2.x := (P1.x + x) * 0.5;
  151. P2.y := (P1.y + y) * 0.5;
  152. end
  153. else
  154. P2 := PointFXtoPointF(CurvePtr.apfx[I + 1]);
  155. Path.ConicTo(P1.X + DstX, P1.Y + DstY, P2.X + DstX, P2.Y + DstY);
  156. end;
  157. end;
  158. TT_PRIM_CSPLINE:
  159. begin
  160. I := 0;
  161. while I < CurvePtr.cpfx - 2 do
  162. begin
  163. P1 := PointFXtoPointF(CurvePtr.apfx[I]);
  164. P2 := PointFXtoPointF(CurvePtr.apfx[I + 1]);
  165. P3 := PointFXtoPointF(CurvePtr.apfx[I + 2]);
  166. Path.CurveTo(P1.X + DstX, P1.Y + DstY, P2.X + DstX, P2.Y + DstY,
  167. P3.X + DstX, P3.Y + DstY);
  168. Inc(I, 2);
  169. end;
  170. end;
  171. end;
  172. K := (CurvePtr.cpfx - 1) * SizeOf(TPointFX) + SizeOf(TTPolyCurve);
  173. Dec(S, K);
  174. {$IFDEF HAS_NATIVEINT}
  175. Inc(NativeInt(CurvePtr), K);
  176. {$ELSE}
  177. Inc(Integer(CurvePtr), K);
  178. {$ENDIF}
  179. end;
  180. Path.EndPath(True);
  181. Dec(Res, BufferPtr.cb);
  182. {$IFDEF HAS_NATIVEINT}
  183. Inc(NativeInt(BufferPtr), BufferPtr.cb);
  184. {$ELSE}
  185. Inc(integer(BufferPtr), BufferPtr.cb);
  186. {$ENDIF}
  187. end;
  188. {$IFDEF USESTACKALLOC}
  189. StackFree(GlyphMemPtr);
  190. {$ELSE}
  191. FreeMem(GlyphMemPtr);
  192. {$ENDIF}
  193. Path.EndUpdate;
  194. end;
  195. {$IFDEF USESTACKALLOC}
  196. {$W-}
  197. {$ENDIF}
  198. procedure InternalTextToPath(DC: HDC; Path: TCustomPath; const ARect: TFloatRect; const Text: string; Flags: Cardinal = 0);
  199. const
  200. CHAR_CR = 10;
  201. CHAR_NL = 13;
  202. CHAR_SP = 32;
  203. var
  204. GlyphMetrics: TGlyphMetrics;
  205. TextMetric: TTextMetric;
  206. I, J, TextLen, SpcCount, SpcX, LineStart: Integer;
  207. CharValue: Integer;
  208. CharOffsets: TArrayOfInteger;
  209. CharWidths: TArrayOfInteger;
  210. X, Y, XMax, YMax, MaxRight: Single;
  211. S: string;
  212. TextPath: TFlattenedPath;
  213. OwnedPath: TFlattenedPath;
  214. {$IFDEF USEKERNING}
  215. NextCharValue: Integer;
  216. KerningPairs: PKerningPairArray;
  217. KerningPairCount: Integer;
  218. {$ENDIF}
  219. procedure AlignTextCenter(CurrentI: Integer);
  220. var
  221. w, M, N, PathStart, PathEnd, CharStart, CharEnd: Integer;
  222. Delta: TFloat;
  223. i: Integer;
  224. MinX, MaxX: Single;
  225. begin
  226. Delta := Round(((ARect.Right - ARect.Left) * HorzStretch - X - 1) * 0.5);
  227. PathStart := CharOffsets[LineStart];
  228. PathEnd := CharOffsets[CurrentI] - 1;
  229. if (Flags and DT_SINGLELINE <> 0) and (Flags and DT_NOCLIP <> DT_NOCLIP) then
  230. begin
  231. MinX := ARect.Left + Delta;
  232. MaxX := ARect.Right + Delta;
  233. CharStart := LineStart;
  234. CharEnd := CurrentI;
  235. w := Round(Delta);
  236. for i := LineStart to CurrentI - 1 do
  237. begin
  238. if w < Arect.Left then
  239. begin
  240. CharStart := i + 1;
  241. MinX := w + CharWidths[i];
  242. end;
  243. w := w + CharWidths[i];
  244. if w <= ARect.Right then
  245. begin
  246. CharEnd := i + 1;
  247. MaxX := w;
  248. end;
  249. end;
  250. if (Flags and DT_WORDBREAK <> 0) then
  251. begin
  252. if (CharStart > LineStart) and (Text[CharStart] <> ' ') then
  253. while (Text[CharStart] <> ' ') and (CharStart < CharEnd) do
  254. Inc(CharStart);
  255. if (CharEnd < CurrentI) and (Text[CharEnd] <> ' ') then
  256. while (Text[CharEnd] <> ' ') and (CharEnd > CharStart) do
  257. Dec(CharEnd);
  258. MinX := Round(Delta);
  259. for i := 0 to CharStart - 1 do
  260. MinX := MinX + CharWidths[i];
  261. MaxX := Round(Delta);
  262. for i := 0 to CharEnd - 1 do
  263. MaxX := MaxX + CharWidths[i];
  264. end;
  265. PathStart := CharOffsets[CharStart];
  266. PathEnd := CharOffsets[CharEnd] - 1;
  267. for M := 0 to PathStart - 1 do
  268. SetLength(TextPath.Path[M], 0);
  269. for M := PathEnd + 1 to CharOffsets[CurrentI] - 1 do
  270. SetLength(TextPath.Path[M], 0);
  271. Delta := Delta + (((MinX - ARect.Left) + (ARect.Right - MaxX)) * 0.5) - MinX;
  272. end;
  273. for M := PathStart to PathEnd do
  274. for N := 0 to High(TextPath.Path[M]) do
  275. TextPath.Path[M, N].X := TextPath.Path[M, N].X + Delta;
  276. end;
  277. procedure AlignTextRight(CurrentI: Integer);
  278. var
  279. w, i, M, N, PathStart, PathEnd, CharStart: Integer;
  280. Delta: TFloat;
  281. begin
  282. Delta := Round(ARect.Right * HorzStretch - X - 1);
  283. PathStart := CharOffsets[LineStart];
  284. PathEnd := CharOffsets[CurrentI] - 1;
  285. if (Flags and DT_SINGLELINE <> 0) and (Flags and DT_NOCLIP <> DT_NOCLIP) then
  286. begin
  287. CharStart := LineStart;
  288. w := 0;
  289. for i := LineStart to CurrentI - 1 do
  290. begin
  291. if w + Delta < Arect.Left then
  292. CharStart := i + 1;
  293. w := w + CharWidths[i];
  294. end;
  295. if (Flags and DT_WORDBREAK <> 0) then
  296. if (CharStart > LineStart) and (Text[CharStart] <> ' ') then
  297. while (Text[CharStart] <> ' ') and (CharStart < CurrentI) do
  298. Inc(CharStart);
  299. PathStart := CharOffsets[CharStart];
  300. for M := 0 to PathStart - 1 do
  301. SetLength(TextPath.Path[M], 0);
  302. end;
  303. for M := PathStart to PathEnd do
  304. for N := 0 to High(TextPath.Path[M]) do
  305. TextPath.Path[M, N].X := TextPath.Path[M, N].X + Delta;
  306. end;
  307. procedure AlignTextLeft(CurrentI: Integer);
  308. var
  309. w, i, M, PathEnd, CharEnd: Integer;
  310. begin
  311. if (Flags and DT_SINGLELINE <> 0) and (Flags and DT_NOCLIP <> DT_NOCLIP) then
  312. begin
  313. CharEnd := LineStart;
  314. w := 0;
  315. for i := LineStart to CurrentI - 1 do
  316. begin
  317. w := w + CharWidths[i];
  318. if w <= (ARect.Right - ARect.Left) then
  319. CharEnd := i + 1;
  320. end;
  321. if (Flags and DT_WORDBREAK <> 0) then
  322. if (CharEnd < CurrentI) and (Text[CharEnd] <> ' ') then
  323. while (Text[CharEnd] <> ' ') and (CharEnd > LineStart) do
  324. Dec(CharEnd);
  325. PathEnd := CharOffsets[CharEnd] - 1;
  326. for M := PathEnd + 1 to CharOffsets[CurrentI] - 1 do
  327. SetLength(TextPath.Path[M], 0);
  328. end;
  329. end;
  330. procedure AlignTextJustify(CurrentI: Integer);
  331. var
  332. L, M, N, PathStart, PathEnd: Integer;
  333. SpcDelta, SpcDeltaInc: TFloat;
  334. begin
  335. if (SpcCount < 1) or (Ord(Text[CurrentI]) = CHAR_CR) then
  336. Exit;
  337. SpcDelta := (ARect.Right * HorzStretch - X - 1) / SpcCount;
  338. SpcDeltaInc := SpcDelta;
  339. L := LineStart;
  340. // Trim leading spaces ...
  341. while (L < CurrentI) and (Ord(Text[L]) = CHAR_SP) do Inc(L);
  342. // Now find first space char in line ...
  343. while (L < CurrentI) and (Ord(Text[L]) <> CHAR_SP) do Inc(L);
  344. PathStart := CharOffsets[L - 1];
  345. repeat
  346. M := L + 1;
  347. while (M < CurrentI) and (Ord(Text[M]) <> CHAR_SP) do Inc(M);
  348. PathEnd := CharOffsets[M];
  349. L := M;
  350. for M := PathStart to PathEnd - 1 do
  351. for N := 0 to High(TextPath.Path[M]) do
  352. TextPath.Path[M, N].X := TextPath.Path[M, N].X + SpcDeltaInc;
  353. SpcDeltaInc := SpcDeltaInc + SpcDelta;
  354. PathStart := PathEnd;
  355. until L >= CurrentI;
  356. end;
  357. procedure AlignLine(CurrentI: Integer);
  358. begin
  359. if Assigned(TextPath) and (Length(TextPath.Path) > 0) then
  360. case (Flags and DT_HORZ_ALIGN_MASK) of
  361. DT_LEFT : AlignTextLeft(CurrentI);
  362. DT_CENTER : AlignTextCenter(CurrentI);
  363. DT_RIGHT : AlignTextRight(CurrentI);
  364. DT_JUSTIFY: AlignTextJustify(CurrentI);
  365. end;
  366. end;
  367. procedure AddSpace;
  368. begin
  369. Inc(SpcCount);
  370. X := X + SpcX;
  371. end;
  372. procedure NewLine(CurrentI: Integer);
  373. begin
  374. if (Flags and DT_SINGLELINE <> 0) then
  375. begin
  376. AddSpace;
  377. Exit;
  378. end;
  379. AlignLine(CurrentI);
  380. X := ARect.Left * HorzStretch;
  381. Y := Y + TextMetric.tmHeight;
  382. LineStart := CurrentI;
  383. SpcCount := 0;
  384. end;
  385. function MeasureTextX(const S: string): Integer;
  386. var
  387. I: Integer;
  388. begin
  389. Result := 0;
  390. for I := 1 to Length(S) do
  391. begin
  392. CharValue := Ord(S[I]);
  393. GetGlyphOutline(DC, CharValue, GGODefaultFlags[UseHinting], GlyphMetrics, 0, nil, VertFlip_mat2);
  394. Inc(Result, GlyphMetrics.gmCellIncX);
  395. end;
  396. end;
  397. function NeedsNewLine(X: Single): Boolean;
  398. begin
  399. Result := (ARect.Right > ARect.Left) and (X > ARect.Right * HorzStretch);
  400. end;
  401. begin
  402. {$IFDEF USEKERNING}
  403. KerningPairs := nil;
  404. KerningPairCount := GetKerningPairs(DC, 0, nil);
  405. if GetLastError <> 0 then
  406. RaiseLastOSError;
  407. if KerningPairCount > 0 then
  408. begin
  409. GetMem(KerningPairs, KerningPairCount * SizeOf(TKerningPair));
  410. GetKerningPairs(DC, KerningPairCount, PKerningPair(KerningPairs));
  411. end;
  412. {$ENDIF}
  413. SpcCount := 0;
  414. LineStart := 0;
  415. OwnedPath := nil;
  416. if (Path <> nil) then
  417. begin
  418. if (Path is TFlattenedPath) then
  419. begin
  420. TextPath := TFlattenedPath(Path);
  421. TextPath.Clear;
  422. end
  423. else
  424. begin
  425. OwnedPath := TFlattenedPath.Create;
  426. TextPath := OwnedPath;
  427. end
  428. end else
  429. TextPath := nil;
  430. GetTextMetrics(DC, TextMetric);
  431. TextLen := Length(Text);
  432. X := ARect.Left * HorzStretch;
  433. Y := ARect.Top + TextMetric.tmAscent;
  434. XMax := X;
  435. if not Assigned(Path) or (ARect.Right = ARect.Left) then
  436. MaxRight := MaxSingle //either measuring Text or unbounded Text
  437. else
  438. MaxRight := ARect.Right * HorzStretch;
  439. SetLength(CharOffsets, TextLen + 1);
  440. CharOffsets[0] := 0;
  441. SetLength(CharWidths, TextLen);
  442. GetGlyphOutline(DC, CHAR_SP, GGODefaultFlags[UseHinting], GlyphMetrics,
  443. 0, nil, VertFlip_mat2);
  444. SpcX := GlyphMetrics.gmCellIncX;
  445. if (Flags and DT_SINGLELINE <> 0) or (ARect.Left = ARect.Right) then
  446. begin
  447. // ignore justify when forcing singleline ...
  448. if (Flags and DT_JUSTIFY = DT_JUSTIFY) then
  449. Flags := Flags and not DT_JUSTIFY;
  450. // ignore wordbreak when forcing singleline ...
  451. //if (Flags and DT_WORDBREAK = DT_WORDBREAK) then
  452. // Flags := Flags and not DT_WORDBREAK;
  453. MaxRight := MaxSingle;
  454. end;
  455. // Batch whole path construction so we can be sure that the path isn't rendered
  456. // while we're still modifying it.
  457. if (TextPath <> nil) then
  458. TextPath.BeginUpdate;
  459. for I := 1 to TextLen do
  460. begin
  461. CharValue := Ord(Text[I]);
  462. if CharValue <= 32 then
  463. begin
  464. if (Flags and DT_SINGLELINE = DT_SINGLELINE) then
  465. CharValue := CHAR_SP;
  466. if Assigned(TextPath) then
  467. // Save path list offset of first path of current glyph
  468. CharOffsets[I] := Length(TextPath.Path);
  469. CharWidths[i - 1]:= SpcX;
  470. case CharValue of
  471. CHAR_CR: NewLine(I);
  472. CHAR_NL: ;
  473. CHAR_SP:
  474. begin
  475. if Flags and DT_WORDBREAK = DT_WORDBREAK then
  476. begin
  477. J := I + 1;
  478. while (J <= TextLen) and
  479. ([Ord(Text[J])] * [CHAR_CR, CHAR_NL, CHAR_SP] = []) do
  480. Inc(J);
  481. S := Copy(Text, I, J - I);
  482. if NeedsNewLine(X + MeasureTextX(S)) then
  483. NewLine(I) else
  484. AddSpace;
  485. end else
  486. begin
  487. if NeedsNewLine(X + SpcX) then
  488. NewLine(I)
  489. else
  490. AddSpace;
  491. end;
  492. end;
  493. end;
  494. end
  495. else
  496. begin
  497. if GlyphOutlineToPath(DC, TextPath, X, MaxRight, Y, CharValue,
  498. GlyphMetrics) then
  499. begin
  500. if Assigned(TextPath) then
  501. // Save path list offset of first path of current glyph
  502. CharOffsets[I] := Length(TextPath.Path);
  503. CharWidths[I - 1]:= GlyphMetrics.gmCellIncX;
  504. end else
  505. begin
  506. if Ord(Text[I - 1]) = CHAR_SP then
  507. begin
  508. // this only happens without DT_WORDBREAK
  509. X := X - SpcX;
  510. Dec(SpcCount);
  511. end;
  512. // the current glyph doesn't fit so a word must be split since
  513. // it fills more than a whole line ...
  514. NewLine(I - 1);
  515. if not GlyphOutlineToPath(DC, TextPath, X, MaxRight, Y, CharValue,
  516. GlyphMetrics) then Break;
  517. if Assigned(TextPath) then
  518. // Save path list offset of first path of current glyph
  519. CharOffsets[I] := Length(TextPath.Path);
  520. CharWidths[I - 1]:= GlyphMetrics.gmCellIncX;
  521. end;
  522. X := X + GlyphMetrics.gmCellIncX;
  523. {$IFDEF USEKERNING}
  524. if i < TextLen then NextCharValue := Ord(Text[i + 1]);
  525. for J := 0 to KerningPairCount - 1 do
  526. begin
  527. if (KerningPairs^[J].wFirst = CharValue) and
  528. (KerningPairs^[J].wSecond = NextCharValue) then
  529. begin
  530. X := X + KerningPairs^[J].iKernAmount;
  531. break;
  532. end;
  533. end;
  534. {$ENDIF}
  535. if X > XMax then XMax := X;
  536. end;
  537. end;
  538. if [(Flags and DT_HORZ_ALIGN_MASK)] * [DT_LEFT, DT_CENTER, DT_RIGHT] <> [] then
  539. AlignLine(TextLen);
  540. YMax := Y + TextMetric.tmHeight - TextMetric.tmAscent;
  541. // reverse HorzStretch (if any) ...
  542. if (HorzStretch <> 1) and assigned(TextPath) then
  543. for I := 0 to High(TextPath.Path) do
  544. for J := 0 to High(TextPath.Path[I]) do
  545. TextPath.Path[I, J].X := TextPath.Path[I, J].X * HorzStretch_Inv;
  546. XMax := XMax * HorzStretch_Inv;
  547. X := ARect.Right - XMax;
  548. Y := ARect.Bottom - YMax;
  549. if Flags and (DT_VCENTER or DT_BOTTOM) <> 0 then
  550. begin
  551. if Flags and DT_VCENTER <> 0 then
  552. Y := Y * 0.5;
  553. if Assigned(TextPath) then
  554. for I := 0 to High(TextPath.Path) do
  555. for J := 0 to High(TextPath.Path[I]) do
  556. TextPath.Path[I, J].Y := TextPath.Path[I, J].Y + Y;
  557. end;
  558. {$IFDEF USEKERNING}
  559. if Assigned(KerningPairs) then
  560. FreeMem(KerningPairs);
  561. {$ENDIF}
  562. if (Path <> nil) then
  563. begin
  564. TextPath.EndPath; // TODO : Why is this needed?
  565. if (Path <> TextPath) then
  566. Path.Assign(TextPath);
  567. TextPath.EndUpdate;
  568. OwnedPath.Free;
  569. end;
  570. end;
  571. procedure TextToPath(Font: HFONT; Path: TCustomPath; const ARect: TFloatRect; const Text: string; Flags: Cardinal);
  572. var
  573. DC: HDC;
  574. SavedFont: HFONT;
  575. begin
  576. DC := GetDC(0);
  577. try
  578. SavedFont := SelectObject(DC, Font);
  579. InternalTextToPath(DC, Path, ARect, Text, Flags);
  580. SelectObject(DC, SavedFont);
  581. finally
  582. ReleaseDC(0, DC);
  583. end;
  584. end;
  585. function TextToPolyPolygon(Font: HFONT; const ARect: TFloatRect; const Text: string; Flags: Cardinal): TArrayOfArrayOfFloatPoint;
  586. var
  587. Path: TFlattenedPath;
  588. begin
  589. Path := TFlattenedPath.Create;
  590. try
  591. TextToPath(Font, Path, ARect, Text, Flags);
  592. Result := Path.Path;
  593. finally
  594. Path.Free;
  595. end;
  596. end;
  597. function MeasureTextDC(DC: HDC; const ARect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
  598. begin
  599. Result := ARect;
  600. InternalTextToPath(DC, nil, Result, Text, Flags);
  601. Result.Left := Round(Result.Left);
  602. Result.Top := Round(Result.Top);
  603. Result.Right := Round(Result.Right);
  604. Result.Bottom := Round(Result.Bottom);
  605. end;
  606. function MeasureText(Font: HFONT; const ARect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
  607. var
  608. DC: HDC;
  609. SavedFont: HFONT;
  610. begin
  611. DC := GetDC(0);
  612. try
  613. SavedFont := SelectObject(DC, Font);
  614. Result := MeasureTextDC(DC, ARect, Text, Flags);
  615. SelectObject(DC, SavedFont);
  616. finally
  617. ReleaseDC(0, DC);
  618. end;
  619. end;
  620. procedure SetHinting(Value: TTextHinting);
  621. begin
  622. UseHinting := Value <> thNone;
  623. if (Value = thNoHorz) then
  624. HorzStretch := 16 else
  625. HorzStretch := 1;
  626. HorzStretch_Inv := 1 / HorzStretch;
  627. FillChar(VertFlip_mat2, SizeOf(VertFlip_mat2), 0);
  628. VertFlip_mat2.eM11.value := HorzStretch;
  629. VertFlip_mat2.eM22.value := -1; //reversed Y axis
  630. end;
  631. function GetHinting: TTextHinting;
  632. begin
  633. if HorzStretch <> 1 then Result := thNoHorz
  634. else if UseHinting then Result := thHinting
  635. else Result := thNone;
  636. end;
  637. procedure InitHinting;
  638. begin
  639. {$IFDEF NOHORIZONTALHINTING}
  640. SetHinting(thNoHorz);
  641. {$ELSE}
  642. {$IFDEF NOHINTING}
  643. SetHinting(thNone);
  644. {$ELSE}
  645. SetHinting(thHinting);
  646. {$ENDIF}
  647. {$ENDIF}
  648. end;
  649. initialization
  650. InitHinting;
  651. end.