summaryrefslogtreecommitdiffstats
path: root/share/extensions/inkex/extensions.py
blob: 10f30a27da1729abc43336ba4530f5139fd38359 (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
# -*- 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 .utils import errormsg, Boolean, CloningVat, PY3
from .colors import Color, ColorIdError, ColorError
from .elements import load_svg, BaseElement, ShapeElement, Group, Layer, Grid, \
                      TextElement, FlowPara, FlowDiv
from .base import InkscapeExtension, SvgThroughMixin, SvgInputMixin, SvgOutputMixin, TempDirMixin
from .transforms import Transform

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

stdout = sys.stdout
if PY3:
    unicode = str  # pylint: disable=redefined-builtin,invalid-name

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

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"""
        pass

    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 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"""
        pass

    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, (unicode, 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, unicode)):
            if not os.path.isfile(document):
                raise IOError("Can't find generated document: {}".format(document))

            if self.output_ext == 'svg':
                with open(document, 'r') as fhl:
                    document = fhl.read()
                if '<' in document:
                    document = load_svg(document)
            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 effect(self):
        layer = self.svg.get_current_layer()
        fragment = self.generate()
        if isinstance(fragment, types.GeneratorType):
            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()
                layer.append(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):
        super(TemplateExtension, self).__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)

    def get_template(self):
        """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", "0 0 {} {}".format(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,)

    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).values():
            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().values():
            self.process_element(child)

    def process_element(self, elem, gradients=None):
        """Process one of the selected elements"""
        style = elem.fallback_style(move=False)
        # Colours first
        for name in elem.style.color_props:
            value = style.get(name)
            if value is not None:
                try:
                    style[name] = self._modify_color(name, Color(value))
                except ColorIdError:
                    gradient = self.svg.getElementById(value)
                    gradients.track(gradient, elem, self._ref_cloned, style=style, name=name)
                    if gradient.href is not None:
                        gradients.track(gradient.href, elem, self._xlink_cloned, linker=gradient)
                except ColorError:
                    pass # bad color value, don't touch.
        # Then opacities (usually does nothing)
        for name in elem.style.opacity_props:
            value = style.get(name)
            if value is not None:
                style[name] = self.modify_opacity(name, value)

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

    def _xlink_cloned(self, old_id, new_id, linker):
        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):
        """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.selected.values() 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.")