[PATCH] script: Add alignment argument to Image.Text

Anisse Astier anisse at astier.eu
Mon Aug 6 09:30:14 PDT 2012


Enables scripts to choose the text alignment they want with a seventh
argument to Image.Text API:
new_image = Image.Text("Hello", 1, 1, 1, 1, "DejaVu Bold,Italic 18",
"center");

This argument can be ignored, the default being left align.
Possible values are "left", "center", or "right".

---
 src/plugins/splash/script/script-lib-image.c      |   25 ++++++++++++++++++++-
 src/plugins/splash/script/script-lib-image.script |    4 ++--
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/plugins/splash/script/script-lib-image.c b/src/plugins/splash/script/script-lib-image.c
index 20aec36..5be27fb 100644
--- a/src/plugins/splash/script/script-lib-image.c
+++ b/src/plugins/splash/script/script-lib-image.c
@@ -24,6 +24,7 @@
 #include "ply-label.h"
 #include "ply-pixel-buffer.h"
 #include "ply-utils.h"
+#include "ply-logger.h"
 #include "script.h"
 #include "script-parse.h"
 #include "script-object.h"
@@ -158,8 +159,9 @@ static script_return_t image_text (script_state_t *state,
   script_lib_image_data_t *data = user_data;
   ply_pixel_buffer_t *image;
   ply_label_t *label;
-  script_obj_t *alpha_obj, *font_obj;
+  script_obj_t *alpha_obj, *font_obj, *align_obj;
   int width, height;
+  int align = PLY_LABEL_ALIGN_LEFT;
   char *font;
   
   char *text = script_obj_hash_get_string (state->local, "text");
@@ -188,12 +190,32 @@ static script_return_t image_text (script_state_t *state,
 
   script_obj_unref(font_obj);
 
+  align_obj = script_obj_hash_peek_element(state->local, "align");
+
+  if (script_obj_is_string(align_obj)) {
+    char *align_str = script_obj_as_string(align_obj);
+
+    if(!strcmp("left", align_str))
+      align = PLY_LABEL_ALIGN_LEFT;
+    else if(!strcmp("center", align_str))
+      align = PLY_LABEL_ALIGN_CENTER;
+    else if(!strcmp("right", align_str))
+      align = PLY_LABEL_ALIGN_RIGHT;
+    else
+      ply_error("Unrecognized Image.Text alignment string '%s'. "
+	      "Expecting 'left', 'center', or 'right'\n",
+		align_str);
+    free(align_str);
+  }
+  script_obj_unref(align_obj);
+
   if (!text) return script_return_obj_null ();
 
   label = ply_label_new ();
   ply_label_set_text (label, text);
   if (font)
     ply_label_set_font (label, font);
+  ply_label_set_alignment(label, align);
   ply_label_set_color (label, red, green, blue, alpha);
   ply_label_show (label, NULL, 0, 0);
   
@@ -259,6 +281,7 @@ script_lib_image_data_t *script_lib_image_setup (script_state_t *state,
                               "blue",
                               "alpha",
                               "font",
+                              "align",
                               NULL);
 
   script_obj_unref (image_hash);
diff --git a/src/plugins/splash/script/script-lib-image.script b/src/plugins/splash/script/script-lib-image.script
index 4fa377f..00b1c19 100644
--- a/src/plugins/splash/script/script-lib-image.script
+++ b/src/plugins/splash/script/script-lib-image.script
@@ -14,9 +14,9 @@ Image.Scale = fun (width, height)
   return Image.Adopt (this._Scale(width, height));
 };
 
-Image.Text = fun (text, red, green, blue, alpha, font)
+Image.Text = fun (text, red, green, blue, alpha, font, align)
 {
-  return Image.Adopt (Image._Text (text, red, green, blue, alpha, font));
+  return Image.Adopt (Image._Text (text, red, green, blue, alpha, font, align));
 };
 
 Image |= fun (filename)
-- 
1.7.10.5



More information about the plymouth mailing list