Created
May 10, 2022 03:55
-
-
Save anotherlab/d765163763cb8d7fdc33b447ff53a045 to your computer and use it in GitHub Desktop.
Generate a list of glyph names or a C# constants file from a font
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
#! /usr/bin/env python3 | |
# Use this to generate a list of glyph names from a font to a | |
# text file. This can be passed to pyftsubset to generate a | |
# small font. | |
# This can also generate a C# constants class from the font | |
# Usage: | |
# dump-cmap.py fontfile.ttf glyphlist.txt | |
# or | |
# dump-cmap.py fontfile.ttf constants.cs fontname namespace | |
# | |
# To create a small ttf file, use the following syntax | |
# pyftsubset fontfile.ttf --output-file=newfont.ttf --glyph-names --glyphs-file=glyphlist.txt | |
# | |
# This requires fonttools | |
# intall with pip | |
# pip imstall fonttools | |
# | |
# See https://gist.github.com/anotherlab/6a1198ee247e961882b09c40efb43c5f | |
# for script to collect glyphs used by a Xamarin.Forms or .NET MAUI project | |
from fontTools.ttLib import TTFont | |
import sys | |
def MakeGlyphNamesFile(glyphs, glyphNamesFile): | |
file1 = open(glyphNamesFile, "w+") | |
for glyph in glyphs: | |
file1.write(glyph+'\n') | |
file1.close() | |
return | |
def GlyphNameToDotNet(glyphname): | |
lst = glyphname.split('-') | |
newName = '' | |
for n in lst: | |
newName += n.capitalize() | |
return newName | |
def PythonHexToDotNet(code): | |
newCode = f'{code:08x}' | |
return newCode | |
def MakeDotNet(dic, fName, fontName, nameSpace): | |
file1 = open(fName, "w+") | |
file1.write('namespace ' + nameSpace + '\n') | |
file1.write('{\n') | |
file1.write(' public static class ' + fontName+ '\n') | |
file1.write(' {\n') | |
for glyph in dic: | |
file1.write(' public const string ' + GlyphNameToDotNet(glyph) + ' = "\\U' + PythonHexToDotNet(dic[glyph]) + '";\n') | |
file1.write(' }\n') | |
file1.write('}\n') | |
file1.close() | |
return | |
if len(sys.argv) < 3: | |
print("Usage:") | |
print("dump-cmap.py fontfile.ttf glyphlist.txt") | |
print("or") | |
print("dump-cmap.py fontfile.ttf constants.cs fontname namespace") | |
sys.exit(1) | |
glyphNamesFile = '-' | |
classFile = '-' | |
fontName = '-' | |
nameSpace = '-' | |
print(len(sys.argv)) | |
fontfile = sys.argv[1] | |
if (len(sys.argv) == 5): | |
classFile = sys.argv[2] | |
fontName = sys.argv[3] | |
nameSpace = sys.argv[4] | |
if (len(sys.argv) == 2): | |
glyphNamesFile = sys.argv[2] | |
font = TTFont(fontfile) | |
cmap = font['cmap'] | |
glyphs = {} | |
for table in cmap.tables: | |
if table.format in [12]: | |
for code, name in table.cmap.items(): | |
glyphs[name] = code | |
if glyphNamesFile != '-': | |
MakeGlyphNamesFile(glyphs, glyphNamesFile) | |
if classFile != '-': | |
if fontName != '-': | |
if nameSpace != '-': | |
MakeDotNet(glyphs, classFile, fontName, nameSpace) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment