rdkit.Chem.Draw.spingCanvas

Package rdkit :: Package Chem :: Package Draw :: Module spingCanvas |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Draw.spingCanvas

      1  # $Id$
      2  #
      3  #  Copyright (C) 2008 Greg Landrum
      4  #
      5  #   @@ All Rights Reserved @@
      6  #  This file is part of the RDKit.
      7  #  The contents are covered by the terms of the BSD license
      8  #  which is included in the file license.txt, found at the root
      9  #  of the RDKit source tree.
     10  #
     11  from rdkit.sping import pid
     12
     13  import math,re
     14
     15  from rdkit.sping.PIL.pidPIL import PILCanvas
     16  from rdkit.Chem.Draw.canvasbase import CanvasBase
     17
     18  faceMap={'sans':'helvetica',
     19           'serif':'times'}
     20
     21 -def convertColor(color):
     22    color = pid.Color(color[0],color[1],color[2])
     23    return color
     24
     25
     26 -class Canvas(CanvasBase):
     27 -  def __init__(self, size, name, imageType='png'):
     28      if imageType=="pdf":
     29        from rdkit.sping.PDF.pidPDF import PDFCanvas as _Canvas
     30      elif imageType=="ps":
     31        from rdkit.sping.PS.pidPS import PSCanvas as _Canvas  #@UnresolvedImport
     32      elif imageType=="svg":
     33        from rdkit.sping.SVG.pidSVG import SVGCanvas as _Canvas
     34      elif imageType=="png":
     35        from rdkit.sping.PIL.pidPIL import PILCanvas as _Canvas
     36      else:
     37        raise ValueError('unrecognized format: %s'%imageType)
     38      self.canvas = _Canvas(size=size, name=name)
     39      if hasattr(self.canvas,'_image'):
     40        self._image = self.canvas._image
     41      else:
     42        self._image = None
     43      self.size = size
     44
     45 -  def addCanvasLine(self, p1,p2,color=(0,0,0),color2=None,**kwargs):
     46      if color2 and color2!=color:
     47        mp = (p1[0]+p2[0])/2.,(p1[1]+p2[1])/2.
     48        color = convertColor(color)
     49        self.canvas.drawLine(p1[0],p1[1],mp[0],mp[1],
     50                        color=color,
     51                        width=int(kwargs.get('linewidth',1)),
     52                        dash=kwargs.get('dash',None))
     53        color2 = convertColor(color2)
     54        self.canvas.drawLine(mp[0],mp[1],p2[0],p2[1],
     55                        color=color2,
     56                        width=int(kwargs.get('linewidth',1)),
     57                        dash=kwargs.get('dash',None))
     58      else:
     59        color = convertColor(color)
     60        width=kwargs.get('linewidth',1)
     61        self.canvas.drawLine(p1[0],p1[1],p2[0],p2[1],
     62                        color=color,
     63                        width=int(width),
     64                        dash=kwargs.get('dash',None))
     65
     66 -  def addCanvasText(self, text,pos,font,color=(0,0,0),**kwargs):
     67      text = re.sub(r'\<.+?\>','',text)
     68      font = pid.Font(face=faceMap[font.face],size=font.size)
     69      txtWidth,txtHeight=self.canvas.stringBox(text,font)
     70      bw,bh=txtWidth+txtHeight*0.4,txtHeight*1.4
     71      offset = txtWidth*pos[2]
     72      labelP = pos[0]-txtWidth/2+offset,pos[1]+txtHeight/2
     73      color = convertColor(color)
     74      self.canvas.drawString(text,labelP[0],labelP[1],font,color=color)
     75      return (bw,bh,offset)
     76
     77 -  def addCanvasPolygon(self, ps,color=(0,0,0),fill=True,stroke=False,**kwargs):
     78      if not fill and not stroke: return
     79      edgeWidth=kwargs.get('lineWidth',0)
     80      edgeColor=pid.transparent
     81      color = convertColor(color)
     82      if not stroke:
     83        edgeWidth=0
     84        edgeColor=pid.transparent
     85      else:
     86        edgeWidth=kwargs.get('lineWidth',1)
     87        edgeColor=color
     88      if not fill:
     89        fillColor = pid.transparent
     90      else:
     91        fillColor = color
     92      self.canvas.drawPolygon(ps,edgeColor=edgeColor,edgeWidth=int(edgeWidth),fillColor=fillColor,closed=1)
     93
     94 -  def addCanvasDashedWedge(self,p1,p2,p3,dash=(2,2),color=(0,0,0),
     95                             color2=None,**kwargs):
     96      color = convertColor(color)
     97      dash = (4,4)
     98      pts1 = self._getLinePoints(p1,p2,dash)
     99      pts2 = self._getLinePoints(p1,p3,dash)
    100
    101      if len(pts2)<len(pts1): pts2,pts1=pts1,pts2
    102
    103      for i in range(len(pts1)):
    104        self.canvas.drawLine(pts1[i][0],pts1[i][1],pts2[i][0],pts2[i][1],
    105                        color=color,width=1)
    106
    107 -  def flush(self):
    108      self.canvas.flush()
    109
    110 -  def save(self):
    111      self.canvas.save()
    112