2013-07-21 18:16:55 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Python OpenType Layout Subsetter
|
2013-07-23 11:17:35 -04:00
|
|
|
#
|
|
|
|
# Copyright 2013 Google, Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
# Google Author(s): Behdad Esfahbod
|
|
|
|
#
|
2013-07-21 18:16:55 -04:00
|
|
|
|
2013-07-24 14:37:58 -04:00
|
|
|
# Try running on PyPy
|
|
|
|
try:
|
|
|
|
import numpypy
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
import fontTools.ttx
|
2013-07-24 16:08:35 -04:00
|
|
|
import struct
|
2013-07-21 18:16:55 -04:00
|
|
|
|
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
def add_method (*clazzes):
|
2013-07-21 18:16:55 -04:00
|
|
|
def wrapper(method):
|
2013-07-21 18:40:59 -04:00
|
|
|
for clazz in clazzes:
|
2013-07-24 14:41:47 -04:00
|
|
|
assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
|
2013-07-21 18:40:59 -04:00
|
|
|
setattr (clazz, method.func_name, method)
|
2013-07-21 18:16:55 -04:00
|
|
|
return wrapper
|
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
def unique_sorted (l):
|
2013-07-31 13:33:31 -04:00
|
|
|
return sorted (set (l))
|
2013-07-23 10:23:42 -04:00
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
def safeEval(data, eval=eval):
|
|
|
|
"""A (kindof) safe replacement for eval."""
|
|
|
|
return eval(data, {"__builtins__":{}}, {})
|
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Coverage)
|
2013-07-31 13:50:51 -04:00
|
|
|
def intersect (self, glyphs):
|
2013-07-23 14:52:18 -04:00
|
|
|
"Returns ascending list of matching coverage values."
|
|
|
|
return [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
|
|
|
|
|
2013-08-12 19:24:24 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Coverage)
|
|
|
|
def intersect_glyphs (self, glyphs):
|
|
|
|
"Returns set of intersecting glyphs."
|
|
|
|
return set (g for g in self.glyphs if g in glyphs)
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Coverage)
|
2013-07-31 13:50:51 -04:00
|
|
|
def subset (self, glyphs):
|
2013-07-23 10:50:43 -04:00
|
|
|
"Returns ascending list of remaining coverage values."
|
2013-07-31 13:50:51 -04:00
|
|
|
indices = self.intersect (glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.glyphs = [g for g in self.glyphs if g in glyphs]
|
|
|
|
return indices
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-08-08 22:26:49 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Coverage)
|
|
|
|
def remap (self, coverage_map):
|
|
|
|
"Remaps coverage."
|
|
|
|
self.glyphs = [self.glyphs[i] for i in coverage_map]
|
|
|
|
|
2013-07-23 22:17:39 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ClassDef)
|
2013-07-31 13:50:51 -04:00
|
|
|
def intersect (self, glyphs):
|
2013-08-08 21:09:27 -04:00
|
|
|
"Returns ascending list of matching class values."
|
2013-08-08 21:12:45 -04:00
|
|
|
return unique_sorted (([0] if any (g not in self.classDefs for g in glyphs) else []) + \
|
2013-08-08 21:09:27 -04:00
|
|
|
[v for g,v in self.classDefs.items() if g in glyphs])
|
2013-07-23 22:17:39 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ClassDef)
|
2013-08-12 19:24:24 -04:00
|
|
|
def intersect_class (self, glyphs, klass):
|
|
|
|
"Returns set of glyphs matching class."
|
2013-08-05 22:47:14 -04:00
|
|
|
if klass == 0:
|
2013-08-12 19:24:24 -04:00
|
|
|
return set (g for g in glyphs if g not in self.classDefs)
|
|
|
|
return set (g for g,v in self.classDefs.items() if v == klass and g in glyphs)
|
2013-07-23 22:17:39 -04:00
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ClassDef)
|
2013-07-31 13:50:51 -04:00
|
|
|
def subset (self, glyphs, remap=False):
|
2013-08-08 21:12:45 -04:00
|
|
|
"Returns ascending list of remaining classes."
|
2013-07-21 18:16:55 -04:00
|
|
|
self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
|
2013-08-08 21:12:45 -04:00
|
|
|
# Note: while class 0 has the special meaning of "not matched", if no glyph will
|
|
|
|
# ever /not match/, we can optimize class 0 out too.
|
|
|
|
indices = unique_sorted (([0] if any (g not in self.classDefs for g in glyphs) else []) + \
|
|
|
|
self.classDefs.values ())
|
2013-07-24 12:40:54 -04:00
|
|
|
if remap:
|
|
|
|
self.remap (indices)
|
|
|
|
return indices
|
2013-07-22 12:15:36 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ClassDef)
|
|
|
|
def remap (self, class_map):
|
|
|
|
"Remaps classes."
|
|
|
|
self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format in [1, 2]:
|
2013-08-08 22:59:32 -04:00
|
|
|
return [v for g,v in self.mapping.items() if g in cur_glyphs]
|
2013-07-23 14:52:18 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format in [1, 2]:
|
2013-08-08 22:26:49 -04:00
|
|
|
self.mapping = {g:v for g,v in self.mapping.items() if g in s.glyphs and v in s.glyphs}
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.mapping)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format == 1:
|
2013-08-08 22:59:32 -04:00
|
|
|
indices = self.Coverage.intersect (cur_glyphs)
|
2013-07-23 14:52:18 -04:00
|
|
|
return sum ((self.Sequence[i].Substitute for i in indices), [])
|
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Sequence = [self.Sequence[i] for i in indices]
|
2013-08-08 22:26:49 -04:00
|
|
|
# Now drop rules generating glyphs we don't want
|
|
|
|
indices = [i for i,seq in enumerate (self.Sequence) \
|
|
|
|
if all (sub in s.glyphs for sub in seq.Substitute)]
|
|
|
|
self.Sequence = [self.Sequence[i] for i in indices]
|
|
|
|
self.Coverage.remap (indices)
|
2013-07-21 23:15:32 -04:00
|
|
|
self.SequenceCount = len (self.Sequence)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.SequenceCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format == 1:
|
2013-08-08 22:59:32 -04:00
|
|
|
return sum ((vlist for g,vlist in self.alternates.items() if g in cur_glyphs), [])
|
2013-07-23 14:52:18 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-08-08 22:26:49 -04:00
|
|
|
self.alternates = {g:vlist for g,vlist in self.alternates.items() \
|
|
|
|
if g in s.glyphs and all (v in s.glyphs for v in vlist)}
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.alternates)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:20:13 -04:00
|
|
|
return sum (([seq.LigGlyph for seq in seqs if all(c in s.glyphs for c in seq.Component)]
|
2013-08-08 22:59:32 -04:00
|
|
|
for g,seqs in self.ligatures.items() if g in cur_glyphs), [])
|
2013-07-23 14:52:18 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 23:15:32 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
self.ligatures = {g:v for g,v in self.ligatures.items() if g in s.glyphs}
|
2013-08-08 22:26:49 -04:00
|
|
|
self.ligatures = {g:[seq for seq in seqs \
|
|
|
|
if seq.LigGlyph in s.glyphs and \
|
|
|
|
all(c in s.glyphs for c in seq.Component)]
|
2013-07-21 23:15:32 -04:00
|
|
|
for g,seqs in self.ligatures.items()}
|
|
|
|
self.ligatures = {g:v for g,v in self.ligatures.items() if v}
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.ligatures)
|
2013-07-21 23:15:32 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
2013-07-21 18:16:55 -04:00
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format == 1:
|
2013-08-08 22:59:32 -04:00
|
|
|
indices = self.Coverage.intersect (cur_glyphs)
|
2013-07-23 14:52:18 -04:00
|
|
|
if not indices or \
|
2013-07-31 14:20:13 -04:00
|
|
|
not all (c.intersect (s.glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
|
2013-07-23 14:52:18 -04:00
|
|
|
return []
|
|
|
|
return [self.Substitute[i] for i in indices]
|
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Substitute = [self.Substitute[i] for i in indices]
|
2013-08-08 22:26:49 -04:00
|
|
|
# Now drop rules generating glyphs we don't want
|
|
|
|
indices = [i for i,sub in enumerate (self.Substitute) \
|
|
|
|
if sub in s.glyphs]
|
|
|
|
self.Substitute = [self.Substitute[i] for i in indices]
|
|
|
|
self.Coverage.remap (indices)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.GlyphCount = len (self.Substitute)
|
2013-07-31 14:11:40 -04:00
|
|
|
return bool (self.GlyphCount and all (c.subset (s.glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SinglePos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
return len (self.Coverage.subset (s.glyphs))
|
2013-07-21 18:16:55 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Value = [self.Value[i] for i in indices]
|
|
|
|
self.ValueCount = len (self.Value)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.ValueCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.PairPos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.PairSet = [self.PairSet[i] for i in indices]
|
|
|
|
for p in self.PairSet:
|
2013-07-31 14:11:40 -04:00
|
|
|
p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in s.glyphs]
|
2013-07-21 18:16:55 -04:00
|
|
|
p.PairValueCount = len (p.PairValueRecord)
|
2013-07-21 23:15:32 -04:00
|
|
|
self.PairSet = [p for p in self.PairSet if p.PairValueCount]
|
2013-07-21 18:16:55 -04:00
|
|
|
self.PairSetCount = len (self.PairSet)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.PairSetCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-31 14:11:40 -04:00
|
|
|
class1_map = self.ClassDef1.subset (s.glyphs, remap=True)
|
|
|
|
class2_map = self.ClassDef2.subset (s.glyphs, remap=True)
|
2013-07-22 12:15:36 -04:00
|
|
|
self.Class1Record = [self.Class1Record[i] for i in class1_map]
|
|
|
|
for c in self.Class1Record:
|
|
|
|
c.Class2Record = [c.Class2Record[i] for i in class2_map]
|
|
|
|
self.Class1Count = len (class1_map)
|
|
|
|
self.Class2Count = len (class2_map)
|
2013-07-31 14:11:40 -04:00
|
|
|
return bool (self.Class1Count and self.Class2Count and self.Coverage.subset (s.glyphs))
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.CursivePos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
|
2013-07-21 23:15:32 -04:00
|
|
|
self.EntryExitCount = len (self.EntryExitRecord)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.EntryExitCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
mark_indices = self.MarkCoverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
|
|
|
|
self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
|
2013-07-31 14:11:40 -04:00
|
|
|
base_indices = self.BaseCoverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
|
|
|
|
self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
|
2013-07-22 12:31:33 -04:00
|
|
|
# Prune empty classes
|
2013-07-23 10:23:42 -04:00
|
|
|
class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
|
2013-07-22 12:31:33 -04:00
|
|
|
self.ClassCount = len (class_indices)
|
|
|
|
for m in self.MarkArray.MarkRecord:
|
|
|
|
m.Class = class_indices.index (m.Class)
|
|
|
|
for b in self.BaseArray.BaseRecord:
|
|
|
|
b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
mark_indices = self.MarkCoverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
|
|
|
|
self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
|
2013-07-31 14:11:40 -04:00
|
|
|
ligature_indices = self.LigatureCoverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
|
|
|
|
self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
|
2013-07-22 12:31:33 -04:00
|
|
|
# Prune empty classes
|
2013-07-23 10:23:42 -04:00
|
|
|
class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
|
2013-07-22 12:31:33 -04:00
|
|
|
self.ClassCount = len (class_indices)
|
|
|
|
for m in self.MarkArray.MarkRecord:
|
|
|
|
m.Class = class_indices.index (m.Class)
|
|
|
|
for l in self.LigatureArray.LigatureAttach:
|
|
|
|
for c in l.ComponentRecord:
|
|
|
|
c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
mark1_indices = self.Mark1Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
|
|
|
|
self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
|
2013-07-31 14:11:40 -04:00
|
|
|
mark2_indices = self.Mark2Coverage.subset (s.glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
|
|
|
|
self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
|
2013-07-22 12:31:33 -04:00
|
|
|
# Prune empty classes
|
2013-07-23 10:23:42 -04:00
|
|
|
class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
|
2013-07-22 12:31:33 -04:00
|
|
|
self.ClassCount = len (class_indices)
|
|
|
|
for m in self.Mark1Array.MarkRecord:
|
|
|
|
m.Class = class_indices.index (m.Class)
|
|
|
|
for b in self.Mark2Array.Mark2Record:
|
|
|
|
b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.MultipleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.AlternateSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.LigatureSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.SinglePos,
|
|
|
|
fontTools.ttLib.tables.otTables.PairPos,
|
|
|
|
fontTools.ttLib.tables.otTables.CursivePos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkBasePos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkLigPos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkMarkPos)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.MultipleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.AlternateSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.LigatureSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.SinglePos,
|
|
|
|
fontTools.ttLib.tables.otTables.PairPos,
|
|
|
|
fontTools.ttLib.tables.otTables.CursivePos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkBasePos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkLigPos,
|
|
|
|
fontTools.ttLib.tables.otTables.MarkMarkPos)
|
|
|
|
def collect_lookups (self):
|
|
|
|
return []
|
|
|
|
|
2013-07-23 16:00:32 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
|
|
|
|
def __classify_context (self):
|
2013-07-23 22:51:50 -04:00
|
|
|
|
|
|
|
class ContextHelper:
|
|
|
|
def __init__ (self, klass, Format):
|
|
|
|
if klass.__name__.endswith ('Subst'):
|
2013-07-23 16:35:23 -04:00
|
|
|
Typ = 'Sub'
|
|
|
|
Type = 'Subst'
|
|
|
|
else:
|
|
|
|
Typ = 'Pos'
|
|
|
|
Type = 'Pos'
|
2013-07-23 22:51:50 -04:00
|
|
|
if klass.__name__.startswith ('Chain'):
|
2013-07-23 16:35:23 -04:00
|
|
|
Chain = 'Chain'
|
2013-07-23 16:18:30 -04:00
|
|
|
else:
|
2013-07-23 16:35:23 -04:00
|
|
|
Chain = ''
|
|
|
|
ChainTyp = Chain+Typ
|
|
|
|
|
|
|
|
self.Typ = Typ
|
|
|
|
self.Type = Type
|
|
|
|
self.Chain = Chain
|
|
|
|
self.ChainTyp = ChainTyp
|
|
|
|
|
|
|
|
self.LookupRecord = Type+'LookupRecord'
|
|
|
|
|
2013-07-23 22:57:43 -04:00
|
|
|
if Format == 1:
|
2013-08-08 22:59:32 -04:00
|
|
|
Coverage = lambda r: r.Coverage
|
|
|
|
ChainCoverage = lambda r: r.Coverage
|
2013-08-12 19:24:24 -04:00
|
|
|
ContextData = lambda r: (None,)
|
|
|
|
ChainContextData = lambda r: (None, None, None)
|
|
|
|
RuleData = lambda r: (r.Input,)
|
|
|
|
ChainRuleData = lambda r: (r.Backtrack, r.Input, r.LookAhead)
|
2013-07-24 11:24:39 -04:00
|
|
|
SetRuleData = None
|
|
|
|
ChainSetRuleData = None
|
2013-07-23 22:57:43 -04:00
|
|
|
elif Format == 2:
|
2013-08-08 22:59:32 -04:00
|
|
|
Coverage = lambda r: r.Coverage
|
|
|
|
ChainCoverage = lambda r: r.Coverage
|
2013-07-24 11:26:43 -04:00
|
|
|
ContextData = lambda r: (r.ClassDef,)
|
|
|
|
ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
|
|
|
|
RuleData = lambda r: (r.Class,)
|
|
|
|
ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
|
|
|
|
def SetRuleData (r, d): (r.Class,) = d
|
|
|
|
def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
|
2013-07-23 22:57:43 -04:00
|
|
|
elif Format == 3:
|
2013-08-08 22:59:32 -04:00
|
|
|
Coverage = lambda r: r.Coverage[0]
|
|
|
|
ChainCoverage = lambda r: r.InputCoverage[0]
|
2013-07-23 22:57:43 -04:00
|
|
|
ContextData = None
|
|
|
|
ChainContextData = None
|
|
|
|
RuleData = lambda r: r.Coverage
|
|
|
|
ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
|
2013-07-24 11:24:39 -04:00
|
|
|
SetRuleData = None
|
|
|
|
ChainSetRuleData = None
|
2013-07-23 22:57:43 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % Format
|
|
|
|
|
2013-07-23 17:17:21 -04:00
|
|
|
if Chain:
|
2013-08-08 22:59:32 -04:00
|
|
|
self.Coverage = ChainCoverage
|
2013-07-23 21:08:26 -04:00
|
|
|
self.ContextData = ChainContextData
|
2013-07-23 22:17:39 -04:00
|
|
|
self.RuleData = ChainRuleData
|
2013-07-24 11:24:39 -04:00
|
|
|
self.SetRuleData = ChainSetRuleData
|
2013-07-23 17:17:21 -04:00
|
|
|
else:
|
2013-08-08 22:59:32 -04:00
|
|
|
self.Coverage = Coverage
|
2013-07-23 21:08:26 -04:00
|
|
|
self.ContextData = ContextData
|
2013-07-23 22:17:39 -04:00
|
|
|
self.RuleData = RuleData
|
2013-07-24 11:24:39 -04:00
|
|
|
self.SetRuleData = SetRuleData
|
2013-07-23 16:35:23 -04:00
|
|
|
|
2013-07-23 23:00:39 -04:00
|
|
|
if Format == 1:
|
|
|
|
self.Rule = ChainTyp+'Rule'
|
|
|
|
self.RuleCount = ChainTyp+'RuleCount'
|
|
|
|
self.RuleSet = ChainTyp+'RuleSet'
|
|
|
|
self.RuleSetCount = ChainTyp+'RuleSetCount'
|
2013-08-12 19:24:24 -04:00
|
|
|
self.Intersect = lambda glyphs, ContextData, RuleData: [RuleData] if RuleData in glyphs else []
|
2013-07-23 23:00:39 -04:00
|
|
|
elif Format == 2:
|
|
|
|
self.Rule = ChainTyp+'ClassRule'
|
|
|
|
self.RuleCount = ChainTyp+'ClassRuleCount'
|
|
|
|
self.RuleSet = ChainTyp+'ClassSet'
|
|
|
|
self.RuleSetCount = ChainTyp+'ClassSetCount'
|
2013-08-12 19:24:24 -04:00
|
|
|
self.Intersect = lambda glyphs, ContextData, RuleData: ContextData.intersect_class (glyphs, RuleData)
|
2013-07-23 23:07:42 -04:00
|
|
|
|
2013-07-23 23:00:39 -04:00
|
|
|
self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
|
2013-07-23 16:40:47 -04:00
|
|
|
|
2013-07-23 22:51:50 -04:00
|
|
|
if self.Format not in [1, 2, 3]:
|
|
|
|
return None # Don't shoot the messenger; let it go
|
|
|
|
if not hasattr (self.__class__, "__ContextHelpers"):
|
|
|
|
self.__class__.__ContextHelpers = {}
|
|
|
|
if self.Format not in self.__class__.__ContextHelpers:
|
|
|
|
self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
|
|
|
|
return self.__class__.__ContextHelpers[self.Format]
|
2013-07-23 16:00:32 -04:00
|
|
|
|
2013-07-23 17:31:54 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
if cur_glyphs == None: cur_glyphs = s.glyphs
|
2013-07-23 17:17:21 -04:00
|
|
|
c = self.__classify_context ()
|
|
|
|
|
2013-08-08 22:59:32 -04:00
|
|
|
indices = c.Coverage (self).intersect (s.glyphs)
|
|
|
|
if not indices:
|
|
|
|
return []
|
2013-08-12 19:24:24 -04:00
|
|
|
cur_glyphs = c.Coverage (self).intersect_glyphs (s.glyphs);
|
2013-08-08 22:59:32 -04:00
|
|
|
|
2013-07-23 15:33:00 -04:00
|
|
|
if self.Format == 1:
|
2013-08-12 19:24:24 -04:00
|
|
|
ContextData = c.ContextData (self)
|
2013-07-23 17:42:17 -04:00
|
|
|
rss = getattr (self, c.RuleSet)
|
2013-08-12 19:24:24 -04:00
|
|
|
add = []
|
|
|
|
for i in indices:
|
|
|
|
if not rss[i]: continue
|
|
|
|
for r in getattr (rss[i], c.Rule):
|
|
|
|
if not r: continue
|
|
|
|
if all (all (c.Intersect (s.glyphs, cd, k) for k in klist)
|
|
|
|
for cd,klist in zip (ContextData, c.RuleData (r))):
|
|
|
|
for ll in getattr (r, c.LookupRecord):
|
|
|
|
if not ll: continue
|
|
|
|
seqi = ll.SequenceIndex
|
|
|
|
if seqi == 0:
|
|
|
|
pos_glyphs = set (c.Coverage (self).glyphs[i])
|
|
|
|
else:
|
|
|
|
pos_glyphs = set (r.Input[seqi - 1])
|
|
|
|
lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
|
|
|
|
add.extend (lookup.closure_glyphs (s, cur_glyphs=pos_glyphs))
|
|
|
|
return add
|
2013-07-23 15:33:00 -04:00
|
|
|
elif self.Format == 2:
|
2013-08-12 19:24:24 -04:00
|
|
|
ClassDef = getattr (self, c.ClassDef)
|
|
|
|
indices = ClassDef.intersect (cur_glyphs)
|
|
|
|
ContextData = c.ContextData (self)
|
2013-07-23 23:00:39 -04:00
|
|
|
rss = getattr (self, c.RuleSet)
|
2013-08-12 19:24:24 -04:00
|
|
|
add = []
|
|
|
|
for i in indices:
|
|
|
|
if not rss[i]: continue
|
|
|
|
for r in getattr (rss[i], c.Rule):
|
|
|
|
if not r: continue
|
|
|
|
if all (all (c.Intersect (s.glyphs, cd, k) for k in klist)
|
|
|
|
for cd,klist in zip (ContextData, c.RuleData (r))):
|
|
|
|
for ll in getattr (r, c.LookupRecord):
|
|
|
|
if not ll: continue
|
|
|
|
seqi = ll.SequenceIndex
|
|
|
|
if seqi == 0:
|
|
|
|
pos_glyphs = ClassDef.intersect_class (cur_glyphs, i)
|
|
|
|
else:
|
|
|
|
pos_glyphs = ClassDef.intersect_class (s.glyphs, r.Input[seqi - 1])
|
|
|
|
lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
|
|
|
|
add.extend (lookup.closure_glyphs (s, cur_glyphs=pos_glyphs))
|
|
|
|
return add
|
2013-07-23 15:33:00 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-31 14:20:13 -04:00
|
|
|
if not all (x.intersect (s.glyphs) for x in c.RuleData (self)):
|
2013-07-23 15:33:00 -04:00
|
|
|
return []
|
2013-08-12 19:24:24 -04:00
|
|
|
r = self
|
|
|
|
add = []
|
|
|
|
for ll in getattr (r, c.LookupRecord):
|
|
|
|
if not ll: continue
|
|
|
|
seqi = ll.SequenceIndex
|
|
|
|
if seqi == 0:
|
|
|
|
pos_glyphs = cur_glyphs
|
|
|
|
else:
|
|
|
|
pos_glyphs = r.InputCoverage[seqi].intersect_glyphs (s.glyphs)
|
|
|
|
lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
|
|
|
|
add.extend (lookup.closure_glyphs (s, cur_glyphs=pos_glyphs))
|
|
|
|
return add
|
2013-07-23 15:33:00 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 17:27:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
|
|
|
|
fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-23 17:07:06 -04:00
|
|
|
c = self.__classify_context ()
|
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
indices = self.Coverage.subset (s.glyphs)
|
2013-07-23 17:07:06 -04:00
|
|
|
rss = getattr (self, c.RuleSet)
|
|
|
|
rss = [rss[i] for i in indices]
|
|
|
|
for rs in rss:
|
2013-08-12 15:41:30 -04:00
|
|
|
if not rs: continue
|
|
|
|
ss = getattr (rs, c.Rule)
|
|
|
|
ss = [r for r in ss \
|
2013-08-12 19:24:24 -04:00
|
|
|
if r and all (all (g in s.glyphs for g in glist) \
|
|
|
|
for glist in c.RuleData (r))]
|
2013-08-12 15:41:30 -04:00
|
|
|
setattr (rs, c.Rule, ss)
|
|
|
|
setattr (rs, c.RuleCount, len (ss))
|
2013-07-23 17:27:18 -04:00
|
|
|
# Prune empty subrulesets
|
2013-07-23 21:33:13 -04:00
|
|
|
rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
|
2013-07-23 17:07:06 -04:00
|
|
|
setattr (self, c.RuleSet, rss)
|
|
|
|
setattr (self, c.RuleSetCount, len (rss))
|
|
|
|
return bool (rss)
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-31 14:11:40 -04:00
|
|
|
if not self.Coverage.subset (s.glyphs):
|
2013-07-23 22:41:11 -04:00
|
|
|
return False
|
2013-08-08 22:59:32 -04:00
|
|
|
indices = getattr (self, c.ClassDef).subset (self.Coverage.glyphs, remap=False)
|
2013-07-23 23:00:39 -04:00
|
|
|
rss = getattr (self, c.RuleSet)
|
2013-07-23 22:41:11 -04:00
|
|
|
rss = [rss[i] for i in indices]
|
|
|
|
ContextData = c.ContextData (self)
|
2013-07-31 14:11:40 -04:00
|
|
|
klass_maps = [x.subset (s.glyphs, remap=True) for x in ContextData]
|
2013-07-23 22:41:11 -04:00
|
|
|
for rs in rss:
|
2013-08-12 15:41:30 -04:00
|
|
|
if not rs: continue
|
|
|
|
ss = getattr (rs, c.Rule)
|
|
|
|
ss = [r for r in ss \
|
|
|
|
if r and all (all (k in klass_map for k in klist) \
|
|
|
|
for klass_map,klist in zip (klass_maps, c.RuleData (r)))]
|
|
|
|
setattr (rs, c.Rule, ss)
|
|
|
|
setattr (rs, c.RuleCount, len (ss))
|
|
|
|
|
|
|
|
# Remap rule classes
|
|
|
|
for r in ss:
|
|
|
|
c.SetRuleData (r, [[klass_map.index (k) for k in klist] \
|
|
|
|
for klass_map,klist in zip (klass_maps, c.RuleData (r))])
|
2013-07-23 22:41:11 -04:00
|
|
|
# Prune empty subrulesets
|
2013-07-23 23:00:39 -04:00
|
|
|
rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
|
|
|
|
setattr (self, c.RuleSet, rss)
|
|
|
|
setattr (self, c.RuleSetCount, len (rss))
|
2013-07-23 22:41:11 -04:00
|
|
|
return bool (rss)
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-31 14:11:40 -04:00
|
|
|
return all (x.subset (s.glyphs) for x in c.RuleData (self))
|
2013-07-21 18:40:59 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
2013-07-21 18:16:55 -04:00
|
|
|
|
2013-07-23 16:00:32 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
|
2013-07-23 10:23:42 -04:00
|
|
|
def subset_lookups (self, lookup_indices):
|
2013-07-23 16:18:30 -04:00
|
|
|
c = self.__classify_context ()
|
2013-07-23 10:23:42 -04:00
|
|
|
|
2013-07-23 23:04:43 -04:00
|
|
|
if self.Format in [1, 2]:
|
2013-07-23 23:00:39 -04:00
|
|
|
for rs in getattr (self, c.RuleSet):
|
2013-08-12 15:41:30 -04:00
|
|
|
if not rs: continue
|
|
|
|
for r in getattr (rs, c.Rule):
|
|
|
|
if not r: continue
|
|
|
|
setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
|
|
|
|
if ll.LookupListIndex in lookup_indices])
|
|
|
|
for ll in getattr (r, c.LookupRecord):
|
|
|
|
if not ll: continue
|
|
|
|
ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
|
2013-07-23 15:39:20 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-23 21:33:13 -04:00
|
|
|
setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
|
2013-07-23 16:18:30 -04:00
|
|
|
if ll.LookupListIndex in lookup_indices])
|
|
|
|
for ll in getattr (self, c.LookupRecord):
|
2013-08-12 15:41:30 -04:00
|
|
|
if not ll: continue
|
|
|
|
ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
|
2013-07-23 15:39:20 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 16:00:32 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
|
|
|
|
fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
|
2013-07-23 15:39:20 -04:00
|
|
|
def collect_lookups (self):
|
2013-07-23 16:18:30 -04:00
|
|
|
c = self.__classify_context ()
|
2013-07-23 15:39:20 -04:00
|
|
|
|
2013-07-23 23:04:43 -04:00
|
|
|
if self.Format in [1, 2]:
|
2013-07-23 16:40:47 -04:00
|
|
|
return [ll.LookupListIndex \
|
2013-07-23 23:00:39 -04:00
|
|
|
for rs in getattr (self, c.RuleSet) if rs \
|
|
|
|
for r in getattr (rs, c.Rule) if r \
|
2013-07-23 21:33:13 -04:00
|
|
|
for ll in getattr (r, c.LookupRecord) if ll]
|
2013-07-23 15:29:40 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-23 16:18:30 -04:00
|
|
|
return [ll.LookupListIndex \
|
2013-07-23 21:33:13 -04:00
|
|
|
for ll in getattr (self, c.LookupRecord) if ll]
|
2013-07-23 15:29:40 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
2013-07-23 10:23:42 -04:00
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
2013-07-23 14:52:18 -04:00
|
|
|
if self.Format == 1:
|
2013-08-08 22:59:32 -04:00
|
|
|
return self.ExtSubTable.closure_glyphs (s, cur_glyphs)
|
2013-07-23 14:52:18 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-31 14:11:40 -04:00
|
|
|
return self.ExtSubTable.subset_glyphs (s)
|
2013-07-21 18:16:55 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
if self.Format == 1:
|
2013-07-23 14:52:18 -04:00
|
|
|
return self.ExtSubTable.subset_lookups (lookup_indices)
|
2013-07-23 10:23:42 -04:00
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
|
|
|
|
def collect_lookups (self):
|
|
|
|
if self.Format == 1:
|
|
|
|
return self.ExtSubTable.collect_lookups ()
|
|
|
|
else:
|
|
|
|
assert 0, "unknown format: %s" % self.Format
|
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Lookup)
|
2013-08-08 22:59:32 -04:00
|
|
|
def closure_glyphs (self, s, cur_glyphs=None):
|
|
|
|
return sum ((st.closure_glyphs (s, cur_glyphs) for st in self.SubTable if st), [])
|
2013-07-23 14:52:18 -04:00
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Lookup)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
|
|
|
self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs (s)]
|
2013-07-21 23:15:32 -04:00
|
|
|
self.SubTableCount = len (self.SubTable)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.SubTableCount)
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Lookup)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
for s in self.SubTable:
|
|
|
|
s.subset_lookups (lookup_indices)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Lookup)
|
|
|
|
def collect_lookups (self):
|
2013-07-31 14:11:40 -04:00
|
|
|
return unique_sorted (sum ((st.collect_lookups () for st in self.SubTable if st), []))
|
2013-07-23 10:23:42 -04:00
|
|
|
|
2013-07-21 23:15:32 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LookupList)
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-22 11:46:50 -04:00
|
|
|
"Returns the indices of nonempty lookups."
|
2013-07-31 14:11:40 -04:00
|
|
|
return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (s)]
|
2013-07-22 11:46:50 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LookupList)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
2013-07-24 18:57:06 -04:00
|
|
|
self.Lookup = [self.Lookup[i] for i in lookup_indices if i < self.LookupCount]
|
2013-07-22 11:46:50 -04:00
|
|
|
self.LookupCount = len (self.Lookup)
|
2013-07-23 10:23:42 -04:00
|
|
|
for l in self.Lookup:
|
|
|
|
l.subset_lookups (lookup_indices)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LookupList)
|
|
|
|
def closure_lookups (self, lookup_indices):
|
2013-07-23 13:48:35 -04:00
|
|
|
lookup_indices = unique_sorted (lookup_indices)
|
|
|
|
recurse = lookup_indices
|
2013-07-23 10:23:42 -04:00
|
|
|
while True:
|
2013-07-24 18:57:06 -04:00
|
|
|
recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse if i < self.LookupCount), [])
|
|
|
|
recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices and l < self.LookupCount]
|
2013-07-23 10:23:42 -04:00
|
|
|
if not recurse_lookups:
|
2013-07-23 13:48:35 -04:00
|
|
|
return unique_sorted (lookup_indices)
|
|
|
|
recurse_lookups = unique_sorted (recurse_lookups)
|
|
|
|
lookup_indices.extend (recurse_lookups)
|
|
|
|
recurse = recurse_lookups
|
2013-07-22 11:46:50 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Feature)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
|
|
|
|
# Now map them.
|
|
|
|
self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
|
|
|
|
self.LookupCount = len (self.LookupListIndex)
|
2013-07-21 23:15:32 -04:00
|
|
|
return self.LookupCount
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Feature)
|
|
|
|
def collect_lookups (self):
|
|
|
|
return self.LookupListIndex[:]
|
|
|
|
|
2013-07-22 11:46:50 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.FeatureList)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
"Returns the indices of nonempty features."
|
|
|
|
feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
|
2013-07-23 12:10:46 -04:00
|
|
|
self.subset_features (feature_indices)
|
2013-07-22 11:46:50 -04:00
|
|
|
return feature_indices
|
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.FeatureList)
|
|
|
|
def collect_lookups (self, feature_indices):
|
|
|
|
return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
|
|
|
|
if i < self.FeatureCount), []))
|
|
|
|
|
2013-07-23 12:10:46 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.FeatureList)
|
|
|
|
def subset_features (self, feature_indices):
|
|
|
|
self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
|
|
|
|
self.FeatureCount = len (self.FeatureRecord)
|
|
|
|
return bool (self.FeatureCount)
|
|
|
|
|
2013-07-22 11:46:50 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
|
|
|
|
def subset_features (self, feature_indices):
|
2013-07-22 18:00:31 -04:00
|
|
|
if self.ReqFeatureIndex in feature_indices:
|
|
|
|
self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
|
|
|
|
else:
|
2013-07-22 11:46:50 -04:00
|
|
|
self.ReqFeatureIndex = 65535
|
|
|
|
self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
|
2013-07-22 18:00:31 -04:00
|
|
|
# Now map them.
|
|
|
|
self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
|
2013-07-22 11:46:50 -04:00
|
|
|
self.FeatureCount = len (self.FeatureIndex)
|
2013-07-23 12:10:46 -04:00
|
|
|
return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
|
2013-07-23 10:23:42 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
|
|
|
|
def collect_features (self):
|
|
|
|
feature_indices = self.FeatureIndex[:]
|
|
|
|
if self.ReqFeatureIndex != 65535:
|
|
|
|
feature_indices.append (self.ReqFeatureIndex)
|
|
|
|
return unique_sorted (feature_indices)
|
2013-07-22 11:46:50 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Script)
|
|
|
|
def subset_features (self, feature_indices):
|
|
|
|
if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
|
|
|
|
self.DefaultLangSys = None
|
|
|
|
self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
|
|
|
|
self.LangSysCount = len (self.LangSysRecord)
|
2013-07-23 12:10:46 -04:00
|
|
|
return bool (self.LangSysCount or self.DefaultLangSys)
|
2013-07-23 10:23:42 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Script)
|
|
|
|
def collect_features (self):
|
2013-07-23 11:18:13 -04:00
|
|
|
feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
|
2013-07-23 10:23:42 -04:00
|
|
|
if self.DefaultLangSys:
|
|
|
|
feature_indices.append (self.DefaultLangSys.collect_features ())
|
|
|
|
return unique_sorted (sum (feature_indices, []))
|
2013-07-22 11:46:50 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ScriptList)
|
|
|
|
def subset_features (self, feature_indices):
|
|
|
|
self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
|
|
|
|
self.ScriptCount = len (self.ScriptRecord)
|
2013-07-23 12:10:46 -04:00
|
|
|
return bool (self.ScriptCount)
|
2013-07-22 11:46:50 -04:00
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ScriptList)
|
|
|
|
def collect_features (self):
|
|
|
|
return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
|
|
|
|
|
2013-07-23 14:52:18 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'))
|
2013-07-31 14:20:13 -04:00
|
|
|
def closure_glyphs (self, s):
|
|
|
|
s.table = self.table
|
2013-07-23 14:52:18 -04:00
|
|
|
feature_indices = self.table.ScriptList.collect_features ()
|
|
|
|
lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
|
2013-07-31 14:20:13 -04:00
|
|
|
orig_glyphs = s.glyphs
|
|
|
|
glyphs = unique_sorted (s.glyphs)
|
2013-07-23 14:52:18 -04:00
|
|
|
while True:
|
2013-07-31 14:20:13 -04:00
|
|
|
s.glyphs = glyphs
|
|
|
|
additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (s) \
|
2013-07-24 18:57:06 -04:00
|
|
|
for i in lookup_indices if i < self.table.LookupList.LookupCount), []))
|
2013-07-23 14:52:18 -04:00
|
|
|
additions = unique_sorted (g for g in additions if g not in glyphs)
|
|
|
|
if not additions:
|
2013-07-31 14:20:13 -04:00
|
|
|
s.glyphs = orig_glyphs
|
2013-07-23 14:52:18 -04:00
|
|
|
return glyphs
|
|
|
|
glyphs.extend (additions)
|
2013-07-31 14:20:13 -04:00
|
|
|
del s.table
|
2013-07-23 14:52:18 -04:00
|
|
|
|
2013-07-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-31 19:53:57 -04:00
|
|
|
s.glyphs = s.glyphs_gsubed
|
2013-07-31 14:11:40 -04:00
|
|
|
lookup_indices = self.table.LookupList.subset_glyphs (s)
|
2013-07-22 11:46:50 -04:00
|
|
|
self.subset_lookups (lookup_indices)
|
2013-07-23 10:23:42 -04:00
|
|
|
self.prune_lookups ()
|
|
|
|
return True
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
2013-07-22 11:46:50 -04:00
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
"Retrains specified lookups, then removes empty features, language systems, and scripts."
|
2013-07-22 12:48:17 -04:00
|
|
|
self.table.LookupList.subset_lookups (lookup_indices)
|
|
|
|
feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
|
|
|
|
self.table.ScriptList.subset_features (feature_indices)
|
2013-07-22 11:46:50 -04:00
|
|
|
|
2013-07-23 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
|
|
|
def prune_lookups (self):
|
|
|
|
"Remove unreferenced lookups"
|
|
|
|
feature_indices = self.table.ScriptList.collect_features ()
|
|
|
|
lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
|
|
|
|
lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
|
|
|
|
self.subset_lookups (lookup_indices)
|
|
|
|
|
2013-07-23 12:10:46 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
|
|
|
def subset_feature_tags (self, feature_tags):
|
|
|
|
feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
|
|
|
|
self.table.FeatureList.subset_features (feature_indices)
|
|
|
|
self.table.ScriptList.subset_features (feature_indices)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
2013-07-23 12:46:52 -04:00
|
|
|
def prune_pre_subset (self, options):
|
2013-07-31 15:59:21 -04:00
|
|
|
if options.layout_features and '*' not in options.layout_features:
|
|
|
|
self.subset_feature_tags (options.layout_features)
|
2013-07-23 12:10:46 -04:00
|
|
|
self.prune_lookups ()
|
|
|
|
return True
|
|
|
|
|
2013-07-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GDEF'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-31 19:53:57 -04:00
|
|
|
glyphs = s.glyphs_gsubed
|
2013-07-22 12:48:17 -04:00
|
|
|
table = self.table
|
|
|
|
if table.LigCaretList:
|
2013-07-31 19:53:57 -04:00
|
|
|
indices = table.LigCaretList.Coverage.subset (glyphs)
|
2013-07-22 12:48:17 -04:00
|
|
|
table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
|
|
|
|
table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
|
|
|
|
if not table.LigCaretList.LigGlyphCount:
|
|
|
|
table.LigCaretList = None
|
|
|
|
if table.MarkAttachClassDef:
|
2013-07-31 19:53:57 -04:00
|
|
|
table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
|
2013-07-22 12:48:17 -04:00
|
|
|
if not table.MarkAttachClassDef.classDefs:
|
|
|
|
table.MarkAttachClassDef = None
|
|
|
|
if table.GlyphClassDef:
|
2013-07-31 19:53:57 -04:00
|
|
|
table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
|
2013-07-22 12:48:17 -04:00
|
|
|
if not table.GlyphClassDef.classDefs:
|
|
|
|
table.GlyphClassDef = None
|
|
|
|
if table.AttachList:
|
2013-07-31 19:53:57 -04:00
|
|
|
indices = table.AttachList.Coverage.subset (glyphs)
|
2013-07-22 12:48:17 -04:00
|
|
|
table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
|
|
|
|
table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
|
|
|
|
if not table.AttachList.GlyphCount:
|
|
|
|
table.AttachList = None
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
|
2013-07-22 12:48:17 -04:00
|
|
|
|
2013-07-24 18:51:05 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('kern'))
|
|
|
|
def prune_pre_subset (self, options):
|
|
|
|
# Prune unknown kern table types
|
|
|
|
self.kernTables = [t for t in self.kernTables if hasattr (t, 'kernTable')]
|
|
|
|
return bool (self.kernTables)
|
|
|
|
|
2013-07-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('kern'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-31 19:53:57 -04:00
|
|
|
glyphs = s.glyphs_gsubed
|
2013-07-22 12:57:02 -04:00
|
|
|
for t in self.kernTables:
|
2013-07-31 19:53:57 -04:00
|
|
|
t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
|
2013-07-22 12:57:02 -04:00
|
|
|
self.kernTables = [t for t in self.kernTables if t.kernTable]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.kernTables)
|
2013-07-21 22:26:16 -04:00
|
|
|
|
2013-07-22 14:49:54 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
|
|
|
self.metrics = {g:v for g,v in self.metrics.items() if g in s.glyphs}
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.metrics)
|
2013-07-22 14:29:08 -04:00
|
|
|
|
2013-07-22 14:49:54 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('hdmx'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-31 15:59:21 -04:00
|
|
|
self.hdmx = {sz:{g:v for g,v in l.items() if g in s.glyphs} for (sz,l) in self.hdmx.items()}
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.hdmx)
|
2013-07-22 14:49:54 -04:00
|
|
|
|
2013-07-22 15:29:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('VORG'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
|
|
|
self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in s.glyphs}
|
2013-07-22 15:29:17 -04:00
|
|
|
self.numVertOriginYMetrics = len (self.VOriginRecords)
|
|
|
|
return True # Never drop; has default metrics
|
|
|
|
|
2013-07-23 12:56:06 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('post'))
|
2013-07-24 13:34:47 -04:00
|
|
|
def prune_pre_subset (self, options):
|
2013-07-31 15:59:21 -04:00
|
|
|
if not options.glyph_names:
|
2013-07-23 12:56:06 -04:00
|
|
|
self.formatType = 3.0
|
|
|
|
return True
|
|
|
|
|
2013-07-22 15:06:23 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('post'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-23 12:56:06 -04:00
|
|
|
self.extraNames = [] # This seems to do it
|
2013-07-23 10:28:47 -04:00
|
|
|
return True
|
2013-07-22 15:17:12 -04:00
|
|
|
|
2013-07-24 16:08:35 -04:00
|
|
|
# Copied from _g_l_y_f.py
|
|
|
|
ARG_1_AND_2_ARE_WORDS = 0x0001 # if set args are words otherwise they are bytes
|
|
|
|
ARGS_ARE_XY_VALUES = 0x0002 # if set args are xy values, otherwise they are points
|
|
|
|
ROUND_XY_TO_GRID = 0x0004 # for the xy values if above is true
|
|
|
|
WE_HAVE_A_SCALE = 0x0008 # Sx = Sy, otherwise scale == 1.0
|
|
|
|
NON_OVERLAPPING = 0x0010 # set to same value for all components (obsolete!)
|
|
|
|
MORE_COMPONENTS = 0x0020 # indicates at least one more glyph after this one
|
|
|
|
WE_HAVE_AN_X_AND_Y_SCALE = 0x0040 # Sx, Sy
|
|
|
|
WE_HAVE_A_TWO_BY_TWO = 0x0080 # t00, t01, t10, t11
|
|
|
|
WE_HAVE_INSTRUCTIONS = 0x0100 # instructions follow
|
|
|
|
USE_MY_METRICS = 0x0200 # apply these metrics to parent glyph
|
|
|
|
OVERLAP_COMPOUND = 0x0400 # used by Apple in GX fonts
|
|
|
|
SCALED_COMPONENT_OFFSET = 0x0800 # composite designed to have the component offset scaled (designed for Apple)
|
|
|
|
UNSCALED_COMPONENT_OFFSET = 0x1000 # composite designed not to have the component offset scaled (designed for MS)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
|
|
|
|
def getComponentNamesFast (self, glyfTable):
|
|
|
|
if struct.unpack(">h", self.data[:2])[0] >= 0:
|
|
|
|
return [] # Not composite
|
|
|
|
data = self.data
|
|
|
|
i = 10
|
|
|
|
components = []
|
|
|
|
more = 1
|
|
|
|
while more:
|
|
|
|
flags, glyphID = struct.unpack(">HH", data[i:i+4])
|
|
|
|
i += 4
|
|
|
|
flags = int(flags)
|
|
|
|
components.append (glyfTable.getGlyphName (int (glyphID)))
|
|
|
|
|
|
|
|
if flags & ARG_1_AND_2_ARE_WORDS: i += 4
|
|
|
|
else: i += 2
|
|
|
|
if flags & WE_HAVE_A_SCALE: i += 2
|
|
|
|
elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
|
|
|
|
elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
|
|
|
|
more = flags & MORE_COMPONENTS
|
|
|
|
return components
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
|
|
|
|
def remapComponentsFast (self, indices):
|
|
|
|
if struct.unpack(">h", self.data[:2])[0] >= 0:
|
|
|
|
return # Not composite
|
|
|
|
data = bytearray (self.data)
|
|
|
|
i = 10
|
|
|
|
more = 1
|
|
|
|
while more:
|
|
|
|
flags = (data[i] << 8) | data[i+1]
|
|
|
|
glyphID = (data[i+2] << 8) | data[i+3]
|
|
|
|
# Remap
|
|
|
|
glyphID = indices.index (glyphID)
|
|
|
|
data[i+2] = glyphID >> 8
|
|
|
|
data[i+3] = glyphID & 0xFF
|
|
|
|
i += 4
|
|
|
|
flags = int(flags)
|
|
|
|
|
|
|
|
if flags & ARG_1_AND_2_ARE_WORDS: i += 4
|
|
|
|
else: i += 2
|
|
|
|
if flags & WE_HAVE_A_SCALE: i += 2
|
|
|
|
elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
|
|
|
|
elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
|
|
|
|
more = flags & MORE_COMPONENTS
|
|
|
|
self.data = str (data)
|
|
|
|
|
2013-07-24 16:52:47 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
|
|
|
|
def dropInstructionsFast (self):
|
|
|
|
numContours = struct.unpack(">h", self.data[:2])[0]
|
|
|
|
data = bytearray (self.data)
|
|
|
|
i = 10
|
|
|
|
if numContours >= 0:
|
|
|
|
i += 2 * numContours # endPtsOfContours
|
|
|
|
instructionLen = (data[i] << 8) | data[i+1]
|
|
|
|
# Zero it
|
|
|
|
data[i] = data [i+1] = 0
|
|
|
|
i += 2
|
2013-07-24 17:25:35 -04:00
|
|
|
if instructionLen:
|
|
|
|
# Splice it out
|
|
|
|
data = data[:i] + data[i+instructionLen:]
|
2013-07-24 16:52:47 -04:00
|
|
|
else:
|
|
|
|
more = 1
|
|
|
|
while more:
|
|
|
|
flags = (data[i] << 8) | data[i+1]
|
|
|
|
# Turn instruction flag off
|
|
|
|
flags &= ~WE_HAVE_INSTRUCTIONS
|
|
|
|
data[i+0] = flags >> 8
|
|
|
|
data[i+1] = flags & 0xFF
|
|
|
|
i += 4
|
|
|
|
flags = int(flags)
|
|
|
|
|
|
|
|
if flags & ARG_1_AND_2_ARE_WORDS: i += 4
|
|
|
|
else: i += 2
|
|
|
|
if flags & WE_HAVE_A_SCALE: i += 2
|
|
|
|
elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
|
|
|
|
elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
|
|
|
|
more = flags & MORE_COMPONENTS
|
|
|
|
# Cut off
|
|
|
|
data = data[:i]
|
|
|
|
if len(data) % 4:
|
|
|
|
# add pad bytes
|
|
|
|
nPadBytes = 4 - (len(data) % 4)
|
|
|
|
for i in range (nPadBytes):
|
|
|
|
data.append (0)
|
|
|
|
self.data = str (data)
|
|
|
|
|
2013-07-23 12:58:37 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('glyf'))
|
2013-07-31 14:20:13 -04:00
|
|
|
def closure_glyphs (self, s):
|
|
|
|
glyphs = unique_sorted (s.glyphs)
|
2013-07-23 12:58:37 -04:00
|
|
|
decompose = glyphs
|
|
|
|
# I don't know if component glyphs can be composite themselves.
|
|
|
|
# We handle them anyway.
|
|
|
|
while True:
|
|
|
|
components = []
|
|
|
|
for g in decompose:
|
2013-07-24 16:08:35 -04:00
|
|
|
if g not in self.glyphs:
|
2013-07-23 23:13:23 -04:00
|
|
|
continue
|
2013-07-24 16:08:35 -04:00
|
|
|
gl = self.glyphs[g]
|
|
|
|
if hasattr (gl, "data"):
|
|
|
|
for c in gl.getComponentNamesFast (self):
|
|
|
|
if c not in glyphs:
|
|
|
|
components.append (c)
|
|
|
|
else:
|
|
|
|
# TTX seems to expand gid0..3 always
|
|
|
|
if gl.isComposite ():
|
|
|
|
for c in gl.components:
|
|
|
|
if c.glyphName not in glyphs:
|
|
|
|
components.append (c.glyphName)
|
2013-07-23 12:58:37 -04:00
|
|
|
components = [c for c in components if c not in glyphs]
|
|
|
|
if not components:
|
|
|
|
return glyphs
|
|
|
|
decompose = unique_sorted (components)
|
|
|
|
glyphs.extend (components)
|
|
|
|
|
2013-07-22 16:47:24 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('glyf'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
|
|
|
self.glyphs = {g:v for g,v in self.glyphs.items() if g in s.glyphs}
|
|
|
|
indices = [i for i,g in enumerate (self.glyphOrder) if g in s.glyphs]
|
2013-07-24 16:08:35 -04:00
|
|
|
for v in self.glyphs.values ():
|
|
|
|
if hasattr (v, "data"):
|
|
|
|
v.remapComponentsFast (indices)
|
|
|
|
else:
|
|
|
|
pass # No need
|
2013-07-31 14:11:40 -04:00
|
|
|
self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.glyphs)
|
2013-07-22 16:47:24 -04:00
|
|
|
|
2013-07-23 12:37:41 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('glyf'))
|
2013-07-23 12:46:52 -04:00
|
|
|
def prune_post_subset (self, options):
|
2013-07-31 15:59:21 -04:00
|
|
|
if not options.hinting:
|
2013-07-24 16:52:47 -04:00
|
|
|
for v in self.glyphs.values ():
|
|
|
|
if hasattr (v, "data"):
|
|
|
|
v.dropInstructionsFast ()
|
|
|
|
else:
|
|
|
|
v.program = fontTools.ttLib.tables.ttProgram.Program()
|
|
|
|
v.program.fromBytecode([])
|
2013-07-23 12:37:41 -04:00
|
|
|
return True
|
|
|
|
|
2013-07-23 13:37:13 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('CFF '))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-23 13:37:13 -04:00
|
|
|
assert 0, "unimplemented"
|
|
|
|
|
2013-07-31 19:47:37 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('cmap'))
|
|
|
|
def closure_glyphs (self, s):
|
|
|
|
tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [1, 10]]
|
|
|
|
extra = []
|
2013-07-31 20:16:24 -04:00
|
|
|
for u in s.unicodes_requested:
|
2013-07-31 19:47:37 -04:00
|
|
|
found = False
|
|
|
|
for table in tables:
|
|
|
|
if u in table.cmap:
|
|
|
|
extra.append (table.cmap[u])
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
s.log ("No glyph for Unicode value %s; skipping." % u)
|
|
|
|
return extra
|
|
|
|
|
2013-07-23 12:58:37 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('cmap'))
|
|
|
|
def prune_pre_subset (self, options):
|
2013-07-31 15:59:21 -04:00
|
|
|
if not options.legacy_cmap:
|
2013-07-23 13:05:42 -04:00
|
|
|
# Drop non-Unicode / non-Symbol cmaps
|
|
|
|
self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
|
2013-07-31 15:59:21 -04:00
|
|
|
if not options.symbol_cmap:
|
2013-07-23 13:05:42 -04:00
|
|
|
self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [1, 10]]
|
2013-07-23 12:58:37 -04:00
|
|
|
# TODO Only keep one subtable?
|
|
|
|
# For now, drop format=0 which can't be subset_glyphs easily?
|
|
|
|
self.tables = [t for t in self.tables if t.format != 0]
|
|
|
|
return bool (self.tables)
|
|
|
|
|
2013-07-22 15:17:12 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('cmap'))
|
2013-07-31 14:11:40 -04:00
|
|
|
def subset_glyphs (self, s):
|
2013-07-31 19:53:57 -04:00
|
|
|
s.glyphs = s.glyphs_cmaped
|
2013-07-22 15:17:12 -04:00
|
|
|
for t in self.tables:
|
2013-07-22 16:21:24 -04:00
|
|
|
# For reasons I don't understand I need this here
|
|
|
|
# to force decompilation of the cmap format 14.
|
|
|
|
try:
|
|
|
|
getattr (t, "asdf")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2013-07-22 16:01:15 -04:00
|
|
|
if t.format == 14:
|
2013-07-22 16:21:24 -04:00
|
|
|
# XXX We drop all the default-UVS mappings (g==None)
|
2013-07-31 14:11:40 -04:00
|
|
|
t.uvsDict = {v:[(u,g) for (u,g) in l if g in s.glyphs] for (v,l) in t.uvsDict.items()}
|
2013-07-22 16:01:15 -04:00
|
|
|
t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
|
|
|
|
else:
|
2013-07-31 19:56:19 -04:00
|
|
|
t.cmap = {u:g for (u,g) in t.cmap.items() if g in s.glyphs_requested or u in s.unicodes_requested}
|
2013-07-22 16:01:15 -04:00
|
|
|
self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)]
|
2013-07-22 18:47:32 -04:00
|
|
|
# XXX Convert formats when needed
|
2013-08-08 23:49:00 -04:00
|
|
|
# In particular, if we have a format=12 without non-BMP
|
|
|
|
# characters, either drop format=12 one or convert it
|
|
|
|
# to format=4 if there's not one.
|
2013-07-23 11:03:49 -04:00
|
|
|
return bool (self.tables)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableClass('name'))
|
2013-07-23 12:46:52 -04:00
|
|
|
def prune_pre_subset (self, options):
|
2013-07-31 15:59:21 -04:00
|
|
|
if '*' not in options.name_IDs:
|
|
|
|
self.names = [n for n in self.names if n.nameID in options.name_IDs]
|
|
|
|
if not options.name_legacy:
|
2013-07-23 13:19:03 -04:00
|
|
|
self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
|
2013-07-31 15:59:21 -04:00
|
|
|
if '*' not in options.name_languages:
|
|
|
|
self.names = [n for n in self.names if n.langID in options.name_languages]
|
2013-07-23 13:19:03 -04:00
|
|
|
return True # Retain even if empty
|
2013-07-22 15:17:12 -04:00
|
|
|
|
2013-07-22 15:06:23 -04:00
|
|
|
|
2013-07-23 12:56:54 -04:00
|
|
|
drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
|
2013-07-23 15:08:39 -04:00
|
|
|
drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
|
2013-07-23 16:56:50 -04:00
|
|
|
drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
|
2013-07-23 12:37:41 -04:00
|
|
|
no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
|
2013-07-23 12:56:54 -04:00
|
|
|
hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
|
2013-07-23 11:05:25 -04:00
|
|
|
|
2013-07-23 12:10:46 -04:00
|
|
|
# Based on HarfBuzz shapers
|
|
|
|
layout_features_dict = {
|
|
|
|
# Default shaper
|
|
|
|
'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
|
|
|
|
'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
|
|
|
|
'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
|
|
|
|
'ltr': ['ltra', 'ltrm'],
|
|
|
|
'rtl': ['rtla', 'rtlm'],
|
|
|
|
# Complex shapers
|
2013-08-04 16:54:46 -04:00
|
|
|
'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3', 'cswh', 'mset'],
|
2013-07-23 12:10:46 -04:00
|
|
|
'hangul': ['ljmo', 'vjmo', 'tjmo'],
|
|
|
|
'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
|
|
|
|
'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
|
|
|
|
'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
|
|
|
|
}
|
|
|
|
layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
|
|
|
|
|
2013-07-22 14:49:54 -04:00
|
|
|
# TODO OS/2 ulUnicodeRange / ulCodePageRange?
|
2013-07-23 12:59:13 -04:00
|
|
|
# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
|
2013-07-23 15:29:40 -04:00
|
|
|
# TODO Avoid recursing too much
|
2013-07-23 13:22:04 -04:00
|
|
|
# TODO Text direction considerations
|
|
|
|
# TODO Text script / language considerations
|
2013-07-24 19:21:40 -04:00
|
|
|
# TODO Drop unknown tables? Using DefaultTable.prune?
|
2013-07-24 17:58:29 -04:00
|
|
|
# TODO Drop GPOS Device records if not hinting?
|
2013-08-09 14:22:48 -04:00
|
|
|
# TODO Hookup options.verbose to font.verbose?
|
|
|
|
# TODO Move font name loading hack to Subsetter?
|
2013-07-22 13:02:24 -04:00
|
|
|
|
2013-07-24 13:34:47 -04:00
|
|
|
|
2013-07-31 14:11:40 -04:00
|
|
|
class Subsetter:
|
2013-07-31 15:59:21 -04:00
|
|
|
|
|
|
|
class Options:
|
|
|
|
drop_tables = drop_tables_default
|
|
|
|
layout_features = layout_features_all
|
|
|
|
hinting = False
|
|
|
|
glyph_names = False
|
|
|
|
legacy_cmap = False
|
|
|
|
symbol_cmap = False
|
|
|
|
name_IDs = [1, 2] # Family and Style
|
|
|
|
name_legacy = False
|
|
|
|
name_languages = [0x0409] # English
|
|
|
|
mandatory_glyphs = True # First four for TrueType, .notdef for CFF
|
|
|
|
recalc_bboxes = False # Slows us down
|
|
|
|
|
|
|
|
def __init__ (self, **kwargs):
|
|
|
|
|
|
|
|
self.set (**kwargs)
|
|
|
|
|
|
|
|
def set (self, **kwargs):
|
|
|
|
for k,v in kwargs.items ():
|
|
|
|
if not hasattr (self, k):
|
|
|
|
raise Exception ("Unknown option '%s'" % k)
|
|
|
|
setattr (self, k, v)
|
|
|
|
|
|
|
|
def parse_opts (self, argv, ignore_unknown=False):
|
|
|
|
ret = []
|
|
|
|
opts = {}
|
|
|
|
for a in argv:
|
|
|
|
if not a.startswith ('--'):
|
|
|
|
ret.append (a)
|
|
|
|
continue
|
|
|
|
a = a[2:]
|
|
|
|
i = a.find ('=')
|
|
|
|
if i == -1:
|
|
|
|
if a.startswith ("no-"):
|
|
|
|
k = a[3:]
|
|
|
|
v = False
|
|
|
|
else:
|
|
|
|
k = a
|
|
|
|
v = True
|
|
|
|
else:
|
|
|
|
k = a[:i]
|
|
|
|
v = a[i+1:]
|
|
|
|
k = k.replace ('-', '_')
|
|
|
|
if not hasattr (self, k):
|
|
|
|
if ignore_unknown:
|
|
|
|
ret.append (a)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise Exception ("Unknown option '%s'" % a)
|
|
|
|
|
|
|
|
ov = getattr (self, k)
|
|
|
|
if isinstance (ov, bool):
|
|
|
|
v = bool (v)
|
|
|
|
elif isinstance (ov, int):
|
|
|
|
v = int (v)
|
|
|
|
elif isinstance (ov, list):
|
|
|
|
v = v.split (',')
|
|
|
|
v = [int (x, 0) if x[0] in range (10) else x for x in v]
|
|
|
|
|
|
|
|
opts[k] = v
|
|
|
|
self.set (**opts)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
def __init__ (self, font=None, options=None, log=None):
|
2013-07-31 15:59:21 -04:00
|
|
|
|
|
|
|
if isinstance (font, basestring):
|
|
|
|
font = fontTools.ttx.TTFont (font)
|
|
|
|
if not log:
|
|
|
|
log = Logger()
|
|
|
|
if not options:
|
|
|
|
options = Options()
|
|
|
|
|
2013-07-31 14:45:13 -04:00
|
|
|
self.font = font
|
|
|
|
self.options = options
|
2013-07-31 15:03:43 -04:00
|
|
|
self.log = log
|
2013-07-31 19:56:19 -04:00
|
|
|
self.unicodes_requested = set ()
|
|
|
|
self.glyphs_requested = set ()
|
2013-07-31 14:11:40 -04:00
|
|
|
|
2013-07-31 20:11:17 -04:00
|
|
|
def populate (self, glyphs=[], unicodes=[], text=""):
|
2013-07-31 19:56:19 -04:00
|
|
|
self.unicodes_requested.update (unicodes)
|
2013-07-31 20:16:24 -04:00
|
|
|
if isinstance (text, str):
|
2013-07-31 20:11:17 -04:00
|
|
|
text = text.decode ("utf8")
|
2013-07-31 19:47:37 -04:00
|
|
|
for u in text:
|
2013-07-31 20:11:17 -04:00
|
|
|
self.unicodes_requested.add (ord (u))
|
2013-07-31 19:56:19 -04:00
|
|
|
self.glyphs_requested.update (glyphs)
|
2013-07-31 14:11:40 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
def pre_prune (self):
|
2013-07-31 19:47:37 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
for tag in self.font.keys():
|
2013-07-31 15:22:02 -04:00
|
|
|
if tag == 'GlyphOrder': continue
|
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
if tag in self.options.drop_tables or \
|
|
|
|
(tag in hinting_tables and not self.options.hinting):
|
2013-07-31 15:22:02 -04:00
|
|
|
self.log (tag, "dropped")
|
2013-07-31 20:16:24 -04:00
|
|
|
del self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
clazz = fontTools.ttLib.getTableClass(tag)
|
|
|
|
|
|
|
|
if hasattr (clazz, 'prune_pre_subset'):
|
2013-07-31 20:16:24 -04:00
|
|
|
table = self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
retain = table.prune_pre_subset (self.options)
|
|
|
|
self.log.lapse ("prune '%s'" % tag)
|
|
|
|
if not retain:
|
|
|
|
self.log (tag, "pruned to empty; dropped")
|
2013-07-31 20:16:24 -04:00
|
|
|
del self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
self.log (tag, "pruned")
|
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
def closure_glyphs (self):
|
|
|
|
|
|
|
|
self.glyphs = self.glyphs_requested
|
2013-07-31 20:04:08 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
if 'cmap' in self.font:
|
|
|
|
extra_glyphs = self.font['cmap'].closure_glyphs (self)
|
2013-07-31 20:04:08 -04:00
|
|
|
self.glyph = self.glyphs.copy ()
|
|
|
|
self.glyphs.update (extra_glyphs)
|
|
|
|
self.glyphs_cmaped = self.glyphs
|
|
|
|
|
|
|
|
if self.options.mandatory_glyphs:
|
|
|
|
self.glyphs = self.glyphs.copy ()
|
2013-07-31 20:16:24 -04:00
|
|
|
if 'glyf' in self.font:
|
2013-07-31 20:04:08 -04:00
|
|
|
for i in range (4):
|
2013-07-31 20:16:24 -04:00
|
|
|
self.glyphs.add (self.font.getGlyphName (i))
|
2013-07-31 20:04:08 -04:00
|
|
|
self.log ("Added first four glyphs to subset")
|
|
|
|
else:
|
|
|
|
self.glyphs.add ('.notdef')
|
|
|
|
self.log ("Added .notdef glyph to subset")
|
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
if 'GSUB' in self.font:
|
2013-07-31 19:47:37 -04:00
|
|
|
self.log ("Closing glyph list over 'GSUB': %d glyphs before" % len (self.glyphs))
|
2013-08-08 21:57:02 -04:00
|
|
|
self.log.glyphs (self.glyphs, font=self.font)
|
2013-07-31 20:16:24 -04:00
|
|
|
self.glyphs = set (self.font['GSUB'].closure_glyphs (self))
|
2013-07-31 19:47:37 -04:00
|
|
|
self.log ("Closed glyph list over 'GSUB': %d glyphs after" % len (self.glyphs))
|
2013-08-08 21:57:02 -04:00
|
|
|
self.log.glyphs (self.glyphs, font=self.font)
|
2013-07-31 15:22:02 -04:00
|
|
|
self.log.lapse ("close glyph list over 'GSUB'")
|
2013-07-31 19:47:37 -04:00
|
|
|
self.glyphs_gsubed = self.glyphs
|
2013-07-31 15:22:02 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
if 'glyf' in self.font:
|
2013-07-31 19:47:37 -04:00
|
|
|
self.log ("Closing glyph list over 'glyf': %d glyphs before" % len (self.glyphs))
|
2013-08-08 21:57:02 -04:00
|
|
|
self.log.glyphs (self.glyphs, font=self.font)
|
2013-07-31 20:16:24 -04:00
|
|
|
self.glyphs = set (self.font['glyf'].closure_glyphs (self))
|
2013-07-31 19:47:37 -04:00
|
|
|
self.log ("Closed glyph list over 'glyf': %d glyphs after" % len (self.glyphs))
|
2013-08-08 21:57:02 -04:00
|
|
|
self.log.glyphs (self.glyphs, font=self.font)
|
2013-07-31 15:22:02 -04:00
|
|
|
self.log.lapse ("close glyph list over 'glyf'")
|
2013-07-31 19:47:37 -04:00
|
|
|
self.glyphs_glyfed = self.glyphs
|
|
|
|
|
|
|
|
self.glyphs_all = self.glyphs
|
2013-07-31 15:22:02 -04:00
|
|
|
|
2013-07-31 19:47:37 -04:00
|
|
|
self.log ("Retaining %d glyphs: " % len (self.glyphs_all))
|
2013-07-31 15:22:02 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
def subset_glyphs (self):
|
|
|
|
for tag in self.font.keys():
|
2013-07-31 15:22:02 -04:00
|
|
|
if tag == 'GlyphOrder': continue
|
|
|
|
clazz = fontTools.ttLib.getTableClass(tag)
|
|
|
|
|
|
|
|
if tag in no_subset_tables:
|
|
|
|
self.log (tag, "subsetting not needed")
|
|
|
|
elif hasattr (clazz, 'subset_glyphs'):
|
2013-07-31 20:16:24 -04:00
|
|
|
table = self.font[tag]
|
2013-07-31 19:53:57 -04:00
|
|
|
self.glyphs = self.glyphs_all
|
2013-07-31 15:22:02 -04:00
|
|
|
retain = table.subset_glyphs (self)
|
|
|
|
self.log.lapse ("subset '%s'" % tag)
|
|
|
|
if not retain:
|
|
|
|
self.log (tag, "subsetted to empty; dropped")
|
2013-07-31 20:16:24 -04:00
|
|
|
del self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
else:
|
|
|
|
self.log (tag, "subsetted")
|
|
|
|
else:
|
|
|
|
self.log (tag, "NOT subset; don't know how to subset")
|
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
glyphOrder = self.font.getGlyphOrder()
|
2013-07-31 20:04:08 -04:00
|
|
|
glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
|
2013-07-31 20:16:24 -04:00
|
|
|
self.font.setGlyphOrder (glyphOrder)
|
|
|
|
self.font._buildReverseGlyphOrderDict ()
|
2013-07-31 20:04:08 -04:00
|
|
|
self.log.lapse ("subset GlyphOrder")
|
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
def post_prune (self):
|
|
|
|
for tag in self.font.keys():
|
2013-07-31 20:04:08 -04:00
|
|
|
if tag == 'GlyphOrder': continue
|
|
|
|
clazz = fontTools.ttLib.getTableClass(tag)
|
2013-07-31 15:22:02 -04:00
|
|
|
if hasattr (clazz, 'prune_post_subset'):
|
2013-07-31 20:16:24 -04:00
|
|
|
table = self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
retain = table.prune_post_subset (self.options)
|
|
|
|
self.log.lapse ("prune '%s'" % tag)
|
|
|
|
if not retain:
|
|
|
|
self.log (tag, "pruned to empty; dropped")
|
2013-07-31 20:16:24 -04:00
|
|
|
del self.font[tag]
|
2013-07-31 15:22:02 -04:00
|
|
|
else:
|
|
|
|
self.log (tag, "pruned")
|
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
def subset (self, font):
|
|
|
|
|
|
|
|
self.font = font
|
2013-08-01 12:05:26 -04:00
|
|
|
|
2013-07-31 20:16:24 -04:00
|
|
|
self.font.recalcBBoxes = self.options.recalc_bboxes
|
|
|
|
|
|
|
|
self.pre_prune ()
|
|
|
|
self.closure_glyphs ()
|
|
|
|
self.subset_glyphs ()
|
|
|
|
self.post_prune ()
|
|
|
|
|
2013-08-01 12:05:26 -04:00
|
|
|
del self.font
|
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
import sys, time
|
2013-07-31 15:22:02 -04:00
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
class Logger:
|
|
|
|
|
|
|
|
def __init__ (self, verbose=False, xml=False, timing=False):
|
|
|
|
self.verbose = verbose
|
|
|
|
self.xml = xml
|
|
|
|
self.timing = timing
|
|
|
|
self.last_time = self.start_time = time.time ()
|
|
|
|
|
|
|
|
def parse_opts (self, argv):
|
|
|
|
argv = argv[:]
|
|
|
|
for v in ['verbose', 'xml', 'timing']:
|
|
|
|
if "--"+v in argv:
|
|
|
|
setattr (self, v, True)
|
|
|
|
argv.remove ("--"+v)
|
|
|
|
return argv
|
|
|
|
|
|
|
|
def __call__ (self, *things):
|
|
|
|
if not self.verbose:
|
|
|
|
return
|
|
|
|
print ' '.join (str (x) for x in things)
|
|
|
|
|
|
|
|
def lapse (self, *things):
|
|
|
|
if not self.timing:
|
|
|
|
return
|
|
|
|
new_time = time.time ()
|
|
|
|
print "Took %0.3fs to %s" % (new_time - self.last_time, ' '.join (str (x) for x in things))
|
|
|
|
self.last_time = new_time
|
|
|
|
|
2013-08-08 21:57:02 -04:00
|
|
|
def glyphs (self, glyphs, glyph_names=True, font=None):
|
|
|
|
self ("Names: ", sorted (glyphs))
|
|
|
|
if font:
|
|
|
|
glyphOrder = font.getGlyphOrder()
|
|
|
|
self ("Gids : ", sorted (glyphOrder.index (g) for g in glyphs))
|
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
def font (self, font, file=sys.stdout):
|
|
|
|
if not self.xml:
|
|
|
|
return
|
|
|
|
import xmlWriter, sys
|
|
|
|
writer = xmlWriter.XMLWriter (file)
|
|
|
|
font.disassembleInstructions = False # Work around ttx bug
|
|
|
|
for tag in font.keys():
|
|
|
|
writer.begintag (tag)
|
|
|
|
writer.newline ()
|
|
|
|
font[tag].toXML(writer, font)
|
|
|
|
writer.endtag (tag)
|
|
|
|
writer.newline ()
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
def main (args):
|
2013-07-23 14:52:18 -04:00
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
log = Logger ()
|
|
|
|
args = log.parse_opts (args)
|
2013-07-22 11:57:13 -04:00
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
options = Subsetter.Options ()
|
|
|
|
args = options.parse_opts (args)
|
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
if len (args) < 2:
|
2013-07-31 15:59:21 -04:00
|
|
|
import sys
|
2013-07-21 18:40:59 -04:00
|
|
|
print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
|
|
|
|
sys.exit (1)
|
|
|
|
|
2013-07-31 15:03:43 -04:00
|
|
|
fontfile = args[0]
|
2013-07-31 19:58:59 -04:00
|
|
|
args = args[1:]
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-24 19:21:40 -04:00
|
|
|
# TODO Option for ignoreDecompileErrors?
|
2013-07-31 14:45:13 -04:00
|
|
|
font = fontTools.ttx.TTFont (fontfile)
|
2013-07-31 15:22:02 -04:00
|
|
|
s = Subsetter (font=font, options=options, log=log)
|
2013-07-31 15:59:21 -04:00
|
|
|
log.lapse ("load font")
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-31 19:27:14 -04:00
|
|
|
# Hack:
|
|
|
|
#
|
|
|
|
# If we don't need glyph names, change 'post' class to not try to
|
|
|
|
# load them. It avoid lots of headache with broken fonts as well
|
|
|
|
# as loading time.
|
|
|
|
#
|
|
|
|
# Ideally ttLib should provide a way to ask it to skip loading
|
|
|
|
# glyph names. But it currently doesn't provide such a thing.
|
|
|
|
#
|
|
|
|
if not options.glyph_names \
|
2013-08-08 21:18:17 -04:00
|
|
|
and all (any (g.startswith (p) for p in ['gid', 'glyph', 'uni', 'U+']) \
|
2013-07-31 19:58:59 -04:00
|
|
|
for g in args):
|
2013-07-31 19:27:14 -04:00
|
|
|
post = fontTools.ttLib.getTableClass('post')
|
|
|
|
saved = post.decode_format_2_0
|
|
|
|
post.decode_format_2_0 = post.decode_format_3_0
|
|
|
|
f = font['post']
|
|
|
|
if f.formatType == 2.0:
|
|
|
|
f.formatType = 3.0
|
|
|
|
post.decode_format_2_0 = saved
|
|
|
|
del post, saved, f
|
|
|
|
|
|
|
|
names = font.getGlyphNames()
|
|
|
|
log.lapse ("loading glyph names")
|
2013-07-31 19:58:59 -04:00
|
|
|
|
|
|
|
glyphs = []
|
2013-07-31 19:47:37 -04:00
|
|
|
unicodes = []
|
2013-07-31 19:58:59 -04:00
|
|
|
for g in args:
|
2013-07-31 19:27:14 -04:00
|
|
|
if g in names:
|
2013-07-31 19:58:59 -04:00
|
|
|
glyphs.append (g)
|
2013-07-31 19:27:14 -04:00
|
|
|
continue
|
2013-08-08 21:18:17 -04:00
|
|
|
if g.startswith ('uni') or g.startswith ('U+'):
|
|
|
|
if g.startswith ('uni') and len (g) > 3:
|
|
|
|
g = g[3:]
|
|
|
|
elif g.startswith ('U+') and len (g) > 2:
|
|
|
|
g = g[2:]
|
|
|
|
u = int (g, 16)
|
2013-07-31 19:47:37 -04:00
|
|
|
unicodes.append (u)
|
2013-07-31 19:27:14 -04:00
|
|
|
continue
|
|
|
|
if g.startswith ('gid') or g.startswith ('glyph'):
|
|
|
|
if g.startswith ('gid') and len (g) > 3:
|
|
|
|
g = g[3:]
|
|
|
|
elif g.startswith ('glyph') and len (g) > 5:
|
|
|
|
g = g[5:]
|
|
|
|
try:
|
2013-07-31 19:58:59 -04:00
|
|
|
glyphs.append (font.getGlyphName (int (g), requireReal=1))
|
2013-07-31 19:27:14 -04:00
|
|
|
except ValueError:
|
|
|
|
raise Exception ("Invalid glyph identifier %s" % g)
|
|
|
|
continue
|
|
|
|
raise Exception ("Invalid glyph identifier %s" % g)
|
|
|
|
log.lapse ("compile glyph list")
|
2013-07-31 19:58:59 -04:00
|
|
|
log ("Unicodes:", unicodes)
|
2013-07-31 19:27:14 -04:00
|
|
|
log ("Glyphs:", glyphs)
|
|
|
|
|
2013-07-31 19:47:37 -04:00
|
|
|
s.populate (glyphs=glyphs, unicodes=unicodes)
|
|
|
|
s.subset (font)
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-07-24 12:40:54 -04:00
|
|
|
font.save (fontfile + '.subset')
|
2013-07-31 15:59:21 -04:00
|
|
|
log.lapse ("compile and save font")
|
2013-07-24 12:40:54 -04:00
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
log.last_time = s.log.start_time
|
|
|
|
log.lapse ("make one with everything (TOTAL TIME)")
|
2013-07-24 19:20:04 -04:00
|
|
|
|
2013-07-31 15:59:21 -04:00
|
|
|
log.font (font)
|
2013-07-24 13:34:47 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-07-31 15:59:21 -04:00
|
|
|
import sys
|
|
|
|
main (sys.argv[1:])
|