[feaLib] Build ScriptList subtable

Also, fixed a bug that caused language tags to be sometimes stripped.
Added an assertion to verify that language tags are always 4 chars.
This commit is contained in:
Sascha Brawer 2015-09-07 21:34:10 +02:00
parent d3c743bb27
commit 6140a4ec2b
8 changed files with 116 additions and 19 deletions

View File

@ -76,6 +76,7 @@ class AnchorDefinition(Statement):
class LanguageStatement(Statement):
def __init__(self, location, language, include_default, required):
Statement.__init__(self, location)
assert(len(language) == 4)
self.language = language
self.include_default = include_default
self.required = required

View File

@ -85,6 +85,7 @@ class Builder(object):
# Build a table for mapping (tag, lookup_indices) to feature_index.
# For example, ('liga', (2,3,7)) --> 23.
feature_indices = {}
scripts = {} # 'cyrl' --> {'DEU': [23, 24]} for feature #23,24
for key, lookups in sorted(self.features_.items()):
script, lang, feature_tag = key
# l.lookup_index will be None when a lookup is not needed
@ -94,6 +95,7 @@ class Builder(object):
if l.lookup_index is not None])
if len(lookup_indices) == 0:
continue
feature_key = (feature_tag, lookup_indices)
feature_index = feature_indices.get(feature_key)
if feature_index is None:
@ -106,8 +108,29 @@ class Builder(object):
frec.Feature.LookupCount = len(lookup_indices)
table.FeatureList.FeatureRecord.append(frec)
feature_indices[feature_key] = feature_index
scripts.setdefault(script, {}).setdefault(lang, []).append(
feature_index)
# Build ScriptList.
for script, lang_features in sorted(scripts.items()):
srec = otTables.ScriptRecord()
srec.ScriptTag = script
srec.Script = otTables.Script()
srec.Script.DefaultLangSys = None
srec.Script.LangSysRecord = []
for lang, feature_indices in sorted(lang_features.items()):
langsys = otTables.LangSys()
langsys.LookupOrder = None
langsys.ReqFeatureIndex = 0xFFFF
langsys.FeatureCount = len(feature_indices)
langsys.FeatureIndex = feature_indices
if lang == "dflt":
srec.Script.DefaultLangSys = langsys
else:
srec.Script.LangSysRecord.append(langsys)
srec.Script.LangSysCount = len(srec.Script.LangSysRecord)
table.ScriptList.ScriptRecord.append(srec)
# TODO(sascha): Build table.ScriptList.
table.ScriptList.ScriptCount = len(table.ScriptList.ScriptRecord)
table.FeatureList.FeatureCount = len(table.FeatureList.FeatureRecord)
table.LookupList.LookupCount = len(table.LookupList.Lookup)
@ -158,6 +181,7 @@ class Builder(object):
self.cur_lookup_ = None
def set_language(self, location, language, include_default):
assert(len(language) == 4)
if self.cur_lookup_name_:
raise FeatureLibError(
"Within a named lookup block, it is not allowed "

View File

@ -142,16 +142,16 @@ class BuilderTest(unittest.TestCase):
def test_language(self):
builder = Builder(None, TTFont())
builder.add_language_system(None, 'latn', 'FRA')
builder.add_language_system(None, 'latn', 'FRA ')
builder.start_feature(location=None, name='test')
builder.set_script(location=None, script='cyrl')
builder.set_language(location=None, language='RUS',
builder.set_language(location=None, language='RUS ',
include_default=False)
self.assertEqual(builder.language_systems, {('cyrl', 'RUS')})
builder.set_language(location=None, language='BGR',
self.assertEqual(builder.language_systems, {('cyrl', 'RUS ')})
builder.set_language(location=None, language='BGR ',
include_default=True)
self.assertEqual(builder.language_systems,
{('latn', 'FRA'), ('cyrl', 'BGR')})
{('latn', 'FRA '), ('cyrl', 'BGR ')})
def test_language_in_lookup_block(self):
self.assertRaisesRegex(

View File

@ -174,7 +174,7 @@ class Parser(object):
self.expect_keyword_("required")
required = True
self.expect_symbol_(";")
return ast.LanguageStatement(location, language.strip(),
return ast.LanguageStatement(location, language,
include_default, required)
def parse_lookup_(self, vertical):

View File

@ -5,7 +5,19 @@
<GSUB>
<Version value="1.0"/>
<ScriptList>
<!-- ScriptCount=0 -->
<!-- ScriptCount=1 -->
<ScriptRecord index="0">
<ScriptTag value="DFLT"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=2 -->
<FeatureIndex index="0" value="0"/>
<FeatureIndex index="1" value="1"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
</ScriptList>
<FeatureList>
<!-- FeatureCount=2 -->

View File

@ -5,10 +5,53 @@
<GSUB>
<Version value="1.0"/>
<ScriptList>
<!-- ScriptCount=0 -->
<!-- ScriptCount=3 -->
<ScriptRecord index="0">
<ScriptTag value="DFLT"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
<ScriptRecord index="1">
<ScriptTag value="cyrl"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
<ScriptRecord index="2">
<ScriptTag value="latn"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</DefaultLangSys>
<!-- LangSysCount=2 -->
<LangSysRecord index="0">
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</LangSysRecord>
<LangSysRecord index="1">
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=1 -->
<FeatureIndex index="0" value="0"/>
</LangSysRecord>
</Script>
</ScriptRecord>
</ScriptList>
<FeatureList>
<!-- FeatureCount=2 -->
<!-- FeatureCount=1 -->
<FeatureRecord index="0">
<FeatureTag value="liga"/>
<Feature>
@ -18,13 +61,6 @@
<LookupListIndex index="2" value="2"/>
</Feature>
</FeatureRecord>
<FeatureRecord index="1">
<FeatureTag value="liga"/>
<Feature>
<!-- LookupCount=1 -->
<LookupListIndex index="0" value="2"/>
</Feature>
</FeatureRecord>
</FeatureList>
<LookupList>
<!-- LookupCount=3 -->

View File

@ -5,7 +5,19 @@
<GSUB>
<Version value="1.0"/>
<ScriptList>
<!-- ScriptCount=0 -->
<!-- ScriptCount=1 -->
<ScriptRecord index="0">
<ScriptTag value="DFLT"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=2 -->
<FeatureIndex index="0" value="0"/>
<FeatureIndex index="1" value="1"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
</ScriptList>
<FeatureList>
<!-- FeatureCount=2 -->

View File

@ -5,7 +5,19 @@
<GSUB>
<Version value="1.0"/>
<ScriptList>
<!-- ScriptCount=0 -->
<!-- ScriptCount=1 -->
<ScriptRecord index="0">
<ScriptTag value="DFLT"/>
<Script>
<DefaultLangSys>
<ReqFeatureIndex value="65535"/>
<!-- FeatureCount=2 -->
<FeatureIndex index="0" value="0"/>
<FeatureIndex index="1" value="1"/>
</DefaultLangSys>
<!-- LangSysCount=0 -->
</Script>
</ScriptRecord>
</ScriptList>
<FeatureList>
<!-- FeatureCount=2 -->