+
    i                     8   R t ^ RIHt ^ RIHt ^ RIHt ^ RIHt RR.t	 ! R R]
4      t]^ R	3R
 ltR tR tR tRR lt]R8X  dT   ^ RIt]! ]P(                  4      ^8X  d0   ^ RIt]P,                  ! ]P.                  ! 4       P0                  4       ]! 4        R# R# )aF  T2CharString glyph width optimizer.

CFF glyphs whose width equals the CFF Private dictionary's ``defaultWidthX``
value do not need to specify their width in their charstring, saving bytes.
This module determines the optimum ``defaultWidthX`` and ``nominalWidthX``
values for a font, when provided with a list of glyph widths.)TTFont)defaultdict)add)reduceoptimizeWidthsmainc                   ,   a  ] tR t^t o R tR tRtV tR# )missingdictc                    Wn         R # Nmissing_func)selfr   s   &&v/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/fontTools/cffLib/width.py__init__missingdict.__init__   s    (    c                $    V P                  V4      # r   r   )r   vs   &&r   __missing__missingdict.__missing__   s      ##r   r   N)__name__
__module____qualname____firstlineno__r   r   __static_attributes____classdictcell__)__classdict__s   @r   r	   r	      s     )$ $r   r	   Fc                 l  aa
aa \        V P                  4       4      pV^ ,          VR,          uoo
\        WP                  4       S4      oV'       d   V
VV3R lp\	        S
S^,
          R4      pMVVV3R lp\	        SS
^,           4      p\        V4      pSpV F  p	V! WV	,          4      pWV	&   K  	  V# )    c                    < V S8  d   S# S# r    )xmaxxstarttotals   &r   <lambda>cumSum.<locals>.<lambda>"       QXE858r   c                    < V S8  d   S# S# r   r!   )r"   minxr$   r%   s   &r   r&   r'   %   r(   r   )sortedkeysr   valuesranger	   )fopr$   
decreasingr-   missingdomainoutr   r"   r#   r*   r%   s   &&f&      @@@r   cumSumr6      s    !&&(Da$r(JD$2xxz5)E8tTAXr*8tTAX&
g
CAqA$KA  Jr   c                 ^   \        V R 4      '       g/   \        \        4      pV  F  pW4;;,          ^,          uu&   K  	  Tp ^ pV P                  4        FV  w  rFWA8X  d   K  \	        WB,
          4      pV^k8:  d   WV,          pK/  VR8:  d   WV^,          ,          pKG  WV^,          ,          pKX  	  V# )itemsik  )hasattrr   intr8   abs)widthsdefaultnominaldwcostfreqdiffs   &&&     r   byteCostrD   2   s    67##ADAID D<<><1;3;LDT\1HD1HD " Kr   c                  a  \        \        4      pS  F  pW;;,          ^,          uu&   K  	  ^\        VP                  4       4      ,          p\	        S 4      \        S 4      rT\        \        WE^,           4      4      p\	        V 3R lV 4       4      p\        S 4      ^,          ^,           pV F@  p	\        S RV	4      W,           8  d   K  V F  p
\        S W4      pW8  g   K  TpT
pT	pK  	  KB  	  XX3# )zSBruteforce version.  Veeeeeeeeeeeeeeeeery slow.  Only works for smallests of fonts.c              3   >   <"   T F  p\        SR V4      x  K  	  R # 5ir   rD   ).0r>   r<   s   & r   	<genexpr>+optimizeWidthsBruteforce.<locals>.<genexpr>T   s      WPVW&$!@!@PVs   N)	r   r:   maxr.   minlistr/   lenrD   )r<   r?   r@   maxDefaultAdvantageminwmaxwr4   bestCostWithoutDefaultbestCostr>   r=   rA   bestDefaultbestNominals   f             r   optimizeWidthsBruteforcerV   G   s     	CA		  c!((*o-Vc&k$%Qh'(F  WPV WW6{Q"HFD'*X-KKGFG5D%%   ##r   c                  a aaaaaaaaaaaa \        S R4      '       g/   \        \        4      pS  F  pW;;,          ^,          uu&   K  	  Vo \        S P	                  4       4      pV^ ,          VR,          rT\        \        WE^,           4      4      p\        S \        R7      o\        S \        R7      o\        S \        RR7      o\        S \        RR7      o\        V3R l4      o\        V3R l4      o\        VVV 3R l4      o\        V3R l4      o\        V3R	 l4      o\        VV3R
 l4      o\        VV3R l4      o\        VV3R lR7      oSS,          pSS,          SS,          ,
          p. p	VSS,          8X  de   SS^l,
          SR,
          .p
