)EjNBddlZddlmZddlmZGddeZdS)N)MissingComponentError)AbstractPointPencVeZdZdZd dZedZd dZdZ d d Z d d Z dS) HashPointPena5 This pen can be used to check if a glyph's contents (outlines plus components) have changed. Components are added as the original outline plus each composite's transformation. Example: You have some TrueType hinting code for a glyph which you want to compile. The hinting code specifies a hash value computed with HashPointPen that was valid for the glyph's outlines at the time the hinting code was written. Now you can calculate the hash for the glyph's current outlines to check if the outlines have changed, which would probably make the hinting code invalid. > glyph = ufo[name] > hash_pen = HashPointPen(glyph.width, ufo) > glyph.drawPoints(hash_pen) > ttdata = glyph.lib.get("public.truetype.instructions", None) > stored_hash = ttdata.get("id", None) # The hash is stored in the "id" key > if stored_hash is None or stored_hash != hash_pen.hash: > logger.error(f"Glyph hash mismatch, glyph '{name}' will have no instructions in font.") > else: > # The hash values are identical, the outline has not changed. > # Compile the hinting code ... > pass If you want to compare a glyph from a source format which supports floating point coordinates and transformations against a glyph from a format which has restrictions on the precision of floats, e.g. UFO vs. TTF, you must use an appropriate rounding function to make the values comparable. For TTF fonts with composites, this construct can be used to make the transform values conform to F2Dot14: > ttf_hash_pen = HashPointPen(ttf_glyph_width, ttFont.getGlyphSet()) > ttf_round_pen = RoundingPointPen(ttf_hash_pen, transformRoundFunc=partial(floatToFixedToFloat, precisionBits=14)) > ufo_hash_pen = HashPointPen(ufo_glyph.width, ufo) > ttf_glyph.drawPoints(ttf_round_pen, ttFont["glyf"]) > ufo_round_pen = RoundingPointPen(ufo_hash_pen, transformRoundFunc=partial(floatToFixedToFloat, precisionBits=14)) > ufo_glyph.drawPoints(ufo_round_pen) > assert ttf_hash_pen.hash == ufo_hash_pen.hash rNcF||_dt|dzg|_dS)Nzw%s )glyphsetrounddata)self glyphWidthglyphSets MC:\PYTHON\MyICR_Suite\python\Lib\site-packages\fontTools/pens/hashPointPen.py__init__zHashPointPen.__init__2s&  U:q1112 cd|j}t|dkr9tj|d}|S)Nascii)joinr lenhashlibsha512encode hexdigest)r r s rhashzHashPointPen.hash6sRwwty!! t99  >$++g"6"677AACCD rc dSN)r identifierkwargss r beginPathzHashPointPen.beginPath=s rc:|jddS)N|r append)r s rendPathzHashPointPen.endPath@s rFc ||d}n|d}|j||dd|dddS)Norgz+gr%)r pt segmentTypesmoothnamer r!pt_types raddPointzHashPointPen.addPointCsT  GG!!nG G8RU88be88899999rc 2dd|D}|jd |j||n#t $rt |wxYw|jd|ddS)Nrcg|]}|dS)+r).0ts r z-HashPointPen.addComponent..Ss7771hh777r[(z)])rr r&r drawPointsKeyErrorr)r baseGlyphNametransformationr r!trs r addComponentzHashPointPen.addComponentRs WW77777 8 8  7 M- ( 3 3D 9 9 9 9 7 7 7' 66 6 7 R$$$$$s AA6)rNr)NFNN) __name__ __module__ __qualname____doc__rpropertyrr"r'r1r?rrrrrs''R3333X        : : : :%%%%%%rr)rfontTools.pens.basePenrfontTools.pens.pointPenrrrrrrGsq888888444444Q%Q%Q%Q%Q%#Q%Q%Q%Q%Q%r