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

[makeotf] Use FontTools to copy font tables #739

Merged
merged 2 commits into from
Feb 10, 2019
Merged
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
84 changes: 32 additions & 52 deletions python/afdko/makeotf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

from fontTools.ttLib import TTFont, TTLibError
from fontTools.ttLib.tables.DefaultTable import DefaultTable
from fontTools.misc.py23 import open, tounicode, tobytes

from afdko import convertfonttocid, fdkutils, ufotools
Expand Down Expand Up @@ -2167,11 +2168,6 @@ def copyTTFGlyphTables(inputFilePath, tempOutputPath, outputPath):
# tempOutputPath exists and is an OTF/CFF font.
# outputPath does not yet exist, or is the same as inputFilePath.

success, tableDump = fdkutils.get_shell_command_output([
'spot', tempOutputPath])
if not success:
raise MakeOTFShellError

# Get the final glyph name list.
success, output = fdkutils.get_shell_command_output([
'tx', '-mtx', tempOutputPath])
Expand All @@ -2180,49 +2176,32 @@ def copyTTFGlyphTables(inputFilePath, tempOutputPath, outputPath):

glyphList = re.findall(r"[\n\r]glyph\[\d+\]\s+{([^,]+)", output)

if os.path.exists(outputPath):
os.remove(outputPath)
font = TTFont(inputFilePath)
temp_font = TTFont(tempOutputPath)

print("Fixing output font 'post' table...")
fixPost(glyphList, inputFilePath, outputPath)
fixPost(glyphList, font)

print("Fixing output font 'head' table...")
fixHead(tempOutputPath, outputPath)
print("Fixing output font 'hhea' table...")
fixHhea(tempOutputPath, outputPath)
fixHead(temp_font, font)

tempTablePath = fdkutils.get_temp_file_path()
print("Fixing output font 'hhea' table...")
fixHhea(temp_font, font)

print("Copying makeotf-generated tables from temp OTF file to output "
"font...")
for tableTag in ["GDEF", "GSUB", "GPOS", "cmap", "name", "OS/2", "BASE"]:
if tableTag not in tableDump:
continue

if not fdkutils.run_shell_command([
'sfntedit', '-x', '%s=%s' % (tableTag, tempTablePath),
tempOutputPath]):
print("Error removing table '%s' from OTF font reference." %
tableTag)
raise MakeOTFShellError

if not fdkutils.run_shell_command([
'sfntedit', '-a', '%s=%s' % (tableTag, tempTablePath),
outputPath]):
print("Error adding makeotf-made table '%s' to TrueType font." %
tableTag)
raise MakeOTFShellError

print(" copied \"%s\"" % tableTag)

# fix checksums
if not fdkutils.run_shell_command(['sfntedit', '-f', outputPath]):
raise MakeOTFShellError
tags = ["GDEF", "GSUB", "GPOS", "cmap", "name", "OS/2", "BASE"]
copy_tables(temp_font, font, tags)

print("Succeeded in merging makeotf tables with TrueType source font to "
"final TrueType output font at '%s'." % outputPath)

font.save(outputPath)
font.close()
temp_font.close()


def fixPost(glyphList, inputFilePath, outputPath):
def fixPost(glyphList, font):
"""
Replace 'post' table
"""
Expand All @@ -2233,48 +2212,49 @@ def fixPost(glyphList, inputFilePath, outputPath):
if gname not in kStdNames:
extraNamesList.append(gname)

font = TTFont(inputFilePath)
post_table = font['post']
post_table.formatType = 2
post_table.glyphOrder = glyphOrderList
post_table.extraNames = extraNamesList
font.save(outputPath)


def update_table_items(table_tag, items_list, font1_path, font2_path):
def update_table_items(table_tag, items_list, font1, font2):
"""
font1 will get new values from font2
"""
font1 = TTFont(font1_path)
font2 = TTFont(font2_path)

font1_table = font1[table_tag]
font2_table = font2[table_tag]

for item_name in items_list:
setattr(font1_table, item_name, getattr(font2_table, item_name))

font1.save(font1_path)
font1.close()
font2.close()


def fixHead(tempOTFFilePath, outputPath):
def fixHead(temp_font, font):
"""
tempOTFFilePath is an OTF/CFF font.
outputPath is a TTF font.
temp_font is an OTF/CFF font.
font is a TTF font.
Need to update the head values. Can't just copy the entire table
from the OTF temp font, as as some of the head table values control
interpretation of the glyf data.
"""
head_items = ("fontRevision", "created", "modified", "macStyle",
"xMin", "xMax", "yMin", "yMax")
update_table_items('head', head_items, outputPath, tempOTFFilePath)
update_table_items('head', head_items, font, temp_font)


def fixHhea(tempOTFFilePath, outputPath):
def fixHhea(temp_font, font):
hhea_items = ("ascent", "descent", "lineGap")
update_table_items('hhea', hhea_items, outputPath, tempOTFFilePath)
update_table_items('hhea', hhea_items, font, temp_font)


def copy_tables(temp_font, font, tags):
for tag in tags:
if tag not in temp_font:
continue
table = DefaultTable(tag)
table.data = temp_font.getTableData(tag)
font[tag] = table
print(' copied "%s"' % tag)


def makeRelativePath(curDir, targetPath):
Expand Down