renamed extension cleanup; fixed some handling of elements and styling
This commit is contained in:
parent
788e3cc4fb
commit
6a74979c53
@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
|
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
|
||||||
<name>Fix Global Stroke Width / Opacity / Style</name>
|
<name>Cleanup Styles</name>
|
||||||
<id>fablabchemnitz.de.cleanup</id>
|
<id>fablabchemnitz.de.cleanup</id>
|
||||||
<param name="stroke_width" type="float" precision="4" min="0" max="5" gui-text="Stroke width">0.1</param>
|
<param name="stroke_width" type="float" precision="4" min="0.0000" max="5.0000" gui-text="Stroke width">0.1000</param>
|
||||||
<param name="stroke_units" gui-text="Units" type="optiongroup" appearance="combo">
|
<param name="stroke_units" gui-text="Units" type="optiongroup" appearance="combo">
|
||||||
<option value="px">px</option>
|
<option value="px">px</option>
|
||||||
<option value="pt">pt</option>
|
<option value="pt">pt</option>
|
||||||
@ -15,6 +15,7 @@
|
|||||||
<param name="reset_fill_attributes" type="bool" gui-text="Reset fill style attributes" gui-description="Sets 'fill:none;' to style attribute">true</param>
|
<param name="reset_fill_attributes" type="bool" gui-text="Reset fill style attributes" gui-description="Sets 'fill:none;' to style attribute">true</param>
|
||||||
<param name="apply_hairlines" type="bool" gui-text="Add additional hairline style" gui-description="Adds 'vector-effect:non-scaling-stroke;' and '-inkscape-stroke:hairline;' Hint: stroke-width is kept in background. All hairlines still have a valued width.">true</param>
|
<param name="apply_hairlines" type="bool" gui-text="Add additional hairline style" gui-description="Adds 'vector-effect:non-scaling-stroke;' and '-inkscape-stroke:hairline;' Hint: stroke-width is kept in background. All hairlines still have a valued width.">true</param>
|
||||||
<param name="apply_black_strokes" type="bool" gui-text="Apply black strokes where strokes missing" gui-description="Adds 'stroke:#000000;' to style attribute">true</param>
|
<param name="apply_black_strokes" type="bool" gui-text="Apply black strokes where strokes missing" gui-description="Adds 'stroke:#000000;' to style attribute">true</param>
|
||||||
|
<label>This extension works on current selection or for complete document</label>
|
||||||
<effect needs-live-preview="true">
|
<effect needs-live-preview="true">
|
||||||
<object-type>all</object-type>
|
<object-type>all</object-type>
|
||||||
<effects-menu>
|
<effects-menu>
|
||||||
|
@ -48,62 +48,72 @@ class Cleanup(inkex.EffectExtension):
|
|||||||
|
|
||||||
#stroke and fill styles can be included in style attribute or they can exist separately (can occure in older SVG files). We do not parse other attributes than style
|
#stroke and fill styles can be included in style attribute or they can exist separately (can occure in older SVG files). We do not parse other attributes than style
|
||||||
def changeStyle(self, node):
|
def changeStyle(self, node):
|
||||||
if node.attrib.has_key('style'):
|
nodeDict = []
|
||||||
style = node.get('style')
|
nodeDict.append(inkex.addNS('line','svg'))
|
||||||
if style:
|
nodeDict.append(inkex.addNS('polyline','svg'))
|
||||||
#add missing style attributes if required
|
nodeDict.append(inkex.addNS('polygon','svg'))
|
||||||
if style.endswith(';') is False:
|
nodeDict.append(inkex.addNS('circle','svg'))
|
||||||
style += ';'
|
nodeDict.append(inkex.addNS('ellipse','svg'))
|
||||||
if re.search('(;|^)stroke:(.*?)(;|$)', style) is None: #if "stroke" is None, add one. We need to distinguish because there's also attribute "-inkscape-stroke" that's why we check starting with ^ or ;
|
nodeDict.append(inkex.addNS('rect','svg'))
|
||||||
style += 'stroke:none;'
|
nodeDict.append(inkex.addNS('path','svg'))
|
||||||
if "stroke-width:" not in style:
|
nodeDict.append(inkex.addNS('g','svg'))
|
||||||
style += 'stroke-width:' + str(self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)) + ';'
|
if node.tag in nodeDict:
|
||||||
if "stroke-opacity:" not in style:
|
if node.attrib.has_key('style'):
|
||||||
style += 'stroke-opacity:' + str(self.options.opacity / 100) + ';'
|
style = node.get('style')
|
||||||
|
if style:
|
||||||
|
#add missing style attributes if required
|
||||||
|
if style.endswith(';') is False:
|
||||||
|
style += ';'
|
||||||
|
if re.search('(;|^)stroke:(.*?)(;|$)', style) is None: #if "stroke" is None, add one. We need to distinguish because there's also attribute "-inkscape-stroke" that's why we check starting with ^ or ;
|
||||||
|
style += 'stroke:none;'
|
||||||
|
if "stroke-width:" not in style:
|
||||||
|
style += 'stroke-width:{:1.4f};'.format(self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units))
|
||||||
|
if "stroke-opacity:" not in style:
|
||||||
|
style += 'stroke-opacity:{:1.1f};'.format(self.options.opacity / 100)
|
||||||
|
|
||||||
if self.options.apply_hairlines is True:
|
if self.options.apply_hairlines is True:
|
||||||
if "vector-effect:non-scaling-stroke" not in style:
|
if "vector-effect:non-scaling-stroke" not in style:
|
||||||
style += 'vector-effect:non-scaling-stroke;'
|
style += 'vector-effect:non-scaling-stroke;'
|
||||||
if "-inkscape-stroke:hairline" not in style:
|
if "-inkscape-stroke:hairline" not in style:
|
||||||
style += '-inkscape-stroke:hairline;'
|
style += '-inkscape-stroke:hairline;'
|
||||||
|
|
||||||
if re.search('fill:(.*?)(;|$)', style) is None: #if "fill" is None, add one.
|
if re.search('fill:(.*?)(;|$)', style) is None: #if "fill" is None, add one.
|
||||||
style += 'fill:none;'
|
style += 'fill:none;'
|
||||||
|
|
||||||
#then parse the content and check what we need to adjust
|
#then parse the content and check what we need to adjust
|
||||||
declarations = style.split(';')
|
declarations = style.split(';')
|
||||||
for i, decl in enumerate(declarations):
|
for i, decl in enumerate(declarations):
|
||||||
parts = decl.split(':', 2)
|
parts = decl.split(':', 2)
|
||||||
if len(parts) == 2:
|
if len(parts) == 2:
|
||||||
(prop, val) = parts
|
(prop, val) = parts
|
||||||
prop = prop.strip().lower()
|
prop = prop.strip().lower()
|
||||||
if prop == 'stroke-width':
|
if prop == 'stroke-width':
|
||||||
new_val = self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)
|
new_val = self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)
|
||||||
declarations[i] = prop + ':' + str(new_val)
|
declarations[i] = prop + ':{:1.4f}'.format(new_val)
|
||||||
if prop == 'stroke-opacity':
|
if prop == 'stroke-opacity':
|
||||||
new_val = str(self.options.opacity / 100)
|
new_val = self.options.opacity / 100
|
||||||
declarations[i] = prop + ':' + new_val
|
declarations[i] = prop + ':{:1.1f}'.format(new_val)
|
||||||
if self.options.reset_style_attributes is True:
|
if self.options.reset_style_attributes is True:
|
||||||
if prop == 'stroke-dasharray':
|
if prop == 'stroke-dasharray':
|
||||||
declarations[i] = ''
|
declarations[i] = ''
|
||||||
if prop == 'stroke-dashoffset':
|
if prop == 'stroke-dashoffset':
|
||||||
declarations[i] = ''
|
declarations[i] = ''
|
||||||
if prop == 'stroke-linejoin':
|
if prop == 'stroke-linejoin':
|
||||||
declarations[i] = ''
|
declarations[i] = ''
|
||||||
if prop == 'stroke-linecap':
|
if prop == 'stroke-linecap':
|
||||||
declarations[i] = ''
|
declarations[i] = ''
|
||||||
if prop == 'stroke-miterlimit':
|
if prop == 'stroke-miterlimit':
|
||||||
declarations[i] = ''
|
declarations[i] = ''
|
||||||
if self.options.apply_black_strokes is True:
|
if self.options.apply_black_strokes is True:
|
||||||
if prop == 'stroke':
|
if prop == 'stroke':
|
||||||
if val == 'none':
|
if val == 'none':
|
||||||
new_val = '#000000'
|
new_val = '#000000'
|
||||||
declarations[i] = prop + ':' + new_val
|
declarations[i] = prop + ':' + new_val
|
||||||
if self.options.reset_fill_attributes is True:
|
if self.options.reset_fill_attributes is True:
|
||||||
if prop == 'fill':
|
if prop == 'fill':
|
||||||
new_val = 'none'
|
new_val = 'none'
|
||||||
declarations[i] = prop + ':' + new_val
|
declarations[i] = prop + ':' + new_val
|
||||||
node.set('style', ';'.join(declarations))
|
node.set('style', ';'.join(declarations))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
Cleanup().run()
|
Cleanup().run()
|
Reference in New Issue
Block a user