MainUnit.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. unit MainUnit;
  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 TextDemoVPR Example (based on VPR example)
  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) 2000-2005
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Christian-W. Budde (GR32 version 2.0 port)
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$include GR32.inc}
  36. uses
  37. {$IFDEF FPC} LCLIntf, LResources, Buttons, {$ENDIF} Messages, SysUtils,
  38. Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls,
  39. GR32, GR32_Paths, GR32_Image, GR32_Layers;
  40. type
  41. TMainForm = class(TForm)
  42. ButtonExit: TButton;
  43. ButtonSelectFont: TButton;
  44. CheckBoxSingleLine: TCheckBox;
  45. CheckBoxWordbreak: TCheckBox;
  46. FontDialog: TFontDialog;
  47. GroupBoxFont: TGroupBox;
  48. GroupBoxLayout: TGroupBox;
  49. GroupBoxRendering: TGroupBox;
  50. Img: TImage32;
  51. LblFontInfo: TLabel;
  52. LblGamma: TLabel;
  53. LblGammaValue: TLabel;
  54. PaintBox32: TPaintBox32;
  55. PnlControl: TPanel;
  56. PnlImage: TPanel;
  57. PnlZoom: TPanel;
  58. RadioGroupHinting: TRadioGroup;
  59. RadioGroupHorizontalAlign: TRadioGroup;
  60. RadioGroupVerticalAlign: TRadioGroup;
  61. RadioGroupMethod: TRadioGroup;
  62. StatusBar: TStatusBar;
  63. TbrGamma: TTrackBar;
  64. GroupBoxGamma: TGroupBox;
  65. PanelLeft: TPanel;
  66. procedure FormCreate(Sender: TObject);
  67. procedure FormDestroy(Sender: TObject);
  68. procedure FormResize(Sender: TObject);
  69. procedure ButtonExitClick(Sender: TObject);
  70. procedure ButtonSelectFontClick(Sender: TObject);
  71. procedure ImgClick(Sender: TObject);
  72. procedure ImgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer;
  73. Layer: TCustomLayer);
  74. procedure RadioGroupHintingClick(Sender: TObject);
  75. procedure RadioGroupHorizontalAlignClick(Sender: TObject);
  76. procedure RadioGroupMethodClick(Sender: TObject);
  77. procedure TbrGammaChange(Sender: TObject);
  78. private
  79. FPath: TFlattenedPath;
  80. public
  81. procedure BuildPolygonFromText;
  82. procedure RenderText;
  83. procedure DisplayFontInfo;
  84. end;
  85. var
  86. MainForm: TMainForm;
  87. implementation
  88. {$R *.dfm}
  89. uses
  90. {$IFNDEF FPC}
  91. Types,
  92. System.UITypes,
  93. {$ENDIF}
  94. GR32.Text.Types,
  95. GR32_Backends,
  96. GR32_Gamma,
  97. GR32_Polygons;
  98. const
  99. CLoremIpsum =
  100. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin sit ' +
  101. 'amet nulla. Nam turpis nisl, faucibus ut, pulvinar eget, ' +
  102. 'porta ac, lacus. Nam ultricies quam sed est. Mauris ' +
  103. 'auctor nibh ut dui. Phasellus facilisis libero sit amet urna. ' +
  104. 'Pellentesque non lorem. Donec aliquam, turpis in ornare placerat, ' +
  105. 'risus justo rhoncus nibh, vitae commodo sem eros vitae ' +
  106. 'massa. Donec tincidunt. Suspendisse potenti. Praesent ' +
  107. 'sapien augue, fermentum in, aliquet et, vestibulum vel, neque. ' +
  108. 'Vivamus diam. Suspendisse commodo odio non erat. Fusce ornare, ipsum ' +
  109. 'et luctus eleifend, sapien lectus placerat ante, a posuere ' +
  110. 'nibh risus nec quam. Pellentesque pretium. Etiam leo urna, ' +
  111. 'gravida eu, pellentesque eu, imperdiet in, enim. Nam nunc. ' +
  112. 'Quisque commodo.' + #10#10 +
  113. 'In scelerisque. Mauris vitae magna. Curabitur tempor. Pellentesque ' +
  114. 'condimentum. Maecenas molestie turpis sed arcu pulvinar ' +
  115. 'malesuada. Morbi quis metus in leo vestibulum mollis. ' +
  116. 'Ut libero arcu, molestie eget, tincidunt at, lobortis et,' +
  117. 'libero. Duis molestie venenatis magna. Nulla non ligula. Proin est. ' +
  118. 'Curabitur nisl. Nulla facilisi. Nam dolor nulla, mollis ' +
  119. 'non, tristique eu, vestibulum eget, mi. Donec venenatis, ' +
  120. 'lacus adipiscing interdum laoreet, risus odio ullamcorper turpis, ' +
  121. 'at feugiat pede neque ac dui.' + #10#10 +
  122. 'Nulla quis dolor eget justo ullamcorper consectetur. Mauris in ante. ' +
  123. 'Integer placerat dui at orci. Pellentesque at augue. Fusce ' +
  124. 'a turpis. Aliquam tincidunt dolor ut augue. Quisque ' +
  125. 'euismod mi ultrices mi. Sed pulvinar dolor sagittis mauris. Sed iaculis ' +
  126. 'nisl sed orci. Sed massa nisl, porta a, blandit vel, ' +
  127. 'ultrices quis, neque. Curabitur consequat urna id pede. ' +
  128. 'Suspendisse sed metus.';
  129. procedure TMainForm.FormCreate(Sender: TObject);
  130. var
  131. FontHinting: IFontHintingSupport;
  132. begin
  133. FPath := TFlattenedPath.Create;
  134. SetGamma(TbrGamma.Position * 0.001);
  135. Img.SetupBitmap(True, clWhite32);
  136. Img.Bitmap.Font.Name := 'Georgia';
  137. Img.Bitmap.Font.Size := 9;
  138. FontDialog.Font.Assign(Img.Bitmap.Font);
  139. DisplayFontInfo;
  140. // TODO : This is a misuse of TPaintBox32; A TImage32 would have been more suitable.
  141. PaintBox32.Buffer.SetSizeFrom(PaintBox32);
  142. PaintBox32.Buffer.Clear(clWhite32);
  143. if Supports(Img.Bitmap.Backend, IFontHintingSupport, FontHinting) then
  144. RadioGroupHinting.ItemIndex := Ord(FontHinting.GetHinting)
  145. else
  146. RadioGroupHinting.Enabled := False;
  147. {$ifndef FPC}
  148. Self.Padding.SetBounds(4,4,4,4);
  149. ButtonExit.AlignWithMargins := True;
  150. ButtonSelectFont.AlignWithMargins := True;
  151. GroupBoxFont.AlignWithMargins := True;
  152. GroupBoxLayout.AlignWithMargins := True;
  153. RadioGroupHorizontalAlign.AlignWithMargins := True;
  154. RadioGroupVerticalAlign.AlignWithMargins := True;
  155. CheckBoxSingleLine.AlignWithMargins := True;
  156. CheckBoxWordbreak.AlignWithMargins := True;
  157. GroupBoxRendering.AlignWithMargins := True;
  158. RadioGroupHinting.AlignWithMargins := True;
  159. GroupBoxGamma.AlignWithMargins := True;
  160. RadioGroupMethod.AlignWithMargins := True;
  161. {$else}
  162. ButtonExit.BorderSpacing.Around := 4;
  163. ButtonSelectFont.BorderSpacing.Around := 4;
  164. GroupBoxFont.BorderSpacing.Around := 4;
  165. GroupBoxLayout.BorderSpacing.Around := 4;
  166. RadioGroupHorizontalAlign.BorderSpacing.Around := 4;
  167. RadioGroupVerticalAlign.BorderSpacing.Around := 4;
  168. CheckBoxSingleLine.BorderSpacing.Around := 4;
  169. CheckBoxWordbreak.BorderSpacing.Around := 4;
  170. GroupBoxRendering.BorderSpacing.Around := 4;
  171. RadioGroupHinting.BorderSpacing.Around := 4;
  172. GroupBoxGamma.BorderSpacing.Around := 4;
  173. RadioGroupMethod.BorderSpacing.Around := 4;
  174. {$endif}
  175. end;
  176. procedure TMainForm.FormDestroy(Sender: TObject);
  177. begin
  178. FPath.Free;
  179. end;
  180. procedure TMainForm.ButtonSelectFontClick(Sender: TObject);
  181. begin
  182. if FontDialog.Execute then
  183. begin
  184. Img.Bitmap.Font.Assign(FontDialog.Font);
  185. BuildPolygonFromText;
  186. RenderText;
  187. DisplayFontInfo;
  188. end;
  189. end;
  190. procedure TMainForm.ImgClick(Sender: TObject);
  191. begin
  192. if RadioGroupMethod.ItemIndex + 1 < RadioGroupMethod.Items.Count then
  193. RadioGroupMethod.ItemIndex := RadioGroupMethod.ItemIndex + 1
  194. else
  195. RadioGroupMethod.ItemIndex := 0;
  196. end;
  197. procedure TMainForm.ImgMouseMove(Sender: TObject; Shift: TShiftState; X,
  198. Y: Integer; Layer: TCustomLayer);
  199. const
  200. Delta = 16;
  201. begin
  202. PaintBox32.Buffer.Draw(PaintBox32.Buffer.BoundsRect,
  203. Rect(X - Delta , Y - Delta, X + Delta, Y + Delta), Img.Bitmap);
  204. PaintBox32.Repaint;
  205. end;
  206. procedure TMainForm.BuildPolygonFromText;
  207. var
  208. Intf: ITextToPathSupport;
  209. DestRect: TFloatRect;
  210. Flag: Integer;
  211. begin
  212. if Supports(Img.Bitmap.Backend, ITextToPathSupport, Intf) then
  213. begin
  214. DestRect := FloatRect(Img.BoundsRect);
  215. GR32.InflateRect(DestRect, -10, -10);
  216. Flag := RadioGroupHorizontalAlign.ItemIndex;
  217. case RadioGroupVerticalAlign.ItemIndex of
  218. 0: ;
  219. 1: Flag := Flag or DT_VCENTER;
  220. else
  221. Flag := Flag or DT_BOTTOM;
  222. end;
  223. if CheckBoxSingleLine.Checked then
  224. Flag := Flag or DT_SINGLELINE;
  225. if CheckBoxWordbreak.Checked then
  226. Flag := Flag or DT_WORDBREAK;
  227. Intf.TextToPath(FPath, DestRect, CLoremIpsum, Flag);
  228. end else
  229. raise Exception.Create(RCStrInpropriateBackend);
  230. end;
  231. procedure TMainForm.RenderText;
  232. begin
  233. Img.SetupBitmap(True, clWhite32);
  234. case RadioGroupMethod.ItemIndex of
  235. 0: PolyPolygonFS(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  236. 1: PolyPolygonFS_LCD(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  237. 2: PolyPolygonFS_LCD2(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  238. end;
  239. // paint the close-up of the image around the mouse cursor ...
  240. with Img.ScreenToClient(Mouse.CursorPos) do
  241. ImgMouseMove(nil, [], X, Y, nil);
  242. end;
  243. function FontStylesToString(FontStyles: TFontStyles): string;
  244. var
  245. Styles: TFontStyles;
  246. begin
  247. Styles := [fsBold, fsItalic] * FontStyles;
  248. if Styles = [] then
  249. Result := ''
  250. else
  251. if Styles = [fsBold] then
  252. Result := ', Bold'
  253. else
  254. if Styles = [fsItalic] then
  255. Result := ', Italic'
  256. else
  257. Result := ', Bold & Italic';
  258. end;
  259. procedure TMainForm.DisplayFontInfo;
  260. begin
  261. LblFontInfo.Caption := Format('%s'#10'%d%s', [FontDialog.Font.Name, FontDialog.Font.Size, FontStylesToString(FontDialog.Font.Style)]);
  262. end;
  263. procedure TMainForm.RadioGroupMethodClick(Sender: TObject);
  264. begin
  265. RenderText;
  266. end;
  267. procedure TMainForm.TbrGammaChange(Sender: TObject);
  268. begin
  269. SetGamma(TbrGamma.Position * 0.001);
  270. LblGammaValue.Caption := Format('(%1.2f)', [TbrGamma.Position * 0.001]);
  271. RenderText;
  272. end;
  273. procedure TMainForm.RadioGroupHintingClick(Sender: TObject);
  274. var
  275. FontHinting: IFontHintingSupport;
  276. begin
  277. if not Supports(Img.Bitmap.Backend, IFontHintingSupport, FontHinting) then
  278. exit;
  279. case RadioGroupHinting.ItemIndex of
  280. 0: FontHinting.SetHinting(thNone);
  281. 1: FontHinting.SetHinting(thNoHorz);
  282. else
  283. FontHinting.SetHinting(thHinting)
  284. end;
  285. BuildPolygonFromText;
  286. RenderText;
  287. end;
  288. procedure TMainForm.FormResize(Sender: TObject);
  289. begin
  290. BuildPolygonFromText;
  291. RenderText;
  292. end;
  293. procedure TMainForm.RadioGroupHorizontalAlignClick(Sender: TObject);
  294. begin
  295. BuildPolygonFromText;
  296. RenderText;
  297. end;
  298. procedure TMainForm.ButtonExitClick(Sender: TObject);
  299. begin
  300. Close;
  301. end;
  302. end.