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.
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(Hi nstance,'M YFONT','RT _FONT');
ResStream.SaveToFile('Z:\m yfont.ttf' );
Button1.Font.Name:='z:\Myf ont';
button1.Font.size:=20;
Showmessage(button1.font.n ame);
end;
and viola!
Good luck!!
MyFont RT_FONT lucon.ttf
Compile it and include it in your app:
{$R *.DFM}
{$Resource z:\font.res}
procedure TForm1.Button1Click(Sender
var resStream:TREsourcestream;
begin
resStream:=TREsourcestream
ResStream.SaveToFile('Z:\m
Button1.Font.Name:='z:\Myf
button1.Font.size:=20;
Showmessage(button1.font.n
end;
and viola!
Good luck!!
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
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(hIn stance, 'HARRIN', 'RT_FONT');
try Res2.SavetoFile(ExtractFil ePath(Appl ication.EX EName)+'ha ring10.ttf ');
finally
Res2.Free;
end;
AddFontResource(PChar(Extr actFilePat h(Applicat ion.EXENam e)+'haring 10.ttf'));
SendMessage(HWND_BROADCAST ,WM_FONTCH ANGE,0,0);
FontAdded := True;
end;
end;
procedure TfrmDeb.FormDestroy(Sender : TObject);
begin
if FontAdded then begin
RemoveFontResource(PChar(E xtractFile Path(Appli cation.EXE Name)+'har ing10.ttf' ));
SendMessage(HWND_BROADCAST ,WM_FONTCH ANGE,0,0);
if FileExists(ExtractFilePath (Applicati on.EXEName )+'haring1 0.ttf') then
DeleteFile(ExtractFilePath (Applicati on.EXEName )+'haring1 0.ttf');
end;
end;
procedure TfrmDeb.FormCreate(Sender:
var
FontList: TStringList;
begin
FontList := Screen.Fonts;
if not FontList.Find('Harrington'
begin
Res2 := TResourceStream.Create(hIn
try Res2.SavetoFile(ExtractFil
finally
Res2.Free;
end;
AddFontResource(PChar(Extr
SendMessage(HWND_BROADCAST
FontAdded := True;
end;
end;
procedure TfrmDeb.FormDestroy(Sender
begin
if FontAdded then begin
RemoveFontResource(PChar(E
SendMessage(HWND_BROADCAST
if FileExists(ExtractFilePath
DeleteFile(ExtractFilePath
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 :)
ASKER
> 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.
> 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.
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.
ASKER
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 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
ASKER
Sorry for the delay slick, I forgot about this issue a while ago, as a result I'm giving you double the points.
GL
Mike