a
    —ÆàgD1  ã                	   @   sH  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mZ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j!eedœdd„Z)efej!ee*ge*f ej!dœd d!„Z+ed"œej!eee*ge*f ej!d#œd$d%„Z,ej!ej!e&d&œd'd(„Z-d=ee ej.ej/e&e&d*œd+d,„Z0ej1ee e e&e&dd-œd.d/„Z2ee e	e&d0œd1d2„Z3d)d3œej1ee e e&e&e&dd4œd5d6„Z4d>d)d3œej1eee  e&e&e&dd8œd9d„Z5d?d:d;„Z6e7d<k�rDe6ƒ  dS )@zz Simplify TrueType glyphs by merging overlapping contours/components.

Requires https://github.com/fonttools/skia-pathops
é    N)ÚCallableÚIterableÚOptionalÚMapping)Ú
CFFFontSet)ÚttFont)Ú_g_l_y_f)Ú_h_m_t_x)ÚT2CharString)ÚotRoundÚnoRound)Ú
TTGlyphPen)ÚT2CharStringPenÚremoveOverlapsc                   @   s   e Zd ZdS )ÚRemoveOverlapsErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úW/var/www/sistema_ama/venv/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_path9   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   ÚcomponentsOverlap1   s    ør5   )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   ÚttfGlyphFromSkPathL   s    

r8   )r   Ú
charStringr   c                 C   sH   |j |jjkrd }n|j |jj }t|d d�}|  |¡ | |j|j¡S )N)Úwidthr   )r:   ÚprivateZdefaultWidthXZnominalWidthXr   r   ZgetCharStringZglobalSubrs)r   r9   r:   Zt2Penr   r   r   Ú_charString_from_SkPathW   s    
r<   )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©r=   r   r   r,   h   ó    z_round_path.<locals>.<genexpr>)r   r   Úadd)r   r=   Zrounded_pathZverbZpointsr   r@   r   Ú_round_pathc   s    "rC   r@   )r   ÚdebugGlyphNamer=   r   c             
   C   s®   zt j| | jd�W S  t jy&   Y n0 t| |d�} z"t j| | jd�} t d|¡ | W S  t jy  } z0t tj	¡r||  
¡  td|›�ƒ|‚W Y d }~n
d }~0 0 tdƒ‚d S )N)Ú	clockwiser@   zkskia-pathops failed to simplify '%s' with float coordinates, but succeded using rounded integer coordinatesz%Failed to remove overlaps from glyph ZUnreachable)r   ZsimplifyrE   ZPathOpsErrorrC   ÚlogÚdebugÚisEnabledForÚloggingÚDEBUGÚdumpr   ÚAssertionError)r   rD   r=   Úer   r   r   Ú	_simplifyl   s*    ýÿþrN   )Úpath1Úpath2r   c                 C   s    dd„ | j D ƒdd„ |j D ƒkS )Nc                 S   s   h | ]}t |ƒ’qS r   )Útuple)r)   Úcr   r   r   Ú	<setcomp>”   rA   z_same_path.<locals>.<setcomp>)Zcontours)rO   rP   r   r   r   Ú
_same_path“   s    rT   T)r   r   r7   Ú	hmtxTableÚremoveHintingr   c           
      C   s”   ||  }|j dks$| ¡ r„t||ƒr„t| |ƒ}t|| ƒ}t||ƒs„t|ƒ || < }|jr\J ‚||  \}}	|	|jkr€||jf|| < dS |r�| 	¡  dS )Nr   TF)
ZnumberOfContoursr-   r5   r   rN   rT   r8   ÚprogramZxMinrV   )
r   r   r7   rU   rV   r"   r   rP   r:   Zlsbr   r   r   ÚremoveTTGlyphOverlaps—   s&    ÿþý




rX   )ÚfontÚ
glyphNamesr   rV   ÚignoreErrorsr   c              	      s”   | d ‰ | d }t |‡ fdd„d�}tƒ }|D ]H}zt||ˆ ||ƒrN| |¡ W q. tyt   |sd‚ t d|¡ Y q.0 q.t dt|ƒd 	|¡¡ d S )	NÚglyfZhmtxc                    s$   ˆ |    ¡ rˆ |   ˆ ¡jnd| fS )Nr   )r-   ZgetCompositeMaxpValuesZmaxComponentDepth)Únamer6   r   r   Ú<lambda>Ê   s
    
ÿúz'_remove_glyf_overlaps.<locals>.<lambda>)Úkeyú"Failed to remove overlaps for '%s'ú"Removed overlaps for %s glyphs:
%sú )
ÚsortedÚsetrX   rB   r   rF   ÚerrorrG   r/   Újoin)rY   rZ   r   rV   r[   rU   Úmodifiedr   r   r6   r   Ú_remove_glyf_overlapsº   s$    
þ
ÿrh   )r   r   Ú
cffFontSetr   c                 C   sF   t | |ƒ}t|| td�}t||ƒsB|d j}t|||  ƒ|| < dS dS )Nr@   r   TF)r   rN   r   rT   ZCharStringsr<   )r   r   ri   r   rP   ZcharStringsr   r   r   Ú_remove_charstring_overlapsâ   s    


rj   )ÚremoveUnusedSubroutines)rY   rZ   r   rV   r[   rk   r   c           	   	   C   s¢   | d j }tƒ }|D ]F}zt|||d�r2| |¡ W q tyX   |sH‚ t d|¡ Y q0 q|snt d¡ d S |rz| ¡  |r†| 	¡  t dt
|ƒd |¡¡ d S )NúCFF )r   r   ri   r`   z-No overlaps found in the specified CFF glyphsra   rb   )Zcffrd   rj   rB   r   rF   re   rG   Zremove_hintsZremove_unused_subroutinesr/   rf   )	rY   rZ   r   rV   r[   rk   ri   rg   r   r   r   r   Ú_remove_cff_overlapsö   s,    	
ý
rm   F)rY   rZ   rV   r[   rk   r   c                C   sj   d| vrd| vrt dƒ‚|du r(|  ¡ }|  ¡ }d| v rJt| ||||d� d| v rft| |||||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 for fonts with 'glyf' or 'CFF ' tables.
    Raises NotImplementedError if 'glyf' or 'CFF ' tables are 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).
        removeUnusedSubroutines (bool): set to False to keep unused subroutines
            in CFF table after removing overlaps. Default is to remove them if
            any glyphs are modified.
    r\   rl   zANo outline data found in the font: missing 'glyf' or 'CFF ' tableN)rY   rZ   r   rV   r[   )rY   rZ   r   rV   r[   rk   )ÚNotImplementedErrorZgetGlyphOrderZgetGlyphSetrh   rm   )rY   rZ   rV   r[   rk   r   r   r   r   r     s0    ÿûúc                 C   sà   ddl }|jdtd�}|jdddd� |jd	d
dd� |jddddd� |jdddd� |jdddd� |jdddd� | | ¡} t | j¡�>}t|| j	pœd| j
 | j| j d� | | j¡ W d  ƒ n1 sÒ0    Y  dS )z:Simplify glyphs in TTFont by merging overlapping contours.r   Nzfonttools ttLib.removeOverlaps)ÚdescriptionÚinputz	INPUT.ttfzInput font file)ÚmetavarÚhelpÚoutputz
OUTPUT.ttfzOutput font fileÚglyphsZGLYPHSÚ*z4Optional list of glyph names to remove overlaps from)rq   Únargsrr   z--keep-hintingÚ
store_truez>Keep hinting for unmodified glyphs, default is to drop hinting)Úactionrr   z--ignore-errorszOignore errors while removing overlaps, thus keeping the tricky glyphs unchangedz--keep-unused-subroutineszrKeep unused subroutines in CFF table after removing overlaps, default is to remove them if any glyphs are modified)rY   rZ   rV   r[   rk   )ÚargparseÚArgumentParserÚ__doc__Úadd_argumentÚ
parse_argsr   ÚTTFontrp   r   rt   Zkeep_hintingÚignore_errorsZkeep_unused_subroutinesÚsavers   )Úargsry   ÚparserrY   r   r   r   ÚmainY  sJ    ÿüýýý
ûrƒ   Ú__main__)T)NTF)N)8r{   r2   rI   Útypingr   r   r   r   ZfontTools.cffLibr   ZfontTools.ttLibr   ZfontTools.ttLib.tablesr   r	   ZfontTools.misc.psCharStringsr
   ZfontTools.misc.roundToolsr   r   ZfontTools.pens.ttGlyphPenr   ZfontTools.pens.t2CharStringPenr   r   Ú__all__Ú	Exceptionr   Ú	getLoggerrF   ÚstrZ_TTGlyphZ_TTGlyphMappingr   r   ZGlyphComponentr!   ZGlyphÚboolr5   r8   r<   ÚfloatrC   rN   rT   Ztable__g_l_y_fZtable__h_m_t_xrX   r~   rh   rj   rm   r   rƒ   r   r   r   r   r   Ú<module>   s¨   
ÿþÿþüû'	 ûú%ù*ûùø'   üú
ù>
/
