
    }f                     P   d Z ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ d	 Zd
 Z G d d          Z G d de          Zd"dZd#dZ	 	 	 	 	 d$dededededededefdZ G d de          Z G d de          Z G d  d!e          ZdS )%zHelpers for writing unit tests.    )Iterable)BytesION)TestCase)Configtobytes)	XMLWriterc                    t                      }d}t          | t                    r|| z  }nt          | t                    r|t	          | d          z  }n\t          | t
                    r#|d                    d | D                       z  }n$t          dt          |           j	        z            |dz  }|j
                            |d           |j        d         S )	aA  Parses a snippet of XML.

    Input can be either a single string (unicode or UTF-8 bytes), or a
    a sequence of strings.

    The result is in the same format that would be returned by
    XMLReader, but the parser imposes no constraints on the root
    element so it can be called on small snippets of TTX files.
    s   <root>utf-8    c              3   6   K   | ]}t          |d           V  dS )r   Nr   ).0ss     Q/var/www/rtfcompare/venv/lib/python3.11/site-packages/fontTools/misc/testTools.py	<genexpr>zparseXML.<locals>.<genexpr>"   s,      @@7++@@@@@@r   z0expected string or sequence of strings; found %rs   </root>r      )TestXMLReader_
isinstancebytesstrr   r   join	TypeErrortype__name__parserParseroot)
xmlSnippetreaderxmls      r   parseXMLr!      s     F
C*e$$ 

z	J	$	$ 
wz7+++	J	)	) 
sxx@@Z@@@@@@>:'(
 
 	
 :C
MQ;q>r   c                     d t          |                                          D             }|D ]\  }}}|                    ||||            |                                 |S )Nc                 <    g | ]}t          |t                    |S  )r   r   )r   es     r   
<listcomp>z parseXmlInto.<locals>.<listcomp>.   s'    TTTAsASAST!TTTr   )r!   stripfromXMLpopulateDefaults)font	parseIntor   
parsed_xmlnameattrscontents          r   parseXmlIntor0   -   st    TTXj&6&6&8&899TTTJ * 6 6eW$w5555   r   c                   R    e Zd Zd Zd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zd ZdS )FakeFontc                     || _         d t          |          D             | _        d| _        i | _        t                      | _        d S )Nc                     i | ]\  }}||	S r$   r$   )r   igs      r   
<dictcomp>z%FakeFont.__init__.<locals>.<dictcomp>8   s    &J&J&J1q!&J&J&Jr   F)glyphOrder_	enumeratereverseGlyphOrderDict_lazytablesr   cfg)selfglyphss     r   __init__zFakeFont.__init__6   sE    !&J&J	&8I8I&J&J&J#	88r   c                     | j         |         S Nr<   )r>   tags     r   __getitem__zFakeFont.__getitem__=   s    {3r   c                     || j         |<   d S rB   rC   )r>   rD   tables      r   __setitem__zFakeFont.__setitem__@   s     Cr   Nc                 8    | j                             ||          S rB   )r<   get)r>   rD   defaults      r   rJ   zFakeFont.getC   s    {sG,,,r   c                     | j         |         S rB   r:   r>   r-   s     r   
getGlyphIDzFakeFont.getGlyphIDF   s    *400r   c                        fd|D             S )Nc                 :    g | ]}                     |          S r$   )rO   r   gidr>   s     r   r&   z+FakeFont.getGlyphIDMany.<locals>.<listcomp>J   s%    444$$444r   r$   r>   lsts   ` r   getGlyphIDManyzFakeFont.getGlyphIDManyI   s    44444444r   c                 V    |t          | j                  k     r| j        |         S d|z  S )Nz	glyph%.5d)lenr8   )r>   glyphIDs     r   getGlyphNamezFakeFont.getGlyphNameL   s1    S)****#G,,((r   c                        fd|D             S )Nc                 :    g | ]}                     |          S r$   )rZ   rR   s     r   r&   z-FakeFont.getGlyphNameMany.<locals>.<listcomp>S   s'    6663!!#&&666r   r$   rT   s   ` r   getGlyphNameManyzFakeFont.getGlyphNameManyR   s    6666#6666r   c                     | j         S rB   )r8   r>   s    r   getGlyphOrderzFakeFont.getGlyphOrderU       r   c                     | j         S rB   rM   r_   s    r   getReverseGlyphMapzFakeFont.getReverseGlyphMapX   s    **r   c                 D    t          |                                           S rB   )sortedr`   r_   s    r   getGlyphNameszFakeFont.getGlyphNames[   s    d((**+++r   rB   )r   
__module____qualname__r@   rE   rH   rJ   rO   rV   rZ   r]   r`   rc   rf   r$   r   r   r2   r2   5   s               ! ! !- - - -1 1 15 5 5) ) )7 7 7     + + +, , , , ,r   r2   c                   &    e Zd Zd Zd Zd Zd ZdS )r   c                     ddl m}  |            | _        | j        | j        _        | j        | j        _        | j        | j        _        d | _	        g | _
        d S )Nr   )ParserCreate)xml.parsers.expatrk   r   startElement_StartElementHandlerendElement_EndElementHandleraddCharacterData_CharacterDataHandlerr   stack)r>   rk   s     r   r@   zTestXMLReader_.__init__`   sZ    222222"lnn*.*<'(,(8%+/+A(	


r   c                     ||g f}| j         r'| j         d         d                             |           n|| _        | j                             |           d S Nr   )rs   appendr   )r>   r-   r.   elements       r   rm   zTestXMLReader_.startElement_j   s]    #: 	 JrN1$$W----DI
'"""""r   c                 8    | j                                          d S rB   )rs   poprN   s     r   ro   zTestXMLReader_.endElement_r   s    
r   c                 R    | j         d         d                             |           d S ru   )rs   rw   )r>   datas     r   rq   z TestXMLReader_.addCharacterData_u   s'    
2q  &&&&&r   N)r   rg   rh   r@   rm   ro   rq   r$   r   r   r   r   _   sP          # # #  ' ' ' ' 'r   r   
c                     t          t                      |           }|j                            d           |j                                         |S )N)
newlinestrr   )r	   r   fileseektruncate)r   writers     r   makeXMLWriterr   y   sG    wyyZ888F
KQ
KMr   c                     t                      } | ||           |j                                                            d          }|                    d          sJ |                                S )zCall the passed toXML function and return the written content as a
    list of lines (unicode strings).
    Result is stripped of XML declaration and OS-specific newline characters.
    r   r}   )r   r   getvaluedecodeendswith
splitlines)functtFontr   r    s       r   getXMLr      sh    
 __FD
+


 
 
'
'
0
0C<<>>r   TFstringttLibVersioncheckSumAdjustmentmodifiedcreatedsfntVersionreturnc                     |rt          j        dd|           } |rt          j        dd|           } |rt          j        dd|           } |rt          j        dd|           } |rt          j        dd|           } | S )zJStrip stuff like ttLibVersion, checksums, timestamps, etc. from TTX dumps.z ttLibVersion="[^"]+" z sfntVersion="[^"]+"z#<checkSumAdjustment value="[^"]+"/>z<modified value="[^"]+"/>z<created value="[^"]+"/>)resub)r   r   r   r   r   r   s         r   stripVariableItemsFromTTXr      s      =/V<< <.F;; K=r6JJ A3R@@ @2B??Mr   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )MockFontzYA font-like object that automatically adds any looked up glyphname
    to its glyphOrder.c                 t     dg _          G  fddt                    } |ddi           _        d _        d S )Nz.notdefc                       e Zd Z fdZdS ))MockFont.__init__.<locals>.AllocatingDictc                 d    j                             |           t          |           }|| |<   |S rB   )_glyphOrderrw   rX   )reverseDictkeyrS   r>   s      r   __missing__z5MockFont.__init__.<locals>.AllocatingDict.__missing__   s6     '',,,+&&#&C 
r   N)r   rg   rh   r   r_   s   r   AllocatingDictr      s.              r   r   r   F)r   dict_reverseGlyphOrderr;   )r>   r   s   ` r   r@   zMockFont.__init__   sd    %;	 	 	 	 	 	 	T 	 	 	 #1.)Q"@"@			r   c                      | j         |         }|S rB   r   )r>   glyphrS   s      r   rO   zMockFont.getGlyphID   s    %e,
r   c                     | j         S rB   r   r_   s    r   rc   zMockFont.getReverseGlyphMap   s    &&r   c                     | j         |         S rB   r   )r>   rS   s     r   rZ   zMockFont.getGlyphName   s    $$r   c                     | j         S rB   r   r_   s    r   r`   zMockFont.getGlyphOrder   ra   r   N)	r   rg   rh   __doc__r@   rO   rc   rZ   r`   r$   r   r   r   r      si             ' ' '% % %         r   r   c                       e Zd Zd ZdS )r   c                 l    t          j        | |           t          | d          s| j        | _        d S d S )NassertRaisesRegex)	_TestCaser@   hasattrassertRaisesRegexpr   )r>   
methodNames     r   r@   zTestCase.__init__   sE    4,,, t011 	=%)%<D"""	= 	=r   N)r   rg   rh   r@   r$   r   r   r   r      s#        = = = = =r   r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )DataFilesHandlerc                 "    d | _         d| _        d S )Nr   )tempdirnum_tempfilesr_   s    r   setUpzDataFilesHandler.setUp   s    r   c                 J    | j         rt          j        | j                    d S d S rB   )r   shutilrmtreer_   s    r   tearDownzDataFilesHandler.tearDown   s.    < 	(M$,'''''	( 	(r   c                     t           j                            t          j        | j                 j                  }t           j                            |d|          S )Nr|   )ospathdirnamesysmodulesrg   __file__r   )r>   testfilefolders      r   getpathzDataFilesHandler.getpath   s9    T_!=!FGGw||FFH555r   c                 H    | j         st          j                    | _         d S d S rB   )r   tempfilemkdtempr_   s    r   temp_dirzDataFilesHandler.temp_dir   s*    | 	.#+--DLLL	. 	.r   c                     |                                   t          j                            | j        |          }t          j        ||           |S rB   )r   r   r   r   r   r   copy2)r>   	font_path	file_nametemppaths       r   	temp_fontzDataFilesHandler.temp_font   s=    7<<i88Y)))r   N)r   rg   rh   r   r   r   r   r   r$   r   r   r   r      s_          ( ( (6 6 6. . .    r   r   )r}   rB   )TTTTF)r   collections.abcr   ior   r   r   r   r   r   unittestr   r   fontTools.configr   fontTools.misc.textToolsr   fontTools.misc.xmlWriterr	   r!   r0   r2   objectr   r   r   r   boolr   r   r   r$   r   r   <module>r      s1   % % $ $ $ $ $ $       				 				  



  * * * * * * # # # # # # , , , , , , . . . . . .  :  ', ', ', ', ', ', ', ',T' ' ' ' 'V ' ' '4   
 
 
 
 #   	
   	   2         v      >= = = = =y = = =    x     r   