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"?>
|
||||
<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>
|
||||
<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">
|
||||
<option value="px">px</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="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>
|
||||
<label>This extension works on current selection or for complete document</label>
|
||||
<effect needs-live-preview="true">
|
||||
<object-type>all</object-type>
|
||||
<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
|
||||
def changeStyle(self, node):
|
||||
if node.attrib.has_key('style'):
|
||||
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:' + str(self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)) + ';'
|
||||
if "stroke-opacity:" not in style:
|
||||
style += 'stroke-opacity:' + str(self.options.opacity / 100) + ';'
|
||||
nodeDict = []
|
||||
nodeDict.append(inkex.addNS('line','svg'))
|
||||
nodeDict.append(inkex.addNS('polyline','svg'))
|
||||
nodeDict.append(inkex.addNS('polygon','svg'))
|
||||
nodeDict.append(inkex.addNS('circle','svg'))
|
||||
nodeDict.append(inkex.addNS('ellipse','svg'))
|
||||
nodeDict.append(inkex.addNS('rect','svg'))
|
||||
nodeDict.append(inkex.addNS('path','svg'))
|
||||
nodeDict.append(inkex.addNS('g','svg'))
|
||||
if node.tag in nodeDict:
|
||||
if node.attrib.has_key('style'):
|
||||
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 "vector-effect:non-scaling-stroke" not in style:
|
||||
style += 'vector-effect:non-scaling-stroke;'
|
||||
if "-inkscape-stroke:hairline" not in style:
|
||||
style += '-inkscape-stroke:hairline;'
|
||||
|
||||
if re.search('fill:(.*?)(;|$)', style) is None: #if "fill" is None, add one.
|
||||
style += 'fill:none;'
|
||||
|
||||
#then parse the content and check what we need to adjust
|
||||
declarations = style.split(';')
|
||||
for i, decl in enumerate(declarations):
|
||||
parts = decl.split(':', 2)
|
||||
if len(parts) == 2:
|
||||
(prop, val) = parts
|
||||
prop = prop.strip().lower()
|
||||
if prop == 'stroke-width':
|
||||
new_val = self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)
|
||||
declarations[i] = prop + ':' + str(new_val)
|
||||
if prop == 'stroke-opacity':
|
||||
new_val = str(self.options.opacity / 100)
|
||||
declarations[i] = prop + ':' + new_val
|
||||
if self.options.reset_style_attributes is True:
|
||||
if prop == 'stroke-dasharray':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-dashoffset':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-linejoin':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-linecap':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-miterlimit':
|
||||
declarations[i] = ''
|
||||
if self.options.apply_black_strokes is True:
|
||||
if prop == 'stroke':
|
||||
if val == 'none':
|
||||
new_val = '#000000'
|
||||
declarations[i] = prop + ':' + new_val
|
||||
if self.options.reset_fill_attributes is True:
|
||||
if prop == 'fill':
|
||||
new_val = 'none'
|
||||
declarations[i] = prop + ':' + new_val
|
||||
node.set('style', ';'.join(declarations))
|
||||
if self.options.apply_hairlines is True:
|
||||
if "vector-effect:non-scaling-stroke" not in style:
|
||||
style += 'vector-effect:non-scaling-stroke;'
|
||||
if "-inkscape-stroke:hairline" not in style:
|
||||
style += '-inkscape-stroke:hairline;'
|
||||
|
||||
if re.search('fill:(.*?)(;|$)', style) is None: #if "fill" is None, add one.
|
||||
style += 'fill:none;'
|
||||
|
||||
#then parse the content and check what we need to adjust
|
||||
declarations = style.split(';')
|
||||
for i, decl in enumerate(declarations):
|
||||
parts = decl.split(':', 2)
|
||||
if len(parts) == 2:
|
||||
(prop, val) = parts
|
||||
prop = prop.strip().lower()
|
||||
if prop == 'stroke-width':
|
||||
new_val = self.svg.unittouu(str(self.options.stroke_width) + self.options.stroke_units)
|
||||
declarations[i] = prop + ':{:1.4f}'.format(new_val)
|
||||
if prop == 'stroke-opacity':
|
||||
new_val = self.options.opacity / 100
|
||||
declarations[i] = prop + ':{:1.1f}'.format(new_val)
|
||||
if self.options.reset_style_attributes is True:
|
||||
if prop == 'stroke-dasharray':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-dashoffset':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-linejoin':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-linecap':
|
||||
declarations[i] = ''
|
||||
if prop == 'stroke-miterlimit':
|
||||
declarations[i] = ''
|
||||
if self.options.apply_black_strokes is True:
|
||||
if prop == 'stroke':
|
||||
if val == 'none':
|
||||
new_val = '#000000'
|
||||
declarations[i] = prop + ':' + new_val
|
||||
if self.options.reset_fill_attributes is True:
|
||||
if prop == 'fill':
|
||||
new_val = 'none'
|
||||
declarations[i] = prop + ':' + new_val
|
||||
node.set('style', ';'.join(declarations))
|
||||
|
||||
if __name__ == '__main__':
|
||||
Cleanup().run()
|
Reference in New Issue
Block a user