예제 #1
0
def add_gdef(self, ff):
    gdef = feaast.TableBlock("GDEF")
    gc = self.glyphclasses
    categories = {"base": [], "mark": [], "ligature": [], "component": []}
    for k, v in gc.items():
        categories[v] = categories.get(v, []) + [k]
    if categories:
        gdef.statements.append(
            feaast.GlyphClassDefStatement(
                _to_inline_class(categories["base"]),
                _to_inline_class(categories["mark"]),
                _to_inline_class(categories["ligature"]),
                _to_inline_class(categories["component"]),
            ))
        ff.statements.append(gdef)
예제 #2
0
 def parse_table_(self):
     assert self.is_cur_keyword_("table")
     location, name = self.cur_token_location_, self.expect_tag_()
     table = ast.TableBlock(location, name)
     self.expect_symbol_("{")
     handler = {
         "GDEF": self.parse_table_GDEF_,
     }.get(name)
     if handler:
         handler(table)
     else:
         raise FeatureLibError('"table %s" is not supported' % name.strip(),
                               location)
     self.expect_symbol_("}")
     end_tag = self.expect_tag_()
     if end_tag != name:
         raise FeatureLibError('Expected "%s"' % name.strip(),
                               self.cur_token_location_)
     self.expect_symbol_(";")
     return table
예제 #3
0
    def _buildFeatureFile(self, tables):
        doc = ast.FeatureFile()
        statements = doc.statements

        if self._glyphclasses:
            statements.append(ast.Comment("# Glyph classes"))
            statements.extend(self._glyphclasses.values())

        if self._markclasses:
            statements.append(ast.Comment("\n# Mark classes"))
            statements.extend(c[1] for c in sorted(self._markclasses.items()))

        if self._lookups:
            statements.append(ast.Comment("\n# Lookups"))
            for lookup in self._lookups.values():
                statements.extend(getattr(lookup, "targets", []))
                statements.append(lookup)

        # Prune features
        features = self._features.copy()
        for ftag in features:
            scripts = features[ftag]
            for stag in scripts:
                langs = scripts[stag]
                for ltag in langs:
                    langs[ltag] = [
                        l for l in langs[ltag] if l.lower() in self._lookups
                    ]
                scripts[stag] = {t: l for t, l in langs.items() if l}
            features[ftag] = {t: s for t, s in scripts.items() if s}
        features = {t: f for t, f in features.items() if f}

        if features:
            statements.append(ast.Comment("# Features"))
            for ftag, scripts in features.items():
                feature = ast.FeatureBlock(ftag)
                stags = sorted(scripts, key=lambda k: 0 if k == "DFLT" else 1)
                for stag in stags:
                    feature.statements.append(ast.ScriptStatement(stag))
                    ltags = sorted(scripts[stag],
                                   key=lambda k: 0 if k == "dflt" else 1)
                    for ltag in ltags:
                        include_default = True if ltag == "dflt" else False
                        feature.statements.append(
                            ast.LanguageStatement(
                                ltag, include_default=include_default))
                        for name in scripts[stag][ltag]:
                            lookup = self._lookups[name.lower()]
                            lookupref = ast.LookupReferenceStatement(lookup)
                            feature.statements.append(lookupref)
                statements.append(feature)

        if self._gdef and "GDEF" in tables:
            classes = []
            for name in ("BASE", "MARK", "LIGATURE", "COMPONENT"):
                if name in self._gdef:
                    classname = "GDEF_" + name.lower()
                    glyphclass = ast.GlyphClassDefinition(
                        classname, self._gdef[name])
                    statements.append(glyphclass)
                    classes.append(ast.GlyphClassName(glyphclass))
                else:
                    classes.append(None)

            gdef = ast.TableBlock("GDEF")
            gdef.statements.append(ast.GlyphClassDefStatement(*classes))
            statements.append(gdef)

        return doc