Will using a custom font require installing it?

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Will using a custom font require installing it?

Post by JumpyVB »

I want to use a specific font in my Gambas app.

Is it possible to do so without "installing" it to /usr/share/fonts/ + $ sudo fc-cache -v -f

For example
TextLabel1.Font = Font["./myasetts/fa_solid.ttf"]
doesn't seem to work.

It's a font containing glyph icons. So it would not make sense to install for system wide use.
User avatar
BruceSteers
Posts: 1581
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Will using a custom font require installing it?

Post by BruceSteers »

The Short answer is "Yes" a font needs to be installed, but not necessarily in the systems /usr/share/fonts/ dir

have you tried installing it to ~/.local/share/fonts ?
~/.local/share/fonts will not require root privileges to install there and delete.
Possibly your program can install the font there on load, then use it, then remove it when quitting?

I just tried this and it works....


' Load font during _init() to ensure is loaded before other components
Static Public Sub _init()
  
  If Not Exist(User.Home &/ ".local/share/fonts") Then Mkdir User.Home &/ ".local/share/fonts"
  If Not Exist(User.Home &/ ".local/share/fonts/Autumn Wind.ttf") Then Copy "./Autumn Wind.ttf" To User.Home &/ ".local/share/fonts/Autumn Wind.ttf"
  
End

Public Sub Form_Open()

  Dim tb As TextBox = New TextBox(Me)
  tb.Text = "hello"
  tb.Height = 32

  tb.Font = Font["Autumn Wind, 16"]


End

' Remove font file on exit
Public Sub Form_Close()
  
  If Exist(User.Home &/ ".local/share/fonts/Autumn Wind.ttf") Then Kill User.Home &/ ".local/share/fonts/Autumn Wind.ttf"
  
End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1581
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Will using a custom font require installing it?

Post by BruceSteers »

I also found i did not need to run fc-cache it works without it :)
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Will using a custom font require installing it?

Post by vuott »

With Ubuntu and Linux Mint you can also use the folder "User.Home &/ ".fonts".

Reusing the example of BruceSteers:
Public Sub Form_Open()

  If Not Exist(User.Home &/ ".fonts") Then Mkdir User.Home &/ ".fonts"
  If Not Exist(User.Home &/ ".fonts/FreeMono.ttf") Then Copy "/usr/share/fonts/truetype/freefont/FreeMono.ttf" To User.Home &/ ".fonts/FreeMono.ttf"
  TextArea1.Text = "hello"
  TextArea1.Height = 32
  
  TextArea1.Font = Font["FreeMono, 16"]

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Re: Will using a custom font require installing it?

Post by JumpyVB »

BruceSteers wrote: Monday 18th September 2023 7:40pm The Short answer is "Yes" a font needs to be installed, but not necessarily in the systems /usr/share/fonts/ dir

have you tried installing it to ~/.local/share/fonts ?
~/.local/share/fonts will not require root privileges to install there and delete.
Possibly your program can install the font there on load, then use it, then remove it when quitting?

I just tried this and it works....
' Load font during _init() to ensure is loaded before other components
Static Public Sub _init()
  If Not Exist(User.Home &/ ".local/share/fonts") Then Mkdir User.Home &/ ".local/share/fonts"
  If Not Exist(User.Home &/ ".local/share/fonts/Autumn Wind.ttf") Then Copy "./Autumn Wind.ttf" To User.Home &/ ".local/share/fonts/Autumn Wind.ttf"
End
Public Sub Form_Open()
  Dim tb As TextBox = New TextBox(Me)
  tb.Text = "hello"
  tb.Height = 32
  tb.Font = Font["Autumn Wind, 16"]
End
Yeas I like this solution thank you. And thank you for reintroducing me to ~.local/share/fonts - I had forgot it completely. And a huge plus not having to run fc-cache to use fonts in this location!

PS: I think I will skip removing the font at the end for now. But if I decide to do it, I think I will maybe use the default ramdisk location /dev/shm/ as the temporary store for the font asset and make a link pointing to it from ~.local/share/fonts - To speed things up and preserve ssd wrtite cycles.
Bigfooter
Posts: 5
Joined: Sunday 27th August 2023 4:59pm

Re: Will using a custom font require installing it?

Post by Bigfooter »

I tried different ways to do it without installing but nothing worked. That's why I just install it.
Post Reply