summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@gmail.com>2020-03-10 23:10:08 +0100
committerRay Strode <halfline@gmail.com>2020-03-11 13:44:05 +0000
commitf4cf64abfda3384d21bc932034ed1d9700fa417d (patch)
treea6e6df25fe1a2af474b8184fd8f6b242505594e6
parentae2fedb1eb6b6829bb879b48878bbf5c4e642000 (diff)
script: add Image.Crop(x, y, width, height)
The script plugin currently allows pixel buffers to be resized and scaled, but provides no mechanism for script theme authors to just crop the buffer, which is necessary for images that cannot be stretched, such as progress bars with gradients. This commit adds that feature as a new Crop method.
-rw-r--r--src/plugins/splash/script/script-lib-image.c28
-rw-r--r--src/plugins/splash/script/script-lib-image.script5
2 files changed, 33 insertions, 0 deletions
diff --git a/src/plugins/splash/script/script-lib-image.c b/src/plugins/splash/script/script-lib-image.c
index c53e0b0..cc671d4 100644
--- a/src/plugins/splash/script/script-lib-image.c
+++ b/src/plugins/splash/script/script-lib-image.c
@@ -131,6 +131,25 @@ static script_return_t image_rotate (script_state_t *state,
return script_return_obj_null ();
}
+static script_return_t image_crop (script_state_t *state,
+ void *user_data)
+{
+ script_lib_image_data_t *data = user_data;
+ ply_pixel_buffer_t *image = script_obj_as_native_of_class (state->this, data->class);
+ int x = script_obj_hash_get_number (state->local, "x");
+ int y = script_obj_hash_get_number (state->local, "y");
+ int width = script_obj_hash_get_number (state->local, "width");
+ int height = script_obj_hash_get_number (state->local, "height");
+
+ if (image) {
+ ply_rectangle_t clip_area = { 0, 0, width, height };
+ ply_pixel_buffer_t *new_image = ply_pixel_buffer_new (width, height);
+ ply_pixel_buffer_fill_with_buffer_with_clip (new_image, image, -x, -y, &clip_area);
+ return script_return_obj (script_obj_new_native (new_image, data->class));
+ }
+ return script_return_obj_null ();
+}
+
static script_return_t image_scale (script_state_t *state,
void *user_data)
{
@@ -264,6 +283,15 @@ script_lib_image_data_t *script_lib_image_setup (script_state_t *state,
"angle",
NULL);
script_add_native_function (image_hash,
+ "_Crop",
+ image_crop,
+ data,
+ "x",
+ "y",
+ "width",
+ "height",
+ NULL);
+ script_add_native_function (image_hash,
"_Scale",
image_scale,
data,
diff --git a/src/plugins/splash/script/script-lib-image.script b/src/plugins/splash/script/script-lib-image.script
index ab4b7cf..8568ceb 100644
--- a/src/plugins/splash/script/script-lib-image.script
+++ b/src/plugins/splash/script/script-lib-image.script
@@ -9,6 +9,11 @@ Image.Rotate = fun (angle)
return Image.Adopt (this._Rotate(angle));
};
+Image.Crop = fun (x, y, width, height)
+{
+ return Image.Adopt (this._Crop(x, y, width, height));
+};
+
Image.Scale = fun (width, height)
{
return Image.Adopt (this._Scale(width, height));