Custom fonts on MacOS problem

I’m trying to use some non-standard fonts with GMT on MacOS. As my own experimentation lead nowhere I made heavy use of Gallery example 31 but had no luck as well.

The font is installed and usable on the system. gmt text -L sees it. But for some reason coast doesn’t like it.

I’m stumped where I’m making a mistake – maybe one of the more knowledgeable folks can point me in the right direction.

MacOS 10.15.5
GMT 6.2.0_479acc6_2020.07.16 [64-bit]

Stripped script to reproduce the error:

#!/usr/bin/env bash

function gmt() { /usr/local/opt/gmt_dev/bin/gmt "$@"; }
export -f gmt

gmt text -L

AWK=${AWK:-awk}

$AWK '{print $1, 0.700, 0}' <<- EOF > PSL_custom_fonts.txt
GothamRounded-Book
EOF

gmt set FONT GothamRounded-Book


gmt begin fonts
gmt basemap -JL-24/16/15.5/16.5/15c -R-25.9/14.1/-22.1/17.9+r -Ba1dg1d
gmt end show

While the script generates a plot it doesn’t use the font and defaults to Helvetica instead:

kristof$ ./fonts.sh
gmt text [core] 6.2.0_479acc6_2020.07.16 [64-bit] - Plot or typeset text

[…]

	Font #	Font Name
	------------------------------------
	  0	Helvetica
[…]
	 38	GothicBBB-Medium-EUC-V
	 39	GothamRounded-Book
For additional fonts, see "Using non-default fonts with GMT" in the documentation.
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
kristof$

Thank you for your help!

Looks like gmt reads your list of files but postscript light does not. I see it is looking in (1) current directory, (2) your ~/.gmt/cache directory, the official GMT share/postscriptlight directory (which I dont think we install anymore), and a directory pointed to by environmental parameter PSL_SHAREDIR. I would place a copy of your PSL_custom_fonts.txt in the cache dir and see if that helps. Once it works and the PS file uses the font, the next battle is to make sure ghostscript finds the actual fonts.

Hi Paul,

thank you for looking into this. As you recommended I put PSL_custom_fonts.txt into ~/.gmt/cache. Additionally I deleted the one in the current directory and commented the awk part out. Now GMT can’t find it and I get another error …

kristof$ ./fonts.sh
6.2.0_479acc6_2020.07.16
gmt text [core] 6.2.0_479acc6_2020.07.16 [64-bit] - Plot or typeset text

[…]

	Font #	Font Name
	------------------------------------
	  0	Helvetica
[…]	
	 38	GothicBBB-Medium-EUC-V
For additional fonts, see "Using non-default fonts with GMT" in the documentation.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
gmtset [WARNING]: Length <unit> k not supported - revert to default unit [point]
gmtset [WARNING]: GothamRounded-Boo not a valid number and may not be decoded properly.
gmtset [WARNING]: Representation of font size not recognised. Using default.
kristof$

Sorry, I did not mean to remove the one you had in the current directory, just duplicated into cache. I think GMT is reading that while postscript light is reading the other… Not ideal but if we can get it to work we can fix it. But odd if you followed the ex31 which is similar.

Oh, ok – I missed that. My apologies. Now there is one in the script’s directory as well as in ~/.gmt/cache and I get the same error as in my original post.

But ex31 doesn’t work here as well (I never ran it, I thoughtlessly assumed that it works):

ex31 kristof$ ./ex31.sh
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (42) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (40) out of range (0-38); reset to 0
     [22 identical lines removed]
PSL: Warning: Selected font (40) out of range (0-38); reset to 0
PSL: Warning: Selected font (41) out of range (0-38); reset to 0
PSL: Warning: Selected font (41) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
PSL: Warning: Selected font (41) out of range (0-38); reset to 0
PSL: Warning: Selected font (41) out of range (0-38); reset to 0
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
     [46 identical lines removed]
PSL: Warning: Selected font (39) out of range (0-38); reset to 0
ex31 kristof$

So ex31 runs for us in tests but we set this in testgmt.in

export GS_FONTPATH="@CMAKE_CURRENT_SOURCE_DIR@/ex31/fonts"

so this becomes the full path to your directory with fonts. Then psconvert can find them.

Do I understand you correctly that both for my test script and ex31 on my machine psconvert is having trouble finding the fonts?

That is my guess. Presumable ex31.sh runs for you and makes a PS file without any errors/warnings?. You can run psconvert and what it says and if it picks up. Run it with -S to see the gs command and try that manually and experiment with that path.

My apologies, the last dump was from running ex31. It generates a plot but reverts to Helvetica and generates a lot of those:

PSL: Warning: Selected font (39) out of range (0-38); reset to 0

Currently I experiment with line 15

gmt set PS_CONVERT="C-sFONTPATH=$(dirname $0)/fonts"

from ex31.sh. It seems that the directory name doesn’t get extracted:

ex31 kristof$ echo $(dirname "$0")
dirname: illegal option -- b
usage: dirname path

ex31 kristof$

Whereas `BASH_SOURCE[0] seems to work:

ex31 kristof$ echo $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

/usr/local/opt/gmt_dev/share/doc/examples/ex31
ex31 kristof$

I modified ex31.sh a bit from line 15:

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
echo $CURRENT_DIR
FONT_DIR="C-sFONTPATH=${CURRENT_DIR}/fonts"
echo $FONT_DIR
gmt set PS_CONVERT=${FONT_DIR}

Unfortunately the problem persists. It generates a plot but throws a lot of reset to 0 and doesn’t use the non-standard font.

OK. I did this with a copy of the ex31 directory:

  1. use gmt begin ex31 ps
  2. Remove the show at the end
  3. Ignore the reset to 0 messages for now
  4. mt psconvert ex31.ps -C-sFONTPATH=${GS_FONTPATH} -Tf -P

The PDF looks good.

After moving the deletion fo the font list outside, the script works directly fine with PDF and PS with gmt end show since both gs and gv consults the GS_FONTPATH and psconvert is passed that arg.

Mh, I get a plot but it still is in Helvetica and lots of reset to 0. Something is different.

try to reduce it to just a single pstext call of whtever you have to do to still get the error, then open an issue so we all can have a look.

Back from vacation … and I think I found the problem:

For some reason awk converts my decimal point to a decimal comma. This is correct for my locale but not for PostScript.

When running

AWK=${AWK:-awk}

$AWK '{print $1, 0.700, 0}' <<- EOF > PSL_custom_fonts.txt
GothamRounded-Book
EOF

I got

GothamRounded-Book 0,7 0

instead of the expected

GothamRounded-Book 0.700 0

in my PSL_custom_fonts.txt file. Why? I have no idea. I suspect my locale to be the culprit, but I was unable to find a source for this.

Same problem with ex31. As soon as I change the comma to a point in PSL_custom_fonts.txt itself and don’t let awk mess with it everything runs smooth.

This might explain why you can’t reproduce the error @pwessel as you locale most probably uses the decimal point.

Try to add this ti the top of your script.

export LC_ALL=C

Thanks Paul, that did the trick.

A hint for others with similar problems:

Locale

Your set locale (= language specific settings) might not be compatible with the formats expected by ghostscript. Here in my case the systems locale is de_DE.UTF-8 which uses a comma , instead of a point . as decimal point character. This confuses ghostscript - which does the PostScript conversion in the background - as it expects a point as decimal point character.
You can fix this by using

export LC_ALL=C

at the top of your script. See https://unix.stackexchange.com/a/87763 for more information on LC_ALL.

Path to Font Directory

If you want to use a custom font with GMT under macOS chances are that you have it already installed on your system. Now ghostscript needs to know where to look for it. This can easily be done by opening the macOS Font Book App (system app, comes preinstalled) and selecting the font you want to use. Then just hit Cmd + I. You get a lot of information about the font. Just scroll down and have a look under Location. There you’ll find something like

/Users/kristof/Library/Fonts/PoiretOne-Regular.ttf

We need only the path to the folder without the actual font file name so we just take

/Users/kristof/Library/Fonts

(note the missing trailing slash) and export it as well at the top of your script:

export GS_FONTPATH="/Users/kristof/Library/Fonts"

Be aware that macOS uses several different locations for system fonts and user installed fonts.