[gnome-font-viewer] font-widget: sync from Sushi



commit 4940840922877a957d67ac5ad8585b6c12bdc34e
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue May 1 18:20:26 2012 -0400

    font-widget: sync from Sushi

 src/sushi-font-widget.c |   64 ++++++++++++++++++++++++++++++++++------------
 1 files changed, 47 insertions(+), 17 deletions(-)
---
diff --git a/src/sushi-font-widget.c b/src/sushi-font-widget.c
index 95fbdcd..2b4cc39 100644
--- a/src/sushi-font-widget.c
+++ b/src/sushi-font-widget.c
@@ -76,13 +76,18 @@ static const gchar punctuation_text_stock[] = "0123456789.:,;(*!?')";
  * License: GPLv2+
  */
 static void
-draw_string (cairo_t *cr,
+draw_string (SushiFontWidget *self,
+             cairo_t *cr,
              GtkBorder padding,
 	     const gchar *text,
 	     gint *pos_y)
 {
   cairo_font_extents_t font_extents;
   cairo_text_extents_t extents;
+  GtkTextDirection text_dir;
+  gint pos_x;
+
+  text_dir = gtk_widget_get_direction (GTK_WIDGET (self));
 
   cairo_font_extents (cr, &font_extents);
   cairo_text_extents (cr, text, &extents);
@@ -90,8 +95,14 @@ draw_string (cairo_t *cr,
   if (pos_y != NULL)
     *pos_y += font_extents.ascent + font_extents.descent +
       extents.y_advance + padding.top;
+  if (text_dir == GTK_TEXT_DIR_LTR)
+    pos_x = padding.left;
+  else {
+    pos_x = gtk_widget_get_allocated_width (GTK_WIDGET (self)) -
+      extents.x_advance - padding.right;
+  }
 
-  cairo_move_to (cr, padding.left, *pos_y);
+  cairo_move_to (cr, pos_x, *pos_y);
   cairo_show_text (cr, text);
 
   *pos_y += padding.bottom;
@@ -187,11 +198,33 @@ random_string_from_available_chars (FT_Face face,
   return g_string_free (retval, FALSE);
 }
 
-static void
-build_strings_for_face (SushiFontWidget *self)
+static gboolean
+set_pango_sample_string (SushiFontWidget *self)
 {
   const gchar *sample_string;
+  gboolean retval = FALSE;
+
+  sample_string = pango_language_get_sample_string (pango_language_from_string (NULL));
+  if (check_font_contain_text (self->priv->face, sample_string))
+    retval = TRUE;
+
+  if (!retval) {
+    sample_string = pango_language_get_sample_string (pango_language_from_string ("C"));
+    if (check_font_contain_text (self->priv->face, sample_string))
+      retval = TRUE;
+  }
+
+  if (retval) {
+    g_free (self->priv->sample_string);
+    self->priv->sample_string = g_strdup (sample_string);
+  }
 
+  return retval;
+}
+
+static void
+build_strings_for_face (SushiFontWidget *self)
+{
   /* if we don't have lowercase/uppercase/punctuation text in the face,
    * we omit it directly, and render a random text below.
    */
@@ -210,16 +243,13 @@ build_strings_for_face (SushiFontWidget *self)
   else
     self->priv->punctuation_text = NULL;
 
-  sample_string = pango_language_get_sample_string (NULL);
-
-  if (check_font_contain_text (self->priv->face, sample_string))
-    self->priv->sample_string = g_strdup (sample_string);
-  else
+  if (!set_pango_sample_string (self))
     self->priv->sample_string = random_string_from_available_chars (self->priv->face, 36);
 
-  if (self->priv->face->family_name == NULL) {
-    self->priv->font_name = NULL;
-  } else {
+  g_free (self->priv->font_name);
+  self->priv->font_name = NULL;
+
+  if (self->priv->face->family_name != NULL) {
     gchar *font_name = 
       g_strconcat (self->priv->face->family_name, " ",
                    self->priv->face->style_name, NULL);
@@ -466,7 +496,7 @@ sushi_font_widget_draw (GtkWidget *drawing_area,
 
   if (self->priv->font_name != NULL) {
     cairo_set_font_size (cr, title_size);
-    draw_string (cr, padding, self->priv->font_name, &pos_y);
+    draw_string (self, cr, padding, self->priv->font_name, &pos_y);
   }
 
   if (pos_y > allocated_height)
@@ -476,17 +506,17 @@ sushi_font_widget_draw (GtkWidget *drawing_area,
   cairo_set_font_size (cr, alpha_size);
 
   if (self->priv->lowercase_text != NULL)
-    draw_string (cr, padding, self->priv->lowercase_text, &pos_y);
+    draw_string (self, cr, padding, self->priv->lowercase_text, &pos_y);
   if (pos_y > allocated_height)
     goto end;
 
   if (self->priv->uppercase_text != NULL)
-    draw_string (cr, padding, self->priv->uppercase_text, &pos_y);
+    draw_string (self, cr, padding, self->priv->uppercase_text, &pos_y);
   if (pos_y > allocated_height)
     goto end;
 
   if (self->priv->punctuation_text != NULL)
-    draw_string (cr, padding, self->priv->punctuation_text, &pos_y);
+    draw_string (self, cr, padding, self->priv->punctuation_text, &pos_y);
   if (pos_y > allocated_height)
     goto end;
 
@@ -494,7 +524,7 @@ sushi_font_widget_draw (GtkWidget *drawing_area,
 
   for (i = 0; i < n_sizes; i++) {
     cairo_set_font_size (cr, sizes[i]);
-    draw_string (cr, padding, self->priv->sample_string, &pos_y);
+    draw_string (self, cr, padding, self->priv->sample_string, &pos_y);
     if (pos_y > allocated_height)
       break;
   }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]