fsmMLK / inkscapeDimensions

Inkscape extension to assist creating dimension annotations.
GNU General Public License v3.0
64 stars 10 forks source link

Incorrect Dimensions with Non-Pixel User Unit #6

Open jphilbert opened 4 years ago

jphilbert commented 4 years ago

Just installed this extension on 0.92.3 and was getting some incorrect labels. I often make documents using inches with a scale of 1:1. With a test document w/ display units in inches and scale of 1, I made a reference 4in line. This yielded dimension of 0.0416666682292. Oddly this equals 4in/96px so I gathered the extension thought the document was still using pixel units.

I traced it down to this line of code I think: https://github.com/fsmMLK/inkscapeDimensions/blob/897a76a7b657af28d888bc673301b68af11ceeb2/dimensions.py#L659

I also noticed that if the document scale is changed it will scale the labeled dimension. For example changing the scale from 1 to 2, a 4in line becomes 0.083.

FYI, this seems to fix things for me:

try:
    elem = self.getElemFromXpath('/svg:svg/sodipodi:namedview')
    doc_scale = self.getElemAtrib(elem, 'scale-x')
    doc_scale = float(doc_scale)
except:
   doc_scale = 1

value = self.unit2unit(value, self.documentUnit, unit) / doc_scale
fsmMLK commented 4 years ago

I don't see how to include a scale-x attribute to documents in 0.92.3. I tried playing with scaling and display unit. It did not generated a scale-x attribute to the svg. Can you explain how you do this? ps: inkscape's units mumbo-jumbo is a pain...

jphilbert commented 4 years ago

Actually I was a bit too quick in suggesting the scale part of the solution. However I got scale-x in my test document set to 2 I could not reproduce it or change it in Inkscape. I did seem to be able to get the scale correctly by calculating the ratio of the document width and the view box width via,

        try:
            elem = self.getElemFromXpath('/svg:svg')
            width = self.getElemAtrib(elem, 'width')
            width = width.replace(self.documentUnit, '')
            width = float(width)
            elem = self.getElemFromXpath('/svg:svg')
            view_width = self.getElemAtrib(elem, 'viewBox')
            view_width = str.split(view_width, ' ')[2]
            view_width = float(view_width)
            self.documentScale = view_width / width
        except:
            self.documentScale = 1

I did this in the effect method as I also found that the font size needed to be scale too.

Honestly, I think the simplest solution is to just to be sure the scale in the document properties is set to 1. Then only line 659 needs value = self.unit2unit(value, self.documentUnit, unit) (if of course you can replicate my initial issue).

fsmMLK commented 4 years ago

It is a bit unusual to change x-scale of the document since it changes the relation between user unit and (real world) unit, which should be "standard" (https://www.w3.org/TR/css3-values/#absolute-lengths).

Is there a reason for you to change the scale in that way?

jphilbert commented 4 years ago

I agree and would have never noticed it until now. For some reason when I create a new SVG, it defaults to mm but its easier for me to work in inches. When I change this, the x-scale seems to change. I never noticed any side effects of this until now. Now that I know it can cause scaling issues here I'll just be sure to reset this property back to 1.

I still think there still may be an issue in using self.userUnit2unit(value, unit) which seems to work only when the document dimensions are 'px'. self.unit2unit(value, self.documentUnit, unit) I think would be more appropriate.

fsmMLK commented 4 years ago

This 'scale X' thing relates real world units (cm, in, mm, etc) to inkscape's internal unit (px). Its name is quite misleading. It has nothing to do with usual scales we encounter in architecture, engineering, etc (1:2, 1:5, 4:1) etc.

The easiest way to revert its value back to default is: 1-) change display unit to px 2-) set 'scale X' to 1.0 3-) change display unit back to inch. You will notice 'scale X' will change automatically to 96. Leave the scale as it is.

I will investigate what is going on with the self.userUnit2unit <-> self.unit2unit.

davidhealey commented 4 years ago

I've found the same issue. My drawing is in mm but the displayed dimension is incorrect, unless I set it to use pixels.

image

spalatofhi commented 4 years ago

I observe the exact same behavior as @davidhealey. I haven't played with any document parameters: units are mm, scalings are 1, etc.

carlos-jenkins commented 3 years ago

I just experienced the same issue. Ubuntu 20.04 with Inkscape 1.0.1 (2020-10-17).

Ugly workaround:

In the Dimensions panel, select Pixel in the unit, and uncheck the Add unit symbol.

In that way the value displayed is the expected value (tested with mm, not sure others). Just add the mm manually if it is really required.

Way to reproduce the issue:

Configure your document to use mm as Display units:

image

Create an horizontal line of 100mm. Open the Dimensions extension dialog and select Add unit symbol and Unit: mm.

image

carlos-jenkins commented 3 years ago

This 'scale X' thing relates real world units (cm, in, mm, etc) to inkscape's internal unit (px). Its name is quite misleading. It has nothing to do with usual scales we encounter in architecture, engineering, etc (1:2, 1:5, 4:1) etc.

The easiest way to revert its value back to default is: 1-) change display unit to px 2-) set 'scale X' to 1.0 3-) change display unit back to inch. You will notice 'scale X' will change automatically to 96. Leave the scale as it is.

I will investigate what is going on with the self.userUnit2unit <-> self.unit2unit.

I can also confirm that following those instructions make it work. I don't know what Inkscape is doing under the hood with those Scale x. My defaults when a document is opened is Display units: mm and Scale x: 1.0.

image

fsmMLK commented 3 years ago

I will investigate one way to 'solve' this. Scale-x is a pain and different users prefer different behaviors. Please do not expect a big change soon. =(