spakin / SimpInkScr

Simple Inkscape Scripting
https://inkscape.org/~pakin/%E2%98%85simple-inkscape-scripting
GNU General Public License v3.0
321 stars 32 forks source link

Feature Request; Flip Object #18

Closed moltra closed 2 years ago

moltra commented 2 years ago

Would you be able to add the ability to flip the selected object? I am currently flipping both vertically and horizontally each text box in a drawing I am working on. I am talking about around 200+ text boxes.

spakin commented 2 years ago

A flip is just a scale by -1. Hence, if you're running a sufficiently recent version of Simple Inkscape Scripting, which exposes svg_root and supports modifying an object's transformation matrix, you can flip all selected objects with the following script:

for obj in [inkex_object(o) for o in svg_root.selected.values()]:
    ctr = obj.bounding_box().center
    xform = inkex.Transform()
    xform.add_translate(ctr)
    xform.add_scale(-1, -1)
    xform.add_translate(-ctr)
    obj.transform = xform * obj.transform

The first line loops over all objects that have been selected in the Inkscape GUI, converting them to Simple Inkscape Scripting objects. The second line finds the object's center. For text, this is the starting point on the baseline because the Inkscape API doesn't provide a convenient mechanism for computing the true bounding box (and therefore center) of a piece of text. The next four lines define a translation of the object to the origin, a horizontal and vertical flip around the origin, and a translation of the object back to its previous location. The final line applies these transformations to the object atop whatever transformations already were applied to it.