Skip to content

Instantly share code, notes, and snippets.

@thpham
Created January 15, 2025 22:12
Show Gist options
  • Save thpham/85676543fdcdee18a3d43be329c437bf to your computer and use it in GitHub Desktop.
Save thpham/85676543fdcdee18a3d43be329c437bf to your computer and use it in GitHub Desktop.
Create a custom font file and copy missing glyphs from source font.
import fontforge
# Open the source and target fonts
source_font = fontforge.open("fa-brands-400.ttf")
target_font = fontforge.open("original.ttf")
# Define the list of missing glyphs (for simplicity, you can define them manually)
missing_glyphs = ["x-twitter", "square-x-twitter"] # Add your missing glyphs here
# Iterate over each missing glyph
for glyph_name in missing_glyphs:
# Check if the glyph exists in the source font
if glyph_name in source_font:
source_glyph = source_font[glyph_name]
# Check if the glyph exists in the target font
if glyph_name not in target_font:
# Create new character in target font if it doesn't exist
target_font.createChar(source_glyph.unicode, glyph_name)
# Now we ensure the glyph exists in the target font
target_glyph = target_font[glyph_name]
# Clear existing outlines in the target glyph (if any)
target_glyph.clear()
source_glyph.export(f"{glyph_name}.svg")
# Copy outlines from the source glyph to the target glyph
target_glyph.importOutlines(
f"{glyph_name}.svg"
) # Export source glyph to SVG, then import
print(f"Glyph {glyph_name} fully added/updated in target font.")
# Save the target font with updated glyphs
target_font.save("FontAwesome.font")
final_font = fontforge.open("FontAwesome.font")
final_font.generate("FontAwesome.ttf")
print("Generated FontAwesome.ttf with added glyphs.")
# Close the fonts
source_font.close()
target_font.close()
final_font.close()

Notes

fontforge -lang=py -c "
import fontforge

# Open the font file
font = fontforge.open('FontAwesome.ttf')

# List of glyph names to filter
target_glyphs = ['x-twitter', 'square-x-twitter']

# Loop over all glyphs
for glyph in font.glyphs():
    name = glyph.glyphname  # Get the name of the current glyph
    if name in target_glyphs:  # Check if the name matches one of the target names
        unicode = glyph.unicode  # Get the Unicode of the current glyph
        if unicode != -1:  # Only process glyphs that have a Unicode value
            print(f'Glyph {name} has Unicode U+{unicode:04X}')  # Print name and Unicode in hexadecimal

# Close the font
font.close()
"


fontforge -lang=py -script merge_fonts.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment