Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@psmay
Created December 2, 2014 15:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psmay/fd3e7e91893f6012b262 to your computer and use it in GitHub Desktop.
Save psmay/fd3e7e91893f6012b262 to your computer and use it in GitHub Desktop.
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

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

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