Last active
December 16, 2023 18:18
-
-
Save fdiary/5dbade3da21bed6d7a8e0abf8ff697b8 to your computer and use it in GitHub Desktop.
Fontforge script to generate a vertical font : all glyphs are rotated 90 degrees anticlockwise
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 python | |
# | |
# Usage: | |
# fontforge -script generate_vertical_font.py original_font.ttf | |
import fontforge | |
import math | |
import os | |
import psMat | |
import sys | |
font_path = sys.argv[1] | |
ff = fontforge.open(font_path) | |
if ff.is_cid: | |
print 'CID font is not supported.' | |
sys.exit(1) | |
new_sfnt_names = tuple( | |
(i[0], i[1], 'Vert ' + i[2] if i[1] in ('Family', 'Fullname', 'Preferred Family', 'Trademark', 'UniqueID') else i[2]) | |
for i in ff.sfnt_names | |
) | |
ff.sfnt_names = new_sfnt_names | |
ff.fontname = 'Vert-' + ff.fontname | |
ff.familyname = 'Vert ' + ff.familyname | |
ff.fullname = 'Vert ' + ff.fullname | |
vertical_lookups = [x for x in ff.gsub_lookups if x.startswith("'vert'") or x.startswith("'vrt2'") or x in ('gsubvert', 'j-vert')] | |
vertical_lookup_subtables = sum( | |
[ff.getLookupSubtables(x) for x in vertical_lookups], | |
() | |
) | |
matrix = psMat.compose( | |
psMat.rotate(math.radians(90)), | |
psMat.translate(ff.em - ff.descent, -ff.descent), | |
) | |
for glyph in ff.glyphs(): | |
if glyph.width == ff.em: | |
for subtable in vertical_lookup_subtables: | |
tables = glyph.getPosSub(subtable) | |
if len(tables): | |
ff.selection.select(tables[0][2]) | |
ff.copy() | |
ff.selection.select(glyph.glyphname) | |
ff.paste() | |
break | |
glyph.transform(matrix) | |
ff.generate('Vert-' + os.path.splitext(os.path.basename(font_path))[0] + '.ttf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment