Parsing Units and Matrix Scaling Logic Updates #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bugs tackled:
Transform_styles parsing issue
Within svg2mod > svg > svg.py | transform_styles function
Problem: Regex parsing doesn't capture full values
Solution: Expand the regex pattern using "+" to capture more than one value
re.searchjust matches the first character in the string and the\Dwill only catch the first non-digit.Something like
1pxwill returnp, which won't match the appropriate key inunit_convert.I suspect this will resolve the posted Issue:
Path width export doesn't match Inkscape value · Issue #61 · svg2mod/svg2mod
The user reports an issue with scaling and in the post screenshot uses 'mm' (millimeters), which due to the
re.search(r"\D"would not have gotten matched and scaled properly.Additionally, the same problem occurs a few lines later here:
re.search(r"\d", self.style[style]).group()to grab the digits in front of the unit would only grab the first value. If it was a two digit number,"22px"for example would only return2as the value.So this calculation would subsequently be wrong due to the first line.
The solution was just to add the "+" metacharacter for the regex to match more characters:
\D+and\d+respectively.Matrix xscale() and yscale() issues
Within svg2mod > svg > svg.py | xscale and yscale functions within Matrix class
Problem: ZeroDivisionError when denominator in formula is zero
Solution: Add an
ifcheck to return the default value in this caseThe issue is immediately apparent: the denominator in the calculation can throw a
ZeroDivisionErrorif not properly handled.I've encountered the exact issue when attempting to add a path to my SVG that has been mirrored along the X-axis and Y-axis, thus inputting a matrix of [0.0, -1.0, -1.0, 0.0, 0.0, 0.0]

The solution was to simply add a check for this case. For example:

This is the formula for the logic and you can see that for the default case (where the identity matrix is used
vect = [1, 0, 0, 1, 0, 0]) we pass a value of one in and get the positive value ofc.For more context to describe the problem,
The Inkscape SVG code is:
matrix(a, b, c, d, e, f)is a way to represent a 2D transformation in SVG. Each parameter in the matrix plays a role in transforming the element.This URL pointing to the W3 documentation isn't very helpful here.
The SVG documentation and definition for transform points to the CSS standard of the same thing:
Coordinate Systems, Transformations and Units — SVG 2
points to
CSS Transforms Module Level 1
These are pretty technical documents but do not concisely point out the implementation.
matrix() - CSS: Cascading Style Sheets | MDN
MDN has a clearer representation.
I suggest updating the docstring here to replace the last line to say:
SVGs implement the same transform that CSS does
see https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix#values