2020-07-30 01:16:18 +02:00
#!/usr/bin/env python3
'''
Copyright ( C ) 2013 Matthew Dockrey ( gfish @ cyphertext . 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 . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2021-04-12 19:14:57 +02:00
Based on
- coloreffect . py by Jos Hirth and Aaron C . Spike
2021-04-19 20:54:38 +02:00
- cleanup . py ( https : / / github . com / attoparsec / inkscape - extensions ) by attoparsec
2021-04-12 19:14:57 +02:00
Author : Mario Voigt / FabLab Chemnitz
Mail : mario . voigt @stadtfabrikanten.org
Last Patch : 12.04 .2021
License : GNU GPL v3
Notes :
- This extension does not check if attributes contain duplicates properties like " opacity:1;fill:#393834;fill-opacity:1;opacity:1;fill:#393834;fill-opacity:1 " . We assume the SVG syntax is correct
2020-07-30 01:16:18 +02:00
'''
import inkex
2021-04-06 15:30:14 +02:00
import re
2020-07-30 01:16:18 +02:00
2021-04-04 01:51:59 +02:00
class Cleanup ( inkex . EffectExtension ) :
2021-04-12 19:14:57 +02:00
groups = [ ]
2021-04-15 17:03:47 +02:00
def add_arguments ( self , pars ) :
2021-04-19 20:54:38 +02:00
pars . add_argument ( " --tab " )
2021-04-15 17:03:47 +02:00
pars . add_argument ( " --mode " , default = " Lines " , help = " Join paths with lines or polygons " )
pars . add_argument ( " --dedicated_style_attributes " , default = " ignore " , help = " Handling of dedicated style attributes " )
pars . add_argument ( " --stroke_width_override " , type = inkex . Boolean , default = False , help = " Override stroke width " )
pars . add_argument ( " --stroke_width " , type = float , default = 0.100 , help = " Stroke width " )
pars . add_argument ( " --stroke_width_units " , default = " mm " , help = " Stroke width unit " )
pars . add_argument ( " --stroke_opacity_override " , type = inkex . Boolean , default = False , help = " Override stroke opacity " )
pars . add_argument ( " --stroke_opacity " , type = float , default = " 100.0 " , help = " Stroke opacity ( % ) " )
pars . add_argument ( " --reset_stroke_attributes " , type = inkex . Boolean , help = " Remove stroke style attributes ' stroke-dasharray ' , ' stroke-dashoffset ' , ' stroke-linejoin ' , ' stroke-linecap ' , ' stroke-miterlimit ' " )
pars . add_argument ( " --reset_fill_attributes " , type = inkex . Boolean , help = " Sets ' fill:none;fill-opacity:1; ' to style attribute " )
pars . add_argument ( " --apply_hairlines " , type = inkex . Boolean , help = " Adds ' vector-effect:non-scaling-stroke; ' and ' -inkscape-stroke:hairline; ' Hint: stroke-width is kept in background. All hairlines still have a valued width. " )
pars . add_argument ( " --apply_black_strokes " , type = inkex . Boolean , help = " Adds ' stroke:#000000; ' to style attribute " )
pars . add_argument ( " --remove_group_styles " , type = inkex . Boolean , help = " Remove styles from groups " )
2021-04-12 19:14:57 +02:00
2020-07-30 01:16:18 +02:00
def effect ( self ) :
2021-04-06 15:30:14 +02:00
if len ( self . svg . selected ) == 0 :
2020-07-30 01:16:18 +02:00
self . getAttribs ( self . document . getroot ( ) )
else :
2021-04-19 22:07:01 +02:00
for element in self . svg . selected . values ( ) :
self . getAttribs ( element )
2021-04-12 19:14:57 +02:00
#finally remove the styles from collected groups (if enabled)
if self . options . remove_group_styles is True :
for group in self . groups :
if group . attrib . has_key ( ' style ' ) is True :
group . attrib . pop ( ' style ' )
2020-07-30 01:16:18 +02:00
2021-04-06 15:30:14 +02:00
def getAttribs ( self , node ) :
2020-07-30 01:16:18 +02:00
self . changeStyle ( node )
for child in node :
self . getAttribs ( child )
2021-04-06 15:30:14 +02:00
#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 ) :
2021-04-12 19:14:57 +02:00
#we check/modify the style of all shapes (not groups)
if isinstance ( node , inkex . ShapeElement ) and not isinstance ( node , inkex . Group ) :
# the final styles applied to this element (with influence from top level elements like groups)
composed_style = node . composed_style ( )
composedStyleAttributes = str ( composed_style ) . split ( ' ; ' ) #array
composedStyleAttributesDict = { }
if len ( composed_style ) > 0 : #Style "composed_style" might contain just empty '' string which will lead to failing dict update
for composedStyleAttribute in composedStyleAttributes :
composedStyleAttributesDict . update ( { ' {} ' . format ( composedStyleAttribute . split ( ' : ' ) [ 0 ] ) : composedStyleAttribute . split ( ' : ' ) [ 1 ] } )
#three options to handle dedicated attributes (attributes not in the "style" attribute, but separate):
# - just delete all dedicated properties
# - merge dedicated properties, and prefer them over those from composed style
# - merge dedicated properties, but prefer properties from composed style
dedicatedStyleAttributesDict = { }
popDict = [ ]
popDict . extend ( [ ' stroke ' , ' stroke-opacity ' , ' stroke-width ' , ' stroke-dasharray ' , ' stroke-dashoffset ' , ' stroke-linecap ' , ' stroke-linejoin ' , ' stroke-miterlimit ' , ' fill ' , ' fill-opacity ' ] )
for popItem in popDict :
if node . attrib . has_key ( str ( popItem ) ) :
dedicatedStyleAttributesDict . update ( { ' {} ' . format ( popItem ) : node . get ( popItem ) } )
node . attrib . pop ( popItem )
#inkex.utils.debug("composedStyleAttributesDict = " + str(composedStyleAttributesDict))
#inkex.utils.debug("dedicatedStyleAttributesDict = " + str(dedicatedStyleAttributesDict))
if self . options . dedicated_style_attributes == ' prefer_dedicated ' :
composedStyleAttributesDict . update ( dedicatedStyleAttributesDict )
node . set ( ' style ' , composedStyleAttributesDict )
elif self . options . dedicated_style_attributes == ' prefer_composed ' :
dedicatedStyleAttributesDict . update ( composedStyleAttributesDict )
node . set ( ' style ' , dedicatedStyleAttributesDict )
elif self . options . dedicated_style_attributes == ' ignore ' :
pass
# now parse the style with possibly merged dedicated attributes modded style attribute (dedicatedStyleAttributes)
if node . attrib . has_key ( ' style ' ) is False :
node . set ( ' style ' , ' stroke:#000000; ' ) #we add basic stroke color black. We cannot set to empty value or just ";" because it will not update properly
style = node . get ( ' 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 self . options . stroke_width_override is True and " stroke-width: " not in style :
style + = ' stroke-width: {:1.4f} ; ' . format ( self . svg . unittouu ( str ( self . options . stroke_width ) + self . options . stroke_width_units ) )
if self . options . stroke_opacity_override is True and " stroke-opacity: " not in style :
style + = ' stroke-opacity: {:1.1f} ; ' . format ( self . options . stroke_opacity / 100 )
2021-04-06 15:30:14 +02:00
2021-04-12 19:14:57 +02:00
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 ' and self . options . stroke_width_override is True :
new_val = self . svg . unittouu ( str ( self . options . stroke_width ) + self . options . stroke_width_units )
declarations [ i ] = prop + ' : {:1.4f} ' . format ( new_val )
if prop == ' stroke-opacity ' and self . options . stroke_opacity_override is True :
new_val = self . options . stroke_opacity / 100
declarations [ i ] = prop + ' : {:1.1f} ' . format ( new_val )
if self . options . reset_stroke_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
if prop == ' fill-opacity ' :
new_val = ' 1 '
declarations [ i ] = prop + ' : ' + new_val
node . set ( ' style ' , ' ; ' . join ( declarations ) )
# if element is group we add it to collection to remove it's style after parsing all selected items
elif isinstance ( node , inkex . ShapeElement ) and isinstance ( node , inkex . Group ) :
self . groups . append ( node )
2020-07-30 01:16:18 +02:00
2020-08-31 21:25:41 +02:00
if __name__ == ' __main__ ' :
Cleanup ( ) . run ( )