o
    e                     @   sT  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlZddlZeeZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdgd Zdeg edd< deg edd< deg edd< deg edd< eed< eedksJ edd Zeed< eed< edd Zeed < eed!< eed< g d"Zd#d$ e eD Z!d%Z"d&d' Z#g d(Z$d)d* Z%e%d+Z&e%d,Z'e%d-Z(ej)fd.d/Z*ed!ed0 Z+d1d2 Z,G d3d4 d4e-Z.G d5d6 d6e/Z0g d7Z1G d8d9 d9e0Z2G d:d; d;e2Z3G d<d= d=e3Z4G d>d? d?e/Z5G d@dA dAe5Z6G dBdC dCe/Z7dDdE Z8dS )FzwpsCharStrings.py -- module implementing various kinds of CharStrings:
CFF dictionary data and Type1/Type2 CharStrings.
    )fixedToFloatfloatToFixedfloatToFixedToStrstrToFixedToFloat)bytechrbyteord	bytesjoinstrjoin)	BoundsPenNc                 C   sb   |dkr|t || f}|d }n|}z| j| }W n ty'   d |f Y S w | |}||fS )N      )r   	operatorsKeyErrorhandle_operator)selfb0dataindexopoperatorvalue r   K/var/www/myenv/lib/python3.10/site-packages/fontTools/misc/psCharStrings.pyread_operator   s   

r   c                 C   s   |d |fS )N   r   r   r   r   r   r   r   r   	read_byte"      r   c                 C   s(   t || }|d d | d |d fS )N      l   r   r   r   r   r   r   b1r   r   r   read_smallInt1&   s   r$   c                 C   s*   t || }|d  d | d |d fS )N   r   r    r   r!   r"   r   r   r   read_smallInt2+   s   r&   c                 C   &   t d|||d  \}||d fS )N>h   structunpackr   r   r   r   r   r   r   r   read_shortInt0      r.   c                 C   r'   )N>l   r*   r-   r   r   r   read_longInt5   r/   r2   c                 C   s.   t d|||d  \}t|dd|d fS )Nr0   r1      precisionBits)r+   r,   r   r-   r   r   r   read_fixed1616:   s   r6   c                 C   s   t sJ t|fS N)NotImplementedErrorNotImplementedr   r   r   r   read_reserved?   s   r:   c                 C   sh   d}	 t || }|d }|d@ d? }|d@ }|dkrn|t|  }|dkr'n|t|  }qt||fS )N Tr      r1      )r   realNibblesfloat)r   r   r   r   numberbnibble0nibble1r   r   r   read_realNumberD   s   rD   r          r   r1   r%               )0123456789.EE-N-c                 C   s   i | ]\}}||qS r   r   ).0ivr   r   r   
<dictcomp>w   s    r\      c                 C   s~   i }i }| D ]4}t |dkr|d ||d < n
|dd  ||d < t|d tr1|d ||d < q|d f||d < q||fS )Nr)   r   r   )len
isinstancetuple)operatorListoperopcitemr   r   r   buildOperatorDict|   s   re   )4r   hstem   vstemr1   vmoveto   rlineto   hlineto   vlineto   	rrcurveto
   callsubr   return   endchar)r=   vsindex)r3   blend)   hstemhm)   hintmask)   cntrmask   rmoveto   hmoveto)   vstemhm)   
rcurveline)   
rlinecurve)   	vvcurveto)   	hhcurveto)rI   	callgsubrrJ   	vhcurveto   	hvcurveto)r   r   ignore))r   ri   and))r   r1   or))r   rn   not))r   rw   store))r   	   abs))r   rz   add))r   r}   sub)r   r   div))r      load))r   r   neg))r   r=   eq))r   r   drop))r   r   put))r   r   get))r   r   ifelse))r   r   random))r   r   mul))r   r   sqrt))r   r   dup))r   rH   exch))r   rI   r   ))r   rJ   roll))r   "   hflex))r   #   flex))r   $   hflex1))r   %   flex1c                 C   sd   | dkrt d}t d}n| dkrd }t d}n| dksJ t d}d }|t tjtj|fdd}|S )	NcffrH   rI   t1rG   t2c                 S   s  d|   kr
dkrn n|| d }|S d|   krdkr4n n| d } || d? d || d@  }|S d	|   kr>d
krUn n|  d } || d? d || d@  }|S |d urnd|   krcdkrnn n	||d|  }|S |d u rt d |d|d|  }|S ||d|  }|S )Nik   r   r    k  rw   r   rG   iir%   i i  r(   zk4-byte T2 number got passed to the IntType handler. This should happen only when reading in old XML files.
r0   )logwarning)r   
fourByteOpr   packr,   	twoByteOpcoder   r   r   	encodeInt   s,   
 	z getIntEncoder.<locals>.encodeInt)r   r+   r   r,   )formatr   r   r   r   r   r   getIntEncoder   s    


%r   r   r   r   c                 C   s2   t | dd}|d@ dkrt|d? S d|d| S )zFor T2 onlyr3   r4   i  r      r0   )r   encodeIntT2)fr   r   r   r   r   encodeFixed   s   r   r=   c                 C   s$  | dkrt S d|  }|d d dkr|dd  }n|d d dkr)d|dd   }g }|ra|d	 }|dd  }|d
krX|d d }|dkrN|dd  }d}n
|dkrX|dd  }|t|  |s-|d t|d rq|d td}td	t|dD ]}|t|| d> ||d  B  }q}|S )Ng        z%.8Gr)   z0.r   ri   z-0.rX   r   rV   rW   +r=   rJ   r1   )realZeroBytesappendrealNibblesDictr^   r   range)r   snibblescc2drZ   r   r   r   encodeFloat  s6   

"r   c                   @   s   e Zd ZdS )CharStringCompileErrorN)__name__
__module____qualname__r   r   r   r   r   +  s    r   c                   @   sZ  e Zd ZdTddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZeZd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dS )USimpleT2DecompilerNc                 C   s8   || _ t|| _|| _t|| _|| _|| _|   d S r7   )
localSubrscalcSubrBias	localBiasglobalSubrs
globalBiasprivateblenderreset)r   r   r   r   r   r   r   r   __init__0  s   

zSimpleT2Decompiler.__init__c                 C   s(   g | _ g | _d| _d| _d| _d| _d S Nr   )callingStackoperandStack	hintCounthintMaskBytes
numRegionsvsIndexr   r   r   r   r   9  s   
zSimpleT2Decompiler.resetc                 C   s   | j | | }|rg }|j}ndd }| jj}d}	 ||\}}}|d u r*n-|| |rRd| }	t| |	d }
|
d urM|
|}|rL|\}}|| n	|   n|| q|r^|| | j d= d S )Nc                 S      d S r7   r   )xr   r   r   <lambda>H  s    z,SimpleT2Decompiler.execute.<locals>.<lambda>r   Top_)r   r   needsDecompilationr   getTokengetattrpopall
setProgram)r   
charStringr   programpushToProgrampushToStackr   token
isOperatorhandlerNamehandlerrvr   r   r   r   executeA  s8   

