Render text to SVG file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
deepflame
Posts: 3
Joined: 2015-02-02T05:14:03-07:00
Authentication code: 6789

Render text to SVG file

Post by deepflame »

Hi!

I am trying to render text to an SVG file. Is this generally supported? Rendering to raster image formats like PNG works great.

The SVG I get looks like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="62" height="14">
  <image id="image0" width="62" height="14" x="0" y="0"
    xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAOCAAAAABS0PA1AAAABGdBTUEAALGPC/xhBQAAACBjSFJN
AAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElN
RQffAgIOBBps5Y7FAAABT0lEQVQoz2P4TxFgOK6NzD2tDEL4AVTBfWmytP9B1a6VzSV/+P//o0Zc
Ho9h2jdr8fk/A6mxWfB/Msev/64boUKn9TK1QAqmiAhVQrQzNL+rsP//SmjTuzx3qPZbAvve5juB
tNdl/I/iOvlH4ANU6DRT3Uuggsv8B994QrQL/vt/Wv7/5ID//7+wfoRor0v5//8b63ug9AGD/6pZ
E86awoROC/4FKajM+v//JNTx//+fl/5fwiUuLs5zB6I9oRMoI3ATSPzku2ewOWxiBUwI5HAgjun4
//8JPOiA2nti////e+QfRHtZ7v//r5m/glzvWpj1Ti50F0wIqr04+///I8ja7wof/9ZoAfX7aeGT
nzJDwOHbzrfkv47IN5gQVPtJoeOv3ZG1/9+oxul0Dxby8xU5PJ+DtZ9iuPc/0+4/TAiq/f80CfEp
YO0UprqhrB0AqOnAeZyU9I4AAAAldEVYdGRhdGU6Y3JlYXRlADIwMTUtMDItMDJUMTQ6MDQ6MjYr
MDE6MDAP8PUlAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE1LTAyLTAyVDE0OjA0OjI2KzAxOjAwfq1N
mQAAABF0RVh0bGFiZWwAaGVsbG8gd29ybGSR/7emAAAAEnRFWHRsYWJlbDpwb2ludHNpemUAMTLR
TL0XAAAAAElFTkSuQmCC" />
</svg>
This is not really what I want as I am looking for a plain vector file.

I use Imagemagick on OSX:

Code: Select all

Version: ImageMagick 6.9.0-3 Q16 x86_64 2015-01-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
Features: DPC Modules
Delegates (built-in): bzlib cairo fontconfig freetype jng jpeg lcms ltdl lzma png rsvg tiff webp xml zlib
Any help is highly appreciated.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Render text to SVG file

Post by snibgo »

SVG files are plain text. If you want to insert text in an SVG file, use a text tool.
snibgo's IM pages: im.snibgo.com
deepflame
Posts: 3
Joined: 2015-02-02T05:14:03-07:00
Authentication code: 6789

Re: Render text to SVG file

Post by deepflame »

snibgo wrote:SVG files are plain text. If you want to insert text in an SVG file, use a text tool.
You are right, but the idea was to "render" text to an SVG file having the text as outlines (SVG path elements).

I would not like to use the standard SVG text element and reference the font inside as this is a requirement in the project we work on.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Render text to SVG file

Post by snibgo »

ImageMagic is a processor for raster images, ie pixels. It is very powerful for this. It can also process vectors, rasterising them. It can do this by itself, or using delegate libraries or delegate programs. IM can't convert raster to vector.

For converting text to a path, Inkscape is the obvious tool.

For general-purpose conversion of raster to path, potrace works fine. So you could use IM to convert text to raster, then potrace to convert from raster to path. But InkScape will give a much better result.
snibgo's IM pages: im.snibgo.com
deepflame
Posts: 3
Joined: 2015-02-02T05:14:03-07:00
Authentication code: 6789

Re: Render text to SVG file

Post by deepflame »

Thanks for the suggestion. Yes, I tried Inkscape already but there are unfortunately some bugs when running it headless and exporting to plain SVG.

Doing some more research we found the Harfbuzz hb-view tool to do exactly what we want but with some compromise. For example it does not have alignment and other things that Imagemagick has.

Guess we will settle using Harfbuzz then if Imagemagick cannot do it.
Post Reply