When importing SVGs into Glyphs programmatically there’s some surprising scaling behavior with notably inconsistent behavior between importing the paths into the master and importing an SVG as a background image.
Base Font
To make this easily repeatable, open Fira Sans Regular TTF in Glyphs.app: https://fonts.gstatic.com/s/firasans/v17/va9E4kDNxMZdWfMOD5VfkILKSTbndQ.ttf
This font is UPEM 1000, and has ascender to descender total height of 1200. (Ascender: 935, Descender: -265)
Test SVG
Create this file as test.svg
somewhere locally. You’ll note that it is height 1000, but it has elements that extend all the way to 1200. Elements beyond the height of the SVG do get imported as paths.
<svg xmlns="http://www.w3.org/2000/svg" height="1000" width="1200">
<rect x="0" y="0" width="100%" height="100%" fill="blue" />
<rect x="0" y="0" width="100" height="100" fill="red" />
<rect x="100" y="100" width="100" height="100" fill="red" />
<rect x="200" y="200" width="100" height="100" fill="red" />
<rect x="300" y="300" width="100" height="100" fill="red" />
<rect x="400" y="400" width="100" height="100" fill="red" />
<rect x="500" y="500" width="100" height="100" fill="red" />
<rect x="600" y="600" width="100" height="100" fill="red" />
<rect x="700" y="700" width="100" height="100" fill="red" />
<rect x="800" y="800" width="100" height="100" fill="red" />
<rect x="900" y="900" width="100" height="100" fill="red" />
<rect x="1000" y="1000" width="100" height="100" fill="red" />
<rect x="1100" y="1100" width="100" height="100" fill="red" />
</svg>
Import Macro
Needs to be edited to specify the directory containing the test SVG.
import os
from Foundation import NSURL
thisFont = Glyphs.font
# EDIT ME
directory = "/PATH/TO/DIR/CONTAINING/TEST/SVG/"
del thisFont.glyphs['test'] # more-easily run tests after each other.
for fileName in os.listdir(directory):
if fileName.endswith(".svg"):
svgFilePath = directory+fileName
svgFileUrl = NSURL.alloc().initFileURLWithPath_(svgFilePath)
glyphName = fileName.replace(".svg","")
unicodeValue = None
newGlyph = GSGlyph(glyphName, autoName=False)
newGlyph.name = glyphName
newGlyph.unicode = unicodeValue
# This fills out a whole bunch of defaults on newGlyph.
thisFont.glyphs.append(newGlyph)
# Populate the master layer.
masterLayer = newGlyph.layers[0]
masterLayer.importOutlinesFromURL_scale_error_(svgFileUrl, 1, None)
# Define the SVG layer.
svgLayer = GSLayer()
svgLayer.setSVGColorLayer_(True)
svgBackgroundImage = GSBackgroundImage.alloc().initWithURL_(svgFileUrl)
svgLayer.setBackgroundImage_(svgBackgroundImage)
# Add the layers.
newGlyph.layers.append(svgLayer)
Bug?
This is what the output looks like. To me, this is unexpected.