
    }fEw                        d Z ddlmZ d Zd Zd-dZd Zd Zd	 Z G d
 de	          Z
d Zd.dZd-dZd Zd Zd Zd Zd Z	 	 	 	 d/dZd-dZedk    rddlZ eej                  dk    r&ddlZ ej         ej                    j                   ddlZ ej        dd          Ze                     ddd d!"           e                     d#d$d dd%&           e!                    ej        dd                   Z"e"j#        dnd' Z$ ee"j%                  Z% e&d(            e& ee%                      ee%e$          Z' e&d)            e&e'            ee'          Z( e&d*            e& ee(                     e%e(k    sJ  e&d+            e& e ee%e$                                e&d,            e& e ee%e$                               dS dS )0aZ  T2CharString operator specializer and generalizer.

PostScript glyph drawing operations can be expressed in multiple different
ways. For example, as well as the ``lineto`` operator, there is also a
``hlineto`` operator which draws a horizontal line, removing the need to
specify a ``dx`` coordinate, and a ``vlineto`` operator which draws a
vertical line, removing the need to specify a ``dy`` coordinate. As well
as decompiling :class:`fontTools.misc.psCharStrings.T2CharString` objects
into lists of operations, this module allows for conversion between general
and specific forms of the operation.

    )maxStackLimitc                    t          | t                    r|                                 } g }| D ]Y}	 t          |          }n1# t          $ r$ 	 t          |          }n# t          $ r Y nw xY wY nw xY w|                    |           Z|S N)
isinstancestrsplitint
ValueErrorfloatappend)stringprogramtokens      U/var/www/rtfcompare/venv/lib/python3.11/site-packages/fontTools/cffLib/specializer.pystringToProgramr      s    &#  G  	JJEE 	 	 	e   	
 	uNs5   A
A/AA/
A)&A/(A))A/.A/c                 @    d                     d | D                       S )N c              3   4   K   | ]}t          |          V  d S r   )r   ).0xs     r   	<genexpr>z"programToString.<locals>.<genexpr>$   s(      ,,qCFF,,,,,,    )join)r   s    r   programToStringr   #   s#    88,,G,,,,,,r   Nc                    d}d}d}d}g }g }t          |           }|D ]}	t          |	t                    s|                    |	           .|	dk    r\|J d ||          z   }
|d         }||
z  dz   }|| d         g|| d<   ||t	          |          z   dz
  z  }t	          |          }|	dk    r!|d         }t          |          t          u sJ no|sm|	dv rid	}|	d
v }|r|t	          ||d                   z   }nt	          |          }|r5|dz  |z  r-|                    d          }|                    d|gf           |	dv rV|r|                    d|f           |                    |	g f           |                    dt          |          gf           n|                    |	|f           g }|r|                    d|f           |S )u\  Takes a T2CharString program list and returns list of commands.
    Each command is a two-tuple of commandname,arg-list.  The commandname might
    be empty string if no commandname shall be emitted (used for glyph width,
    hintmask/cntrmask argument, as well as stray arguments at the end of the
    program (🤷).
    'getNumRegions' may be None, or a callable object. It must return the
    number of regions. 'getNumRegions' takes a single argument, vsindex. It
    returns the numRegions for the vsindex.
    The Charstring may or may not start with a width value. If the first
    non-blend operator has an odd number of arguments, then the first argument is
    a width, and is popped off. This is complicated with blend operators, as
    there may be more than one before the first hint or moveto operator, and each
    one reduces several arguments to just one list argument. We have to sum the
    number of arguments that are not part of the blend arguments, and all the
    'numBlends' values. We could instead have said that by definition, if there
    is a blend operator, there is no width value, since CFF2 Charstrings don't
    have width values. I discussed this with Behdad, and we are allowing for an
    initial width value in this case because developers may assemble a CFF2
    charstring from CFF Charstrings, which could have width values.
    Fr   blendN   vsindex>
   hstemvstemendcharhmovetohstemhmrmovetovmovetovstemhmcntrmaskhintmaskT>   r#   r&       >   r(   r)   )	iterr   r   r   lentyper	   popnext)r   getNumRegionsseenWidthOpvsIndexlenBlendStacklastBlendIndexcommandsstackitr   numSourceFonts	numBlendsnumBlendArgsparitynumArgswidths                   r   programToCommandsr?   '   sZ   , KGMNHE	gB 7 7%%% 	LLG ,,,w!7!77N b	I$~59L%*L=>>%:$;E<-..!YU3a77M ZZNiBiG==C''''' 	/5 -
 $
 $
 K44F % (#eNOO.D*E*EEe** /GaK61 /		!eW...,,, -U,,,OOUBK(((OOR$r((,----OOUEN+++ %U$$$Or   c                     g }| D ]W}t          |t                    r+|                    |           |                    d           B|                    |           X|S )Nr   )r   listextendr   )args
token_listargs      r   _flattenBlendArgsrF      sp    J # #c4   	#c"""g&&&&c""""r   c                     g }| D ]Y\  }}t          d |D                       rt          |          }|                    |           |r|                    |           Z|S )zqTakes a commands list as returned by programToCommands() and converts
    it back to a T2CharString program list.c              3   @   K   | ]}t          |t                    V  d S r   r   rA   r   rE   s     r   r   z$commandsToProgram.<locals>.<genexpr>   ,      55z#t$$555555r   )anyrF   rB   r   )r6   r   oprC   s       r   commandsToProgramrN      sz     G  D5555555 	+$T**Dt 	NN2Nr   c              #      K   t          |           |z  dk    rt          |           t          dt          |           |          D ]}| |||z            V  dS )z'Group the list el into groups of size nr   N)r-   r
   range)elnis      r   _everyNrT      sm      
2ww{ann1c"ggq!!  QUm r   c                   ,   e Zd Zed             Zed             Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zed             ZdS )!_GeneralizerDecombinerCommandsMapc              #   Z   K   t          |           dk    rt          |           d| fV  d S )Nr*   r%   r-   r
   rC   s    r   r%   z)_GeneralizerDecombinerCommandsMap.rmoveto   s:      t99>>T"""$r   c              #   j   K   t          |           dk    rt          |           d| d         dgfV  d S Nr   r%   r   rX   rY   s    r   r#   z)_GeneralizerDecombinerCommandsMap.hmoveto   sB      t99>>T"""47A,''''''r   c              #   j   K   t          |           dk    rt          |           dd| d         gfV  d S r[   rX   rY   s    r   r&   z)_GeneralizerDecombinerCommandsMap.vmoveto   sB      t99>>T"""1d1g,''''''r   c              #   ^   K   | st          |           t          | d          D ]} d| fV  	d S )Nr*   rlinetor
   rT   rY   s    r   r^   z)_GeneralizerDecombinerCommandsMap.rlineto   sS       	#T"""D!$$ 	$ 	$Dd#####	$ 	$r   c              #      K   | st          |           t          |           }	 	 dt          |          dgfV  ddt          |          gfV  +# t          $ r Y d S w xY wNTr^   r   r
   r,   r0   StopIterationrC   r8   s     r   hlinetoz)_GeneralizerDecombinerCommandsMap.hlineto   s       	#T"""$ZZ	1 488Q-0000 1d2hh-00001  	 	 	DD	   ,A 
AAc              #      K   | st          |           t          |           }	 	 ddt          |          gfV  dt          |          dgfV  +# t          $ r Y d S w xY wra   rb   rd   s     r   vlinetoz)_GeneralizerDecombinerCommandsMap.vlineto   s       	#T"""$ZZ	1 1d2hh-0000 488Q-00001  	 	 	DD	rf   c              #   ^   K   | st          |           t          | d          D ]} d| fV  	d S )N   	rrcurvetor_   rY   s    r   rk   z+_GeneralizerDecombinerCommandsMap.rrcurveto   sS       	#T"""D!$$ 	& 	&D%%%%%	& 	&r   c              #   |  K   t          |           dk     st          |           dz  dk    rt          |           t          |           dz  dk    r4d| d         | d         | d         | d         | d         dgfV  | dd          } t          | d          D ]&} d| d         d| d         | d         | d         dgfV  'd S N   r   r*   rk   r         r-   r
   rT   rY   s    r   	hhcurvetoz+_GeneralizerDecombinerCommandsMap.hhcurveto   s      t99q==CIIMA--T"""t99q=Aa$q'47DGT!Wa PQQQQ8DD!$$ 	L 	LDa!T!Wd1gtAw JKKKKK	L 	Lr   c           	   #   |  K   t          |           dk     st          |           dz  dk    rt          |           t          |           dz  dk    r4d| d         | d         | d         | d         d| d         gfV  | dd          } t          | d          D ]&} dd| d         | d         | d         d| d         gfV  'd S rm   rq   rY   s    r   	vvcurvetoz+_GeneralizerDecombinerCommandsMap.vvcurveto   s      t99q==CIIMA--T"""t99q=Aa$q'47DGQQ PQQQQ8DD!$$ 	L 	LDDGT!Wd1gq$q' JKKKKK	L 	Lr   c              #     K   t          |           dk     st          |           dz  dvrt          |           d }t          |           dz  dk    r*t          |           dz  dk    }| d d         | dd          }} t          | d          }	 	 t          |          } d	| d
         d
| d         | d         d
| d         gfV  t          |          } d	d
| d
         | d         | d         | d         d
gfV  g# t          $ r Y nw xY w|r\|} |r,d	| d
         d
| d         | d         | d         | d         gfV  d S d	d
| d
         | d         | d         | d         | d         gfV  d S d S Nrn      >   r   r   rn   rp   r*   r   rp   Trk   r   ro   r-   r
   rT   r0   rc   rC   	last_argslastStraightr8   s       r   	hvcurvetoz+_GeneralizerDecombinerCommandsMap.hvcurveto   s     t99q==CIIM==T"""	t99q=At99q=A-L"3B3ibcc)DT1	PBxx"T!Waa$q'1d1g$NOOOOBxx"QQa$q'47A$NOOOO	P
  	 	 	D	 	VD V"T!Waa$q'47DQRG$TUUUUUU"QQa$q'47DQRG$TUUUUUU	V 	V   A(C4 4
D Dc              #     K   t          |           dk     st          |           dz  dvrt          |           d }t          |           dz  dk    r*t          |           dz  dk    }| d d         | dd          }} t          | d          }	 	 t          |          } d	d
| d
         | d         | d         | d         d
gfV  t          |          } d	| d
         d
| d         | d         d
| d         gfV  g# t          $ r Y nw xY w|r\|} |r,d	d
| d
         | d         | d         | d         | d         gfV  d S d	| d
         d
| d         | d         | d         | d         gfV  d S d S rv   ry   rz   s       r   	vhcurvetoz+_GeneralizerDecombinerCommandsMap.vhcurveto  s     t99q==CIIM==T"""	t99q=At99q=A-L"3B3ibcc)DT1	PBxx"QQa$q'47A$NOOOOBxx"T!Waa$q'1d1g$NOOOO	P
  	 	 	D	 	VD V"QQa$q'47DQRG$TUUUUUU"T!Waa$q'47DQRG$TUUUUUU	V 	Vr~   c              #      K   t          |           dk     st          |           dz  dk    rt          |           | d d         | dd          }} t          | d          D ]} d| fV  	d|fV  d S )Nrw   rj   r*   rk   r^   rq   rC   r{   s     r   
rcurvelinez,_GeneralizerDecombinerCommandsMap.rcurveline  s      t99q==CIIMQ..T"""ss)T"##YiD!$$ 	& 	&D%%%%%)$$$$$$r   c              #      K   t          |           dk     st          |           dz  dk    rt          |           | d d         | dd          }} t          | d          D ]} d| fV  	d|fV  d S )Nrw   r*   r   ir^   rk   rq   r   s     r   
rlinecurvez,_GeneralizerDecombinerCommandsMap.rlinecurve(  s      t99q==CIIMQ..T"""ss)T"##YiD!$$ 	$ 	$Dd#####I&&&&&&r   N)__name__
__module____qualname__staticmethodr%   r#   r&   r^   re   rh   rk   rr   rt   r}   r   r   r    r   r   rV   rV      s}           \ 
 ( ( \(
 ( ( \(
 $ $ \$ 	 	 \	 	 	 \	 & & \& L L \L L L \L V V \V. V V \V. % % \% ' ' \' ' 'r   rV   c                    t          d | D                       rd | D             }n| }|d         }|d d         }t          |          |z  dz
  |dz   z  t          |          k    st          |           d |d |         D             }||d          t                    }fdt          d|          D             }d t	          ||          D             }|S )	Nc                 8    g | ]}t          |t                    S r   rI   rJ   s     r   
<listcomp>z)_convertBlendOpToArgs.<locals>.<listcomp>7  s"    777cJsD!!777r   c                 f    g | ].}t          |t                    rt          |          n|gD ]}|/S r   r   rA   _convertBlendOpToArgs)r   erS   s      r   r   z)_convertBlendOpToArgs.<locals>.<listcomp>8  s^     
 
 
2<Q2E2EN+A...A3
 
  
 
 
 
r   r   r   c                     g | ]}|gS r   r   rJ   s     r   r   z)_convertBlendOpToArgs.<locals>.<listcomp>Q  s    555SC5555r   c                 *    g | ]}||z            S r   r   )r   rS   	deltaArgs
numRegionss     r   r   z)_convertBlendOpToArgs.<locals>.<listcomp>T  s4       *+	!a*n$%  r   r   c                 &    g | ]\  }}||z   d gz   S )r   r   )r   abs      r   r   z)_convertBlendOpToArgs.<locals>.<listcomp>W  s&    FFF$!Q!a%1#+FFFr   )rL   r-   r
   rP   zip)		blendListrC   r:   defaultArgsnumDeltaValues	deltaList
blend_argsr   r   s	          @@r   r   r   2  s7   
 77Y77788 
 

 
 
  RI 9DTi'!+Ja(CII55###55D)$4555KYZZ I^^N    /4Q
