Package rdkit :: Package Chem :: Package Draw :: Module canvasbase

[hide private]
[frames] | no frames]

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

     1  # $Id$
     2  #
     3  #  Copyright (C) 2010 Gianluca Sforna
     4  #
     5  #  All Rights Reserved
     6  #
     7  #  This file is part of the RDKit.
     8  #  The contents are covered by the terms of the BSD license
     9  #  which is included in the file license.txt, found at the root
    10  #  of the RDKit source tree.
    11
    12  import math
    13
    14 -class CanvasBase:
    15      """Base class for specialized canvas backends"""
    16
    17 -    def addCanvasLine(self, p1, p2, color=(0, 0, 0), color2=None, **kwargs):
    18          """Draw a single line on the canvas
    19
    20              This function will draw a line between `p1` and `p2` with the
    21              given `color`.
    22              If `color2` is specified, it will be used to draw the second half
    23              of the segment
    24          """
    25          raise NotImplementedError('This should be implemented')
    26
    27 -    def addCanvasText(self, text, pos, font, color=(0, 0, 0), **kwargs):
    28          """Draw some text
    29
    30             The provided `text` is drawn at position `pos` using the given
    31             `font` and the chosen `color`.
    32          """
    33          raise NotImplementedError('This should be implemented')
    34
    35 -    def addCanvasPolygon(self, ps, color=(0, 0 ,0), **kwargs):
    36          """Draw a polygon
    37
    38             Draw a polygon identified by vertexes given in `ps` using
    39             the given `color`
    40          """
    41          raise NotImplementedError('This should be implemented')
    42
    43 -    def addCanvasDashedWedge(self, p1, p2, p3, dash=(2, 2),
    44                               color=(0, 0, 0), color2=None, **kwargs):
    45          """Draw a dashed wedge
    46
    47             The wedge is identified by the three points `p1`, `p2`, and `p3`.
    48             It will be drawn using the given `color`; if `color2` is specified
    49             it will be used for the second half of the wedge
    50
    51             TODO: fix comment, I'm not sure what `dash` does
    52
    53          """
    54          raise NotImplementedError('This should be implemented')
    55
    56 -    def flush(self):
    57          """Complete any remaining draw operation
    58
    59             This is supposed to be the last operation on the canvas before
    60             saving it
    61          """
    62          raise NotImplementedError('This should be implemented')
    63
    64 -    def _getLinePoints(self, p1, p2, dash):
    65          x1, y1 = p1
    66          x2, y2 = p2
    67          dx = x2 - x1
    68          dy = y2 - y1
    69          lineLen = math.sqrt(dx * dx + dy * dy)
    70          theta = math.atan2(dy, dx)
    71          cosT = math.cos(theta)
    72          sinT = math.sin(theta)
    73
    74          pos = (x1, y1)
    75          pts = [pos]
    76          dist = 0
    77          currDash = 0
    78          while dist < lineLen:
    79              currL = dash[currDash % len(dash)]
    80              if (dist + currL > lineLen): currL = lineLen - dist
    81              endP = (pos[0] + currL * cosT, pos[1] + currL * sinT)
    82              pts.append(endP)
    83              pos = endP
    84              dist += currL
    85              currDash += 1
    86          return pts
    87