B
    3RcX                 @   s   d Z dZdZed e ZddlT dZed ZG dd	 d	ZG d
d dZdZ	dZ
dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZG dd dZd#ddZG d d! d!Zd"S )$a1  
PIDDLE (Plug-In Drawing, Does Little Else)
2D Plug-In Drawing System

Magnus Lie Hetland
Andy Robinson
Joseph J. Strout
and others

February-March 1999

On coordinates: units are Big Points, approximately 1/72 inch.
The origin is at the top-left, and coordinates increase down (y)
and to the right (x).

z1.00.    )*H   gRQ@c               @   s    e Zd ZdZdd Zdd ZdS )
StateSaveral  This is a little utility class for saving and restoring the
          default drawing parameters of a canvas.  To use it, add a line
          like this before changing any of the parameters:

                  saver = StateSaver(myCanvas)

          then, when "saver" goes out of scope, it will automagically
          restore the drawing parameters of myCanvas.c             C   s*   || _ |j| _|j| _|j| _|j| _d S )N)canvasdefaultLineColordefaultFillColordefaultLineWidthdefaultFont)selfr    r   .lib/python3.7/site-packages/rdkit/sping/pid.py__init__w   s
    zStateSaver.__init__c             C   s,   | j | j_ | j| j_| j| j_| j| j_d S )N)r   r   r	   r
   r   )r   r   r   r   __del__~   s    


zStateSaver.__del__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   m   s   r   c               @   s2   e Zd ZdZdddZdd Zd	d
 Zdd ZdS )Fontz5This class represents font typeface, size, and style.   r   Nc             C   s2   | j }||d< ||d< ||d< ||d< ||d< d S )Nbolditalic	underlinesizeface)__dict__)r   r   r   r   r   r   dr   r   r   r      s    zFont.__init__c             C   sH   | j |j kr@| j|jkr@| j|jkr@| j|jkr@| j|jkr@dS dS dS )z-Compare two fonts to see if they're the same.r      N)r   r   r   r   r   )r   otherr   r   r   __cmp__   s
    zFont.__cmp__c             C   s    d| j | j| j| jt| jf S )NzFont(%d,%d,%d,%d,%s))r   r   r   r   reprr   )r   r   r   r   __repr__   s    zFont.__repr__c             C   s   t dd S )Nz$piddle.Font has read-only attributes)	TypeError)r   namevaluer   r   r   __setattr__   s    zFont.__setattr__)r   r   r   r   N)r   r   r   r   r   r    r"   r&   r   r   r   r   r      s
   
	r   r         	c               @   s  e Zd ZdZd>ddZ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dAddZdBddZdCddZdDddZdEd!d"Zd#d$ ZdFd%d&ZdGd'd(ZdHd)d*ZdId+d,ZdJd-d.ZdKd/d0ZdLd2d3ZdMd4d5ZdNd6d7ZdOd8d9ZdPd:d;ZdQd<d=ZdS )RCanvaszThis is the base class for a drawing canvas.  The 'plug-in renderers'
          we speak of are really just classes derived from this one, which implement
          the various drawing methods.i,  i,  PIDDLEc             C   sV   t | _t| _d| _t | _dd }|| _dd }|| _dd }|| _	|| | _
| _dS )	zInitialize the canvas, and set default drawing parameters.
                    Derived classes should be sure to call this method.r   c             S   s   d S )Nr   )r   xyr   r   r   ignoreClick   s    z$Canvas.__init__.<locals>.ignoreClickc             S   s   d S )Nr   )r   r8   r9   r   r   r   
ignoreOver   s    z#Canvas.__init__.<locals>.ignoreOverc             S   s   d S )Nr   )r   keyZ	modifiersr   r   r   	ignoreKey   s    z"Canvas.__init__.<locals>.ignoreKeyN)Zblackr   transparentr	   r
   r   r   ZonClickZonOverZonKeyr   r$   )r   r   r$   r:   r;   r=   r   r   r   r      s    zCanvas.__init__c             C   s   | j S )N)r   )r   r   r   r   getSize   s    zCanvas.getSizec             C   s   dS )zIReturns 1 if onClick, onOver, and onKey events are possible, 0 otherwise.r   r   )r   r   r   r   isInteractive   s    zCanvas.isInteractivec             C   s   dS )zReturns 1 if the drawing can be meaningfully updated over time                     (e.g., screen graphics), 0 otherwise (e.g., drawing to a file).r   r   )r   r   r   r   	canUpdate   s    zCanvas.canUpdatec             C   s   dS )z2Call this to clear and reset the graphics context.Nr   )r   r   r   r   clear   s    zCanvas.clearc             C   s   dS )zCall this to indicate that any comamnds that have been issued                     but which might be buffered should be flushed to the screenNr   )r   r   r   r   flush  s    zCanvas.flushNc             C   s   dS )aT  For backends that can be save to a file or sent to a
                    stream, create a valid file out of what's currently been
                    drawn on the canvas.  Trigger any finalization here.
                    Though some backends may allow further drawing after this call,
                    presume that this is not possible for maximum portability

                    file may be either a string or a file object with a write method
                         if left as the default, the canvas's current name will be used

                    format may be used to specify the type of file format to use as
                         well as any corresponding extension to use for the filename
                         This is an optional argument and backends may ignore it if
                         they only produce one file format.Nr   )r   fileformatr   r   r   save  s    zCanvas.savec             C   s   dS )zFor interactive Canvases, displays the given string in the                     'info line' somewhere where the user can probably see it.Nr   )r   sr   r   r   setInfoLine  s    zCanvas.setInfoLinec             C   s   |  ||| |fS )N)stringWidth
fontHeight)r   rG   fontr   r   r   	stringBox   s    zCanvas.stringBoxc             C   s   t ddS )zxReturn the logical width of the string if it were drawn                     in the current font (defaults to self.font).rI   N)NotImplementedError)r   rG   rK   r   r   r   rI   #  s    zCanvas.stringWidthc             C   s   |s
| j }d|j S )zMFind the height of one line of text (baseline to baseline) of the given font.g333333?)r   r   )r   rK   r   r   r   rJ   )  s    zCanvas.fontHeightc             C   s   t ddS )z6Find the ascent (height above base) of the given font.
fontAscentN)rM   )r   rK   r   r   r   rN   1  s    zCanvas.fontAscentc             C   s   t ddS )z7Find the descent (extent below base) of the given font.fontDescentN)rM   )r   rK   r   r   r   rO   5  s    zCanvas.fontDescentr   h  c             C   s   t || d }t || d }t||| }	t||| }
tt|||d  d d}|dk rdd}ddlm}m}m} g }t|| }|}xZtt	|d D ]F}|	|||d	 |   |
|||d	 |   f}|
| || }qW |S )
z4Return a list of points approximating the given arc.g       @g      $@
         r   )sincospir   g     f@)absminmaxmathrT   rU   rV   floatrangeintappend)r   x1y1x2y2startAngextentZxScaleZyScaler8   r9   ZstepsrT   rU   rV   	pointliststepangleiZpointr   r   r   	arcPoints;  s     0
zCanvas.arcPointsc	       %   	   C   s  t tt||||t |||| t||||t |||| d}	d|	 }
|
|
 }||
 }|}|}d } } }}|d|  d|  | }|d|  d|  | }d| d|  d|  }d| d|  d|  }d| d|  }d| d|  }|| }|| }|| }|| }|| ||
  }|| ||
  }d| }d| } |d|  }!| d|  }"||fg}#x`t|	D ]T}$|| | }|| | }|| |! }|| |" }|| }||  }|#||f qlW |#S )z=Return a list of points approximating the given Bezier curve.rR   g      ?r   r(      r'   )rX   rY   r\   r^   )%r   r_   r`   ra   rb   x3y3x4y4ZbezierStepsZdt1Zdt2Zdt3ZxxZyyZuxZuyZvxZvyZaxZayZbxZbyZcxZcyZmx1Zmy1Zlx1Zly1ZkxZkyZmxZmyZlxZly	pointListrh   r   r   r   curvePointsU  sF    8
zCanvas.curvePointsc          	   K   s   ddl }| |}	|	|||j d  }
|	|||j d  }|dd}|dd}|dd}|d}x0|D ](}| |||||| || }||
 }qvW dS )zLBreaks string into lines (on 
, , 
, or 
), and calls drawString on each.r   Ng     f@z

z
)rZ   rJ   rU   rV   rT   replacesplit
drawString)r   rG   r8   r9   rK   colorrg   kwargsrZ   hdydxlinesliner   r   r   drawMultiLineString  s    


zCanvas.drawMultiLineStringc       	      K   s   t ddS )z-Draw a straight line between x1,y1 and x2,y2.drawLineN)rM   )	r   r_   r`   ra   rb   rv   widthdashrw   r   r   r   r~     s    zCanvas.drawLinec       
      K   s:   x4|D ],\}}}}	| j ||||	||fd|i| qW dS )zxDraw a set of lines of uniform color and width.                      lineList: a list of (x1,y1,x2,y2) line coordinates.r   N)r~   )
r   ZlineListrv   r   r   rw   r_   r`   ra   rb   r   r   r   	drawLines  s    zCanvas.drawLinesc             K   s   t ddS )z'Draw a string starting at location x,y.ru   N)rM   )r   rG   r8   r9   rK   rv   rg   rw   r   r   r   ru     s    zCanvas.drawStringc          
   K   s:   |  ||||||||}| j|f|	|
|||d| dS )z7Draw a Bezier curve with control points x1,y1 to x4,y4.)	edgeColor	edgeWidth	fillColorclosedr   N)rp   drawPolygon)r   r_   r`   ra   rb   rk   rl   rm   rn   r   r   r   r   r   rw   re   r   r   r   	drawCurve  s    zCanvas.drawCurvec	             K   s>   ||f||f||f||fg}
| j |
|||fd|d|	 dS )zcDraw the rectangle between x1,y1, and x2,y2.                     These should have x1<x2 and y1<y2.r   )r   r   N)r   )r   r_   r`   ra   rb   r   r   r   r   rw   ro   r   r   r   drawRect  s    zCanvas.drawRect   c             K   s  t ||t|| }}t ||t|| }}|d }|d }t|||| || ddft|| ||| |ft|| |||| ddft||| ||| ft|| |||| ddft|| ||| |ft|||| || ddft||| ||| fg}| j||||	fd|
d| dS )	zDraw a rounded rectangle between x1,y1, and x2,y2,                     with corners inset as ellipses with x radius rx and y radius ry.                     These should have x1<x2, y1<y2, rx>0, and ry>0.r'      iZ   r   r   )r   r   N)rX   rY   	figureArc
figureLine
drawFigure)r   r_   r`   ra   rb   ZrxZryr   r   r   r   rw   rz   ry   partListr   r   r   drawRoundRect  s    ,,,0zCanvas.drawRoundRectc	             K   s6   |  ||||dd}
| j|
|||fd|d|	 dS )z}Draw an orthogonal ellipse inscribed within the rectangle x1,y1,x2,y2.                     These should have x1<x2 and y1<y2.r   ih  r   )r   r   N)ri   r   )r   r_   r`   ra   rb   r   r   r   r   rw   re   r   r   r   drawEllipse  s    zCanvas.drawEllipsec             K   sl   || d || d f}|  ||||||}| ||g |d g td|	 | j|||tfd|
i| dS )a#  Draw a partial ellipse inscribed within the rectangle x1,y1,x2,y2,                     starting at startAng degrees and covering extent degrees.   Angles                     start with 0 to the right (+x) and increase counter-clockwise.                     These should have x1<x2 and y1<y2.r'   r   r   N)ri   r   r>   )r   r_   r`   ra   rb   rc   rd   r   r   r   r   rw   centerre   r   r   r   drawArc  s     zCanvas.drawArcc             K   s   t ddS )zdrawPolygon(pointlist) -- draws a polygon
                    pointlist: a list of (x,y) tuples defining vertices
                    closed: if 1, adds an extra segment connecting the last point to the first
                    r   N)rM   )r   re   r   r   r   r   r   rw   r   r   r   r     s    zCanvas.drawPolygonc             K   s   g }x|D ]}	|	d }
