The International Simutrans Forum

 

Author Topic: Patch: Looking for fonts in subfolders (unix)  (Read 1742 times)

0 Members and 1 Guest are viewing this topic.

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Patch: Looking for fonts in subfolders (unix)
« on: April 16, 2020, 11:24:53 PM »
Although Simutrans has had TrueType font suport for some time, it was apparently unable to find the fonts installed in my linux system. Looking at the code everything seemed fine, it was looking for fonts in /usr/share/fonts/ (among others). However, all my fonts are installed in subfolders (like /usr/share/fonts/TTF), and Simutrans didn't look at subfolders! This patch fix this, among other things:

  • Change the predefined list of font folders. Some are not used nowadays or just deprecated (like ~/.fonts/ which is replaced by ~/.local/share/fonts/) (
  • Look for subfolders (only one level deep) at runtime making use of dirent.h (should be present in all unix-like systems)
  • Avoid the use of ~ to specify the path of the folder. This was not working, so getenv("HOME") is used instead. I've applied the same logic to Apple and BeOS systems (is anyone still using BeOS?)

My goal with this patch was only to fix the search of fonts without dealing with the implementation of the font system - just by fixing the list of folders it needs. And so it does. However, the implementation of the function which receives the list of folders is... suboptimal. I haven't looked at it in detail, but it seems that the function iterates from the first folder visited to the first not visited folder everytime it request another folder to search for fonts. For example, if we have:

/usr/share/fonts/
/usr/share/fonts/noto/
/usr/share/fonts/TTF/

1) The first iteration:

Visit /usr/share/fonts/ and end (not visited before).

2) The second iteration:

Visit /usr/share/fonts/ and continue (second time - already visited).
Visit /usr/share/fonts/noto/ and end (not visited before).

3) The third iteration:

Visit /usr/share/fonts/ and continue (third time - already visited).
Visit /usr/share/fonts/noto/ and continue (second time - already visited).
Visit /usr/share/fonts/TTF/ and end (not visited before).

And so on! Which each additional folder loading times get exponentially worse (it just wasn't noticeable before because it didn't have to iterate much), so the next big thing would be to solve this. But that's work for another patch.
« Last Edit: April 17, 2020, 02:59:43 PM by Roboron »

Offline ceeac

  • Devotee
  • *
  • Posts: 247
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #1 on: April 17, 2020, 06:07:45 AM »
Please use tabs for indentation, not spaces.
There is also a bug: result is a stack variable; after the function returns, the returned pointer references invalid memory.

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #2 on: April 17, 2020, 02:58:36 PM »
Please use tabs for indentation, not spaces.
There is also a bug: result is a stack variable; after the function returns, the returned pointer references invalid memory.

Weird, I've always used tabs for indentation. Something went wrong with my editor, I guess. This time it is all tabs.

You are right, I've also changed it so it reserves the memory it needs.


Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #3 on: April 23, 2020, 02:43:19 PM »
On rasberrian, the ~/.font/ folder seems still be in use according to google. Some for other distros with the other paths. With Linux, there are really often no bets and the LSD never ever took off. Simply remove the path will break it on some of those distros.

I added a slightly different code without memory leak that will also iterate one level down through al;l folders, and each folder exactly once in r9041. Please try it out.



Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #4 on: April 24, 2020, 09:50:07 PM »
Please try it out.

It is working perfectly! I can finally play with Comic Sans  :D



One thing to note is that the path displayed in the GUI is not correct at all. It is appending the simutrans font folder (I guess) before the actual path (in the above image, /usr/share/fonts/TTF ). But that bug corresponds to another code not related to this patch, I guess.

EDIT: Wait. Since your patch doesn't include the ~ path fix (using getenv("HOME") instead of ~ to specify the home folder), Simutrans is not looking for fonts in ~/.local/share/fonts/ (nor in ~/.fonts/). Please review this (>人<;)
« Last Edit: April 24, 2020, 10:01:31 PM by Roboron »

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5691
  • Languages: EN, NO
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #5 on: April 25, 2020, 04:04:12 PM »
I have an /etc/fonts/fonts.conf that lists where fonts are, among other things. It is apparently part of something called fontconfig. I don't know how widely available that is.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #6 on: April 26, 2020, 01:13:39 PM »
Not too widely, it seems, and it depends strongly on the release.

