How to read the 'Title' property of the Details Tab of a FONT TRUE TYPE file?

2

I'm developing a small application that reads a .MDB, of which there are font names. So I check if these fonts are installed, if not, I install them. But I have to know the name of the Font Title that is inside the properties / Datalhes screen to know exactly the font to be inserted. Since I have file name="x.ttf" and name in the details tab "x.ttf", ai ok works fine, but when I have "x.ttf" = filename and "xx.ttf" in the property there everything goes wrong.

    
asked by anonymous 13.03.2014 / 04:16

1 answer

2

It's worth answering for the history and if anyone needs it in the future.

The simplest way is to install, verify and uninstall, thus accessing the font name. Here is the routine:

Function NomedaFonte(Font: WideString):WideString;
type
  TGetFontResourceInfoW = function(Name: PWideChar; var BufSize: Cardinal;
    Buffer: pointer; InfoType: Cardinal): LongBool; stdcall;
var
  naf, k, y: integer;
  gfri: TGetFontResourceInfoW;
  lf: array of TLogFontW;
  lfsz: Cardinal;
  hfnt: HFONT;
  Nome: WideString;
begin
  gfri := GetProcAddress(GetModuleHandle('gdi32.dll'), 'GetFontResourceInfoW');
  if @gfri = nil then
    raise Exception.Create('GetFontResourceInfoW in gdi32.dll não funciona.');
  if LowerCase(ExtractFileExt(Font)) = '.pfm' then
    Font := Font + '|' + ChangeFileExt(Font, '.pfb');
  naf := AddFontResourceW(PWideChar(Font)); //carrega fonte
  try
    if naf > 0 then
    begin
      SetLength(lf, naf);
      lfsz := naf * SizeOf(TLogFontW);
      if not gfri(PWideChar(Font), lfsz, @lf[0], 2) then
        raise Exception.Create('GetFontResourceInfoW não pode ser chamado.');
      y := 0;
      naf := lfsz div SizeOf(TLogFont);
      for k := 0 to naf - 1 do
      begin
        hfnt := CreateFontIndirectW(lf[k]);
        try
          Nome := lf[k].lfFaceName; //Pega nome da fonte
          Result := Nome;
        finally
          DeleteObject(hfnt);
        end;
      end;
    end;
  finally
    RemoveFontResourceW(PWideChar(Font)); //Descarrega fonte
  end;
end;

Source (in German).

    
26.05.2014 / 22:19