Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom font in ImageMagick on Heroku

I'm building some functionality into a Rails app that requires ImageMagick. I'm currently using the RMagick gem to overlay custom text atop an image. I'd like to be able to offer the user a selection of fonts to choose from, however I'm finding the stock set of fonts that came with GhostScript (what I have on my system) to be rather limiting.

What I'd like to know is how I can import/load custom fonts (e.g. Lato) for use with ImageMagick in a way that will work locally but also on Heroku. Specifically I'm building an image caption (see code below) and want to be able to specify a font that's not in the list.

Here's my sample code:

font = 'Lato'
text = Magick::Image.read("caption:#{caption_text.to_s}") {
    self.size = "#800x600"
    self.colorspace = RGBColorspace
    self.background_color = "Transparent"
    self.depth = 8
    self.format = 'PNG'
    self.fill = '#333333'
    self.font = font
}.first

If I run identify -list font I get the following list of fonts that ImageMagick has access to on my system.

➜  Desktop identify -list font

Path: /usr/local/Cellar/imagemagick/6.9.3-0_2/etc/ImageMagick-6/type-ghostscript.xml
  Font: AvantGarde-Book
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/a010013l.pfb
  Font: AvantGarde-BookOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/a010033l.pfb
  Font: AvantGarde-Demi
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/a010015l.pfb
  Font: AvantGarde-DemiOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/a010035l.pfb
  Font: Bookman-Demi
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/b018015l.pfb
  Font: Bookman-DemiItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/b018035l.pfb
  Font: Bookman-Light
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 300
    glyphs: /usr/local/share/ghostscript/fonts/b018012l.pfb
  Font: Bookman-LightItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 300
    glyphs: /usr/local/share/ghostscript/fonts/b018032l.pfb
  Font: Courier
    family: Courier
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n022003l.pfb
  Font: Courier-Bold
    family: Courier
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n022004l.pfb
  Font: Courier-BoldOblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n022024l.pfb
  Font: Courier-Oblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n022023l.pfb
  Font: fixed
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019003l.pfb
  Font: Helvetica
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019003l.pfb
  Font: Helvetica-Bold
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019004l.pfb
  Font: Helvetica-BoldOblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019024l.pfb
  Font: Helvetica-Narrow
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019043l.pfb
  Font: Helvetica-Narrow-Bold
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019044l.pfb
  Font: Helvetica-Narrow-BoldOblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019064l.pfb
  Font: Helvetica-Narrow-Oblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019063l.pfb
  Font: Helvetica-Oblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019023l.pfb
  Font: NewCenturySchlbk-Bold
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/c059016l.pfb
  Font: NewCenturySchlbk-BoldItalic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/c059036l.pfb
  Font: NewCenturySchlbk-Italic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/c059033l.pfb
  Font: NewCenturySchlbk-Roman
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/c059013l.pfb
  Font: Palatino-Bold
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/p052004l.pfb
  Font: Palatino-BoldItalic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/p052024l.pfb
  Font: Palatino-Italic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/p052023l.pfb
  Font: Palatino-Roman
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/p052003l.pfb
  Font: Symbol
    family: Symbol
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/s050000l.pfb
  Font: Times-Bold
    family: Times
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n021004l.pfb
  Font: Times-BoldItalic
    family: Times
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n021024l.pfb
  Font: Times-Italic
    family: Times
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021023l.pfb
  Font: Times-Roman
    family: Times
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021003l.pfb

If I run bash on Heroku I get the following list:

➜  Desktop heroku run bash
Running bash on staging-myapp... up, run.5220
~ $ identify -list font

