
    ]MhD1                        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e          Z ej        d          Zeeej        f         Z dede dej!        fdZ"dej#        de fdZ$dej%        de de&fdZ'dej!        dej%        fdZ(dej!        dedefdZ)efdej!        dee*ge*f         dej!        fdZ+eddej!        dedee*ge*f         dej!        fd Z,d!ej!        d"ej!        de&fd#Z-	 d6dede d%ej.        d&ej/        d'e&de&fd(Z0d)ej1        d*ee         de d'e&d+e&ddfd,Z2dede d-e	de&fd.Z3d$d/d)ej1        d*ee         de d'e&d+e&d0e&ddfd1Z4	 	 	 d7d$d/d)ej1        d*eee                  d'e&d+e&d0e&ddfd3Z5d8d4Z6e7d5k    r e6             dS dS )9zz 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                       e Zd ZdS )RemoveOverlapsErrorN)__name__
__module____qualname__     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/fontTools/ttLib/removeOverlaps.pyr   r      s        Dr   r   zfontTools.ttLib.removeOverlaps	glyphNameglyphSetreturnc                     t          j                    }|                    |          }||                              |           |S )Nr   )pathopsPathgetPendraw)r   r   pathpathPens       r   skPathFromGlyphr$   "   s>    <>>Dkk8k,,GYW%%%Kr   	componentc                 d    |                                  \  }}t          ||          } |j        | S N)getComponentInfor$   	transform)r%   r   baseGlyphNametransformationr"   s        r   skPathFromGlyphComponentr,   )   s8     %.$>$>$@$@!M>=(33D4>>**r   glyphc           
      X                                      st          d          t           j                  dk     rdS i dt          dt
          j        f fdt          fdt          j	        t          t           j                            d          D                       S )Nz5This method only works with TrueType composite glyphs   Findexr   c                 X    | vrt          j        |                    | <   |          S r'   )r,   
components)r0   component_pathsr-   r   s    r   _get_nth_component_pathz2componentsOverlap.<locals>._get_nth_component_path9   s<    ''%= '& &OE" u%%r   c              3      K   | ]A\  }}t          j         |           |          t           j        j        d d           V  BdS )F)fix_windingkeep_starting_pointsN)r   opPathOpINTERSECTION).0ijr4   s      r   	<genexpr>z$componentsOverlap.<locals>.<genexpr>@   sx       	 	 Aq 	
##A&&##A&&N'!&	
 	
 	
	 	 	 	 	 	r   )isComposite
ValueErrorlenr2   intr   r   any	itertoolscombinationsrange)r-   r   r4   r3   s   ``@@r   componentsOverlaprG   1   s     RPQQQ
5q  uO&s &w| & & & & & & & &  	 	 	 	 *5U5E1F1F+G+GKK	 	 	 	 	 	r   r"   c                     t          d           }|                     |           |                                }|                                rJ |                    d            |S )Nr   )	glyfTable)r   r!   r-   r?   recalcBounds)r"   ttPenr-   s      r   ttfGlyphFromSkPathrL   L   se    %%%EIIeKKMME  """""	&&&Lr   
charStringc                     |j         |j        j        k    rd }n|j         |j        j        z
  }t	          |d           }|                     |           |                    |j        |j                  S )N)widthr   )rO   privatedefaultWidthXnominalWidthXr   r!   getCharStringglobalSubrs)r"   rM   rO   t2Pens       r   _charString_from_SkPathrV   W   sr     :-;;; :#5#CC%$777EIIez1:3IJJJr   roundc                 r    t          j                    }| D ]\  }} |j        |gfd|D             R    |S )Nc              3   \   K   | ]&} |d                     |d                   fV  'dS )r      Nr   )r;   prW   s     r   r>   z_round_path.<locals>.<genexpr>h   s@       L L%%!++uuQqT{{!; L L L L L Lr   )r   r   add)r"   rW   rounded_pathverbpointss    `   r   _round_pathr`   c   s^     <>>L N NfM L L L LV L L LMMMMMr   rW   debugGlyphNamec                   	 t          j        | | j                  S # t           j        $ r Y nw xY wt	          | |          } 	 t          j        | | j                  } t
                              d|           | S # t           j        $ rP}t
                              t          j	                  r| 
                                 t          d|          |d }~ww xY w)N)	clockwisera   zkskia-pathops failed to simplify '%s' with float coordinates, but succeded using rounded integer coordinatesz%Failed to remove overlaps from glyph )r   simplifyrd   PathOpsErrorr`   logdebugisEnabledForloggingDEBUGdumpr   AssertionError)r"   rb   rW   es       r   	_simplifyro   l   s    ????    t5)))D???		=	
 	
 	

    GM** 	IIKKK!FNFF
 
	s%    //7A< <CACCpath1path2c                 J    d | j         D             d |j         D             k    S )Nc                 ,    h | ]}t          |          S r   tupler;   cs     r   	<setcomp>z_same_path.<locals>.<setcomp>   s    ---E!HH---r   c                 ,    h | ]}t          |          S r   rt   rv   s     r   rx   z_same_path.<locals>.<setcomp>   s    1S1S1Sq%((1S1S1Sr   )contours)rp   rq   s     r   
_same_pathr{      s.    --en---1S1SEN1S1S1SSSr   TrI   	hmtxTableremoveHintingc                    ||          }|j         dk    s$|                                rt          ||          rqt          | |          }t	          ||           }t          ||          sAt          |          x|| <   }|j        rJ ||          \  }}	|	|j        k    r||j        f|| <   dS |r|	                                 dS )Nr   TF)
numberOfContoursr?   rG   r$   ro   r{   rL   programxMinr}   )
r   r   rI   r|   r}   r-   r"   rq   rO   lsbs
             r   removeTTGlyphOverlapsr      s     i E 	"" 	#eX.. 	# y(33 $	** $&& 	+=e+D+DDIi 5}$$$"9-JE3ej  (-uz':	)$4 5r   font
glyphNamesignoreErrorsc                    | d         | d         }t          |fd          }t                      }|D ]Y}	 t          ||||          r|                    |           ,# t          $ r! |s t
                              d|           Y Vw xY wt
                              dt          |          d	                    |                     d S )Nglyfhmtxc                     |                                           r |                                        j        nd| fS )Nr   )r?   getCompositeMaxpValuesmaxComponentDepth)namerI   s    r   <lambda>z'_remove_glyf_overlaps.<locals>.<lambda>   sD     T?..00	$66yAASS
 r   )key"Failed to remove overlaps for '%s'"Removed overlaps for %s glyphs:
%s )
sortedsetr   r\   r   rg   errorrh   rA   join)	r   r   r   r}   r   r|   modifiedr   rI   s	           @r   _remove_glyf_overlapsr      s    VIVI
 
 
 
 

 
 
J uuH 	G 	G		G$8Y	=  ( Y'''" 	G 	G 	G II:IFFFFF	G
 II3S]]CHHXDVDVWWWWWs   (A""(BB
cffFontSetc                     t          | |          }t          || t                    }t          ||          s(|d         j        }t          |||                    || <   dS dS )Nra   r   TF)r$   ro   r   r{   CharStringsrV   )r   r   r   r"   rq   charStringss         r   _remove_charstring_overlapsr      sl     9h//D dIW555E dE""  m/!8I@V!W!WIt5r   )removeUnusedSubroutinesr   c                    | d         j         }t                      }|D ]X}	 t          |||          r|                    |           +# t          $ r! |s t
                              d|           Y Uw xY w|st
                              d           d S |r|                                 |r|	                                 t
                              dt          |          d                    |                     d S )NCFF )r   r   r   r   z-No overlaps found in the specified CFF glyphsr   r   )cffr   r   r\   r   rg   r   rh   remove_hintsremove_unused_subroutinesrA   r   )	r   r   r   r}   r   r   r   r   r   s	            r   _remove_cff_overlapsr      s/    f!JuuH G G	
	G*#!%   (
 Y'''" 	G 	G 	G II:IFFFFF	G
  		ABBB "!!! /,,...II3S]]CHHXDVDVWWWWWs   'A		(A43A4Fc                    d| vrd| vrt          d          ||                                 }|                                 }d| v rt          | ||||           d| v rt	          | |||||           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   r   zANo outline data found in the font: missing 'glyf' or 'CFF ' tableN)r   r   r   r}   r   )r   r   r   r}   r   r   )NotImplementedErrorgetGlyphOrdergetGlyphSetr   r   )r   r   r}   r   r   r   s         r   r   r     s    > TfD00!O
 
 	
 ''))
 !!H~~!'%	
 	
 	
 	
 ~~!'%$;	
 	
 	
 	
 	
 	
 ~r   c                    ddl }|                    dt                    }|                    ddd           |                    d	d
d           |                    dddd           |                    ddd           |                    ddd           |                    ddd           |                    |           } t          j        | j                  5 }t          || j	        pd| j
         | j        | j                    |                    | j                   ddd           dS # 1 swxY w 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)r   r   r}   r   r   )argparseArgumentParser__doc__add_argument
parse_argsr   TTFontr   r   r   keep_hintingignore_errorskeep_unused_subroutinessaver   )argsr   parserr   s       r   mainr   Y  s    OOO$$(g %  F ;LMMM
,=OPPP
C	     M    
 3     #?     T""D	tz	"	" d{*d"//+(,(D$D	
 	
 	
 	
 			$+                 s    AD44D8;D8__main__)T)NTFr'   )8r   rD   rj   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   	getLoggerrg   str_TTGlyph_TTGlyphMappingr   r$   GlyphComponentr,   GlyphboolrG   rL   rV   floatr`   ro   r{   table__g_l_y_ftable__h_m_t_xr   r   r   r   r   r   r   r   r   r   r   <module>r      s   
      8 8 8 8 8 8 8 8 8 8 8 8 ' ' ' ' ' ' " " " " " " + + + + + + + + + + + + 5 5 5 5 5 5 6 6 6 6 6 6 6 6 0 0 0 0 0 0 : : : : : :  
	 	 	 	 	) 	 	 	 g899#v./s o ',    +&+2A+ + + +X^  4    6W\ hn    	K
,	K$0	K	K 	K 	K 	K ;B 
,'7\    '.	$( $( $(
,$($( UGUN#	$(
 \$( $( $( $(NTgl T7< TD T T T T       &  &	 
   
       F%X
-%X %X 	%X
 %X %X 
%X %X %X %XP  	
 
   6 %)"X "X "X
-"X "X 	"X
 "X "X ""X 
"X "X "X "XN +/	;
 %);
 ;
 ;

-;
#';
 ;
 	;
 ";
 
;
 ;
 ;
 ;
|, , , ,^ zDFFFFF r   