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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
teracow
Posts: 19
Joined: 2013-04-30T02:55:12-07:00
Authentication code: 6789
Location: Brisbane, Australia

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

Post 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.
Last edited by teracow on 2016-06-17T13:44:58-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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".
snibgo's IM pages: im.snibgo.com
User avatar
teracow
Posts: 19
Joined: 2013-04-30T02:55:12-07:00
Authentication code: 6789
Location: Brisbane, Australia

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

Post by teracow »

I was worried that might be the case... lol...

Thanks snibgo.
User avatar
teracow
Posts: 19
Joined: 2013-04-30T02:55:12-07:00
Authentication code: 6789
Location: Brisbane, Australia

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

Post 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
Last edited by teracow on 2016-06-21T10:50:29-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

Good stuff. Thanks for sharing the solution.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

The fall back font is the default system font. You would have to change that in your OS.
User avatar
teracow
Posts: 19
Joined: 2013-04-30T02:55:12-07:00
Authentication code: 6789
Location: Brisbane, Australia

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

Post 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)
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post 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.
User avatar
teracow
Posts: 19
Joined: 2013-04-30T02:55:12-07:00
Authentication code: 6789
Location: Brisbane, Australia

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

Post 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.
Post Reply