Magick++ (GraphicsMagick) problems when using drawing text with DrawableFont() on macOS

Environment

  • MacOSX 10.11.6
  • GraphicsMagick 1.3.25 (installing with Homebrew)

If you get error when you specify "Arial" or "Microsoft Sans Serif" to font family. For example as following code:

 // drawfont.cpp
 #include <iostream>
 #include <Magick++.h>

 using namespace Magick;
 using namespace std;

 int main(int argc, char **argv)
 {
     try {
         InitializeMagick(*argv);
         Color bg_color = Color("red");

         Image image("1000x1000", bg_color);

         std::list<Magick::Drawable> drawList;

         drawList.push_back(DrawablePointSize(200));
         drawList.push_back(DrawableFont("Microsoft Sans Serif", AnyStyle, 400, AnyStretch));
         //drawList.push_back(DrawableFont("/Library/Fonts/Arial.ttf"));     // no problem
         drawList.push_back(DrawableText(10,150,"hello"));
         drawList.push_back(DrawableFont("Arial", AnyStyle, 400, AnyStretch));
         drawList.push_back(DrawableText(10,500,"hello"));

         image.draw(drawList);
         image.write("out.jpg");
     }
     catch(exception & error_)
     {
         cout<<"Caught exception : " << error_.what() << endl;
     }
 }

Error Message

I get the following error

 $ clang++ -std=c++11 -L/usr/local/lib -lGraphicsMagick++ -I/usr/local/include/GraphicsMagick -o drawfont drawfont.cpp
 $ ./drawfont
 Caught exception : Magick: Unable to read font (/usr/local/share/ghostscript/fonts/n019003l.pfb) reported by magick/annotate.c:1063 (RenderFreetype)

Solution

setup to type.mgk file.

script of generate type.mgk from system_profiler's font list (mgkgen.py)

setup sequence

$ system_profiler -xml SPFontsDataType > fontset.xml
$ python3 mgkgen.py fontset.xml > type.mgk
# backup if you need
$ cp /usr/local/Cellar/graphicsmagick/1.3.25/lib/GraphicsMagick/config/type.mgk /usr/local/Cellar/graphicsmagick/1.3.25/lib/GraphicsMagick/config/type.mgk.bk
$ cp type.mgk /usr/local/Cellar/graphicsmagick/1.3.25/lib/GraphicsMagick/config/type.mgk

check font list

 $ gm convert -list font
 Path: /usr/local/Cellar/graphicsmagick/1.3.25/lib/GraphicsMagick/config/type.mgk

 Name                             Family                  Style   Stretch  Weight
 --------------------------------------------------------------------------------
 .SFNSDisplay-Ultralight          System Font             normal  normal    400
 BigCaslon-Medium                 Big Caslon              normal  normal    400
 Silom                            Silom                   normal  normal    400
 .SFNSText-Light                  System Font             normal  normal    400
 Chalkduster                      Chalkduster             normal  normal    400
 STIXSizeThreeSym-Regular         STIXSizeThreeSym        normal  normal    400
 TimesNewRomanPSMT                Times New Roman         normal  normal    400
 AppleBraille-Pinpoint8Dot        Apple Braille           normal  normal    400
         :
 MalayalamMN                      Malayalam MN            normal  normal    400
 MalayalamMN-Bold                 Malayalam MN            normal  normal    400
 STIXSizeTwoSym-Regular           STIXSizeTwoSym          normal  normal    400
 AlTarikh                         Al Tarikh               normal  normal    400
 .AlTarikhPUA                     .Al Tarikh PUA          normal  normal    400
 DecoTypeNaskh                    DecoType Naskh          normal  normal    400
 .DecoTypeNaskhPUA                .DecoType Naskh PUA     normal  normal    400
 Farah                            Farah                   normal  normal    400
 .FarahPUA                        .Farah PUA              normal  normal    400

try again

 $ ./drawfont
 $ ls
 out.jpg
comments powered by Disqus