[PATCH] Add alignment and width for labels.

Dr. Tilmann Bubeck t.bubeck at reinform.de
Sun Apr 22 23:06:03 PDT 2012


A ply_label can now be alignment with ply_label_set_alignment()
taking one of PLY_LABEL_ALIGN_LEFT, PLY_LABEL_ALIGN_CENTER, or
PLY_LABEL_ALIGN_RIGHT.

This alignment is made within a horizontal box which width can be set with
ply_label_set_width().

Both functions are now used by the plugin "two-step" to make sure, that
the prompt for "ask-for-password" gets centered on the screen. Previously
the prompt started in the middle of the screen and was cropped at the right
border of the screen (fixes bz #681513). This lead to unreadable prompts
for disk encryption.
---
 src/libply-splash-graphics/ply-label-plugin.h |    5 ++
 src/libply-splash-graphics/ply-label.c        |   34 ++++++++++++
 src/libply-splash-graphics/ply-label.h        |   10 ++++
 src/plugins/controls/label/plugin.c           |   72 +++++++++++++++++++++++-
 src/plugins/splash/two-step/plugin.c          |   16 ++++--
 5 files changed, 128 insertions(+), 9 deletions(-)

diff --git a/src/libply-splash-graphics/ply-label-plugin.h b/src/libply-splash-graphics/ply-label-plugin.h
index cf3cedd..8c1ce44 100644
--- a/src/libply-splash-graphics/ply-label-plugin.h
+++ b/src/libply-splash-graphics/ply-label-plugin.h
@@ -29,6 +29,7 @@
 #include "ply-event-loop.h"
 #include "ply-pixel-buffer.h"
 #include "ply-pixel-display.h"
+#include "ply-label.h"
 
 typedef struct _ply_label_plugin ply_label_plugin_t;
 typedef struct _ply_label_plugin_control ply_label_plugin_control_t;
@@ -52,6 +53,10 @@ typedef struct
 
   void (* set_text_for_control) (ply_label_plugin_control_t *label,
                                  const char                 *text);
+  void (* set_alignment_for_control) (ply_label_plugin_control_t  *label,
+                                      const ply_label_alignment_t alignment);
+  void (* set_width_for_control) (ply_label_plugin_control_t  *label,
+                                  long                        width);
   void (* set_font_for_control) (ply_label_plugin_control_t *label,
                                  const char                 *fontdesc);
   void (* set_color_for_control) (ply_label_plugin_control_t *label,
diff --git a/src/libply-splash-graphics/ply-label.c b/src/libply-splash-graphics/ply-label.c
index b97bb62..9919c6f 100644
--- a/src/libply-splash-graphics/ply-label.c
+++ b/src/libply-splash-graphics/ply-label.c
@@ -46,6 +46,8 @@ struct _ply_label
   ply_label_plugin_control_t *control;
 
   char *text;
+  ply_label_alignment_t alignment;
+  long width;
   char *fontdesc;
   float red;
   float green;
@@ -68,6 +70,8 @@ ply_label_new (void)
   label->green = 1;
   label->blue = 1;
   label->alpha = 1;
+  label->alignment = PLY_LABEL_ALIGN_LEFT;
+  label->width = -1;
   return label;
 }
 
@@ -128,6 +132,10 @@ ply_label_load_plugin (ply_label_t *label)
   if (label->text != NULL)
     label->plugin_interface->set_text_for_control (label->control,
                                                    label->text);
+  label->plugin_interface->set_alignment_for_control (label->control,
+						      label->alignment);
+  label->plugin_interface->set_width_for_control (label->control,
+						  label->width);
   if (label->fontdesc != NULL)
     label->plugin_interface->set_font_for_control (label->control,
                                                    label->fontdesc);
@@ -224,6 +232,32 @@ ply_label_set_text (ply_label_t *label,
                                                  text);
 }
 
