home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / FONTSAMP / DRAWPAGE.PAS < prev    next >
Pascal/Delphi Source File  |  1998-04-14  |  6KB  |  223 lines

  1. unit Drawpage;
  2.  
  3. interface
  4.  
  5. uses SysUtils, Graphics, StdCtrls;
  6.  
  7. { Call DrawOnePage to form each sampler page either during
  8. printing or for previewing with an offscreen bitmap. }
  9.  
  10. procedure DrawOnePage(
  11.   Canvas: TCanvas;        { Printer or TBitmap Canvas for preview }
  12.   FontListBox: TListBox;  { List of fonts with multiple selections }
  13.   Page,                   { Page number (FontList selection index) }
  14.   PageWidth,              { Unscaled page width in pixels }
  15.   PageHeight: Integer;    { Unscaled page height in pixels }
  16.   Previewing: Boolean;    { True if previewing; else printing }
  17.   PpiX, PpiY: Integer     { Pixels per inch on X and Y axes }
  18. );
  19.  
  20. implementation
  21.  
  22. uses Main;
  23.  
  24. var
  25.   C: TCanvas;
  26.   FontName, HeaderName: String;
  27.   PixelsPerInchX, PixelsPerInchY: Integer;
  28.   Preview: Boolean;
  29.  
  30. { Return selected font at index }
  31. function SelectedFont(ListBox: TListBox; Index: Integer): String;
  32. var
  33.   I: Integer;
  34. begin
  35.   with ListBox do
  36.   for I := 0 to Items.Count - 1 do
  37.   if Selected[I] then
  38.   begin
  39.     Dec(Index);
  40.     if Index <= 0 then
  41.     begin
  42.       Result := Items[I];
  43.       Exit;
  44.     end;
  45.   end;
  46.   Result := 'System';  { Shouldn't happen, but... }
  47. end;
  48.  
  49. { Assign font name, style, and size to Canvas font }
  50. procedure SetFont(const Name: String; Style: TFontStyles;
  51.   Size: Integer);
  52. begin
  53. { Adjust point size for preview page's logical pixels per
  54. inch relative to the form's actual pixels per inch. This allows
  55. the program to draw into the bitmap with TextOut, and then
  56. display the bitmap in real size with Canvas.Draw. Some print
  57. previewers use StretchDraw, which produces relatively poor results.}
  58.   if Preview then
  59.     Size := Round(Size * (PixelsPerInchY / MainForm.PixelsPerInch));
  60. { Assign parameters to Canvas C Font property }
  61.   C.Font.Name := Name;
  62.   C.Font.Style := Style;
  63.   C.Font.Size := Size;
  64. end;
  65.  
  66. { Return pixel width of Name in inches }
  67. function InchWidth(const Name: String): Double;
  68. begin
  69.   Result := C.TextWidth(Name);
  70.   Result := Result / PixelsPerInchX;
  71. end;
  72.  
  73. { Return pixel height of Name in inches }
  74. function InchHeight(const Name: String): Double;
  75. begin
  76.   Result := C.TextHeight(Name);
  77.   Result := Result / PixelsPerInchY;
  78. end;
  79.  
  80. { Write string S at inch coordinates X and Y }
  81. procedure TextAtInch(X, Y: Double; const S: String);
  82. var
  83.   Px, Py: Integer;
  84. begin
  85.   Px := Round(X * PixelsPerInchX);
  86.   Py := Round(Y * PixelsPerInchY);
  87.   C.TextOut(Px, Py, S);
  88. end;
  89.  
  90. { Draw a line at inch coordinates X1, Y1, X2, Y2 }
  91. procedure LineAtInch(X1, Y1, X2, Y2: Double);
  92. var
  93.   Px1, Py1, Px2, Py2: Integer;
  94. begin
  95.   Px1 := Round(X1 * PixelsPerInchX);
  96.   Py1 := Round(Y1 * PixelsPerInchY);
  97.   Px2 := Round(X2 * PixelsPerInchX);
  98.   Py2 := Round(Y2 * PixelsPerInchY);
  99.   C.MoveTo(Px1, Py1);
  100.   C.LineTo(Px2, Py2);
  101. end;
  102.  
  103. { Draw header at top of page }
  104. procedure DrawHeader(const Name: String);
  105. var
  106.   S: String[24];
  107. begin
  108.   SetFont(HeaderName, [fsBold], 12);
  109.   TextAtInch(0.5, 0.5, Name);
  110.   SetFont(HeaderName, [fsItalic], 12);
  111.   S := 'Font Sampler by Tom Swan';
  112.   TextAtInch(8.0 - InchWidth(S), 0.5, S);
  113.   LineAtInch(0.5, 0.5, 8.0, 0.5);
  114. end;
  115.  
  116. { Draw footer at bottom of page }
  117. procedure DrawFooter(Page: Integer);
  118. begin
  119.   SetFont(HeaderName, [], 12);
  120.   TextAtInch(0.5, 10.5, 'Page ' + IntToStr(Page));
  121. end;
  122.  
  123. { Draw sample character set (Ascii 32-255) }
  124. procedure DrawCharacterSet;
  125. var
  126.   H: Double;
  127.   procedure DrawOneLine(J, K: Integer);
  128.   var
  129.     I: Integer;
  130.     S: String;
  131.   begin
  132.     S := '';
  133.     for I := J to K do
  134.       S := S + Chr(I);
  135.     TextAtInch(0.5, H, S);
  136.     H := H + InchHeight('M');
  137.   end;
  138. begin
  139.   SetFont(HeaderName, [fsBold], 12);
  140.   TextAtInch(0.5, 1.4, 'Character set:');
  141.   SetFont(FontName, [], 10);
  142.   H := 1.5 + InchHeight('M');
  143.   DrawOneLine(32, 80);
  144.   DrawOneLine(81, 129);
  145.   DrawOneLine(130, 178);
  146.   DrawOneLine(179, 227);
  147.   DrawOneLIne(228, 256);
  148. end;
  149.  
  150. { Draw sample text in 8, 14, and 24 point sizes }
  151. procedure DrawPointSamples;
  152. var
  153.   H, M: Double;
  154.   procedure DrawOneSample(Pts: Integer);
  155.   begin
  156.     SetFont(HeaderName, [fsBold], 12);
  157.     TextAtInch(0.5, H, IntToStr(Pts) + ' points:');
  158.     M := InchHeight('M');
  159.     H := H + M;
  160.     SetFont(FontName, [], Pts);
  161.     TextAtInch(0.5, H, 'AaBbCc 1234567890');
  162.     H := H + M * 2;
  163.   end;
  164. begin
  165.   H := 4.0;
  166.   DrawOneSample(8);
  167.   DrawOneSample(14);
  168.   DrawOneSample(24);
  169. end;
  170.  
  171. { Draw normal, italic, bold, and bold-italic samples }
  172. procedure DrawNormBoldItal;
  173. var
  174.   H, M: Double;
  175.   procedure DrawOneLine(const S: String; Style: TFontStyles);
  176.   begin
  177.     SetFont(HeaderName, [fsBold], 12);
  178.     TextAtInch(0.5, H, S);
  179.     M := InchHeight('M');
  180.     H := H + M;
  181.     SetFont(FontName, Style, 12);
  182.     TextAtInch(0.5, H, 'AaBbCc 1234567890');
  183.     H := H + M * 2;
  184.   end;
  185. begin
  186.   H := 7.0;
  187.   DrawOneLine('Normal 12 points:', []);
  188.   DrawOneLine('Italic 12 points:', [fsItalic]);
  189.   DrawOneLine('Bold 12 points:', [fsBold]);
  190.   DrawOneLine('Bold Italic 12 points:', [fsBold, fsItalic]);
  191. end;
  192.  
  193. { Printing and preview code calls this procedure to draw
  194. each page. See declaration at top of file for descriptions
  195. of the parameters. }
  196. procedure DrawOnePage(Canvas: TCanvas; FontListBox: TListBox;
  197.   Page, PageWidth, PageHeight: Integer; Previewing: Boolean;
  198.   PpiX, PpiY: Integer);
  199. begin
  200. { Save some parameters in global variables for easy access }
  201.   C := Canvas;
  202.   C.Pen.Color := clBlack;
  203.   PixelsPerInchX := PpiX;
  204.   PixelsPerInchY := PpiY;
  205.   Preview := Previewing;
  206. { Draw the font samples on the Canvas (Printer or Preview) }
  207.   with Canvas do
  208.   begin
  209.     FillRect(ClipRect);  { Erase page }
  210.     if (FontListBox = nil) or (FontListBox.SelCount < 1) then
  211.       Exit;  { Display / print blank page if no font selected }
  212.     FontName := SelectedFont(FontListBox, Page);
  213.     HeaderName := 'Arial';
  214.     DrawHeader(FontName);  { These statements draw the }
  215.     DrawFooter(Page);      { header, footer, and font samples }
  216.     DrawCharacterSet;
  217.     DrawPointSamples;
  218.     DrawNormBoldItal;
  219.   end;
  220. end;
  221.  
  222. end.
  223.