zSimpleT2Decompiler.executec                 C      | j d }| j d= |S Nr   r   r   r   r   r   r   pop`     
zSimpleT2Decompiler.popc                 C   s    | j d d  }g | j d d < |S r7   r  r   stackr   r   r   r   e  s   zSimpleT2Decompiler.popallc                 C   s   | j | d S r7   )r   r   r  r   r   r   pushj  s   zSimpleT2Decompiler.pushc                 C   s   | j r	 d S d S r7   r  r   r   r   r   r   	op_returnm  s   zSimpleT2Decompiler.op_returnc                 C   r   r7   r   r  r   r   r   
op_endcharq     zSimpleT2Decompiler.op_endcharc                 C   r   r7   r   r  r   r   r   	op_ignoret  r  zSimpleT2Decompiler.op_ignorec                 C   &   |   }| j|| j  }| | d S r7   )r  r   r   r  r   r   	subrIndexsubrr   r   r   op_callsubrw     zSimpleT2Decompiler.op_callsubrc                 C   r  r7   )r  r   r   r  r  r   r   r   op_callgsubr|  r  zSimpleT2Decompiler.op_callgsubrc                 C      |    d S r7   
countHintsr  r   r   r   op_hstem  r   zSimpleT2Decompiler.op_hstemc                 C   r  r7   r  r  r   r   r   op_vstem  r   zSimpleT2Decompiler.op_vstemc                 C   r  r7   r  r  r   r   r   
op_hstemhm  r   zSimpleT2Decompiler.op_hstemhmc                 C   r  r7   r  r  r   r   r   
op_vstemhm  r   zSimpleT2Decompiler.op_vstemhmc                 C   s>   | j s|   | jd d | _ | jd || j \}}||fS )Nrt   rw   r   )r   r   r   r   getBytes)r   r   r   r   r   r   op_hintmask  s
   zSimpleT2Decompiler.op_hintmaskc                 C       |   }| jt|d  | _d S Nr)   )r   r   r^   r   argsr   r   r   r        zSimpleT2Decompiler.countHintsc                 C      t r7   r8   r  r   r   r   op_and  r  zSimpleT2Decompiler.op_andc                 C   r,  r7   r-  r  r   r   r   op_or  r  zSimpleT2Decompiler.op_orc                 C   r,  r7   r-  r  r   r   r   op_not  r  zSimpleT2Decompiler.op_notc                 C   r,  r7   r-  r  r   r   r   op_store  r  zSimpleT2Decompiler.op_storec                 C   r,  r7   r-  r  r   r   r   op_abs  r  zSimpleT2Decompiler.op_absc                 C   r,  r7   r-  r  r   r   r   op_add  r  zSimpleT2Decompiler.op_addc                 C   r,  r7   r-  r  r   r   r   op_sub  r  zSimpleT2Decompiler.op_subc                 C   r,  r7   r-  r  r   r   r   op_div  r  zSimpleT2Decompiler.op_divc                 C   r,  r7   r-  r  r   r   r   op_load  r  zSimpleT2Decompiler.op_loadc                 C   r,  r7   r-  r  r   r   r   op_neg  r  zSimpleT2Decompiler.op_negc                 C   r,  r7   r-  r  r   r   r   op_eq  r  zSimpleT2Decompiler.op_eqc                 C   r,  r7   r-  r  r   r   r   op_drop  r  zSimpleT2Decompiler.op_dropc                 C   r,  r7   r-  r  r   r   r   op_put  r  zSimpleT2Decompiler.op_putc                 C   r,  r7   r-  r  r   r   r   op_get  r  zSimpleT2Decompiler.op_getc                 C   r,  r7   r-  r  r   r   r   	op_ifelse  r  zSimpleT2Decompiler.op_ifelsec                 C   r,  r7   r-  r  r   r   r   	op_random  r  zSimpleT2Decompiler.op_randomc                 C   r,  r7   r-  r  r   r   r   op_mul  r  zSimpleT2Decompiler.op_mulc                 C   r,  r7   r-  r  r   r   r   op_sqrt  r  zSimpleT2Decompiler.op_sqrtc                 C   r,  r7   r-  r  r   r   r   op_dup  r  zSimpleT2Decompiler.op_dupc                 C   r,  r7   r-  r  r   r   r   op_exch  r  zSimpleT2Decompiler.op_exchc                 C   r,  r7   r-  r  r   r   r   op_index  r  zSimpleT2Decompiler.op_indexc                 C   r,  r7   r-  r  r   r   r   op_roll  r  zSimpleT2Decompiler.op_rollc           
      C   s   | j dkr| j | _ |  }|| j d  }| jd u r&| j||  d = d S t| j| }||  }}||k r]|| j  }| j|| }| | j|}	| j|  |	7  < |}|d7 }||k s7g | j|d < d S Nr   r   )r   r   getNumRegionsr  r   r   r^   r   )
r   r   	numBlendsnumOpsargiend_argstupleinext_tideltasdeltar   r   r   op_blend  s&   


zSimpleT2Decompiler.op_blendc                 C   s    |   }|| _| j|| _d S r7   )r  r   r   rE  r   )r   r   vir   r   r   
op_vsindex  s   zSimpleT2Decompiler.op_vsindexNN)-r   r   r   r   r   r  r  r   r  r  r  r  r  r  r!  r"  r#  r$  r&  op_cntrmaskr   r.  r/  r0  r1  r2  r3  r4  r5  r6  r7  r8  r9  r:  r;  r<  r=  r>  r?  r@  rA  rB  rC  rN  rP  r   r   r   r   r   /  sV    
	r   )rf   rh   rk   rm   rp   rs   rv   )r   	closepathry   r|   )r   hsbwr   r   r   r   r   )r   
dotsection))r   r   vstem3))r   r)   hstem3))r   rq   seac))r   rt   sbwr   ))r   r3   callothersubr))r      r  ))r   !   setcurrentpointc                   @   sT   e Zd Z		dddZdd ZdddZd	d
 Zdd Zdd Zdd Z	dd Z
dS )T2WidthExtractorNc                 C   s"   t | |||| || _|| _d S r7   )r   r   nominalWidthXdefaultWidthX)r   r   r   r_  r`  r   r   r   r   r   r     s   	
zT2WidthExtractor.__init__c                 C   s   t |  d| _d| _d S r   )r   r   gotWidthwidthr   r   r   r   r   $  s   

zT2WidthExtractor.resetr   c                 C   s`   |   }| js.|t|d A r'| jd usJ d| j|d  | _|dd  }n| j| _d| _|S )Nr)   z5CFF2 CharStrings must not have an initial width valuer   r   )r   ra  r^   r`  r_  rb  )r   evenOddr*  r   r   r   popallWidth)  s   zT2WidthExtractor.popallWidthc                 C   r'  r(  )rd  r   r^   r)  r   r   r   r   8  r+  zT2WidthExtractor.countHintsc                 C   r  r7   rd  r  r   r   r   
op_rmoveto<  r   zT2WidthExtractor.op_rmovetoc                 C      |  d d S Nr   re  r  r   r   r   
op_hmoveto?     zT2WidthExtractor.op_hmovetoc                 C   rg  rh  re  r  r   r   r   
op_vmovetoB  rj  zT2WidthExtractor.op_vmovetoc                 C   r  r7   re  r  r   r   r   r  E  r   zT2WidthExtractor.op_endcharrQ  r   )r   r   r   r   r   rd  r   rf  ri  rk  r  r   r   r   r   r^    s    

r^  c                       s  e Zd Z		djddZdd Z fddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1d`da Z2dbdc Z3ddde Z4dfdg Z5dhdi Z6  Z7S )kT2OutlineExtractorNc              	   C   s&   t | |||||| || _d| _d S r   )r^  r   pen	subrLevel)r   rn  r   r   r_  r`  r   r   r   r   r   r   J  s   
	
zT2OutlineExtractor.__init__c                 C   s    t |  d| _d| _d| _d S )Nr   r   r   )r^  r   currentPoint	sawMoveToro  r   r   r   r   r   `  s   

zT2OutlineExtractor.resetc                    sB   |  j d7  _ t | |  j d8  _ | j dkr|   d S d S Nr   r   )ro  superr  endPath)r   r   	__class__r   r   r  f  s   
zT2OutlineExtractor.executec                 C   s,   | j \}}||d  ||d  f}|| _ |S rD  )rq  )r   pointr   yr   r   r   
_nextPointm  s   
zT2OutlineExtractor._nextPointc                 C   s   | j | | d| _d S rh  )rn  moveTorz  rr  r   rx  r   r   r   rMoveTos  s   
zT2OutlineExtractor.rMoveToc                 C   s&   | j s| d | j| | d S Nrp  )rr  r}  rn  lineTorz  r|  r   r   r   rLineTow  s   
zT2OutlineExtractor.rLineToc                 C   s6   | j s| d | j}| j|||||| d S r~  )rr  r}  rz  rn  curveTo)r   pt1pt2pt3	nextPointr   r   r   rCurveTo|  s   
 zT2OutlineExtractor.rCurveToc                 C      | j r| j  d| _ d S r   )rr  rn  	closePathr   r   r   r   r       

zT2OutlineExtractor.closePathc                 C   s   | j r	|   d S d S r7   )rr  r  r   r   r   r   ru    s   zT2OutlineExtractor.endPathc                 C   s   |    | |   d S r7   ru  r}  rd  r  r   r   r   rf    s   zT2OutlineExtractor.op_rmovetoc                 C   s$   |    | | dd df d S rs  r  r  r   r   r   ri       zT2OutlineExtractor.op_hmovetoc                 C   s$   |    | d| dd f d S rD  r  r  r   r   r   rk    r  zT2OutlineExtractor.op_vmovetoc           
   	   C   sl   |    |  }|r4ddlm} |\}}}}|| }| j|d || }	| j|	dddd||f d S d S )Nr   StandardEncodingr   r   r   r   r   r   r   )ru  rd  $fontTools.encodings.StandardEncodingr  rn  addComponent)
r   r   r*  r  adxadybcharachar	baseGlyphaccentGlyphr   r   r   r    s   zT2OutlineExtractor.op_endcharc                 C   s<   |   }tdt|dD ]}|||d  }| | qd S )Nr   r)   )r   r   r^   r  )r   r   r*  rZ   rx  r   r   r   
op_rlineto  s
   zT2OutlineExtractor.op_rlinetoc                 C   rg  rh  alternatingLinetor  r   r   r   
op_hlineto  rj  zT2OutlineExtractor.op_hlinetoc                 C   rg  r   r  r  r   r   r   
op_vlineto  rj  zT2OutlineExtractor.op_vlinetoc           
      C   sX   |   }tdt|dD ]}|||d  \}}}}}}	| ||f||f||	f qdS )z${dxa dya dxb dyb dxc dyc}+ rrcurvetor   rq   N)r   r   r^   r  )
r   r   r*  rZ   dxadyadxbdybdxcdycr   r   r   op_rrcurveto  s   zT2OutlineExtractor.op_rrcurvetoc           
      C   sn   |   }tdt|d dD ]}|||d  \}}}}}}	| ||f||f||	f q| |dd  dS )z-{dxa dya dxb dyb dxc dyc}+ dxd dyd rcurveliner   r)   rq   N)r   r   r^   r  r  )
r   r   r*  rZ   r  r  r  r  dxddydr   r   r   op_rcurveline  s
   z T2OutlineExtractor.op_rcurvelinec                 C   sv   |   }|dd }tdt|dD ]}| |||d   q|dd \}}}}}	}
| ||f||f|	|
f dS )z-{dxa dya}+ dxb dyb dxc dyc dxd dyd rlinecurveNir   r)   )r   r   r^   r  r  )r   r   r*  lineArgsrZ   r  r  r  r  r  r  r   r   r   op_rlinecurve  s   z T2OutlineExtractor.op_rlinecurvec           	      C   s~   |   }t|d r|d }|dd }nd}tdt|dD ]}|||d  \}}}}| ||f||fd|f d}qdS )z!dx1? {dya dxb dyb dyc}+ vvcurvetor)   r   r   Nr1   r   r^   r   r  )	r   r   r*  dx1rZ   r  r  r  r  r   r   r   op_vvcurveto     zT2OutlineExtractor.op_vvcurvetoc           	      C   s~   |   }t|d r|d }|dd }nd}tdt|dD ]}|||d  \}}}}| ||f||f|df d}qdS )z!dy1? {dxa dxb dyb dxc}+ hhcurvetor)   r   r   Nr1   r  )	r   r   r*  dy1rZ   r  r  r  r  r   r   r   op_hhcurveto  r  zT2OutlineExtractor.op_hhcurvetoc                 C   0   |   }|r| |}|r| |}|sdS dS )zdy1 dx2 dy2 dx3 {dxa dxb dyb dyc dyd dxe dye dxf}* dyf? vhcurveto (30)
        {dya dxb dyb dxc dxd dxe dye dyf}+ dxf? vhcurveto
        N)r   vcurvetohcurvetor   r   r*  r   r   r   op_vhcurveto     

zT2OutlineExtractor.op_vhcurvetoc                 C   r  )zpdx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf?
        {dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?
        N)r   r  r  r  r   r   r   op_hvcurveto  r  zT2OutlineExtractor.op_hvcurvetoc                 C   sd   |   \}}}}}}}d }	 }
 }}| }| ||	f||f||
f | ||f||f||f d S r   r   r  )r   r   r  dx2dy2dx3dx4dx5dx6r  dy3dy4dy6dy5r   r   r   op_hflex  s
   zT2OutlineExtractor.op_hflexc                 C   sZ   |   \}}}}}}}}	}
}}}}| ||f||f||f | ||	f|
|f||f d S r7   r  )r   r   r  r  r  r  r  r  r  r  r  r  r  r  fdr   r   r   op_flex%  s   "zT2OutlineExtractor.op_flexc              	   C   sp   |   \	}}}}}}}}	}
d }}|| | | |	  }| ||f||f||f | ||f||	f|
|f d S r   r  )r   r   r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   	op_hflex1*  s
   zT2OutlineExtractor.op_hflex1c                 C   s   |   \}}}}}}}}	}
}}|| | | |
 }|| | |	 | }t|t|kr1|}| }n| }|}| ||f||f||f | ||	f|
|f||f d S r7   )r   r   r  )r   r   r  r  r  r  r  r  r  r  r  r  d6dxdyr  r  r   r   r   op_flex12  s   zT2OutlineExtractor.op_flex1c                 C   r,  r7   r-  r  r   r   r   r.  @  r  zT2OutlineExtractor.op_andc                 C   r,  r7   r-  r  r   r   r   r/  C  r  zT2OutlineExtractor.op_orc                 C   r,  r7   r-  r  r   r   r   r0  F  r  zT2OutlineExtractor.op_notc                 C   r,  r7   r-  r  r   r   r   r1  I  r  zT2OutlineExtractor.op_storec                 C   r,  r7   r-  r  r   r   r   r2  L  r  zT2OutlineExtractor.op_absc                 C   r,  r7   r-  r  r   r   r   r3  O  r  zT2OutlineExtractor.op_addc                 C   r,  r7   r-  r  r   r   r   r4  R  r  zT2OutlineExtractor.op_subc                 C   sD   |   }|   }|| }|| }||kr| | d S | | d S r7   )r  r  )r   r   num2num1d1d2r   r   r   r5  U  s   zT2OutlineExtractor.op_divc                 C   r,  r7   r-  r  r   r   r   r6  _  r  zT2OutlineExtractor.op_loadc                 C   r,  r7   r-  r  r   r   r   r7  b  r  zT2OutlineExtractor.op_negc                 C   r,  r7   r-  r  r   r   r   r8  e  r  zT2OutlineExtractor.op_eqc                 C   r,  r7   r-  r  r   r   r   r9  h  r  zT2OutlineExtractor.op_dropc                 C   r,  r7   r-  r  r   r   r   r:  k  r  zT2OutlineExtractor.op_putc                 C   r,  r7   r-  r  r   r   r   r;  n  r  zT2OutlineExtractor.op_getc                 C   r,  r7   r-  r  r   r   r   r<  q  r  zT2OutlineExtractor.op_ifelsec                 C   r,  r7   r-  r  r   r   r   r=  t  r  zT2OutlineExtractor.op_randomc                 C   r,  r7   r-  r  r   r   r   r>  w  r  zT2OutlineExtractor.op_mulc                 C   r,  r7   r-  r  r   r   r   r?  z  r  zT2OutlineExtractor.op_sqrtc                 C   r,  r7   r-  r  r   r   r   r@  }  r  zT2OutlineExtractor.op_dupc                 C   r,  r7   r-  r  r   r   r   rA    r  zT2OutlineExtractor.op_exchc                 C   r,  r7   r-  r  r   r   r   rB    r  zT2OutlineExtractor.op_indexc                 C   r,  r7   r-  r  r   r   r   rC    r  zT2OutlineExtractor.op_rollc                 C   s<   |   }|D ]}|r|df}nd|f}| | | }qd S r   )r   r  )r   isHorizontalr*  argrx  r   r   r   r    s   

z$T2OutlineExtractor.alternatingLinetoc                 C   s\   |d d \}}}}|dd  }t |dkr|d }g }nd}| d|f||f||f |S Nr1   r   r   r^   r  )r   r*  r  r  r  r  r  r   r   r   r       zT2OutlineExtractor.vcurvetoc                 C   s\   |d d \}}}}|dd  }t |dkr|d }g }nd}| |df||f||f |S r  r  )r   r*  r  r  r  r  r  r   r   r   r    r  zT2OutlineExtractor.hcurvetorQ  )8r   r   r   r   r   r  rz  r}  r  r  r  ru  rf  ri  rk  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.  r/  r0  r1  r2  r3  r4  r5  r6  r7  r8  r9  r:  r;  r<  r=  r>  r?  r@  rA  rB  rC  r  r  r  __classcell__r   r   rv  r   rm  I  sn    
	


rm  c                   @   s   e Zd Zdd Zdd Zdd Zd-dd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,S ).T1OutlineExtractorc                 C   s   || _ || _|   d S r7   )rn  subrsr   )r   rn  r  r   r   r   r     s   zT1OutlineExtractor.__init__c                 C   s    d| _ d| _d| _t|  d S r   )flexingrb  sbxrm  r   r   r   r   r   r     s   zT1OutlineExtractor.resetc                 C   r  r   )rr  rn  ru  r   r   r   r   ru    r  zT1OutlineExtractor.endPathr   c                 C      |   S r7   r   )r   rc  r   r   r   rd       zT1OutlineExtractor.popallWidthc                 C   s$   | j }|d |d |d< |d< d S )Nr  r   r  r  r   r   r   r     s   zT1OutlineExtractor.exchc                 C   s$   | j rd S |   | |   d S r7   )r  ru  r}  r   r  r   r   r   rf    s   zT1OutlineExtractor.op_rmovetoc                 C   s6   | j r
| d d S |   | |  d df d S r   )r  r  ru  r}  r   r  r   r   r   ri    s
   
zT1OutlineExtractor.op_hmovetoc                 C   s>   | j r| d |   d S |   | d|  d f d S r   )r  r  r   ru  r}  r   r  r   r   r   rk    s   
zT1OutlineExtractor.op_vmovetoc                 C   r  r7   )r  r  r   r   r   op_closepath  r   zT1OutlineExtractor.op_closepathc                 C   s   |   }|\}}||f| _d S r7   )r   rq  )r   r   r*  r   ry  r   r   r   op_setcurrentpoint  s   z%T1OutlineExtractor.op_setcurrentpointc                 C   r  r7   )ru  r  r   r   r   r    r   zT1OutlineExtractor.op_endcharc                 C   s,   |   \}}|| _|| _|| jd f| _d S rh  )r   rb  r  rq  )r   r   r  wxr   r   r   op_hsbw  s   zT1OutlineExtractor.op_hsbwc                 C   r  r7   r  r  r   r   r   op_sbw  r   zT1OutlineExtractor.op_sbwc                 C   s    |   }| j| }| | d S r7   )r  r  r  r  r   r   r   r    s   
zT1OutlineExtractor.op_callsubrc                 C   sT   |   }|   }|dkr|dkr|   d| _d S |dkr&|dkr(d| _d S d S d S )Nr   ri   r   )r  doFlexr  )r   r   r  nArgsr   r   r   op_callothersubr  s   

z#T1OutlineExtractor.op_callothersubrc                 C   r   r7   r   r  r   r   r   op_pop  r  zT1OutlineExtractor.op_popc                 C   s4  |   }|   }|    |   }|   }|   }|   }|   }|   }|   }	|   }
|   }|   }|   }|   }|   }|   }| ||  | ||  | | | | | |
 | |	 | d  | | | | | | | | | | | | | d  | | | | d S r7   )r  r  r  )r   finalyfinalxp3yp3xbcp4ybcp4xbcp3ybcp3xp2yp2xbcp2ybcp2xbcp1ybcp1xrpyrpxr   r   r   r    sB   












zT1OutlineExtractor.doFlexc                 C   r  r7   r  r  r   r   r   op_dotsection1  r   z T1OutlineExtractor.op_dotsectionc                 C   r  r7   r  r  r   r   r   	op_hstem34  r   zT1OutlineExtractor.op_hstem3c           
   	   C   sh   ddl m} |  \}}}}}|| }| j|d || }	|| j | }| j|	dddd||f dS )zasb adx ady bchar achar seacr   r  r  r   N)r  r  r   rn  r  r  )
r   r   r  asbr  r  r  r  r  r  r   r   r   op_seac7  s   zT1OutlineExtractor.op_seacc                 C   r  r7   r  r  r   r   r   	op_vstem3B  r   zT1OutlineExtractor.op_vstem3Nrl  )r   r   r   r   r   ru  rd  r   rf  ri  rk  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s,    
	*r  c                   @   s   e Zd ZeZee\ZZe	Z
eZd%ddZd&ddZdd Zdd	 Zd
d Zdd Zd&ddZdd Zd'ddZdd Zdd Zdd ZeeefddZdd Zdd  Zd&d!d"Zd#d$ Z dS )(T2CharStringNc                 C   s:   |d u rg }|| _ || _|| _|d ur|ng | _d | _d S r7   )bytecoder   r   r   _cur_vsindex)r   r  r   r   r   r   r   r   r   L  s   
zT2CharString.__init__c                 C   sN   | j }|d us	J |d ur|| _n| jd u r!t|dr|jnd| _|| jS )Nr   r   )r   r  hasattrr   rE  )r   r   pdr   r   r   rE  U  s   
zT2CharString.getNumRegionsc                 C   s2   | j d u rd| jjt| f S d| jjt| f S )Nz<%s (source) at %x>z<%s (bytecode) at %x>)r  rw  r   idr   r   r   r   __repr__^  s   
zT2CharString.__repr__c                 C      t S r7   )r   r   r   r   r   r   d  r  zT2CharString.getIntEncoderc                 C   r  r7   )r   r   r   r   r   getFixedEncoderg  r  zT2CharString.getFixedEncoderc                 C   s:   |   sd S t| jdg }| || j| j}||  d S NSubrs)r   r   r   decompilerClassr   r  )r   r  
decompilerr   r   r   	decompilej  s
   zT2CharString.decompilec              	   C   sF   t | jdg }| ||| j| jj| jj| j|}||  |j| _d S r  )r   r   outlineExtractorr   r_  r`  r  rb  )r   rn  r   r  	extractorr   r   r   drawq  s   
	zT2CharString.drawc                 C   s   t |}| | |jS r7   )r
   r  bounds)r   glyphSet	boundsPenr   r   r   
calcBounds  s   
zT2CharString.calcBoundsFc           
      C   sr  | j d urd S | j}| j}|r|r|d dv r|d d }n|r+t|d ts+tdg }|  }|  }d}t|}||k r|| }	|d }t|	trxz|	dd ||	 D  W n t
yg   td|	 w |	d	v rw|||  |d }n"t|	tr|||	 nt|	tr|||	 nJ d
t|	 ||k s?zt|}W n ty   t|  w | | d S )Nr   )r~   r   z@T2CharString or Subr has items on the stack after last operator.r   r   c                 s   s    | ]}t |V  qd S r7   )r   )rY   rA   r   r   r   	<genexpr>  s    z'T2CharString.compile.<locals>.<genexpr>zillegal operator: %sr   r   zunsupported type: %s)r  opcodesr   r_   strr   r   r   r^   extendr   r   intr?   typer   	TypeErrorr   errorsetBytecode)
r   isCFF2r  r   r  r   r   rZ   endr  r   r   r   compile  sV   




zT2CharString.compilec                 C   s
   | j d uS r7   )r  r   r   r   r   r     s   
zT2CharString.needsDecompilationc                 C      || _ d | _d S r7   )r   r  )r   r   r   r   r   r        
zT2CharString.setProgramc                 C   r  r7   )r  r   )r   r  r   r   r   r    r  zT2CharString.setBytecodec           	      C   s   | j d ur)||| j krdS || j | }|d }| j| }|| || j |\}}n||| jkr2dS | j| }|d }||t}|||fS )N)Nr   r   r   )r  operandEncodingr   r  )	r   r   r^   r   r_   r   r  r  r  r   r   r   r     s   




zT2CharString.getTokenc                 C   sP   | j d ur|| }| j || }|}n	| j| }|d }t||ks$J ||fS rh  )r  r   r^   )r   r   nBytesnewIndexbytesr   r   r   r%    s   

zT2CharString.getBytesc                 C   s   |S r7   r   )r   r   r   r   r   r     r  zT2CharString.handle_operatorc                 C   s  ddl m} | jd ur|| j d S d}g }	 | |\}}}|d u r%nW|rf|dv rR| |\}}}g }	|D ]}
|	|t|
d q7t|	}d|||g }nd||g }|	| |
  g }nt|trrt|dd}nt|}|| q|rd|}|	| d S d S )	Nr   )
num2binaryTr  rw    r3   r4   )fontTools.misc.textToolsr   r  dumphexr   r   r   r	   joinwritenewliner_   r?   r   r  )r   	xmlWriterttFontr   r   r*  r  r  hintMaskbitsbyteliner   r   r   toXML  s<   




zT2CharString.toXMLc                 C   s4  ddl m}m} |dr| || d S t|}| }g }t|}d}||k r|| }	|d }zt|	}	W nQ t	y   zt
|	dd}	W n: t	y   ||	 |	dv r|| }
d}tdt|
d	D ]}|t||
||d	   }qe|| |d }Y nw ||	 Y nw ||	 ||k s*| | d S )
Nr   )
binary2numreadHexrawr   r3   r4   r      rw   )r"  r.  r/  r   r  r	   splitr^   r  
ValueErrorr   r   r   r   r   )r   nameattrscontentr.  r/  r   r  rZ   r  mask	maskBytesjr   r   r   fromXML  sD   





zT2CharString.fromXML)NNNNr7   )F)!r   r   r   t2OperandEncodingr  re   t2Operatorsr   r  r   r  rm  r  r   rE  r  r   r   r  r  r  r  r   r   r  r^   r   r_   r   r%  r   r-  r:  r   r   r   r   r  F  s,    

		

,
&r  c                       sN   e Zd ZeZee\ZZd fdd	Z	dd Z
dd Zdd	 Zd
d Z  ZS )T1CharStringNc                    s   t  || || _d S r7   )rt  r   r  )r   r  r   r  rv  r   r   r   &  s   
zT1CharString.__init__c                 C   r  r7   )encodeIntT1r   r   r   r   r   *  r  zT1CharString.getIntEncoderc                 C   s   dd }d S )Nc                 S   s   t d)Nz8Type 1 charstrings don't support floating point operands)r  )r   r   r   r   r   .  r  z1T1CharString.getFixedEncoder.<locals>.encodeFixedr   )r   r   r   r   r   r   -  r   zT1CharString.getFixedEncoderc                 C   sL   | j d u rd S g }d}	 | |\}}}|d u rn|| q| | d S r   )r  r   r   r   )r   r   r   r  r  r   r   r   r  1  s   

zT1CharString.decompilec                 C   s"   t || j}||  |j| _d S r7   )r  r  r  rb  )r   rn  r  r   r   r   r  =  s   
zT1CharString.draw)NNN)r   r   r   t1OperandEncodingr  re   t1Operatorsr   r  r   r   r   r  r  r  r   r   rv  r   r=  "  s    r=  c                   @   sr   e Zd ZeZdddZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )DictDecompilerNc                 C   s   g | _ || _i | _|| _d S r7   )r  stringsdictparent)r   rB  rD  r   r   r   r   F  s   
zDictDecompiler.__init__c                 C   s   t | jdksJ d| jS )Nr   znon-empty stack)r^   r  rC  r   r   r   r   getDictL  s   zDictDecompiler.getDictc                 C   sl   d}t |}| jj}||k r4t|| }|d }| j| }|| |||\}}|d ur.|| ||k sd S d S rD  )r^   r  r   r   r  )r   r   r   lenDatar  r   r  r   r   r   r   r  P  s   
zDictDecompiler.decompilec                 C   r	  r
  r  r  r   r   r   r  \  r  zDictDecompiler.popc                 C   s   | j d d  }| j d d = |S r7   rG  r)  r   r   r   r   a  s   zDictDecompiler.popallc                 C   s   |\}}t |tr+d}tt|d ddD ]}|| }t| d| }||f| }qnt| d| }||}|dkrB| j| d S || j|< d S )Nr   r   r   arg_r   )r_   r`   r   r^   r   r  r  rC  )r   r   argTyper   rZ   r  
arghandlerr   r   r   r   f  s   
zDictDecompiler.handle_operatorc                 C   s,   t | jd tr| | j}|S |  }|S r   )r_   r  listarg_blend_numberr  )r   r4  outr   r   r   
arg_numberv  s
   zDictDecompiler.arg_numberc                 C   s4   g }|   }t|}|| |d |  }|S )Nr   )r  r^   r   r   )r   r4  rM  	blendArgs
numMastersdummyr   r   r   rL  }  s   

zDictDecompiler.arg_blend_numberc                 C   s   | j |   S r7   )rB  r  r   r4  r   r   r   arg_SID  rj  zDictDecompiler.arg_SIDc                 C   r  r7   r  rR  r   r   r   	arg_array  r  zDictDecompiler.arg_arrayc                 C   s   | j dd}| j|d }|  }|  }t|}||| ks#J dg| }|d }d}	d}
|	|k rY||	 |
 }|}
||	|  }|g||||   }|||	< |	d7 }	|	|k s4|S )a  
        There may be non-blend args at the top of the stack. We first calculate
        where the blend args start in the stack. These are the last
        numMasters*numBlends) +1 args.
        The blend args starts with numMasters relative coordinate values, the  BlueValues in the list from the default master font. This is followed by
        numBlends list of values. Each of  value in one of these lists is the
        Variable Font delta for the matching region.

        We re-arrange this to be a list of numMaster entries. Each entry starts with the corresponding default font relative value, and is followed by
        the delta values. We then convert the default values, the first item in each entry, to an absolute value.
        r   r   r   N)rC  r   rD  rE  r  r   r^   )r   r4  r   rP  rF  r*  numArgsr   	numDeltasrZ   prevValnewValmasterOffset	blendListr   r   r   arg_blendList  s(   
zDictDecompiler.arg_blendListc                 C   sJ   |   }g }|rt|d tr|}|S d}|D ]}|| }|| q|S r   )r   r_   rK  r   )r   r4  	valueListrM  currentr[   r   r   r   	arg_delta  s   zDictDecompiler.arg_deltar7   )r   r   r   cffDictOperandEncodingr  r   rE  r  r  r   r   rN  rL  rS  rT  r[  r^  r   r   r   r   rA  C  s    
	"rA  c                 C   s0   t | }|dk rd}|S |dk rd}|S d}|S )Ni  r   il  r   i   )r^   )r  nSubrsbiasr   r   r   r     s   r   )9__doc__fontTools.misc.fixedToolsr   r   r   r   r"  r   r   r   r	   fontTools.pens.boundsPenr
   r+   logging	getLoggerr   r   r   r   r$   r&   r.   r2   r6   r:   rD   r?  r^   r;  r_  r>   	enumerater   
maxOpStackre   r<  r   encodeIntCFFr>  r   r   r   r   r   	Exceptionr   objectr   r@  r^  rm  r  r  r=  rA  r   r   r   r   r   <module>   sr    

;4	  J3  f  ]!y