def testWrite(self): writer = UFOWriter(self.dstDir, formatVersion=2) writer.setKerningGroupConversionRenameMaps(self.downConversionMapping) writer.writeKerning(self.kerning) writer.writeGroups(self.groups) # test groups path = os.path.join(self.dstDir, "groups.plist") with open(path, "rb") as f: writtenGroups = readPlist(f) self.assertEqual(writtenGroups, self.expectedWrittenGroups) # test kerning path = os.path.join(self.dstDir, "kerning.plist") with open(path, "rb") as f: writtenKerning = readPlist(f) self.assertEqual(writtenKerning, self.expectedWrittenKerning) self.tearDownUFO()
def readPlist(self): path = os.path.join(self.dstDir, "fontinfo.plist") with open(path, "rb") as f: plist = readPlist(f) return plist
def compareFileStructures(self, path1, path2, expectedInfoData, testFeatures): # result metainfoPath1 = os.path.join(path1, "metainfo.plist") fontinfoPath1 = os.path.join(path1, "fontinfo.plist") kerningPath1 = os.path.join(path1, "kerning.plist") groupsPath1 = os.path.join(path1, "groups.plist") libPath1 = os.path.join(path1, "lib.plist") featuresPath1 = os.path.join(path1, "features.plist") glyphsPath1 = os.path.join(path1, "glyphs") glyphsPath1_contents = os.path.join(glyphsPath1, "contents.plist") glyphsPath1_A = os.path.join(glyphsPath1, "A_.glif") glyphsPath1_B = os.path.join(glyphsPath1, "B_.glif") # expected result metainfoPath2 = os.path.join(path2, "metainfo.plist") fontinfoPath2 = os.path.join(path2, "fontinfo.plist") kerningPath2 = os.path.join(path2, "kerning.plist") groupsPath2 = os.path.join(path2, "groups.plist") libPath2 = os.path.join(path2, "lib.plist") featuresPath2 = os.path.join(path2, "features.plist") glyphsPath2 = os.path.join(path2, "glyphs") glyphsPath2_contents = os.path.join(glyphsPath2, "contents.plist") glyphsPath2_A = os.path.join(glyphsPath2, "A_.glif") glyphsPath2_B = os.path.join(glyphsPath2, "B_.glif") # look for existence self.assertEqual(os.path.exists(metainfoPath1), True) self.assertEqual(os.path.exists(fontinfoPath1), True) self.assertEqual(os.path.exists(kerningPath1), True) self.assertEqual(os.path.exists(groupsPath1), True) self.assertEqual(os.path.exists(libPath1), True) self.assertEqual(os.path.exists(glyphsPath1), True) self.assertEqual(os.path.exists(glyphsPath1_contents), True) self.assertEqual(os.path.exists(glyphsPath1_A), True) self.assertEqual(os.path.exists(glyphsPath1_B), True) if testFeatures: self.assertEqual(os.path.exists(featuresPath1), True) # look for aggrement with open(metainfoPath1, "rb") as f: data1 = readPlist(f) with open(metainfoPath2, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2) with open(fontinfoPath1, "rb") as f: data1 = readPlist(f) self.assertEqual(sorted(data1.items()), sorted(expectedInfoData.items())) with open(kerningPath1, "rb") as f: data1 = readPlist(f) with open(kerningPath2, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2) with open(groupsPath1, "rb") as f: data1 = readPlist(f) with open(groupsPath2, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2) with open(libPath1, "rb") as f: data1 = readPlist(f) with open(libPath2, "rb") as f: data2 = readPlist(f) if "UFO1" in libPath1: for key in removeFromFormatVersion1Lib: if key in data1: del data1[key] if "UFO1" in libPath2: for key in removeFromFormatVersion1Lib: if key in data2: del data2[key] self.assertEqual(data1, data2) with open(glyphsPath1_contents, "rb") as f: data1 = readPlist(f) with open(glyphsPath2_contents, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2) with open(glyphsPath1_A, "rb") as f: data1 = readPlist(f) with open(glyphsPath2_A, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2) with open(glyphsPath1_B, "rb") as f: data1 = readPlist(f) with open(glyphsPath2_B, "rb") as f: data2 = readPlist(f) self.assertEqual(data1, data2)
def test_readPlist_from_file(pl): with open(os.path.join(datadir, "test.plist"), "rb") as f: pl2 = plistlib.readPlist(f) assert isinstance(pl2["someData"], plistlib.Data) assert pl2 == pl assert not f.closed
def test_readPlist_from_path(pl): path = os.path.join(datadir, "test.plist") pl2 = plistlib.readPlist(path) assert isinstance(pl2["someData"], plistlib.Data) assert pl2 == pl
def main(args=None): from io import open options = parse_args(args) config = getConfig(options.config) svg_file = options.infile # Parse SVG to read the width, height attributes defined in it svgObj = parseSvg(svg_file) width = float(svgObj.attrib['width'].replace("px", " ")) height = float(svgObj.attrib['height'].replace("px", " ")) name = os.path.splitext(os.path.basename(svg_file))[0] ufo_font_path = config['font']['ufo'] # Get the font metadata from UFO reader = UFOReader(ufo_font_path) writer = UFOWriter(ufo_font_path) infoObject = InfoObject() reader.readInfo(infoObject) fontName = config['font']['name'] # Get the configuration for this svg try: svg_config = config['svgs'][name] except KeyError: print("\033[93mSkip: Configuration not found for svg : %r\033[0m" % name) return if 'unicode' in svg_config: unicodeVal = unicode_hex_list(svg_config['unicode']) else: unicodeVal = None glyphWidth = width + int(svg_config['left']) + int(svg_config['right']) if glyphWidth < 0: raise UFOLibError("Glyph %s has negative width." % name) contentsPlistPath = ufo_font_path + '/glyphs/contents.plist' try: with open(contentsPlistPath, "rb") as f: contentsPlist = readPlist(f) except: raise UFOLibError("The file %s could not be read." % contentsPlistPath) glyph_name = svg_config['glyph_name'] # Replace all capital letters with a following '_' to avoid file name clash in Windows glyph_file_name = re.sub(r'([A-Z]){1}', lambda pat: pat.group(1) + '_', glyph_name) + '.glif' if glyph_name in contentsPlist: existing_glyph = True else: existing_glyph = False # Calculate the transformation to do transform = transform_list(config['font']['transform']) base = 0 if 'base' in svg_config: base = int(svg_config['base']) transform[4] += int(svg_config['left']) # X offset = left bearing transform[5] += height + base # Y offset glif = svg2glif(svg_file, name=svg_config['glyph_name'], width=glyphWidth, height=getattr(infoObject, 'unitsPerEm'), unicodes=unicodeVal, transform=transform, version=config['font']['version']) if options.outfile is None: output_file = ufo_font_path + '/glyphs/' + glyph_file_name else: output_file = options.outfile with open(output_file, 'w', encoding='utf-8') as f: f.write(glif) print( "\033[94m[%s]\033[0m \033[92mConvert\033[0m %s -> %s \033[92m✔️\033[0m" % (fontName, name, output_file)) # If this is a new glyph, add it to the UFO/glyphs/contents.plist if not existing_glyph: contentsPlist[glyph_name] = glyph_file_name writePlistAtomically(contentsPlist, contentsPlistPath) print( "\033[94m[%s]\033[0m \033[92mAdd\033[0m %s -> %s \033[92m✔️\033[0m" % (fontName, glyph_name, glyph_file_name)) lib_obj = reader.readLib() lib_obj['public.glyphOrder'].append(glyph_name) writer.writeLib(lib_obj)