aboutsummaryrefslogtreecommitdiff
path: root/lib/d2/app/model/generate_svg_text.py
blob: c56784a44b156e2f2fa0a3bae3c9df5f41ce396e (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from collections import namedtuple

class SvgText(object):
    """Object for creating simple svg text elements
    """
    TEMPLATE = u"""
        <text
            id="{id}"
            x="{x}"
            y="{y}"
            fill="{fill}"
            font-size="{font_size}"
            {rotation}>
                {data}
        </text>
        """
    ROTATION = u"""
        transform="rotate({rotation}, {x}, {y})"
        """

    def __call__(self, id, x, y, rotation, data, font_size, fill):
        if rotation:
            rotate = self._build_rotation(rotation, x, y)
        else:
            rotate = ''
        return self.TEMPLATE.format(id=id, x=x, y=y, rotation=rotate,
                data=data, font_size=font_size, fill=fill)

    def _build_rotation(self, rotation, x, y):
        return self.ROTATION.format(rotation=rotation, x=x, y=y)

class Svg(object):
    TEMPLATE = u"""
        <svg
            xmlns:svg="http://www.w3.org/2000/svg"
            xmlns:xlink="http://www.w3.org/1999/xlink"
            xmlns="http://www.w3.org/2000/svg"
            version="1.1"
            zoomAndPan="magnify" preserveAspectRatio="xMidYMid meet"
            viewBox="0 0 {width} {height}"
            style="display:inline">
            <g
                pointer-events="none"
                text-anchor="left"
                font-size="12"
                fill="blue"
                stroke="none"
                display="inline">
                {text}
            </g>
        </svg>"""

    FONT_COLOR = {u'Person': 'blue',
                  u'Conference Room': 'green',
                  u'Work Area': 'cyan',
                  u'Empty': 'red',
                  u'Restroom': 'coral'}

    FONT_SIZE = { u'Person': '6',
                  u'Conference Room': '6',
                  u'Work Area': '6',
                  u'Empty': '6',
                  u'Restroom': '6'}

    def __init__(self):
        self.svgText = SvgText()
        self.body = []

    def _transform_coord(self, x_inkscape, y_inkscape):
        """ x, y coordinates stored in database is based on inkscape 
        convention. Need to transform into svg convention
        """
        x_new = x_inkscape
        y_new = self.height - y_inkscape

        return (x_new, y_new)

    def set_map(self, map):
        self.width = map.width
        self.height = map.height

    def add(self, row):
        font_color = self.FONT_COLOR[row.type]
        font_size = self.FONT_SIZE[row.type]
        (x, y) = self._transform_coord(row.x, row.y)
        self.body.append(self.svgText(row.id,
                                 x, 
                                 y, 
                                 None,
                                 row.label,
                                 font_size,
                                 font_color))

    def render(self):
        out = self.TEMPLATE.format(
                width = self.width,
                height = self.height,
                text = u"\n".join(self.body),
        )
        return out.encode('utf-8')