Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus Pascal - Linux - How to use a custom font in your Lazarus application

1 Posts
1 Users
0 Reactions
9 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2874
Topic starter  

In one of my applications I use my own font (see also Windows, MacOS) and I wanted use that same TTF font in the Linux binary.

Since I'm not totally convinced this is the "best" way to do this (not a Linux GUI user myself), her what worked for me.

For one your font needs to be copied to "~/.fonts", and I did it this way: 
When the font cannot be found in the directory, the application will create the directory and copy the font (distributed with the executable and placed in the same directory as the executable!), and show a message that the application needs to restart - you could do an automatic restart with a TProcess but for odd reason with my application this kept failing and locking files.

So this is my code, used in the OnCreate event of the main form;

  if not FileExistsUTF8(ExpandFileNameUTF8('~/.fonts')+DirectorySeparator+'yourfont.ttf') then
    begin
      ForceDirectoriesUTF8(ExpandFileNameUTF8('~/.fonts'));

      CopyFile( ExtractFilePath(ParamStr(0))+DirectorySeparator+'yourfont.ttf', 
                ExpandFileNameUTF8('~/.fonts')+DirectorySeparator+'yourfont.ttf' );

      MessageDlg('Installed "yourfont.ttf'' font in "~/.fonts".'+LineEnding+LineEnding+
                 'Closing YourApplicationName now.'+LineEnding+LineEnding+
                 'Please restart this one time so the installed font becomes available.', mtInformation, [mbOK],0);

      Application.Terminate;
    end;               

 

The restart is needed so your application sees the newly copied font.
After the restart it will see that it was installed in "~/.fonts".

 


   
ReplyQuote
Share: