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

Source Code for Module rdkit.Chem.Draw.spingCanvas

  1  # $Id: spingCanvas.py 1760 2011-06-17 05:53:16Z glandrum $ 
  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 canvasbase import CanvasBase 
 17   
 18  faceMap={'sans':'helvetica'} 
 19   
20 -def convertColor(color):
21 color = pid.Color(color[0],color[1],color[2]) 22 return color
23 24
25 -class Canvas(CanvasBase):
26 - def __init__(self, size, name, imageType='png'):
27 if imageType=="pdf": 28 from rdkit.sping.PDF.pidPDF import PDFCanvas as Canvas 29 elif imageType=="ps": 30 from rdkit.sping.PS.pidPS import PSCanvas as Canvas 31 elif imageType=="svg": 32 from rdkit.sping.SVG.pidSVG import SVGCanvas as Canvas 33 elif imageType=="png": 34 from rdkit.sping.PIL.pidPIL import PILCanvas as Canvas 35 else: 36 raise ValueError,'unrecognized format: %s'%imageType 37 self.canvas = Canvas(size=size, name=name) 38 if hasattr(self.canvas,'_image'): 39 self._image = self.canvas._image 40 else: 41 self._image = None 42 self.size = size
43
44 - def addCanvasLine(self, p1,p2,color=(0,0,0),color2=None,**kwargs):
45 if color2 and color2!=color: 46 mp = (p1[0]+p2[0])/2.,(p1[1]+p2[1])/2. 47 color = convertColor(color) 48 self.canvas.drawLine(p1[0],p1[1],mp[0],mp[1], 49 color=color, 50 width=int(kwargs.get('linewidth',1)), 51 dash=kwargs.get('dash',None)) 52 color2 = convertColor(color2) 53 self.canvas.drawLine(mp[0],mp[1],p2[0],p2[1], 54 color=color2, 55 width=int(kwargs.get('linewidth',1)), 56 dash=kwargs.get('dash',None)) 57 else: 58 color = convertColor(color) 59 width=kwargs.get('linewidth',1) 60 self.canvas.drawLine(p1[0],p1[1],p2[0],p2[1], 61 color=color, 62 width=int(width), 63 dash=kwargs.get('dash',None))
64
65 - def addCanvasText(self, text,pos,font,color=(0,0,0),**kwargs):
66 text = re.sub(r'\<.+?\>','',text) 67 font = pid.Font(face=faceMap[font.face],size=font.size) 68 txtWidth,txtHeight=self.canvas.stringBox(text,font) 69 labelP = pos[0]-txtWidth/2,pos[1]+txtHeight/2 70 xPad = kwargs.get('xPadding',0) 71 yPad = kwargs.get('yPadding',0) 72 x1 = pos[0]-txtWidth/2 - xPad 73 y1 = pos[1]+txtHeight/2 + yPad 74 x2 = pos[0]+txtWidth/2 + xPad 75 y2 = pos[1]-txtHeight/2 - yPad 76 bgColor=kwargs.get('bgColor',(1,1,1)) 77 bgColor = convertColor(bgColor) 78 self.canvas.drawRect(x1,y1,x2,y2, 79 edgeColor=pid.transparent, 80 edgeWidth=0,fillColor=bgColor) 81 color = convertColor(color) 82 self.canvas.drawString(text,labelP[0],labelP[1],font,color=color)
83
84 - def addCanvasPolygon(self, ps,color=(0,0,0),fill=True,stroke=False,**kwargs):
85 if not fill and not stroke: return 86 edgeWidth=kwargs.get('lineWidth',0) 87 edgeColor=pid.transparent 88 color = convertColor(color) 89 if not stroke: 90 edgeWidth=0 91 edgeColor=pid.transparent 92 else: 93 edgeWidth=kwargs.get('lineWidth',1) 94 edgeColor=color 95 if not fill: 96 fillColor = pid.transparent 97 else: 98 fillColor = color 99 self.canvas.drawPolygon(ps,edgeColor=edgeColor,edgeWidth=int(edgeWidth),fillColor=fillColor,closed=1)
100
101 - def addCanvasDashedWedge(self,p1,p2,p3,dash=(2,2),color=(0,0,0), 102 color2=None,**kwargs):
103 color = convertColor(color) 104 dash = (4,4) 105 pts1 = self._getLinePoints(p1,p2,dash) 106 pts2 = self._getLinePoints(p1,p3,dash) 107 108 if len(pts2)<len(pts1): pts2,pts1=pts1,pts2 109 110 for i in range(len(pts1)): 111 self.canvas.drawLine(pts1[i][0],pts1[i][1],pts2[i][0],pts2[i][1], 112 color=color,width=1)
113
114 - def flush(self):
115 self.canvas.flush()
116
117 - def save(self):
118 self.canvas.save()
119