2008. augusztus 11., hétfő

How to tell if a selected font is a True Type font


Problem/Question/Abstract:

How to tell if a selected font is a True Type font

Answer:

Solve 1:

uses
  SysUtils, WinTypes, Classes, Forms, Dialogs, StdCtrls, Controls;

function isTrueType(FontName: string): Boolean;
  procedure Button1Click(Sender: TObject);

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if isTrueType('Courier') then
        showmessage('Is a true type font')
      else
        showmessage('Not true type');
    end;

    function EnumFontFamProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
      FontType: Integer; TF: TForm1): Integer; export; stdcall;
    begin
      Result := 1;
      if FontType and TRUETYPE_FONTTYPE > 0 then
        Result := 0; {stop enumerating}
    end;

    function TForm1.isTrueType(FontName: string): Boolean;
    var
      Buffer: array[0..255] of Char;
    begin
      StrPCopy(Buffer, FontName);
      result := (EnumFontFamilies(Canvas.Handle, Buffer, @EnumFontFamProc, LongInt(Self)) = false);
    end;


Solve 2:

function IsTrueType(const FaceName: string): Boolean;
var
  Canvas: TCanvas;
  DC: THandle;
  TextMetric: TTextMetric;
begin
  Canvas := TCanvas.Create;
  try
    DC := GetDC(GetDesktopWindow);
    try
      Canvas.Handle := DC;
      Canvas.Font.Name := FaceName;
      GetTextMetrics(Canvas.Handle, TextMetric);
      Result := (TextMetric.tmPitchAndFamily and TMPF_TRUETYPE) <> 0;
    finally
      ReleaseDC(GetDesktopWindow, DC);
    end;
  finally
    Canvas.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése