a
    w=ic                     @   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   o/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/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$| rt||rt| |}t|| }dd |jD dd |jD krt| || < }|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 s0    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   
	' %   
@

