Created
January 15, 2025 13:23
-
-
Save zvodd/35ba80993b5bc28fe796c0233c2ad186 to your computer and use it in GitHub Desktop.
Export a selection of glyphs as SVG using FontForge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## In FontForge( https://fontforge.org ) with font of choice open press Ctrl + . to open the script dialog, paste and run. | |
## You will be asked to save a file. Font will be put in the selected folder with the prefix of the name entered. | |
# | |
## Recommend: Symbols NerdFont at https://www.nerdfonts.com/font-downloads | |
## | |
import os | |
import pprint | |
# Get the current font | |
current_font = fontforge.activeFont() | |
# Get the directory to save SVGs through FontForge's save dialog | |
output_path = fontforge.saveFilename("Choose directory for SVG exports", "", "") | |
if output_path: | |
# Extract the directory path | |
output_dir, name_prefix = os.path.split(output_path) | |
# Uncommet to prevent prefix | |
#name_prefix = "" | |
# Uncommet to select all glyphs | |
#current_font.selection.all() | |
glyphs = [current_font[g] for g in current_font.selection] | |
#fontforge.logWarning(pprint.pformat(glyphs[:10])) | |
if not glyphs: | |
# If no glyphs selected, ask user if they want to export all | |
if fontforge.askString("No glyphs selected", "Export all glyphs?"): | |
glyphs = list(current_font.glyphs()) | |
else: | |
fontforge.logWarning("Export cancelled - no glyphs selected") | |
# Export each glyph | |
for glyph in glyphs: | |
try: | |
svg_filename = f"{name_prefix}_{glyph.glyphname}.svg" | |
svg_path = os.path.join(output_dir, svg_filename) | |
glyph.export(svg_path) | |
fontforge.logWarning(f"Exported: {svg_filename}") | |
except: | |
fontforge.logWarning(f"Error exporting glyph '{glyph.glyphname}'") | |
fontforge.ask("Done", f"Export completed. Files saved to:\n{output_dir}", ["Ok"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment