首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi GDI+ 學習筆記 (24):字體度量與已安裝字體列舉

     編輯:Delphi

TGPFontFamily 取得 Em 高度、行距、上升/下降距;列舉系統所有字體

字體度量單位為「設計單位」(Design Units),需配合 Font.GetSize (指定單位) 換算成像素。TGPInstalledFontCollection 可列舉系統所有已安裝字體家族名稱。

1. 取得單一字體度量


var
  fontFamily: TGPFontFamily;
  font: TGPFont;
begin
  fontFamily := TGPFontFamily.Create('宋體');
  font := TGPFont.Create(fontFamily, 9, FontStyleRegular, UnitPixel);
  ShowMessage(FloatToStr(font.GetSize)); // 字號大小 (像素)
  ShowMessage(IntToStr(fontFamily.GetEmHeight(FontStyleRegular))); // Em 高度 (設計單位)
  ShowMessage(IntToStr(fontFamily.GetLineSpacing(FontStyleRegular))); // 行距
  ShowMessage(IntToStr(fontFamily.GetCellAscent(FontStyleRegular))); // 上升距
  ShowMessage(IntToStr(fontFamily.GetCellDescent(FontStyleRegular))); // 下降距
  font.Free; fontFamily.Free;
end;

度量換算公式

像素大小 = FontSize * (EmHeight / LineSpacing) (近似)

2. 列舉所有已安裝字體


var
  fonts: TGPFontCollection;
  fArr: array of TGPFontFamily;
  count, ti: Integer;
  s: string;
  i: Integer;
begin
  fonts := TGPInstalledFontCollection.Create;
  count := fonts.GetFamilyCount;
  SetLength(fArr, count);
  for i := 0 to count - 1 do fArr[i] := TGPFontFamily.Create;
  fonts.GetFamilies(count, fArr, ti);
  Memo1.Clear;
  for i := 0 to count - 1 do begin
    fArr[i].GetFamilyName(s);
    Memo1.Lines.Add(s);
    fArr[i].Free;
  end;
  fonts.Free;
end;

FontStyle 常數

Delphi微軟說明
FontStyleRegularRegular正常
FontStyleBoldBold粗體
FontStyleItalicItalic斜體
FontStyleUnderlineUnderline底線
FontStyleStrikeoutStrikeout刪除線

Unit 常數

Delphi微軟單位
UnitPixelPixel裝置像素
UnitPointPoint印表機點 (1/72 吋)
UnitInchInch英吋
UnitMillimeterMillimeter公釐
UnitDocumentDocument文件單位 (1/300 吋)
UnitDisplayDisplay顯示裝置單位
UnitWorldWorld世界座標單位

字體度量是排版引擎、報表產生器、PDF 輸出的基石。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved