Page 1 of 1

SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T12:07:42-07:00
by teracow
Hello,

using: ImageMagick 6.8.9-8 Q16 x86_64 2016-05-31
on: OpenSUSE 13.2 64b

Anyone know how I can specify a particular font to use, but if that is not available, the fallback font(s) to use?

Code: Select all

convert -size x100 -font "Century-Schoolbook-L-Bold-Italic" -background white -stroke black -strokewidth 1 label:"my label message" "output.png"
So, if "Century-Schoolbook-L-Bold-Italic" is unavailable, I'd like to specify the next font: "Droid-Serif-Bold-Italic", and if that isn't available, then: "URW-Bookman-L-Demi-Bold-Italic" and so on.

Thanks.

Re: How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T12:30:10-07:00
by snibgo
I don't think you can specify automatic fallback fonts for "label:", You would have to find if the one(s) you want are available, with "-list font".

Re: How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T12:32:33-07:00
by teracow
I was worried that might be the case... lol...

Thanks snibgo.

Re: How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T13:38:57-07:00
by teracow
So, I wrote this:

Code: Select all

#!/bin/bash

# Return the first preferred font name available to ImageMagic. If none found then return "".

function WantedFonts
	{

	local font_list=""

	font_list+="Century-Schoolbook-L-Bold-Italic\n"
	font_list+="Droid-Serif-Bold-Italic\n"
	font_list+="FreeSerif-Bold-Italic\n"
	font_list+="Nimbus-Roman-No9-L-Medium-Italic\n"
	font_list+="Times-BoldItalic\n"
	font_list+="URW-Palladio-L-Bold-Italic\n"
	font_list+="Utopia-Bold-Italic\n"
	font_list+="Bitstream-Charter-Bold-Italic\n"

	echo -e "$font_list"

	}

function FirstPreferredFont
	{

	local preferred_fonts=$(WantedFonts)
	local available_fonts=$(convert -list font | grep "Font:" | sed 's| Font: ||')
	local first_available_font=""

	while read preferred_font ; do
		while read available_font ; do
			[ "$preferred_font" == "$available_font" ] && break 2
		done <<< "$available_fonts"
	done <<< "$preferred_fonts"

	if [ ! -z "$preferred_font" ] ; then
		echo "$preferred_font"
	else
		# uncomment 2nd line down to return first installed font if no preferred fonts could be found.
		# for 'convert -font' this isn't needed as it will use a default font if specified font is "".

		#read first_available_font others <<< $available_fonts

		echo "$first_available_font"
	fi

	}

echo "$(FirstPreferredFont)"
and it seems to do what I need. Have to put it in the main script now. :D

update: modified font name separation char

Re: SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T14:01:47-07:00
by snibgo
Good stuff. Thanks for sharing the solution.

Re: SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T14:08:56-07:00
by fmw42
The fall back font is the default system font. You would have to change that in your OS.

Re: SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T15:20:14-07:00
by teracow
fmw42 wrote:The fall back font is the default system font. You would have to change that in your OS.
I'm trying to keep the code portable so as to reduce the config-work required for the end-user.

Thanks Fred. 8)

Re: SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T16:54:15-07:00
by GeeMack
teracow wrote:I'm trying to keep the code portable so as to reduce the config-work required for the end-user.
I have a couple IM scripts that use fonts which don't reside on all my computers. I keep the scripts in their own folders, and keep a copy of the OTF or TTF font file right in the same folder. To use the fonts in the script, I just name the font file itself when setting the font in the IM command like...

Code: Select all

convert ^
   input.jpg ^
   -gravity southeast ^
   -pointsize 24 ^
   -font LiberationMono-Regular.ttf ^
   -annotate +20+20 "Image by GeeMack" ^
      output.jpg
I use these IM commands and scripts on Windows machines, and it works great. It takes a tiny bit of storage, maybe 50K to 200K for a font file, but it doesn't require installing the font in the computer where I'm running the IM command. I don't know if it works like this in a *nix system, but I can't see why it wouldn't. Maybe you can get some use out of this technique.

Re: SOLVED - How do I specify font usage order? (fallback fonts)

Posted: 2016-06-17T18:26:55-07:00
by teracow
GeeMack wrote:
teracow wrote:I'm trying to keep the code portable so as to reduce the config-work required for the end-user.
I have a couple IM scripts that use fonts which don't reside on all my computers. I keep the scripts in their own folders, and keep a copy of the OTF or TTF font file right in the same folder. To use the fonts in the script, I just name the font file itself when setting the font in the IM command like...

Code: Select all

convert ^
   input.jpg ^
   -gravity southeast ^
   -pointsize 24 ^
   -font LiberationMono-Regular.ttf ^
   -annotate +20+20 "Image by GeeMack" ^
      output.jpg
That does work great! :D

Code: Select all

convert -size x100 -font "./SomeNewRareFontFile.ttf" -background white -stroke black -strokewidth 1 label:"my label message" "output.png"
Thanks GeeMack. I probably didn't need to write that font fall-back code then.