several fixes and more extensions from 1.1 back again

This commit is contained in:
2022-09-02 03:15:14 +02:00
parent 1f3e8b9cb5
commit a38a160484
74 changed files with 11200 additions and 3 deletions

View File

@ -0,0 +1,20 @@
[
{
"name": "Normalize Drawing Scale",
"id": "fablabchemnitz.de.normalize_drawing_scale",
"path": "laser_check",
"dependent_extensions": null,
"original_name": "Normalize Drawing Scale",
"original_id": "fablabchemnitz.de.normalize_drawing_scale",
"license": "GNU GPL v3",
"license_url": "",
"comment": "",
"source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.2/src/branch/master/extensions/fablabchemnitz/normalize_drawing_scale",
"fork_url": null,
"documentation_url": "https://stadtfabrikanten.org/display/IFM/Normale+Drawing+Scale",
"inkscape_gallery_url": null,
"main_authors": [
"github.com/eridur-de"
]
}
]

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Normalize Drawing Scale</name>
<id>fablabchemnitz.de.normalize_drawing_scale</id>
<param name="remove_viewbox" type="bool" gui-text="Remove viewBox and set document units to px" gui-description="Removes viewBox attribute from svg:svg. Warning: this disabled the feature to change the document units to anything else than px unit.">false</param>
<param name="target_scale" type="float" min="0.001" max="9999.000" precision="3" gui-text="Target scale (%)" gui-description="Default is 100%">100.0</param>
<effect needs-live-preview="false">
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Transformations"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">normalize_drawing_scale.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,100 @@
#!/usr/bin/env python3
import inkex
from inkex import Transform
from lxml import etree
class NormalizeDrawingScale(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument('--remove_viewbox', type=inkex.Boolean, default=True)
pars.add_argument('--target_scale', type=float, default=100.0)
def effect(self):
format_units = inkex.units.parse_unit(self.svg.get('width'))[1] #get the "Format:" unit at "Display tab"
namedView = self.document.getroot().find(inkex.addNS('namedview', 'sodipodi'))
display_units = namedView.get(inkex.addNS('document-units', 'inkscape')) #means the "Display units:" at "Display tab"
docScale = self.svg.scale
inkscapeScale = self.svg.inkscape_scale #this is the "Scale:" value at "Display tab"
docWidth = self.svg.get('width')
docHeight = self.svg.get('height')
docWidth_fin = inkex.units.parse_unit(docWidth)[0]
docHeight_fin = inkex.units.parse_unit(docHeight)[0]
vxMin, vyMin, vxMax, vyMax = self.svg.get_viewbox()
vxTotal = vxMax - vxMin
targetScale = self.options.target_scale / 100
visualScaleX = self.svg.unittouu(str(vxTotal / self.svg.viewport_width) + display_units)
formatScaleX = self.svg.unittouu(str(vxTotal / self.svg.viewport_width) + format_units)
docWidth_new = docWidth_fin * visualScaleX * inkscapeScale
docHeight_new = docHeight_fin * visualScaleX * inkscapeScale
docWidth_new = docWidth_fin * targetScale / inkscapeScale
docHeight_new = docHeight_fin * targetScale / inkscapeScale
#inkex.errormsg("format_units: " + str(format_units))
#inkex.errormsg("display_units: " + str(display_units))
#inkex.errormsg("docScale: {:0.6f}".format(docScale))
#inkex.errormsg("inkscapeScale: {:0.6f}".format(inkscapeScale))
#inkex.errormsg("docWidth_fin: {:0.3f}{}".format(docWidth_fin, format_units))
#inkex.errormsg("docHeight_fin: {:0.3f}{}".format(docHeight_fin, format_units))
#inkex.errormsg("vxTotal: " + str(vxTotal))
#inkex.errormsg("docWidth_new: {:0.3f}{} ({:0.3f}px)".format(docWidth_new, format_units, self.svg.unittouu(str(docWidth_new) + format_units)))
#inkex.errormsg("docHeight_new: {:0.3f}{} ({:0.3f}px)".format(docHeight_new, format_units, self.svg.unittouu(str(docHeight_new) + format_units)))
#inkex.errormsg("targetScale: {:0.6f}".format(targetScale))
#inkex.errormsg("visualScaleX: {:0.6f}".format(visualScaleX))
#inkex.errormsg("formatScaleX: {:0.6f}".format(formatScaleX))
if inkscapeScale == targetScale: #strange rule. might break sth.
inkex.utils.debug("Nothing to do. Scale is already 100%")
return
if visualScaleX == 0.0: #seems there is no viewBox attribute, then ...
#inkex.errormsg("viewBox attribute is missing in svg:svg. Applying new one ...")
visualScaleX = 1.0 #this is the case we deal with px as display unit and we removed the viewBox
self.svg.set('viewBox', '0 0 {} {}'.format(targetScale * docWidth_fin, targetScale * docHeight_fin))
if round(visualScaleX, 5) != targetScale or self.options.remove_viewbox is True:
#set scale to 100% (we adjust viewBox)
sc = (1 / (targetScale / inkscapeScale))
if self.options.remove_viewbox is False:
viewBoxNew = '0 0 {} {}'.format(docWidth_fin / targetScale, docHeight_fin / targetScale)
#inkex.errormsg("viewBox modifying to: {}".format(viewBoxNew))
#inkex.errormsg("width modifying to: {}{}".format(docWidth_fin, format_units))
#inkex.errormsg("height modifying to: {}{}".format(docHeight_fin, format_units))
self.svg.set('viewBox', viewBoxNew)
self.svg.set('width', "{}{}".format(docWidth_fin, format_units))
self.svg.set('height', "{}{}".format(docHeight_fin, format_units))
else:
#inkex.errormsg("viewBox popping; setting back to px ...")
self.svg.pop('viewBox')
self.document.getroot().set('inkscape:document-units', 'px')
self.svg.set('width', docWidth_fin)
self.svg.set('height', docHeight_fin)
namedView.attrib[inkex.addNS('document-units', 'inkscape')] = 'px'
translation_matrix = [[sc, 0.0, 0.0], [0.0, sc, 0.0]]
#select each top layer and apply the transformation to scale
processed = []
for element in self.document.getroot().iter(tag=etree.Element):
if element != self.document.getroot():
if element.tag == inkex.addNS('g','svg'):
parent = element.getparent()
if parent.get('inkscape:groupmode') != 'layer' and element.get('inkscape:groupmode') == 'layer':
element.transform = Transform(translation_matrix) @ element.composed_transform()
processed.append(element)
#do the same for all elements which lay on first level and which are not a layer
for element in self.document.getroot().getchildren():
if isinstance(element, inkex.ShapeElement) and element not in processed:
element.transform = Transform(translation_matrix) @ element.composed_transform()
else:
inkex.utils.debug("Nothing to do. Scale is already 100%")
return
if __name__ == '__main__':
NormalizeDrawingScale().run()