15

I'm working on an app that requires a particular barcode true type font that is unlikely to be on the user's PC.

Can I somehow embed the font in the app, or do I need to use the installer to install the font?

starblue
  • 55,348
  • 14
  • 97
  • 151
ChuckO
  • 2,543
  • 6
  • 34
  • 41

2 Answers2

14

In my opinion the most easy way is to use AddFontMemResourceEx in case the font is embedded as a resource into the EXE. It allows loading the font directly from memory, no need to save the font to file.

Code Example:

function LoadResourceFont( const ResourceName : string ) : boolean;
var
   ResStream : tResourceStream;
   FontsCount : integer;
   hFont : tHandle;
begin
   ResStream := tResourceStream.Create(hInstance, ResourceName, RT_RCDATA);
   hFont := AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
   result := (hFont <> 0);
   ResStream.Free();
end;
blerontin
  • 2,892
  • 5
  • 34
  • 60
  • 1
    Nice. For me the code was " ResStream := tResourceStream.Create(hInstance, ResourceName, RT_FONT);" and not RT_RCDATA. I dragged FontAwesome into the "Melander Resource Editor" and it created a FONT typer resource – Giorgio Calzolato Sep 30 '20 at 07:39
8

Yes, you can save it as a resource in the EXE file, and on user's pc, you can extract it as a file using a TResourceStream instance. Then you can call AddFontResource API function. At last, you should send a WM_FONTCHANGE message to all top-level windows in the system (Check Remark section of AddFontResource description in MSDN website).

If you need an example code, let me know.

vcldeveloper
  • 7,399
  • 2
  • 33
  • 39
  • 2
    Be aware that you might break some copyright laws or license restrictions, if you do not have the redistribution rights for this font. – Warren P Jun 08 '10 at 17:40