Ejemplo n.º 1
0
def addLookupReferences(
    feature, lookups, script=None, languages=None, exclude_dflt=False
):
    """Add references to named lookups to the feature's statements.
    If `script` (str) and `languages` (sequence of str) are provided,
    only register the lookup for the given script and languages,
    optionally with `exclude_dflt` directive.
    Otherwise add a global reference which will be registered for all
    the scripts and languages in the feature file's `languagesystems`
    statements.
    """
    assert lookups
    if not script:
        for lookup in lookups:
            feature.statements.append(ast.LookupReferenceStatement(lookup))
        return

    feature.statements.append(ast.ScriptStatement(script))
    if exclude_dflt:
        for language in languages or ("dflt",):
            feature.statements.append(
                ast.LanguageStatement(language, include_default=False)
            )
            for lookup in lookups:
                feature.statements.append(ast.LookupReferenceStatement(lookup))
    else:
        feature.statements.append(ast.LanguageStatement("dflt", include_default=True))
        for lookup in lookups:
            feature.statements.append(ast.LookupReferenceStatement(lookup))
        for language in languages or ():
            if language == "dflt":
                continue
            feature.statements.append(
                ast.LanguageStatement(language, include_default=True)
            )
Ejemplo n.º 2
0
def asFeaAST(self, allLanguages=[("DFLT", "dflt")]):
    if set(allLanguages) == set(self.routine.languages):
        if self.routine.usecount == 1:
            return self.routine.asFeaAST(inFeature=True)
        return feaast.LookupReferenceStatement(self.routine.asFeaAST())
    f = feaast.Block()
    lastLang = 'dflt'
    for s,l in self.routine.languages:
        f.statements.append(feaast.ScriptStatement(s))
        if l != lastLang:
            f.statements.append(feaast.LanguageStatement("%4s" % l))
            lastLang = l
        f.statements.append(feaast.LookupReferenceStatement(self.routine.asFeaAST()))
    return f
Ejemplo n.º 3
0
def asFeaAST(self, expand=False):
    if expand or not self.routine.languages:
        if self.routine.usecount == 1:
            return self.routine.asFeaAST(inFeature=True)
        return feaast.LookupReferenceStatement(self.routine.asFeaAST())
    f = feaast.Block()
    lastLang = 'dflt'
    for s, l in self.routine.languages:
        f.statements.append(feaast.ScriptStatement(s))
        if l != lastLang:
            f.statements.append(feaast.LanguageStatement("%4s" % l))
            lastLang = l
        f.statements.append(
            feaast.LookupReferenceStatement(self.routine.asFeaAST()))
    return f
Ejemplo n.º 4
0
    def parse_lookup_(self, vertical):
        assert self.is_cur_keyword_("lookup")
        location, name = self.cur_token_location_, self.expect_name_()

        if self.next_token_ == ";":
            lookup = self.lookups_.resolve(name)
            if lookup is None:
                raise FeatureLibError("Unknown lookup \"%s\"" % name,
                                      self.cur_token_location_)
            self.expect_symbol_(";")
            return ast.LookupReferenceStatement(location, lookup)

        use_extension = False
        if self.next_token_ == "useExtension":
            self.expect_keyword_("useExtension")
            use_extension = True

        block = ast.LookupBlock(location, name, use_extension)
        self.parse_block_(block, vertical)
        self.lookups_.define(name, block)
        return block
Ejemplo n.º 5
0
def asFeaAST(self, expand=False):
    # if self.routine.usecount == 1:
    #     return self.routine.asFeaAST()
    f = feaast.Block()
    f.statements.append(feaast.LookupReferenceStatement(self.routine.asFeaAST()))
    return f
Ejemplo n.º 6
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