Board index » delphi » Need help determining default font sizes for non-truetype fonts

Need help determining default font sizes for non-truetype fonts

im trying to get the default font sizes for non-truetype fonts, like ms sans
serif, it seems it only lets you do 8, 10, 12, 14, 18, 24... how do i get
those sizes for a font? i need to find out what sizes non-truetype fonts
contain for a custom font dialog i am making. any help is appreciated

andy

 

Re:Need help determining default font sizes for non-truetype fonts


"Andy" <andyt...@earthlink.net> skrev i melding
news:Zuh85.32875$_b3.834030@newsread1.prod.itd.earthlink.net...

Quote
> im trying to get the default font sizes for non-truetype fonts, like ms
sans
> serif, it seems it only lets you do 8, 10, 12, 14, 18, 24... how do i get
> those sizes for a font? i need to find out what sizes non-truetype fonts
> contain for a custom font dialog i am making. any help is appreciated

Hi !

I struggeled with something similar, had to find out what OEM Fixed-Pitch
fonts (the types that may be used for DOS screens) that was installed on the
machine, and what distinct sizes I would get. The trouble is, you request
Windows for a font with a number of characteristics, like pitch, name,
weight, etc., and Windows ALLWAYS returns a font (="font mapping"). But it's
rarely *excactly* the font you asked for ;-).
The solution to my problem was to create fonts of all reasonable sizes,
examine the metrics for the returned font, and build a list of distinct
fonts. If I asked for a "6x6 Courier", I could be given a "Courier 5x7",
which was allready in my list. In that case, the 6x6 was removed from my font
selection list.

<snip>

type

// Holds values for both required and returned height/width
  PFontDef = ^TFontDef;
  TFontDef = record
    FontHandle: HFONT;
    case integer of
      0: (W,
          H      : byte;
          WReq,
          HReq   : byte;
          Bold,
          UserDef: boolean);
      1: (Hi: longint;
          Lo: Word);
  end;

// Test Create fonts

function CreateOEMFont(var ADef: TFontDef): HFONT;
var
  LogF: TLogFont;
  aHDC : HDC;
  HBMP: HBITMAP;
  TM  : TTextMetric;
begin
  with LogF do begin
    FillChar(LogF, sizeOf(TLogFont), 0);
    lfHeight:=ADef.HReq;
    lfWidth:=ADef.WReq;
    if ADef.Bold then
      lfWeight:=FW_BOLD
    else
      lfWeight:=FW_NORMAL;
    lfCharSet:=OEM_CHARSET;
    lfOutPrecision:=OUT_RASTER_PRECIS;
    lfQuality:=PROOF_QUALITY;
    lfPitchAndFamily:=FIXED_PITCH;
  end;
  Result:=CreateFontIndirect(LogF);
  if ADef.W = 0 then begin
    aHDC:=CreateCompatibleDC(0);
    HBMP:=CreateCompatibleBitmap(0, 100, 100);
    SelectObject(aHDC, HBMP);
    SelectObject(aHDC, result);
    GetTextMetrics(aHDC, TM);
    ADef.W:=TM.tmAveCharWidth;
    ADef.H:=TM.tmHeight;
    DeleteDC(aHDC);
    DeleteObject(HBMP);
  end;
end;

function FtDef(AW, AH, AWReq, AHReq: integer; ABold, AUserDef: boolean):
TFontDef;
begin
  with Result do begin
    W:=AW;
    H:=AH;
    WReq:=AWReq;
    HReq:=AHReq;
    Bold:=ABold;
    UserDef:=AUserDef;
  end;
end;

// Test create a number of fonts, examine result font - Add in list only the
distinct sizes

procedure GetDosFonts;

  procedure AddFont(Width, Height: integer; ABold: boolean);
  var
    PDef: PFontDef;
    Tmp : string;
  begin
    PDef:=AllocMem(SizeOf(TFontDef));
    with PDef^ do begin
      WReq:=Width;
      HReq:=Height;
      Bold:=ABold;
      UserDef:=false;
      Tmp:=IntToStr(Width)+' x ';
      if Width < 10 then
        Tmp:='0'+Tmp;
      if Height < 10 then
        Tmp:=Tmp+'0';
      Tmp:=Tmp+IntToStr(Height);
      DosFonts.AddObject(Tmp, TObject(PDef));
      FontHandle:=CreateOEMFont(PDef^);
      if (PDef^.W <> Width) or (PDef^.H <> Height) then begin
        FreeMem(PDef);
        DosFonts.Delete(DosFonts.Count-1);
      end;
    end;
  end;

begin
  DosFonts:=TStringList.create;

// List built from experience, but I would never know if all these font sizes
// could be returned on a particular machine

  AddFont(4, 6, NORMAL);
  AddFont(5, 12, NORMAL);
  AddFont(7, 9, NORMAL);
  AddFont(6, 8, BOLD);
  AddFont(7, 12, NORMAL);
  AddFont(7, 14, NORMAL);
  AddFont(7, 15, NORMAL);
  AddFont(8,  8, NORMAL);
  AddFont(8, 12, NORMAL);
  AddFont(8, 16, NORMAL);
  AddFont(10, 18, NORMAL);
  AddFont(10, 20, NORMAL);
  AddFont(12, 16, NORMAL);
  AddFont(12, 22, NORMAL);
  DosFonts.sorted:=true;
end;

<snip>

Hope this was of some help....

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Other Threads