def testRoundTrip(self): srcDir = GLYPHSETDIR dstDir = self.dstDir src = GlyphSet(srcDir) dst = GlyphSet(dstDir) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) # compare raw file data: for glyphName in src.keys(): fileName = src.contents[glyphName] org = file(os.path.join(srcDir, fileName), READ_MODE).read() new = file(os.path.join(dstDir, fileName), READ_MODE).read() self.assertEqual(org, new, "%r .glif file differs after round tripping" % glyphName)
def importAllGlifFiles(font, dirName=None, doProgress=True, bar=None): """import all GLIFs into a FontLab font""" if dirName is None: if font.file_name: dir, base = os.path.split(font.file_name) base = base.split(".")[0] + ".glyphs" dirName = os.path.join(dir, base) else: dirName = GetFolder("Please select a folder with .glif files") glyphSet = GlyphSet(dirName) glyphNames = glyphSet.keys() glyphNames.sort() barStart = 0 closeBar = False if doProgress: if not bar: bar = ProgressBar("Importing Glyphs", len(glyphNames)) closeBar = True else: barStart = bar.getCurrentTick() else: bar = None try: for i in range(len(glyphNames)): #if not (i % 10) and not bar.tick(barStart + i): # raise KeyboardInterrupt glyphName = glyphNames[i] flGlyph = NewGlyph(font, glyphName, clear=True) pen = FLPointPen(flGlyph) glyph = GlyphPlaceholder() glyphSet.readGlyph(glyphName, glyph, pen) if hasattr(glyph, "width"): flGlyph.width = int(round(glyph.width)) if hasattr(glyph, "unicodes"): flGlyph.unicodes = glyph.unicodes if hasattr(glyph, "note"): flGlyph.note = glyph.note # XXX must encode if hasattr(glyph, "lib"): from cStringIO import StringIO from robofab.plistlib import writePlist lib = glyph.lib if lib: if len(lib) == 1 and "org.robofab.fontlab.customdata" in lib: data = lib["org.robofab.fontlab.customdata"].data else: f = StringIO() writePlist(glyph.lib, f) data = f.getvalue() flGlyph.customdata = data # XXX the next bit is only correct when font is the current font :-( fl.UpdateGlyph(font.FindGlyph(glyphName)) if bar and not i % 10: bar.tick(barStart + i) except KeyboardInterrupt: if bar: bar.close() bar = None fl.UpdateFont(FontIndex(font)) if bar and closeBar: bar.close()
def testRoundTrip(self): srcDir = GLYPHSETDIR dstDir = self.dstDir src = GlyphSet(srcDir) dst = GlyphSet(dstDir) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) # compare raw file data: for glyphName in src.keys(): fileName = src.contents[glyphName] org = file(os.path.join(srcDir, fileName), READ_MODE).read() new = file(os.path.join(dstDir, fileName), READ_MODE).read() self.assertEqual( org, new, "%r .glif file differs after round tripping" % glyphName)
def testGetUnicodes(self): src = GlyphSet(GLYPHSETDIR) unicodes = src.getUnicodes() for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs if not hasattr(g, "unicodes"): self.assertEqual(unicodes[glyphName], []) else: self.assertEqual(g.unicodes, unicodes[glyphName])
def testGetUnicodes(self): src = GlyphSet(GLYPHSETDIR) unicodes = src.getUnicodes() for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs if not hasattr(g, "unicodes"): self.assertEqual(unicodes[glyphName], []) else: self.assertEqual(g.unicodes, unicodes[glyphName])
def testGuessSmoothPen(self): glyphSet = GlyphSet(getDemoFontGlyphSetPath()) for name in glyphSet.keys(): digestPen = DigestPointPen() glyphSet[name].drawPoints(digestPen) digest1 = digestPen.getDigest() digestPen = DigestPointPen() pen = GuessSmoothPointPen(digestPen) glyphSet[name].drawPoints(pen) digest2 = digestPen.getDigest() self.assertEqual(digest1, digest2)
def testGuessSmoothPen(self): glyphSet = GlyphSet(getDemoFontGlyphSetPath()) for name in glyphSet.keys(): digestPen = DigestPointPen() glyphSet[name].drawPoints(digestPen) digest1 = digestPen.getDigest() digestPen = DigestPointPen() pen = GuessSmoothPointPen(digestPen) glyphSet[name].drawPoints(pen) digest2 = digestPen.getDigest() self.assertEqual(digest1, digest2)
def testCustomFileNamingScheme(self): def myGlyphNameToFileName(glyphName, glyphSet): return "prefix" + glyphNameToFileName(glyphName, glyphSet) src = GlyphSet(GLYPHSETDIR) dst = GlyphSet(self.dstDir, myGlyphNameToFileName) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) d = {} for k, v in src.contents.items(): print k, v d[k] = "prefix" + v self.assertEqual(d, dst.contents)
def testReverseContents2(self): src = GlyphSet(GLYPHSETDIR) dst = GlyphSet(self.dstDir) dstMap = dst.getReverseContents() self.assertEqual(dstMap, {}) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) self.assertNotEqual(dstMap, {}) srcMap = dict(src.getReverseContents()) # copy self.assertEqual(dstMap, srcMap) del srcMap["a.glif"] dst.deleteGlyph("a") self.assertEqual(dstMap, srcMap)
def testCustomFileNamingScheme(self): def myGlyphNameToFileName(glyphName, glyphSet): return "prefix" + glyphNameToFileName(glyphName, glyphSet) src = GlyphSet(GLYPHSETDIR) dst = GlyphSet(self.dstDir, myGlyphNameToFileName) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) d = {} for k, v in src.contents.items(): print k, v d[k] = "prefix" + v self.assertEqual(d, dst.contents)
def testReverseContents2(self): src = GlyphSet(GLYPHSETDIR) dst = GlyphSet(self.dstDir) dstMap = dst.getReverseContents() self.assertEqual(dstMap, {}) for glyphName in src.keys(): g = src[glyphName] g.drawPoints(None) # load attrs dst.writeGlyph(glyphName, g, g.drawPoints) self.assertNotEqual(dstMap, {}) srcMap = dict(src.getReverseContents()) # copy self.assertEqual(dstMap, srcMap) del srcMap["a.glif"] dst.deleteGlyph("a") self.assertEqual(dstMap, srcMap)
def testShapesFromGlyphSet(self): glyphSet = GlyphSet(getDemoFontGlyphSetPath()) for name in glyphSet.keys(): self._doTest(glyphSet[name].drawPoints, name)
def importAllGlifFiles(font, dirName=None, doProgress=True, bar=None): """import all GLIFs into a FontLab font""" if dirName is None: if font.file_name: dir, base = os.path.split(font.file_name) base = base.split(".")[0] + ".glyphs" dirName = os.path.join(dir, base) else: dirName = GetFolder("Please select a folder with .glif files") glyphSet = GlyphSet(dirName) glyphNames = glyphSet.keys() glyphNames.sort() barStart = 0 closeBar = False if doProgress: if not bar: bar = ProgressBar("Importing Glyphs", len(glyphNames)) closeBar = True else: barStart = bar.getCurrentTick() else: bar = None try: for i in range(len(glyphNames)): #if not (i % 10) and not bar.tick(barStart + i): # raise KeyboardInterrupt glyphName = glyphNames[i] flGlyph = NewGlyph(font, glyphName, clear=True) pen = FLPointPen(flGlyph) glyph = GlyphPlaceholder() glyphSet.readGlyph(glyphName, glyph, pen) if hasattr(glyph, "width"): flGlyph.width = int(round(glyph.width)) if hasattr(glyph, "unicodes"): flGlyph.unicodes = glyph.unicodes if hasattr(glyph, "note"): flGlyph.note = glyph.note # XXX must encode if hasattr(glyph, "lib"): from cStringIO import StringIO from robofab.plistlib import writePlist lib = glyph.lib if lib: if len(lib ) == 1 and "org.robofab.fontlab.customdata" in lib: data = lib["org.robofab.fontlab.customdata"].data else: f = StringIO() writePlist(glyph.lib, f) data = f.getvalue() flGlyph.customdata = data # XXX the next bit is only correct when font is the current font :-( fl.UpdateGlyph(font.FindGlyph(glyphName)) if bar and not i % 10: bar.tick(barStart + i) except KeyboardInterrupt: if bar: bar.close() bar = None fl.UpdateFont(FontIndex(font)) if bar and closeBar: bar.close()
def testShapesFromGlyphSet(self): glyphSet = GlyphSet(getDemoFontGlyphSetPath()) for name in glyphSet.keys(): self._doTest(glyphSet[name].drawPoints, name)
"""Read all glyphs from the demo font, and write them out again. This is useful for testing round-tripping stability, but also to update the font when the GLIF format changes. The third application is to update the contents.plist file in case glyphs have been added or removed. """ import os from robofab.test.testSupport import getDemoFontPath from robofab.glifLib import GlyphSet from robofab.pens.adapterPens import GuessSmoothPointPen ufoPath = getDemoFontPath() glyphSet = GlyphSet(os.path.join(ufoPath, "glyphs")) glyphSet.rebuildContents( ) # ignore existing contents.plist, rebuild from dir listing for name in glyphSet.keys(): g = glyphSet[name] g.drawPoints(None) # force all attrs to be loaded def drawPoints(pen): pen = GuessSmoothPointPen(pen) g.drawPoints(pen) glyphSet.writeGlyph(name, g, drawPoints) glyphSet.writeContents() # write out contents.plist
"""Read all glyphs from the demo font, and write them out again. This is useful for testing round-tripping stability, but also to update the font when the GLIF format changes. The third application is to update the contents.plist file in case glyphs have been added or removed. """ import os from robofab.test.testSupport import getDemoFontPath from robofab.glifLib import GlyphSet from robofab.pens.adapterPens import GuessSmoothPointPen ufoPath = getDemoFontPath() glyphSet = GlyphSet(os.path.join(ufoPath, "glyphs")) glyphSet.rebuildContents() # ignore existing contents.plist, rebuild from dir listing for name in glyphSet.keys(): g = glyphSet[name] g.drawPoints(None) # force all attrs to be loaded def drawPoints(pen): pen = GuessSmoothPointPen(pen) g.drawPoints(pen) glyphSet.writeGlyph(name, g, drawPoints) glyphSet.writeContents() # write out contents.plist