def update(self): glyphs_dict = {} font = TTFont(font_VTT_source) glyphs = font.getGlyphOrder() for glyph in glyphs: glyphs_dict[glyph] = [font.getGlyphID(glyph)] font = TTFont(font_new) glyphs = font.getGlyphOrder() for glyph in glyphs: if glyph in glyphs_dict: glyphs_dict[glyph].append(font.getGlyphID(glyph)) id_dict = {v[0]: v[1] for v in glyphs_dict.values() if len(v) == 2} root = self.tree.find("glyf") for child in root: talk = child.find("instructions//talk").text if talk != None: glyph_id = child.attrib["ID"] new_id = id_dict[int(glyph_id)] child.set("ID", str(id_dict[int(glyph_id)])) assembly = child.find("instructions//assembly").text assembly_content = [] if assembly: for line in assembly.split("\n"): if line.startswith("OFFSET[R]"): line = line.split(",") line[1] = " %s" % (id_dict[int(line[1])]) line = ",".join(line) print(line) assembly_content.append(line) child.find("instructions//assembly").text = "\n".join( assembly_content)
def flattenGlyphs(input, fontNumber, output): font = TTFont(input, fontNumber=fontNumber) font.recalcBBoxes = False if "glyf" in font: for glyphName in font.getGlyphOrder(): glyph = font["glyf"][glyphName] coordinates, endPtsOfContours, flags = glyph.getCoordinates( font["glyf"]) glyph.numberOfContours = len(endPtsOfContours) glyph.coordinates = coordinates glyph.endPtsOfContours = endPtsOfContours glyph.flags = flags glyph.program = ttProgram.Program() font["glyf"][glyphName] = glyph elif "CFF " in font: cff = font["CFF "] fontName = cff.cff.fontNames[0] topDict = cff.cff[fontName] for glyphID in range(len(font.getGlyphOrder())): charString = topDict.CharStrings.charStringsIndex[glyphID] charString.decompile() localSubrs = getattr(charString.private, "Subrs", []) globalSubrs = charString.globalSubrs inlinedProgram = inlineProgram(localSubrs, globalSubrs, charString.program) charString.program = inlinedProgram if "Private" in topDict.rawDict and "Subrs" in topDict.Private.rawDict: topDict.Private.Subrs del topDict.Private.rawDict["Subrs"] del topDict.Private.Subrs topDict.GlobalSubrs.items = [] else: raise FlattenError("Could not flatten glyphs.") font.save(output)
def removeOverlaps( font: ttFont.TTFont, glyphNames: Optional[Iterable[str]] = None, removeHinting: bool = True, ) -> None: """Simplify glyphs in TTFont by merging overlapping contours. Overlapping components are first decomposed to simple contours, then merged. Currently this only works with TrueType fonts with 'glyf' table. Raises NotImplementedError if 'glyf' table is absent. Note that removing overlaps invalidates the hinting. By default we drop hinting from all glyphs whether or not overlaps are removed from a given one, as it would look weird if only some glyphs are left (un)hinted. Args: font: input TTFont object, modified in place. glyphNames: optional iterable of glyph names (str) to remove overlaps from. By default, all glyphs in the font are processed. removeHinting (bool): set to False to keep hinting for unmodified glyphs. """ try: glyfTable = font["glyf"] except KeyError: raise NotImplementedError( "removeOverlaps currently only works with TTFs") hmtxTable = font["hmtx"] # wraps the underlying glyf Glyphs, takes care of interfacing with drawing pens glyphSet = font.getGlyphSet() if glyphNames is None: glyphNames = font.getGlyphOrder() # process all simple glyphs first, then composites with increasing component depth, # so that by the time we test for component intersections the respective base glyphs # have already been simplified glyphNames = sorted( glyphNames, key=lambda name: ( glyfTable[name].getCompositeMaxpValues(glyfTable).maxComponentDepth if glyfTable[name].isComposite() else 0, name, ), ) modified = set() for glyphName in glyphNames: if removeTTGlyphOverlaps(glyphName, glyphSet, glyfTable, hmtxTable, removeHinting): modified.add(glyphName) log.debug("Removed overlaps for %s glyphs:\n%s", len(modified), " ".join(modified))
def reorderFont(input, fontNumber, desiredGlyphOrder, output): font = TTFont(input, fontNumber=fontNumber, lazy=False) font = fullyLoadFont(font) if "CFF " in font: cff = font["CFF "] fontName = cff.cff.fontNames[0] topDict = cff.cff[fontName] topDict.compilerClass = ReorderedTopDictCompiler glyphOrder = font.getGlyphOrder() reorderedGlyphs = reorderGlyphs(glyphOrder, desiredGlyphOrder) if reorderedGlyphs is None: return False font.setGlyphOrder(reorderedGlyphs) # Glyph order is cached in a few places, clear those out. if "glyf" in font: del font["glyf"].glyphOrder if hasattr(font, "_reverseGlyphOrderDict"): del font._reverseGlyphOrderDict # CFF stores glyph order internally, set it: if "CFF " in font: cff = font["CFF "] fontName = cff.cff.fontNames[0] topDict = cff.cff[fontName] topDict.charset = reorderedGlyphs fixLayoutCoverage(font) tmp = BytesIO() font.save(tmp) tableOrder = font.reader.keys() success = computeTableOrder(tableOrder) if not success: tmp.close() return False outputStream = open(output, "wb") reorderFontTables(tmp, outputStream, tableOrder) tmp.close() outputStream.close() return True
FONT = { "uniE723": "0", "uniF4EF": "3", "uniF259": "7", "uniED7A": "5", "uniF202": "2", "uniE99E": "4", "uniEA38": "9", "uniED43": '1', "uniE153": '8', "uniE1DC": '6' } dic = { font['glyf'][i].coordinates.array.tobytes().hex(): FONT[i] for i in font.getGlyphOrder()[2:] } # 下载当前页面woff文件 def get_cookies(cookies_string): cookies_string = '; ' + cookies_string + ';' keys = re.findall('; (.*?)=', cookies_string) values = re.findall('=(.*?);', cookies_string) cookies = dict(zip(keys, values)) return cookies cookies_str = '__mta=46011163.1586164010929.1586518522667.1586522393594.17; uuid_n_v=v1; uuid=3A8EEC5077E611EAB94CB3AA241DB1BC2B262789B09744949DDABB9017794081; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_cuid=1714ebe0c91c8-0df0ece2c085eb-3a36510f-144000-1714ebe0c91c8; _lxsdk=3A8EEC5077E611EAB94CB3AA241DB1BC2B262789B09744949DDABB9017794081; mojo-uuid=841eeb4e30d6c9694e330b8659315f92; __mta=46011163.1586164010929.1586164010929.1586440736365.2; _csrf=705df182c262270ac02e678ed0a7a9c3cc8eb7d4a9285bd5a6cb1f9a1458ea04; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1586164010,1586437425,1586518481; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1586522393; _lxsdk_s=171645279d4-3f5-3b3-422%7C%7C1' cookies = get_cookies(cookies_str) user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'