* * *

Author Topic: [Solved] Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?  (Read 694 times)

ozznixon

  • Jr. Member
  • **
  • Posts: 69
    • http://www.modernpascal.com/
I am developing a True Color "Win CRT" type of component, and a "WYSIWYG" type of component. I have the foundation in place on the first component, however, Courier New, Lucida nor Consola allow me to render the "IBM High Character Set Graphics"... and as a CRT replacement - it's important I do that. Both of these components use the canvas of the form the are placed on. No need for an additional layer. The support co-existing with other components, etc.

I have found on StackOverflow AddFontResourceEx, but that is a GDI32 direct call - does not work with 64bit, nor other operating systems. I do not mind having to $IFDEF for each OS different code - I just want this to work for us (Lazarus developers) so we have something other than a 16 color Console - and in the WYSIWYG something easier than RichMemo for those of us who do not want to deal with all of the hurdles of font control with it either.

Thank you for any assist!
Ozz
« Last Edit: July 09, 2018, 02:02:22 pm by ozznixon »
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

Thaddy

  • Hero Member
  • *****
  • Posts: 6731
Re: Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?
« Reply #1 on: July 07, 2018, 03:28:19 pm »
Include it as a resource instead of having it on disk. And font handling s tricky but this can be done cross-platform.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

ozznixon

  • Jr. Member
  • **
  • Posts: 69
    • http://www.modernpascal.com/
Re: Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?
« Reply #2 on: July 07, 2018, 04:16:00 pm »
@Thaddy

Two questions:

1. What is the command to load it from file into a resource file?

2. What is the command to load it from Resource so I can use it?

I will debug my way through, I just do not know where to start ... I know it can be done from file, as I have see other products on Github do it - but, the code is always C and Windows.

Thanks for the assist!
Ozz
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

taazz

  • Hero Member
  • *****
  • Posts: 5336
Re: Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?
« Reply #3 on: July 07, 2018, 05:18:28 pm »
you create a text file with one line for the font usually something along the line of
Code: [Select]
LabelFont RT_FONT MyFontFile.ttf
and save it as FontRes.rc in you application you add a line
Code: Pascal  [Select]
  1. {$R FontRes.rc}
  2.  
and that will compile a .res from the rc file and include a resource in your application's exe no in order to load the font at runtime you need to save the resource out to a file in a temp directory. Avoid using the applications directory, you might not have rights to write to it. Eg everything under program files. Then you use windows api tolaod the font. something along the lines of.
Code: Pascal  [Select]
  1. var
  2.   MyResStream: TResourceStream;
  3. begin
  4.   MyResStream:=TResourceStream.Create(hInstance, 'LabelFont', RT_FONT);
  5.   try
  6.     MyResStream.SavetoFile('C:\TEMP\MyFontFile.ttf');
  7.     AddFontResource(PChar('C:\TEMP\MyFontFile.ttf'));  
  8.     SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
  9.   finally
  10.     MyResStream.Free
  11.   end;
  12.  

and you should be able to use it like any other font in the system.
« Last Edit: July 07, 2018, 05:27:15 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ozznixon

  • Jr. Member
  • **
  • Posts: 69
    • http://www.modernpascal.com/
Re: Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?
« Reply #4 on: July 07, 2018, 07:46:45 pm »
@taazz, thanks I will try that on my Windows machine in a few minutes.

I did find for Mac, simple copy the ttf file(s) to either /Library/Fonts or ~/Library/Fonts   ... and my program has access at run-time.

On Linux, it looks like mkdir /usr/local/share/myfolder and put the file(s) in that folder and my program has access at run-time.

I am testing all 3 of these - to see if my tANSIScreen component works on each platform as expected.

Thanks thaddy and taazz!
O.
---
Want to kick the tires to a Free Pascal like script engine? http://www.ModernPascal.com/

sash

  • Jr. Member
  • **
  • Posts: 84
Re: Load TTF from Disk - for Form.Canvas.Font (Windows, Linux and Mac)?
« Reply #5 on: July 10, 2018, 02:40:37 pm »
On Linux, it looks like mkdir /usr/local/share/myfolder and put the file(s) in that folder and my program has access at run-time.

On linux, /usr/... is a root-owned path. I believe it should be /usr/share/fonts/.
For a per-user fonts use ~/.fonts/
Also you could use symlinks.
« Last Edit: July 10, 2018, 02:47:34 pm by sash »
Lazarus 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus