a
    èÝEb¢  ã                   @   s‚  d Z ddlZddlZddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlZd	gZG d
d„ deƒZe d¡Zeeejf Zeeejdœdd„Zejedœdd„Zejeedœdd„Z ejejdœdd„Z!e	fejee"ge"f ejdœdd„Z#ejeejdœdd„Z$d)eeej%ej&eed œd!d"„Z'd*ej(eee  edd$œd%d	„Z)d+d&d'„Z*e+d(k�r~e*ƒ  dS ),zz Simplify TrueType glyphs by merging overlapping contours/components.

Requires https://github.com/fonttools/skia-pathops
é    N)ÚCallableÚIterableÚOptionalÚMapping)ÚotRound)ÚttFont)Ú_g_l_y_f)Ú_h_m_t_x)Ú
TTGlyphPenÚremoveOverlapsc                   @   s   e Zd ZdS )ÚRemoveOverlapsErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú^/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/fontTools/ttLib/removeOverlaps.pyr      s   r   zfontTools.ttLib.removeOverlaps)Ú	glyphNameÚglyphSetÚreturnc                 C   s&   t  ¡ }|j|d�}||   |¡ |S )N©r   )ÚpathopsÚPathZgetPenÚdraw)r   r   ÚpathZpathPenr   r   r   ÚskPathFromGlyph   s    r   )Ú	componentr   c                 C   s    |   ¡ \}}t||ƒ}|j|Ž S ©N)ZgetComponentInfor   Z	transform)r   r   ZbaseGlyphNameZtransformationr   r   r   r   ÚskPathFromGlyphComponent&   s    
r   )Úglyphr   r   c                    sh   ˆ  ¡ stdƒ‚tˆjƒdk r"dS i ‰ttjdœ‡‡‡fdd„‰ t‡ fdd„t 	t
tˆjƒƒd¡D ƒƒS )	Nz5This method only works with TrueType composite glyphsé   F)Úindexr   c                    s$   | ˆ vrt ˆj|  ˆƒˆ | < ˆ |  S r   )r   Ú
components)r    )Úcomponent_pathsr   r   r   r   Ú_get_nth_component_path6   s
    
ÿz2componentsOverlap.<locals>._get_nth_component_pathc                 3   s2   | ]*\}}t jˆ |ƒˆ |ƒt jjd d d�V  qdS )F)Zfix_windingZkeep_starting_pointsN)r   ÚopZPathOpZINTERSECTION)Ú.0ÚiÚj)r#   r   r   Ú	<genexpr>=   s   ùûz$componentsOverlap.<locals>.<genexpr>)ÚisCompositeÚ
ValueErrorÚlenr!   Úintr   r   ÚanyÚ	itertoolsÚcombinationsÚrange)r   r   r   )r#   r"   r   r   r   ÚcomponentsOverlap.   s    ør1   )r   r   c                 C   s8   t d d�}|  |¡ | ¡ }| ¡ r(J ‚|jd d� |S )Nr   ©Ú	glyfTable)r
   r   r   r)   ZrecalcBounds)r   ZttPenr   r   r   r   ÚttfGlyphFromSkPathI   s    

r4   )r   Úroundr   c                    s:   t  ¡ }| D ](\}}|j|g‡ fdd„|D ƒ¢R Ž  q|S )Nc                 3   s&   | ]}ˆ |d  ƒˆ |d ƒfV  qdS )r   é   Nr   )r%   Úp©r5   r   r   r(   Y   ó    z_round_path.<locals>.<genexpr>)r   r   Úadd)r   r5   Zrounded_pathÚverbZpointsr   r8   r   Ú_round_pathT   s    "r<   )r   ÚdebugGlyphNamer   c              
   C   sª   zt j| | jd�W S  t jy&   Y n0 t| ƒ} z"t j| | jd�} t d|¡ | W S  t jyœ } z0t tj	¡rx|  
