def compareToUFO(self, doInfo=True, doKerning=True, doGroups=True, doLib=True, doFeatures=True): reader = UFOReader(self.ufoPath) results = {} if doInfo: infoMatches = True info = self.font.info for attr, expectedValue in fontInfoVersion2.items(): # cheat by skipping attrs that aren't supported if info._ufoToFLAttrMapping[attr]["nakedAttribute"] is None: continue writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"]= infoMatches if doKerning: kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning if doGroups: groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups if doFeatures: features = self.font.features.text expectedFeatures = reader.readFeatures() results["features"] = expectedFeatures == features if doLib: lib = dict(self.font.lib) expectedLib = reader.readLib() results["lib"] = expectedLib == lib return results
def extractFontFromUFO(pathOrFile, destination, doGlyphs=True, doInfo=True, doKerning=True, doGroups=True, doFeatures=True, doLib=True, customFunctions=[]): source = UFOReader(pathOrFile) if doInfo: source.readInfo(destination.info) if doKerning: kerning = source.readKerning() destination.kerning.update(kerning) if doGroups: groups = source.readGroups() destination.groups.update(groups) if doFeatures: features = source.readFeatures() destination.features.text = features if doLib: lib = source.readLib() destination.lib.update(lib) if doGlyphs: glyphSet = source.getGlyphSet() for glyphName in glyphSet.keys(): destination.newGlyph(glyphName) glyph = destination[glyphName] pointPen = glyph.getPointPen() glyphSet.readGlyph(glyphName=glyphName, glyphObject=glyph, pointPen=pointPen) for function in customFunctions: function(source, destination)
def compareToUFO(self, doInfo=True, doKerning=True, doGroups=True, doLib=True, doFeatures=True): reader = UFOReader(self.ufoPath) results = {} if doInfo: infoMatches = True info = self.font.info for attr, expectedValue in expectedFontInfo1To2Conversion.items(): writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"]= infoMatches if doKerning: kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning if doGroups: groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups if doFeatures: features = self.font.features.text expectedFeatures = expectedFormatVersion1Features # FontLab likes to add lines to the features, so skip blank lines. features = [line for line in features.splitlines() if line] expectedFeatures = [line for line in expectedFeatures.splitlines() if line] results["features"] = expectedFeatures == features if doLib: lib = dict(self.font.lib) expectedLib = reader.readLib() for key in removeFromFormatVersion1Lib: if key in expectedLib: del expectedLib[key] results["lib"] = expectedLib == lib return results
def compareToUFO(self, doInfo=True): reader = UFOReader(ufoPath2) results = {} # info infoMatches = True info = self.font.info for attr, expectedValue in fontInfoVersion2.items(): writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"]= infoMatches # kerning kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning # groups groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups # features features = self.font.features.text expectedFeatures = reader.readFeatures() results["features"] = expectedFeatures == features # lib lib = dict(self.font.lib) expectedLib = reader.readLib() results["lib"] = expectedLib == lib return results
def combineKerning(path1, path2, destPath, WORRY=False): """ This is a rather nasty tool, but it does the job. Give it two UFOs that you want to combine the kerning from. It'll go through, and combine the kerning of two UFOs. If WORRY is set to True, it will enforce a rule that one can't add together kerning that has groups which contain different members. It finishes by writing out a 'dummy' UFO, one that contains no glyphs, but only the groups and kerning info for the combined kerning. One must go in a open the UFO package to grab the kerning and put in into a UFO with the glyphs combined. This way, at least, the source UFOs are not touched, incase one needs to beat a retreat to the source UFOs. """ ufo1 = UFOReader(path1) groups1 = ufo1.readGroups() kerning1 = ufo1.readKerning() ufo2 = UFOReader(path2) groups2 = ufo2.readGroups() kerning2 = ufo2.readKerning() combinedKerning = dict(kerning1) for pair, value in kerning2.items(): if pair in combinedKerning: assert value == combinedKerning[pair] combinedKerning[pair] = value combinedGroups = {} for groups in (groups1, groups2): for name, group in groups.items(): if not name.startswith("@MMK_"): continue if name in combinedGroups and WORRY: assert group == combinedGroups[name] combinedGroups[name] = group else: combinedGroups[name] = group ufo = UFOWriter(destPath) ufo.writeKerning(combinedKerning) ufo.writeGroups(combinedGroups)
def _loadData(self, path): from robofab.ufoLib import UFOReader reader = UFOReader(path) fontLib = reader.readLib() # info reader.readInfo(self.info) # kerning self.kerning.update(reader.readKerning()) self.kerning.setChanged(False) # groups self.groups.update(reader.readGroups()) # features if reader.formatVersion == 1: # migrate features from the lib features = [] classes = fontLib.get("org.robofab.opentype.classes") if classes is not None: del fontLib["org.robofab.opentype.classes"] features.append(classes) splitFeatures = fontLib.get("org.robofab.opentype.features") if splitFeatures is not None: order = fontLib.get("org.robofab.opentype.featureorder") if order is None: order = splitFeatures.keys() order.sort() else: del fontLib["org.robofab.opentype.featureorder"] del fontLib["org.robofab.opentype.features"] for tag in order: oneFeature = splitFeatures.get(tag) if oneFeature is not None: features.append(oneFeature) features = "\n".join(features) else: features = reader.readFeatures() self.features.text = features # hint data self.psHints = PostScriptFontHintValues(self) if postScriptHintDataLibKey in fontLib: del fontLib[postScriptHintDataLibKey] # lib self.lib.update(fontLib) # glyphs self._glyphSet = reader.getGlyphSet() self._hasNotChanged(doGlyphs=False)
def compareToUFO(self, doInfo=True, doKerning=True, doGroups=True, doLib=True, doFeatures=True): reader = UFOReader(self.ufoPath) results = {} if doInfo: infoMatches = True info = self.font.info for attr, expectedValue in expectedFontInfo1To2Conversion.items(): writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"] = infoMatches if doKerning: kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning if doGroups: groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups if doFeatures: features = self.font.features.text expectedFeatures = expectedFormatVersion1Features # FontLab likes to add lines to the features, so skip blank lines. features = [line for line in features.splitlines() if line] expectedFeatures = [ line for line in expectedFeatures.splitlines() if line ] results["features"] = expectedFeatures == features if doLib: lib = dict(self.font.lib) expectedLib = reader.readLib() for key in removeFromFormatVersion1Lib: if key in expectedLib: del expectedLib[key] results["lib"] = expectedLib == lib return results
def compareToUFO(self, doInfo=True): reader = UFOReader(ufoPath1) results = {} # info infoMatches = True info = self.font.info for attr, expectedValue in expectedFontInfo1To2Conversion.items(): writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"]= infoMatches # kerning kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning # groups groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups # features features = self.font.features.text f = open(os.path.join(ufoPath2, "features.fea"), "r") expectedFeatures = f.read() f.close() match = True features = [line for line in features.splitlines() if line] expectedFeatures = [line for line in expectedFeatures.splitlines() if line] if expectedFeatures != features or reader.readFeatures() != "": match = False results["features"] = match # lib lib = dict(self.font.lib) expectedLib = reader.readLib() for key in removeFromFormatVersion1Lib: if key in expectedLib: del expectedLib[key] results["lib"] = expectedLib == lib return results
def compareToUFO(self, doInfo=True, doKerning=True, doGroups=True, doLib=True, doFeatures=True): reader = UFOReader(self.ufoPath) results = {} if doInfo: infoMatches = True info = self.font.info for attr, expectedValue in fontInfoVersion2.items(): # cheat by skipping attrs that aren't supported if info._ufoToFLAttrMapping[attr]["nakedAttribute"] is None: continue writtenValue = getattr(info, attr) if expectedValue != writtenValue: infoMatches = False break results["info"] = infoMatches if doKerning: kerning = self.font.kerning.asDict() expectedKerning = reader.readKerning() results["kerning"] = expectedKerning == kerning if doGroups: groups = dict(self.font.groups) expectedGroups = reader.readGroups() results["groups"] = expectedGroups == groups if doFeatures: features = self.font.features.text expectedFeatures = reader.readFeatures() results["features"] = expectedFeatures == features if doLib: lib = dict(self.font.lib) expectedLib = reader.readLib() results["lib"] = expectedLib == lib return results
def importKerningMMK(font, UFOfilepath): report = 'Import Kerning Report ' + asctime( ) + '\nUFO file: ' + UFOfilepath kernClasses = {} kern_L_R_table = {} kernTable = {} feaClasses = {} dicGroups = {} UFO = UFOReader(UFOfilepath) kernGroups = UFO.readGroups() kernTable = UFO.readKerning() # progress = ProgressBar('Converting group names', ticks = len(kernGroups.items())) for groupname, content in kernGroups.items(): # progress.tick() if len(content) != 0: if content[0] == '': content.pop(0) if groupname.startswith('@'): # @MMK_ = MM kern class classname = '_' + groupname[7:] baseGlyph = content[0] content[0] = baseGlyph + '\'' classcontent = content dicGroups[groupname] = baseGlyph if kernClasses.has_key(classname): if kernClasses[classname] == classcontent: kern_L_R_table[classname] = (True, True) else: classname = generateClassName(kernClasses, classname) if groupname[5] == 'L': # @MMK_L_namegroup kern_L_R_table[classname] = (True, False) elif groupname[5] == 'R': # @MMK_R_namegroup kern_L_R_table[classname] = (False, True) else: print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname else: if groupname[5] == 'L': # @MMK_L_namegroup kern_L_R_table[classname] = (True, False) elif groupname[5] == 'R': # @MMK_R_namegroup kern_L_R_table[classname] = (False, True) else: print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname kernClasses[classname] = classcontent else: # fea class feaClasses[groupname] = content #' '.join(content) else: print 'WARNING! Group with NULL content: %s Ignored.' % groupname report = report + '\n\nGroup with NULL content: %s Ignored.' % groupname # progress.close() # progress = ProgressBar('Merging kerning and fea-classes',ticks = len(kernClasses.items()) + len(feaClasses.items())) classes = {} for classname, content in kernClasses.items(): # progress.tick() classes[classname] = content #.split(' ') for classname, content in feaClasses.items(): # progress.tick() classes[classname] = content #.split(' ') # progress.close() report = report + diffGroups(font.groups, classes, kern_L_R_table) font.groups.clear() font.groups = classes font.update() # progress = ProgressBar('Left/Right identification', ticks = len(font.naked().classes)) for index, kernClass in enumerate(font.naked().classes): # progress.tick() if kernClass.startswith('_'): nameClass = kernClass.split(':')[0] leftBool, rightBool = kern_L_R_table[nameClass] font.naked().SetClassFlags(index, leftBool, rightBool) font.update() print '\nConverting Groups from MetricsMachine to Fontlab Classes: DONE\n' # progress.close() # progress = ProgressBar('Left pairs converting', ticks = len(kernTable.items())) new_kern1 = {} for (left, right), value in kernTable.items(): # progress.tick() if dicGroups.has_key(left): baseGlyph = dicGroups[left] new_kern1[(baseGlyph, right)] = value else: if left.startswith('@'): print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.' else: new_kern1[(left, right)] = value # progress.close() # progress = ProgressBar('Right pairs converting', ticks = len(new_kern1.items())) new_kern2 = {} for (left, right), value in new_kern1.items(): # progress.tick() if dicGroups.has_key(right): baseGlyph = dicGroups[right] new_kern2[(left, baseGlyph)] = value else: if right.startswith('@'): print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.' else: new_kern2[(left, right)] = value # progress.close() report = report + diffKerning(font.kerning, new_kern2) font.kerning.clear() font.kerning.update(new_kern2) font.update() reportfile = open(UFOfilepath.replace('.ufo', '.log'), 'w') reportfile.write(report) reportfile.close() print '\nConverting Kerning from MetricsMachine to Fontlab: DONE\n'
def importKerningMMK (font, UFOfilepath): report = 'Import Kerning Report ' + asctime() + '\nUFO file: ' + UFOfilepath kernClasses = {} kern_L_R_table = {} kernTable = {} feaClasses = {} dicGroups = {} UFO = UFOReader(UFOfilepath) kernGroups = UFO.readGroups() kernTable = UFO.readKerning() # progress = ProgressBar('Converting group names', ticks = len(kernGroups.items())) for groupname, content in kernGroups.items(): # progress.tick() if len(content) != 0: if content[0] == '': content.pop(0) if groupname.startswith('@'): # @MMK_ = MM kern class classname = '_' + groupname[7:] baseGlyph = content[0] content[0] = baseGlyph + '\'' classcontent = content dicGroups[groupname] = baseGlyph if kernClasses.has_key(classname): if kernClasses[classname] == classcontent: kern_L_R_table[classname] = (True, True) else: classname = generateClassName(kernClasses, classname) if groupname[5] == 'L': # @MMK_L_namegroup kern_L_R_table[classname] = (True, False) elif groupname[5] == 'R': # @MMK_R_namegroup kern_L_R_table[classname] = (False, True) else: print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname else: if groupname[5] == 'L': # @MMK_L_namegroup kern_L_R_table[classname] = (True, False) elif groupname[5] == 'R': # @MMK_R_namegroup kern_L_R_table[classname] = (False, True) else: print "WARNING! Something wrong whith LEFT/RIGHT identification...", groupname kernClasses[classname] = classcontent else: # fea class feaClasses[groupname] = content#' '.join(content) else: print 'WARNING! Group with NULL content: %s Ignored.' % groupname report = report + '\n\nGroup with NULL content: %s Ignored.' % groupname # progress.close() # progress = ProgressBar('Merging kerning and fea-classes',ticks = len(kernClasses.items()) + len(feaClasses.items())) classes = {} for classname, content in kernClasses.items(): # progress.tick() classes[classname] = content#.split(' ') for classname, content in feaClasses.items(): # progress.tick() classes[classname] = content#.split(' ') # progress.close() report = report + diffGroups(font.groups, classes, kern_L_R_table) font.groups.clear() font.groups = classes font.update() # progress = ProgressBar('Left/Right identification', ticks = len(font.naked().classes)) for index, kernClass in enumerate(font.naked().classes): # progress.tick() if kernClass.startswith('_'): nameClass = kernClass.split(':')[0] leftBool, rightBool = kern_L_R_table[nameClass] font.naked().SetClassFlags(index, leftBool, rightBool) font.update() print '\nConverting Groups from MetricsMachine to Fontlab Classes: DONE\n' # progress.close() # progress = ProgressBar('Left pairs converting', ticks = len(kernTable.items())) new_kern1 = {} for (left, right), value in kernTable.items(): # progress.tick() if dicGroups.has_key(left): baseGlyph = dicGroups[left] new_kern1[(baseGlyph, right)] = value else: if left.startswith('@'): print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.' else: new_kern1[(left, right)] = value # progress.close() # progress = ProgressBar('Right pairs converting', ticks = len(new_kern1.items())) new_kern2 = {} for (left, right), value in new_kern1.items(): # progress.tick() if dicGroups.has_key(right): baseGlyph = dicGroups[right] new_kern2[(left, baseGlyph)] = value else: if right.startswith('@'): print "WARNING! Something wrong with pair:", left, right, value, 'Ignored.' else: new_kern2[(left, right)] = value # progress.close() report = report + diffKerning(font.kerning, new_kern2) font.kerning.clear() font.kerning.update(new_kern2) font.update() reportfile = open(UFOfilepath.replace('.ufo', '.log'), 'w') reportfile.write(report) reportfile.close() print '\nConverting Kerning from MetricsMachine to Fontlab: DONE\n'