From d9672de8a1e47006a98e285fba506e158e5f4e99 Mon Sep 17 00:00:00 2001 From: Jonathan Neuhauser Date: Thu, 10 Feb 2022 22:09:24 +0100 Subject: [PATCH] fix pixelsnap in mm based documents --- pixelsnap.inx | 4 ++ pixelsnap.py | 24 ++++--- ...ap__--id__p1__--id__r3__--snap_to__bl.out} | 0 ...ath1434__--id__path1434__--snap_to__bl.out | 11 +++ ...ath1434__--id__path1434__--snap_to__tl.out | 11 +++ tests/data/svg/pixelsnap_simple.svg | 69 +++++++++++++++++++ tests/test_pixelsnap.py | 28 +++++++- 7 files changed, 138 insertions(+), 9 deletions(-) rename tests/data/refs/{pixelsnap__--id__p1__--id__r3.out => pixelsnap__--id__p1__--id__r3__--snap_to__bl.out} (100%) create mode 100644 tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__bl.out create mode 100644 tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__tl.out create mode 100644 tests/data/svg/pixelsnap_simple.svg diff --git a/pixelsnap.inx b/pixelsnap.inx index bac2c3a2..a9c3203c 100644 --- a/pixelsnap.inx +++ b/pixelsnap.inx @@ -8,6 +8,10 @@ true 0.5 + + + + all diff --git a/pixelsnap.py b/pixelsnap.py index ef0ef24b..471da70a 100755 --- a/pixelsnap.py +++ b/pixelsnap.py @@ -106,6 +106,8 @@ def transform_dimensions(transform, width=None, height=None, inverse=False): class PixelSnap(inkex.EffectExtension): + """Snap objects to pixels""" + def add_arguments(self, pars): """Add inx options""" pars.add_argument( @@ -131,6 +133,13 @@ class PixelSnap(inkex.EffectExtension): default=0.5, help="Maximum slope to consider straight (%)", ) + pars.add_argument( + "-s", + "--snap_to", + default="tl", + choices=["tl", "bl"], + help="Origin of the coordinate system", + ) def vertical(self, pt1, pt2): hlen = abs(pt1[0] - pt2[0]) @@ -176,7 +185,7 @@ class PixelSnap(inkex.EffectExtension): stroke_width = 0 if stroke and setval is None: - stroke_width = self.svg.unittouu(style("stroke-width").strip()) + stroke_width = self.svg.to_dimensionless(style("stroke-width").strip()) if setval: style["stroke-width"] = setval @@ -419,7 +428,6 @@ class PixelSnap(inkex.EffectExtension): width, height = transform_dimensions(transform, width, height, inverse=True) x, y = transform_point(transform, [x, y], inverse=True) - y += self.document_offset / transform.d # Position the elem at the newly calculate values @@ -475,12 +483,12 @@ class PixelSnap(inkex.EffectExtension): self.snap_image(elem, parent_transform) def effect(self): - svg = self.document.getroot() - - self.document_offset = ( - self.svg.unittouu(svg.attrib["height"]) % 1 - ) # although SVG units are absolute, the elements are positioned relative to the top of the page, rather than zero - + if self.options.snap_to == "bl": + self.document_offset = ( + self.svg.to_dimensionless(self.svg.get_viewbox()[3]) % 1 + ) + else: + self.document_offset = 0 for id, elem in self.svg.selection.items(): try: self.pixel_snap(elem) diff --git a/tests/data/refs/pixelsnap__--id__p1__--id__r3.out b/tests/data/refs/pixelsnap__--id__p1__--id__r3__--snap_to__bl.out similarity index 100% rename from tests/data/refs/pixelsnap__--id__p1__--id__r3.out rename to tests/data/refs/pixelsnap__--id__p1__--id__r3__--snap_to__bl.out diff --git a/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__bl.out b/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__bl.out new file mode 100644 index 00000000..5782872b --- /dev/null +++ b/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__bl.out @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__tl.out b/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__tl.out new file mode 100644 index 00000000..5a3ce2b9 --- /dev/null +++ b/tests/data/refs/pixelsnap__--id__rect1144__--id__path1302__--id__path1430__--id__path1434__--id__path1434__--snap_to__tl.out @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/data/svg/pixelsnap_simple.svg b/tests/data/svg/pixelsnap_simple.svg new file mode 100644 index 00000000..a25eaa78 --- /dev/null +++ b/tests/data/svg/pixelsnap_simple.svg @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + diff --git a/tests/test_pixelsnap.py b/tests/test_pixelsnap.py index ef6feceb..074abe0d 100644 --- a/tests/test_pixelsnap.py +++ b/tests/test_pixelsnap.py @@ -7,4 +7,30 @@ from inkex.tester.filters import CompareOrderIndependentStyle class TestPixelSnapEffectBasic(ComparisonMixin, TestCase): effect_class = PixelSnap compare_filters = [CompareOrderIndependentStyle()] - comparisons = [("--id=p1", "--id=r3")] + comparisons = [("--id=p1", "--id=r3", "--snap_to=bl")] + + +class TestPixelSnapEffectMM(ComparisonMixin, TestCase): + """Test pixel snap in mm based documents""" + + effect_class = PixelSnap + compare_file = "svg/pixelsnap_simple.svg" + compare_filters = [CompareOrderIndependentStyle()] + comparisons = [ + ( + "--id=rect1144", + "--id=path1302", + "--id=path1430", + "--id=path1434", + "--id=path1434", + "--snap_to=tl", + ), + ( + "--id=rect1144", + "--id=path1302", + "--id=path1430", + "--id=path1434", + "--id=path1434", + "--snap_to=bl", + ), + ] -- GitLab