Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add otf2ttf #625

Merged
merged 3 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ before_build:
- flake8 python\afdko\fdkutils.py
- flake8 python\afdko\makeinstancesufo.py
- flake8 python\afdko\makeotf.py
- flake8 python\afdko\otf2ttf.py
- flake8 python\afdko\otfpdf.py
- flake8 python\afdko\pdfmetrics.py
- flake8 python\afdko\ttfcomponentizer.py
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ before_script:
- flake8 fdkutils.py
- flake8 makeinstancesufo.py
- flake8 makeotf.py
- flake8 otf2ttf.py
- flake8 otfpdf.py
- flake8 pdfmetrics.py
- flake8 ttfcomponentizer.py
Expand Down
92 changes: 92 additions & 0 deletions python/afdko/otf2ttf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
from __future__ import print_function, division, absolute_import
import sys
from fontTools.ttLib import TTFont, newTable
from cu2qu.pens import Cu2QuPen
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttx import makeOutputFileName
import argparse


# default approximation error, measured in UPEM
MAX_ERR = 1.0

# default 'post' table format
POST_FORMAT = 2.0

# assuming the input contours' direction is correctly set (counter-clockwise),
# we just flip it to clockwise
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 "]

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())

post = ttFont["post"]
post.formatType = post_format
post.extraNames = []
post.mapping = {}
post.glyphOrder = glyphOrder

ttFont.sfntVersion = "\000\001\000\000"


def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument("input", nargs='+', metavar="INPUT")
parser.add_argument("-o", "--output")
parser.add_argument("-e", "--max-error", type=float, default=MAX_ERR)
parser.add_argument("--post-format", type=float, default=POST_FORMAT)
parser.add_argument(
"--keep-direction", dest='reverse_direction', action='store_false')
options = parser.parse_args(args)

for path in options.input:
output = options.output or makeOutputFileName(path,
outputDir=None,
extension='.ttf')
font = TTFont(path)
otf_to_ttf(font,
post_format=options.post_format,
max_err=options.max_error,
reverse_direction=options.reverse_direction)
font.save(output)


if __name__ == "__main__":
sys.exit(main())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
booleanOperations~=0.8.0
cu2qu~=1.5.0
defcon~=0.5.3
fontMath~=0.4.7
fontPens~=0.1.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def _get_console_scripts():
('makeinstancesufo', 'makeinstancesufo:main'),
('otc2otf', 'otc2otf:main'),
('otf2otc', 'otf2otc:main'),
('otf2ttf', 'otf2ttf:main'),
('stemhist', 'stemhist:main'),
('ttfcomponentizer', 'ttfcomponentizer:main'),
('ttxn', 'ttxn:main'),
Expand Down
Loading