[PATCH 3/4] ImageText support

unknown author cooker at mandrivalinux.org
Wed Aug 19 05:42:02 PDT 2009


---
 src/libplybootsplash/ply-image.c             |   14 ++++++
 src/libplybootsplash/ply-image.h             |    1 +
 src/libplybootsplash/ply-label-plugin.h      |    4 ++
 src/libplybootsplash/ply-label.c             |   46 ++++++++++++++++++++
 src/libplybootsplash/ply-label.h             |    6 +++
 src/plugins/controls/label/plugin.c          |   59 +++++++++++++++++++++++++-
 src/plugins/splash/script/script-lib-image.c |   28 ++++++++++++
 7 files changed, 157 insertions(+), 1 deletions(-)

diff --git a/src/libplybootsplash/ply-image.c b/src/libplybootsplash/ply-image.c
index 812313c..44299f3 100644
--- a/src/libplybootsplash/ply-image.c
+++ b/src/libplybootsplash/ply-image.c
@@ -112,6 +112,20 @@ ply_image_new (const char *filename)
   return image;
 }
 
+ply_image_t *
+ply_image_new_canvas (long width, long height)
+{
+  ply_image_t *image;
+  image = calloc (1, sizeof (ply_image_t));
+
+  image->filename = strdup ("dummy");
+  image->layout.address = calloc (height * width, 4);
+  image->width = width;
+  image->height = height;
+
+  return image;
+}
+
 void
 ply_image_free (ply_image_t *image)
 {
diff --git a/src/libplybootsplash/ply-image.h b/src/libplybootsplash/ply-image.h
index 780050e..1ce399e 100644
--- a/src/libplybootsplash/ply-image.h
+++ b/src/libplybootsplash/ply-image.h
@@ -30,6 +30,7 @@ typedef struct _ply_image ply_image_t;
 
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
 ply_image_t *ply_image_new (const char *filename);
+ply_image_t *ply_image_new_canvas (long width, long height);
 void ply_image_free (ply_image_t *image);
 bool ply_image_load (ply_image_t *image);
 uint32_t *ply_image_get_data (ply_image_t *image);
diff --git a/src/libplybootsplash/ply-label-plugin.h b/src/libplybootsplash/ply-label-plugin.h
index fbb65f6..81b7650 100644
--- a/src/libplybootsplash/ply-label-plugin.h
+++ b/src/libplybootsplash/ply-label-plugin.h
@@ -49,6 +49,10 @@ typedef struct
 
   long (* get_width_of_control) (ply_label_plugin_control_t        *label);
   long (* get_height_of_control) (ply_label_plugin_control_t        *label);
+  ply_image_t * (* text_to_image) (const char *text,
+                                   double      red,
+                                   double      green,
+                                   double      blue);
 } ply_label_plugin_interface_t;
 
 #endif /* PLY_LABEL_PLUGIN_H */
diff --git a/src/libplybootsplash/ply-label.c b/src/libplybootsplash/ply-label.c
index 16a8b1f..83e9f7e 100644
--- a/src/libplybootsplash/ply-label.c
+++ b/src/libplybootsplash/ply-label.c
@@ -210,4 +210,50 @@ ply_label_get_height (ply_label_t *label)
 
   return label->plugin_interface->get_height_of_control (label->control);
 }
+
+
+ply_image_t *
+ply_label_text_to_image (const char* text,
+                         double      red,
+                         double      green,
+                         double      blue)
+{
+  get_plugin_interface_function_t get_label_plugin_interface;
+
+  ply_module_handle_t *module_handle = ply_open_module (PLYMOUTH_PLUGIN_PATH "label.so");
+
+  if (module_handle == NULL)
+    return NULL;
+
+  get_label_plugin_interface = (get_plugin_interface_function_t)
+      ply_module_look_up_function (module_handle,
+                                   "ply_label_plugin_get_interface");
+
+  if (get_label_plugin_interface == NULL)
+    {
+      ply_save_errno ();
+      ply_close_module (module_handle);
+      ply_restore_errno ();
+      return NULL;
+    }
+
+  const ply_label_plugin_interface_t *plugin_interface = get_label_plugin_interface ();
+
+  if (plugin_interface == NULL)
+    {
+      ply_save_errno ();
+      ply_close_module (module_handle);
+      ply_restore_errno ();
+      return NULL;
+    }
+
+  ply_image_t *image = plugin_interface->text_to_image (text, red, green, blue);
+  
+  ply_close_module (module_handle);
+  
+  return image;
+}
+
+
+
 /* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
diff --git a/src/libplybootsplash/ply-label.h b/src/libplybootsplash/ply-label.h
index c4673a2..cc265f8 100644
--- a/src/libplybootsplash/ply-label.h
+++ b/src/libplybootsplash/ply-label.h
@@ -27,6 +27,7 @@
 #include <unistd.h>
 
 #include "ply-event-loop.h"
+#include "ply-image.h"
 #include "ply-window.h"
 
 typedef struct _ply_label ply_label_t;
@@ -49,6 +50,11 @@ void ply_label_set_text (ply_label_t *label,
 
 long ply_label_get_width (ply_label_t *label);
 long ply_label_get_height (ply_label_t *label);
+
+ply_image_t *ply_label_text_to_image (const char* text,
+                                      double      red,
+                                      double      green,
+                                      double      blue);
 #endif
 
 #endif /* PLY_LABEL_H */
