B
    Sc?              4   @   s  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stedd Zeed< eed< edd Zeed < eed!< eed< d"d#d$d%d&d'd(d)d*d+d,d-d.dd/gZ d0d1 e!e D Z"d2Z#d3d4 Z$d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhg4Z%didj Z&e&dkZ'e&dlZ(e&dmZ)ej*fdndoZ+ed!edp Z,dqdr Z-G dsdt dte.Z/G dudv dve0Z1d5d6d7d8d9d:d;dwd<d=dxd>dDdEdLdMdydzd{d|d}dVd~ddgZ2G dd de1Z3G dd de3Z4G dd de4Z5G dd de0Z6G dd de6Z7G dd de0Z8dd Z9dS )zwpsCharStrings.py -- module implementing various kinds of CharStrings:
CFF dictionary data and Type1/Type2 CharStrings.
    )fixedToFloatfloatToFixedfloatToFixedToStrstrToFixedToFloat)bytechrbyteord	bytesjoinstrjoin)	BoundsPenNc             C   s`   |dkr"|t || f}|d }n|}y| j| }W n tk
rL   d |fS X | |}||fS )N      )r   	operatorsKeyErrorhandle_operator)selfb0dataindexopoperatorvalue r   ;lib/python3.7/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   s    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   r   r   r   r   read_smallInt2%   s    r"   c             C   s&   t d|||d  \}||d fS )Nz>h   )structunpack)r   r   r   r   r   r   r   r   read_shortInt)   s    r&   c             C   s&   t d|||d  \}||d fS )Nz>l   )r$   r%   )r   r   r   r   r   r   r   r   read_longInt-   s    r(   c             C   s.   t d|||d  \}t|dd|d fS )Nz>lr'      )precisionBits)r$   r%   r   )r   r   r   r   r   r   r   r   read_fixed16161   s    r+   c             C   s   t stt|fS )N)NotImplementedErrorAssertionErrorNotImplemented)r   r   r   r   r   r   r   read_reserved5   s    r/   c             C   sj   d}xXt || }|d }|d@ d? }|d@ }|dkr8P |t|  }|dkrNP |t|  }qW t||fS )N r      r'      )r   realNibblesfloat)r   r   r   r   ZnumberbZnibble0Znibble1r   r   r   read_realNumber9   s    r6   r          r   r'   r!               0123456789.EzE--c             C   s   i | ]\}}||qS r   r   ).0ivr   r   r   
<dictcomp>]   s    rM      c             C   s   i }i }xp| D ]h}t |dkr0|d ||d < n|dd  ||d < t|d trd|d ||d < q|d f||d < qW ||fS )Nr#   r   r   )len
isinstancetuple)ZoperatorListZoperZopcitemr   r   r   buildOperatorDictb   s    
rS   )r   Zhstem)   Zvstem)r'   Zvmoveto)   Zrlineto)   Zhlineto)   Zvlineto)   Z	rrcurveto)
   Zcallsubr)   return)   endchar)r2   vsindex)r)   blend)   Zhstemhm)   hintmask)   cntrmask)   Zrmoveto)   Zhmoveto)   Zvstemhm)   Z
rcurveline)   Z
rlinecurve)   Z	vvcurveto)   Z	hhcurveto)r;   Z	callgsubr)r<   Z	vhcurveto)   Z	hvcurveto))r   r   ignore))r   rT   and))r   r'   or))r   rU   not))r   rX   Zstore))r   	   abs))r   rY   add))r   rZ   sub))r   r   Zdiv))r      load))r   r\   neg))r   r2   eq))r   r`   Zdrop))r   rc   Zput))r   re   get))r   rf   Zifelse))r   rg   Zrandom))r   rh   mul))r   rj   Zsqrt))r   rk   dup))r   r:   exch))r   r;   r   ))r   r<   Zroll))r   "   Zhflex))r   #   Zflex))r   $   Zhflex1))r   %   Zflex1c             C   sN   | dkrt d}n"| dkr$t d}n| dks0td }|t tjtjfdd}|S )Ncffr;   t1r9   t2c             S   s  d|   krdkr&n n|| d }nd|   kr:dkrdn n&| d } || d? d || d@  }nd	|   krxd
krn n(|  d } || d? d || d@  }n`|d krd|   krdkrn n|d|d|  }nt d |d|d|  }n||d|  }|S )Nik   r   r   ik  rX   r   r9   iir!   i i  r:   z>hzk4-byte T2 number got passed to the IntType handler. This should happen only when reading in old XML files.
z>l)logZwarning)r   
fourByteOpr   packr%   coder   r   r   	encodeInt   s    


z getIntEncoder.<locals>.encodeInt)r   r-   r$   r   r%   )formatr   r   r   r   r   getIntEncoder   s    

r   r   r   r   c             C   s6   t | dd}|d@ dkr$t|d? S d|d| S dS )zFor T2 onlyr)   )r*   i  r      z>lN)r   encodeIntT2)fr   r   r   r   r   encodeFixed   s    r   r2   c             C   s*  | dkrt S d|  }|d d dkr2|dd  }n |d d dkrRd|dd   }g }xl|r|d	 }|dd  }|d
kr|d d }|dkr|dd  }d}n|dkr|dd  }|t|  qXW |d t|d r|d td}x8td	t|dD ]$}|t|| d> ||d  B  }qW |S )Ng        z%.8Gr#   z0.r   rT   z-0.rI   r   rH   zE-+r2   r<   r'   )realZeroBytesappendrealNibblesDictrO   r   range)r   sZnibblescZc2drK   r   r   r   encodeFloat   s4    

$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 )N)
localSubrscalcSubrBias	localBiasglobalSubrs
globalBiasprivateblenderreset)r   r   r   r   r   r   r   r   __init__
  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     s    zSimpleT2Decompiler.resetc             C   s   | j | | }|r$g }|j}ndd }| jj}d}xv||\}}}|d krTP || |rd| }	t| |	d }
|
d k	r|
|}|r|\}}|| q|   q:|| q:W |r|| | j d= d S )Nc             S   s   d S )Nr   )xr   r   r   <lambda>"      z,SimpleT2Decompiler.execute.<locals>.<lambda>r   Zop_)r   r   needsDecompilationr   getTokengetattrpopall
setProgram)r   
charStringr   programZpushToProgramZpushToStackr   token
isOperatorZhandlerNamehandlerrvr   r   r   r   execute  s4    


zSimpleT2Decompiler.executec             C   s   | j d }| j d= |S )Nr   )r   )r   r   r   r   r   pop:  s    
zSimpleT2Decompiler.popc             C   s    | j d d  }g | j d d < |S )N)r   )r   stackr   r   r   r   ?  s    zSimpleT2Decompiler.popallc             C   s   | j | d S )N)r   r   )r   r   r   r   r   pushD  s    zSimpleT2Decompiler.pushc             C   s
   | j rd S )N)r   )r   r   r   r   r   	op_returnG  s    zSimpleT2Decompiler.op_returnc             C   s   d S )Nr   )r   r   r   r   r   
op_endcharK  s    zSimpleT2Decompiler.op_endcharc             C   s   d S )Nr   )r   r   r   r   r   	op_ignoreN  s    zSimpleT2Decompiler.op_ignorec             C   s&   |   }| j|| j  }| | d S )N)r   r   r   r   )r   r   	subrIndexsubrr   r   r   op_callsubrQ  s    zSimpleT2Decompiler.op_callsubrc             C   s&   |   }| j|| j  }| | d S )N)r   r   r   r   )r   r   r   r   r   r   r   op_callgsubrV  s    zSimpleT2Decompiler.op_callgsubrc             C   s   |    d S )N)
countHints)r   r   r   r   r   op_hstem[  s    zSimpleT2Decompiler.op_hstemc             C   s   |    d S )N)r   )r   r   r   r   r   op_vstem]  s    zSimpleT2Decompiler.op_vstemc             C   s   |    d S )N)r   )r   r   r   r   r   
op_hstemhm_  s    zSimpleT2Decompiler.op_hstemhmc             C   s   |    d S )N)r   )r   r   r   r   r   
op_vstemhma  s    zSimpleT2Decompiler.op_vstemhmc             C   s>   | j s|   | jd d | _ | jd || j \}}||fS )NrW   rX   r   )r   r   r   r   getBytes)r   r   r   r   r   r   op_hintmaskd  s
    zSimpleT2Decompiler.op_hintmaskc             C   s    |   }| jt|d  | _d S )Nr#   )r   r   rO   )r   argsr   r   r   r   m  s    zSimpleT2Decompiler.countHintsc             C   s   t d S )N)r,   )r   r   r   r   r   op_andr  s    zSimpleT2Decompiler.op_andc             C   s   t d S )N)r,   )r   r   r   r   r   op_ort  s    zSimpleT2Decompiler.op_orc             C   s   t d S )N)r,   )r   r   r   r   r   op_notv  s    zSimpleT2Decompiler.op_notc             C   s   t d S )N)r,   )r   r   r   r   r   op_storex  s    zSimpleT2Decompiler.op_storec             C   s   t d S )N)r,   )r   r   r   r   r   op_absz  s    zSimpleT2Decompiler.op_absc             C   s   t d S )N)r,   )r   r   r   r   r   op_add|  s    zSimpleT2Decompiler.op_addc             C   s   t d S )N)r,   )r   r   r   r   r   op_sub~  s    zSimpleT2Decompiler.op_subc             C   s   t d S )N)r,   )r   r   r   r   r   op_div  s    zSimpleT2Decompiler.op_divc             C   s   t d S )N)r,   )r   r   r   r   r   op_load  s    zSimpleT2Decompiler.op_loadc             C   s   t d S )N)r,   )r   r   r   r   r   op_neg  s    zSimpleT2Decompiler.op_negc             C   s   t d S )N)r,   )r   r   r   r   r   op_eq  s    zSimpleT2Decompiler.op_eqc             C   s   t d S )N)r,   )r   r   r   r   r   op_drop  s    zSimpleT2Decompiler.op_dropc             C   s   t d S )N)r,   )r   r   r   r   r   op_put  s    zSimpleT2Decompiler.op_putc             C   s   t d S )N)r,   )r   r   r   r   r   op_get  s    zSimpleT2Decompiler.op_getc             C   s   t d S )N)r,   )r   r   r   r   r   	op_ifelse  s    zSimpleT2Decompiler.op_ifelsec             C   s   t d S )N)r,   )r   r   r   r   r   	op_random  s    zSimpleT2Decompiler.op_randomc             C   s   t d S )N)r,   )r   r   r   r   r   op_mul  s    zSimpleT2Decompiler.op_mulc             C   s   t d S )N)r,   )r   r   r   r   r   op_sqrt  s    zSimpleT2Decompiler.op_sqrtc             C   s   t d S )N)r,   )r   r   r   r   r   op_dup  s    zSimpleT2Decompiler.op_dupc             C   s   t d S )N)r,   )r   r   r   r   r   op_exch  s    zSimpleT2Decompiler.op_exchc             C   s   t d S )N)r,   )r   r   r   r   r   op_index  s    zSimpleT2Decompiler.op_indexc             C   s   t d S )N)r,   )r   r   r   r   r   op_roll  s    zSimpleT2Decompiler.op_rollc       
      C   s   | j dkr| j | _ |  }|| j d  }| jd krJ| j||  d = nzt| j| }||  }}xP||k r|| j  }| j|| }| | j|}	| j|  |	7  < |}|d7 }qfW g | j|d < d S )Nr   r   )r   r   getNumRegionsr   r   r   rO   r   )
r   r   	numBlendsZnumOpsZargiZend_argsZtupleiZnext_tiZdeltasZdeltar   r   r   op_blend  s     



zSimpleT2Decompiler.op_blendc             C   s    |   }|| _| j|| _d S )N)r   r   r   r   r   )r   r   Z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   Zop_cntrmaskr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sT   
	r   )rq   Z	closepath)ru   Zhsbw))r   r   Z
dotsection))r   r   Zvstem3))r   r#   Zhstem3))r   rV   Zseac))r   rW   Zsbw))r   r)   Zcallothersubr))r      r   ))r   !   Zsetcurrentpointc               @   sP   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 )N)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 )Nr   )r   r   gotWidthwidth)r   r   r   r   r     s    
zT2WidthExtractor.resetr   c             C   s`   |   }| js\|t|d A rN| jd k	s0td| j|d  | _|dd  }n| j| _d| _|S )Nr#   z5CFF2 CharStrings must not have an initial width valuer   r   )r   r   rO   r   r-   r   r   )r   evenOddr   r   r   r   popallWidth  s    zT2WidthExtractor.popallWidthc             C   s    |   }| jt|d  | _d S )Nr#   )r   r   rO   )r   r   r   r   r   r     s    zT2WidthExtractor.countHintsc             C   s   |    d S )N)r   )r   r   r   r   r   
op_rmoveto  s    zT2WidthExtractor.op_rmovetoc             C   s   |  d d S )Nr   )r   )r   r   r   r   r   
op_hmoveto  s    zT2WidthExtractor.op_hmovetoc             C   s   |  d d S )Nr   )r   )r   r   r   r   r   
op_vmoveto  s    zT2WidthExtractor.op_vmovetoc             C   s   |    d S )N)r   )r   r   r   r   r   r     s    zT2WidthExtractor.op_endchar)NN)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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 )Nr   )r   r   pen	subrLevel)r   r   r   r   r   r   r   r   r   r   r   r     s    zT2OutlineExtractor.__init__c             C   s    t |  d| _d| _d| _d S )N)r   r   r   )r   r   currentPoint	sawMoveTor   )r   r   r   r   r     s    
zT2OutlineExtractor.resetc                s>   |  j d7  _ t | |  j d8  _ | j dkr:|   d S )Nr   r   )r   superr   endPath)r   r   )	__class__r   r   r     s
    
zT2OutlineExtractor.executec             C   s,   | j \}}||d  ||d  f}|| _ |S )Nr   r   )r   )r   pointr   yr   r   r   
_nextPoint  s    
zT2OutlineExtractor._nextPointc             C   s   | j | | d| _d S )Nr   )r   ZmoveTor   r   )r   r   r   r   r   rMoveTo  s    zT2OutlineExtractor.rMoveToc             C   s&   | j s| d | j| | d S )N)r   r   )r   r   r   ZlineTor   )r   r   r   r   r   rLineTo  s    
zT2OutlineExtractor.rLineToc             C   s6   | j s| d | j}| j|||||| d S )N)r   r   )r   r   r   r   ZcurveTo)r   Zpt1Zpt2Zpt3Z	nextPointr   r   r   rCurveTo$  s    
zT2OutlineExtractor.rCurveToc             C   s   | j r| j  d| _ d S )Nr   )r   r   	closePath)r   r   r   r   r  *  s    
zT2OutlineExtractor.closePathc             C   s   | j r|   d S )N)r   r  )r   r   r   r   r   /  s    zT2OutlineExtractor.endPathc             C   s   |    | |   d S )N)r   r   r   )r   r   r   r   r   r   J  s    zT2OutlineExtractor.op_rmovetoc             C   s$   |    | | dd df d S )Nr   r   )r   r   r   )r   r   r   r   r   r   M  s    zT2OutlineExtractor.op_hmovetoc             C   s$   |    | d| dd f d S )Nr   r   )r   r   r   )r   r   r   r   r   r   P  s    zT2OutlineExtractor.op_vmovetoc       
   	   C   sh   |    |  }|rdddlm} |\}}}}|| }| j|d || }	| j|	dddd||f d S )Nr   )StandardEncoding)r   r   r   r   r   r   r   )r   r   $fontTools.encodings.StandardEncodingr  r   addComponent)
r   r   r   r  adxadybcharachar	baseGlyphaccentGlyphr   r   r   r   S  s    zT2OutlineExtractor.op_endcharc             C   s@   |   }x2tdt|dD ]}|||d  }| | qW d S )Nr   r#   )r   r   rO   r  )r   r   r   rK   r   r   r   r   
op_rlinetoc  s    zT2OutlineExtractor.op_rlinetoc             C   s   |  d d S )Nr   )alternatingLineto)r   r   r   r   r   
op_hlinetoi  s    zT2OutlineExtractor.op_hlinetoc             C   s   |  d d S )Nr   )r  )r   r   r   r   r   
op_vlinetok  s    zT2OutlineExtractor.op_vlinetoc       
      C   s\   |   }xNtdt|dD ]:}|||d  \}}}}}}	| ||f||f||	f qW dS )z${dxa dya dxb dyb dxc dyc}+ rrcurvetor   rV   N)r   r   rO   r  )
r   r   r   rK   dxadyadxbdybdxcdycr   r   r   op_rrcurvetoq  s    zT2OutlineExtractor.op_rrcurvetoc       
      C   sr   |   }xRtdt|d dD ]:}|||d  \}}}}}}	| ||f||f||	f qW | |dd  dS )z-{dxa dya dxb dyb dxc dyc}+ dxd dyd rcurveliner   r#   rV   N)r   r   rO   r  r  )
r   r   r   rK   r  r  r  r  dxddydr   r   r   op_rcurvelinex  s
    z T2OutlineExtractor.op_rcurvelinec             C   sz   |   }|dd }x.tdt|dD ]}| |||d   q&W |dd \}}}}}	}
| ||f||f|	|
f dS )z-{dxa dya}+ dxb dyb dxc dyc dxd dyd rlinecurveNir   r#   )r   r   rO   r  r  )r   r   r   ZlineArgsrK   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}xNtdt|dD ]:}|||d  \}}}}| ||f||fd|f d}q@W dS )z!dx1? {dya dxb dyb dyc}+ vvcurvetor#   r   r   Nr'   )r   rO   r   r  )	r   r   r   dx1rK   r  r  r  r  r   r   r   op_vvcurveto  s    zT2OutlineExtractor.op_vvcurvetoc       	      C   s   |   }t|d r*|d }|dd }nd}xNtdt|dD ]:}|||d  \}}}}| ||f||f|df d}q@W dS )z!dy1? {dxa dxb dyb dxc}+ hhcurvetor#   r   r   Nr'   )r   rO   r   r  )	r   r   r   dy1rK   r  r  r  r  r   r   r   op_hhcurveto  s    zT2OutlineExtractor.op_hhcurvetoc             C   s.   |   }x |r(| |}|r
| |}q
W dS )z}dy1 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  s
    
zT2OutlineExtractor.op_vhcurvetoc             C   s.   |   }x |r(| |}|r
| |}q
W dS )zddx1 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   r   r   op_hvcurveto  s
    
zT2OutlineExtractor.op_hvcurvetoc             C   sd   |   \}}}}}}}d }	 }
 }}| }| ||	f||f||
f | ||f||f||f d S )Nr   )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 )N)r   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 )Nr   )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rb|}| }n
| }|}| ||f||f||f | ||	f|
|f||f d S )N)r   rr   r  )r   r   r  r  r%  r&  r'  r+  r(  r,  r)  r.  Zd6ZdxZdyr*  r-  r   r   r   op_flex1  s    zT2OutlineExtractor.op_flex1c             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_andc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_orc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_notc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_storec             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_absc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_addc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_subc             C   sB   |   }|   }|| }|| }||kr4| | n
| | d S )N)r   r   )r   r   Znum2Znum1Zd1Zd2r   r   r   r     s    zT2OutlineExtractor.op_divc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_loadc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_negc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_eqc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_dropc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_putc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_getc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_ifelsec             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_randomc             C   s   t d S )N)r,   )r   r   r   r   r   r      s    zT2OutlineExtractor.op_mulc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_sqrtc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_dupc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_exchc             C   s   t d S )N)r,   )r   r   r   r   r   r     s    zT2OutlineExtractor.op_indexc             C   s   t d S )N)r,   )r   r   r   r   r   r   
  s    zT2OutlineExtractor.op_rollc             C   s@   |   }x2|D ]*}|r |df}nd|f}| | | }qW d S )Nr   )r   r  )r   ZisHorizontalr   argr   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 )Nr'   r   r   )rO   r  )r   r   r  r  r  r  r  r   r   r   r!    s    zT2OutlineExtractor.vcurvetoc             C   s\   |d d \}}}}|dd  }t |dkr:|d }g }nd}| |df||f||f |S )Nr'   r   r   )rO   r  )r   r   r  r  r  r  r  r   r   r   r"  %  s    zT2OutlineExtractor.hcurveto)NN)8r   r   r   r   r   r   r   r   r  r  r  r   r   r   r   r   r  r  r  r  r  r  r  r   r#  r$  r/  r1  r2  r3  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r!  r"  __classcell__r   r   )r   r   r      sh   
	
	
r   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 )N)r   subrsr   )r   r   r7  r   r   r   r   2  s    zT1OutlineExtractor.__init__c             C   s    d| _ d| _d| _t|  d S )Nr   )flexingr   sbxr   r   )r   r   r   r   r   7  s    zT1OutlineExtractor.resetc             C   s   | j r| j  d| _ d S )Nr   )r   r   r   )r   r   r   r   r   =  s    
zT1OutlineExtractor.endPathr   c             C   s   |   S )N)r   )r   r   r   r   r   r   B  s    zT1OutlineExtractor.popallWidthc             C   s$   | j }|d |d  |d< |d< d S )Nr  r   )r   )r   r   r   r   r   r|   E  s    zT1OutlineExtractor.exchc             C   s$   | j r
d S |   | |   d S )N)r8  r   r   r   )r   r   r   r   r   r   L  s    zT1OutlineExtractor.op_rmovetoc             C   s6   | j r| d d S |   | |  d df d S )Nr   )r8  r   r   r   r   )r   r   r   r   r   r   Q  s
    
zT1OutlineExtractor.op_hmovetoc             C   s>   | j r| d |   d S |   | d|  d f d S )Nr   )r8  r   r|   r   r   r   )r   r   r   r   r   r   X  s    
zT1OutlineExtractor.op_vmovetoc             C   s   |    d S )N)r  )r   r   r   r   r   op_closepath`  s    zT1OutlineExtractor.op_closepathc             C   s   |   }|\}}||f| _d S )N)r   r   )r   r   r   r   r   r   r   r   op_setcurrentpointb  s    z%T1OutlineExtractor.op_setcurrentpointc             C   s   |    d S )N)r   )r   r   r   r   r   r   g  s    zT1OutlineExtractor.op_endcharc             C   s,   |   \}}|| _|| _|| jd f| _d S )Nr   )r   r   r9  r   )r   r   r9  Zwxr   r   r   op_hsbwj  s    zT1OutlineExtractor.op_hsbwc             C   s   |    d S )N)r   )r   r   r   r   r   op_sbwo  s    zT1OutlineExtractor.op_sbwc             C   s    |   }| j| }| | d S )N)r   r7  r   )r   r   r   r   r   r   r   r   s  s    
zT1OutlineExtractor.op_callsubrc             C   sJ   |   }|   }|dkr0|dkr0|   d| _n|dkrF|dkrFd| _d S )Nr   rT   r   )r   doFlexr8  )r   r   r   ZnArgsr   r   r   op_callothersubrw  s    z#T1OutlineExtractor.op_callothersubrc             C   s   d S )Nr   )r   r   r   r   r   op_pop  s    zT1OutlineExtractor.op_popc             C   s4  |   }|   }|    |   }|   }|   }|   }|   }|   }|   }	|   }
|   }|   }|   }|   }|   }|   }| ||  | ||  | | | | | |
 | |	 | d  | | | | | | | | | | | | | d  | | | | d S )N)r   r   r  )r   ZfinalyZfinalxZp3yZp3xZbcp4yZbcp4xZbcp3yZbcp3xZp2yZp2xZbcp2yZbcp2xZbcp1yZbcp1xZrpyZrpxr   r   r   r>    sB    












zT1OutlineExtractor.doFlexc             C   s   |    d S )N)r   )r   r   r   r   r   op_dotsection  s    z T1OutlineExtractor.op_dotsectionc             C   s   |    d S )N)r   )r   r   r   r   r   	op_hstem3  s    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   r   r   r   r   r   N)r  r  r   r   r  r9  )
r   r   r  Zasbr  r  r	  r
  r  r  r   r   r   op_seac  s    zT1OutlineExtractor.op_seacc             C   s   |    d S )N)r   )r   r   r   r   r   	op_vstem3  s    zT1OutlineExtractor.op_vstem3N)r   )r   r   r   r   r   r   r   r|   r   r   r   r:  r;  r   r<  r=  r   r?  r@  r>  rA  rB  rC  rD  r   r   r   r   r6  0  s*   

*	r6  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 krg }|| _ || _|| _|d k	r*|ng | _d | _d S )N)bytecoder   r   r   _cur_vsindex)r   rF  r   r   r   r   r   r   r     s    zT2CharString.__init__c             C   sN   | j }|d k	st|d k	r"|| _n | jd krBt|dr<|jnd| _|| jS )Nr^   r   )r   r-   rG  hasattrr^   r   )r   r^   Zpdr   r   r   r     s    
zT2CharString.getNumRegionsc             C   s6   | j d krd| jjt| f S d| jjt| f S d S )Nz<%s (source) at %x>z<%s (bytecode) at %x>)rF  r   r   id)r   r   r   r   __repr__  s    
zT2CharString.__repr__c             C   s   t S )N)r   )r   r   r   r   r     s    zT2CharString.getIntEncoderc             C   s   t S )N)r   )r   r   r   r   getFixedEncoder  s    zT2CharString.getFixedEncoderc             C   s:   |   sd S t| jdg }| || j| j}||  d S )NSubrs)r   r   r   decompilerClassr   r   )r   r7  Z
decompilerr   r   r   	decompile  s
    zT2CharString.decompilec          	   C   sF   t | jdg }| ||| j| jj| jj| j|}||  |j| _d S )NrL  )r   r   outlineExtractorr   r   r   r   r   )r   r   r   r7  	extractorr   r   r   draw  s    

zT2CharString.drawc             C   s   t |}| | |jS )N)r
   rQ  Zbounds)r   ZglyphSetZ	boundsPenr   r   r   
calcBounds  s    
zT2CharString.calcBoundsFc       
      C   s  | j d k	rd S | j}| j}|r<|rV|d dkrV|d d }n|rVt|d tsVtdg }|  }|  }d}t|}x||k rH|| }	|d }t|	try|	dd ||	 D  W n  t
k
r   td|	 Y nX |	d	kr|||  |d }qxt|	tr|||	 qxt|	tr2|||	 qxdsxtd
t|	 qxW yt|}W n" tk
rx   t|  Y nX | | 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 )N)r   )rJ   r5   r   r   r   	<genexpr>  s    z'T2CharString.compile.<locals>.<genexpr>zillegal operator: %s)rb   rd   zunsupported type: %s)rF  opcodesr   rP   strr   r   rK  rO   extendr   r   intr4   r-   typer   	TypeErrorr   errorsetBytecode)
r   ZisCFF2rT  r   rF  r   r   rK   endr   r   r   r   compile  sJ    



zT2CharString.compilec             C   s
   | j d k	S )N)rF  )r   r   r   r   r   #  s    zT2CharString.needsDecompilationc             C   s   || _ d | _d S )N)r   rF  )r   r   r   r   r   r   &  s    zT2CharString.setProgramc             C   s   || _ d | _d S )N)rF  r   )r   rF  r   r   r   r[  *  s    zT2CharString.setBytecodec       	      C   s   | j d k	rR||| j krdS || j | }|d }| j| }|| || j |\}}n$||| jkrddS | j| }|d }||t}|||fS )N)Nr   r   r   )rF  operandEncodingr   rU  )	r   r   rO   r   rP   r   r   r   r   r   r   r   r   .  s    



zT2CharString.getTokenc             C   sP   | j d k	r&|| }| j || }|}n| j| }|d }t||ksHt||fS )Nr   )rF  r   rO   r-   )r   r   ZnBytesZnewIndexbytesr   r   r   r   ?  s    

zT2CharString.getBytesc             C   s   |S )Nr   )r   r   r   r   r   r   J  s    zT2CharString.handle_operatorc             C   s  ddl m} | jd k	r$|| j nd}g }x| |\}}}|d krHP |r|dkr| |\}}}g }	x |D ]}
|	|t|
d qnW t|	}d|||g }nd||g }|	| |
  g }q.t|trt|dd}nt|}|| q.W |rd|}|	| d S )Nr   )
num2binary)rb   rd   rX    r)   )r*   )fontTools.misc.textToolsr`  rF  Zdumphexr   r   r   r	   joinwritenewlinerP   r4   r   rU  )r   Z	xmlWriterZttFontr`  r   r   r   r   ZhintMaskbitsZbyteliner   r   r   toXMLM  s8    




zT2CharString.toXMLc             C   sB  ddl m}m} |dr,| || d S t|}| }g }t|}d}x||k r2|| }	|d }yt|	}	W n t	k
r$   yt
|	dd}	W n~ t	k
r   ||	 |	dkr|| }
d}x4tdt|
d	D ] }|t||
||d	   }qW || |d }Y nX ||	 Y qNX ||	 qNW | | d S )
Nr   )
binary2numreadHexrawr   r)   )r*   )rb   rd   r   rX   )rb  ri  rj  ry   r[  r	   splitrO   rW  
ValueErrorr   r   r   r   r   )r   nameZattrsZcontentri  rj  r   r\  rK   r   maskZ	maskBytesjr   r   r   fromXMLr  s:    


 
zT2CharString.fromXML)NNNN)N)N)F)N)!r   r   r   t2OperandEncodingr^  rS   t2Operatorsr   rT  r   rM  r   rO  r   r   rJ  r   rK  rN  rQ  rR  r]  r   r   r[  rO   r   rP   r   r   r   rh  rq  r   r   r   r   rE    s*   
	
	

,
%rE  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 )N)r   r   r7  )r   rF  r   r7  )r   r   r   r     s    zT1CharString.__init__c             C   s   t S )N)encodeIntT1)r   r   r   r   r     s    zT1CharString.getIntEncoderc             C   s   dd }d S )Nc             S   s   t dd S )Nz8Type 1 charstrings don't support floating point operands)rY  )r   r   r   r   r     s    z1T1CharString.getFixedEncoder.<locals>.encodeFixedr   )r   r   r   r   r   rK    s    zT1CharString.getFixedEncoderc             C   sN   | j d krd S g }d}x(| |\}}}|d kr2P || qW | | d S )Nr   )rF  r   r   r   )r   r   r   r   r   r   r   r   rN    s    
zT1CharString.decompilec             C   s"   t || j}||  |j| _d S )N)r6  r7  r   r   )r   r   rP  r   r   r   rQ    s    
zT1CharString.draw)NNN)r   r   r   t1OperandEncodingr^  rS   t1Operatorsr   rT  r   r   rK  rN  rQ  r5  r   r   )r   r   rt    s   rt  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 )N)r   stringsdictparent)r   ry  r{  r   r   r   r     s    zDictDecompiler.__init__c             C   s   t | jdkstd| jS )Nr   znon-empty stack)rO   r   r-   rz  )r   r   r   r   getDict  s    zDictDecompiler.getDictc             C   sf   d}t |}| jj}xL||k r`t|| }|d }| j| }|| |||\}}|d k	r|| qW d S )Nr   r   )rO   r   r   r   r^  )r   r   r   ZlenDatar   r   r   r   r   r   r   rN    s    

zDictDecompiler.decompilec             C   s   | j d }| j d= |S )Nr   )r   )r   r   r   r   r   r     s    
zDictDecompiler.popc             C   s   | j d d  }| j d d = |S )N)r   )r   r   r   r   r   r     s    zDictDecompiler.popallc             C   s   |\}}t |trZd}xXtt|d ddD ](}|| }t| d| }||f| }q,W nt| d| }||}|dkr| j| n
|| j|< d S )Nr   r   r   Zarg_r_   )rP   rQ   r   rO   r   r   rV  rz  )r   r   ZargTyper   rK   r4  Z
arghandlerr   r   r   r     s    
zDictDecompiler.handle_operatorc             C   s*   t | jd tr| | j}n|  }|S )Nr   )rP   r   listarg_blend_numberr   )r   rn  outr   r   r   
arg_number  s    zDictDecompiler.arg_numberc             C   s4   g }|   }t|}|| |d |  }|S )Nr_   )r   rO   r   r   )r   rn  r  Z	blendArgs
numMastersZdummyr   r   r   r~    s    

zDictDecompiler.arg_blend_numberc             C   s   | j |   S )N)ry  r   )r   rn  r   r   r   arg_SID  s    zDictDecompiler.arg_SIDc             C   s   |   S )N)r   )r   rn  r   r   r   	arg_array  s    zDictDecompiler.arg_arrayc             C   s   | j dd}| j|d }|  }|  }t|}||| ksFtdg| }|d }d}	d}
xN|	|k r||	 |
 }|}
||	|  }|g||||   }|||	< |	d7 }	qbW |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)rz  ry   r{  r   r   r   rO   r-   )r   rn  r^   r  r   r   ZnumArgsr   Z	numDeltasrK   ZprevValZnewValZmasterOffsetZ	blendListr   r   r   arg_blendList  s$    

zDictDecompiler.arg_blendListc             C   sL   |   }g }|r$t|d tr$|}n$d}x|D ]}|| }|| q.W |S )Nr   )r   rP   r}  r   )r   rn  Z	valueListr  ZcurrentrL   r   r   r   	arg_delta  s    
zDictDecompiler.arg_delta)N)r   r   r   cffDictOperandEncodingr^  r   r|  rN  r   r   r   r  r~  r  r  r  r  r   r   r   r   rx    s   
	 rx  c             C   s,   t | }|dk rd}n|dk r$d}nd}|S )Ni  r   il  ik  i   )rO   )r7  ZnSubrsZbiasr   r   r   r   )  s    r   ):__doc__ZfontTools.misc.fixedToolsr   r   r   r   rb  r   r   r   r	   ZfontTools.pens.boundsPenr
   r$   ZloggingZ	getLoggerr   r   r   r   r    r"   r&   r(   r+   r/   r6   rv  rO   r-   rr  r  r3   	enumerater   Z
maxOpStackrS   rs  r   ZencodeIntCFFru  r   r   r   r   r   	Exceptionr   objectr   rw  r   r   r6  rE  rt  rx  r   r   r   r   r   <module>   s  

+	  3*  2  V!u