U
    Dvfw                     @   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k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st)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   sr   t | tr|  } g }| D ]R}zt|}W n6 tk
r`   zt|}W n tk
rZ   Y nX Y nX || q|S N)
isinstancestrsplitint
ValueErrorfloatappend)stringprogramtoken r   @/tmp/pip-unpacked-wheel-qlge9rch/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>$   s     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k	sTtd|| }
|d }||
 d }|| d g|| d< ||t| d 7 }t|}q$n|	dkr|d }t|tkstnn|s>|	dkr>d	}|	d
k}|r|t||d  }nt|}|r>|d |A r>|d}|d|gf |	dk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>
   hstemhmhintmaskhstemcntrmaskvstemhmendcharvmovetovstemrmovetohmovetoT>   r    r#       >   r   r   )
iterr   r   r
   AssertionErrorlentyper   popnext)r   getNumRegionsZseenWidthOpvsIndexZlenBlendStackZlastBlendIndexcommandsstackitr   ZnumSourceFonts	numBlendsZnumBlendArgsZparityZnumArgswidthr   r   r   programToCommands'   sT    




r3   c                 C   s<   g }| D ].}t |tr,|| |d q|| q|S )Nr   )r   listextendr
   )argsZ
token_listargr   r   r   _flattenBlendArgs   s    

r8   c                 C   sH   g }| D ]:\}}t dd |D r*t|}|| |r|| q|S )zqTakes 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   r4   r   r7   r   r   r   r      s     z$commandsToProgram.<locals>.<genexpr>)anyr8   r5   r
   )r.   r   opr6   r   r   r   commandsToProgram   s    
r=   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    rB   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   r6   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   rD   rE   r   r   r   r#      s    z)_GeneralizerDecombinerCommandsMap.hmovetoc                 c   s*   t | dkrt| dd| d gfV  d S rF   rD   rE   r   r   r   r       s    z)_GeneralizerDecombinerCommandsMap.vmovetoc                 c   s*   | st | t| dD ]} d| fV  qd S )Nr$   rlinetor   rB   rE   r   r   r   rG      s    z)_GeneralizerDecombinerCommandsMap.rlinetoc                 c   sX   | st | t| }z*dt|dgfV  ddt|gfV  qW n tk
rR   Y nX d S NrG   r   r   r&   r+   StopIterationr6   r0   r   r   r   hlineto   s    z)_GeneralizerDecombinerCommandsMap.hlinetoc                 c   sX   | st | t| }z*ddt|gfV  dt|dgfV  qW n tk
rR   Y nX d S rI   rJ   rL   r   r   r   vlineto   s    z)_GeneralizerDecombinerCommandsMap.vlinetoc                 c   s*   | st | t| dD ]} d| fV  qd S )N   	rrcurvetorH   rE   r   r   r   rP      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$   rP   r         r(   r   rB   rE   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 rQ   rU   rE   r   r   r   	vvcurveto   s    *z+_GeneralizerDecombinerCommandsMap.vvcurvetoc                 c   sN  t | dk st | d dk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k
r   Y nX |rJ|} |r d| d	 d	| d | d | d | d
 gfV  n*dd	| d	 | d | d | d
 | d gfV  d S NrR      >   r   r   rR   rT   r$   r   rT   rP   r   rS   r(   r   rB   r+   rK   r6   	last_argsZlastStraightr0   r   r   r   	hvcurveto   s&    
&,,z+_GeneralizerDecombinerCommandsMap.hvcurvetoc                 c   sN  t | dk st | d dk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k
r   Y nX |rJ|} |r dd	| d	 | d | d | d
 | d gfV  n*d| d	 d	| d | d | d | d
 gfV  d S rX   r[   r\   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 )NrY   rO   r$   rP   rG   rU   r6   r]   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 )NrY   r$   r   irG   rP   rU   ra   r   r   r   
rlinecurve*  s    z,_GeneralizerDecombinerCommandsMap.rlinecurveN)__name__
__module____qualname__staticmethodr"   r#   r    rG   rM   rN   rP   rV   rW   r^   r_   rb   rc   r   r   r   r   rC      s4   







	
	


rC   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   r9   r:   r   r   r   
<listcomp>9  s     z)_convertBlendOpToArgs.<locals>.<listcomp>c                 S   s.   g | ]&}t |trt|n|gD ]}|q qS r   r   r4   _convertBlendOpToArgs)r   erA   r   r   r   rh   :  s    r   r   c                 S   s   g | ]
}|gqS r   r   r:   r   r   r   rh   S  s     c                    s   g | ]} ||  qS r   r   )r   rA   Z	deltaArgsZ
numRegionsr   r   rh   V  s    r   c                 S   s   g | ]\}}|| d g qS )r   r   )r   abr   r   r   rh   Y  s     )r;   r(   r   r>   zip)Z	blendListr6   r1   ZdefaultArgsZnumDeltaValuesZ	deltaList
blend_argsr   rl   r   rj   4  s$    

rj   Fc              	   C   s   g }t }| D ]\}}tdd |D rtzdd |D }W n: tk
rr   |rl|d|f |d|gf n Y nX t||d }|s|||f qz||D ]}|| qW q tk
r   |r|d|f |d|gf n Y qX q|S )Nc                 S   s   g | ]}t |tqS r   r9   r:   r   r   r   rh   b  s     z&generalizeCommands.<locals>.<listcomp>c                 S   s.   g | ]&}t |trt|n|gD ]}|q qS r   ri   )r   r7   r@   r   r   r   rh   d  s   r%   )rC   r;   r   r
   getattr)r.   ignoreErrorsresultmappingr<   r6   funccommandr   r   r   generalizeCommands]  s4    

rw   c                 K   s   t tt| |f|S r   )r=   rw   r3   r   r,   kwargsr   r   r   generalizeProgram  s    rz   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 )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|   r   r   r   _categorizeVector  s    r   c                 C   s(   | dkr|S |dkr| S | |kr$| S d S )Nr{   r   rm   rn   r   r   r   _mergeCategories  s    r   c                 C   s(   | dkrdS | dkrdS | dks$t | S )Nr}   r|   Z0r)r'   )rm   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st|
|dd  q|
|	 ||
 ||	 }q|S )Nr   r   r   )r(   r   r4   r
   r   r'   r5   )r6   num_argsZ	stack_usenew_argsrA   r7   Zprev_stack_useZnum_sourcesZ	blendlistZ
num_blendsrp   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stt| d |g| dd   S | | S )Nr   c                 S   s   g | ]\}}t ||qS r   )_addArgs)r   vaZvbr   r   r   rh     s     z_addArgs.<locals>.<listcomp>r   r   )r   r4   r(   r   ro   r'   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krt|	\}
}	|
|dd   |	f| |< q|dkrt|	d d \}}t|	d	d  \}}|| d
 ||	dd  | f| |< qq|stt| d ddD ]}| | \}}	|dkrt|	dkstt|	dd \}
}	|
d }||	f| |< |dkr| |= ql|rl|dkrl|| |d  d krl| |d  \}}t|	dkr(t|dks,tzt|	d |d g}W n tk
r`   Y qlY nX ||f| |d < | |= qlqltdt| d D ]p}| | \}}	| |d  d | |d  d  }}|dkr4||  krdkr4n nHt|	dkst|d dkrd|	d gn
|	d dg}	d|	f| |< q|dd  d
krt|	dkr||  krldkrn n|d dk|d dkA st|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rb|}n0|dkrt|dkrd}nt|dkrd}nP||fdk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rFn nqt	||}|d kr^q|dkrt	||}|d krqd| d
 }nX|dkrt	|t
|}|d krq|d d
 }n$t	||}|d krڐq|| d
 }|rt|t| |k r||| f| |d < | |= qtt| D ]}| | \}}	|dkrdd|dd   |	f| |< q,|dd  d
kr,|d d dkr,|d d \}}|dk|dkA rt|	d dkst|d krd}|d krd}|dkr|}|dkrt
|}||hddhkst||ft|	d r||krr|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)rr   r   r   r   r"   >   r"   rG   rP   r$   r`   ZcurvetorR   Z	00curvetorS   Zlineto0lineto>   rM   rN   >   rM   r   rN   rG   r|   rT   r~   )r   rO   rc   rb   >   rP   rb   rG   rc   rN   rM   >   r   0movetor}   >   hhvhrrhvvvr{   rY   c                 s   s   | ]}t |tV  qd S r   r9   r:   r   r   r   r     s     z%specializeCommands.<locals>.<genexpr>)rw   r4   r>   r(   r   r'   r   r   r)   r   r   r;   r   )r.   rr   ZgeneralizeFirstZpreserveTopologyZmaxstackrA   Zv1Zv2r<   r6   cZc1Zargs1c2Zargs2_Z
other_argsr   ZprvZnxtposZop1Zop2Znew_opZd0d1Zd2Zd3dZop0r   r   r   specializeCommands  s   (,,0$

& "&&<$
,0










$




&
$r   c                 K   s   t tt| |f|S r   )r=   r   r3   rx   r   r   r   specializeProgram  s    r   __main__r   zfonttools cffLib.specialerz&CFF CharString generalizer/specializer)descriptionr   rv   *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 krdn|  S )Nr   )r   optionsnum_regions)r-   r   r   r   <lambda>A      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   r3   r8   r=   rB   objectrC   rj   rw   rz   r   r   r   r   r   r   r   rd   sysr(   argvdoctestexittestmodfailedargparseArgumentParserparseradd_argument
parse_argsr   r   r,   r   printr.   Zprogram2r'   r   r   r   r   <module>   s|   
] )
)

	5    
  $



