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
|
|
|
|
|
|
|
import fontTools.ttx
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
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):
|
|
|
|
return sorted ({v:1 for v in l}.keys ())
|
|
|
|
|
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Coverage)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-23 10:50:43 -04:00
|
|
|
"Returns ascending list of remaining coverage values."
|
2013-07-21 18:16:55 -04:00
|
|
|
indices = [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
|
|
|
|
self.glyphs = [g for g in self.glyphs if g in glyphs]
|
|
|
|
return indices
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-07-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ClassDef)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 12:15:36 -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-07-23 10:23:42 -04:00
|
|
|
return unique_sorted (self.classDefs.values ())
|
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-21 18:16:55 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format in [1, 2]:
|
|
|
|
self.mapping = {g:v for g,v in self.mapping.items() if g in 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
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Sequence = [self.Sequence[i] for i in 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
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
|
|
|
self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
|
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
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 23:15:32 -04:00
|
|
|
if self.Format == 1:
|
|
|
|
self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
|
|
|
|
self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
|
|
|
|
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
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.Substitute = [self.Substitute[i] for i in indices]
|
|
|
|
self.GlyphCount = len (self.Substitute)
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.GlyphCount and all (c.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
return len (self.Coverage.subset_glyphs (glyphs))
|
2013-07-21 18:16:55 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (glyphs)
|
2013-07-21 18:16:55 -04:00
|
|
|
self.PairSet = [self.PairSet[i] for i in indices]
|
|
|
|
for p in self.PairSet:
|
|
|
|
p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
|
|
|
|
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-23 10:47:03 -04:00
|
|
|
class1_map = self.ClassDef1.subset_glyphs (glyphs)
|
|
|
|
class2_map = self.ClassDef2.subset_glyphs (glyphs)
|
2013-07-22 12:15:36 -04:00
|
|
|
self.ClassDef1.remap (class1_map)
|
|
|
|
self.ClassDef2.remap (class2_map)
|
|
|
|
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-23 10:56:04 -04:00
|
|
|
return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
mark_indices = self.MarkCoverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
base_indices = self.BaseCoverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
mark_indices = self.MarkCoverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
ligature_indices = self.LigatureCoverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
mark1_indices = self.Mark1Coverage.subset_glyphs (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-23 10:47:03 -04:00
|
|
|
mark2_indices = self.Mark2Coverage.subset_glyphs (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-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:40:59 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (glyphs)
|
2013-07-21 22:48:08 -04:00
|
|
|
self.SubRuleSet = [self.SubRuleSet[i] for i in indices]
|
|
|
|
self.SubRuleSetCount = len (self.SubRuleSet)
|
|
|
|
for rs in self.SubRuleSet:
|
|
|
|
rs.SubRule = [r for r in rs.SubRule
|
|
|
|
if all (g in glyphs for g in r.Input)]
|
|
|
|
rs.SubRuleCount = len (rs.SubRule)
|
2013-07-21 23:15:32 -04:00
|
|
|
# Prune empty subrulesets
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.SubRuleSetCount)
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.Coverage.subset_glyphs (glyphs) and self.ClassDef.subset_glyphs (glyphs))
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-23 10:47:03 -04:00
|
|
|
return all (c.subset_glyphs (glyphs) for c in self.Coverage)
|
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-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:40:59 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = self.Coverage.subset_glyphs (glyphs)
|
2013-07-21 22:48:08 -04:00
|
|
|
self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices]
|
|
|
|
self.ChainSubRuleSetCount = len (self.ChainSubRuleSet)
|
|
|
|
for rs in self.ChainSubRuleSet:
|
|
|
|
rs.ChainSubRule = [r for r in rs.ChainSubRule
|
|
|
|
if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)]
|
|
|
|
rs.ChainSubRuleCount = len (rs.ChainSubRule)
|
2013-07-21 23:15:32 -04:00
|
|
|
# Prune empty subrulesets
|
|
|
|
return self.ChainSubRuleSetCount
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 2:
|
2013-07-23 10:47:03 -04:00
|
|
|
return self.Coverage.subset_glyphs (glyphs) and \
|
|
|
|
self.LookAheadClassDef.subset_glyphs (glyphs) and \
|
|
|
|
self.BacktrackClassDef.subset_glyphs (glyphs) and \
|
|
|
|
self.InputClassDef.subset_glyphs (glyphs)
|
2013-07-21 18:40:59 -04:00
|
|
|
elif self.Format == 3:
|
2013-07-23 10:47:03 -04:00
|
|
|
return all (c.subset_glyphs (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage)
|
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 10:23:42 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
|
|
|
|
fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
self.SubstLookupRecord = [ll for ll in self.SubstLookupRecord if ll.LookupListIndex in lookup_indices]
|
|
|
|
for ll in self.SubstLookupRecord:
|
|
|
|
ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
|
|
|
|
fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
|
|
|
|
def collect_lookups (self):
|
|
|
|
return [ll.LookupListIndex for ll in self.SubstLookupRecord]
|
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-21 18:16:55 -04:00
|
|
|
if self.Format == 1:
|
2013-07-23 10:47:03 -04:00
|
|
|
return self.ExtSubTable.subset_glyphs (glyphs)
|
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 10:56:04 -04:00
|
|
|
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-21 18:40:59 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.Lookup)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
|
|
|
self.SubTable = [s for s in self.SubTable if s.subset_glyphs (glyphs)]
|
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):
|
|
|
|
return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable), []))
|
|
|
|
|
2013-07-21 23:15:32 -04:00
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LookupList)
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 11:46:50 -04:00
|
|
|
"Returns the indices of nonempty lookups."
|
2013-07-23 10:47:03 -04:00
|
|
|
return [i for (i,l) in enumerate (self.Lookup) if l.subset_glyphs (glyphs)]
|
2013-07-22 11:46:50 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.tables.otTables.LookupList)
|
|
|
|
def subset_lookups (self, lookup_indices):
|
|
|
|
self.Lookup = [self.Lookup[i] for i in lookup_indices]
|
|
|
|
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):
|
|
|
|
while True:
|
|
|
|
recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in lookup_indices), [])
|
|
|
|
recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
|
|
|
|
if not recurse_lookups:
|
|
|
|
return lookup_indices
|
|
|
|
lookup_indices = unique_sorted (lookup_indices + 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)]
|
|
|
|
self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
|
|
|
|
self.FeatureCount = len (self.FeatureRecord)
|
|
|
|
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-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 10:23:42 -04:00
|
|
|
return self.FeatureCount or self.ReqFeatureIndex != 65535
|
|
|
|
|
|
|
|
@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 10:23:42 -04:00
|
|
|
return self.LangSysCount or self.DefaultLangSys
|
|
|
|
|
|
|
|
@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)
|
|
|
|
return self.ScriptCount
|
|
|
|
|
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-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
|
|
|
lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
|
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-22 12:48:17 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('GDEF'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 12:48:17 -04:00
|
|
|
table = self.table
|
|
|
|
if table.LigCaretList:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = table.LigCaretList.Coverage.subset_glyphs (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:
|
|
|
|
table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
|
|
|
|
if not table.MarkAttachClassDef.classDefs:
|
|
|
|
table.MarkAttachClassDef = None
|
|
|
|
if table.GlyphClassDef:
|
|
|
|
table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
|
|
|
|
if not table.GlyphClassDef.classDefs:
|
|
|
|
table.GlyphClassDef = None
|
|
|
|
if table.AttachList:
|
2013-07-23 10:47:03 -04:00
|
|
|
indices = table.AttachList.Coverage.subset_glyphs (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
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableClass('kern'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 12:57:02 -04:00
|
|
|
for t in self.kernTables:
|
|
|
|
t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
|
|
|
|
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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 14:29:08 -04:00
|
|
|
self.metrics = {g:v for (g,v) in self.metrics.items() if g in 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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 14:49:54 -04:00
|
|
|
self.hdmx = {s:{g:v for (g,v) in l.items() if g in glyphs} for (s,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-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 15:29:17 -04:00
|
|
|
self.VOriginRecords = {g:v for (g,v) in self.VOriginRecords.items() if g in glyphs}
|
|
|
|
self.numVertOriginYMetrics = len (self.VOriginRecords)
|
|
|
|
return True # Never drop; has default metrics
|
|
|
|
|
2013-07-22 15:06:23 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('post'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-23 10:57:56 -04:00
|
|
|
# TODO Drop names? How?
|
2013-07-22 18:54:36 -04:00
|
|
|
self.extraNames = []
|
2013-07-23 10:28:47 -04:00
|
|
|
return True
|
2013-07-22 15:17:12 -04:00
|
|
|
|
2013-07-22 16:47:24 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('glyf'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
2013-07-22 16:47:24 -04:00
|
|
|
self.glyphs = {g:v for (g,v) in self.glyphs.items() if g in glyphs}
|
|
|
|
self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
|
2013-07-23 10:56:04 -04:00
|
|
|
return bool (self.glyphs)
|
2013-07-22 16:47:24 -04:00
|
|
|
|
2013-07-22 15:17:12 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('cmap'))
|
2013-07-23 10:47:03 -04:00
|
|
|
def subset_glyphs (self, glyphs):
|
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-22 16:01:15 -04:00
|
|
|
t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
|
|
|
|
t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
|
|
|
|
else:
|
|
|
|
t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
|
|
|
|
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-07-23 11:03:49 -04:00
|
|
|
return bool (self.tables)
|
|
|
|
|
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableClass('cmap'))
|
|
|
|
def prune (self, options):
|
|
|
|
# Drop non-Unicode / non-Symbol cmaps
|
|
|
|
# TODO Add option for this
|
|
|
|
# TODO Only keep one subtable?
|
|
|
|
self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
|
2013-07-23 10:47:03 -04:00
|
|
|
# For now, drop format=0 which can't be subset_glyphs easily?
|
2013-07-22 17:04:04 -04:00
|
|
|
self.tables = [t for t in self.tables if t.format != 0]
|
2013-07-23 11:24:20 -04:00
|
|
|
return bool (self.tables)
|
2013-07-23 11:03:49 -04:00
|
|
|
|
|
|
|
@add_method(fontTools.ttLib.getTableClass('name'))
|
|
|
|
def prune (self, options):
|
|
|
|
# TODO Make sure something remains?
|
|
|
|
# TODO Add option for this.
|
|
|
|
# TODO Drop even more (license, etc)? / Drop completely?
|
|
|
|
self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1 and n.langID == 0x0409]
|
|
|
|
return bool (self.names)
|
2013-07-22 15:17:12 -04:00
|
|
|
|
2013-07-22 15:06:23 -04:00
|
|
|
|
2013-07-23 11:17:35 -04:00
|
|
|
@add_method(fontTools.ttLib.getTableClass('glyf'))
|
|
|
|
def closure_glyphs (self, glyphs):
|
2013-07-23 11:22:11 -04:00
|
|
|
glyphs = unique_sorted (glyphs)
|
2013-07-23 11:42:48 -04:00
|
|
|
decompose = glyphs
|
|
|
|
# I don't know if component glyphs can be composite themselves.
|
|
|
|
# We handle them anyway.
|
2013-07-23 11:22:11 -04:00
|
|
|
while True:
|
|
|
|
components = []
|
2013-07-23 11:42:48 -04:00
|
|
|
for g in decompose:
|
2013-07-23 11:22:11 -04:00
|
|
|
gl = self[g]
|
|
|
|
if gl.isComposite ():
|
|
|
|
for c in gl.components:
|
|
|
|
if c.glyphName not in glyphs:
|
|
|
|
components.append (c.glyphName)
|
|
|
|
components = [c for c in components if c not in glyphs]
|
|
|
|
if not components:
|
|
|
|
return glyphs
|
2013-07-23 11:42:48 -04:00
|
|
|
decompose = unique_sorted (components)
|
|
|
|
glyphs.extend (components)
|
2013-07-23 11:17:35 -04:00
|
|
|
|
|
|
|
|
2013-07-23 11:05:25 -04:00
|
|
|
drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH', 'VDMX']
|
|
|
|
no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name']
|
|
|
|
# For now drop these
|
|
|
|
drop_tables += ['cvt ', 'fpgm', 'prep']
|
|
|
|
|
|
|
|
|
2013-07-22 14:49:54 -04:00
|
|
|
# TODO OS/2 ulUnicodeRange / ulCodePageRange?
|
2013-07-22 18:03:10 -04:00
|
|
|
# TODO Drop unneeded GSUB/GPOS entries
|
2013-07-22 13:02:24 -04:00
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2013-07-23 11:38:26 -04:00
|
|
|
import sys, time
|
|
|
|
|
|
|
|
start_time = time.time ()
|
|
|
|
last_time = start_time
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-22 11:57:13 -04:00
|
|
|
verbose = False
|
|
|
|
if "--verbose" in sys.argv:
|
|
|
|
verbose = True
|
|
|
|
sys.argv.remove ("--verbose")
|
2013-07-22 12:02:16 -04:00
|
|
|
xml = False
|
|
|
|
if "--xml" in sys.argv:
|
|
|
|
xml = True
|
|
|
|
sys.argv.remove ("--xml")
|
2013-07-23 11:38:26 -04:00
|
|
|
timing = False
|
|
|
|
if "--timing" in sys.argv:
|
|
|
|
timing = True
|
|
|
|
sys.argv.remove ("--timing")
|
|
|
|
|
|
|
|
def lapse (what):
|
|
|
|
if not timing:
|
|
|
|
return
|
|
|
|
global last_time
|
|
|
|
new_time = time.time ()
|
|
|
|
print "Took %0.3fs to %s" % (new_time - last_time, what)
|
|
|
|
last_time = new_time
|
2013-07-22 11:57:13 -04:00
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
if len (sys.argv) < 3:
|
|
|
|
print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
|
|
|
|
sys.exit (1)
|
|
|
|
|
|
|
|
fontfile = sys.argv[1]
|
|
|
|
glyphs = sys.argv[2:]
|
|
|
|
|
2013-07-22 14:29:08 -04:00
|
|
|
# Always include .notdef; anything else?
|
2013-07-22 17:30:31 -04:00
|
|
|
glyphs.append ('.notdef')
|
2013-07-22 14:29:08 -04:00
|
|
|
|
2013-07-21 18:40:59 -04:00
|
|
|
font = fontTools.ttx.TTFont (fontfile)
|
2013-07-22 17:30:31 -04:00
|
|
|
font.disassembleInstructions = False
|
2013-07-23 11:38:26 -04:00
|
|
|
lapse ("load font")
|
2013-07-21 18:40:59 -04:00
|
|
|
|
|
|
|
names = font.getGlyphNames()
|
|
|
|
# Convert to glyph names
|
|
|
|
glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
|
2013-07-23 11:38:26 -04:00
|
|
|
lapse ("compile glyph list")
|
2013-07-21 18:40:59 -04:00
|
|
|
|
2013-07-23 11:17:35 -04:00
|
|
|
glyphs_requested = glyphs[:]
|
2013-07-22 17:00:36 -04:00
|
|
|
# Close over composite glyphs
|
|
|
|
if 'glyf' in font:
|
2013-07-23 11:17:35 -04:00
|
|
|
if verbose:
|
|
|
|
print "Closing glyph list over 'glyf' components."
|
|
|
|
glyphs_closed = font['glyf'].closure_glyphs (glyphs)
|
|
|
|
if verbose:
|
|
|
|
print "Before: %d glyphs; after: %d glyphs" % (len (glyphs_requested), len (glyphs_closed))
|
2013-07-23 11:38:26 -04:00
|
|
|
lapse ("close glyph list over 'glyf' components")
|
2013-07-23 11:17:35 -04:00
|
|
|
else:
|
|
|
|
glyphs_closed = glyphs
|
|
|
|
del glyphs
|
|
|
|
|
2013-07-23 10:28:47 -04:00
|
|
|
if verbose:
|
2013-07-23 11:17:35 -04:00
|
|
|
print "Retaining %d glyphs: " % len (glyphs_closed)
|
2013-07-22 17:00:36 -04:00
|
|
|
|
2013-07-22 12:02:16 -04:00
|
|
|
if xml:
|
2013-07-22 11:57:13 -04:00
|
|
|
import xmlWriter
|
|
|
|
writer = xmlWriter.XMLWriter (sys.stdout)
|
2013-07-22 13:13:49 -04:00
|
|
|
|
2013-07-23 11:03:49 -04:00
|
|
|
prune_options = []
|
|
|
|
|
2013-07-22 13:01:33 -04:00
|
|
|
for tag in font.keys():
|
2013-07-22 13:13:49 -04:00
|
|
|
|
2013-07-22 14:29:08 -04:00
|
|
|
if tag == 'GlyphOrder':
|
|
|
|
continue
|
|
|
|
|
2013-07-22 13:13:49 -04:00
|
|
|
if tag in drop_tables:
|
2013-07-22 14:29:08 -04:00
|
|
|
if verbose:
|
|
|
|
print tag, "dropped."
|
2013-07-22 13:13:49 -04:00
|
|
|
del font[tag]
|
|
|
|
continue
|
|
|
|
|
2013-07-22 13:01:33 -04:00
|
|
|
clazz = fontTools.ttLib.getTableClass(tag)
|
2013-07-23 11:03:49 -04:00
|
|
|
|
|
|
|
if 'prune' in vars (clazz):
|
|
|
|
table = font[tag]
|
2013-07-23 11:38:26 -04:00
|
|
|
retain = table.prune (prune_options)
|
|
|
|
lapse ("prune '%s'" % tag)
|
|
|
|
if not retain:
|
2013-07-23 11:24:20 -04:00
|
|
|
if verbose:
|
|
|
|
print tag, "pruned to empty; dropped."
|
|
|
|
del font[tag]
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if verbose:
|
|
|
|
print tag, "pruned."
|
2013-07-22 13:13:49 -04:00
|
|
|
|
2013-07-23 11:03:49 -04:00
|
|
|
if tag in no_subset_tables:
|
2013-07-22 12:57:02 -04:00
|
|
|
if verbose:
|
2013-07-23 11:03:49 -04:00
|
|
|
print tag, "subsetting not needed."
|
|
|
|
elif 'subset_glyphs' in vars (clazz):
|
|
|
|
table = font[tag]
|
2013-07-23 11:17:35 -04:00
|
|
|
if tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post', 'cmap']: # What else?
|
|
|
|
glyphs = glyphs_requested
|
|
|
|
else:
|
|
|
|
glyphs = glyphs_closed
|
2013-07-23 11:38:26 -04:00
|
|
|
retain = table.subset_glyphs (glyphs)
|
|
|
|
lapse ("subset '%s'" % tag)
|
|
|
|
if not retain:
|
2013-07-23 11:03:49 -04:00
|
|
|
if verbose:
|
|
|
|
print tag, "subsetted to empty; dropped."
|
2013-07-23 11:24:20 -04:00
|
|
|
del font[tag]
|
|
|
|
continue
|
2013-07-23 11:03:49 -04:00
|
|
|
else:
|
|
|
|
if verbose:
|
|
|
|
print tag, "subsetted."
|
2013-07-23 11:17:35 -04:00
|
|
|
del glyphs
|
2013-07-22 11:57:13 -04:00
|
|
|
else:
|
2013-07-22 12:02:16 -04:00
|
|
|
if verbose:
|
2013-07-23 11:03:49 -04:00
|
|
|
print tag, "NOT subset; don't know how to subset."
|
|
|
|
continue
|
|
|
|
|
2013-07-22 14:29:08 -04:00
|
|
|
|
|
|
|
glyphOrder = font.getGlyphOrder()
|
2013-07-23 11:17:35 -04:00
|
|
|
glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
|
2013-07-22 14:29:08 -04:00
|
|
|
font.setGlyphOrder (glyphOrder)
|
|
|
|
font._buildReverseGlyphOrderDict ()
|
2013-07-23 11:38:26 -04:00
|
|
|
lapse ("subset GlyphOrder")
|
2013-07-21 23:15:32 -04:00
|
|
|
|
2013-07-22 17:30:31 -04:00
|
|
|
if xml:
|
|
|
|
for tag in font.keys():
|
|
|
|
writer.begintag (tag)
|
|
|
|
writer.newline ()
|
|
|
|
font[tag].toXML(writer, font)
|
|
|
|
writer.endtag (tag)
|
|
|
|
writer.newline ()
|
|
|
|
|
2013-07-22 11:46:50 -04:00
|
|
|
font.save (fontfile + '.subset')
|
2013-07-23 11:38:26 -04:00
|
|
|
lapse ("compile and save font")
|
|
|
|
|
|
|
|
last_time = start_time
|
|
|
|
lapse ("make one with everything (TOTAL TIME)")
|