I do not understand why ~ is not working. Anyway, test r9053
« Last Edit: April 26, 2020, 01:32:11 PM by prissi »

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #7 on: May 05, 2020, 11:51:40 AM »
I do not understand why ~ is not working. Anyway, test r9053

It is still not working :/

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #8 on: May 05, 2020, 12:25:07 PM »
Sorry, there was a typo. So the string was composed correctly, but then the original wrong one was fed to opendir. r9065 should fix this. Please try again.

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #9 on: May 07, 2020, 09:59:47 PM »
Sorry, there was a typo. So the string was composed correctly, but then the original wrong one was fed to opendir. r9065 should fix this. Please try again.

It is still not finding my fonts located at ~/.local/share/fonts

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #10 on: May 08, 2020, 02:07:09 AM »
But the path is definitvely passed to the search.I will try it in a virtualo amchine to see what happens. Maybe opendir does not like to closing slash?

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #11 on: May 08, 2020, 10:11:56 AM »
But the path is definitvely passed to the search.I will try it in a virtualo amchine to see what happens. Maybe opendir does not like to closing slash?

I've added some printf to debug and I have found this:

Quote
trying the path: /home/rober/simutrans/.fonts/
trying the path: /home/rober/simutrans/.local/share/fonts/

The function you used, *dr_query_homedir(), does not return the home user directory, but the simutrans "home" directory.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #12 on: May 08, 2020, 01:32:40 PM »
Ups, ok try r9075. I added a "../" after the homedir. I hope this is enough.

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #13 on: May 08, 2020, 10:00:33 PM »
Ups, ok try r9075. I added a "../" after the homedir. I hope this is enough.

Not a valid solution it seems xD

Quote
trying path: /home/rober/simutrans/../.fonts/
trying path: /home/rober/simutrans/../.local/share/fonts/

I think you will need to implement a function that actually returns the home user directory.
« Last Edit: May 08, 2020, 10:13:12 PM by Roboron »

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4869
  • Languages: EN, DE, AT
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #14 on: May 09, 2020, 08:22:42 AM »
why not make this configurable by user? i.e. let them specify the path to their fonts in simuconf.tab or another text file.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #15 on: May 09, 2020, 12:28:48 PM »
In principle, all fonts can be compied into the simutrans directory anyway ... This seems rather the system directory for fonts (which I find stange, since a multi user system storing its fonts locally seems counterintuitive ... Anyway, now using getenv, sinc the ~ problem only occurs on Unix (and maybe Amiga OS ,,, ?) in r9078

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #16 on: May 10, 2020, 12:08:45 AM »
Anyway, now using getenv, sinc the ~ problem only occurs on Unix (and maybe Amiga OS ,,, ?) in r9078

This finally solves it.

BUT I've noticed that it is not finding fonts in parent directories inside the home user directory (~/.local/share/fonts and ~/.fonts), only in subdirectories. It's weird because it is finding fonts in parent directories inside system directories (like /usr/share/fonts). Weird.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10622
  • Languages: De,EN,JP
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #17 on: May 10, 2020, 01:02:09 PM »
Is should not have worked at all, since it returned again the path with ~ to the folder search. Fixed (finally) hopefully in r9082.

Offline Roboron

  • Devotee
  • *
  • Posts: 277
    • Las Galácticas Aventuras de Komoyo Diga
  • Languages: ES, EN
Re: Patch: Looking for fonts in subfolders (unix)
« Reply #18 on: May 11, 2020, 01:13:59 PM »
Is should not have worked at all, since it returned again the path with ~ to the folder search. Fixed (finally) hopefully in r9082.

Finally it is working as intended. Good job!