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  # $Id: spingCanvas.py 1029 2009-03-23 17:00:45Z glandrum $
     2  #
     3  #  Copyright (C) 2008 Greg Landrum
     4  #
     5  #   @@ All Rights Reserved  @@
     6  #
     7  from rdkit.sping import pid
     8  import math
     9
    10  faceMap={'sans':'helvetica'}
    11
    12 -def convertColor(color):
    13    color = pid.Color(color[0],color[1],color[2])
    14    return color
    15
    16 -def _getLinePoints(p1,p2,dash):
    17    x1,y1=p1
    18    x2,y2=p2
    19    dx = x2-x1
    20    dy = y2-y1
    21    lineLen = math.sqrt(dx*dx+dy*dy)
    22    theta = math.atan2(dy,dx)
    23    cosT = math.cos(theta)
    24    sinT = math.sin(theta)
    25
    26    pos = (x1,y1)
    27    pts = [pos]
    28    dist = 0
    29    currDash = 0
    30    while dist < lineLen:
    31      currL = dash[currDash%len(dash)]
    32      if(dist+currL > lineLen): currL = lineLen-dist
    33      endP = (pos[0] + currL*cosT, pos[1] + currL*sinT)
    34      pts.append(endP)
    35      pos = endP
    36      dist += currL
    37      currDash += 1
    38    return pts
    39
    40 -def addCanvasLine(canvas,p1,p2,color=(0,0,0),color2=None,**kwargs):
    41    if color2 and color2!=color:
    42      mp = (p1[0]+p2[0])/2.,(p1[1]+p2[1])/2.
    43      color = convertColor(color)
    44      canvas.drawLine(p1[0],p1[1],mp[0],mp[1],
    45                      color=color,
    46                      width=int(kwargs.get('linewidth',1)),
    47                      dash=kwargs.get('dash',None))
    48      color2 = convertColor(color2)
    49      canvas.drawLine(mp[0],mp[1],p2[0],p2[1],
    50                      color=color2,
    51                      width=int(kwargs.get('linewidth',1)),
    52                      dash=kwargs.get('dash',None))
    53    else:
    54      color = convertColor(color)
    55      width=kwargs.get('linewidth',1)
    56      canvas.drawLine(p1[0],p1[1],p2[0],p2[1],
    57                      color=color,
    58                      width=int(width),
    59                      dash=kwargs.get('dash',None))
    60
    61 -def addCanvasText(canvas,text,pos,font,color=(0,0,0),**kwargs):
    62    font = pid.Font(face=faceMap[font.face],size=font.size)
    63    txtWidth=canvas.stringWidth(text,font)
    64    txtHeight=canvas.fontAscent(font)
    65    labelP = pos[0]-txtWidth/2,pos[1]+txtHeight/2
    66    xPad = kwargs.get('xPadding',0)
    67    yPad = kwargs.get('yPadding',0)
    68    x1 = pos[0]-txtWidth/2 - xPad
    69    y1 = pos[1]+txtHeight/2 + yPad
    70    x2 = pos[0]+txtWidth/2 + xPad
    71    y2 = pos[1]-txtHeight/2 - yPad
    72    bgColor=kwargs.get('bgColor',(1,1,1))
    73    bgColor = convertColor(bgColor)
    74    canvas.drawRect(x1,y1,x2,y2,
    75                    edgeColor=pid.transparent,
    76                    edgeWidth=0,fillColor=bgColor)
    77    color = convertColor(color)
    78    canvas.drawString(text,labelP[0],labelP[1],font,color=color)
    79
    80 -def addCanvasPolygon(canvas,ps,color=(0,0,0),**kwargs):
    81    edgeWidth=kwargs.get('lineWidth',0)
    82    edgeColor=pid.transparent
    83    color = convertColor(color)
    84    canvas.drawPolygon(ps,edgeColor=edgeColor,edgeWidth=int(edgeWidth),fillColor=color,closed=1)
    85
    86 -def addCanvasDashedWedge(canvas,p1,p2,p3,dash=(2,2),color=(0,0,0),
    87                           color2=None,**kwargs):
    88    color = convertColor(color)
    89    dash = (4,4)
    90    pts1 = _getLinePoints(p1,p2,dash)
    91    pts2 = _getLinePoints(p1,p3,dash)
    92
    93    if len(pts2)<len(pts1): pts2,pts1=pts1,pts2
    94
    95    for i in range(len(pts1)):
    96      canvas.drawLine(pts1[i][0],pts1[i][1],pts2[i][0],pts2[i][1],
    97                      color=color,width=1)
    98