def readaps(self, filename, omitaps='', params=None): omittedaps = set(omitaps.replace( ',', ' ').split()) # allow comma- and/or space-separated list if filename.endswith('.ufo'): f = ufo.Ufont(filename, params=params) self.fontinfo = f.fontinfo for g in f.deflayer: ufo_g = f.deflayer[g] advb = ufo_g['advance'] adv = advb.width if advb is not None else 0 bbox = getbbox(ufo_g) glyph = Glyph(g, advance=adv, bbox=bbox) self.glyphs[g] = glyph if 'anchor' in ufo_g._contents: for a in ufo_g._contents['anchor']: if a.element.attrib['name'] not in omittedaps: glyph.add_anchor(a.element.attrib) self.all_aps.setdefault(a.element.attrib['name'], []).append(glyph) elif filename.endswith('.xml'): currGlyph = None currPoint = None self.fontinfo = {} for event, elem in et.iterparse(filename, events=('start', 'end')): if event == 'start': if elem.tag == 'glyph': name = elem.get('PSName', '') if name: currGlyph = Glyph(name) self.glyphs[name] = currGlyph currPoint = None elif elem.tag == 'point': currPoint = {'name': elem.get('type', '')} elif elem.tag == 'location' and currPoint is not None: currPoint['x'] = int(elem.get('x', 0)) currPoint['y'] = int(elem.get('y', 0)) elif elem.tag == 'font': n = elem.get('name', '') x = n.split('-') if len(x) == 2: self.fontinfo['familyName'] = x[0] self.fontinfo[ 'openTypeNamePreferredFamilyName'] = x[0] self.fontinfo['styleMapFamilyName'] = x[0] self.fontinfo['styleName'] = x[1] self.fontinfo[ 'openTypeNamePreferredSubfamilyName'] = x[1] self.fontinfo[ 'postscriptFullName'] = "{0} {1}".format(*x) self.fontinfo['postscriptFontName'] = n elif event == 'end': if elem.tag == 'point': if currGlyph and currPoint['name'] not in omittedaps: currGlyph.add_anchor(currPoint) self.all_aps.setdefault(currPoint['name'], []).append(currGlyph) currPoint = None elif elem.tag == 'glyph': currGlyph = None
def doit(args): psffont = UFO.Ufont(args.ifont, params=args.paramsobj) rffont = OpenFont(args.ifont) outfont = args.ofont getParameters(psffont) updateTLPieces(rffont) rebuildTLComps(rffont) rffont.save(outfont) return
def read_font(self, ufo_nm): self.file_nm = ufo_nm ufo_f = ufo.Ufont(ufo_nm) for g_name in ufo_f.deflayer: glyph = Glyph(g_name) self.glyphs[g_name] = glyph ufo_g = ufo_f.deflayer[g_name] unicode_lst = ufo_g['unicode'] if unicode_lst: # store primary encoding, allow for double encoding self.unicodes.setdefault(unicode_lst[0].hex, []).append(glyph) if 'anchor' in ufo_g: for anchor in ufo_g['anchor']: a_attr = anchor.element.attrib glyph.add_anchor(a_attr['name'], int(float(a_attr['x'])), int(float(a_attr['y'])))
def openfont(params, path, family, style) : # Only try if directory esists ufodir = os.path.join(path,family+"-"+style+".ufo") font = UFO.Ufont(ufodir, params=params) if os.path.isdir(ufodir) else None return font