Chem.Draw.aggCanvas

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

# Source Code for Module Chem.Draw.aggCanvas

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