Path: /etc/ImageMagick/type-ghostscript.xml
  Font: AvantGarde-Book
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/a010013l.pfb
  Font: AvantGarde-BookOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/a010033l.pfb
  Font: AvantGarde-Demi
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010015l.pfb
  Font: AvantGarde-DemiOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010035l.pfb
  Font: Bookman-Demi
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/b018015l.pfb
  Font: Bookman-DemiItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/b018035l.pfb
  Font: Bookman-Light
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/b018012l.pfb
  Font: Bookman-LightItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/b018032l.pfb
  Font: Courier
    family: Courier
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022003l.pfb
  Font: Courier-Bold
    family: Courier
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022004l.pfb
  Font: Courier-BoldOblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022024l.pfb
  Font: Courier-Oblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022023l.pfb
  Font: fixed
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Helvetica
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Helvetica-Bold
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb
  Font: Helvetica-BoldOblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019024l.pfb
  Font: Helvetica-Narrow
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb
  Font: Helvetica-Narrow-Bold
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019044l.pfb
  Font: Helvetica-Narrow-BoldOblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019064l.pfb
  Font: Helvetica-Narrow-Oblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019063l.pfb
  Font: Helvetica-Oblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019023l.pfb
  Font: NewCenturySchlbk-Bold
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059016l.pfb
  Font: NewCenturySchlbk-BoldItalic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059036l.pfb
  Font: NewCenturySchlbk-Italic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059033l.pfb
  Font: NewCenturySchlbk-Roman
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059013l.pfb
  Font: Palatino-Bold
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052004l.pfb
  Font: Palatino-BoldItalic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052024l.pfb
  Font: Palatino-Italic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/p052023l.pfb
  Font: Palatino-Roman
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/p052003l.pfb
  Font: Symbol
    family: Symbol
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/s050000l.pfb
  Font: Times-Bold
    family: Times
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021004l.pfb
  Font: Times-BoldItalic
    family: Times
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021024l.pfb
  Font: Times-Italic
    family: Times
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021023l.pfb
  Font: Times-Roman
    family: Times
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021003l.pfb

Path: System Fonts
  Font: Century-Schoolbook-Bold
    family: Century Schoolbook
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059016l.pfb
  Font: Century-Schoolbook-Bold-Italic
    family: Century Schoolbook
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059036l.pfb
  Font: Century-Schoolbook-Italic
    family: Century Schoolbook
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059033l.pfb
  Font: Century-Schoolbook-Roman
    family: Century Schoolbook
    style: Normal
    stretch: Normal
    weight: 500
    glyphs: /usr/share/fonts/type1/gsfonts/c059013l.pfb
  Font: DejaVu-Sans-Bold
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
  Font: DejaVu-Sans-Bold-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf
  Font: DejaVu-Sans-Book
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
  Font: DejaVu-Sans-Condensed
    family: DejaVu Sans
    style: Normal
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf
  Font: DejaVu-Sans-Condensed-Bold
    family: DejaVu Sans
    style: Normal
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Bold.ttf
  Font: DejaVu-Sans-Condensed-Bold-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-BoldOblique.ttf
  Font: DejaVu-Sans-Condensed-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Oblique.ttf
  Font: DejaVu-Sans-ExtraLight
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 200
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf
  Font: DejaVu-Sans-Mono-Bold
    family: DejaVu Sans Mono
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf
  Font: DejaVu-Sans-Mono-Bold-Oblique
    family: DejaVu Sans Mono
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-BoldOblique.ttf
  Font: DejaVu-Sans-Mono-Book
    family: DejaVu Sans Mono
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
  Font: DejaVu-Sans-Mono-Oblique
    family: DejaVu Sans Mono
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf
  Font: DejaVu-Sans-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf
  Font: DejaVu-Serif-Bold
    family: DejaVu Serif
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf
  Font: DejaVu-Serif-Bold-Italic
    family: DejaVu Serif
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-BoldItalic.ttf
  Font: DejaVu-Serif-Book
    family: DejaVu Serif
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf
  Font: DejaVu-Serif-Condensed
    family: DejaVu Serif
    style: Normal
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed.ttf
  Font: DejaVu-Serif-Condensed-Bold
    family: DejaVu Serif
    style: Normal
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Bold.ttf
  Font: DejaVu-Serif-Condensed-Bold-Italic
    family: DejaVu Serif
    style: Italic
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-BoldItalic.ttf
  Font: DejaVu-Serif-Condensed-Italic
    family: DejaVu Serif
    style: Italic
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Italic.ttf
  Font: DejaVu-Serif-Italic
    family: DejaVu Serif
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-Italic.ttf
  Font: Dingbats-Regular
    family: Dingbats
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/d050000l.pfb
  Font: Nimbus-Mono-Bold
    family: Nimbus Mono
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022004l.pfb
  Font: Nimbus-Mono-Bold-Oblique
    family: Nimbus Mono
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022024l.pfb
  Font: Nimbus-Mono-Regular
    family: Nimbus Mono
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022003l.pfb
  Font: Nimbus-Mono-Regular-Oblique
    family: Nimbus Mono
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022023l.pfb
  Font: Nimbus-Roman-No9-Medium
    family: Nimbus Roman No9
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021004l.pfb
  Font: Nimbus-Roman-No9-Medium-Italic
    family: Nimbus Roman No9
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021024l.pfb
  Font: Nimbus-Roman-No9-Regular
    family: Nimbus Roman No9
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021003l.pfb
  Font: Nimbus-Roman-No9-Regular-Italic
    family: Nimbus Roman No9
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021023l.pfb
  Font: Nimbus-Sans-Bold
    family: Nimbus Sans
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb
  Font: Nimbus-Sans-Bold-Condensed
    family: Nimbus Sans
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019044l.pfb
  Font: Nimbus-Sans-Bold-Condensed-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019064l.pfb
  Font: Nimbus-Sans-Bold-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019024l.pfb
  Font: Nimbus-Sans-Regular
    family: Nimbus Sans
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Nimbus-Sans-Regular-Condensed
    family: Nimbus Sans
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb
  Font: Nimbus-Sans-Regular-Condensed-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019063l.pfb
  Font: Nimbus-Sans-Regular-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019023l.pfb
  Font: Standard-Symbols-Regular
    family: Standard Symbols
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/s050000l.pfb
  Font: URW-Bookman-Demi-Bold
    family: URW Bookman
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/b018015l.pfb
  Font: URW-Bookman-Demi-Bold-Italic
    family: URW Bookman
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/b018035l.pfb
  Font: URW-Bookman-Light
    family: URW Bookman
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/b018012l.pfb
  Font: URW-Bookman-Light-Italic
    family: URW Bookman
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/b018032l.pfb
  Font: URW-Chancery-Medium-Italic
    family: URW Chancery
    style: Italic
    stretch: Normal
    weight: 500
    glyphs: /usr/share/fonts/type1/gsfonts/z003034l.pfb
  Font: URW-Gothic-Book
    family: URW Gothic
    style: Normal
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/a010013l.pfb
  Font: URW-Gothic-Book-Oblique
    family: URW Gothic
    style: Oblique
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/a010033l.pfb
  Font: URW-Gothic-Demi
    family: URW Gothic
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010015l.pfb
  Font: URW-Gothic-Demi-Oblique
    family: URW Gothic
    style: Oblique
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010035l.pfb
  Font: URW-Palladio-Bold
    family: URW Palladio
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052004l.pfb
  Font: URW-Palladio-Bold-Italic
    family: URW Palladio
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052024l.pfb
  Font: URW-Palladio-Italic
    family: URW Palladio
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/p052023l.pfb
  Font: URW-Palladio-Roman
    family: URW Palladio
    style: Normal
    stretch: Normal
    weight: 500
    glyphs: /usr/share/fonts/type1/gsfonts/p052003l.pfb

