Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Installing True Type Font in Windows & MacOS - Berlin


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 13 - Last Post: Jan 27, 2017 3:43 PM Last Post By: Antonio Estevez
Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Installing True Type Font in Windows & MacOS - Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 1:35 PM
As per [True Type Fonts|https://community.embarcadero.com/index.php/blogs/entry/developer-skill-sprint-customize-the-look-and-feel-of-ui-elements-with-the-bitmap-style-designer-and-true-type-fonts] mentioned how to install Fonts in Android and iOS. But how can I install in Windows & MacOS? I have to use Firemonkey so no Win API.

Edited by: Md. Shariful Alam Khan on Jan 26, 2017 3:36 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 2:08 PM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Shariful wrote:

But how can I install in Windows & MacOS?
I have to use Firemonkey so no Win API.

Font installation is a platform-specific operation, and FireMonkey does not
provide any cross-platform means of installing fonts, so you have no choice
but to deploy the TTF file with your app and then use platform-specific APIs
to install the TTF file into the OS as needed, such as with the Win32 API
AddFontResource/Ex() function on Windows. Just be sure to {$IFDEF} your
installation code accordingly.

--
Remy Lebeau (TeamB)
Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2017 11:11 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Shariful wrote:

But how can I install in Windows & MacOS?
I have to use Firemonkey so no Win API.

Font installation is a platform-specific operation, and FireMonkey does not
provide any cross-platform means of installing fonts, so you have no choice
but to deploy the TTF file with your app and then use platform-specific APIs
to install the TTF file into the OS as needed, such as with the Win32 API
AddFontResource/Ex() function on Windows. Just be sure to {$IFDEF} your
installation code accordingly.

--
Remy Lebeau (TeamB)

I tried to use as following for windows with compiler directive:
AddFontResource('FontAwesome.TTF') ;
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;

but it's not showing FontAwesome fonts on non installed font PC
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 11:19 AM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Shariful wrote:

I tried to use as following for windows with compiler directive:
AddFontResource('FontAwesome.TTF') ;
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
but it's not showing FontAwesome fonts on non installed font PC

Are you really giving it a relative file path? If so, the file is likely
not located where you expect it to be, depending on how you are deploying
it. Always use absolute file paths.

Is AddFontResource() returning an error? You are not checking for that, eg:

if AddFontResource('FontAwesome.TTF') > 0 then begin
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end else
begin
  // failed to install!
end;


Also, pay attention to the following note in the documentation:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183326.aspx

This function installs the font only for the current session. When the system
restarts, the font will not be present. To have the font installed even after
restarting the system, the font must be listed in the registry.

A font listed in the registry and installed to a location other than the
%windir%\fonts\ folder cannot be modified, deleted, or replaced as long as
it is loaded in any session. In order to change one of these fonts, it must
first be removed by calling RemoveFontResource, removed from the font registry
(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts),
and the system restarted. After restarting the system, the font will no longer
be loaded and can be changed.

--
Remy Lebeau (TeamB)
Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 2:06 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Shariful wrote:

I tried to use as following for windows with compiler directive:
AddFontResource('FontAwesome.TTF') ;
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
but it's not showing FontAwesome fonts on non installed font PC

Are you really giving it a relative file path? If so, the file is likely
not located where you expect it to be, depending on how you are deploying
it. Always use absolute file paths.

Is AddFontResource() returning an error? You are not checking for that, eg:

if AddFontResource('FontAwesome.TTF') > 0 then begin
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end else
begin
  // failed to install!
end;


Also, pay attention to the following note in the documentation:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183326.aspx

This function installs the font only for the current session. When the system
restarts, the font will not be present. To have the font installed even after
restarting the system, the font must be listed in the registry.

A font listed in the registry and installed to a location other than the
%windir%\fonts\ folder cannot be modified, deleted, or replaced as long as
it is loaded in any session. In order to change one of these fonts, it must
first be removed by calling RemoveFontResource, removed from the font registry
(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts),
and the system restarted. After restarting the system, the font will no longer
be loaded and can be changed.

--
Remy Lebeau (TeamB)

I did as following:
procedure TForm20.FormCreate(Sender: TObject);
begin
{$IFDEF MSWINDOWS}
if AddFontResource('FontAwesome.ttf') > 0 then
begin
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
Showmessage('installed!');
end
else
begin
Showmessage('failed to install!');
end;
{$ENDIF}
end;

I got installed message but the fonts are not showing. In MacOS as per [http://stackoverflow.com/questions/28996039/loading-a-custom-true-type-font] works perfectly.
Hans Jakobsen

Posts: 103
Registered: 9/10/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 4:19 PM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Firemonkey does not currently support dynamic loading of fonts on Windows. You have to install the font in Windows so it is available before the application loads.

See:
https://quality.embarcadero.com/browse/RSP-16207
and
http://stackoverflow.com/questions/21433459/firemonkey-adding-a-font-from-resource-to-memory-and-using-it
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 4:53 PM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Shariful wrote:

I did as following:
<snip>
I got installed message but the fonts are not showing.

Where are you looking exactly? Are you expecting your Form's controls to
automatically pick up the font at runtime? If so, they won't be able to
because you are installing the font too late. The Form's OnCreate event
is fired after the Form's design-time controls are streamed in. You would
have to install the font at app startup before creating the TForm20 object.

--
Remy Lebeau (TeamB)
Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 11:33 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Shariful wrote:

I did as following:
<snip>
I got installed message but the fonts are not showing.

Where are you looking exactly? Are you expecting your Form's controls to
automatically pick up the font at runtime? If so, they won't be able to
because you are installing the font too late. The Form's OnCreate event
is fired after the Form's design-time controls are streamed in. You would
have to install the font at app startup before creating the TForm20 object.

--
Remy Lebeau (TeamB)

Still not working, did as following:
unit uaddfont;

interface

uses
FMX.Dialogs{$IFDEF MSWINDOWS}, Winapi.Windows, Winapi.Messages{$ENDIF};

procedure AddFonts;

implementation

procedure AddFonts;
begin
{$IFDEF MSWINDOWS}
if AddFontResource('FontAwesome.ttf') > 0 then
begin
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end
else
begin
Showmessage('failed to install!');
end;
{$ENDIF}
end;

program GridPanelHomeScreen;

uses
System.StartUpCopy,
FMX.Forms,
HomeScreenNavigation_GridPanel in 'HomeScreenNavigation_GridPanel.pas' {Form20},
FMX.FontGlyphs.Android in 'FMX.FontGlyphs.Android.pas',
FontAwesomeCodes in 'FontAwesomeCodes.pas',
uaddfont in 'uaddfont.pas';

{$R *.res}

begin
Application.Initialize;
AddFonts;
Application.CreateForm(TForm20, Form20);
Application.Run;
end.

So I belive have to install it by installer, by code it won't install.
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2017 12:14 AM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
El 27/01/2017 a las 8:33, Md. Shariful Alam Khan escribió:
Remy Lebeau (TeamB) wrote:
Shariful wrote:

Still not working, did as following:
unit uaddfont;

interface

uses
FMX.Dialogs{$IFDEF MSWINDOWS}, Winapi.Windows, Winapi.Messages{$ENDIF};

procedure AddFonts;

implementation

procedure AddFonts;
begin
{$IFDEF MSWINDOWS}
if AddFontResource('FontAwesome.ttf') > 0 then
begin
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end
else
begin
Showmessage('failed to install!');
end;
{$ENDIF}
end;

Try calling CreateScalableFontResource to create a .fot file and then register the .fot font file:

uses
   System.IOUtils;
 
procedure AddFonts;
{$IFDEF MSWINDOWS}
var
   TtfFile: String;
   FotFile: String;
   Exists : Boolean;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
   // Assuming that FontAwesome.ttf is in the same folder as the executable
   TtfFile:= TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), 'FontAwesome.ttf');
   FotFile:= TPath.ChangeExtension(TtfFile, '.fot');
 
   if not TFile.Exists(FotFile) then
     Exists:= CreateScalableFontResource(0, PChar(FotFile), PChar(TtfFile), nil)
   else
     Exists:= True;
 
   if Exists and (AddFontResource(PChar(FotFile)) > 0) then
   begin
     SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
   end
   else
   begin
     Showmessage('failed to install!');
   end;
{$ENDIF}
end;

