def test_buildCoverage(self): cov = builder.buildCoverage({"two", "four"}, {"two": 2, "four": 4}) self.assertEqual(getXML(cov.toXML), '<Coverage>' ' <Glyph value="two"/>' ' <Glyph value="four"/>' '</Coverage>')
def test_buildCoverage(self): cov = builder.buildCoverage({"two", "four"}, {"two": 2, "four": 4}) assert getXML(cov.toXML) == [ "<Coverage>", ' <Glyph value="two"/>', ' <Glyph value="four"/>', "</Coverage>", ]
def build(self): markClasses = self.buildMarkClasses_(self.marks) markClassList = sorted(markClasses.keys(), key=markClasses.get) marks = {mark: (markClasses[mc], anchor) for mark, (mc, anchor) in self.marks.items()} st = otTables.MarkMarkPos() st.Format = 1 st.ClassCount = len(markClasses) st.Mark1Coverage = otl.buildCoverage(marks, self.glyphMap) st.Mark2Coverage = otl.buildCoverage(self.baseMarks, self.glyphMap) st.Mark1Array = otl.buildMarkArray(marks, self.glyphMap) st.Mark2Array = otTables.Mark2Array() st.Mark2Array.Mark2Count = len(st.Mark2Coverage.glyphs) st.Mark2Array.Mark2Record = [] for base in st.Mark2Coverage.glyphs: anchors = [self.baseMarks[base].get(mc) for mc in markClassList] st.Mark2Array.Mark2Record.append(otl.buildMark2Record(anchors)) return self.buildLookup_([st])
def build(self): subtables = [] for prefix, suffix, mapping in self.substitutions: st = otTables.ReverseChainSingleSubst() st.Format = 1 self.setBacktrackCoverage_(prefix, st) self.setLookAheadCoverage_(suffix, st) st.Coverage = otl.buildCoverage(mapping.keys(), self.glyphMap) st.GlyphCount = len(mapping) st.Substitute = [mapping[g] for g in st.Coverage.glyphs] subtables.append(st) return self.buildLookup_(subtables)
def setInputCoverage_(self, glyphs, subtable): subtable.InputGlyphCount = len(glyphs) subtable.InputCoverage = [] for g in glyphs: coverage = otl.buildCoverage(g, self.glyphMap) subtable.InputCoverage.append(coverage)
def setLookAheadCoverage_(self, suffix, subtable): subtable.LookAheadGlyphCount = len(suffix) subtable.LookAheadCoverage = [] for s in suffix: coverage = otl.buildCoverage(s, self.glyphMap) subtable.LookAheadCoverage.append(coverage)
def setBacktrackCoverage_(self, prefix, subtable): subtable.BacktrackGlyphCount = len(prefix) subtable.BacktrackCoverage = [] for p in reversed(prefix): coverage = otl.buildCoverage(p, self.glyphMap) subtable.BacktrackCoverage.append(coverage)
def _post_process(self, otf): font = self._font gdef = otf["GDEF"].table classdef = gdef.GlyphClassDef.classDefs for glyph in font: if glyph.lib.get(GLYPHCLASS_KEY) == "mark": classdef[glyph.name] = 3 constants = font.lib.get(MATH_KEY) if constants: from fontTools.ttLib import newTable from fontTools.ttLib.tables import otTables from fontTools.otlLib import builder as otl glyphMap = {n: i for i, n in enumerate(font.glyphOrder)} table = otTables.MATH() table.Version = 0x00010000 table.MathConstants = otTables.MathConstants() for c in constants: if c == "MinConnectorOverlap": continue v = constants[c] if c not in ("ScriptPercentScaleDown", "ScriptScriptPercentScaleDown", "DelimitedSubFormulaMinHeight", "DisplayOperatorMinHeight", "RadicalDegreeBottomRaisePercent"): vr = otTables.MathValueRecord() vr.Value = v v = vr setattr(table.MathConstants, c, v) extended = set() italic = {} accent = {} vvars = {} hvars = {} vcomp = {} hcomp = {} for glyph in font: math = glyph.lib.get(MATH_KEY) if math: if "IsExtendedShape" in math: extended.add(glyph.name) if "ItalicCorrection" in math: italic[glyph.name] = otTables.MathValueRecord() italic[glyph.name].Value = math["ItalicCorrection"] if "TopAccentHorizontal" in math: accent[glyph.name] = otTables.MathValueRecord() accent[glyph.name].Value = math["TopAccentHorizontal"] if "GlyphVariantsVertical" in math: vvars[glyph.name] = math["GlyphVariantsVertical"] if "GlyphCompositionVertical" in math: vcomp[glyph.name] = math["GlyphCompositionVertical"] if "GlyphVariantsHorizontal" in math: hvars[glyph.name] = math["GlyphVariantsHorizontal"] if "GlyphCompositionHorizontal" in math: hcomp[glyph.name] = math["GlyphCompositionHorizontal"] table.MathGlyphInfo = otTables.MathGlyphInfo() table.MathGlyphInfo.populateDefaults() coverage = otl.buildCoverage(italic.keys(), glyphMap) table.MathGlyphInfo.MathItalicsCorrectionInfo = otTables.MathItalicsCorrectionInfo() table.MathGlyphInfo.MathItalicsCorrectionInfo.Coverage = coverage table.MathGlyphInfo.MathItalicsCorrectionInfo.ItalicsCorrection = [italic[n] for n in coverage.glyphs] coverage = otl.buildCoverage(accent.keys(), glyphMap) table.MathGlyphInfo.MathTopAccentAttachment = otTables.MathTopAccentAttachment() table.MathGlyphInfo.MathTopAccentAttachment.TopAccentCoverage = coverage table.MathGlyphInfo.MathTopAccentAttachment.TopAccentAttachment = [accent[n] for n in coverage.glyphs] table.MathGlyphInfo.ExtendedShapeCoverage = otl.buildCoverage(extended, glyphMap) table.MathVariants = otTables.MathVariants() table.MathVariants.MinConnectorOverlap = constants["MinConnectorOverlap"] coverage = otl.buildCoverage(vvars.keys(), glyphMap) table.MathVariants.VertGlyphCoverage = coverage table.MathVariants.VertGlyphConstruction = [] for name in coverage.glyphs: variants = vvars[name] construction = otTables.MathGlyphConstruction() construction.populateDefaults() construction.VariantCount = len(variants) construction.MathGlyphVariantRecord = [] for variant in variants: bbox = font[variant].getBounds(font) record = otTables.MathGlyphVariantRecord() record.VariantGlyph = variant record.AdvanceMeasurement = int(bbox[-1] - bbox[1] + 1) construction.MathGlyphVariantRecord.append(record) if name in vcomp: construction.GlyphAssembly = otTables.GlyphAssembly() construction.GlyphAssembly.ItalicsCorrection = otTables.MathValueRecord() construction.GlyphAssembly.ItalicsCorrection.Value = 0 construction.GlyphAssembly.PartRecords = [] for comp in vcomp[name]: record = otTables.GlyphPartRecord() record.glyph = comp[0] f, s, e, a = [int(v) for v in comp[1].split(",")] record.StartConnectorLength = s record.EndConnectorLength = e record.FullAdvance = a record.PartFlags = f construction.GlyphAssembly.PartRecords.append(record) table.MathVariants.VertGlyphConstruction.append(construction) coverage = otl.buildCoverage(hvars.keys(), glyphMap) table.MathVariants.HorizGlyphCoverage = coverage table.MathVariants.HorizGlyphConstruction = [] for name in coverage.glyphs: variants = hvars[name] construction = otTables.MathGlyphConstruction() construction.populateDefaults() construction.VariantCount = len(variants) construction.MathGlyphVariantRecord = [] for variant in variants: bbox = font[variant].getBounds(font) record = otTables.MathGlyphVariantRecord() record.VariantGlyph = variant record.AdvanceMeasurement = int(bbox[-2] - bbox[0] + 1) construction.MathGlyphVariantRecord.append(record) if name in hcomp: construction.GlyphAssembly = otTables.GlyphAssembly() construction.GlyphAssembly.ItalicsCorrection = otTables.MathValueRecord() construction.GlyphAssembly.ItalicsCorrection.Value = 0 construction.GlyphAssembly.PartRecords = [] for comp in hcomp[name]: record = otTables.GlyphPartRecord() record.glyph = comp[0] f, s, e, a = [int(v) for v in comp[1].split(",")] record.StartConnectorLength = s record.EndConnectorLength = e record.FullAdvance = a record.PartFlags = f construction.GlyphAssembly.PartRecords.append(record) table.MathVariants.HorizGlyphConstruction.append(construction) otf["MATH"] = newTable("MATH") otf["MATH"].table = table