File: canvasbase.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (86 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
#  Copyright (C) 2010 Gianluca Sforna
#
#  All Rights Reserved
#
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.

import math


class CanvasBase:
  """Base class for specialized canvas backends"""

  def addCanvasLine(self, p1, p2, color=(0, 0, 0), color2=None, **kwargs):
    """Draw a single line on the canvas

            This function will draw a line between `p1` and `p2` with the
            given `color`.
            If `color2` is specified, it will be used to draw the second half
            of the segment
        """
    raise NotImplementedError('This should be implemented')

  def addCanvasText(self, text, pos, font, color=(0, 0, 0), **kwargs):
    """Draw some text

           The provided `text` is drawn at position `pos` using the given
           `font` and the chosen `color`.
        """
    raise NotImplementedError('This should be implemented')

  def addCanvasPolygon(self, ps, color=(0, 0, 0), **kwargs):
    """Draw a polygon

           Draw a polygon identified by vertexes given in `ps` using
           the given `color`
        """
    raise NotImplementedError('This should be implemented')

  def addCanvasDashedWedge(self, p1, p2, p3, dash=(2, 2), color=(0, 0, 0), color2=None, **kwargs):
    """Draw a dashed wedge

           The wedge is identified by the three points `p1`, `p2`, and `p3`.
           It will be drawn using the given `color`; if `color2` is specified
           it will be used for the second half of the wedge

           TODO: fix comment, I'm not sure what `dash` does

        """
    raise NotImplementedError('This should be implemented')

  def flush(self):
    """Complete any remaining draw operation

           This is supposed to be the last operation on the canvas before
           saving it
        """
    raise NotImplementedError('This should be implemented')

  def _getLinePoints(self, p1, p2, dash):
    x1, y1 = p1
    x2, y2 = p2
    dx = x2 - x1
    dy = y2 - y1
    lineLen = math.sqrt(dx * dx + dy * dy)
    theta = math.atan2(dy, dx)
    cosT = math.cos(theta)
    sinT = math.sin(theta)

    pos = (x1, y1)
    pts = [pos]
    dist = 0
    currDash = 0
    while dist < lineLen:
      currL = dash[currDash % len(dash)]
      if dist + currL > lineLen:
        currL = lineLen - dist
      endP = (pos[0] + currL * cosT, pos[1] + currL * sinT)
      pts.append(endP)
      pos = endP
      dist += currL
      currDash += 1
    return pts