diff --git a/pixelsnap.inx b/pixelsnap.inx index bac2c3a2a84b8e2432610b091724c32ff2d1509f..a9c3203cefe4bfbc31cc8317e2ceadcc27940790 100644 --- a/pixelsnap.inx +++ b/pixelsnap.inx @@ -8,6 +8,10 @@ true 0.5 + + + + all diff --git a/pixelsnap.py b/pixelsnap.py index ef0ef24b720d9d9646937a27a0ed3e4ff523e87e..471da70a7e7a7e89becc25e24420048b72e143bf 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 0000000000000000000000000000000000000000..5782872b7554dca1650a5c1ae5d909980e05ab52 --- /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 0000000000000000000000000000000000000000..5a3ce2b929f5ae75a0e2429a0641329e3dae1353 --- /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 0000000000000000000000000000000000000000..a25eaa7867a883520eacef672ca68dbf8f7e942d --- /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 ef6feceb65ca7d31c38a77359bbe5c0d27efa417..074abe0d11a14bb1e04b45e6bc56eff4ca4e96af 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", + ), + ]