diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
commit | 35a96bde514a8897f6f0fcc41c5833bf63df2e2a (patch) | |
tree | 657d15a03cc46bd099fc2c6546a7a4ad43815d9f /share/extensions/ink2canvas_lib | |
parent | Initial commit. (diff) | |
download | inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.tar.xz inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'share/extensions/ink2canvas_lib')
-rw-r--r-- | share/extensions/ink2canvas_lib/__init__.py | 0 | ||||
-rw-r--r-- | share/extensions/ink2canvas_lib/__pycache__/__init__.cpython-39.pyc | bin | 0 -> 147 bytes | |||
-rw-r--r-- | share/extensions/ink2canvas_lib/__pycache__/canvas.cpython-39.pyc | bin | 0 -> 7658 bytes | |||
-rw-r--r-- | share/extensions/ink2canvas_lib/__pycache__/svg.cpython-39.pyc | bin | 0 -> 10331 bytes | |||
-rw-r--r-- | share/extensions/ink2canvas_lib/canvas.py | 191 | ||||
-rw-r--r-- | share/extensions/ink2canvas_lib/svg.py | 293 |
6 files changed, 484 insertions, 0 deletions
diff --git a/share/extensions/ink2canvas_lib/__init__.py b/share/extensions/ink2canvas_lib/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/share/extensions/ink2canvas_lib/__init__.py diff --git a/share/extensions/ink2canvas_lib/__pycache__/__init__.cpython-39.pyc b/share/extensions/ink2canvas_lib/__pycache__/__init__.cpython-39.pyc Binary files differnew file mode 100644 index 0000000..b59cdcb --- /dev/null +++ b/share/extensions/ink2canvas_lib/__pycache__/__init__.cpython-39.pyc diff --git a/share/extensions/ink2canvas_lib/__pycache__/canvas.cpython-39.pyc b/share/extensions/ink2canvas_lib/__pycache__/canvas.cpython-39.pyc Binary files differnew file mode 100644 index 0000000..d774ae0 --- /dev/null +++ b/share/extensions/ink2canvas_lib/__pycache__/canvas.cpython-39.pyc diff --git a/share/extensions/ink2canvas_lib/__pycache__/svg.cpython-39.pyc b/share/extensions/ink2canvas_lib/__pycache__/svg.cpython-39.pyc Binary files differnew file mode 100644 index 0000000..c58117c --- /dev/null +++ b/share/extensions/ink2canvas_lib/__pycache__/svg.cpython-39.pyc diff --git a/share/extensions/ink2canvas_lib/canvas.py b/share/extensions/ink2canvas_lib/canvas.py new file mode 100644 index 0000000..c219132 --- /dev/null +++ b/share/extensions/ink2canvas_lib/canvas.py @@ -0,0 +1,191 @@ +# 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. +# +""" +Convas module for ink2canvas extension +""" + +from inkex import Color + +class Canvas(object): + """Canvas API helper class""" + + def __init__(self, parent, width, height, context="ctx"): + self.obj = context + self.code = [] # stores the code + self.style = {} + self.styleCache = {} # 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): + from textwrap import dedent + 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 equalStyle(self, style, key): + """Checks if the last style used is the same or there's no style yet""" + if key in self.styleCache: + return True + if key not in style: + return True + return style[key] == self.styleCache[key] + + def beginPath(self): + self.write("ctx.beginPath();") + + def createLinearGradient(self, href, x1, y1, x2, y2): + data = (href, x1, y1, x2, y2) + self.write("var %s = \ + ctx.createLinearGradient(%f,%f,%f,%f);" % data) + + def createRadialGradient(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 addColorStop(self, href, pos, color): + self.write("%s.addColorStop(%f, %s);" % (href, pos, color)) + + def getColor(self, rgb, alpha): + return "'{}'".format(str(Color(rgb).to_rgba(alpha))) + + def setGradient(self, href): + """ + for stop in gstops: + style = simplestyle.parseStyle(stop.get("style")) + stop_color = style["stop-color"] + opacity = style["stop-opacity"] + color = self.getColor(stop_color, opacity) + pos = float(stop.get("offset")) + self.addColorStop(href, pos, color) + """ + return None # href + + def setOpacity(self, value): + self.write("ctx.globalAlpha = %.1f;" % float(value)) + + def setFill(self, value): + try: + alpha = self.style["fill-opacity"] + except: + alpha = 1 + if not value.startswith("url("): + fill = self.getColor(value, alpha) + self.write("ctx.fillStyle = %s;" % fill) + + def setStroke(self, value): + try: + alpha = self.style["stroke-opacity"] + except: + alpha = 1 + self.write("ctx.strokeStyle = %s;" % self.getColor(value, alpha)) + + def setStrokeWidth(self, value): + self.write("ctx.lineWidth = %f;" % self.parent.svg.unittouu(value)) + + def setStrokeLinecap(self, value): + self.write("ctx.lineCap = '%s';" % value) + + def setStrokeLinejoin(self, value): + self.write("ctx.lineJoin = '%s';" % value) + + def setStrokeMiterlimit(self, value): + self.write("ctx.miterLimit = %s;" % value) + + def setFont(self, value): + self.write("ctx.font = \"%s\";" % value) + + def moveTo(self, x, y): + self.write("ctx.moveTo(%f, %f);" % (x, y)) + + def lineTo(self, x, y): + self.write("ctx.lineTo(%f, %f);" % (x, y)) + + def quadraticCurveTo(self, cpx, cpy, x, y): + data = (cpx, cpy, x, y) + self.write("ctx.quadraticCurveTo(%f, %f, %f, %f);" % data) + + def bezierCurveTo(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, w, h, rx=0, ry=0): + if rx or ry: + # rounded rectangle, starts top-left anticlockwise + self.moveTo(x, y + ry) + self.lineTo(x, y + h - ry) + self.quadraticCurveTo(x, y + h, x + rx, y + h) + self.lineTo(x + w - rx, y + h) + self.quadraticCurveTo(x + w, y + h, x + w, y + h - ry) + self.lineTo(x + w, y + ry) + self.quadraticCurveTo(x + w, y, x + w - rx, y) + self.lineTo(x + rx, y) + self.quadraticCurveTo(x, y, x, y + ry) + else: + self.write("ctx.rect(%f, %f, %f, %f);" % (x, y, w, h)) + + 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 fillText(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 closePath(self): + if "fill" in self.style and self.style["fill"] != "none": + self.write("ctx.fill();") + if "stroke" in self.style and self.style["stroke"] != "none": + self.write("ctx.stroke();") diff --git a/share/extensions/ink2canvas_lib/svg.py b/share/extensions/ink2canvas_lib/svg.py new file mode 100644 index 0000000..75796fc --- /dev/null +++ b/share/extensions/ink2canvas_lib/svg.py @@ -0,0 +1,293 @@ +# 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. +# +""" +Element parsing and context for ink2canvas extensions +""" + +from __future__ import unicode_literals + +import inkex + +class Element(object): + """Base Element""" + def __init__(self, node): + self.node = node + + def attr(self, val): + """Get attribute""" + try: + attr = float(self.node.get(val)) + except: + attr = self.node.get(val) + return attr + + +class GradientDef(Element): + def __init__(self, node, stops): + self.node = node + self.stops = stops + + +class LinearGradientDef(GradientDef): + def get_data(self): + x1 = self.attr("x1") + y1 = self.attr("y1") + x2 = self.attr("x2") + y2 = self.attr("y2") + # self.createLinearGradient(href, x1, y1, x2, y2) + + def draw(self): + pass + + +class RadialGradientDef(GradientDef): + def get_data(self): + cx = self.attr("cx") + cy = self.attr("cy") + r = self.attr("r") + # self.createRadialGradient(href, cx, cy, r, cx, cy, r) + + def draw(self): + pass + + +class AbstractShape(Element): + def __init__(self, command, node, ctx): + self.node = node + self.command = command + self.ctx = ctx + + def get_data(self): + return + + def get_style(self): + return self.node.style + + def set_style(self, style): + """Translates style properties names into method calls""" + self.ctx.style = style + for key in style: + tmp_list = [s.capitalize() for s in key.split("-")] + method = "set" + "".join(tmp_list) + if hasattr(self.ctx, method) and style[key] != "none": + getattr(self.ctx, method)(style[key]) + # saves style to compare in next iteration + self.ctx.style_cache = style + + def has_transform(self): + return bool(self.attr("transform")) + + def get_transform(self): + return self.node.transform.to_hexad() + + def has_gradient(self): + style = self.get_style() + if "fill" in style: + fill = style["fill"] + return fill.startswith("url(#linear") or fill.startswith("url(#radial") + return False + + def get_gradient_href(self): + style = self.get_style() + if "fill" in style: + return style["fill"][5:-1] + return + + def has_clip(self): + return bool(self.attr("clip-path")) + + def start(self, gradient): + self.gradient = gradient + self.ctx.write("\n// #%s" % self.attr("id")) + if self.has_transform() or self.has_clip(): + self.ctx.save() + + def draw(self): + data = self.get_data() + style = self.get_style() + self.ctx.beginPath() + if self.has_transform(): + trans_matrix = self.get_transform() + self.ctx.transform(*trans_matrix) # unpacks argument list + if self.has_gradient(): + self.gradient.draw() + self.set_style(style) + # unpacks "data" in parameters to given method + getattr(self.ctx, self.command)(*data) + self.ctx.closePath() + + def end(self): + if self.has_transform() or self.has_clip(): + self.ctx.restore() + + +class G(AbstractShape): + def draw(self): + # get layer label, if exists + if self.has_transform(): + trans_matrix = self.get_transform() + self.ctx.transform(*trans_matrix) + + +class Rect(AbstractShape): + def get_data(self): + x = self.attr("x") + y = self.attr("y") + w = self.attr("width") + h = self.attr("height") + rx = self.attr("rx") or 0 + ry = self.attr("ry") or 0 + return x, y, w, h, rx, ry + + +class Circle(AbstractShape): + def __init__(self, command, node, ctx): + AbstractShape.__init__(self, command, node, ctx) + self.command = "arc" + + def get_data(self): + import math + cx = self.attr("cx") + cy = self.attr("cy") + r = self.attr("r") + return cx, cy, r, 0, math.pi * 2, True + + +class Ellipse(AbstractShape): + def get_data(self): + cx = self.attr("cx") + cy = self.attr("cy") + rx = self.attr("rx") + ry = self.attr("ry") + return cx, cy, rx, ry + + def draw(self): + import math + cx, cy, rx, ry = self.get_data() + style = self.get_style() + self.ctx.beginPath() + if self.has_transform(): + trans_matrix = self.get_transform() + self.ctx.transform(*trans_matrix) # unpacks argument list + self.set_style(style) + + KAPPA = 4 * ((math.sqrt(2) - 1) / 3) + self.ctx.moveTo(cx, cy - ry) + self.ctx.bezierCurveTo(cx + (KAPPA * rx), cy - ry, cx + rx, cy - (KAPPA * ry), cx + rx, cy) + self.ctx.bezierCurveTo(cx + rx, cy + (KAPPA * ry), cx + (KAPPA * rx), cy + ry, cx, cy + ry) + self.ctx.bezierCurveTo(cx - (KAPPA * rx), cy + ry, cx - rx, cy + (KAPPA * ry), cx - rx, cy) + self.ctx.bezierCurveTo(cx - rx, cy - (KAPPA * ry), cx - (KAPPA * rx), cy - ry, cx, cy - ry) + self.ctx.closePath() + + +class Path(AbstractShape): + def pathMoveTo(self, data): + self.ctx.moveTo(data[0], data[1]) + self.currentPosition = data[0], data[1] + + def pathLineTo(self, data): + self.ctx.lineTo(data[0], data[1]) + self.currentPosition = data[0], data[1] + + def pathCurveTo(self, data): + x1, y1, x2, y2 = data[0], data[1], data[2], data[3] + x, y = data[4], data[5] + self.ctx.bezierCurveTo(x1, y1, x2, y2, x, y) + self.currentPosition = x, y + + def draw(self): + """Gets the node type and calls the given method""" + style = self.get_style() + self.ctx.beginPath() + if self.has_transform(): + trans_matrix = self.get_transform() + self.ctx.transform(*trans_matrix) # unpacks argument list + self.set_style(style) + + # Draws path commands + path_command = {"M": self.pathMoveTo, + "L": self.pathLineTo, + "C": self.pathCurveTo} + # Make sure we only have Lines and curves (no arcs etc) + for comm, data in self.node.path.to_superpath().to_path().to_arrays(): + if comm in path_command: + path_command[comm](data) + + self.ctx.closePath() + + +class Line(Path): + def get_data(self): + x1 = self.attr("x1") + y1 = self.attr("y1") + x2 = self.attr("x2") + y2 = self.attr("y2") + return ("M", (x1, y1)), ("L", (x2, y2)) + + +class Polygon(Path): + def get_data(self): + points = self.attr("points").strip().split(" ") + points = map(lambda x: x.split(","), points) + comm = [] + for pt in points: # creating path command similar + pt = list(map(float, pt)) + comm.append(["L", pt]) + comm[0][0] = "M" # first command must be a 'M' => moveTo + return comm + + +class Polyline(Polygon): + pass + + +class Text(AbstractShape): + def text_helper(self, tspan): + if not len(tspan): + return tspan.text + for ts in tspan: + return ts.text + self.text_helper(ts) + ts.tail + + def set_text_style(self, style): + keys = ("font-style", "font-weight", "font-size", "font-family") + text = [] + for key in keys: + if key in style: + text.append(style[key]) + self.ctx.setFont(" ".join(text)) + + def get_data(self): + x = self.attr("x") + y = self.attr("y") + return x, y + + def draw(self): + x, y = self.get_data() + style = self.get_style() + if self.has_transform(): + trans_matrix = self.get_transform() + self.ctx.transform(*trans_matrix) # unpacks argument list + self.set_style(style) + self.set_text_style(style) + + for tspan in self.node: + text = self.text_helper(tspan) + _x = float(tspan.get("x").split()[0]) + _y = float(tspan.get("y").split()[0]) + self.ctx.fillText(text, _x, _y) |