PNG / SVG Export

OK, here is a snippet you can paste in Window > Macro Panel. Select the glyphs and run the code from the macro window. It will prompt you for a folder and then store the SVGs with glyph names and an .svg suffix:

from AppKit import NSSize
GlyphAsImage = NSClassFromString("GlyphAsImage")
folder = GetFolder(allowsMultipleSelection=False)
if folder:
	folderURL = NSURL.fileURLWithPath_(folder)
	for layer in Font.selectedLayers:
		preset = {
			"active": True,
			"name": "SVG",
			"size": layer.ascender,
			"factor": 1,
			"format": "SVG"
		}
		layerSize = NSSize(layer.width, layer.ascender)
		svgData = GlyphAsImage.svgDataWithLayer_origSize_settings_(layer, layerSize, preset)
		svgFileName = layer.parent.name + ".svg"
		svgURL = folderURL.URLByAppendingPathComponent_(svgFileName)
		svgData.writeToURL_atomically_(svgURL, False)

(edit by @GeorgSeifert: I simplified the code a bit)

1 Like

Hey, thanks!!

I am trying to figure out how to get SVG from GSinstance instead of selectedLayers.
I guess trying to use the filter will not work. Right?

Which direction should I look for?

Interpolate the layers and pass them through this one by one. Best to use an export plug-in.

1 Like

To get an interpolated layer:

instance = Font.instances[1]
interpolatedFont = instance.interpolatedFontProxy
interpolatedGlyph = interpolatedFont.glyphs["A"]
interpolatedLayer = interpolatedGlyph.layers[0]
1 Like

Hi Mekka,
Can I get a script that export the selected glyphs as png images?

  1. I need to be able to select the image size (for example 512x512, or 716x716, or whatever)
  2. I need to be able to draw a rectangle to represent the bbox
  3. Ideally, a small line to represent also the baseline and the xheight (just a tiny little short line, for example from 0,0 to 0,10 on the baseline)

It all need to be centered inside the png image, having a little 10px margin arround it.
If you need more detail, get in touch with me via Skype.

Thanks!

Why don’t you use DrawBot for this? You can use DrawBot straight from within Glyphs.

You can do that natively from Glyphs. Take a look at the PS and EPS export plugins.

What i use for the drawbot plugin to save as SVG

from robofab.world import CurrentFont, RGlyph
import GlyphsApp

height = 700
margin = 200
minMargin = 0

font = CurrentFont()
layers = GlyphsApp.Glyphs.font.selectedLayers

Scale = float(height) / float(font.info.unitsPerEm)

for l in layers:
    g = RGlyph(layer=l)
    print(g.name)

    minX, minY, maxX, maxY = g.box
    extraSpace = 0
    offset = 0
    offsetY = 0
    offsetX = 0

    extraSpace *= Scale
    offset *= Scale
    width = margin + (g.width * Scale) + margin + extraSpace
    newPage(width, height + (2 * margin))

    # Draw transparent square
    fill(0, 0, 0, 0)  # Set fill color with alpha (0 for transparency)
    rect(0, 0, width, height + (2 * margin))

    fill(0, 0, 0)  # Set fill color to black
    translate(margin + offsetY, margin + offsetX)
    scale(Scale)

    drawGlyph(g)
    saveImage("export/" + g.name + ".svg")
2 Likes

I tried this macro panel script on glyphs/shapes that are using Native Color gradients, but most colors came out as flat colors, some with slight gradient (not same as design). Is it possible to retain the gradient data for the exported SVGs? Thanks! :slight_smile:

Update: I used picosvg to do the job instead