Last active
December 14, 2023 20:25
-
-
Save themorgantown/fe1920a9c983ab23267110cbfb82c9c7 to your computer and use it in GitHub Desktop.
Get SVGs of every glyph in every TTF, organized into folders
This file contains 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
# start with brew install fontforge | |
import fontforge | |
import os | |
import re | |
def sanitize_filename(name): | |
"""Sanitize the glyph name to be safe for filenames.""" | |
return re.sub(r'[\\/*?:"<>|]', '_', name) | |
# Directory containing your .ttf files | |
root_font_directory = "/input" | |
svg_output_directory = "/output" | |
# Create SVG output directory if it doesn't exist | |
if not os.path.exists(svg_output_directory): | |
os.makedirs(svg_output_directory) | |
print(f"Searching for fonts in {root_font_directory}") | |
# Traverse the directory tree | |
for subdir, dirs, files in os.walk(root_font_directory): | |
for file in files: | |
if file.endswith(".ttf"): | |
font_path = os.path.join(subdir, file) | |
try: | |
font = fontforge.open(font_path) | |
font_name = os.path.splitext(file)[0] | |
print(f"Processing font: {font_name} in {subdir}") | |
# Create a new directory for each font file | |
font_directory = os.path.join(svg_output_directory, font_name) | |
os.makedirs(font_directory, exist_ok=True) | |
# Export each glyph to an SVG file | |
for glyph in font.glyphs(): | |
glyph_name = sanitize_filename(glyph.glyphname) | |
svg_filename = f"{font_name}_{glyph_name}.svg" | |
full_path = os.path.join(font_directory, svg_filename) | |
# Get the bounding box of the glyph | |
bbox = glyph.boundingBox() | |
width = bbox[2] - bbox[0] | |
height = bbox[3] - bbox[1] | |
# Export the glyph using its bounding box dimensions | |
glyph.export(full_path, format='svg', width=width, height=height) | |
print(f"Exported {glyph_name} to {full_path}") | |
font.close() | |
except Exception as e: | |
print(f"Error processing {font_path}: {e}") | |
print("SVG export complete.") | |
# optional convert from svg to svgz without deleting originals | |
import os | |
import gzip | |
import shutil | |
def compress_svg_to_svgz(svg_path, svgz_path): | |
with open(svg_path, 'rb') as f_in, gzip.open(svgz_path, 'wb') as f_out: | |
shutil.copyfileobj(f_in, f_out) | |
# Directory containing your SVG files | |
svg_directory = "/Users/daniel/Dropbox/JakeSiteDL/luc.devroye.org/pape/svg" | |
print(f"Converting SVG files in {svg_directory} to SVGZ") | |
# Traverse the directory tree | |
for subdir, dirs, files in os.walk(svg_directory): | |
for file in files: | |
if file.endswith(".svg"): | |
svg_path = os.path.join(subdir, file) | |
svgz_path = svg_path + 'z' # Append 'z' to make the extension '.svgz' | |
# Compress the SVG to SVGZ | |
compress_svg_to_svgz(svg_path, svgz_path) | |
print(f"Converted {svg_path} to {svgz_path}") | |
print("SVG to SVGZ conversion complete.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment