clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / python / blockdiag-family / src / actdiag / metrics.py
metrics.py 3.3K
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 __future__ import division1718from collections import namedtuple1920import blockdiag.metrics21from blockdiag.utils import XY, Box2223from actdiag import elements242526class DiagramMetrics(blockdiag.metrics.DiagramMetrics):27 def __init__(self, diagram, **kwargs):28 super(DiagramMetrics, self).__init__(diagram, **kwargs)2930 if diagram.page_padding is None:31 if diagram.orientation == 'landscape':32 padding = self.node_width + self.span_width33 self.page_padding = [0, 0, 0, padding]34 else:35 padding = self.node_height + self.span_height36 self.page_padding = [padding, 0, 0, 0]3738 def pagesize(self, width=None, height=None):39 if width:40 self.colwidth = width41 else:42 width = self.colwidth4344 if height:45 self.colheight = height46 else:47 height = self.colheight4849 return super(DiagramMetrics, self).pagesize(width, height)5051 def frame(self, lanes):52 dummy = elements.DiagramNode(None)53 dummy.xy = XY(0, 0)54 dummy.colwidth = self.colwidth55 dummy.colheight = self.colheight56 cell = self.cell(dummy, use_padding=False)5758 headerbox = Box(cell.topleft.x - self.span_width // 2,59 (cell.topleft.y - self.node_height -60 self.span_height - 2),61 cell.topright.x + self.span_width // 2,62 cell.topright.y - self.span_height // 2)6364 outline = Box(headerbox[0], headerbox[1], headerbox[2],65 cell.bottom.y + self.span_height // 2)6667 separators = [(XY(headerbox[0], headerbox[3]),68 XY(headerbox[2], headerbox[3]))]6970 for lane in lanes[:-1]:71 x = lane.xy.x + lane.colwidth + 17273 m = self.cell(lane, use_padding=False)74 span_width = self.spreadsheet.span_width[x] // 275 x1 = m.right.x + span_width7677 xy = (XY(x1, outline[1]), XY(x1, outline[3]))78 separators.append(xy)7980 Frame = namedtuple('Frame', 'headerbox outline separators')81 return Frame(headerbox, outline, separators)8283 def lane_textbox(self, lane):84 headerbox = self.frame([]).headerbox85 m = self.cell(lane, use_padding=False)86 x1 = m.left.x87 x2 = m.right.x8889 return Box(x1, headerbox[1], x2, headerbox[3])9091 def lane_headerbox(self, lane):92 headerbox = self.frame([]).headerbox93 m = self.cell(lane)94 x1 = m.left.x - self.spreadsheet.span_width[lane.xy.x] // 295 x2 = m.right.x + self.spreadsheet.span_width[lane.xy.x + 1] // 29697 return Box(x1, headerbox[1], x2, headerbox[3])