Skip to content

Commit

Permalink
Merge pull request #712 from dougfelt/master
Browse files Browse the repository at this point in the history
Wrap ttLib cmap code that constructs format 12/13 tables.
  • Loading branch information
bstell committed Jun 23, 2015
2 parents b4a9024 + 3ee9df7 commit 06adab6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build_time/src/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from cleaner import Cleaner
from fontTools.ttLib.tables import _c_m_a_p
from fontTools_wrapper_funcs import change_method, _cmap_format_4_compile
from fontTools_wrapper_funcs import change_method, _cmap_format_4_compile, _cmap_format_12_or_13_compile
from glyph_sets import get_whitespace_and_ignorable_list


Expand All @@ -28,8 +28,10 @@ def clean_invalid_glyphs_and_remove_hinting(fontfile, hinting, output, verbose):
# subset of format 12.
# do we still what this?
change_method(_c_m_a_p.cmap_format_4,_cmap_format_4_compile, 'compile')
old_12_or_13_compile = change_method(_c_m_a_p.cmap_format_12_or_13, _cmap_format_12_or_13_compile, 'compile')
cleaner.save(output)
cleaner.close()
change_method(_c_m_a_p.cmap_format_12_or_13, old_12_or_13_compile, 'compile')


def cleanup(fontfile, hinting, output, verbose):
Expand Down
47 changes: 47 additions & 0 deletions build_time/src/fontTools_wrapper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
from fontTools.ttLib import getSearchRange
from fontTools.ttLib.tables import _c_m_a_p
from fontTools.misc.py23 import bytesjoin
import array
import operator
import struct
Expand Down Expand Up @@ -314,3 +315,49 @@ def _cmap_format_4_compile(self, ttFont):
return header + data


def _cmap_format_12_or_13_compile(self, ttFont):
if self.data:
return struct.pack(">HHLLL", self.format, self.reserved, self.length, self.language, self.nGroups) + self.data
charCodes = list(self.cmap.keys())
lenCharCodes = len(charCodes)
names = list(self.cmap.values())
nameMap = ttFont.getReverseGlyphMap()
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
except KeyError:
nameMap = ttFont.getReverseGlyphMap(rebuild=True)
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
except KeyError:
# allow virtual GIDs in format 12 tables
gids = []
for name in names:
try:
gid = nameMap[name]
except KeyError:
try:
if (name[:3] == 'gid'):
gid = eval(name[3:])
else:
gid = ttFont.getGlyphID(name)
except:
raise KeyError(name)

gids.append(gid)

cmap = {} # code:glyphID mapping
list(map(operator.setitem, [cmap]*len(charCodes), charCodes, gids))

charCodes.sort()
nGroups = 0
dataList = []
maxIndex = len(charCodes)
for index in range(maxIndex):
charCode = charCodes[index]
glyphID = cmap[charCode]
dataList.append(struct.pack(">LLL", charCode, charCode, glyphID))
nGroups = nGroups + 1
data = bytesjoin(dataList)
lengthSubtable = len(data) +16
assert len(data) == (nGroups*12) == (lengthSubtable-16)
return struct.pack(">HHLLL", self.format, self.reserved , lengthSubtable, self.language, nGroups) + data

0 comments on commit 06adab6

Please sign in to comment.