-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvg_processor.py
76 lines (64 loc) · 2.65 KB
/
svg_processor.py
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
# Copyright 2015-2016 Scott Bezek and the splitflap contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import re
import xml
from xml.dom import minidom
"""
Processes SVG files generated by pcbnew to colorize and merge
"""
logger = logging.getLogger(__name__)
class SvgProcessor(object):
def __init__(self, input_file):
self.dom = minidom.parse(input_file)
self.svg_node = self.dom.documentElement
def apply_color_transform(self, transform_function):
# Set fill and stroke on all groups
for group in self.svg_node.getElementsByTagName('g'):
SvgProcessor._apply_transform(group, {
'fill': transform_function,
'stroke': transform_function,
})
def import_groups(self, from_svg_processor):
for child in from_svg_processor.svg_node.childNodes:
if child.nodeType != child.ELEMENT_NODE or child.tagName != 'g':
continue
group = child
output_node = self.dom.importNode(group, True)
self.svg_node.appendChild(output_node)
def write(self, filename):
with open(filename, 'wb') as output_file:
self.svg_node.writexml(output_file)
def wrap_with_group(self, attrs):
parent = self.svg_node
wrapper = self.dom.createElement("g")
for k,v in attrs.items():
wrapper.setAttribute(k,v)
for child in parent.getElementsByTagName('g'):
parent.removeChild(child)
wrapper.appendChild(child)
parent.appendChild(wrapper)
@staticmethod
def _apply_transform(node, values):
original_style = node.attributes['style'].value
for (k,v) in values.items():
escaped_key = re.escape(k)
m = re.search(r'\b' + escaped_key + r':(?P<value>[^;]*);', original_style)
if m:
transformed_value = v(m.group('value'))
original_style = re.sub(
r'\b' + escaped_key + r':[^;]*;',
k + ':' + transformed_value + ';',
original_style)
node.attributes['style'] = original_style