
    McgD1                        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j8                  d      Zeeej>                  f   Z dede dejB                  fdZ"dejF                  de fdZ$dejJ                  de de&fdZ'dejB                  dejJ                  fdZ(dejB                  dedefdZ)efdejB                  dee*ge*f   dejB                  fdZ+eddejB                  dedee*ge*f   dejB                  fd Z,d!ejB                  d"ejB                  de&fd#Z-	 d5dede d%ej\                  d&ej^                  d'e&de&fd(Z0d)ejb                  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jb                  d*ee   de d'e&d+e&d0e&ddfd1Z4	 	 	 d6d$d/d)ejb                  d*eee      d'e&d+e&d0e&ddfd2Z5d7d3Z6e7d4k(  r e6        yy)8zz 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y)RemoveOverlapsErrorN)__name__
__module____qualname__     Y/var/www/html/bid-api/venv/lib/python3.12/site-packages/fontTools/ttLib/removeOverlaps.pyr   r      s    r   r   zfontTools.ttLib.removeOverlaps	glyphNameglyphSetreturnc                 z    t        j                         }|j                  |      }||    j                  |       |S )Nr   )pathopsPathgetPendraw)r   r   pathpathPens       r   skPathFromGlyphr$   "   s5    <<>Dkk8k,GYW%Kr   	componentc                 ^    | j                         \  }}t        ||      } |j                  | S N)getComponentInfor$   	transform)r%   r   baseGlyphNametransformationr"   s        r   skPathFromGlyphComponentr,   )   s4     %.$>$>$@!M>=(3D4>>>**r   glyphc           
      :     j                         st        d      t         j                        dk  ry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                 N    | vrt        j                  |          | <   |    S r'   )r,   
components)r0   component_pathsr-   r   s    r   _get_nth_component_pathz2componentsOverlap.<locals>._get_nth_component_path9   s7    '%=  '&OE" u%%r   c              3      K   | ]E  \  }}t        j                   |       |      t         j                  j                  d d        G yw)F)fix_windingkeep_starting_pointsN)r   opPathOpINTERSECTION).0ijr4   s      r   	<genexpr>z$componentsOverlap.<locals>.<genexpr>@   sT      	 LDAq 	

#A&#A&NN''!&	
 	
 Ls   AA)isComposite
ValueErrorlenr2   intr   r   any	itertoolscombinationsrange)r-   r   r4   r3   s   ``@@r   componentsOverlaprG   1   s    PQQ
5q O&s &w|| &  	 **5U5E5E1F+GK	 	 	r   r"   c                     t        d       }| j                  |       |j                         }|j                         rJ |j	                  d        |S )Nr   )	glyfTable)r   r!   r-   r?   recalcBounds)r"   ttPenr-   s      r   ttfGlyphFromSkPathrL   L   sK    %EIIeKKME  """	&Lr   
charStringc                    |j                   |j                  j                  k(  rd }n#|j                   |j                  j                  z
  }t	        |d       }| j                  |       |j                  |j                  |j                        S )N)widthr   )rO   privatedefaultWidthXnominalWidthXr   r!   getCharStringglobalSubrs)r"   rM   rO   t2Pens       r   _charString_from_SkPathrV   W   sv     :--;;;  :#5#5#C#CC%$7EIIez11:3I3IJJr   roundc                 |    t        j                         }| D ]!  \  }} |j                  |gfd|D          # |S )Nc              3   J   K   | ]  } |d           |d         f  yw)r      Nr   )r;   prW   s     r   r>   z_round_path.<locals>.<genexpr>h   s'      LV%!+uQqT{!;Vs    #)r   r   add)r"   rW   rounded_pathverbpointss    `   r   _round_pathr`   c   s?     <<>LfM LV LM r   rW   debugGlyphNamec                   	 t        j                  | | j                        S # t         j                  $ r Y nw xY wt	        | |      } 	 t        j                  | | j                        } t
        j                  d|       | S # t         j                  $ rG}t
        j                  t        j                        r| j                          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?		=	

  GMM*IIK!3N3EF
	s%    # 99
8B CACCpath1path2c                     | j                   D ch c]  }t        |       c}|j                   D ch c]  }t        |       c}k(  S c c}w c c}w r'   )contourstuple)rp   rq   cs      r   
_same_pathrv      sA    #nn-nE!Hn-ENN1SNq%(N1SSS-1Ss
   AATrI   	hmtxTableremoveHintingc                 ^   ||    }|j                   dkD  s|j                         rwt        ||      rkt        | |      }t	        ||       }t        ||      sGt        |      x|| <   }|j                  rJ ||    \  }}	|	|j                  k7  r||j                  f|| <   y|r|j                          y)Nr   TF)
numberOfContoursr?   rG   r$   ro   rv   rL   programxMinrx   )
r   r   rI   rw   rx   r-   r"   rq   rO   lsbs
             r   removeTTGlyphOverlapsr~      s     i E 	"eX.y(3 $	* $&+=e+DDIi 5}}$$"9-JE3ejj (-uzz':	)$r   font
glyphNamesignoreErrorsc                 L   | d   | d   }t        |fd      }t               }|D ]#  }	 t        ||||      r|j                  |       % t
        j                  dt        |      dj                  |             y # t        $ r |s t
        j                  d|       Y zw xY w)Nglyfhmtxc                 r    |    j                         r |    j                        j                  | fS d| fS )Nr   )r?   getCompositeMaxpValuesmaxComponentDepth)namerI   s    r   <lambda>z'_remove_glyf_overlaps.<locals>.<lambda>   sF     T?..0 $66y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   rx   r   rw   modifiedr   rI   s	           @r   _remove_glyf_overlapsr      s     VIVI
 

J uH		G$8Y	= Y'   II3S]CHHXDVW # 	GII:IF	Gs    A>>"B#"B#
cffFontSetc                     t        | |      }t        || t              }t        ||      s"|d   j                  }t        |||          || <   yy)Nra   r   TF)r$   ro   r   rv   CharStringsrV   )r   r   r   r"   rq   charStringss         r   _remove_charstring_overlapsr      sW     9h/D dIW5E dE" m//!8I@V!WIr   )removeUnusedSubroutinesr   c                    | d   j                   }t               }|D ]"  }	 t        |||      r|j                  |       $ |st
        j                  d       y |r|j                          |r|j                          t
        j                  dt        |      dj                  |             y # t        $ r |s t
        j                  d|       Y w xY w)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   rx   r   r   r   r   r   s	            r   _remove_cff_overlapsr      s     f!!JuH	
	G*#!%
 Y'   		AB!,,.II3S]CHHXDVW # 	GII:IF	Gs   B--"CCc                    d| vrd| vrt        d      || j                         }| j                         }d| v rt        | ||||       d| v rt	        | |||||       yy)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   rx   r   )r   r   r   rx   r   r   )NotImplementedErrorgetGlyphOrdergetGlyphSetr   r   )r   r   rx   r   r   r   s         r   r   r     s    > TfD0!O
 	
 '')
 !H~!'%	
 ~!'%$;	
 r   c                 h   ddl }|j                  dt              }|j                  ddd       |j                  d	d
d       |j                  dddd       |j                  ddd       |j                  ddd       |j                  ddd       |j	                  |       } t        j                  | j                        5 }t        || j                  xs d| j                   | j                  | j                          |j                  | j                         ddd       y# 1 sw Y   yxY w)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   rx   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  sF    $$(g % F ;LM
,=OP
C	   M  
 3   #?   T"D	tzz	"d{{*d"///++(,(D(D$D	
 			$++ 
#	"	"s   AD((D1__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   rv   table__g_l_y_ftable__h_m_t_xr~   r   r   r   r   r   r   r   r   r   r   <module>r      sH  
   8 8 ' " + + 5 6 0 :  
	) 	 g89#v./s o ',, +&&+2A+X^^  4 6W\\ hnn 	K
,,	K$0	K	K ;B
,,'7\\ '.	$(
,,$($( UGUN#	$(
 \\$(NTgll T7<< TD T     &&  &&	 
   
 F%X
--%X %X 	%X
 %X %X 
%XP  	
 
6 %)"X
--"X "X 	"X
 "X "X ""X 
"XN +/	;
 %);

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