-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from fontTools.ttLib import TTFont, newTable | ||
from fontTools.pens.ttGlyphPen import TTGlyphPen | ||
from fontTools.misc.cliTools import makeOutputFileName | ||
from fontTools import configLogger | ||
from cu2qu.pens import Cu2QuPen | ||
import sys | ||
import os | ||
import logging | ||
import argparse | ||
from glob import glob | ||
|
||
LILEX_OTF_GLOB = 'build/otf/*.otf' | ||
OUTPUT_DIR = 'build/ttf-not_hinted/' | ||
|
||
|
||
log = logging.getLogger() | ||
|
||
MAX_ERR = 1.0 | ||
|
||
POST_FORMAT = 2.0 | ||
|
||
REVERSE_DIRECTION = True | ||
|
||
|
||
def glyphs_to_quadratic( | ||
glyphs, max_err=MAX_ERR, reverse_direction=REVERSE_DIRECTION): | ||
quadGlyphs = {} | ||
for gname in glyphs.keys(): | ||
glyph = glyphs[gname] | ||
ttPen = TTGlyphPen(glyphs) | ||
cu2quPen = Cu2QuPen(ttPen, max_err, | ||
reverse_direction=reverse_direction) | ||
glyph.draw(cu2quPen) | ||
quadGlyphs[gname] = ttPen.glyph() | ||
return quadGlyphs | ||
|
||
|
||
def otf_to_ttf(ttFont, post_format=POST_FORMAT, **kwargs): | ||
assert ttFont.sfntVersion == "OTTO" | ||
assert "CFF " in ttFont | ||
|
||
glyphOrder = ttFont.getGlyphOrder() | ||
|
||
ttFont["loca"] = newTable("loca") | ||
ttFont["glyf"] = glyf = newTable("glyf") | ||
glyf.glyphOrder = glyphOrder | ||
glyf.glyphs = glyphs_to_quadratic(ttFont.getGlyphSet(), **kwargs) | ||
del ttFont["CFF "] | ||
glyf.compile(ttFont) | ||
|
||
ttFont["maxp"] = maxp = newTable("maxp") | ||
maxp.tableVersion = 0x00010000 | ||
maxp.maxZones = 1 | ||
maxp.maxTwilightPoints = 0 | ||
maxp.maxStorage = 0 | ||
maxp.maxFunctionDefs = 0 | ||
maxp.maxInstructionDefs = 0 | ||
maxp.maxStackElements = 0 | ||
maxp.maxSizeOfInstructions = 0 | ||
maxp.maxComponentElements = max( | ||
len(g.components if hasattr(g, 'components') else []) | ||
for g in glyf.glyphs.values()) | ||
maxp.compile(ttFont) | ||
|
||
post = ttFont["post"] | ||
post.formatType = post_format | ||
post.extraNames = [] | ||
post.mapping = {} | ||
post.glyphOrder = glyphOrder | ||
try: | ||
post.compile(ttFont) | ||
except OverflowError: | ||
post.formatType = 3 | ||
log.warning("Dropping glyph names, they do not fit in 'post' table.") | ||
|
||
ttFont.sfntVersion = "\000\001\000\000" | ||
|
||
|
||
files = glob(LILEX_OTF_GLOB) | ||
for input_file in files: | ||
output = makeOutputFileName(input_file, outputDir=OUTPUT_DIR, | ||
extension='.ttf', | ||
overWrite=True) | ||
font = TTFont(input_file, fontNumber=0) | ||
otf_to_ttf(font, | ||
post_format=POST_FORMAT, | ||
max_err=MAX_ERR, | ||
reverse_direction=True) | ||
font.save(output) |