From 6a74979c53a742354b12168c1a8c53f88c3377ac Mon Sep 17 00:00:00 2001 From: leyghisbb Date: Wed, 7 Apr 2021 00:29:14 +0200 Subject: [PATCH] renamed extension cleanup; fixed some handling of elements and styling --- extensions/fablabchemnitz/cleanup.inx | 5 +- extensions/fablabchemnitz/cleanup.py | 120 ++++++++++++++------------ 2 files changed, 68 insertions(+), 57 deletions(-) diff --git a/extensions/fablabchemnitz/cleanup.inx b/extensions/fablabchemnitz/cleanup.inx index b6001cdc..fc0c9065 100644 --- a/extensions/fablabchemnitz/cleanup.inx +++ b/extensions/fablabchemnitz/cleanup.inx @@ -1,8 +1,8 @@ - Fix Global Stroke Width / Opacity / Style + Cleanup Styles fablabchemnitz.de.cleanup - 0.1 + 0.1000 @@ -15,6 +15,7 @@ true true true + all diff --git a/extensions/fablabchemnitz/cleanup.py b/extensions/fablabchemnitz/cleanup.py index b4c165a3..b1b56375 100644 --- a/extensions/fablabchemnitz/cleanup.py +++ b/extensions/fablabchemnitz/cleanup.py @@ -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() \ No newline at end of file