o
    h                      @   s  d Z ddlmZ dd Zdd ZdOdd	Zd
d Zdd Zdd ZG dd de	Z
dd ZdPddZdOddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Z		%		&dQd'd(ZdOd)d*Zed+krjddlZeejd,kryddlZee j ddlZejd-d.d/Z e j!d0d1d2d3d4 e j!d5d6d2dd7d8 e j!d9d:dd;d< e j!d=d>e"d?d@ e #ejd,d Z$e$j%re$j&du rdndAdB Z'ee$j%Z%e(dC e(ee% ee%e'Z)e(dD e(e) ee)Z*e(dE e(ee* e%e*ksJ e(dF e(eee%e' e(dG e(eee%e' e$j+rlddHl,m-Z- e-e$j+Z+e+dI j.j/d Z0e0j1Z2e23 D ]Z4e2e4 Z5e56  e5j7j'Z'ee5j%e'edJe5_%q*e$j8du rXddKl9m:Z: e:e$j+d%dLdMZ;ne$j8Z;e;rne(dNe; e+<e; dS dS dS dS )RaZ  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 ]'}zt|}W n ty.   zt|}W n	 ty+   Y nw Y nw || q|S N)
isinstancestrsplitint
ValueErrorfloatappend)stringprogramtoken r   p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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>$   s    z"programToString.<locals>.<genexpr>)join)r   r   r   r   programToString#   s   r   Nc                 C   s  d}d}d}d}g }g }t | }|D ]}	t|	ts||	 q|	dkrU|dus)J d|| }
|d }||
 d }|| d g|| d< t|}||| d 7 }|}q|	dkrf|d }t|tu seJ n2|s|	dv rd	}|	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. 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lenStackparitynumArgswidthr   r   r   programToCommands'   sV   


r;   c                 C   s<   g }| D ]}t |tr|| |d q|| q|S )Nr   )r   listextendr
   )args
token_listargr   r   r   _flattenBlendArgs   s   

rA   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       | ]}t |tV  qd S r   r   r<   r   r@   r   r   r   r          z$commandsToProgram.<locals>.<genexpr>)anyrA   r=   r
   )r1   r   opr>   r   r   r   commandsToProgram   s   

rH   c                 c   sF    t | }|| dkrt| td||D ]}| |||  V  qdS )z'Group the list el into groups of size nr   N)r(   r   range)elnlir   r   r   _everyN   s   rN   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   r>   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   rP   rQ   r   r   r   r         z)_GeneralizerDecombinerCommandsMap.hmovetoc                 c   s,    t | dkrt| dd| d gfV  d S rR   rP   rQ   r   r   r   r!      rS   z)_GeneralizerDecombinerCommandsMap.vmovetoc                 c   ,    | st | t| dD ]} d| fV  qd S )Nr%   rlinetor   rN   rQ   r   r   r   rU         z)_GeneralizerDecombinerCommandsMap.rlinetoc                 c   sT    | st | t| }z	 dt|dgfV  ddt|gfV  q ty)   Y d S w NTrU   r   r   r'   r+   StopIterationr>   r3   r   r   r   hlineto      z)_GeneralizerDecombinerCommandsMap.hlinetoc                 c   sT    | st | t| }z	 ddt|gfV  dt|dgfV  q ty)   Y d S w rX   rY   r[   r   r   r   vlineto   r]   z)_GeneralizerDecombinerCommandsMap.vlinetoc                 c   rT   )N   	rrcurvetorV   rQ   r   r   r   r`      rW   z+_GeneralizerDecombinerCommandsMap.rrcurvetoc                 c   s    t | }|dk s|d dkrt| |d 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  q9d S N   r   r%   r`   r         r(   r   rN   r>   rL   r   r   r   	hhcurveto      *(z+_GeneralizerDecombinerCommandsMap.hhcurvetoc              	   c   s    t | }|dk s|d dkrt| |d 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  q9d S ra   re   rf   r   r   r   	vvcurveto   rh   z+_GeneralizerDecombinerCommandsMap.vvcurvetoc                 c   sF   t | }|dk s|d dvrt| d }|d dkr.|d dk}| d d | dd  } }t| d}z0	 t|} d	| d
 d
| d | d d
| d gfV  t|} d	d
| d
 | d | d | d d
gfV  q5 tyl   Y nw |r|} |rd	| 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 Nrb      >   r   r   rb   rd   r%   r   rd   Tr`   r   rc   r(   r   rN   r+   rZ   r>   rL   	last_argslastStraightr3   r   r   r   	hvcurveto   2   
&&..z+_GeneralizerDecombinerCommandsMap.hvcurvetoc                 c   sF   t | }|dk s|d dvrt| d }|d dkr.|d dk}| d d | dd  } }t| d}z0	 t|} d	d
| d
 | d | d | d d
gfV  t|} d	| d
 d
| d | d d
| d gfV  q5 tyl   Y nw |r|} |rd	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 rj   rm   rn   r   r   r   	vhcurveto  rr   z+_GeneralizerDecombinerCommandsMap.vhcurvetoc                 c   h    t | }|dk s|d dkrt| | d d | dd  } }t| dD ]} d| fV  q%d|fV  d S )Nrk   r_   r%   r`   rU   re   r>   rL   ro   r   r   r   
rcurveline%     z,_GeneralizerDecombinerCommandsMap.rcurvelinec                 c   rt   )Nrk   r%   r   irU   r`   re   rv   r   r   r   
rlinecurve/  rx   z,_GeneralizerDecombinerCommandsMap.rlinecurveN)__name__
__module____qualname__staticmethodr    r   r!   rU   r\   r^   r`   rg   ri   rq   rs   rw   ry   r   r   r   r   rO      s6    













	rO   c                    s   t dd | D rdd | D }n| }|d }|d d }t|}|| d |d  |ks3t| dd |d | D }||d   t } fddtd|D }d	d t||D }|S )
Nc                 S      g | ]}t |tqS r   rC   rD   r   r   r   
<listcomp>?      z)_convertBlendOpToArgs.<locals>.<listcomp>c                 S   .   g | ]}t |trt|n|gD ]}|qqS r   r   r<   _convertBlendOpToArgs)r   erM   r   r   r   r   @  s    r   r   c                 S   s   g | ]}|gqS r   r   rD   r   r   r   r   Z  s    c                    s   g | ]
} ||  qS r   r   )r   rM   	deltaArgs
numRegionsr   r   r   ]  s    r   c                 S   s   g | ]\}}|| d g qS )r   r   )r   abr   r   r   r   `  s    )rF   r(   r   rI   zip)	blendListr>   r5   rL   defaultArgsnumDeltaValues	deltaList
blend_argsr   r   r   r   :  s&   

r   Fc              	   C   s   g }t }| D ]o\}}tdd |D r9z	dd |D }W n ty8   |r5|d|f |d|gf n Y nw t||d }|d u rK|||f qz||D ]}|| qPW q tyu   |rr|d|f |d|gf n Y qw |S )Nc                 S   r~   r   rC   rD   r   r   r   r   i  r   z&generalizeCommands.<locals>.<listcomp>c                 S   r   r   r   )r   r@   rK   r   r   r   r   k  s    r&   )rO   rF   r   r
   getattr)r1   ignoreErrorsresultmappingrG   r>   funccommandr   r   r   generalizeCommandsd  s>   
	r   c                 K      t tt| |fi |S r   )rH   r   r;   r   r,   kwargsr   r   r   generalizeProgram     r   c                 C   sP   | 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   r   r   r   _categorizeVector  s   r   c                 C   s(   | dkr|S |dkr| S | |kr| S d S )Nr   r   r   r   r   r   r   _mergeCategories  s   r   c                 C   s(   | dkrdS | dkrdS | dv sJ | S )Nr   r   0rr   )r   r   r   r   _negateCategory  s   r   c                 C   sF  t | }d}g }d}||k r| | }|d7 }t|ts%|| |d7 }nx|}t |d }|g}|d| 7 }||k rgt| | trg|| tk rg|| |  |d7 }||7 }||k rgt| | trg|| tk sGt |}	g }
|D ]	}|
|d  qo|D ]}|d dksJ |
|dd  q{|
|	 ||
 ||	 }||k s|S )Nr   r   r   )r(   r   r<   r
   r   r=   )r>   num_args	stack_usenew_argsrM   r@   prev_stack_usenum_sources	blendlist
num_blendsr   r   r   r   _convertToBlendCmds  sJ   




0r   c                 C   s   t |tr9t | tr4t| t|ks| d |d krt dd t| d d |d d D | d g S || } }t | trT| d dksFJ t| d |g| dd   S | | S )Nr   c                 S   s   g | ]	\}}t ||qS r   )_addArgs)r   vavbr   r   r   r      s    z_addArgs.<locals>.<listcomp>r   r   )r   r<   r(   r   r   r   r   r   r   r   r     s   

 .

r   c                 C   sP   d}d}| D ]}t |tu rt||t| }||d 7 }q|d7 }qt||S )Nr   r   r   )r)   r<   max_argsStackUse)r>   stackLenmaxLenr@   r   r   r   r   	  s   

r   T0   c           "   	   C   s  |r	t | |d} nt| } tt| d ddD ]@}d| | d   kr-| |d  d krWn q| |d  d | | d }}d|d |d  |d |d  gf| |d < | |= qtt| D ]J}| | \}}	|dv r}t|	\}
}	|
|dd   |	f| |< q^|dkrt|	d d \}}t|	d	d  \}}|| d
 ||	dd  | f| |< q^q^|s5tt| d ddD ]~}| | \}}	|dkrt|	dksJ t|	dd \}
}	|
d }||	f| |< |dkr| |= q|r4|dv r4|| |d  d kr4| |d  \}}t|	dkrt|dksJ zt|	d |d g}W n
 ty'   Y qw ||f| |d < | |= qqtdt| d D ]}| | \}}	| |d  d | |d  d }}|dv r||  krhdkrn n$t|	dkssJ |d dkrd|	d gn|	d dg}	d|	f| |< q>|dd  d
krt|	dkr||  krdkrn q>|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>q>| rt| d d nd}tt| d ddD ]}| |d  \}}| | \}}d }||hddhkrL||kr3|}nt|}|dkrD|dkrDd}n|dkrKd}n||fdv rV|}n||hddhkrb|}nd
|dd    krv|dd  krn nz|d d \}}|d d \}}|dks|dks||  krdkrn nqt	||}|d u rq|dkrt	||}|d u rqd| d
 }n,|dkrt	|t
|}|d u rِq|d d
 }nt	||}|d u rq|| d
 }t|}t|t|| } |r| |k r||| f| |d < | |= | }q|}qtt| D ]}| | \}}	|dv r=d|dd   |	f| |< q"|dd  d
kr|d d dvrt|	}|d d \}!}|!dk|dkA rm|d dksmJ |!d krtd}!|d kr{d}|!dkr|}!|dkrt
|!}|!|hddhksJ |!|f|d r|!|kr|!dk|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 ]}| | \}}	td"d# |	D r|t|	f| |< q| S )$N)r   r   r   r   r    >   rU   r    r`   r%   ru   curvetorb   	00curvetorc   lineto0lineto>   r\   r^   >   r   r\   r^   rU   r   rd   r   )r   r_   ry   rw   >   rU   ry   r`   rw   r^   r\   >   r   0movetor   >   hhhvrrvhvvr   rk   c                 s   rB   r   rC   rD   r   r   r   r   ;  rE   z%specializeCommands.<locals>.<genexpr>)r   r<   rI   r(   r   r   r   r)   r   r   r   r   rF   r   )"r1   r   generalizeFirstpreserveTopologymaxstackrM   v1v2rG   r>   cc1args1c2args2_
other_argsr   prvnxtposstackUseop1op2new_oprL   d0d1d2d3dargs1StackUsecombinedStackUseop0r   r   r   specializeCommands  s&  (,,0$& "&&>$

,0









$





$
$r   c                 K   r   r   )rH   r   r;   r   r   r   r   specializeProgramA  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   z--fontFONTFILEzCFF2 font to specialize.)r   r   r   z-oz--output-filezOutput font file name.)r)   r   c                 C   s   t tj| d u rd S |  S )Nr   )r   optionsnum_regions)r.   r   r   r   <lambda>p  s
    r   zProgram:z	Commands:zProgram from commands:zGeneralized program:zSpecialized program:)TTFontCFF2)r   )makeOutputFileNamez.specialized)	overWritesuffixSavingr   )F)FTFr   )=__doc__fontTools.cffLibr   r   r   r;   rA   rH   rN   objectrO   r   r   r   r   r   r   r   r   r   r   r   rz   sysr(   argvdoctestexittestmodfailedargparseArgumentParserparseradd_argumentr   
parse_argsr   r   r   r,   printr1   program2fontfontTools.ttLibr   cfftopDictIndexcff2CharStringscharstringskeys	glyphName
charstring	decompileprivateoutput_filefontTools.misc.cliToolsr   outfilesaver   r   r   r   <module>   s   
\	 
*
)
	:
  
-





=