Skip to content

Commit

Permalink
Added TTF convert script
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Oct 11, 2019
1 parent 7622752 commit 9c9de88
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/convert.py
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)

0 comments on commit 9c9de88

Please sign in to comment.