fRenderText.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. unit fRenderText;
  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. {$I GR32.inc}
  36. uses
  37. {$IFDEF FPC} LCLType, LResources, {$ELSE} Windows, {$ENDIF}
  38. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  39. GR32, ComCtrls, GR32_Image, Buttons;
  40. type
  41. TFormRenderText = class(TForm)
  42. BtnAntialias1: TSpeedButton;
  43. BtnAntialias2: TSpeedButton;
  44. BtnAntialias3: TSpeedButton;
  45. BtnAntialias4: TSpeedButton;
  46. BtnClearType: TSpeedButton;
  47. BtnClickMe: TButton;
  48. BtnTextOut: TSpeedButton;
  49. EditText: TEdit;
  50. Image: TImage32;
  51. LblAALevel: TLabel;
  52. LblEnterText: TLabel;
  53. PnlControl: TPanel;
  54. procedure FormCreate(Sender: TObject);
  55. procedure BtnClickMeClick(Sender: TObject);
  56. procedure EditTextChange(Sender: TObject);
  57. procedure ImageResize(Sender: TObject);
  58. procedure BtnTextOutClick(Sender: TObject);
  59. public
  60. AALevel: Integer;
  61. procedure Draw;
  62. end;
  63. var
  64. FormRenderText: TFormRenderText;
  65. implementation
  66. {$IFDEF FPC}
  67. {$R *.lfm}
  68. {$ELSE}
  69. {$R *.dfm}
  70. {$ENDIF}
  71. procedure TFormRenderText.FormCreate(Sender: TObject);
  72. begin
  73. Image.SetupBitmap;
  74. with Image.Bitmap.Font do
  75. begin
  76. Name := 'Tahoma';
  77. Size := 20;
  78. Style := [fsBold, fsItalic];
  79. end;
  80. PnlControl.DoubleBuffered := True;
  81. EditText.DoubleBuffered := True;
  82. end;
  83. procedure TFormRenderText.Draw;
  84. begin
  85. with Image do
  86. begin
  87. Bitmap.Clear;
  88. Bitmap.RenderText(10, 10, EditText.Text, AALevel, $FFFFFFFF);
  89. Invalidate;
  90. end;
  91. end;
  92. procedure TFormRenderText.EditTextChange(Sender: TObject);
  93. begin
  94. Draw;
  95. end;
  96. procedure TFormRenderText.ImageResize(Sender: TObject);
  97. begin
  98. Image.SetupBitmap;
  99. Draw;
  100. end;
  101. procedure TFormRenderText.BtnClickMeClick(Sender: TObject);
  102. var
  103. I: Integer;
  104. A, B, C: Int64;
  105. Str: string;
  106. begin
  107. Screen.Cursor := crHourGlass;
  108. {$IFNDEF FPC}
  109. QueryPerformanceFrequency(C);
  110. QueryPerformanceCounter(A);
  111. {$ENDIF}
  112. with Image.Bitmap do
  113. for I := 0 to 100 do
  114. RenderText(
  115. Random(Width - 40),
  116. Random(Height - 40),
  117. IntToStr(Random(100)),
  118. AALevel,
  119. Color32(Random(255), Random(255), Random(255), Random(255)));
  120. {$IFNDEF FPC}
  121. QueryPerformanceCounter(B);
  122. with TBitmap32.Create do
  123. try
  124. Font.Color := clWhite;
  125. Font.Size := 8;
  126. Font.Style := [];
  127. SetSize(100,8);
  128. str := FloatToStrF(1000 * (B - A) / C, ffFixed, 4, 4) + ' ms';
  129. SetSize(TextWidth(str),TextHeight(str));
  130. Textout(0, 0, str);
  131. DrawTo(Image.Bitmap, Image.Bitmap.Width - Width, Image.Bitmap.Height-Height);
  132. finally
  133. Free;
  134. end;
  135. {$ENDIF}
  136. Screen.Cursor := crDefault;
  137. Image.Invalidate;
  138. end;
  139. procedure TFormRenderText.BtnTextOutClick(Sender: TObject);
  140. begin
  141. AALevel := TControl(Sender).Tag;
  142. Draw;
  143. end;
  144. end.