Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notes on mass-importing SVG outlines into FontForge via Python

Notes on dumping SVG outlines into a FontForge file

Sources I've looked at:

I have a hunch that createMappedChar() doesn't work unless you've done loadNameList('glyphlist.txt') or loadNameList('aglfn.txt') (BSD-licensed Adobe glyph lists). An alternative seems to be to go by codepoint with createChar().

import fontforge
#fontforge.loadNameList('aglfn.txt') # Might be optional

#font = fontforge.open('existing.sfd')
font = fontforge.font() # new font

glyphNameAndOutlineFilename = {
    ('A', 'capital-a-glyph.svg'),
    ('Adieresis', 'capital-adieresis-glyph.svg'),
    ...
}

#characterAndOutlineFilename = {
#   ('A', 'capital-a-glyph.svg'),
#   ('Ä', 'capital-adieresis-glyph.svg'),
#   ...
#}

for name, filename in glyphNameAndOutlineFilename:
    glyph = font.createMappedChar(name)
    glyph.importOutlines(filename)
    # May need to set glyph.width here

#for c, filename in characterAndOutlineFilename:
#   glyph = font.createChar(ord(c))
#   glyph.importOutlines(filename)
#   # May need to set glyph.width here

#font.generate('output-font.ttf')
font.save('output-font.sfd')
@deszcz
Copy link

deszcz commented Jul 26, 2015

And i get:

Multiple names when parsing glyphlist for unicode ae
Failed to parse color
Failed to parse color
Internal Error: Some fragments did not join
Failed to parse color
Failed to parse color
Rename A by U0041
Rename B by U0042

@pelson
Copy link

pelson commented Mar 24, 2017

A critical piece that I needed was to set the font's encoding:

font.encoding = "UnicodeFull"

Without that, I was finding:

>>> font.createMappedChar('a').unicode
6356992

Rather than the expected 97.

@asdf8dfafjk
Copy link

asdf8dfafjk commented Nov 7, 2020

They still lose color for me ....

@psmay
Copy link
Author

psmay commented Nov 7, 2020

As in color emoji? I don't think FontForge has any support for that.

@asdf8dfafjk
Copy link

asdf8dfafjk commented Nov 9, 2020

Looks like you're right. Issue here for color support fontforge/fontforge#677

@br750
Copy link

br750 commented May 24, 2021

Hello, there is a way to set RBearing when i use a script to creat font from SVG ? Thank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment