a
    w=icc                     @   s  d Z ddlmZ dd Zdd Zd>dd	Zd
d Zdd Zdd ZG dd de	Z
dd Zd?ddZd@ddZdd Zdd Zdd Zdd  Zd!d" ZdAd%d&ZdBd'd(Zed)krddlZeejd*krddlZee j ddlZejd+d,d-Zej d.d/d0d1d2 ej d3d4d0dd5d6 e!ejd*d Z"e"j#du rBdnd7d8 Z$ee"j%Z%e&d9 e&ee% ee%e$Z'e&d: e&e' ee'Z(e&d; e&ee( e%e(ksJ e&d< e&eee%e$ e&d= e&eee%e$ dS )CaZ  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                 C   sn   t | tr|  } g }| D ]N}zt|}W n2 ty\   zt|}W n tyV   Y n0 Y n0 || q|S N)
isinstancestrsplitint
ValueErrorfloatappend)stringprogramtoken r   m/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/fontTools/cffLib/specializer.pystringToProgram   s    
r   c                 C   s   d dd | D S )N c                 s   s   | ]}t |V  qd S r   )r   ).0xr   r   r   	<genexpr>$       z"programToString.<locals>.<genexpr>)join)r   r   r   r   programToString#   s    r   Nc                 C   s  d}d}d}d}g }g }t | }|D ]l}	t|	ts@||	 q$|	dkr|dusTJ d|| }
|d }||
 d }|| d g|| d< ||t| d 7 }t|}q$n|	dkr|d }t|tu sJ nn|s>|	dv r>d	}|	d
v }|r|t||d  }nt|}|r>|d |A r>|d}|d|gf |	dv r|r\|d|f ||	g f |dt|gf n||	|f g }q$|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. If
	the vsindex argument is None, getNumRegions returns the default number
	of regions for the charstring, else 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.
	FNr   blend   Zvsindex>
   rmovetocntrmaskZhstemhintmaskZhstemhmvmovetohmovetoendcharZvstemZvstemhmT>   r   r       >   r   r   )	iterr   r   r
   lentyper   popnext)r   getNumRegionsZseenWidthOpvsIndexZlenBlendStackZlastBlendIndexcommandsstackitr   ZnumSourceFonts	numBlendsZnumBlendArgsZparityZnumArgswidthr   r   r   programToCommands'   sT    




r/   c                 C   s<   g }| D ].}t |tr,|| |d q|| q|S )Nr   )r   listextendr
   )argsZ
token_listargr   r   r   _flattenBlendArgs|   s    

r4   c                 C   sH   g }| D ]:\}}t dd |D r*t|}|| |r|| q|S )znTakes a commands list as returned by programToCommands() and converts
	it back to a T2CharString program list.c                 s   s   | ]}t |tV  qd S r   r   r0   r   r3   r   r   r   r      r   z$commandsToProgram.<locals>.<genexpr>)anyr4   r1   r
   )r*   r   opr2   r   r   r   commandsToProgram   s    
r9   c                 c   sD   t | | dkrt| tdt | |D ]}| |||  V  q(dS )z'Group the list el into groups of size nr   N)r$   r   range)elnir   r   r   _everyN   s    r>   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd ZdS )!_GeneralizerDecombinerCommandsMapc                 c   s"   t | dkrt| d| fV  d S )Nr!   r   r$   r   r2   r   r   r   r      s    z)_GeneralizerDecombinerCommandsMap.rmovetoc                 c   s*   t | dkrt| d| d dgfV  d S Nr   r   r   r@   rA   r   r   r   r      s    z)_GeneralizerDecombinerCommandsMap.hmovetoc                 c   s*   t | dkrt| dd| d gfV  d S rB   r@   rA   r   r   r   r      s    z)_GeneralizerDecombinerCommandsMap.vmovetoc                 c   s*   | st | t| dD ]} d| fV  qd S )Nr!   rlinetor   r>   rA   r   r   r   rC      s    z)_GeneralizerDecombinerCommandsMap.rlinetoc                 c   sV   | st | t| }z*dt|dgfV  ddt|gfV  qW n tyP   Y n0 d S NrC   r   r   r#   r'   StopIterationr2   r,   r   r   r   hlineto   s    z)_GeneralizerDecombinerCommandsMap.hlinetoc                 c   sV   | st | t| }z*ddt|gfV  dt|dgfV  qW n tyP   Y n0 d S rE   rF   rH   r   r   r   vlineto   s    z)_GeneralizerDecombinerCommandsMap.vlinetoc                 c   s*   | st | t| dD ]} d| fV  qd S )N   	rrcurvetorD   rA   r   r   r   rL      s    z+_GeneralizerDecombinerCommandsMap.rrcurvetoc                 c   s   t | dk st | d dkr$t| t | d dkrjd| d | d | d | d | d dgfV  | dd  } t| dD ]*} d| d d| d | d | d dgfV  qtd S N   r   r!   rL   r         r$   r   r>   rA   r   r   r   	hhcurveto   s    $*z+_GeneralizerDecombinerCommandsMap.hhcurvetoc              	   c   s   t | dk st | d dkr$t| t | d dkrjd| d | d | d | d d| d gfV  | dd  } t| dD ]*} dd| d | d | d d| d gfV  qtd S rM   rQ   rA   r   r   r   	vvcurveto   s    $*z+_GeneralizerDecombinerCommandsMap.vvcurvetoc                 c   sL  t | dk st | d dvr$t| d }t | d dkrbt | d dk}| d d | dd   } }t| d}zbt|} d| d	 d	| d | d d	| d
 gfV  t|} dd	| d	 | d | d | d
 d	gfV  qnW n ty   Y n0 |rH|} |rd| d	 d	| d | d | d | d
 gfV  n*dd	| d	 | d | d | d
 | d gfV  d S NrN      >   r   r   rN   rP   r!   r   rP   rL   r   rO   r$   r   r>   r'   rG   r2   	last_argsZlastStraightr,   r   r   r   	hvcurveto   s$    $
&,,z+_GeneralizerDecombinerCommandsMap.hvcurvetoc                 c   sL  t | dk st | d dvr$t| d }t | d dkrbt | d dk}| d d | dd   } }t| d}zbt|} dd	| d	 | d | d | d
 d	gfV  t|} d| d	 d	| d | d d	| d
 gfV  qnW n ty   Y n0 |rH|} |rdd	| d	 | d | d | d
 | d gfV  n*d| d	 d	| d | d | d | d
 gfV  d S rT   rW   rX   r   r   r   	vhcurveto   s$    $
&,,z+_GeneralizerDecombinerCommandsMap.vhcurvetoc                 c   sf   t | dk st | d dkr$t| | d d | dd   } }t| dD ]} d| fV  qHd|fV  d S )NrU   rK   r!   rL   rC   rQ   r2   rY   r   r   r   
rcurveline  s
    $z,_GeneralizerDecombinerCommandsMap.rcurvelinec                 c   sf   t | dk st | d dkr$t| | d d | dd   } }t| dD ]} d| fV  qHd|fV  d S )NrU   r!   r   irC   rL   rQ   r]   r   r   r   
rlinecurve  s
    $z,_GeneralizerDecombinerCommandsMap.rlinecurveN)__name__
__module____qualname__staticmethodr   r   r   rC   rI   rJ   rL   rR   rS   rZ   r[   r^   r_   r   r   r   r   r?      s4   




	
	





r?   c                    s   t dd | D r"dd | D }n| }|d }|d d }t|| d |d  t|ksft| dd |d | D }||d   t } fddtd|D }d	d t||D }|S )
Nc                 S   s   g | ]}t |tqS r   r5   r6   r   r   r   
<listcomp>  r   z)_convertBlendOpToArgs.<locals>.<listcomp>c                 S   s.   g | ]&}t |trt|n|gD ]}|q qS r   r   r0   _convertBlendOpToArgs)r   er=   r   r   r   rd     s   r   r   c                 S   s   g | ]
}|gqS r   r   r6   r   r   r   rd   /  r   c                    s   g | ]} ||  qS r   r   )r   r=   Z	deltaArgsZ
numRegionsr   r   rd   2  r   r   c                 S   s   g | ]\}}|| d g qS )r   r   )r   abr   r   r   rd   3  r   )r7   r$   r   r:   zip)Z	blendListr2   r-   ZdefaultArgsZnumDeltaValuesZ	deltaList
blend_argsr   rh   r   rf     s    
rf   Fc              	   C   s   g }t }| D ]\}}tdd |D rrzdd |D }W n8 typ   |rj|d|f |d|gf n Y n0 t||d }|s|||f qz||D ]}|| qW q ty   |r|d|f |d|gf n Y q0 q|S )Nc                 S   s   g | ]}t |tqS r   r5   r6   r   r   r   rd   ;  r   z&generalizeCommands.<locals>.<listcomp>c                 S   s.   g | ]&}t |trt|n|gD ]}|q qS r   re   )r   r3   r<   r   r   r   rd   =  r   r"   )r?   r7   r   r
   getattr)r*   ignoreErrorsresultmappingr8   r2   funccommandr   r   r   generalizeCommands6  s0    
rs   c                 K   s   t tt| |fi |S r   )r9   rs   r/   r   r(   kwargsr   r   r   generalizeProgramX  s    rv   c                 C   sV   | d s2| d s d| dd fS d| dd fS n | d sJd| dd fS d| fS dS )ab  
	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   )rx   r   r   r   _categorizeVector\  s    r{   c                 C   s(   | dkr|S |dkr| S | |kr$| S d S )Nrw   r   ri   rj   r   r   r   _mergeCategoriesv  s    r}   c                 C   s(   | dkrdS | dkrdS | dv s$J | S )Nry   rx   Z0rr   )ri   r   r   r   _negateCategory|  s    r~   c                 C   s,  t | }d}g }d}||k r(| | }t|tsL|| |d7 }|d7 }q|}t |d }|g}|d7 }|d| 7 }||k rt| | tr|| |  |d7 }||7 }|| tkrvqqvt |}	g }
|D ]}|
|d  q|D ]&}|d dksJ |
|dd  q|
|	 ||
 ||	 }q|S )Nr   r   r   )r$   r   r0   r
   r   r1   )r2   num_argsZ	stack_usenew_argsr=   r3   Zprev_stack_useZnum_sourcesZ	blendlistZ
num_blendsrl   r   r   r   _convertToBlendCmds  s@    






r   c                 C   s   t |trrt | trht| t|ks4| d |d kr:t dd t| d d |d d D | d g S ||  } }t | tr| d dksJ t| d |g| dd   S | | S )Nr   c                 S   s   g | ]\}}t ||qS r   )_addArgs)r   vaZvbr   r   r   rd     r   z_addArgs.<locals>.<listcomp>r   r   )r   r0   r$   r   rk   r   r|   r   r   r   r     s    

 .

r   T0   c              	   C   s  |rt | |d} nt| } tt| d ddD ]}d| | d   krZ| |d  d kr.n q.| |d  d | | d  }}d|d |d  |d |d  gf| |d < | |= q.tt| D ]}| | \}}	|dv rt|	\}
}	|
|dd   |	f| |< q|dkrt|	d d \}}t|	d	d  \}}|| d
 ||	dd  | f| |< qq|s~tt| d ddD ]}| | \}}	|dkrt|	dksJ t|	dd \}
}	|
d }||	f| |< |dkr| |= ql|rl|dv rl|| |d  d krl| |d  \}}t|	dkr(t|dks,J zt|	d |d g}W n ty^   Y qlY n0 ||f| |d < | |= qlqltdt| d D ]p}| | \}}	| |d  d | |d  d  }}|dv r2||  krdkr2n nHt|	dksJ |d dkrd|	d gn
|	d dg}	d|	f| |< q|dd  d
krt|	dkr||  krjdkrn n|d dk|d dkA sJ |d dkrd}n,|d dkrd}n|d dkrd}nd}|	d | t|	d |	|d   }	d|	f| |< qqtt| d ddD ]}| |d  \}}| | \}}d }||hddhkr||kr`|}n0|dkr~t|dkr~d}nt|dkrd}nP||fdv r|}n:||hddhkr|}n d
|dd    kr|dd  krn n|d d \}}|d d \}}|dks|dks||  kr<dkrDn nqt||}|d u r\q|dkrt||}|d u r~qd| d
 }nX|dkrt|t	|}|d u rq|d d
 }n$t||}|d u rؐq|| d
 }|rt|t| |k r||| f| |d < | |= qtt| D ]}| | \}}	|dv rbd|dd   |	f| |< q*|dd  d
kr*|d d dvr*|d d \}}|dk|dkA rt|	d dksJ |d krd}|d krd}|dkr|}|dkrt	|}||hddhksJ ||ft|	d r||krp|dkt|	d! dkA r|	d d	 |	dd   |	d	d  }	n.|dkr|	dd |	d d  |	dd   }	|| d
 |	f| |< q*q*tt| D ]6}| | \}}	t
d"d# |	D r|t|	f| |< q| S )$N)rn   r   r   r   r   >   r   rC   rL   r!   r\   ZcurvetorN   Z	00curvetorO   Zlineto0lineto>   rI   rJ   >   rI   r   rJ   rC   rx   rP   rz   )r   rK   r_   r^   >   )rC   r_   )rL   r^   rJ   rI   >   r   Z0movetory   >   ZvhvvrrhhZhvrw   rU   c                 s   s   | ]}t |tV  qd S r   r5   r6   r   r   r   r     r   z%specializeCommands.<locals>.<genexpr>)rs   r0   r:   r$   r{   r   r   r%   r}   r~   r7   r   )r*   rn   ZgeneralizeFirstZpreserveTopologyZmaxstackr=   v1v2r8   r2   cc1Zargs1c2Zargs2_Z
other_argsr   ZprvZnxtposZop1Zop2Znew_opZd0d1d2Zd3dZop0r   r   r   specializeCommands  s    (,,0$

 "&&<$
,0






$
&
$r   c                 K   s   t tt| |fi |S r   )r9   r   r/   rt   r   r   r   specializeProgram  s    r   __main__r   zfonttools cffLib.specialerz&CFF CharString generalizer/specializer)descriptionr   rr   *z	Commands.)metavarnargshelpz--num-regionsZ
NumRegionsz5Number of variable-font regions for blend opertaions.)r   r   defaultr   c                 C   s   t tj| d u rdn|  S )Nr   )r   optionsnum_regions)r)   r   r   r   <lambda>  r   r   zProgram:z	Commands:zProgram from commands:zGeneralized program:zSpecialized program:)N)F)N)FTFr   )N))__doc__ZfontTools.cffLibr   r   r   r/   r4   r9   r>   objectr?   rf   rs   rv   r{   r}   r~   r   r   r   r   r`   sysr$   argvdoctestexittestmodfailedargparseArgumentParserparseradd_argument
parse_argsr   r   r(   r   printr*   Zprogram2r   r   r   r   <module>   sf   
U
y#
"
4    
  