Obviously the available options on Heroku are more expansive than what's on my machine, but still quite limited.

like image 921
Daniel Bonnell Avatar asked May 02 '16 19:05

Daniel Bonnell


1 Answers

I figured it out! It was surprisingly easy to do, if not a little too obvious to be mentioned in the docs. If there is a better way to do this, please share and I'll accept your answer.

When creating a new caption (this probably applies to other types of text as well), you can specify a font as a string (e.g. ArialBold) if it's in your /etc/ImageMagick/type-ghostscript.xml. If it's not listed there though, you can also pass the absolute file path as a string. Make sure to use double quotes. ImageMagick will accept TTF and OTF fonts (possibly others but I haven't tested).

So if I run the following code, it will pick Arial Bold Italic for the font, even though it's not in my fontmap.

require 'RMagick'
include Magick

caption_text = "This is a test"
font = "/Library/Fonts/Arial\ Bold\ Italic.ttf"
text = Magick::Image.read("caption:#{caption_text}") {
    # this wraps the text to fixed width
    self.size = "800x600"
    self.colorspace = RGBColorspace
    self.background_color = "Transparent"
    self.depth = 8
    self.format = 'PNG'
    self.fill = '#333333'
    self.font = font
}.first
text.write('text.png')

This should be Heroku safe too since I can easily include custom fonts in my assets/fonts folder.

like image 194
Daniel Bonnell Avatar answered Sep 29 '22 01:09

Daniel Bonnell