Is it possible to render a font using the font family in ImageMagick?
I have tried everything I can think of and nothing seems to work. I have done a lot of searching and I can't seem to find anything that addresses this specific issue.
I am ultimately using PHP, but I have also tried using ImageMagick on the command line with no success. I am using ImageMagick 6.7.6 on linux. Here is what I have done so far.
- I have tested the fonts that ImageMagick reckognizes using the
identify -list font
command - I have manually created a type.xml file based on the xml files that came installed on my system
- I have created a type.xml file using this script: imagick_type_gen
- I have tested with multiple font types: ttf, otf, pfb, ttc
Here is the example output from identify -list font
for some fonts I have tested with:
Font: DejaVu-Sans-Book
family: DejaVu Sans
style: Normal
stretch: Normal
weight: 400
glyphs: /usr/share/fonts/dejavu/DejaVuSans.ttf
Font: Baskerville-Regular
family: Baskerville
style: Normal
stretch: Normal
weight: 400
glyphs: /usr/share/fonts/custom/Baskerville.ttc
Font: Palatino-Roman
family: Palatino
style: Normal
stretch: Normal
weight: 400
glyphs: /usr/share/fonts/urw-fonts/p052003l.pfb
Font: Honey-Script-Light
family: Honey Script
style: Normal
stretch: Normal
weight: 300
glyphs: /usr/share/fonts/custom/HoneyScript-Light.ttf
If I specify the full font name everything works as expected:
convert -pointsize 32 -font "Honey-Script-Light" label:'Testing' testing.png
However, when I try to use the font family (both listed by identify -list font
and listed in type.xml) I get the default system font:
convert -pointsize 32 -family "Honey Script" label:'Testing' testing.png
I have also tried specifying all of the font's parameters and it again does not work:
convert -pointsize 32 -stretch Normal -style Normal -weight 300 -family "Honey Script" label:'Testing' testing.png
When I do a similar process through PHP, it actually returns an error as soon as I try to set the font family of the draw object.
$draw->setFontFamily('Honey Script');
This is the error that gets thrown:
PHP Fatal error: Uncaught exception 'ImagickDrawException' with message 'Unable to set font family; parameter not found in the list of configured fonts
Just like the command line option, if I specify the full font name, the image is rendered properly.
$draw->setFont('Honey-Script-Light');
From everything I have found about how to configure a font to be used with ImageMagick, the fonts are indeed configured. Why can't I select the fonts based on the font family?
Is this possibly an undocumented bug? Is there something I am missing?
I realize that I could simply specify the full font name and be done with it, but there is a reason specific to my project that make it much better to specify the font family.
Why would specifying the font family be an option if it does not work? This is what ImageMagick says regarding the font family option:
This setting suggests a font family that ImageMagick should try to use for rendering text. If the family can be found it is used; if not, a default font (e.g., "Arial") or a family known to be similar is substituted (e.g., "Courier" might be used if "System" is requested but not found).
Any ideas are very welcome. I am pretty much stuck at this point.
EDIT:
The main reason I would prefer to use font families rather than fully named fonts is because of the input I am processing. I receive files from a third party that need to be processed. These files specify fonts as:
font-family="Honey Script" font-style="Normal" font-stretch="Normal" font-weight="300"
If I had to specify the exact font name based on that information I would need to create a mapping for every possible combination of font family and attributes to the exact font name. That would be very difficult to maintain as hundreds of fonts are added to the system.
It seems to me that there must be a way to get ImageMagick to retrieve the exact font based on all the available details about the font as listed above.
If that is not possible then why would ImageMagick have the option to specify font family at all?
convert list
. That said, I notice some SVG files I'm working with from Inkscape do indeed only have a family specified in text elements. Inkscape does seem to select a sensible font from that family, so it might be worth doing some research on how its renderer (Pango) does it? – Trencher