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