def test_parse_empty_glyphs(self): # data = '({glyphname="A";})' data = '({})' parser = Parser(GSGlyph) result = parser.parse(data) self.assertEqual(len(result), 1) glyph = result[0] self.assertIsInstance(glyph, GSGlyph) defaults_as_none = [ "category", "color", "lastChange", "leftKerningGroup", "leftMetricsKey", "name", "note", "rightKerningGroup", "rightMetricsKey", "script", "subCategory", "unicode", "widthMetricsKey", ] for attr in defaults_as_none: self.assertIsNone(getattr(glyph, attr)) self.assertIsNotNone(glyph.userData) defaults_as_true = [ "export", ] for attr in defaults_as_true: self.assertTrue(getattr(glyph, attr))
def loads(value): """Read a .glyphs file from a bytes object. Return the unpacked root object (an ordered dictionary). """ p = Parser() print('>>> Parsing .glyphs file') data = p.parse(value) print('>>> Casting parsed values') cast_data(data) return data
def loads(value, dict_type=dict): """Read a .glyphs file from a bytes object. Return the unpacked root object (which usually is a dictionary). """ p = Parser(dict_type=dict_type) print('>>> Parsing .glyphs file') data = p.parse(value) print('>>> Casting parsed values') cast_data(data) return data
def loads(value): """Read a .glyphs file from a bytes object. Return the unpacked root object (an ordered dictionary). """ p = Parser() logger.info('Parsing .glyphs file') data = p.parse(value) logger.info('Casting parsed values') cast_data(data) return data
def test_parse_glyphs(self): data = GLYPH_DATA parser = Parser(GSGlyph) result = parser.parse(data) glyph = result[0] self.assertEqual(glyph.name, "A") self.assertEqual(glyph.color, 5) self.assertEqual(glyph.lastChange, datetime.datetime(2017, 4, 30, 13, 57, 4)) self.assertEqual(glyph.leftKerningGroup, "A") self.assertEqual(glyph.rightKerningGroup, "A") self.assertEqual(glyph.unicode, "0041")
def _load(path: str) -> GSFont: '''Load `.glyphspackage` bundle. See [googlefonts/glyphsLib#643](https://github.com/googlefonts/glyphsLib/issues/643). ''' with open(os.path.join(path, 'fontinfo.plist'), 'r') as fontinfo_plist: fontinfo = fontinfo_plist.read() with open(os.path.join(path, 'order.plist'), 'r') as order_plist: order = Parser().parse(order_plist.read()) insert_pos = fontinfo.find('instances = (') glyphs = ',\n'.join(Font._read_glyph(path, name) for name in order) glyphs = f'glyphs = (\n{glyphs}\n);\n' return glyphsLib.loads(fontinfo[:insert_pos] + glyphs + fontinfo[insert_pos:-1])
def readGlyphsFile(filePath): print "Import Glyphs File" pool = None try: from Foundation import NSAutoreleasePool, NSDictionary except ImportError: # on Windows, PyObjC is not available with open(filePath, 'rb') as f: data = f.read() if data.startswith("<?xml"): # use the built-in plistlib module for XML plist from plistlib import readPlistFromString GlyphsDoc = readPlistFromString(data) else: # use glyphsLib's Parser for ASCII plist. # Download it from: https://github.com/googlei18n/glyphsLib from glyphsLib.parser import Parser GlyphsDoc = Parser(dict_type=dict).parse(data) else: # on OS X, use NSDictionary pool = NSAutoreleasePool.alloc().init() GlyphsDoc = NSDictionary.alloc().initWithContentsOfFile_(filePath) if not GlyphsDoc: print "Could not load .glyphs file." if pool: pool.drain() return from FL import fl, Font folder, base = os.path.split(filePath) base = base.replace(".glyphs", ".vfb") dest = os.path.join(folder, base) f = Font( ) fl.Add(f) global convertName try: convertName = GlyphsDoc["disablesNiceNames"] != None except: pass if not setFontInfo(f, GlyphsDoc): return False readGlyphs(f, GlyphsDoc) readKerning(f, GlyphsDoc) setLegacyNames(f) readFeatures(f, GlyphsDoc) fl.UpdateFont() f.modified = 0 if pool: pool.drain()
def test_write_node(self): node = classes.GSNode(Point(10, 30), classes.GSNode.CURVE) # http://docu.glyphsapp.com/#gsnode # position: already set # #type: already set # smooth node.smooth = True # connection: deprecated # selected: not written # index, nextNode, prevNode: computed # name node.name = "top-left corner" # userData node.userData["rememberToDownloadARealRemindersApp"] = True self.assertWritesValue( node, '"10 30 CURVE SMOOTH {name = \\"top-left corner\\";\\n\ rememberToDownloadARealRemindersApp = 1;}"', ) # Write floating point coordinates node = classes.GSNode(Point(499.99, 512.01), classes.GSNode.OFFCURVE) self.assertWritesValue(node, '"499.99 512.01 OFFCURVE"') # Write userData with special characters test_user_data = { "\nkey\"';\n\n\n": "\"'value\nseveral lines\n;\n", ";": ";\n", "escapeception": "\\\"\\'\\n\\\\n", } node = classes.GSNode(Point(130, 431), classes.GSNode.LINE) for key, value in test_user_data.items(): node.userData[key] = value # This is the output of Glyphs 1089 expected_output = ( '"130 431 LINE {\\"\\012key\\\\"\';\\012\\012\\012\\" ' '= \\"\\\\"\'value\\012several lines\\012;\\012\\"' ';\\n\\";\\" = \\";\\012\\";\\n' 'escapeception = \\"\\\\\\\\"\\\\\'\\\\\\n\\\\\\\\\\n\\";}"' ) self.assertWritesValue(node, expected_output) # Check that we can read the userData back node = Parser(classes.GSNode).parse(expected_output) self.assertEqual(test_user_data, dict(node.userData))
def run_test(self, text, expected): parser = Parser() self.assertEqual(parser.parse(text), collections.OrderedDict(expected))
def parse(fp, dict_type=dict): from glyphsLib.parser import Parser s = fp.read() p = Parser(current_type=dict_type) return p.parse(s)