clone

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

main
diagramzip / renderers / python / blockdiag-family / src / packetdiag / metrics.py
metrics.py 2.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 collections import defaultdict, namedtuple1718import blockdiag.metrics19from blockdiag.utils import XY, Box202122class DiagramMetrics(blockdiag.metrics.DiagramMetrics):23 def __init__(self, diagram, **kwargs):24 span_height = self.span_height25 span_width = self.span_width26 self.span_height = 027 self.span_width = 028 self.node_width = self.cellsize * 329 self.colwidth = diagram.colwidth30 super(DiagramMetrics, self).__init__(diagram, **kwargs)3132 # reset node_width FORCE33 self.spreadsheet.node_width = defaultdict(lambda: self.node_width)3435 self.spreadsheet.set_span_height(0, span_height * 2)36 self.spreadsheet.set_span_height(diagram.colheight, span_height)37 self.spreadsheet.set_span_width(0, span_width)38 self.spreadsheet.set_span_width(diagram.colwidth, span_width)3940 def measure_line(self, n):41 _Node = namedtuple('Node', 'xy')4243 if n == self.colwidth:44 node = _Node(XY(n - 1, 0))45 pt = self.spreadsheet._node_topleft(node, use_padding=False)46 pt = pt.shift(x=self.node_width)47 else:48 node = _Node(XY(n, 0))49 pt = self.spreadsheet._node_topleft(node, use_padding=False)5051 if n * 2.0 % self.colwidth == 0:52 return (XY(pt.x, pt.y - self.cellsize * 4), pt)53 elif n * 4.0 % self.colwidth == 0:54 return (XY(pt.x, pt.y - self.cellsize * 4), pt)55 else:56 return (XY(pt.x, pt.y - self.cellsize * 2), pt)5758 def measure_label(self, n):59 top, _ = self.measure_line(n)60 cellsize = self.cellsize6162 return Box(top.x - cellsize * 4, top.y - cellsize * 3,63 top.x + cellsize * 4, top.y)