summaryrefslogtreecommitdiffstats
path: root/share/extensions/ink2canvas_lib/canvas.py
blob: 9429420a5e114142b11888251d4e1ef685c9b9e3 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# coding=utf-8
#
# Copyright (C) 2011 Karlisson Bezerra <contact@hacktoon.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Canvas module for ink2canvas extension
"""

from textwrap import dedent

from inkex import Color, Style

# pylint: disable=too-many-public-methods, disable=missing-function-docstring


class Canvas:
    """Canvas API helper class"""

    def __init__(self, parent, width, height, context="ctx"):
        self.obj = context
        self.code = []  # stores the code
        self.style = Style()
        self.style_cache = Style()  # stores the previous style applied
        self.parent = parent
        self.width = width
        self.height = height

    def write(self, text):
        self.code.append("\t" + text.replace("ctx", self.obj) + "\n")

    def output(self):

        html = """
        <!DOCTYPE html>
        <html>
        <head>
            <title>Inkscape Output</title>
        </head>
        <body>
            <canvas id='canvas' width='%d' height='%d'></canvas>
            <script>
            var %s = document.getElementById("canvas").getContext("2d");
            %s
            </script>
        </body>
        </html>
        """
        return dedent(html) % (self.width, self.height, self.obj, "".join(self.code))

    def equal_style(self, style, key):
        """Checks if the last style used is the same or there's no style yet"""
        if key in self.style_cache:
            return True
        if key not in style:
            return True
        return style[key] == self.style_cache[key]

    def begin_path(self):
        self.write("ctx.beginPath();")

    def create_linear_gradient(self, href, x1, y1, x2, y2):
        data = (href, x1, y1, x2, y2)
        self.write(
            "var %s = \
                   ctx.createLinearGradient(%f,%f,%f,%f);"
            % data
        )

    def create_radial_gradient(self, href, cx1, cy1, rx, cx2, cy2, ry):
        data = (href, cx1, cy1, rx, cx2, cy2, ry)
        self.write(
            "var %s = ctx.createRadialGradient\
                   (%f,%f,%f,%f,%f,%f);"
            % data
        )

    def add_color_stop(self, href, pos, color):
        self.write("%s.addColorStop(%f, %s);" % (href, pos, color))

    @staticmethod
    def get_color(rgb, alpha):
        return "'{}'".format(str(Color(rgb).to_rgba(alpha)))

    def set_gradient(self, href):
        """
        for stop in gstops:
            style = simplestyle.parseStyle(stop.get("style"))
            stop_color = style["stop-color"]
            opacity = style["stop-opacity"]
            color = self.get_color(stop_color, opacity)
            pos = float(stop.get("offset"))
            self.add_color_stop(href, pos, color)
        """
        return None  # href

    def set_opacity(self, value):
        self.write("ctx.globalAlpha = %.1f;" % float(value))

    def set_fill(self, value):
        alpha = self.style("fill-opacity")
        if not value.startswith("url("):
            fill = self.get_color(value, alpha)
            self.write("ctx.fillStyle = %s;" % fill)

    def set_stroke(self, value):
        alpha = self.style("stroke-opacity")
        self.write("ctx.strokeStyle = %s;" % self.get_color(value, alpha))

    def set_stroke_width(self, value):
        self.write("ctx.lineWidth = %f;" % self.parent.svg.unittouu(value))

    def set_stroke_linecap(self, value):
        self.write("ctx.lineCap = '%s';" % value)

    def set_stroke_linejoin(self, value):
        self.write("ctx.lineJoin = '%s';" % value)

    def set_stroke_miterlimit(self, value):
        self.write("ctx.miterLimit = %s;" % value)

    def set_font(self, value):
        self.write('ctx.font = "%s";' % value)

    def move_to(self, x, y):
        self.write("ctx.moveTo(%f, %f);" % (x, y))

    def line_to(self, x, y):
        self.write("ctx.lineTo(%f, %f);" % (x, y))

    def close_path(self):
        self.write("ctx.closePath();")

    def quadratic_curve_to(self, cpx, cpy, x, y):
        data = (cpx, cpy, x, y)
        self.write("ctx.quadraticCurveTo(%f, %f, %f, %f);" % data)

    def bezier_curve_to(self, x1, y1, x2, y2, x, y):
        data = (x1, y1, x2, y2, x, y)
        self.write("ctx.bezierCurveTo(%f, %f, %f, %f, %f, %f);" % data)

    def rect(self, x, y, width, height, rx=0, ry=0):
        if rx or ry:
            # rounded rectangle, starts top-left anticlockwise
            self.move_to(x, y + ry)
            self.line_to(x, y + height - ry)
            self.quadratic_curve_to(x, y + height, x + rx, y + height)
            self.line_to(x + width - rx, y + height)
            self.quadratic_curve_to(x + width, y + height, x + width, y + height - ry)
            self.line_to(x + width, y + ry)
            self.quadratic_curve_to(x + width, y, x + width - rx, y)
            self.line_to(x + rx, y)
            self.quadratic_curve_to(x, y, x, y + ry)
        else:
            self.write("ctx.rect(%f, %f, %f, %f);" % (x, y, width, height))

    def arc(self, x, y, r, a1, a2, flag):
        data = (x, y, r, a1, a2, flag)
        self.write("ctx.arc(%f, %f, %f, %f, %.8f, %d);" % data)

    def fill_text(self, text, x, y):
        self.write('ctx.fillText("%s", %f, %f);' % (text, x, y))

    def translate(self, cx, cy):
        self.write("ctx.translate(%f, %f);" % (cx, cy))

    def rotate(self, angle):
        self.write("ctx.rotate(%f);" % angle)

    def scale(self, rx, ry):
        self.write("ctx.scale(%f, %f);" % (rx, ry))

    def transform(self, m11, m12, m21, m22, dx, dy):
        data = (m11, m12, m21, m22, dx, dy)
        self.write("ctx.transform(%f, %f, %f, %f, %f, %f);" % data)

    def save(self):
        self.write("ctx.save();")

    def restore(self):
        self.write("ctx.restore();")

    def finish_path(self):
        if self.style("fill") is not None:
            self.write("ctx.fill();")
        if self.style("stroke") is not None:
            self.write("ctx.stroke();")