def test_sub(self): kerning1 = { ("A", "A"): 1, ("B", "B"): 1, ("NotIn2", "NotIn2"): 1, ("public.kern1.NotIn2", "C"): 1, ("public.kern1.D", "public.kern2.D"): 1, } groups1 = { "public.kern1.NotIn1": ["C"], "public.kern1.D": ["D", "H"], "public.kern2.D": ["D", "H"], } kerning2 = { ("A", "A"): -1, ("B", "B"): 1, ("NotIn1", "NotIn1"): 1, ("public.kern1.NotIn1", "C"): 1, ("public.kern1.D", "public.kern2.D"): 1, } groups2 = { "public.kern1.NotIn2": ["C"], "public.kern1.D": ["D"], "public.kern2.D": ["D", "H"], } obj = MathKerning(kerning1, groups1) - MathKerning(kerning2, groups2) self.assertEqual(sorted(obj.items()), [(('A', 'A'), 2), (('NotIn1', 'NotIn1'), -1), (('NotIn2', 'NotIn2'), 1), (('public.kern1.NotIn1', 'C'), -1), (('public.kern1.NotIn2', 'C'), 1)]) self.assertEqual(sorted(obj.groups()["public.kern1.D"]), ['D', 'H']) self.assertEqual(sorted(obj.groups()["public.kern2.D"]), ['D', 'H'])
def test_compare_same_kerning_diff_groups(self): kerning1 = { ("A", "A"): 0, ("B", "B"): 4, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 4, } kerning2 = { ("A", "A"): 0, ("B", "B"): 4, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 4, } groups1 = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } groups2 = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2", "C3"], } mathKerning1 = MathKerning(kerning1, groups1) mathKerning2 = MathKerning(kerning2, groups2) self.assertTrue(mathKerning1 < mathKerning2) self.assertFalse(mathKerning1 > mathKerning2) self.assertNotEqual(mathKerning1, mathKerning2)
def test_extractKerning(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 1, ("D", "D"): 1.0, ("E", "E"): 1.2, } groups = { "public.kern1.C": ["C", "C1"], "public.kern2.C": ["C", "C1"] } font = TestFont() self.assertEqual(font.kerning.asDict(), {}) self.assertEqual(list(font.groups.items()), []) obj = MathKerning(kerning, groups) obj.extractKerning(font) self.assertEqual( sorted(font.kerning.asDict().items()), [(('B', 'B'), 1), (('D', 'D'), 1), (('E', 'E'), 1), (('public.kern1.C', 'public.kern2.C'), 1)]) self.assertEqual( sorted(font.groups.items()), [('public.kern1.C', ['C', 'C1']), ('public.kern2.C', ['C', 'C1'])])
def test_addTo(self): kerning = { ("A", "A"): 1, ("B", "B"): -1, } obj = MathKerning(kerning) obj.addTo(1) self.assertEqual(sorted(obj.items()), [(('A', 'A'), 2), (('B', 'B'), 0)])
def test_guessPairType(self): kerning = { ("public.kern1.A", "public.kern2.A"): 1, ("A1", "public.kern2.A"): 2, ("public.kern1.A", "A2"): 3, ("A3", "A3"): 4, ("public.kern1.B", "public.kern2.B"): 5, ("public.kern1.B", "B"): 6, ("public.kern1.C", "public.kern2.C"): 7, ("C", "public.kern2.C"): 8, } groups = { "public.kern1.A": ["A", "A1", "A2", "A3"], "public.kern2.A": ["A", "A1", "A2", "A3"], "public.kern1.B": ["B"], "public.kern2.B": ["B"], "public.kern1.C": ["C"], "public.kern2.C": ["C"], } obj = MathKerning(kerning, groups) self.assertEqual(obj.guessPairType(("public.kern1.A", "public.kern2.A")), ('group', 'group')) self.assertEqual(obj.guessPairType(("A1", "public.kern2.A")), ('exception', 'group')) self.assertEqual(obj.guessPairType(("public.kern1.A", "A2")), ('group', 'exception')) self.assertEqual(obj.guessPairType(("A3", "A3")), ('exception', 'exception')) self.assertEqual(obj.guessPairType(("A", "A")), ('group', 'group')) self.assertEqual(obj.guessPairType(("B", "B")), ('group', 'exception')) self.assertEqual(obj.guessPairType(("C", "C")), ('exception', 'group'))
def test_round(self): kerning = { ("A", "A"): 1.99, ("B", "B"): 4, ("C", "C"): 7, ("D", "D"): 9.01, } obj = MathKerning(kerning) obj.round(5) self.assertEqual(sorted(obj.items()), [(('A', 'A'), 0), (('B', 'B'), 5), (('C', 'C'), 5), (('D', 'D'), 10)])
def test_round(self): kerning = { ("A", "A"): 1.99, ("B", "B"): 4, ("C", "C"): 7, ("D", "D"): 9.01, } obj = MathKerning(kerning) obj.round(5) self.assertEqual( sorted(obj.items()), [(('A', 'A'), 0), (('B', 'B'), 5), (('C', 'C'), 5), (('D', 'D'), 10)])
def test_compare_same_kerning_only(self): kerning1 = { ("A", "A"): 0, ("B", "B"): 4, } kerning2 = { ("A", "A"): 0, ("B", "B"): 4, } mathKerning1 = MathKerning(kerning1, {}) mathKerning2 = MathKerning(kerning2, {}) self.assertFalse(mathKerning1 < mathKerning2) self.assertFalse(mathKerning1 > mathKerning2) self.assertEqual(mathKerning1, mathKerning2)
def test_div_tuple_factor(self): kerning = { ("A", "A"): 0, ("B", "B"): 4, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 4, } groups = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } obj = MathKerning(kerning, groups) / (4, 2) self.assertEqual(sorted(obj.items()), [(('B', 'B'), 1), (('C2', 'public.kern2.C'), 0), (('public.kern1.C', 'public.kern2.C'), 1)])
def test_mul(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 2, } groups = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } obj = MathKerning(kerning, groups) * 2 self.assertEqual(sorted(obj.items()), [(('B', 'B'), 2), (('C2', 'public.kern2.C'), 0), (('public.kern1.C', 'public.kern2.C'), 4)])
def test_copy(self): kerning1 = { ("A", "A"): 1, ("B", "B"): 1, ("NotIn2", "NotIn2"): 1, ("public.kern1.NotIn2", "C"): 1, ("public.kern1.D", "public.kern2.D"): 1, } groups1 = { "public.kern1.NotIn1": ["C"], "public.kern1.D": ["D", "H"], "public.kern2.D": ["D", "H"], } obj1 = MathKerning(kerning1, groups1) obj2 = obj1.copy() self.assertEqual(sorted(obj1.items()), sorted(obj2.items()))
def test_cleanup(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 1, ("D", "D"): 1.0, ("E", "E"): 1.2, } groups = {"public.kern1.C": ["C", "C1"], "public.kern2.C": ["C", "C1"]} obj = MathKerning(kerning, groups) obj.cleanup() self.assertEqual(sorted(obj.items()), [(('B', 'B'), 1), (('C', 'public.kern2.C'), 0), (('D', 'D'), 1), (('E', 'E'), 1.2), (('public.kern1.C', 'public.kern2.C'), 1)])
def addKerning(self, instanceLocation=None, sources=None): """ Calculate the kerning data for this location and add it to this instance. * instanceLocation: Location object * source: dict of {sourcename: (source, sourceLocation)} """ items = [] kerningObject = self.font.kerning kerningMasters = [] if instanceLocation is None: instanceLocation = self.locationObject if sources is None: # kerning has no special requests, add the default sources sources = self.sources for sourceName, (source, sourceLocation) in sources.items(): if sourceName in self.muted['kerning']: # kerning in this master was muted, so do not add. if self.verbose and self.logger: self.logger.info("Muting kerning data for %s", instanceLocation) continue items.append((sourceLocation, MathKerning(source.kerning))) bias, m = buildMutator(items) instanceObject = m.makeInstance(instanceLocation) if self.roundGeometry: instanceObject.round() instanceObject.extractKerning(self.font)
def test_div_tuple_factor(self): kerning = { ("A", "A"): 0, ("B", "B"): 4, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 4, } groups = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } obj = MathKerning(kerning, groups) / (4, 2) self.assertEqual( sorted(obj.items()), [(('B', 'B'), 1), (('C2', 'public.kern2.C'), 0), (('public.kern1.C', 'public.kern2.C'), 1)])
def test_mul(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 2, } groups = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } obj = MathKerning(kerning, groups) * 2 self.assertEqual( sorted(obj.items()), [(('B', 'B'), 2), (('C2', 'public.kern2.C'), 0), (('public.kern1.C', 'public.kern2.C'), 4)])
def test_getitem(self): kerning = { ("public.kern1.A", "public.kern2.A"): 1, ("A1", "public.kern2.A"): 2, ("public.kern1.A", "A2"): 3, ("A3", "A3"): 4, } groups = { "public.kern1.A": ["A", "A1", "A2", "A3"], "public.kern2.A": ["A", "A1", "A2", "A3"], } obj = MathKerning(kerning, groups) self.assertEqual(obj["A", "A"], 1) self.assertEqual(obj["A1", "A"], 2) self.assertEqual(obj["A", "A2"], 3) self.assertEqual(obj["A3", "A3"], 4) self.assertEqual(obj["X", "X"], 0) self.assertEqual(obj["A3", "public.kern2.A"], 1) self.assertEqual(sorted(obj.keys())[1], ("A3", "A3")) self.assertEqual(sorted(obj.values())[3], 4)
def test_fallback(self): groups = { "public.kern1.A" : ["A", "A.alt"], "public.kern2.O" : ["O", "O.alt"] } kerning1 = { ("A", "O") : 1000, ("public.kern1.A", "public.kern2.O") : 100 } kerning2 = { ("public.kern1.A", "public.kern2.O") : 200 } kerning1 = MathKerning(kerning1, groups) kerning2 = MathKerning(kerning2, groups) kerning3 = kerning1 + kerning2 self.assertEqual( kerning3["A", "O"], 1200)
def test_add(self): kerning1 = { ("A", "A"): 1, ("B", "B"): 1, ("NotIn2", "NotIn2"): 1, ("public.kern1.NotIn2", "C"): 1, ("public.kern1.D", "public.kern2.D"): 1, } groups1 = { "public.kern1.NotIn1": ["C"], "public.kern1.D": ["D", "H"], "public.kern2.D": ["D", "H"], } kerning2 = { ("A", "A"): -1, ("B", "B"): 1, ("NotIn1", "NotIn1"): 1, ("public.kern1.NotIn1", "C"): 1, ("public.kern1.D", "public.kern2.D"): 1, } groups2 = { "public.kern1.NotIn2": ["C"], "public.kern1.D": ["D", "H"], "public.kern2.D": ["D", "H"], } obj = MathKerning(kerning1, groups1) + MathKerning(kerning2, groups2) self.assertEqual( sorted(obj.items()), [(('B', 'B'), 2), (('NotIn1', 'NotIn1'), 1), (('NotIn2', 'NotIn2'), 1), (('public.kern1.D', 'public.kern2.D'), 2), (('public.kern1.NotIn1', 'C'), 1), (('public.kern1.NotIn2', 'C'), 1)]) self.assertEqual( obj.groups()["public.kern1.D"], ['D', 'H']) self.assertEqual( obj.groups()["public.kern2.D"], ['D', 'H'])
def test_cleanup(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 1, ("D", "D"): 1.0, ("E", "E"): 1.2, } groups = { "public.kern1.C": ["C", "C1"], "public.kern2.C": ["C", "C1"] } obj = MathKerning(kerning, groups) obj.cleanup() self.assertEqual( sorted(obj.items()), [(('B', 'B'), 1), (('C', 'public.kern2.C'), 0), (('D', 'D'), 1), (('E', 'E'), 1.2), (('public.kern1.C', 'public.kern2.C'), 1)])
def test_rmul_tuple_factor(self): kerning = { ("A", "A"): 0, ("B", "B"): 1, ("C2", "public.kern2.C"): 0, ("public.kern1.C", "public.kern2.C"): 2, } groups = { "public.kern1.C": ["C1", "C2"], "public.kern2.C": ["C1", "C2"], } obj = (3, 2) * MathKerning(kerning, groups) self.assertEqual(sorted(obj.items()), [(('B', 'B'), 3), (('C2', 'public.kern2.C'), 0), (('public.kern1.C', 'public.kern2.C'), 6)])
from fontParts.fontshell import RFont from ufoProcessor.varModels import VariationModelMutator from mutatorMath.objects.mutator import buildMutator from fontTools.designspaceLib import AxisDescriptor # kerning exception value. Different results for 1 and 0 value = 0 #f = Font() f = RFont() # doesn't make a difference f.groups["public.kern1.groupA"] = ['one', 'Bee'] f.groups["public.kern2.groupB"] = ['two', 'Three'] f.kerning[('public.kern1.groupA', 'public.kern2.groupB')] = -100 f.kerning[("one", "two")] = value m = MathKerning(f.kerning, f.groups) print("mathKerning object items:", m.items()) print("\tpair", ('public.kern1.groupA', 'public.kern2.groupB'), m[('public.kern1.groupA', 'public.kern2.groupB')]) print("\tpair", ('public.kern1.groupA', 'two'), m[('public.kern1.groupA', 'two')]) print("\tpair", ('one', 'public.kern2.groupB'), m[('one', 'public.kern2.groupB')]) print("\tpair", ('one', 'two'), m[('one', 'two')]) items = [(dict(w=0), m), (dict(w=1), m)] a = AxisDescriptor() a.name = "w" a.minimum = 0 a.default = 0 a.maximum = 1
from fontMath.mathKerning import MathKerning from defcon.objects.font import Font f = Font() f.groups["public.kern1.groupA"] = ['one', 'Bee'] f.groups["public.kern2.groupB"] = ['two', 'Three'] f.kerning[('public.kern1.groupA', 'public.kern2.groupB')] = -100 f.kerning[('one', 'two')] = 0 m = MathKerning(f.kerning, f.groups) print(m.items()) print((m*1.0).items())
def generateInstanceFont(self, spot, generationInfos): # self.w.spotSheet.close() # delattr(self.w, 'spotSheet') if generationInfos['sourceFont']: baseFont = generationInfos['sourceFont'][0] doKerning = generationInfos['interpolateKerning'] doFontInfos = generationInfos['interpolateFontInfos'] doReport = generationInfos['printReport'] progress = ProgressWindow('Generating instance', parentWindow=self.w) fonts = [font for _, font in self.masters] i, j = spot ch = getKeyForValue(i) instanceLocation = Location(horizontal=i, vertical=j) masterLocations = [(Location(horizontal=getValueForKey(_ch), vertical=_j), masterFont) for (_ch, _j), masterFont in self.masters] # Build font newFont = RFont(showUI=False) newFont.info.familyName = baseFont.info.familyName newFont.info.styleName = '%s%s'%(ch.upper(), j+1) interpolatedGlyphs = [] interpolatedInfo = None interpolatedKerning = None interpolationReports = [] # interpolate font infos if doFontInfos: infoMasters = [(location, MathInfo(font.info)) for location, font in masterLocations] try: bias, iM = buildMutator(infoMasters) instanceInfo = iM.makeInstance(instanceLocation) instanceInfo.extractInfo(newFont.info) except: pass # interpolate kerning if doKerning: kerningMasters = [(location, MathKerning(font.kerning)) for location, font in masterLocations] try: bias, kM = buildMutator(kerningMasters) instanceKerning = kM.makeInstance(instanceLocation) instanceKerning.extractKerning(newFont) for key, value in baseFont.groups.items(): newFont.groups[key] = value except: pass # filter compatible glyphs fontKeys = [set(font.keys()) for font in fonts] glyphList = set() for i, item in enumerate(fontKeys): if i == 0: glyphList = item elif i > 0: glyphList = glyphList & item compatibleBaseGlyphList = [] compatibleCompositeGlyphList = [] for glyphName in glyphList: glyphs = [font[glyphName] for font in fonts] compatible = True for glyph in glyphs[1:]: comp, report = glyphs[0].isCompatible(glyph) if comp == False: name = '%s <X> %s'%(fontName(glyphs[0].getParent()), fontName(glyph.getParent())) reportLine = (name, report) if reportLine not in interpolationReports: interpolationReports.append(reportLine) compatible = False if compatible: compatibleBaseGlyphList.append(glyphName) # initiate glyph interpolation for glyphName in compatibleBaseGlyphList: glyphMasters = [(location, MathGlyph(font[glyphName])) for location, font in masterLocations] try: bias, gM = buildMutator(glyphMasters) newGlyph = RGlyph() instanceGlyph = gM.makeInstance(instanceLocation) interpolatedGlyphs.append((glyphName, instanceGlyph.extractGlyph(newGlyph))) except: continue for name, iGlyph in interpolatedGlyphs: newFont.insertGlyph(iGlyph, name) progress.close() digest = [] if doReport: for fontNames, report in interpolationReports: digest.append(fontNames) digest += [u'– %s'%(reportLine) for reportLine in report] digest.append('\n') print '\n'.join(digest) newFont.showUI()