diff --git a/src/plugins/controls/label/plugin.c b/src/plugins/controls/label/plugin.c
index 24bdf94..581355f 100644
--- a/src/plugins/controls/label/plugin.c
+++ b/src/plugins/controls/label/plugin.c
@@ -44,6 +44,7 @@
 #include <pango/pangocairo.h>
 
 #include "ply-frame-buffer.h"
+#include "ply-image.h"
 #include "ply-utils.h"
 #include "ply-window.h"
 
@@ -232,6 +233,61 @@ is_control_hidden (ply_label_plugin_control_t *label)
   return label->is_hidden;
 }
 
+ply_image_t *
+text_to_image (const char *text,
+               double      red,
+               double      green,
+               double      blue)
+{
+  int width;
+  int height;
+
+  {
+    cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data (NULL,CAIRO_FORMAT_ARGB32,0,0,0);
+    cairo_t *cairo_context = cairo_create (cairo_surface);
+    PangoLayout *pango_layout = pango_cairo_create_layout (cairo_context);
+    PangoFontDescription *description = pango_font_description_from_string ("Sans 12");
+    pango_layout_set_font_description (pango_layout, description);
+    pango_layout_set_text (pango_layout, text, -1);
+    pango_cairo_update_layout (cairo_context, pango_layout);
+    pango_layout_get_size (pango_layout, &width, &height);
+    
+    width  /= PANGO_SCALE;
+    height /= PANGO_SCALE;
+
+    pango_font_description_free (description);
+    g_object_unref (pango_layout);
+    cairo_destroy (cairo_context);
+    cairo_surface_destroy (cairo_surface);
+  }
+  
+  int x, y;
+  ply_image_t *canvas = ply_image_new_canvas(width, height);
+  unsigned char *data = ply_image_get_data (canvas);
+  
+  {
+    cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data (data,CAIRO_FORMAT_ARGB32, width, height, width * 4);
+    cairo_t *cairo_context = cairo_create (cairo_surface);
+    PangoLayout *pango_layout = pango_cairo_create_layout (cairo_context);
+    PangoFontDescription *description = pango_font_description_from_string ("Sans 12");
+    pango_layout_set_font_description (pango_layout, description);
+    pango_layout_set_text (pango_layout, text, -1);
+
+    cairo_move_to (cairo_context, 0, 0);
+    cairo_set_source_rgba (cairo_context, red, green, blue, 1.0);
+    pango_cairo_show_layout (cairo_context, pango_layout);
+    cairo_surface_flush (cairo_surface);
+
+    pango_font_description_free (description);
+    g_object_unref (pango_layout);
+    cairo_destroy (cairo_context);
+    cairo_surface_destroy (cairo_surface);
+  }
+  return canvas;
+}
+
+
+
 ply_label_plugin_interface_t *
 ply_label_plugin_get_interface (void)
 {
@@ -245,7 +301,8 @@ ply_label_plugin_get_interface (void)
       .is_control_hidden = is_control_hidden,
       .set_text_for_control = set_text_for_control,
       .get_width_of_control = get_width_of_control,
-      .get_height_of_control = get_height_of_control
+      .get_height_of_control = get_height_of_control,
+      .text_to_image = text_to_image
     };
 
   return &plugin_interface;
diff --git a/src/plugins/splash/script/script-lib-image.c b/src/plugins/splash/script/script-lib-image.c
index 5ea2f0f..68cea9a 100644
--- a/src/plugins/splash/script/script-lib-image.c
+++ b/src/plugins/splash/script/script-lib-image.c
@@ -21,6 +21,7 @@
  */
 #define _GNU_SOURCE
 #include "ply-image.h"
+#include "ply-label.h"
 #include "ply-utils.h"
 #include "script.h"
 #include "script-parse.h"
@@ -143,6 +144,24 @@ static script_return_t image_scale (script_state_t *state,
   return script_return_obj_null ();
 }
 
+
+static script_return_t image_text (script_state_t *state,
+                                   void           *user_data)
+{
+  script_lib_image_data_t *data = user_data;
+  char *text = script_obj_hash_get_string (state->local, "text");
+  float red = script_obj_hash_get_float (state->local, "red");
+  float green = script_obj_hash_get_float (state->local, "green");
+  float blue = script_obj_hash_get_float (state->local, "blue");
+  ply_image_t *new_image = NULL;
+  
+  if (text)
+     new_image = ply_label_text_to_image (text, red, green, blue);
+  if (new_image)
+    return script_return_obj (script_obj_new_native (new_image, data->class));
+  return script_return_obj_null ();
+}
+
 script_lib_image_data_t *script_lib_image_setup (script_state_t *state,
                                                  char         *image_dir)
 {
@@ -173,6 +192,15 @@ script_lib_image_data_t *script_lib_image_setup (script_state_t *state,
                               "height",
                               NULL);
   script_add_native_function (state->global,
+                              "ImageText",
+                              image_text,
+                              data,
+                              "text",
+                              "red",
+                              "green",
+                              "blue",
+                              NULL);
+  script_add_native_function (state->global,
                               "ImageGetWidth",
                               image_get_width,
                               data,
-- 
1.6.4


--=-LYrYSyVwjfE5jSXHx5X5--



More information about the plymouth mailing list