+void
+ply_label_set_alignment (ply_label_t                 *label,
+			 const ply_label_alignment_t alignment)
+{
+  label->alignment = alignment;
+
+  if (label->plugin_interface == NULL)
+    return;
+
+  label->plugin_interface->set_alignment_for_control (label->control,
+						      alignment);
+}
+
+void
+ply_label_set_width (ply_label_t   *label,
+		             long          width)
+{
+  label->width = width;
+
+  if (label->plugin_interface == NULL)
+    return;
+
+  label->plugin_interface->set_width_for_control (label->control,
+						  width);
+}
+
 /*
  * Please see pango documentation, for fontdesc format:
  * http://library.gnome.org/devel/pango/stable/pango-Fonts.html#pango-font-description-from-string
diff --git a/src/libply-splash-graphics/ply-label.h b/src/libply-splash-graphics/ply-label.h
index d79b996..7e3af2d 100644
--- a/src/libply-splash-graphics/ply-label.h
+++ b/src/libply-splash-graphics/ply-label.h
@@ -32,6 +32,12 @@
 
 typedef struct _ply_label ply_label_t;
 
+typedef enum {
+  PLY_LABEL_ALIGN_LEFT,
+  PLY_LABEL_ALIGN_CENTER,
+  PLY_LABEL_ALIGN_RIGHT
+} ply_label_alignment_t;
+
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
 ply_label_t *ply_label_new (void);
 void ply_label_free (ply_label_t *label);
@@ -53,6 +59,10 @@ bool ply_label_is_hidden (ply_label_t *label);
 
 void ply_label_set_text (ply_label_t *label,
                          const char  *text);
+void ply_label_set_alignment (ply_label_t                  *label,
+                              const ply_label_alignment_t  alignment);
+void ply_label_set_width (ply_label_t   *label,
+			              long          width);
 void ply_label_set_font (ply_label_t *label,
                          const char  *fontdesc);
 void ply_label_set_color (ply_label_t *label,
diff --git a/src/plugins/controls/label/plugin.c b/src/plugins/controls/label/plugin.c
index e1ebb97..bf9cd36 100644
--- a/src/plugins/controls/label/plugin.c
+++ b/src/plugins/controls/label/plugin.c
@@ -57,6 +57,9 @@ struct _ply_label_plugin_control
 
   char               *text;
   char               *fontdesc;
+
+  PangoAlignment      alignment;
+  long                width;
   float               red;
   float               green;
   float               blue;
@@ -75,6 +78,8 @@ create_control (void)
   label = calloc (1, sizeof (ply_label_plugin_control_t));
 
   label->is_hidden = true;
+  label->alignment = PANGO_ALIGN_LEFT;
+  label->width     = -1;
 
   return label;
 }
@@ -139,7 +144,9 @@ get_cairo_context_for_sizing (ply_label_plugin_control_t *label)
 static PangoLayout*
 init_pango_text_layout (cairo_t *cairo_context,
 			char *text,
-			char *font_description)
+			char *font_description,
+			PangoAlignment alignment,
+			long width)
 {
   PangoLayout          *pango_layout;
   PangoFontDescription *description;
@@ -154,6 +161,10 @@ init_pango_text_layout (cairo_t *cairo_context,
   pango_layout_set_font_description (pango_layout, description);
   pango_font_description_free (description);
 
+  pango_layout_set_alignment(pango_layout, alignment);
+  if ( width >= 0 ) 
+    pango_layout_set_width(pango_layout, width * PANGO_SCALE);
+
   pango_layout_set_text (pango_layout, text, -1);
   pango_cairo_update_layout (cairo_context, pango_layout);
 
@@ -173,7 +184,7 @@ size_control (ply_label_plugin_control_t *label)
 
   cairo_context = get_cairo_context_for_sizing (label);
 
-  pango_layout = init_pango_text_layout(cairo_context, label->text, label->fontdesc);
+  pango_layout = init_pango_text_layout(cairo_context, label->text, label->fontdesc, label->alignment, label->width);
 
   pango_layout_get_size (pango_layout, &text_width, &text_height);
   label->area.width = (long) ((double) text_width / PANGO_SCALE);
@@ -201,7 +212,7 @@ draw_control (ply_label_plugin_control_t *label,
 
   cairo_context = get_cairo_context_for_pixel_buffer (label, pixel_buffer);
 
-  pango_layout = init_pango_text_layout(cairo_context, label->text, label->fontdesc);
+  pango_layout = init_pango_text_layout(cairo_context, label->text, label->fontdesc, label->alignment, label->width);
 
   pango_layout_get_size (pango_layout, &text_width, &text_height);
   label->area.width = (long) ((double) text_width / PANGO_SCALE);
@@ -225,6 +236,59 @@ draw_control (ply_label_plugin_control_t *label,
 }
 
 static void
+set_alignment_for_control (ply_label_plugin_control_t *label,
+			   const ply_label_alignment_t alignment)
+{
+  ply_rectangle_t dirty_area;
+  PangoAlignment pango_alignment;
+
+  switch(alignment) 
+    {
+    case PLY_LABEL_ALIGN_CENTER:
+      pango_alignment = PANGO_ALIGN_CENTER;
+      break;
+    case PLY_LABEL_ALIGN_RIGHT:
+      pango_alignment = PANGO_ALIGN_RIGHT;
+      break;
+    case PLY_LABEL_ALIGN_LEFT:
+    default:
+      pango_alignment = PANGO_ALIGN_LEFT;
+      break;
+    }
+
+  if (label->alignment != pango_alignment)
+    {
+      dirty_area = label->area;
+      label->alignment = pango_alignment;
+      size_control (label);
+      if (!label->is_hidden && label->display != NULL)
+        ply_pixel_display_draw_area (label->display,
+                                     dirty_area.x, dirty_area.y,
+                                     dirty_area.width, dirty_area.height);
+
+    }
+}
+
+static void
+set_width_for_control (ply_label_plugin_control_t *label,
+		               long                       width)
+{
+  ply_rectangle_t dirty_area;
+
+  if (label->width != width)
+    {
+      dirty_area = label->area;
+      label->width = width;
+      size_control (label);
+      if (!label->is_hidden && label->display != NULL)
+        ply_pixel_display_draw_area (label->display,
+                                     dirty_area.x, dirty_area.y,
+                                     dirty_area.width, dirty_area.height);
+
+    }
+}
+
+static void
 set_text_for_control (ply_label_plugin_control_t *label,
                       const char                 *text)
 {
@@ -343,6 +407,8 @@ ply_label_plugin_get_interface (void)
       .draw_control = draw_control,
       .is_control_hidden = is_control_hidden,
       .set_text_for_control = set_text_for_control,
+      .set_alignment_for_control = set_alignment_for_control,
+      .set_width_for_control = set_width_for_control,
       .set_font_for_control = set_font_for_control,
       .set_color_for_control = set_color_for_control,
       .get_width_of_control = get_width_of_control,
diff --git a/src/plugins/splash/two-step/plugin.c b/src/plugins/splash/two-step/plugin.c
index 8f69e0f..024e0e9 100644
--- a/src/plugins/splash/two-step/plugin.c
+++ b/src/plugins/splash/two-step/plugin.c
@@ -444,18 +444,17 @@ view_show_prompt (view_t     *view,
   ply_boot_splash_plugin_t *plugin;
   int x, y;
   int entry_width, entry_height;
+  unsigned long screen_width, screen_height;
 
   assert (view != NULL);
 
   plugin = view->plugin;
 
+  screen_width = ply_pixel_display_get_width (view->display);
+  screen_height = ply_pixel_display_get_height (view->display);
+
   if (ply_entry_is_hidden (view->entry))
     {
-      unsigned long screen_width, screen_height;
-
-      screen_width = ply_pixel_display_get_width (view->display);
-      screen_height = ply_pixel_display_get_height (view->display);
-
       view->box_area.width = ply_image_get_width (plugin->box_image);
       view->box_area.height = ply_image_get_height (plugin->box_image);
       view->box_area.x = screen_width / 2.0 - view->box_area.width / 2.0;
@@ -480,7 +479,12 @@ view_show_prompt (view_t     *view,
     {
       ply_label_set_text (view->label, prompt);
 
-      x = view->box_area.x + view->lock_area.width / 2;
+      // We center the prompt in the middle and use 80% of the horizontal space.
+      int label_width = screen_width * 100 / 80;
+      ply_label_set_alignment(view->label, PLY_LABEL_ALIGN_CENTER);
+      ply_label_set_width(view->label, label_width);
+
+      x = ( screen_width - label_width ) / 2;
       y = view->box_area.y + view->box_area.height;
 
       ply_label_show (view->label, view->display, x, y);
-- 
1.7.7.6



More information about the plymouth mailing list