rdkit.Chem.Draw.aggCanvas

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

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

      1  # $Id: aggCanvas.py 997 2009-02-25 06:12:43Z glandrum $
      2  #
      3  #  Copyright (C) 2008 Greg Landrum
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  from aggdraw import Brush, Pen
      8  from aggdraw import Font
      9  import math
     10  from rdkit import RDConfig
     11  import os
     12
     13  faceMap={'sans':os.path.join(RDConfig.RDCodeDir,'Chem','Draw','FreeSans.ttf')}
     14
     15 -def convertColor(color):
     16    color = (int(color[0]*255),int(color[1]*255),int(color[2]*255))
     17    return color
     18
     19 -def _getLinePoints(p1,p2,dash):
     20    x1,y1=p1
     21    x2,y2=p2
     22    dx = x2-x1
     23    dy = y2-y1
     24    lineLen = math.sqrt(dx*dx+dy*dy)
     25    theta = math.atan2(dy,dx)
     26    cosT = math.cos(theta)
     27    sinT = math.sin(theta)
     28
     29    pos = (x1,y1)
     30    pts = [pos]
     31    dist = 0
     32    currDash = 0
     33    while dist < lineLen:
     34      currL = dash[currDash%len(dash)]
     35      if(dist+currL > lineLen): currL = lineLen-dist
     36      endP = (pos[0] + currL*cosT, pos[1] + currL*sinT)
     37      pts.append(endP)
     38      pos = endP
     39      dist += currL
     40      currDash += 1
     41    return pts
     42
     43 -def _doLine(canvas,p1,p2,pen,**kwargs):
     44    if kwargs.get('dashes',(0,0)) == (0,0):
     45      canvas.line((p1[0],p1[1],p2[0],p2[1]),pen)
     46    else:
     47      # the antialiasing makes the dashes appear too small
     48      dash = [x*4 for x in kwargs['dashes']]
     49      pts = _getLinePoints(p1,p2,dash)
     50
     51      currDash = 0
     52      dashOn = True
     53      while currDash<(len(pts)-1):
     54        if dashOn:
     55          p1 = pts[currDash]
     56          p2 = pts[currDash+1]
     57          canvas.line((p1[0],p1[1],p2[0],p2[1]),pen)
     58        currDash+=1
     59        dashOn = not dashOn
     60
     61 -def addCanvasLine(canvas,p1,p2,color=(0,0,0),color2=None,**kwargs):
     62    if color2 and color2!=color:
     63      mp = (p1[0]+p2[0])/2.,(p1[1]+p2[1])/2.
     64      color = convertColor(color)
     65      _doLine(canvas,p1,mp,Pen(color,kwargs.get('linewidth',1)),**kwargs)
     66      color2 = convertColor(color2)
     67      _doLine(canvas,mp,p2,Pen(color2,kwargs.get('linewidth',1)),**kwargs)
     68    else:
     69      color = convertColor(color)
     70      _doLine(canvas,p1,p2,Pen(color,kwargs.get('linewidth',1)),**kwargs)
     71
     72 -def addCanvasText(canvas,text,pos,font,color=(0,0,0),**kwargs):
     73    color = convertColor(color)
     74    font = Font(color,faceMap[font.face],size=font.size)
     75    w,h=canvas.textsize(text,font)
     76    bw,bh=w*1.1,h*1.1
     77    dPos = pos[0]-bw/2.,pos[1]-bh/2.
     78    bgColor=kwargs.get('bgColor',(1,1,1))
     79    bgColor = convertColor(bgColor)
     80    canvas.rectangle((dPos[0],dPos[1],dPos[0]+bw,dPos[1]+bh),
     81                     None,Brush(bgColor))
     82    dPos = pos[0]-w/2.,pos[1]-h/2.
     83    canvas.text(dPos,text,font)
     84
     85 -def addCanvasPolygon(canvas,ps,color=(0,0,0),**kwargs):
     86    dps = []
     87    for p in ps:
     88      dps.extend(p)
     89    color = convertColor(color)
     90    canvas.polygon(dps,None,Brush(color));
     91
     92 -def addCanvasDashedWedge(canvas,p1,p2,p3,dash=(2,2),color=(0,0,0),
     93                           color2=None,**kwargs):
     94    pen = Pen(color,kwargs.get('linewidth',1))
     95    dash = (3,3)
     96    pts1 = _getLinePoints(p1,p2,dash)
     97    pts2 = _getLinePoints(p1,p3,dash)
     98
     99    if len(pts2)<len(pts1): pts2,pts1=pts1,pts2
    100
    101    for i in range(len(pts1)):
    102      canvas.line((pts1[i][0],pts1[i][1],pts2[i][0],pts2[i][1]),pen)
    103