-
-
Save Philmist/0cb2f845e93a999c5f48ebebe0c7fedf 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 | |
import logging | |
from glob import glob | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
console_logger = logging.StreamHandler() | |
console_logger.setLevel(logging.DEBUG) | |
logger.addHandler(console_logger) | |
def create_vertical_font(font_path: str): | |
if "Vert-" in font_path: | |
info_string = f"{font_path} may be converted already, skip." | |
logger.info(info_string) | |
return | |
ff = fontforge.open(font_path) | |
if ff.is_cid: | |
info_string = f"{font_path} is CID font, skip." | |
logger.error(info_string) | |
return | |
info_string = f"Convert starting: {font_path}" | |
logger.info(info_string) | |
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) | |
converted_font_path = ( | |
"Vert-" + os.path.splitext(os.path.basename(font_path))[0] + ".ttf" | |
) | |
info_string = f"Writing font to {converted_font_path}" | |
logger.info(info_string) | |
ff.generate(converted_font_path) | |
if __name__ == "__main__": | |
fonts_path = [font for arg in sys.argv[1:] for font in glob(arg)] | |
for font in fonts_path: | |
create_vertical_font(font) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment