rdkit.Chem.Draw.mplCanvas

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

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

      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 matplotlib.lines import Line2D
     12  from matplotlib.patches import Polygon
     13  from matplotlib.axes import Axes
     14  from matplotlib.pyplot import figure
     15  #from matplotlib import textpath,font_manager
     16  import numpy
     17
     18  from rdkit.Chem.Draw.canvasbase import CanvasBase
     19 -class Canvas(CanvasBase):
     20 -  def __init__(self, size, name='', imageType='png'):
     21      self._name = name
     22      self.size=size
     23      dpi = max(size[0],size[1])
     24      figsize=(int(float(size[0])/dpi),int(float(size[1])/dpi))
     25      self._figure = figure(figsize=figsize)
     26      self._axes = self._figure.add_axes([0,0,2.5,2.5])
     27      self._axes.set_xticklabels('')
     28      self._axes.set_yticklabels('')
     29      self._dpi = dpi
     30
     31
     32 -  def rescalePt(self,p1):
     33      return [float(p1[0])/self._dpi,float(self.size[1]-p1[1])/self._dpi]
     34
     35 -  def addCanvasLine(self,p1,p2,color=(0,0,0),color2=None,**kwargs):
     36      canvas = self._axes
     37      p1 = self.rescalePt(p1)
     38      p2 = self.rescalePt(p2)
     39      if color2 and color2!=color:
     40        mp = (p1[0]+p2[0])/2.,(p1[1]+p2[1])/2.
     41        canvas.add_line(Line2D((p1[0],mp[0]),(p1[1],mp[1]),
     42                               color=color,**kwargs))
     43        canvas.add_line(Line2D((mp[0],p2[0]),(mp[1],p2[1]),
     44                               color=color2,**kwargs))
     45      else:
     46        canvas.add_line(Line2D((p1[0],p2[0]),(p1[1],p2[1]),
     47                               color=color,**kwargs))
     48
     49 -  def addCanvasText(self,text,pos,font,color=(0,0,0),**kwargs):
     50      import re
     51      pos = self.rescalePt(pos)
     52      canvas = self._axes
     53      text = re.sub(r'<.*?>','',text)
     54      orientation=kwargs.get('orientation','E')
     55      halign='center'
     56      valign='center'
     57      if orientation=='E':
     58        halign='left'
     59      elif orientation=='W':
     60        halign='right'
     61      elif orientation=='S':
     62        valign='top'
     63      elif orientation=='N':
     64        valign='bottom'
     65
     66
     67      annot=canvas.annotate(text,(pos[0],pos[1]),color=color,
     68                            verticalalignment=valign,
     69                            horizontalalignment=halign,
     70                            weight=font.weight,
     71                            size=font.size*2.0,
     72                            family=font.face,
     73                            backgroundcolor='white')
     74
     75      try:
     76        bb = annot.get_window_extent(renderer=self._figure.canvas.get_renderer())
     77        w,h = bb.width,bb.height
     78        tw,th=canvas.transData.inverted().transform((w,h))
     79      except AttributeError:
     80        tw,th = 0.1,0.1 # <- kludge
     81      #print annot.xytext,w,h,tw,th
     82      return (tw,th,0)
     83
     84 -  def addCanvasPolygon(self,ps,color=(0,0,0),**kwargs):
     85      canvas = self._axes
     86      ps = [self.rescalePt(x) for x in ps]
     87      canvas.add_patch(Polygon(ps,linewidth=0,facecolor=color))
     88
     89
     90 -  def addCanvasDashedWedge(self,p1,p2,p3,dash=(2,2),color=(0,0,0),
     91                             color2=None,**kwargs):
     92      canvas = self._axes
     93      dash= (3,3)
     94      pts1 = self._getLinePoints(p1,p2,dash)
     95      pts2 = self._getLinePoints(p1,p3,dash)
     96      pts1 = [self.rescalePt(p) for p in pts1]
     97      pts2 = [self.rescalePt(p) for p in pts2]
     98      if len(pts2)<len(pts1):
     99        pts2,pts1=pts1,pts2
    100      for i in range(len(pts1)):
    101        if color2 and color2!=color:
    102          mp = (pts1[i][0]+pts2[i][0])/2.,(pts1[i][1]+pts2[i][1])/2.
    103          canvas.add_line(Line2D((pts1[i][0],mp[0]),(pts1[i][1],mp[1]),color=color,**kwargs))
    104          canvas.add_line(Line2D((mp[0],pts2[i][0]),(mp[1],pts2[i][1]),color=color2,**kwargs))
    105        else:
    106          canvas.add_line(Line2D((pts1[i][0],pts2[i][0]),(pts1[i][1],pts2[i][1]),color=color,**kwargs))
    107