Note that this example is only for testing. In a release version creating the .fot file in the same folder as the executable probably will fail.

Md. Shariful Al...

Posts: 70
Registered: 9/9/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2017 4:13 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio Estevez wrote:
El 27/01/2017 a las 8:33, Md. Shariful Alam Khan escribió:
Remy Lebeau (TeamB) wrote:
Shariful wrote:

Still not working, did as following:
unit uaddfont;

interface

uses
FMX.Dialogs{$IFDEF MSWINDOWS}, Winapi.Windows, Winapi.Messages{$ENDIF};

procedure AddFonts;

implementation

procedure AddFonts;
begin
{$IFDEF MSWINDOWS}
if AddFontResource('FontAwesome.ttf') > 0 then
begin
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end
else
begin
Showmessage('failed to install!');
end;
{$ENDIF}
end;

Try calling CreateScalableFontResource to create a .fot file and then register the .fot font file:

uses
   System.IOUtils;
 
procedure AddFonts;
{$IFDEF MSWINDOWS}
var
   TtfFile: String;
   FotFile: String;
   Exists : Boolean;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
   // Assuming that FontAwesome.ttf is in the same folder as the executable
   TtfFile:= TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), 'FontAwesome.ttf');
   FotFile:= TPath.ChangeExtension(TtfFile, '.fot');
 
   if not TFile.Exists(FotFile) then
     Exists:= CreateScalableFontResource(0, PChar(FotFile), PChar(TtfFile), nil)
   else
     Exists:= True;
 
   if Exists and (AddFontResource(PChar(FotFile)) > 0) then
   begin
     SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
   end
   else
   begin
     Showmessage('failed to install!');
   end;
{$ENDIF}
end;

Note that this example is only for testing. In a release version creating the .fot file in the same folder as the executable probably will fail.


No it's not working also tried run the application as Run As Administrator.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2017 12:40 PM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio wrote:

Try calling CreateScalableFontResource to create a .fot
file and then register the .fot font file:

Why? AddFontResource() supports .ttf files.

And besides, CreateScalableFontResource() has been deprecated since XP, and
it requires the .ttf file to exist in the Windows System folder, which
means the installing app must be run elevated as an admin.

--
Remy Lebeau (TeamB)
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2017 3:43 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
El 27/01/2017 a las 21:40, Remy Lebeau (TeamB) escribió:
Antonio wrote:

Try calling CreateScalableFontResource to create a .fot
file and then register the .fot font file:

Why? AddFontResource() supports .ttf files.

It was just a suggestion.
The CreateScalableFontResource function was the only solution I found a few years ago for an application that drew text
on both the screen and the printer using GDI+. The problem was mainly when printing the text.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Installing True Type Font in Windows & MacOS - Berlin [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2017 12:37 PM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Shariful wrote:

Still not working

In what way exactly? You still have not explained WHAT EXACTLY is not working?

And again, why are you passing a relative path to the .ttf file to AddFontResource()?
You should be passing an absolute path instead. Where exactly is the file
being deployed to?

So I belive have to install it by installer, by code it won't install.

Not true. The whole point of AddFontResource() is to allow dynamic installation.
This is clearly explained on MSDN:

Font Installation and Deletion
https://msdn.microsoft.com/en-us/library/dd144833.aspx

--
Remy Lebeau (TeamB)
Michael Leahy

Posts: 239
Registered: 5/9/07
Re: Installing True Type Font in Windows & MacOS - Berlin  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2017 4:59 AM   in response to: Md. Shariful Al... in response to: Md. Shariful Al...
Md. Shariful Alam Khan wrote:
As per [True Type Fonts|https://community.embarcadero.com/index.php/blogs/entry/developer-skill-sprint-customize-the-look-and-feel-of-ui-elements-with-the-bitmap-style-designer-and-true-type-fonts] mentioned how to install Fonts in Android and iOS. But how can I install in Windows & MacOS? I have to use Firemonkey so no Win API.

http://stackoverflow.com/questions/28996039/loading-a-custom-true-type-font has the important bits for MacOS. You don't have to manually edit the info.plist file. The pair can be edited inside of your Delphi project.

For Windows I use Inno Setup which installs the TTF fonts as it installs my app.

Here is another expanded answer in this forum: https://forums.embarcadero.com/thread.jspa?messageID=826402

Edited by: Michael Leahy on Jan 26, 2017 6:15 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02