def readLWFN(path, onlyHeader=False): """reads an LWFN font file, returns raw data""" from fontTools.misc.macRes import ResourceReader reader = ResourceReader(path) try: data = [] for res in reader.get('POST', []): code = byteord(res.data[0]) if byteord(res.data[1]) != 0: raise T1Error('corrupt LWFN file') if code in [1, 2]: if onlyHeader and code == 2: break data.append(res.data[2:]) elif code in [3, 5]: break elif code == 4: f = open(path, "rb") data.append(f.read()) f.close() elif code == 0: pass # comment, ignore else: raise T1Error('bad chunk code: ' + repr(code)) finally: reader.close() data = bytesjoin(data) assertType1(data) return data
def readLWFN(path, onlyHeader=False): """reads an LWFN font file, returns raw data""" from fontTools.misc.macRes import ResourceReader reader = ResourceReader(path) try: data = [] for res in reader.get('POST', []): code = byteord(res.data[0]) if byteord(res.data[1]) != 0: raise T1Error('corrupt LWFN file') if code in [1, 2]: if onlyHeader and code == 2: break data.append(res.data[2:]) elif code in [3, 5]: break elif code == 4: with open(path, "rb") as f: data.append(f.read()) elif code == 0: pass # comment, ignore else: raise T1Error('bad chunk code: ' + repr(code)) finally: reader.close() data = bytesjoin(data) assertType1(data) return data
def getSFNTResIndices(path): """Determine whether a file has a 'sfnt' resource fork or not.""" try: reader = ResourceReader(path) indices = reader.getIndices('sfnt') reader.close() return indices except ResourceError: return []
def listFontGlyphNames(self): """Returns a list of glyph names supported by the current font.""" from fontTools.ttLib import TTFont, TTLibError from fontTools.misc.macRes import ResourceReader, ResourceError path = self.fontFilePath() if path is None: return [] try: # load the font with fontTools # provide a fontNumber as lots of fonts are .ttc font files. # search for the res_name_or_index for .dfont files. res_name_or_index = None fontNumber = None ext = os.path.splitext(path)[-1].lower() if ext == ".ttc": def _getPSName(source): # get PS name name = source["name"] psName = name.getName(6, 1, 0) if psName is None: psName.getName(6, 3, 1) return psName.toStr() ttc = TTFont(path, lazy=True, fontNumber=0) numFonts = ttc.reader.numFonts foundPSName = False for fontNumber in range(numFonts): source = TTFont(path, lazy=True, fontNumber=fontNumber) psName = _getPSName(source) if psName == self._font: foundPSName = True break if not foundPSName: # fallback to the first font in the ttc. fontNumber = 0 elif ext == ".dfont": try: reader = ResourceReader(path) names = reader.getNames("sfnt") if self._font in names: res_name_or_index = self._font else: res_name_or_index = names[0] except ResourceError: pass fontToolsFont = TTFont(path, lazy=True, fontNumber=fontNumber, res_name_or_index=res_name_or_index) except TTLibError: logger.warning("Cannot read the font file for '%s' at the path '%s'", self._font, path) return [] glyphNames = fontToolsFont.getGlyphOrder() fontToolsFont.close() # remove .notdef from glyph names if ".notdef" in glyphNames: glyphNames.remove(".notdef") return glyphNames
def test_read_datafork(self): with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(TEST_RSRC_FORK) try: reader = ResourceReader(tmp.name) resources = [res for typ in reader.keys() for res in reader[typ]] reader.close() self.assertExpected(resources) finally: os.remove(tmp.name)
def __init__(self, path, res_name_or_index): reader = ResourceReader(path) if isinstance(res_name_or_index, basestring): rsrc = reader.getNamedResource('sfnt', res_name_or_index) else: rsrc = reader.getIndResource('sfnt', res_name_or_index) if rsrc is None: raise ttLib.TTLibError("sfnt resource not found: %s" % res_name_or_index) reader.close() self.rsrc = rsrc super(SFNTResourceReader, self).__init__(rsrc.data) self.name = path
def _getTTFontFromSuitcase(path, psFontName, searchOrder=((1, 0), (3, 1))): # Support for .dfont and especially "regular" suitcases is minimal, # and we will not raise an error when the requested font isn't found, # but will return None. rr = ResourceReader(path) if "sfnt" not in rr: return None for index in rr.getIndices("sfnt"): font = TTFont(path, lazy=True, res_name_or_index=index) for platID, platEncID in searchOrder: nameRecord = font["name"].getName(6, platID, platEncID) if nameRecord is not None and str(nameRecord) == psFontName: return font return None
def test_read_namedfork_rsrc(self): if sys.platform != 'darwin': self.skipTest('Not supported on "%s"' % sys.platform) tmp = tempfile.NamedTemporaryFile(delete=False) tmp.close() try: with open(tmp.name + '/..namedfork/rsrc', 'wb') as fork: fork.write(TEST_RSRC_FORK) reader = ResourceReader(tmp.name) resources = [res for typ in reader.keys() for res in reader[typ]] reader.close() self.assertExpected(resources) finally: os.remove(tmp.name)
def __init__(self, path, res_name_or_index): reader = ResourceReader(path) if isinstance(res_name_or_index, basestring): rsrc = reader.getNamedResource('sfnt', res_name_or_index) else: rsrc = reader.getIndResource('sfnt', res_name_or_index) if rsrc is None: raise TTLibError("sfnt resource not found: %s" % res_name_or_index) reader.close() self.rsrc = rsrc super(SFNTResourceReader, self).__init__(rsrc.data) self.name = path
def test_read_file(self): infile = BytesIO(TEST_RSRC_FORK) reader = ResourceReader(infile) resources = [res for typ in reader.keys() for res in reader[typ]] self.assertExpected(resources)