o
    iD1                     @   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   m/var/www/html/eduruby.in/lip-sync/lip-sync-env/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PathZgetPendraw)r   r   pathZpathPenr   r   r   skPathFromGlyph"   s   r   	componentc                 C   s    |   \}}t||}|j| S N)ZgetComponentInfor   Z	transform)r   r   ZbaseGlyphNameZ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)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   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   
charStringc                 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<   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   )r)   pr=   r   r   r,   h   s   & z_round_path.<locals>.<genexpr>)r   r   add)r   r=   Zrounded_pathverbZpointsr   r@   r   _round_pathc   s   "rC   r@   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)	clockwiser@   zkskia-pathops failed to simplify '%s' with float coordinates, but succeded using rounded integer coordinatesz%Failed to remove overlaps from glyph )r   simplifyrE   ZPathOpsErrorrC   logdebugisEnabledForloggingDEBUGdumpr   AssertionError)r   rD   r=   er   r   r   	_simplifyl   s.   rO   path1path2c                 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>   s    z_same_path.<locals>.<setcomp>)Zcontours)rP   rQ   r   r   r   
_same_path   s    rU   Tr7   	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)
ZnumberOfContoursr-   r5   r   rO   rU   r8   programZxMinrW   )
r   r   r7   rV   rW   r"   r   rQ   r:   Zlsbr   r   r   removeTTGlyphOverlaps   s$   





rY   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glyfZhmtxc                    s(    |    r |   j| fS 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setrY   rA   r   rG   errorrH   r/   join)rZ   r[   r   rW   r\   rV   modifiedr   r   r6   r   _remove_glyf_overlaps   s(   


ri   
cffFontSetc                 C   sF   t | |}t|| td}t||s!|d j}t|||  || < dS dS )Nr@   r   TF)r   rO   r   rU   ZCharStringsr<   )r   r   rj   r   rQ   ZcharStringsr   r   r   _remove_charstring_overlaps   s   


rk   )removeUnusedSubroutinesrl   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   rj   ra   z-No overlaps found in the specified CFF glyphsrb   rc   )Zcffre   rk   rA   r   rG   rf   rH   Zremove_hintsZremove_unused_subroutinesr/   rg   )	rZ   r[   r   rW   r\   rl   rj   rh   r   r   r   r   _remove_cff_overlaps   s0   
	

rn   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.
    r]   rm   zANo outline data found in the font: missing 'glyf' or 'CFF ' tableN)rZ   r[   r   rW   r\   )rZ   r[   r   rW   r\   rl   )NotImplementedErrorZgetGlyphOrderZgetGlyphSetri   rn   )rZ   r[   rW   r\   rl   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glyphsZGLYPHS*z4Optional list of glyph names to remove overlaps from)rr   nargsrs   z--keep-hinting
store_truez>Keep hinting for unmodified glyphs, default is to drop hinting)actionrs   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)rZ   r[   rW   r\   rl   )argparseArgumentParser__doc__add_argument
parse_argsr   TTFontrq   r   ru   Zkeep_hintingignore_errorsZkeep_unused_subroutinessavert   )argsrz   parserrZ   r   r   r   mainY  sL   
"r   __main__)T)NTFr    )8r|   r2   rJ   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   	getLoggerrG   strZ_TTGlyphZ_TTGlyphMappingr   r   ZGlyphComponentr!   ZGlyphboolr5   r8   r<   floatrC   rO   rU   Ztable__g_l_y_fZtable__h_m_t_xrY   r   ri   rk   rn   r   r   r   r   r   r   r   <module>   s    




'	
#
(

'


>
/