/S/S  I GF#k9*E*EFFFJr   Fc                 "   g }t           }| D ]\  }}t          d |D                       rQ	 d |D             }nC# t          $ r6 |r0|                    d|f           |                    d|gf           n Y nw xY wt	          ||d           }|s|                    ||f           	  ||          D ]}|                    |           # t          $ r6 |r0|                    d|f           |                    d|gf           n Y w xY w|S )Nc                 8    g | ]}t          |t                    S r   rI   rJ   s     r   r   z&generalizeCommands.<locals>.<listcomp>`  s"    666#
3%%666r   c                 f    g | ].}t          |t                    rt          |          n|gD ]}|/S r   r   )r   rE   rR   s      r   r   z&generalizeCommands.<locals>.<listcomp>b  s`       6@d6K6KV-c222RUQV	      r   r+   )rV   rL   r
   r   getattr)r6   ignoreErrorsresultmappingrM   rC   funccommands           r   generalizeCommandsr   [  s   F/G " "D6666677 	 #        MM2t*---MM2t*---- .- wD)) 	MM2t*%%%
	4:: ' 'g&&&&' 	 	 	  r4j)))rB4j)))) *)	 Ms!   9=A98A9(#C=DDc                 R    t          t          t          | |          fi |          S r   )rN   r   r?   r   r1   kwargss      r   generalizeProgramr     2    ,WmDDOOOO  r   c                     | d         s | d         sd| dd         fS d| dd         fS | d         sd| dd         fS d| fS )a  
    Takes X,Y vector v and returns one of r, h, v, or 0 depending on which
    of X and/or Y are zero, plus tuple of nonzero ones.  If both are zero,
    it returns a single zero still.

    >>> _categorizeVector((0,0))
    ('0', (0,))
    >>> _categorizeVector((1,0))
    ('h', (1,))
    >>> _categorizeVector((0,2))
    ('v', (2,))
    >>> _categorizeVector((1,2))
    ('r', (1, 2))
    r   r   0Nvhrr   )r   s    r   _categorizeVectorr     sg     Q4 	t 	"1":!"":t 	"1":6Mr   c                 6    | dk    r|S |dk    r| S | |k    r| S d S )Nr   r   r   r   s     r   _mergeCategoriesr     s1    CxxCxxAvv4r   c                 2    | dk    rdS | dk    rdS | dv sJ | S )Nr   r   0rr   )r   s    r   _negateCategoryr     s0    CxxsCxxs9999Hr   c                    t          |           }d}g }d}||k     ro| |         }t          |t                    s!|                    |           |dz  }|dz  }n*|}t          |          dz
  }|g}|dz  }|d|z   z  }||k     rpt          | |         t                    rU|                    | |                    |dz  }||z  }||z   t          k    rn!||k     rt          | |         t                    Ut          |          }	g }
|D ]}|
                    |d                    |D ]-}|d         dk    sJ |
                    |dd                    .|
                    |	           |                    |
           ||	z   }||k     o|S )Nr   r   r   )r-   r   rA   r   r   rB   )rC   num_args	stack_usenew_argsrS   rE   prev_stack_usenum_sources	blendlist
num_blendsr   s              r   _convertToBlendCmdsr     s    4yyHIH	A
h,,1g#t$$ '	4OOC   FANII&N
 c((Q,KIFA[(Ix<<ZQ%>%><  a)))Q[(	{*]::  x<<ZQ%>%>< YJJ  * *!!#a&))))  - -2w!||||!!#ad),,,,j)))OOJ'''&3IS h,,V Or   c                    t          |t                    rt          | t                    rtt          |           t          |          k    s| d         |d         k    rt                      d t	          | d d         |d d                   D             | d         gz   S || }} t          | t                    r0| d         dk    sJ t          | d         |          g| dd          z   S | |z   S )Nr   c                 4    g | ]\  }}t          ||          S r   )_addArgs)r   vavbs      r   r   z_addArgs.<locals>.<listcomp>  s&    GGGRHR$$GGGr   r   r   )r   rA   r-   r
   r   r   r   s     r   r   r     s    !T a 	1vvQ1R5AbE>> ll"GG3q"vq"v3F3FGGG1R5'QQaqA!T +uzzzz1q!!"QqrrU**q5Lr   T0   c                 V   |rt          | |          } nt          |           } t          t          |           dz
  dd          D ]y}d| |         d         cxk    r| |dz
           d         k    rOn -| |dz
           d         | |         d         }}d|d         |d         z   |d         |d         z   gf| |dz
  <   | |= zt          t          |                     D ]}| |         \  }}	|dv r%t	          |	          \  }
}	|
|dd          z   |	f| |<   6|dk    rPt	          |	d d                   \  }}t	          |	d	d                    \  }}||z   d
z   ||	dd         z   |z   f| |<   |st          t          |           dz
  dd          D ]}| |         \  }}	|dk    r;t          |	          dk    sJ t	          |	dd                   \  }
}	|
dz   }||	f| |<   |dk    r| |= X|r|dv r|| |dz
           d         k    rs| |dz
           \  }}t          |	          dk    rt          |          dk    sJ 	 t          |	d         |d                   g}n# t          $ r Y w xY w||f| |dz
  <   | |= t          dt          |           dz
            D ]3}| |         \  }}	| |dz
           d         | |dz            d         }}|dv rM||cxk    rdk    r@n n=t          |	          dk    sJ |d         dk    r
d|	d         gn	|	d         dg}	d|	f| |<   |dd          d
k    rt          |	          dk    r||cxk    rdk    rn |d         dk    |d         dk    z  sJ |d         dk    rd}n |d         dk    rd}n|d         dk    rd}nd}|	d |          t          |	          d          z   |	|d          z   }	d|	f| |<   35t          t          |           dz
  dd          D ]}| |dz
           \  }}| |         \  }}d }||hddhk    r=||k    r|}n!|dk    rt          |          dk    rd}nt          |          dk    rd}n||fdv r|}n||hddhk    r|}nd
|dd          cxk    r|dd          k    rn n|d d         \  }}|d d         \  }}|dk    s|dk    s||cxk    rdk    rn nt          ||          }||dk    rt          ||          }|d|z   d
z   }nL|dk    r*t          |t          |                    }|.|dz   d
z   }nt          ||          }|K||z   d
z   }|r3t          |          t          |          z   |k     r|||z   f| |dz
  <   | |= t          t          |                     D ]A}| |         \  }}	|dv rd|dd          z   |	f| |<   %|dd          d
k    r|d d         dvr |d d         \  }}|dk    |dk    z  rt          |	          dz  dk    sJ |d k    rd}|d k    rd}|dk    r|}|dk    rt          |          }||hddhk    sJ ||f            t          |	          dz  rj||k    r>|dk    t          |	          d!z  dk    z  r |	d d	         |	dd          z   |	d	d         z   }	n&|dk    r |	dd         |	d d         z   |	dd          z   }	||z   d
z   |	f| |<   ACt          t          |                     D ]:}| |         \  }}	t          d" |	D                       r|t          |	          f| |<   ;| S )#N)r   r   r   r   r%   >   r^   r%   rk   r*   r   curvetorn   	00curvetoro   lineto0lineto>   re   rh   >   r   re   rh   r^   r   rp   r   )r   rj   r   r   >   r^   r   rk   r   rh   re   >   r   0movetor   >   hhhvrrvhvvr   rw   c              3   @   K   | ]}t          |t                    V  d S r   rI   rJ   s     r   r   z%specializeCommands.<locals>.<genexpr>  rK   r   )r   rA   rP   r-   r   r   r
   r.   r   r   rL   r   )r6   r   generalizeFirstpreserveTopologymaxstackrS   v1v2rM   rC   cc1args1c2args2_
other_argsr   prvnxtposop1op2new_opd0d1d2d3dop0s                                 r   specializeCommandsr     s=	   P  "%h\JJJ>> 3x==1$a,,  A<<<<(1q5/!*<<<<<<a!e_Q'!QB(2a52a5="Q%"Q%-*HIHQUO` 3x==!!  A;D''''--GAtbf*d*HQK)$rr(33IB)$rss)44IBr'I-utAaCy/@5/HHHQK	 6  s8}}q("b11 	 	A{HB [  4yyA~~~~+D1I664\ $h YQK  	R111rXa!e_Q=O7O7O (Q:4yyA~~#j//Q*>*>*>*> (a*Q- @ @AHH!   H#%x.QQK 1c(mma'((  A;DAE?1%xAq'9S222sc7N7N7N7NY7N7N7N7N7Nt99>>>>#%a5C<<AtAw<<d1gq\D$d+HQKabb6Y3t99>>cS6O6O6O6OK6O6O6O6O6OqESLRUc\2222!u||A#A#:
T

4 0 004:=D&-HQK 3x==1$a,, 3 3a!e_
Ua[
U :)[111czz+%%#e**//)FFZZ1__)F3ZSSSFF3ZIy111FF#abb',,,,SW,,,,,!WFB!WFBSyyB#IIrS R((AySyy$Q++9q9,s%b/!*<*<==:cI-%b"--:a)+  	c%jj3u::-88%uu}5HQUO 3x==!!    A;D'''122,,HQKabb6Y2bqb61O#O#O"1"vHCs
scz* *4yy1}))))czzczzczzczz%c**:#s+++c3Z+++4yy1} ?#::s
s4yy1}'9: C#CRCy494tBrE{Bczz#AaCy483d122h>)i/5HQK 3x==!! 8 8A;D5555555 	81$777HQKOs   6I
I! I!c                 R    t          t          t          | |          fi |          S r   )rN   r   r?   r   s      r   specializeProgramr     r   r   __main__r   zfonttools cffLib.specializerz&CFF CharString generalizer/specializer)descriptionr   r   *z	Commands.)metavarnargshelpz--num-regions
NumRegionsz5Number of variable-font regions for blend opertaions.)r   r   defaultr   c                 H    t          t          j        | dn|                    S )Nr   )r	   optionsnum_regions)r3   s    r   <lambda>r   ?  s    S!4'/QQw!WXX r   zProgram:z	Commands:zProgram from commands:zGeneralized program:zSpecialized program:r   )F)FTFr   ))__doc__fontTools.cffLibr   r   r   r?   rF   rN   rT   objectrV   r   r   r   r   r   r   r   r   r   r   r   sysr-   argvdoctestexittestmodfailedargparseArgumentParserparseradd_argument
parse_argsr   r   r1   r   printr6   program2r   r   r   <module>r     s    + * * * * *   - - -X X X Xv  
 
 
  M' M' M' M' M' M' M' M'`& & &R& & & &R     6    2 2 2j    _ _ _ _D	    zJJJ
s38}}"")***OOO$X$&<  F 	9CkRRR
D     --G & 	XX  ogo..G	E*	E//'
"
"###  -88H	E+	E(OOO  **H	E
"###	E//(
#
#$$$h	E
 !!!	E//++G]CC
D
DEEE	E
 !!!	E//++G]CC
D
DEEEEEY r   