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



commit 0d617cf23e6c986868cc1790ac8bc417e78f4e83
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue May 1 17:08:47 2012 -0400

    font-widget: sync from Sushi

 src/sushi-font-widget.c |   70 ++++++++++++++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 28 deletions(-)
---
diff --git a/src/sushi-font-widget.c b/src/sushi-font-widget.c
index 11e1811..95fbdcd 100644
--- a/src/sushi-font-widget.c
+++ b/src/sushi-font-widget.c
@@ -54,7 +54,6 @@ struct _SushiFontWidgetPrivate {
   gchar *sample_string;
 
   gchar *font_name;
-  gboolean font_supports_title;
 };
 
 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
@@ -134,22 +133,26 @@ check_font_contain_text (FT_Face face,
 }
 
 static gchar *
-build_charlist_for_face (FT_Face face)
+build_charlist_for_face (FT_Face face,
+                         gint *length)
 {
   GString *string;
   gulong c;
-  guint glyph;  
+  guint glyph;
+  gint total_chars = 0;
 
   string = g_string_new (NULL);
 
-  /* exclude normal ASCII characters here */
-  c = 255;
-  glyph = 0;
+  c = FT_Get_First_Char (face, &glyph);
 
-  do {
-    c = FT_Get_Next_Char (face, c, &glyph);
+  while (glyph != 0) {
     g_string_append_unichar (string, (gunichar) c);
-  } while (glyph != 0);
+    c = FT_Get_Next_Char (face, c, &glyph);
+    total_chars++;
+  }
+
+  if (length)
+    *length = total_chars;
 
   return g_string_free (string, FALSE);
 }
@@ -164,10 +167,12 @@ random_string_from_available_chars (FT_Face face,
   gchar *ptr, *end;
 
   idx = 0;
-  chars = build_charlist_for_face (face);
+  chars = build_charlist_for_face (face, &total_chars);
+
+  if (total_chars == 0)
+    return NULL;
 
   retval = g_string_new (NULL);
-  total_chars = g_utf8_strlen (chars, -1);
 
   while (idx < n_chars) {
     rand = g_random_int_range (0, total_chars);
@@ -212,11 +217,18 @@ build_strings_for_face (SushiFontWidget *self)
   else
     self->priv->sample_string = random_string_from_available_chars (self->priv->face, 36);
 
-  self->priv->font_name =
-    g_strconcat (self->priv->face->family_name, " ",
-                 self->priv->face->style_name, NULL);
-  self->priv->font_supports_title =
-    check_font_contain_text (self->priv->face, self->priv->font_name);
+  if (self->priv->face->family_name == NULL) {
+    self->priv->font_name = NULL;
+  } else {
+    gchar *font_name = 
+      g_strconcat (self->priv->face->family_name, " ",
+                   self->priv->face->style_name, NULL);
+
+    if (check_font_contain_text (self->priv->face, font_name))
+      self->priv->font_name = font_name;
+    else
+      g_free (font_name);
+  }
 }
 
 static gint *
@@ -326,7 +338,7 @@ sushi_font_widget_size_request (GtkWidget *drawing_area,
   cairo_set_font_face (cr, font);
   cairo_font_face_destroy (font);
 
-  if (self->priv->font_supports_title) {
+  if (self->priv->font_name != NULL) {
       cairo_set_font_size (cr, title_size);
       cairo_font_extents (cr, &font_extents);
       cairo_text_extents (cr, self->priv->font_name, &extents);
@@ -360,18 +372,20 @@ sushi_font_widget_size_request (GtkWidget *drawing_area,
     pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
   }
 
-  pixmap_height += SECTION_SPACING;
+  if (self->priv->sample_string != NULL) {
+    pixmap_height += SECTION_SPACING;
 
-  for (i = 0; i < n_sizes; i++) {
-    cairo_set_font_size (cr, sizes[i]);
-    cairo_font_extents (cr, &font_extents);
-    cairo_text_extents (cr, self->priv->sample_string, &extents);
-    pixmap_height += font_extents.ascent + font_extents.descent +
-      extents.y_advance + padding.top + padding.bottom;
-    pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
+    for (i = 0; i < n_sizes; i++) {
+      cairo_set_font_size (cr, sizes[i]);
+      cairo_font_extents (cr, &font_extents);
+      cairo_text_extents (cr, self->priv->sample_string, &extents);
+      pixmap_height += font_extents.ascent + font_extents.descent +
+        extents.y_advance + padding.top + padding.bottom;
+      pixmap_width = MAX (pixmap_width, extents.width + padding.left + padding.right);
 
-    if ((i == 7) && (min_height != NULL))
-      *min_height = pixmap_height;
+      if ((i == 7) && (min_height != NULL))
+        *min_height = pixmap_height;
+    }
   }
 
   pixmap_height += padding.bottom + SECTION_SPACING;
@@ -450,7 +464,7 @@ sushi_font_widget_draw (GtkWidget *drawing_area,
 
   /* draw text */
 
-  if (self->priv->font_supports_title) {
+  if (self->priv->font_name != NULL) {
     cairo_set_font_size (cr, title_size);
     draw_string (cr, padding, self->priv->font_name, &pos_y);
   }



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