
    jkeI
                     :    d dl Z d dlmZ d dlmZ  G d de      Zy)    N)MissingComponentError)AbstractPointPenc                   N    e Zd ZdZd	dZed        Zd
dZd Z	 	 	 	 ddZ	d
dZ
y)HashPointPena  
    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
    Nc                 <    || _         dt        |d      z  g| _        y )Nzw%s	   )glyphsetrounddata)self
glyphWidthglyphSets      =/usr/lib/python3/dist-packages/fontTools/pens/hashPointPen.py__init__zHashPointPen.__init__$   s     U:q112	    c                     dj                  | j                        }t        |      dk\  r2t        j                  |j                  d            j                         }|S )N    ascii)joinr   lenhashlibsha512encode	hexdigest)r   r   s     r   hashzHashPointPen.hash(   sE    wwtyy!t9>>$++g"67AACDr   c                      y N )r   
identifierkwargss      r   	beginPathzHashPointPen.beginPath/   s    r   c                 :    | j                   j                  d       y )N|r   append)r   s    r   endPathzHashPointPen.endPath2   s    		r   c                 j    |d}n|d   }| j                   j                  | |d   d|d   d       y )Nor   g   z+gr%   )r   ptsegmentTypesmoothnamer    r!   pt_types           r   addPointzHashPointPen.addPoint5   sC     G!!nG		G9RU1IbeBZ89r   c                 2   dj                  |D cg c]  }|d c}      }| j                  j                  d       	 | j                  |   j	                  |        | j                  j                  d| d       y c c}w # t
        $ r t        |      w xY w)Nr   +[(z)])r   r   r&   r	   
drawPointsKeyErrorr   )r   baseGlyphNametransformationr    r!   ttrs          r   addComponentzHashPointPen.addComponentD   s    WW711h78			7MM-(33D9 			1RD$ 8  	7'66	7s   A<B B)r   Nr   )NFNN)__name__
__module____qualname____doc__r   propertyr   r"   r'   r1   r<   r   r   r   r   r      sB    63   :%r   r   )r   fontTools.pens.basePenr   fontTools.pens.pointPenr   r   r   r   r   <module>rD      s     8 4C%# C%r   