o
    hD1                     @   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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 dede dej!fddZ"dej#de fddZ$dej%de de&fddZ'dej!dej%fddZ(dej!dedefdd Z)efdej!d!ee*ge*f dej!fd"d#Z+ed$dej!d%ed!ee*ge*f dej!fd&d'Z,d(ej!d)ej!de&fd*d+Z-	,dCdede d-ej.d.ej/d/e&de&fd0d1Z0d2ej1d3ee de d/e&d4e&ddfd5d6Z2dede d7e	de&fd8d9Z3d,d:d2ej1d3ee de d/e&d4e&d;e&ddfd<d=Z4		,	>dDd,d:d2ej1d3eee  d/e&d4e&d;e&ddfd?dZ5dEd@dAZ6e7dBkrOe6  dS dS )Fzz 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   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fontTools/ttLib/removeOverlaps.pyr      s    r   zfontTools.ttLib.removeOverlaps	glyphNameglyphSetreturnc                 C   s&   t  }|j|d}||  | |S )Nr   )pathopsPathgetPendraw)r   r   pathpathPenr   r   r   skPathFromGlyph"   s   r    	componentc                 C   s    |   \}}t||}|j| S N)getComponentInfor    	transform)r!   r   baseGlyphNametransformationr   r   r   r   skPathFromGlyphComponent)   s   

r'   glyphc                    sj     stdtjdk rdS i dtdtjf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   s4    | ]\}}t j | |t jjd d dV  qdS )F)fix_windingkeep_starting_pointsN)r   opPathOp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   r?   r   c                 C   s8   t d d}| | | }| rJ |jd d |S )Nr   	glyfTable)r   r   r(   r7   recalcBounds)r   ttPenr(   r   r   r   ttfGlyphFromSkPathL   s   

rD   
charStringc                 C   sH   |j |jjkr
d }n|j |jj }t|d d}| | ||j|jS )N)widthr   )rF   privatedefaultWidthXnominalWidthXr   r   getCharStringglobalSubrs)r   rE   rF   t2Penr   r   r   _charString_from_SkPathW   s   
rM   roundc                    s:   t  }| D ]\}}|j|g fdd|D R   q|S )Nc                 3   s(    | ]} |d   |d fV  qdS )r      Nr   )r3   prN   r   r   r6   h   s   & z_round_path.<locals>.<genexpr>)r   r   add)r   rN   rounded_pathverbpointsr   rQ   r   _round_pathc   s   "rV   rQ   debugGlyphNamec             
   C   s   z	t j| | jdW S  t jy   Y nw t| |d} zt j| | jd} td| | W S  t jyJ } zttj	r>| 
  td||d }~ww )N)	clockwiserQ   zkskia-pathops failed to simplify '%s' with float coordinates, but succeded using rounded integer coordinatesz%Failed to remove overlaps from glyph )r   simplifyrX   PathOpsErrorrV   logdebugisEnabledForloggingDEBUGdumpr   AssertionError)r   rW   rN   er   r   r   	_simplifyl   s.   rc   path1path2c                 C   s    dd | j D dd |j D kS )Nc                 S   s   h | ]}t |qS r   )tuple)r3   cr   r   r   	<setcomp>   s    z_same_path.<locals>.<setcomp>)contours)rd   re   r   r   r   
_same_path   s    rj   TrA   	hmtxTableremoveHintingc           
      C   s   ||  }|j dks| rBt||rBt| |}t|| }t||sBt| || < }|jr.J ||  \}}	|	|jkr@||jf|| < dS |rH|	  dS )Nr   TF)
numberOfContoursr7   r?   r    rc   rj   rD   programxMinrl   )
r   r   rA   rk   rl   r(   r   re   rF   lsbr   r   r   removeTTGlyphOverlaps   s$   





rq   font
glyphNamesignoreErrorsc              	      s   | d  | d }t | fddd}t }|D ]#}zt|| ||r'|| W q ty:   |s2 td| Y qw tdt|d	| d S )	Nglyfhmtxc                    s(    |    r |   j| fS d| fS )Nr   )r7   getCompositeMaxpValuesmaxComponentDepth)namer@   r   r   <lambda>   s   
z'_remove_glyf_overlaps.<locals>.<lambda>)key"Failed to remove overlaps for '%s'"Removed overlaps for %s glyphs:
%s )
sortedsetrq   rR   r   r[   errorr\   r9   join)rr   rs   r   rl   rt   rk   modifiedr   r   r@   r   _remove_glyf_overlaps   s(   


r   
cffFontSetc                 C   sF   t | |}t|| td}t||s!|d j}t|||  || < dS dS )NrQ   r   TF)r    rc   r   rj   CharStringsrM   )r   r   r   r   re   charStringsr   r   r   _remove_charstring_overlaps   s   


r   )removeUnusedSubroutinesr   c           	   	   C   s   | d j }t }|D ]"}zt|||dr|| W q
 ty,   |s$ td| Y q
w |s6td d S |r<|  |rB|	  tdt
|d| d S )NCFF )r   r   r   r|   z-No overlaps found in the specified CFF glyphsr}   r~   )cffr   r   rR   r   r[   r   r\   remove_hintsremove_unused_subroutinesr9   r   )	rr   rs   r   rl   rt   r   r   r   r   r   r   r   _remove_cff_overlaps   s0   
	

r   Fc                C   sn   d| vrd| vrt d|du r|  }|  }d| v r%t| ||||d d| v r5t| |||||d dS 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.
    ru   r   zANo outline data found in the font: missing 'glyf' or 'CFF ' tableN)rr   rs   r   rl   rt   )rr   rs   r   rl   rt   r   )NotImplementedErrorgetGlyphOrdergetGlyphSetr   r   )rr   rs   rl   rt   r   r   r   r   r   r     s2   
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	pNd| j
 | j| j d || j W d   dS 1 sjw   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glyphsGLYPHS*z4Optional list of glyph names to remove overlaps from)r   nargsr   z--keep-hinting
store_truez>Keep hinting for unmodified glyphs, default is to drop hinting)actionr   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)rr   rs   rl   rt   r   )argparseArgumentParser__doc__add_argument
parse_argsr   TTFontr   r   r   keep_hintingignore_errorskeep_unused_subroutinessaver   )argsr   parserrr   r   r   r   mainY  sL   
"r   __main__)T)NTFr"   )8r   r<   r^   typingr   r   r   r   fontTools.cffLibr   fontTools.ttLibr   fontTools.ttLib.tablesr   r	   fontTools.misc.psCharStringsr
   fontTools.misc.roundToolsr   r   fontTools.pens.ttGlyphPenr   fontTools.pens.t2CharStringPenr   r   __all__	Exceptionr   	getLoggerr[   str_TTGlyph_TTGlyphMappingr   r    GlyphComponentr'   Glyphboolr?   rD   rM   floatrV   rc   rj   table__g_l_y_ftable__h_m_t_xrq   r   r   r   r   r   r   r   r   r   r   r   <module>   s    




'	
#
(

'


>
/
