2
0

MainUnit.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 RenderText Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Christian-W. Budde
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$include GR32.inc}
  36. uses
  37. {$IFDEF FPC} LCLType, LResources, {$ELSE} Windows, {$ENDIF}
  38. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  39. Buttons,
  40. GR32, GR32_Image;
  41. type
  42. TFormRenderText = class(TForm)
  43. PanelTop: TPanel;
  44. LabelText: TLabel;
  45. EditText: TEdit;
  46. Image: TImage32;
  47. CheckBoxAntiAlias: TCheckBox;
  48. CheckBoxCanvas32: TCheckBox;
  49. CheckBoxBold: TCheckBox;
  50. CheckBoxItalic: TCheckBox;
  51. LabelFont: TLabel;
  52. ComboBoxFont: TComboBox;
  53. ButtonBenchmark: TButton;
  54. Bevel1: TBevel;
  55. procedure FormCreate(Sender: TObject);
  56. procedure ButtonBenchmarkClick(Sender: TObject);
  57. procedure ImageResize(Sender: TObject);
  58. procedure Changed(Sender: TObject);
  59. procedure CheckBoxCanvas32Click(Sender: TObject);
  60. private
  61. function GetFontStyle: TFontStyles;
  62. public
  63. procedure Draw;
  64. end;
  65. var
  66. FormRenderText: TFormRenderText;
  67. implementation
  68. {$R *.dfm}
  69. uses
  70. Types,
  71. GR32_Paths,
  72. GR32_Brushes,
  73. GR32_Polygons,
  74. GR32_System;
  75. function TFormRenderText.GetFontStyle: TFontStyles;
  76. begin
  77. Result := [];
  78. if CheckBoxBold.Checked then
  79. Include(Result, fsBold);
  80. if CheckBoxItalic.Checked then
  81. Include(Result, fsItalic);
  82. end;
  83. procedure TFormRenderText.FormCreate(Sender: TObject);
  84. var
  85. i: integer;
  86. begin
  87. Image.SetupBitmap;
  88. ComboBoxFont.Items.BeginUpdate;
  89. try
  90. ComboBoxFont.Items.Assign(Screen.Fonts);
  91. for i := ComboBoxFont.Items.Count-1 downto 0 do
  92. if (Copy(ComboBoxFont.Items[i], 1, 1) = '@') then
  93. ComboBoxFont.Items.Delete(i);
  94. finally
  95. ComboBoxFont.Items.EndUpdate;
  96. end;
  97. if Screen.Fonts.IndexOf('Segoe UI') <> -1 then
  98. Image.Bitmap.Font.Name := 'Segoe UI'
  99. else
  100. Image.Bitmap.Font.Name := 'Tahoma';
  101. ComboBoxFont.Text := Image.Bitmap.Font.Name;
  102. end;
  103. procedure TFormRenderText.Draw;
  104. var
  105. y: integer;
  106. Height: integer;
  107. Size: integer;
  108. Canvas: TCanvas32;
  109. Brush: TSolidBrush;
  110. begin
  111. Image.Bitmap.Clear;
  112. Image.Bitmap.Font.Style := GetFontStyle;
  113. Image.Bitmap.Font.Name := ComboBoxFont.Text;
  114. Canvas := nil;
  115. try
  116. if CheckboxCanvas32.Checked then
  117. begin
  118. Canvas := TCanvas32.Create(Image.Bitmap);
  119. Brush := TSolidBrush(Canvas.Brushes.Add(TSolidBrush));
  120. Brush.FillColor := clWhite32;
  121. Brush.FillMode := pfNonZero;
  122. end;
  123. y := 3;
  124. Size := 6;
  125. while (y < Image.Bitmap.Height) do
  126. begin
  127. Image.Bitmap.Font.Size := Size;
  128. if (Canvas <> nil) then
  129. Canvas.RenderText(10, y, Format('%d: %s', [Size, EditText.Text]))
  130. else
  131. Image.Bitmap.RenderText(10, y, Format('%d: %s', [Size, EditText.Text]), clWhite32, CheckBoxAntiAlias.Checked);
  132. Size := Trunc(Size * 1.2);
  133. Height := Image.Bitmap.TextHeight(EditText.Text);
  134. y := y + MulDiv(Height, 4, 5);
  135. end;
  136. finally
  137. Canvas.Free;
  138. end;
  139. end;
  140. procedure TFormRenderText.ImageResize(Sender: TObject);
  141. begin
  142. Image.SetupBitmap;
  143. Draw;
  144. end;
  145. procedure TFormRenderText.ButtonBenchmarkClick(Sender: TObject);
  146. var
  147. SaveQuality: TFontQuality;
  148. i: Integer;
  149. Str: string;
  150. StopWatch: TStopWatch;
  151. r: TRect;
  152. Size: TSize;
  153. Canvas: TCanvas32;
  154. Brush: TSolidBrush;
  155. SaveFont: string;
  156. Color: TColor32;
  157. Pos: TPoint;
  158. const
  159. MinSize = 10;
  160. MaxSize = 30;
  161. MaxCount = 10000;
  162. begin
  163. Screen.Cursor := crHourGlass;
  164. SaveQuality := Image.Bitmap.Font.Quality;
  165. SaveFont := Image.Bitmap.Font.Name;
  166. Image.Bitmap.Font.Style := GetFontStyle;
  167. if (CheckBoxAntiAlias.Checked) then
  168. Image.Bitmap.Font.Quality := TFontQuality.fqAntialiased
  169. else
  170. Image.Bitmap.Font.Quality := TFontQuality.fqNonAntialiased;
  171. Canvas := nil;
  172. Brush := nil;
  173. try
  174. if CheckboxCanvas32.Checked then
  175. begin
  176. Canvas := TCanvas32.Create(Image.Bitmap);
  177. Brush := TSolidBrush(Canvas.Brushes.Add(TSolidBrush));
  178. Brush.FillMode := pfNonZero;
  179. end;
  180. StopWatch := TStopWatch.StartNew;
  181. Image.Bitmap.BeginUpdate;
  182. for i := 1 to MaxCount do
  183. begin
  184. Image.Bitmap.Font.Size := MinSize + MulDiv(MaxSize-MinSize, i, MaxCount);
  185. Color := Color32(Random(255), Random(255), Random(255), 64+Random(191));
  186. Pos.X := Random(Image.Bitmap.Width + 10) - 10;
  187. Pos.Y := Random(Image.Bitmap.Height + 10) - 10;
  188. if (Canvas <> nil) then
  189. begin
  190. Brush.FillColor := Color;
  191. Canvas.RenderText(Pos.X, Pos.Y, IntToStr(i));
  192. end else
  193. Image.Bitmap.RenderText(
  194. Pos.X,
  195. Pos.Y,
  196. IntToStr(i),
  197. Color);
  198. end;
  199. Image.Bitmap.EndUpdate;
  200. StopWatch.Stop;
  201. finally
  202. Canvas.Free;
  203. end;
  204. Image.Bitmap.Font.Name := 'Verdana';
  205. Image.Bitmap.Font.Style := [];
  206. Image.Bitmap.Font.Size := 8;
  207. Image.Bitmap.Font.Quality := SaveQuality;
  208. Image.Bitmap.Font.Color := clWhite;
  209. str := Format(' %.0n mS ', [StopWatch.ElapsedMilliseconds * 1.0]);
  210. Size := Image.Bitmap.TextExtent(str);
  211. r := Image.Bitmap.BoundsRect;
  212. r.Left := r.Right - Size.cx;
  213. r.Top := r.Bottom - Size.cy;
  214. Image.Bitmap.FillRectS(r, clBlack32);
  215. Image.Bitmap.Textout(r.Left, r.Top, str);
  216. Image.Bitmap.Font.Name := SaveFont;
  217. Screen.Cursor := crDefault;
  218. Image.Invalidate;
  219. end;
  220. procedure TFormRenderText.Changed(Sender: TObject);
  221. begin
  222. Draw;
  223. end;
  224. procedure TFormRenderText.CheckBoxCanvas32Click(Sender: TObject);
  225. begin
  226. CheckBoxAntiAlias.Enabled := not CheckBoxCanvas32.Checked;
  227. Update;
  228. Draw;
  229. end;
  230. end.