fTextVPR.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. unit fTextVPR;
  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. {$I 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. BtnExit: TButton;
  43. BtnSelectFont: TButton;
  44. CbxSingleLine: TCheckBox;
  45. CbxWordbreak: TCheckBox;
  46. FontDialog: TFontDialog;
  47. GBxFont: TGroupBox;
  48. GbxLayout: TGroupBox;
  49. GbxRendering: 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. RgpHinting: TRadioGroup;
  59. RgpHorzAlign: TRadioGroup;
  60. RgpVerticalAlign: TRadioGroup;
  61. RgxMethod: TRadioGroup;
  62. StatusBar: TStatusBar;
  63. TbrGamma: TTrackBar;
  64. procedure FormCreate(Sender: TObject);
  65. procedure FormDestroy(Sender: TObject);
  66. procedure FormResize(Sender: TObject);
  67. procedure BtnExitClick(Sender: TObject);
  68. procedure BtnSelectFontClick(Sender: TObject);
  69. procedure ImgClick(Sender: TObject);
  70. procedure ImgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer;
  71. Layer: TCustomLayer);
  72. procedure RgpHintingClick(Sender: TObject);
  73. procedure RgpHorzAlignClick(Sender: TObject);
  74. procedure RgxMethodClick(Sender: TObject);
  75. procedure TbrGammaChange(Sender: TObject);
  76. private
  77. FPath: TFlattenedPath;
  78. public
  79. procedure BuildPolygonFromText;
  80. procedure RenderText;
  81. procedure DisplayFontInfo;
  82. end;
  83. var
  84. MainForm: TMainForm;
  85. implementation
  86. {$IFDEF FPC}
  87. {$R *.lfm}
  88. {$ELSE}
  89. {$R *.dfm}
  90. {$ENDIF}
  91. uses
  92. GR32_Backends, GR32_Gamma, GR32_Polygons,
  93. {$IFDEF FPC}
  94. {$IFDEF LCLWin32}
  95. GR32_Text_LCL_Win;
  96. {$ENDIF}
  97. {$IF defined(LCLGtk) or defined(LCLGtk2)}
  98. GR32_Text_LCL_GTK;
  99. {$IFEND}
  100. {$IFDEF LCLCarbon}
  101. GR32_Text_LCL_Carbon;
  102. {$ENDIF}
  103. {$IFDEF LCLCustomDrawn}
  104. GR32_Text_LCL_CustomDrawn;
  105. {$ENDIF}
  106. {$ELSE}
  107. GR32_Text_VCL;
  108. {$ENDIF}
  109. const
  110. CLoremIpsum =
  111. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin sit ' +
  112. 'amet nulla. Nam turpis nisl, faucibus ut, pulvinar eget, ' +
  113. 'porta ac, lacus. Nam ultricies quam sed est. Mauris ' +
  114. 'auctor nibh ut dui. Phasellus facilisis libero sit amet urna. ' +
  115. 'Pellentesque non lorem. Donec aliquam, turpis in ornare placerat, ' +
  116. 'risus justo rhoncus nibh, vitae commodo sem eros vitae ' +
  117. 'massa. Donec tincidunt. Suspendisse potenti. Praesent ' +
  118. 'sapien augue, fermentum in, aliquet et, vestibulum vel, neque. ' +
  119. 'Vivamus diam. Suspendisse commodo odio non erat. Fusce ornare, ipsum ' +
  120. 'et luctus eleifend, sapien lectus placerat ante, a posuere ' +
  121. 'nibh risus nec quam. Pellentesque pretium. Etiam leo urna, ' +
  122. 'gravida eu, pellentesque eu, imperdiet in, enim. Nam nunc. ' +
  123. 'Quisque commodo.' + #10#10 +
  124. 'In scelerisque. Mauris vitae magna. Curabitur tempor. Pellentesque ' +
  125. 'condimentum. Maecenas molestie turpis sed arcu pulvinar ' +
  126. 'malesuada. Morbi quis metus in leo vestibulum mollis. ' +
  127. 'Ut libero arcu, molestie eget, tincidunt at, lobortis et,' +
  128. 'libero. Duis molestie venenatis magna. Nulla non ligula. Proin est. ' +
  129. 'Curabitur nisl. Nulla facilisi. Nam dolor nulla, mollis ' +
  130. 'non, tristique eu, vestibulum eget, mi. Donec venenatis, ' +
  131. 'lacus adipiscing interdum laoreet, risus odio ullamcorper turpis, ' +
  132. 'at feugiat pede neque ac dui.' + #10#10 +
  133. 'Nulla quis dolor eget justo ullamcorper consectetur. Mauris in ante. ' +
  134. 'Integer placerat dui at orci. Pellentesque at augue. Fusce ' +
  135. 'a turpis. Aliquam tincidunt dolor ut augue. Quisque ' +
  136. 'euismod mi ultrices mi. Sed pulvinar dolor sagittis mauris. Sed iaculis ' +
  137. 'nisl sed orci. Sed massa nisl, porta a, blandit vel, ' +
  138. 'ultrices quis, neque. Curabitur consequat urna id pede. ' +
  139. 'Suspendisse sed metus.';
  140. procedure TMainForm.FormCreate(Sender: TObject);
  141. begin
  142. FPath := TFlattenedPath.Create;
  143. SetGamma(TbrGamma.Position * 0.001);
  144. Img.SetupBitmap(True, clWhite32);
  145. Img.Bitmap.Font.Name := 'Georgia';
  146. Img.Bitmap.Font.Size := 9;
  147. FontDialog.Font.Assign(Img.Bitmap.Font);
  148. DisplayFontInfo;
  149. PaintBox32.Buffer.SetSizeFrom(PaintBox32);
  150. PaintBox32.Buffer.Clear(clWhite32);
  151. RgpHinting.ItemIndex := Ord(GetHinting);
  152. {$IFDEF USEGR32GAMMA}
  153. TbrGamma.Enabled := True;
  154. {$ENDIF}
  155. end;
  156. procedure TMainForm.FormDestroy(Sender: TObject);
  157. begin
  158. FPath.Free;
  159. end;
  160. procedure TMainForm.BtnSelectFontClick(Sender: TObject);
  161. begin
  162. if FontDialog.Execute then
  163. begin
  164. Img.Bitmap.Font.Assign(FontDialog.Font);
  165. BuildPolygonFromText;
  166. RenderText;
  167. DisplayFontInfo;
  168. end;
  169. end;
  170. procedure TMainForm.ImgClick(Sender: TObject);
  171. begin
  172. if RgxMethod.ItemIndex + 1 < RgxMethod.Items.Count then
  173. RgxMethod.ItemIndex := RgxMethod.ItemIndex + 1
  174. else
  175. RgxMethod.ItemIndex := 0;
  176. end;
  177. procedure TMainForm.ImgMouseMove(Sender: TObject; Shift: TShiftState; X,
  178. Y: Integer; Layer: TCustomLayer);
  179. const
  180. Delta = 16;
  181. begin
  182. PaintBox32.Buffer.Draw(PaintBox32.Buffer.BoundsRect,
  183. Rect(X - Delta , Y - Delta, X + Delta, Y + Delta), Img.Bitmap);
  184. PaintBox32.Repaint;
  185. end;
  186. procedure TMainForm.BuildPolygonFromText;
  187. var
  188. Intf: ITextToPathSupport;
  189. DestRect: TFloatRect;
  190. Flag: Integer;
  191. begin
  192. if Supports(Img.Bitmap.Backend, ITextToPathSupport, Intf) then
  193. begin
  194. DestRect := FloatRect(Img.BoundsRect);
  195. InflateRect(DestRect, -10, -10);
  196. Flag := RgpHorzAlign.ItemIndex;
  197. case RgpVerticalAlign.ItemIndex of
  198. 0: ;
  199. 1: Flag := Flag or DT_VCENTER;
  200. else Flag := Flag or DT_BOTTOM;
  201. end;
  202. if CbxSingleLine.Checked then
  203. Flag := Flag or DT_SINGLELINE;
  204. if CbxWordbreak.Checked then
  205. Flag := Flag or DT_WORDBREAK;
  206. Intf.TextToPath(FPath, DestRect, CLoremIpsum, Flag);
  207. end else
  208. raise Exception.Create(RCStrInpropriateBackend);
  209. end;
  210. procedure TMainForm.RenderText;
  211. begin
  212. Img.SetupBitmap(True, clWhite32);
  213. case RgxMethod.ItemIndex of
  214. 0: PolyPolygonFS(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  215. 1: PolyPolygonFS_LCD(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  216. 2: PolyPolygonFS_LCD2(Img.Bitmap, FPath.Path, clBlack32, pfWinding);
  217. end;
  218. // paint the close-up of the image around the mouse cursor ...
  219. with Img.ScreenToClient(Mouse.CursorPos) do
  220. ImgMouseMove(nil, [], X, Y, nil);
  221. end;
  222. function FontStylesToString(FontStyles: TFontStyles): string;
  223. var
  224. Styles: TFontStyles;
  225. begin
  226. Styles := [fsBold, fsItalic] * FontStyles;
  227. if Styles = [] then
  228. Result := ''
  229. else if Styles = [fsBold] then
  230. Result := ', Bold'
  231. else if Styles = [fsItalic] then
  232. Result := ', Italic'
  233. else
  234. Result := ', Bold && Italic';
  235. end;
  236. procedure TMainForm.DisplayFontInfo;
  237. begin
  238. with FontDialog.Font do
  239. LblFontInfo.Caption :=
  240. Format('%s'#10'%d%s', [Name, Size, FontStylesToString(Style)]);
  241. end;
  242. procedure TMainForm.RgxMethodClick(Sender: TObject);
  243. begin
  244. RenderText;
  245. end;
  246. procedure TMainForm.TbrGammaChange(Sender: TObject);
  247. begin
  248. SetGamma(TbrGamma.Position * 0.001);
  249. LblGammaValue.Caption := Format('(%1.2f)', [TbrGamma.Position * 0.001]);
  250. RenderText;
  251. end;
  252. procedure TMainForm.RgpHintingClick(Sender: TObject);
  253. begin
  254. case RgpHinting.ItemIndex of
  255. 0: SetHinting(thNone);
  256. 1: SetHinting(thNoHorz);
  257. else SetHinting(thHinting)
  258. end;
  259. BuildPolygonFromText;
  260. RenderText;
  261. end;
  262. procedure TMainForm.FormResize(Sender: TObject);
  263. begin
  264. BuildPolygonFromText;
  265. RenderText;
  266. end;
  267. procedure TMainForm.RgpHorzAlignClick(Sender: TObject);
  268. begin
  269. BuildPolygonFromText;
  270. RenderText;
  271. end;
  272. procedure TMainForm.BtnExitClick(Sender: TObject);
  273. begin
  274. Close;
  275. end;
  276. end.