Add aspect ratio to scaling

This commit is contained in:
Mario Voigt 2021-12-27 16:27:10 +01:00
parent f83deb7b33
commit 5034f80090
2 changed files with 30 additions and 23 deletions

View File

@ -15,6 +15,7 @@
<option value="Vertical">Vertical</option> <option value="Vertical">Vertical</option>
<option value="Uniform">Uniform</option> <option value="Uniform">Uniform</option>
</param> </param>
<param name="keep_aspect" type="bool" gui-text="Keep aspect ratio" gui-description="Does not apply for uniform scaling.">true</param>
<label>This effect will measure the selected paths and scale them to have the given size. Does not work for objects (you need to convert)!</label> <label>This effect will measure the selected paths and scale them to have the given size. Does not work for objects (you need to convert)!</label>
<effect> <effect>
<object-type>all</object-type> <object-type>all</object-type>

View File

@ -17,6 +17,7 @@ class ScaleToSize(inkex.EffectExtension):
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument('--unit') pars.add_argument('--unit')
pars.add_argument("--keep_aspect", type=inkex.Boolean, default=True, help="Does not apply for uniform scaling")
pars.add_argument("--expected_size", type=float, default=1.0, help="The expected size of the object") pars.add_argument("--expected_size", type=float, default=1.0, help="The expected size of the object")
pars.add_argument("--scale_type", default="Horizontal", help="Scale type (Uniform, Horizontal, Vertical)") pars.add_argument("--scale_type", default="Horizontal", help="Scale type (Uniform, Horizontal, Vertical)")
pars.add_argument("--description") pars.add_argument("--description")
@ -37,11 +38,16 @@ class ScaleToSize(inkex.EffectExtension):
bbox = element.bounding_box() bbox = element.bounding_box()
new_horiz_scale = self.options.expected_size * unit_factor / bbox.width new_horiz_scale = self.options.expected_size * unit_factor / bbox.width
new_vert_scale = self.options.expected_size * unit_factor / bbox.height new_vert_scale = self.options.expected_size * unit_factor / bbox.height
if self.options.scale_type == "Horizontal": if self.options.scale_type == "Horizontal":
if self.options.keep_aspect is False:
translation_matrix = [[new_horiz_scale, 0.0, 0.0], [0.0, 1.0, 0.0]] translation_matrix = [[new_horiz_scale, 0.0, 0.0], [0.0, 1.0, 0.0]]
else:
translation_matrix = [[new_horiz_scale, 0.0, 0.0], [0.0, new_horiz_scale, 0.0]]
elif self.options.scale_type == "Vertical": elif self.options.scale_type == "Vertical":
if self.options.keep_aspect is False:
translation_matrix = [[1.0, 0.0, 0.0], [0.0, new_vert_scale, 0.0]] translation_matrix = [[1.0, 0.0, 0.0], [0.0, new_vert_scale, 0.0]]
else:
translation_matrix = [[new_vert_scale, 0.0, 0.0], [0.0, new_vert_scale, 0.0]]
else: #Uniform else: #Uniform
translation_matrix = [[new_horiz_scale, 0.0, 0.0], [0.0, new_vert_scale, 0.0]] translation_matrix = [[new_horiz_scale, 0.0, 0.0], [0.0, new_vert_scale, 0.0]]
element.transform = Transform(translation_matrix) * element.composed_transform() element.transform = Transform(translation_matrix) * element.composed_transform()