1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516from blockdiag.imagedraw.simplesvg import pathdata17from blockdiag.noderenderer import install_renderer18from blockdiag.noderenderer.base import NodeShape19from blockdiag.utils import XY, Box202122class Terminator(NodeShape):23 def __init__(self, node, metrics=None):24 super(Terminator, self).__init__(node, metrics)2526 m = self.metrics.cell(self.node)27 r = self.metrics.cellsize * 228 self.textbox = Box(m.topleft.x + r, m.topleft.y,29 m.bottomright.x - r, m.bottomright.y)3031 def render_shape(self, drawer, _, **kwargs):32 # draw background33 self.render_shape_background(drawer, **kwargs)3435 # draw outline36 if not kwargs.get('shadow') and self.node.background:37 drawer.image(self.textbox, self.node.background)3839 def render_shape_background(self, drawer, **kwargs):40 fill = kwargs.get('fill')4142 m = self.metrics.cell(self.node)43 r = self.metrics.cellsize * 24445 box = m.box46 ellipses = [Box(box[0], box[1], box[0] + r * 2, box[3]),47 Box(box[2] - r * 2, box[1], box[2], box[3])]4849 for e in ellipses:50 if kwargs.get('shadow'):51 e = self.shift_shadow(e)52 if kwargs.get('style') == 'blur':53 drawer.ellipse(e, fill=fill, outline=fill,54 filter='transp-blur')55 else:56 drawer.ellipse(e, fill=fill, outline=fill)57 else:58 drawer.ellipse(e, fill=self.node.color,59 outline=self.node.linecolor,60 style=self.node.style)6162 rect = Box(box[0] + r, box[1], box[2] - r, box[3])63 if kwargs.get('shadow'):64 rect = self.shift_shadow(rect)65 if kwargs.get('style') == 'blur':66 drawer.rectangle(rect, fill=fill, outline=fill,67 filter='transp-blur')68 else:69 drawer.rectangle(rect, fill=fill, outline=fill)70 else:71 drawer.rectangle(rect, fill=self.node.color,72 outline=self.node.color)7374 lines = [(XY(box[0] + r, box[1]), XY(box[2] - r, box[1])),75 (XY(box[0] + r, box[3]), XY(box[2] - r, box[3]))]76 for line in lines:77 if not kwargs.get('shadow'):78 drawer.line(line, fill=self.node.linecolor,79 style=self.node.style)8081 def render_vector_shape(self, drawer, _, **kwargs):82 fill = kwargs.get('fill')8384 # create pathdata85 m = self.metrics.cell(self.node)86 r = self.metrics.cellsize * 287 height = self.metrics.node_height8889 box = Box(m.topleft.x + r, m.topleft.y,90 m.bottomright.x - r, m.bottomright.y)91 if kwargs.get('shadow'):92 box = self.shift_shadow(box)9394 path = pathdata(box[0], box[1])95 path.line(box[2], box[1])96 path.ellarc(r, height / 2, 0, 0, 1, box[2], box[3])97 path.line(box[0], box[3])98 path.ellarc(r, height / 2, 0, 0, 1, box[0], box[1])99100 # draw outline101 if kwargs.get('shadow'):102 if kwargs.get('style') == 'blur':103 drawer.path(path, fill=fill, outline=fill,104 filter='transp-blur')105 else:106 drawer.path(path, fill=fill, outline=fill)107 elif self.node.background:108 drawer.path(path, fill=self.node.color, outline=self.node.color)109 drawer.image(self.textbox, self.node.background)110 drawer.path(path, fill="none",111 outline=self.node.linecolor, style=self.node.style)112 else:113 drawer.path(path, fill=self.node.color,114 outline=self.node.linecolor, style=self.node.style)115116117def setup(self):118 install_renderer('flowchart.terminator', Terminator)