summaryrefslogtreecommitdiff
path: root/lib/extensions/cleanup.py
blob: e06b4bea5137e36b00da30f8468ea3428dae7776 (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
import sys

from inkex import NSS

from ..elements import Fill, Stroke
from ..i18n import _
from .base import InkstitchExtension


class Cleanup(InkstitchExtension):
    def __init__(self, *args, **kwargs):
        InkstitchExtension.__init__(self, *args, **kwargs)
        self.OptionParser.add_option("-f", "--rm_fill", dest="rm_fill", type="inkbool", default=True)
        self.OptionParser.add_option("-s", "--rm_stroke", dest="rm_stroke", type="inkbool", default=True)
        self.OptionParser.add_option("-a", "--fill_threshold", dest="fill_threshold", type="int", default=20)
        self.OptionParser.add_option("-l", "--stroke_threshold", dest="stroke_threshold", type="int", default=5)

    def effect(self):
        self.rm_fill = self.options.rm_fill
        self.rm_stroke = self.options.rm_stroke
        self.fill_threshold = self.options.fill_threshold
        self.stroke_threshold = self.options.stroke_threshold

        # Remove selection, we want every element in the document
        self.selected = {}

        count = 0
        svg = self.document.getroot()
        empty_d_objects = svg.xpath(".//svg:path[@d='' or not(@d)]", namespaces=NSS)
        for empty in empty_d_objects:
            empty.getparent().remove(empty)
            count += 1

        if not self.get_elements():
            print >> sys.stderr, _("%s elements removed" % count)
            return

        for element in self.elements:
            if (isinstance(element, Fill) and self.rm_fill and element.shape.area < self.fill_threshold):
                element.node.getparent().remove(element.node)
                count += 1
            if (isinstance(element, Stroke) and self.rm_stroke and
               element.shape.length < self.stroke_threshold and element.node.getparent() is not None):
                element.node.getparent().remove(element.node)
                count += 1

        print >> sys.stderr, _("%s elements removed" % count)