Restore unittouu / uutounit behavior of 1.1

Summary of this merge request

Follow-up to !329 (merged)

Closes #440 (closed), incl. unit test.

While !329 (merged) is technically correct - everything is consistent - it breaks pretty much every extension out there because the meaning of unittouu was changed in "non-px" documents.

This MR restores the old unittouu/uutounit including their unit tests. Extension authors are encouraged to use the functions to_dimensionless / viewport_to_unit, whose output is more expectable. This "encouragement" happens via docstring and documentation. I won't go as far as deprecating these, yet.

Technical part: Why did unittouu split into to_dimensionless and viewport_to_unit?

Most extension authors apparently use unittouu as Convert this string into a number, while getting rid of the unit. As long as I use this function everywhere, it doesn't matter to what exactly it is converted.

This is fine in theory, but the problem is that this was used inconsistently even in Inkex, leading to bugs such as #367 (closed). In a mm based document, unittouu("5") converted this value to 1.322, because 5px == 1.322mm. So when inkex performed unittouu when giving the position of a rectangle, a Path created with this coordinate - which doesn't allow for units - the path and the rectangle were not coincident.

Now there are two distinct functions.

  • One - to_dimensionless - is: Convert this string into a number, while getting rid of the unit. When I use this function on any SVG attribute and replace the original value with the result, the output doesn't change visually.
  • The other - viewport_to_unit(x, target_unit) - is: What is the length (e.g. the length of a rectangle, without a unit) of an object that has size x on the viewport? This is relevant because in Inkscape toolbars / tools, all dimensions are shown in "viewport units".
    • In other words: What is the SVG representation when I enter x in the width/height widget of the selection tool (set to the unit of x)? Consider
      <svg width="210mm" viewBox="0 0 105 147.5"><rect width="?" height="?"/></svg>,
      i.e. a "mm-based" SVG with scale=2. When I type in 200 in the rectangle tool, set to mm, the XML editor shows 100 = 100px. That's what viewport_to_unit("200mm") = 100 does.
    • Note that this is different than viewport_to_unit("200", "mm"), which would be for a rectangle with a width (in the width/height widget of the rectangle tool) of 200 (px), where I want to write the width in mm in the SVG:
      <rect width="7.00043mm" height="7.00043mm"/>

There are also the opposite functions:

  • to_dimensional simply converts a float to its equivalent with attached unit.
  • unit_to_viewport has probably not many use cases. It answers the following question: What does the the width/height widget of the selection tool (set to unit) show when I select an element with width value as defined in the SVG? Consider again
    <svg width="210mm" viewBox="0 0 105 147.5"><rect width="100" height="100"/></svg>,
    i.e. a "mm-based" SVG with scale=2. To create this rectangle, I have to type viewport_to_unit("100", unit="mm") = 200 into the rectangle tool, if the rectangle tool is set to mm.

And what do unittouu and uutounit do?

They convert to / from what inkex thinks are the document units. These are guessed from the ratio of viewport and viewbox width, and if no matching unit is found (i.e. for scale != 1), px is used. So they actually have neither of the above described behavior, but for scale =1, they are equivalent to viewport_to_unit / unit_to_viewport.

cc: @doctormo @jcwinkler

Edited by Jonathan Neuhauser

Merge request reports

Loading