summaryrefslogtreecommitdiffstats
path: root/share/extensions/inkex/extensions.py
blob: 37ead3d2d8e198a65cc183fdf0b66f689811b368 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Martin Owens <doctormo@gmail.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.
#
"""
A helper module for creating Inkscape effect extensions

This provides the basic generic types of extensions which most writers should
use in their code. See below for the different types.
"""

import os
import re
import sys
import types
from abc import ABC

from .utils import errormsg, Boolean
from .colors import Color, ColorError
from .elements import (
    load_svg,
    BaseElement,
    ShapeElement,
    Group,
    Layer,
    Grid,
    TextElement,
    FlowPara,
    FlowDiv,
    Pattern,
)
from .elements._utils import CloningVat
from .base import (
    InkscapeExtension,
    SvgThroughMixin,
    SvgInputMixin,
    SvgOutputMixin,
    TempDirMixin,
)
from .transforms import Transform
from .elements import LinearGradient, RadialGradient

# All the names that get added to the inkex API itself.
__all__ = (
    "EffectExtension",
    "GenerateExtension",
    "InputExtension",
    "OutputExtension",
    "RasterOutputExtension",
    "CallExtension",
    "TemplateExtension",
    "ColorExtension",
    "TextExtension",
)

stdout = sys.stdout


class EffectExtension(SvgThroughMixin, InkscapeExtension, ABC):
    """
    Takes the SVG from Inkscape, modifies the selection or the document
    and returns an SVG to Inkscape.
    """


class OutputExtension(SvgInputMixin, InkscapeExtension):
    """
    Takes the SVG from Inkscape and outputs it to something that's not an SVG.

    Used in functions for `Save As`
    """

    def effect(self):
        """Effect isn't needed for a lot of Output extensions"""

    def save(self, stream):
        """But save certainly is, we give a more exact message here"""
        raise NotImplementedError("Output extensions require a save(stream) method!")


class RasterOutputExtension(InkscapeExtension):
    """
    Takes a PNG from Inkscape and outputs it to another rather format.

    .. versionadded:: 1.1
    """

    def __init__(self):
        super().__init__()
        self.img = None

    def load(self, stream):
        from PIL import Image

        # disable the PIL decompression bomb DOS attack check.
        Image.MAX_IMAGE_PIXELS = None

        self.img = Image.open(stream)

    def effect(self):
        """Not needed since image isn't being changed"""

    def save(self, stream):
        """Implement raster image saving here from PIL"""
        raise NotImplementedError("Raster Output extension requires a save method!")


class InputExtension(SvgOutputMixin, InkscapeExtension):
    """
    Takes any type of file as input and outputs SVG which Inkscape can read.

    Used in functions for `Open`
    """

    def effect(self):
        """Effect isn't needed for a lot of Input extensions"""

    def load(self, stream):
        """But load certainly is, we give a more exact message here"""
        raise NotImplementedError("Input extensions require a load(stream) method!")


class CallExtension(TempDirMixin, InputExtension):
    """Call an external program to get the output"""

    input_ext = "svg"
    output_ext = "svg"

    def load(self, stream):
        pass  # Not called (load_raw instead)

    def load_raw(self):
        # Don't call InputExtension.load_raw
        TempDirMixin.load_raw(self)
        input_file = self.options.input_file

        if not isinstance(input_file, str):
            data = input_file.read()
            input_file = os.path.join(self.tempdir, "input." + self.input_ext)
            with open(input_file, "wb") as fhl:
                fhl.write(data)

        output_file = os.path.join(self.tempdir, "output." + self.output_ext)
        document = self.call(input_file, output_file) or output_file
        if isinstance(document, str):
            if not os.path.isfile(document):
                raise IOError(f"Can't find generated document: {document}")

            if self.output_ext == "svg":
                with open(document, "r", encoding="utf-8") as fhl:
                    document = fhl.read()
                if "<" in document:
                    document = load_svg(document.encode("utf-8"))
            else:
                with open(document, "rb") as fhl:
                    document = fhl.read()

        self.document = document

    def call(self, input_file, output_file):
        """Call whatever programs are needed to get the desired result."""
        raise NotImplementedError("Call extensions require a call(in, out) method!")


class GenerateExtension(EffectExtension):
    """
    Does not need any SVG, but instead just outputs an SVG fragment which is
    inserted into Inkscape, centered on the selection.
    """

    container_label = ""
    container_layer = False

    def generate(self):
        """
        Return an SVG fragment to be inserted into the selected layer of the document
        OR yield multiple elements which will be grouped into a container group
        element which will be given an automatic label and transformation.
        """
        raise NotImplementedError("Generate extensions must provide generate()")

    def container_transform(self):
        """
        Generate the transformation for the container group, the default is
        to return the center position of the svg document or view port.
        """
        (pos_x, pos_y) = self.svg.namedview.center
        if pos_x is None:
            pos_x = 0
        if pos_y is None:
            pos_y = 0
        return Transform(translate=(pos_x, pos_y))

    def create_container(self):
        """
        Return the container the generated elements will go into.

        Default is a new layer or current layer depending on the :attr:`container_layer`
        flag.

        .. versionadded:: 1.1
        """
        container = (Layer if self.container_layer else Group).new(self.container_label)
        if self.container_layer:
            self.svg.append(container)
        else:
            container.transform = self.container_transform()
            parent = self.svg.get_current_layer()
            try:
                parent_transform = parent.composed_transform()
            except AttributeError:
                pass
            else:
                container.transform = -parent_transform @ container.transform
            parent.append(container)
        return container

    def effect(self):
        layer = self.svg.get_current_layer()
        fragment = self.generate()
        if isinstance(fragment, types.GeneratorType):
            container = self.create_container()
            for child in fragment:
                if isinstance(child, BaseElement):
                    container.append(child)
        elif isinstance(fragment, BaseElement):
            layer.append(fragment)
        else:
            errormsg("Nothing was generated\n")


class TemplateExtension(EffectExtension):
    """
    Provide a standard way of creating templates.
    """

    size_rex = re.compile(r"([\d.]*)(\w\w)?x([\d.]*)(\w\w)?")
    template_id = "SVGRoot"

    def __init__(self):
        self.svg = None
        super().__init__()
        # Arguments added on after add_arguments so it can be overloaded cleanly.
        self.arg_parser.add_argument("--size", type=self.arg_size(), dest="size")
        self.arg_parser.add_argument("--width", type=int, default=800)
        self.arg_parser.add_argument("--height", type=int, default=600)
        self.arg_parser.add_argument("--orientation", default=None)
        self.arg_parser.add_argument("--unit", default="px")
        self.arg_parser.add_argument("--grid", type=Boolean)
        # self.svg = None

    def get_template(self, **kwargs):
        """Can be over-ridden with custom svg loading here"""
        return self.document

    def arg_size(self, unit="px"):
        """Argument is a string of the form X[unit]xY[unit], default units apply
        when missing"""

        def _inner(value):
            try:
                value = float(value)
                return (value, unit, value, unit)
            except ValueError:
                pass
            match = self.size_rex.match(str(value))
            if match is not None:
                size = match.groups()
                return (
                    float(size[0]),
                    size[1] or unit,
                    float(size[2]),
                    size[3] or unit,
                )
            return None

        return _inner

    def get_size(self):
        """Get the size of the new template (defaults to size options)"""
        size = self.options.size
        if self.options.size is None:
            size = (
                self.options.width,
                self.options.unit,
                self.options.height,
                self.options.unit,
            )
        if (
            self.options.orientation == "horizontal"
            and size[0] < size[2]
            or self.options.orientation == "vertical"
            and size[0] > size[2]
        ):
            size = size[2:4] + size[0:2]
        return size

    def effect(self):
        """Creates a template, do not over-ride"""
        (width, width_unit, height, height_unit) = self.get_size()
        width_px = int(self.svg.uutounit(width, "px"))
        height_px = int(self.svg.uutounit(height, "px"))

        self.document = self.get_template()
        self.svg = self.document.getroot()
        self.svg.set("id", self.template_id)
        self.svg.set("width", str(width) + width_unit)
        self.svg.set("height", str(height) + height_unit)
        self.svg.set("viewBox", f"0 0 {width} {height}")
        self.set_namedview(width_px, height_px, width_unit)

    def set_namedview(self, width, height, unit):
        """Setup the document namedview"""
        self.svg.namedview.set("inkscape:document-units", unit)
        self.svg.namedview.set("inkscape:zoom", "0.25")
        self.svg.namedview.set("inkscape:cx", str(width / 2.0))
        self.svg.namedview.set("inkscape:cy", str(height / 2.0))
        if self.options.grid:
            self.svg.namedview.set("showgrid", "true")
            self.svg.namedview.add(Grid(type="xygrid"))


class ColorExtension(EffectExtension):
    """
    A standard way to modify colours in an svg document.
    """

    process_none = False  # should we call modify_color for the "none" color.
    select_all = (ShapeElement,)
    pass_rgba = False
    """
    If true, color and opacity are processed together (as RGBA color) 
    by :func:`modify_color`.

    If false (default), they are processed independently by `modify_color` and 
    `modify_opacity`.

    .. versionadded:: 1.2
    """

    def __init__(self):
        super().__init__()
        self._renamed = {}

    def effect(self):
        # Limiting to shapes ignores Gradients (and other things) from the select_all
        # this prevents defs from being processed twice.
        self._renamed = {}
        gradients = CloningVat(self.svg)
        for elem in self.svg.selection.get(ShapeElement):
            self.process_element(elem, gradients)
        gradients.process(self.process_elements, types=(ShapeElement,))

    def process_elements(self, elem):
        """Process multiple elements (gradients)"""
        for child in elem.descendants():
            self.process_element(child)

    def process_element(self, elem, gradients=None):
        """Process one of the selected elements"""
        style = elem.specified_style()
        # Colours first
        for name in (
            elem.style.associated_props if self.pass_rgba else elem.style.color_props
        ):
            if name not in style:
                continue  # we don't want to process default values
            try:
                value = style(name)
            except ColorError:
                continue  # bad color value, don't touch.
            if isinstance(value, Color):
                col = Color(value)
                if self.pass_rgba:
                    col = col.to_rgba(
                        alpha=elem.style(elem.style.associated_props[name])
                    )
                rgba_result = self._modify_color(name, col)
                elem.style.set_color(rgba_result, name)

            if isinstance(value, (LinearGradient, RadialGradient, Pattern)):
                gradients.track(value, elem, self._ref_cloned, style=style, name=name)
                if value.href is not None:
                    gradients.track(value.href, elem, self._xlink_cloned, linker=value)
        # Then opacities (usually does nothing)
        if self.pass_rgba:
            return
        for name in elem.style.opacity_props:
            value = style(name)
            result = self.modify_opacity(name, value)
            if result not in (value, 1):  # only modify if not equal to old or default
                elem.style[name] = result

    def _ref_cloned(self, old_id, new_id, style, name):
        self._renamed[old_id] = new_id
        style[name] = f"url(#{new_id})"

    def _xlink_cloned(self, old_id, new_id, linker):  # pylint: disable=unused-argument
        lid = linker.get("id")
        linker = self.svg.getElementById(self._renamed.get(lid, lid))
        linker.set("xlink:href", "#" + new_id)

    def _modify_color(self, name, color):
        """Pre-process color value to filter out bad colors"""
        if color or self.process_none:
            return self.modify_color(name, color)
        return color

    def modify_color(self, name, color):
        """Replace this method with your colour modifier method"""
        raise NotImplementedError("Provide a modify_color method.")

    def modify_opacity(
        self, name, opacity
    ):  # pylint: disable=no-self-use, unused-argument
        """Optional opacity modification"""
        return opacity


class TextExtension(EffectExtension):
    """
    A base effect for changing text in a document.
    """

    newline = True
    newpar = True

    def effect(self):
        nodes = self.svg.selection or {None: self.document.getroot()}
        for elem in nodes.values():
            self.process_element(elem)

    def process_element(self, node):
        """Reverse the node text"""
        if node.get("sodipodi:role") == "line":
            self.newline = True
        elif isinstance(node, (TextElement, FlowPara, FlowDiv)):
            self.newline = True
            self.newpar = True

        if node.text is not None:
            node.text = self.process_chardata(node.text)
            self.newline = False
            self.newpar = False

        for child in node:
            self.process_element(child)

        if node.tail is not None:
            node.tail = self.process_chardata(node.tail)

    def process_chardata(self, text):
        """Replaceable chardata method for processing the text"""
        return "".join(map(self.map_char, text))

    @staticmethod
    def map_char(char):
        """Replaceable map_char method for processing each letter"""
        raise NotImplementedError(
            "Please provide a process_chardata or map_char static method."
        )