¡  td|›�ƒ|‚W Y d }~n
d }~0 0 tdƒ‚d S )N)Ú	clockwisezkskia-pathops failed to simplify '%s' with float coordinates, but succeded using rounded integer coordinatesz%Failed to remove overlaps from glyph ZUnreachable)r   Úsimplifyr>   ZPathOpsErrorr<   ÚlogÚdebugÚisEnabledForÚloggingÚDEBUGÚdumpr   ÚAssertionError)r   r=   Úer   r   r   Ú	_simplify]   s*    
ýÿþrH   T)r   r   r3   Ú	hmtxTableÚremoveHintingr   c           
      C   sª   ||  }|j dks$| ¡ ršt||ƒršt| |ƒ}t|| ƒ}dd„ |jD ƒdd„ |jD ƒkršt|ƒ || < }|jrrJ ‚||  \}}	|	|jkr–||jf|| < dS |r¦| 	¡  dS )Nr   c                 S   s   h | ]}t |ƒ’qS r   )Útuple)r%   Úcr   r   r   Ú	<setcomp>“   r9   z(removeTTGlyphOverlaps.<locals>.<setcomp>TF)
ZnumberOfContoursr)   r1   r   rH   Zcontoursr4   ÚprogramZxMinrJ   )
r   r   r3   rI   rJ   r   r   Zpath2ÚwidthZlsbr   r   r   ÚremoveTTGlyphOverlaps   s&    ÿþý

 

rP   F)ÚfontÚ
glyphNamesrJ   r   c              	      sÌ   z| d ‰ W n t y&   tdƒ‚Y n0 | d }|  ¡ }|du rH|  ¡ }t|‡ fdd„d�}tƒ }|D ]H}zt||ˆ ||ƒr†| |¡ W qf ty¬   |sœ‚ t	 
d|¡ Y qf0 qft	 d	t|ƒd
 |¡¡ dS )a½  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.
        ignoreErrors (bool): set to True to ignore errors while removing overlaps,
            thus keeping the tricky glyphs unchanged (fonttools/fonttools#2363).
    Zglyfz-removeOverlaps currently only works with TTFsZhmtxNc                    s$   ˆ |    ¡ rˆ |   ˆ ¡jnd| fS )Nr   )r)   ZgetCompositeMaxpValuesZmaxComponentDepth)Únamer2   r   r   Ú<lambda>Ì   s
    
ÿüz removeOverlaps.<locals>.<lambda>)Úkeyz"Failed to remove overlaps for '%s'z"Removed overlaps for %s glyphs:
%sú )ÚKeyErrorÚNotImplementedErrorZgetGlyphSetZgetGlyphOrderÚsortedÚsetrP   r:   r   r@   ÚerrorrA   r+   Újoin)rQ   rR   rJ   ZignoreErrorsrI   r   Úmodifiedr   r   r2   r   r   ¢   s0    
þ	
ÿc                 C   sž   dd l }| d u r|jdd … } t| ƒdk r<tdƒ | d¡ | d }| d }| dd … pZd }t |¡�$}t||ƒ | |¡ W d   ƒ n1 s�0    Y  d S )Nr   r6   r   zGusage: fonttools ttLib.removeOverlaps INPUT.ttf OUTPUT.ttf [GLYPHS ...])	ÚsysÚargvr+   ÚprintÚexitr   ÚTTFontr   Úsave)Úargsr^   ÚsrcÚdstrR   Úfr   r   r   Úmainâ   s    ÿ

rh   Ú__main__)T)NTF)N),Ú__doc__r.   rC   Útypingr   r   r   r   ZfontTools.misc.roundToolsr   ZfontTools.ttLibr   ZfontTools.ttLib.tablesr   r	   ZfontTools.pens.ttGlyphPenr
   r   Ú__all__Ú	Exceptionr   Ú	getLoggerr@   ÚstrZ_TTGlyphZ_TTGlyphMappingr   r   ZGlyphComponentr   ZGlyphÚboolr1   r4   Úfloatr<   rH   Ztable__g_l_y_fZtable__h_m_t_xrP   rb   r   rh   r   r   r   r   r   Ú<module>   sZ   
ÿÿþ	' ûú%   ü
û@