t |	dd }|
tkrN||dd |dd g n@|
tkrh|| j|  n&|
tkr|| j|  ntd|
 | j||||f||d| q
W dS )aE  drawFigure(partList) -- draws a complex figure
                    partlist: a set of lines, curves, and arcs defined by a tuple whose
                                      first element is one of figureLine, figureArc, figureCurve
                                      and whose remaining 4, 6, or 8 elements are parameters.r   r   Nr'   zunknown figure operator: )r   r   )	listr   extendr   ri   figureCurverp   r#   r   )r   r   r   r   r   r   r   rw   ro   tupleopargsr   r   r   r     s    
 zCanvas.drawFigurec             K   s   t ddS )zDraw a PIL Image into the specified rectangle.  If x2 and y2 are
                    omitted, they are calculated from the image size.	drawImageN)rM   )r   Zimager_   r`   ra   rb   rw   r   r   r   r     s    zCanvas.drawImage)r6   r7   )NN)N)N)N)N)N)r   rP   )NNr   )NNN)NNN)NNr   )NNNr   N)NNNN)r   r   NNNN)NNNN)r   rP   NNNN)NNNr   N)NNNr   N)NN)r   r   r   r   r   r?   r@   rA   rB   rC   rF   rH   rL   rI   rJ   rN   rO   ri   rp   r}   r~   r   ru   r   r   r   r   r   r   r   r   r   r   r   r   r5      sB   
!






5





 

 

 
 
 
r5   wbc             C   s@   | r4t | trt| |}q<t| dr*| }q<tdntd|S )zCommon code for every Canvas.save() operation takes a string
          or a potential file object and assures that a valid fileobj is returnedwritezInvalid file argument to save)
isinstancestropenhasattr
ValueError)rD   Z	openFlagsZfileobjr   r   r   getFileObject  s    


r   c               @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )AffineMatrixNc             C   sF   |r0t |dkr|| _t|t| krB|j| _nddddddg| _d S )Nrj   g      ?r   g        )lenAtype)r   Zinitr   r   r   r   6  s    
zAffineMatrix.__init__c             C   sJ   || j d  || j d  || j d  || j d  | j d | j d g| _ d S )Nr   r   r'   r(      rS   )r   )r   ZsxZsyr   r   r   scale?  s    zAffineMatrix.scalec             C   s   t t| d }t t| d }| jd | | jd |  | jd | | jd |  | jd  | | jd |  | jd  | | jd |  | jd | jd g| _dS )	zCcounter clockwise rotation in standard SVG/libart coordinate systemg     f@r   r'   r   r(   r   rS   N)rZ   rU   ZPIrT   r   )r   ZthetacoZsir   r   r   rotateC  s
    4@zAffineMatrix.rotatec             C   sr   | j d | j d | j d | j d | j d | | j d |  | j d  | j d | | j d |  | j d  g| _ d S )Nr   r   r'   r(   r   rS   )r   )r   ZtxZtyr   r   r   	translateP  s     $zAffineMatrix.translate)N)r   r   r   r   r   r   r   r   r   r   r   r   1  s   
	r   N)r   )r   Z__version_maj_number__Z__version_min_number____version__Zrdkit.sping.colorsZinchcmr   r   r   r   r   ZkeyBkspZkeyDelZkeyLeftZkeyRightZkeyUpZkeyDownZkeyPgUpZkeyPgDnZkeyHomeZkeyEndZkeyClearZkeyTabZmodShiftZ
modControlr5   r   r   r   r   r   r   <module>^   s<   %  Y
