普通网友 2012-05-14 03:26
浏览 502

使用ImageMagick指定具有字体系列的字体

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

Working


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

Not Working


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?

  • 写回答

2条回答 默认 最新

  • doujing6053 2012-05-14 03:39
    关注

    This is due to how font families work; for instance, the "Arial" font family comprises specific font definitions for bold, italic, underlines, etc. You must be specific when choosing a font to use in your images.

    I'm guessing that if you really wanted to, you could do the following:

    1. Take the font family name, replace spaces with dashes
    2. If that works, great!
    3. If not, append -Regular to the end and try again

    But you shouldn't do anything programmatic with font family unless you're forced to ;-)

    My source of imagick knowledge mostly comes from this guy: http://valokuva.org/?cat=1

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题