1# 2# Copyright (C) 2010 Gianluca Sforna 3# 4# All Rights Reserved 5# 6# This file is part of the RDKit. 7# The contents are covered by the terms of the BSD license 8# which is included in the file license.txt, found at the root 9# of the RDKit source tree. 10 11import math 12 13 14class 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), color=(0, 0, 0), color2=None, **kwargs): 44 """Draw a dashed wedge 45 46 The wedge is identified by the three points `p1`, `p2`, and `p3`. 47 It will be drawn using the given `color`; if `color2` is specified 48 it will be used for the second half of the wedge 49 50 TODO: fix comment, I'm not sure what `dash` does 51 52 """ 53 raise NotImplementedError('This should be implemented') 54 55 def flush(self): 56 """Complete any remaining draw operation 57 58 This is supposed to be the last operation on the canvas before 59 saving it 60 """ 61 raise NotImplementedError('This should be implemented') 62 63 def _getLinePoints(self, p1, p2, dash): 64 x1, y1 = p1 65 x2, y2 = p2 66 dx = x2 - x1 67 dy = y2 - y1 68 lineLen = math.sqrt(dx * dx + dy * dy) 69 theta = math.atan2(dy, dx) 70 cosT = math.cos(theta) 71 sinT = math.sin(theta) 72 73 pos = (x1, y1) 74 pts = [pos] 75 dist = 0 76 currDash = 0 77 while dist < lineLen: 78 currL = dash[currDash % len(dash)] 79 if dist + currL > lineLen: 80 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