Link to home
Start Free TrialLog in
Avatar of PaulKorzycki
PaulKorzycki

asked on

Embedding Fonts

How does one go about embedding a font into an application, so as to make sure that it appears properly in a deployed system?  I would rather not actually install the font into the system, but would rather embed the font info within the EXE.
Avatar of edey
edey

I'm pretty sure that without some major surgery for all your controls you'd have to 'install' the font. OTOH is all you want is to present a single exe that doesn't need to be 'installed' you could just tack the font on the end of the exe. You'd just need a little function to read off the last x bytes (size of the font) and copy it into the font directory if the font is not already present there.

GL
Mike
Create a resource file:

MyFont RT_FONT lucon.ttf

Compile it and include it in your app:


{$R *.DFM}
 {$Resource z:\font.res}

procedure TForm1.Button1Click(Sender: TObject);
var resStream:TREsourcestream;
begin
   resStream:=TREsourcestream.Create(Hinstance,'MYFONT','RT_FONT');
   ResStream.SaveToFile('Z:\myfont.ttf');
   Button1.Font.Name:='z:\Myfont';
   button1.Font.size:=20;
   Showmessage(button1.font.name);
end;



and viola!

Good luck!!
Avatar of PaulKorzycki

ASKER

DrDelphi,

Thanks for the reply!

I've created the resource file and been able to save the resourcestream to a file, but Are you sue you can specify a filename as the font?  The font.name property specifies a Window style font, in which case I'd have to stuff it into the Windows fonts directory (I'd prefer not to).

Is there any way to specify a font by FILENAME instead of "Windows name"?

P

here's someting I used

procedure TfrmDeb.FormCreate(Sender: TObject);
var
FontList: TStringList;
begin
FontList := Screen.Fonts;


if not FontList.Find('Harrington', Index) then
    begin
    Res2 := TResourceStream.Create(hInstance, 'HARRIN', 'RT_FONT');
 try   Res2.SavetoFile(ExtractFilePath(Application.EXEName)+'haring10.ttf');
  finally
  Res2.Free;
  end;
    AddFontResource(PChar(ExtractFilePath(Application.EXEName)+'haring10.ttf'));
    SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
    FontAdded := True;
    end;
end;


procedure TfrmDeb.FormDestroy(Sender: TObject);
begin
if FontAdded then begin
  RemoveFontResource(PChar(ExtractFilePath(Application.EXEName)+'haring10.ttf'));
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
  if FileExists(ExtractFilePath(Application.EXEName)+'haring10.ttf') then
  DeleteFile(ExtractFilePath(Application.EXEName)+'haring10.ttf');
  end;
end;
Oh, I guess I should mention, that code was never used because of the legal issues with Font CopyRights and distribution permisions.
> Is there any way to specify a font by FILENAME instead of "Windows name"?

in Dr_Delphis example he actually sets the Font.Name property to a filename
read it again :)
> in Dr_Delphis example he actually sets the Font.Name
> property to a filename read it again :)

Yeah, but I can't get the example to work!  I'll keep plugging.
PaulKorzycki,
  There is a very good reason why it won't work for you... it doesn't work at all. Seem that when I tested this out, Windows gave me the closest font it could find to the name that I asked for.
DrDelphi,

You were on the right track, I think but from what I've seen in other examples, you need the AddFontResource and SendMessage lines.

Even with these lines though, it doesn't work.  How does Delphi/Windows determine the font name?  I suppose it is probably embedded within the font file, right?  So if I embed 'Arial' for example, I have to refer to it as 'Arial' and not 'MyFont' even though the file I create from the resource stream is called MyFont.ttf?

I wish this were easier!
You're on the right track now... the font name is embedded in the font file itself. I have been able to open a TTF file in a hex editor and change it that way, but apart from that you're pretty much stuck with what the file thinks its name should be. Take a look at Slick812's example... between mine and his you should be able to work this out. I don't see any other options... of course I could be missing something....


ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help Slick,

I was hoping this would be a simpler process, but I'll have to deal with it somehow.  I realize that the copyright issue is a big one.  I got the impression that the windows font name was the filename because of DrDelphi's example, as well as another example I saw on the internet.

Thanks again,
P
we gave up any hope of putting font files in the resource, instead .wmf files are created in a vector graphics program with all the text drawn on them and then displayed in a rectangle with the windows text display control background color on it (or with a bitmap background for special presentations). This looks like the text, but it is read only. You may not gain much effect by having a "Special" font in a TEdit or TMemo.
Sorry for the delay slick, I forgot about this issue a while ago, as a result I'm giving you double the points.