Monday, 6 January 2014

What is TLogFont in Delphi? How to use TLogFont to rotate the texts on Canvas?

What is TLogFont in Delphi? How to use TLogFont to rotate the texts on Canvas?

TLogFont structure / record in Delphi is used to play with the fonts to be displayed on the Canvas. You can change the look and feel of the text. You can change the orientation of the text and rotate it to any degree you want. TLogFont is declared in Winapi.Windows unit in Delphi. Following is the sample TLogFont structure in Delphi.

TLogFont = record;
  lfHeight: Integer;
  lfWidth: Integer;
  lfEscapement: Integer;
  lfOrientation: Integer;
  lfWeight: Integer;
  lfItalic: Byte;
  lfUnderline: Byte;
  lfStrikeOut: Byte;
  lfCharSet: Byte;
  lfOutPrecision: Byte;
  lfClipPrecision: Byte;
  lfQuality: Byte;
  lfPitchAndFamily: Byte;
  lfFaceName: array [0 .. lf_FaceSize - 1] of Byte;
end;

Following is the brief explanation of TLogFont members:
  
lfEscapement: The angle between the font's baseline and escapement vectors, in units of 1/10 degrees. 

lfOrientation: The angle between the font's baseline and the device's x-axis, in units of 1/10 degrees.

lfWeight: One of the following flags specifying the boldness (weight) of the font:

FW_DONTCARE = 0 Default weight.
FW_THIN = 100 Thin weight.
FW_EXTRALIGHT = 200 Extra-light weight.
FW_LIGHT = 300 Light weight.
FW_NORMAL = 400 Normal weight.
FW_MEDIUM = 500 Medium weight.
FW_SEMIBOLD = 600 Semi-bold weight.
FW_BOLD = 700 bold weight.
FW_EXTRABOLD = 800 Extra-bold weight.
FW_HEAVY = 900 Heavy weight.

lfWidth: The average width of the font's characters. If 0, the font mapper tries to determine the best value

lfItalic: A non-zero value if the font is italicized, 0 if not.

lfUnderline: A non-zero value if the font is underlined, 0 if not.

lfStrikeOut: A non-zero value if the font is striked out, 0 if not.

lfQuality: field contains the one of quality flags: Default_Quality, Draft_Quality or Proof_Quality.

lfCharSet: It may be set as ANSI_CharSet, OEM_CharSet or Symbol_CharSet.

lfFaceName: It contains font name like Times New Roman, Verdana etc.

Following is the code to demonstrate how to rotate the text on canvas using TLogFont structure in Delphi:

procedure TForm1.FormPaint(Sender: TObject);
var
  lf: TLogFont;
  tf: TFont;
begin
  with Form1.Canvas do
  begin
    Font.Name := 'Arial';
    Font.Size := 24;
    tf := TFont.Create;
    try
      tf.Assign(Font);
      GetObject(tf.Handle, SizeOf(lf), @lf);
      lf.lfEscapement  := 320;
      lf.lfOrientation := 320;
      SetBkMode(Handle, TRANSPARENT);
      tf.Handle := CreateFontIndirect(lf);
      Font.Assign(tf);
    finally
      tf.Free;
    end;
    TextOut(10, Height div 2, 'Your Rotated Text!');
  end;
end;

No comments:

Post a Comment