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
|
#!/usr/bin/env python
'''
image_attributes.py - adjust image attributes which don't have global
GUI options yet
Tool for Inkscape 0.91 to adjust rendering of drawings with linked
or embedded bitmap images created with older versions of Inkscape
or third-party applications.
Copyright (C) 2015, ~suv <suv-sf@users.sf.net>
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.
'''
# local library
import inkex
import simplestyle
class SetAttrImage(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
# main options
self.OptionParser.add_option("--fix_scaling",
action="store", type="inkbool",
dest="fix_scaling", default=True,
help="")
self.OptionParser.add_option("--fix_rendering",
action="store", type="inkbool",
dest="fix_rendering", default=False,
help="")
self.OptionParser.add_option("--aspect_ratio",
action="store", type="string",
dest="aspect_ratio", default="none",
help="Value for attribute 'preserveAspectRatio'")
self.OptionParser.add_option("--aspect_clip",
action="store", type="string",
dest="aspect_clip", default="unset",
help="optional 'meetOrSlice' value")
self.OptionParser.add_option("--aspect_ratio_scope",
action="store", type="string",
dest="aspect_ratio_scope", default="selected_only",
help="scope within which to edit 'preserveAspectRatio' attribute")
self.OptionParser.add_option("--image_rendering",
action="store", type="string",
dest="image_rendering", default="unset",
help="Value for attribute 'image-rendering'")
self.OptionParser.add_option("--image_rendering_scope",
action="store", type="string",
dest="image_rendering_scope", default="selected_only",
help="scope within which to edit 'image-rendering' attribute")
# tabs
self.OptionParser.add_option("--tab_main",
action="store", type="string",
dest="tab_main")
# core method
def change_attribute(self, node, attribute):
for key, value in attribute.items():
if key == 'preserveAspectRatio':
# set presentation attribute
if value != "unset":
node.set(key, str(value))
else:
if node.get(key):
del node.attrib[key]
elif key == 'image-rendering':
node_style = simplestyle.parseStyle(node.get('style'))
if key not in node_style:
# set presentation attribute
if value != "unset":
node.set(key, str(value))
else:
if node.get(key):
del node.attrib[key]
else:
# set style property
if value != "unset":
node_style[key] = str(value)
else:
del node_style[key]
node.set('style', simplestyle.formatStyle(node_style))
else:
pass
def change_all_images(self, node, attribute):
path = 'descendant-or-self::svg:image'
for img in node.xpath(path, namespaces=inkex.NSS):
self.change_attribute(img, attribute)
# methods called via dispatcher
def change_selected_only(self, selected, attribute):
if selected:
for node_id, node in selected.iteritems():
if node.tag == inkex.addNS('image', 'svg'):
self.change_attribute(node, attribute)
def change_in_selection(self, selected, attribute):
if selected:
for node_id, node in selected.iteritems():
self.change_all_images(node, attribute)
def change_in_document(self, selected, attribute):
self.change_all_images(self.document.getroot(), attribute)
def change_on_parent_group(self, selected, attribute):
if selected:
for node_id, node in selected.iteritems():
self.change_attribute(node.getparent(), attribute)
def change_on_root_only(self, selected, attribute):
self.change_attribute(self.document.getroot(), attribute)
# main
def effect(self):
attr_val = []
attr_dict = {}
cmd_scope = None
if self.options.tab_main == '"tab_basic"':
cmd_scope = "in_document"
attr_dict['preserveAspectRatio'] = ("none" if self.options.fix_scaling else "unset")
attr_dict['image-rendering'] = ("optimizeSpeed" if self.options.fix_rendering else "unset")
elif self.options.tab_main == '"tab_aspectRatio"':
attr_val = [self.options.aspect_ratio]
if self.options.aspect_clip != "unset":
attr_val.append(self.options.aspect_clip)
attr_dict['preserveAspectRatio'] = ' '.join(attr_val)
cmd_scope = self.options.aspect_ratio_scope
elif self.options.tab_main == '"tab_image_rendering"':
attr_dict['image-rendering'] = self.options.image_rendering
cmd_scope = self.options.image_rendering_scope
else: # help tab
pass
# dispatcher
if cmd_scope is not None:
try:
change_cmd = getattr(self, 'change_{0}'.format(cmd_scope))
change_cmd(self.selected, attr_dict)
except AttributeError:
inkex.errormsg('Scope "{0}" not supported'.format(cmd_scope))
if __name__ == '__main__':
e = SetAttrImage()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99
|