0

Does anyone have experience or insights into how to convert SVG to PNG using ImageMagick.NET where the SVG has custom embedded fonts?

I've seen similar posts about this, but I'm specifically interested in ImageMagick.net and I would like to avoid calling anything from command line. I currently use Batik, but I want to move this to Azure and I don't want to go through starting the JVM so that the JAR file can be executed.

Note additionally, my problem is that I am getting the SVG from another party and most fonts are not freely available, so I can't just pre-install all potential fonts.

Many thanks for any insight.

Mike Baron
  • 818
  • 2
  • 9
  • 17

1 Answers1

3

I don't know anything about platform, but I can share some insight on the SVG process within ImageMagick. This is more of a long-winded comment than an answer

ImageMagick (of which Magick.net sets on top of) performs tasks on raster images, so any vector input must be "decoded" into a raster image before any additional work be be done. For SVG images, I'm aware of three ways ImageMagick can render vector graphics into authenticated pixels.

  1. Use a primitive internal MSVG coder to draw each shape.
  2. Pass the rendering work to librsvg if compiled with delegate library support. (Usually on *NIX systems)
  3. Call an external command-line application. Identical to the suggestions referenced in the links you provided.

If I would map it out, I would imagine it would look something like this.

SVG delegate map

SVG Fonts with MSVG (option #1)

I believe this is the option your asking about as it would be the default configuration for must ImageMagick installs. For a font to be rendered, the typeface must be found on the system, and supported by freetype (.ttf or .otf files). Embedded fonts are usually base64 ttf files attached inline under the @font-face CSS at-rule. If this is true, you should be able to preprocess the document, extract the font-file to a local/temporary filesystem, and assign it with MagickReadSettings.FontFamily before reading the SVG document. Although I'm not sure if this would work with multiple fonts.

SVG Fonts with RSVG (option #2)

The librsvg offers a lot more support for SVG specs then the internal renderer, but also enforces more restricted approach for external resources. There's also an open issue with adding support to @font-face, so you might be forced to do option #1 anyway.

External Command-line (option #3)

This would be your best option. ImageMagick's delegate.xml file can be altered to call other utilities. Inkscape, for example, can be called by ImageMagick with the following rule..

<delegate decode="svg" command="inkscape.exe -e %o %i"/>

Although I'm not sure if inkscape is a good example as support for CSS fonts are still listed on a wishlist.

TL;DR

... convert SVG to PNG using ImageMagick.NET where the SVG has custom embedded fonts?

All-n-all, it boils down to the right tool for the right job. If your only use-case is converting SVG+CSS to PNG, and you have no additional raster manipulation tasks, a direct utility like batik is more appropriate than ImageMagick. Like it or not, installing a JRE to execute a JAR file might be the best option. Magick.net by itself doesn't meet your requirements.

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • I ended up having to go command line. The problem is there are thousands of old SVG's with embedded fonts so I can't just install every potential font on the server. Great answer though, very thorough and illuminating. – Mike Baron Oct 09 '18 at 01:13