V
 FJ  pSV,          '       d(   SV,          SV^,
          ,          8X  d   V^,          pK6  V	P                  V4       KL  	  McSS^l,           SR,           .p
V
 FJ  pSV,          '       d(   SV,          SV^,           ,          8X  d   V^,          pK6  V	P                  V4       KL  	  \        V	VV 3R lR7      pVS3# )zGiven a list of glyph widths, or dictionary mapping glyph width to number of
glyphs having that, returns a tuple of best CFF default and nominal glyph widths.

This algorithm is linear in UPEM+numGlyphs.r8   )r1   T)r1   r2   c                 x   < SV ,          SV ^l,
          ,          ,           SV R,
          ,          ^,          ,           # l   l  r!   )r"   cumFrqUs   &r   r&    optimizeWidths.<locals>.<lambda>|   *    '!*wq3w//'!d(2Ca2GGr   c                 x   < SV ,          SV ^l,           ,          ,           SV R,           ,          ^,          ,           # rY   r!   )r"   cumFrqDs   &r   r&   r]      r^   r   c                 N   < SV ,          SV ,          ,           SV ,          ,
          # r   r!   )r"   	nomnCostD	nomnCostUr<   s   &r   r&   r]      s    Yq\IaL%@6!9%Lr   c                    < \        SV ,          SV ^l,
          ,          ^,          SV R,
          ,          ^,          4      # rY   rK   )r"   cumMaxUs   &r   r&   r]      .    #gaj'!c'"2Q"6D8IA8MNr   c                    < \        SV ,          SV ^l,           ,          ^,          SV R,           ,          ^,          4      # rY   re   )r"   cumMaxDs   &r   r&   r]      rg   r   c                 8   < \        SV ,          SV ,          4      # r   re   )r"   	dfltCostD	dfltCostUs   &r   r&   r]      s    S1y|%Dr   c                 2   < SV ,          SV ,          ,
          # r   r!   )r"   dfltCostnomnCosts   &r   r&   r]      s    Xa[8A;%>r   c                    < SV ,          # r   r!   )r"   rS   s   &r   r&   r]      s	    r   )keyr[   c                    < \        SV S4      # r   rG   )r=   r>   r<   s   &r   r&   r]      s    HVWg,Nr   r+   )r9   r   r:   r,   r-   rM   r/   r6   r   rK   r	   rL   append)r<   r?   r@   r-   rP   rQ   r4   bestCdfltCendsstartsr$   r=   rS   r`   r\   ri   rf   rn   rk   rl   r>   ro   rb   rc   s   f            @@@@@@@@@@@@r   r   r   d   s    67##ADAID &++- Da$r($%Qh'(F V$GV$GV5GV5G GI GI LMH NI NI DEH >?H &34G WEW 11ED	'""7S='D.9E%..WU^wuqy7I%I
KK 
 7S='D.9E%..WU^wuqy7I%I
KK  $NOGGr   Nc                2   ^ RI pVP                  R\        P                  R7      pVP	                  RR\
        RRR7       VP	                  R	R
RRRR7       VP                  V 4      p V P                   F  p\        V4      pVR,          pVP                  P                  4        Uu. uF  qf^ ,          NK  	  ppV P                  '       d   \        V4      w  rM\        V4      w  r\        R\        V4      W\!        WxV	4      3,          4       K  	  R# u upi )z4Calculate optimum defaultWidthX/nominalWidthX valuesNzfonttools cffLib.width)descriptioninputsFILE+zInput TTF files)metavartypenargshelpz-bz--brute-forcebrute
store_truez$Use brute-force approach (VERY slow))destactionr   hmtxz+glyphs=%d default=%d nominal=%d byteCost=%d)argparseArgumentParserr   __doc__add_argumentstr
parse_argsrz   r   metricsr.   r   rV   r   printrN   rD   )
argsr   parserfontfilefontr   mr<   r=   r>   s
   &         r   r   r      s    $$ LL % F &s#<M   3   T"DKKhF| $ 3 3 56 51A$$ 56:::7?GW-f5G96{Ghv.PQR	
   7s   &D__main__r   )r   fontTools.ttLibr   collectionsr   operatorr   	functoolsr   __all__dictr	   r6   rD   rV   r   r   r   sysrN   argvdoctestexittestmodfailedr!   r   r   <module>r      s   A # #   V
$$$ $ A% .*$:@F!
H z
388}"))*F r   