[gucharmap] Add option to only show glyphs from the selected font



commit 4f213b4f4a4710c3a6460f054f5a9042ab644277
Author: Christian Persch <chpe gnome org>
Date:   Thu Aug 19 20:52:23 2010 +0200

    Add option to only show glyphs from the selected font
    
    Bug #579900.

 docs/reference/gucharmap-sections.txt |    5 +
 gucharmap/gucharmap-charmap.c         |   58 ++++++++++++-
 gucharmap/gucharmap-charmap.h         |    4 +
 gucharmap/gucharmap-chartable.c       |  152 +++++++++++++++++++++++++++++----
 gucharmap/gucharmap-chartable.h       |    3 +
 gucharmap/gucharmap-private.h         |    1 +
 gucharmap/gucharmap-window.c          |   14 +++
 7 files changed, 219 insertions(+), 18 deletions(-)
---
diff --git a/docs/reference/gucharmap-sections.txt b/docs/reference/gucharmap-sections.txt
index f2544fe..bb56d77 100644
--- a/docs/reference/gucharmap-sections.txt
+++ b/docs/reference/gucharmap-sections.txt
@@ -1,3 +1,4 @@
+
 <SECTION>
 <FILE>gucharmap-codepoint-list</FILE>
 <TITLE>GucharmapCodepointList</TITLE>
@@ -75,6 +76,8 @@ GucharmapChartable
 gucharmap_chartable_new
 gucharmap_chartable_set_font
 gucharmap_chartable_set_font_desc
+gucharmap_chartable_set_font_fallback
+gucharmap_chartable_get_font_fallback
 gucharmap_chartable_get_active_character
 gucharmap_chartable_set_active_character
 gucharmap_chartable_set_zoom_enabled
@@ -165,6 +168,8 @@ gucharmap_charmap_set_active_chapter
 gucharmap_charmap_get_active_chapter
 gucharmap_charmap_set_font
 gucharmap_charmap_set_font_desc
+gucharmap_charmap_set_font_fallback
+gucharmap_charmap_get_font_fallback
 gucharmap_charmap_get_chartable
 gucharmap_charmap_get_chapters_view
 gucharmap_charmap_set_chapters_model
diff --git a/gucharmap/gucharmap-charmap.c b/gucharmap/gucharmap-charmap.c
index 8ffb4f2..09d18e1 100644
--- a/gucharmap/gucharmap-charmap.c
+++ b/gucharmap/gucharmap-charmap.c
@@ -72,7 +72,8 @@ enum {
   PROP_ACTIVE_CODEPOINT_LIST,
   PROP_ACTIVE_PAGE,
   PROP_SNAP_POW2,
-  PROP_FONT_DESC
+  PROP_FONT_DESC,
+  PROP_FONT_FALLBACK
 };
 
 static guint gucharmap_charmap_signals[NUM_SIGNALS];
@@ -134,6 +135,9 @@ gucharmap_charmap_get_property (GObject *object,
     case PROP_FONT_DESC:
       g_value_set_boxed (value, gucharmap_charmap_get_font_desc (charmap));
       break;
+    case PROP_FONT_FALLBACK:
+      g_value_set_boolean (value, gucharmap_charmap_get_font_fallback (charmap));
+      break;
     case PROP_SNAP_POW2:
       g_value_set_boolean (value, gucharmap_charmap_get_snap_pow2 (charmap));
       break;
@@ -174,6 +178,9 @@ gucharmap_charmap_set_property (GObject *object,
     case PROP_FONT_DESC:
       gucharmap_charmap_set_font_desc (charmap, g_value_get_boxed (value));
       break;
+    case PROP_FONT_FALLBACK:
+      gucharmap_charmap_set_font_fallback (charmap, g_value_get_boolean (value));
+      break;
     case PROP_SNAP_POW2:
       gucharmap_charmap_set_snap_pow2 (charmap, g_value_get_boolean (value));
       break;
@@ -320,6 +327,20 @@ gucharmap_charmap_class_init (GucharmapCharmapClass *klass)
                          G_PARAM_STATIC_NICK |
                          G_PARAM_STATIC_BLURB));
 
+  /**
+   * GucharmapCharmap:font-fallback:
+   *
+   * Whether font fallback is enabled.
+   *
+   * Since: 2.34
+   */
+  g_object_class_install_property
+    (object_class,
+     PROP_FONT_FALLBACK,
+     g_param_spec_boolean ("font-fallback", NULL, NULL,
+                           TRUE,
+                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_property
     (object_class,
      PROP_SNAP_POW2,
@@ -1353,6 +1374,41 @@ gucharmap_charmap_get_font_desc (GucharmapCharmap *charmap)
   return charmap->priv->font_desc;
 }
 
+/**
+ * gucharmap_charmap_set_font_fallback:
+ * @charmap: a #GucharmapCharmap
+ * @enable_font_fallback: whether to enable font fallback
+ *
+ * Since: 2.34
+ */
+void 
+gucharmap_charmap_set_font_fallback (GucharmapCharmap *charmap,
+                                     gboolean enable_font_fallback)
+{
+  g_return_if_fail (GUCHARMAP_IS_CHARMAP (charmap));
+
+  gucharmap_chartable_set_font_fallback (charmap->priv->chartable, 
+                                         enable_font_fallback);
+
+  g_object_notify (G_OBJECT (charmap), "font-fallback");
+}
+
+/**
+ * gucharmap_charmap_get_font_fallback:
+ * @charmap: a #GucharmapCharmap
+ *
+ * Returns: whether font fallback is enabled
+ *
+ * Since: 2.34
+ */
+gboolean
+gucharmap_charmap_get_font_fallback (GucharmapCharmap *charmap)
+{
+  g_return_val_if_fail (GUCHARMAP_IS_CHARMAP (charmap), FALSE);
+
+  return gucharmap_chartable_get_font_fallback (charmap->priv->chartable);
+}
+
 void
 gucharmap_charmap_set_active_character (GucharmapCharmap *charmap,
                                         gunichar          wc)
diff --git a/gucharmap/gucharmap-charmap.h b/gucharmap/gucharmap-charmap.h
index 91cd8a6..31f783c 100644
--- a/gucharmap/gucharmap-charmap.h
+++ b/gucharmap/gucharmap-charmap.h
@@ -90,6 +90,10 @@ void                     gucharmap_charmap_set_font_desc      (GucharmapCharmap
 
 PangoFontDescription *   gucharmap_charmap_get_font_desc      (GucharmapCharmap  *charmap);
 
+void                     gucharmap_charmap_set_font_fallback  (GucharmapCharmap *charmap,
+                                                               gboolean enable_font_fallback);
+gboolean                 gucharmap_charmap_get_font_fallback  (GucharmapCharmap *charmap);
+
 GucharmapChaptersView *  gucharmap_charmap_get_chapters_view  (GucharmapCharmap       *charmap);
 
 void                     gucharmap_charmap_set_chapters_model (GucharmapCharmap       *charmap,
diff --git a/gucharmap/gucharmap-chartable.c b/gucharmap/gucharmap-chartable.c
index ce0de24..e78831f 100644
--- a/gucharmap/gucharmap-chartable.c
+++ b/gucharmap/gucharmap-chartable.c
@@ -1,6 +1,6 @@
 /*
  * Copyright © 2004 Noah Levitt
- * Copyright © 2007, 2008 Christian Persch
+ * Copyright © 2007, 2008, 2010 Christian Persch
  *
  * Some code copied from gtk+/gtk/gtkiconview:
  * Copyright © 2002, 2004  Anders Carlsson <andersca gnu org>
@@ -61,6 +61,7 @@ enum
   PROP_ACTIVE_CHAR,
   PROP_CODEPOINT_LIST,
   PROP_FONT_DESC,
+  PROP_FONT_FALLBACK,
   PROP_SNAP_POW2,
   PROP_ZOOM_ENABLED,
   PROP_ZOOM_SHOWING
@@ -136,20 +137,56 @@ G_DEFINE_TYPE (GucharmapChartable, gucharmap_chartable, GTK_TYPE_DRAWING_AREA)
 /* utility functions */
 
 static void
+gucharmap_chartable_clear_pango_layout (GucharmapChartable *chartable)
+{
+  GucharmapChartablePrivate *priv = chartable->priv;
+
+  if (priv->pango_layout == NULL)
+    return;
+  g_object_unref (priv->pango_layout);
+  priv->pango_layout = NULL;
+}
+
+static void
+gucharmap_chartable_ensure_pango_layout (GucharmapChartable *chartable)
+{
+  GucharmapChartablePrivate *priv = chartable->priv;
+
+  if (priv->pango_layout != NULL)
+    return;
+
+  priv->pango_layout = gtk_widget_create_pango_layout (GTK_WIDGET (chartable), NULL);
+  pango_layout_set_font_description (priv->pango_layout,
+                                     priv->font_desc);
+
+  if (priv->font_fallback == FALSE) 
+    {
+      PangoAttrList *list;
+
+      list = pango_attr_list_new ();
+      pango_attr_list_insert (list, pango_attr_fallback_new (FALSE));
+      pango_layout_set_attributes (priv->pango_layout, list);
+      pango_attr_list_unref (list);
+    }
+}
+
+static void
 gucharmap_chartable_set_font_desc_internal (GucharmapChartable *chartable,
                                             PangoFontDescription *font_desc /* adopting */)
 {
   GucharmapChartablePrivate *priv = chartable->priv;
+  GtkWidget *widget;
 
   if (priv->font_desc)
     pango_font_description_free (priv->font_desc);
 
   priv->font_desc = font_desc;
- 
-  if (priv->pango_layout)
-    pango_layout_set_font_description (priv->pango_layout, font_desc);
 
-  gtk_widget_queue_resize (GTK_WIDGET (chartable));
+  gucharmap_chartable_clear_pango_layout (chartable);
+
+  widget = GTK_WIDGET (chartable);
+  if (gtk_widget_get_realized (widget))
+    gtk_widget_queue_resize (widget);
 
   g_object_notify (G_OBJECT (chartable), "font-desc");
 }
@@ -486,6 +523,7 @@ layout_scaled_glyph (GucharmapChartable *chartable,
     pango_font_description_set_size (font_desc,
                                      font_factor * pango_font_description_get_size (priv->font_desc));
 
+  gucharmap_chartable_ensure_pango_layout (chartable);
   layout = pango_layout_new (pango_layout_get_context (priv->pango_layout));
 
   pango_layout_set_font_description (layout, font_desc);
@@ -493,6 +531,16 @@ layout_scaled_glyph (GucharmapChartable *chartable,
   buf[gucharmap_unichar_to_printable_utf8 (uc, buf)] = '\0';
   pango_layout_set_text (layout, buf, -1);
 
+  if (priv->font_fallback == FALSE) 
+    {
+      PangoAttrList *list;
+
+      list = pango_attr_list_new ();
+      pango_attr_list_insert (list, pango_attr_fallback_new (FALSE));
+      pango_layout_set_attributes (layout, list);
+      pango_attr_list_unref (list);
+    }
+
   if (font_family)
     *font_family = get_font (layout);
 
@@ -807,6 +855,16 @@ draw_character (GucharmapChartable *chartable,
   if (wc > UNICHAR_MAX || !gucharmap_unichar_validate (wc) || !gucharmap_unichar_isdefined (wc))
     return;
 
+  n = gucharmap_unichar_to_printable_utf8 (wc, buf);
+  pango_layout_set_text (priv->pango_layout, buf, n);
+
+  /* Keep the square empty if font fallback is disabled and the
+   * font has no glyph for this cell.
+   */
+  if (!priv->font_fallback &&
+      pango_layout_get_unknown_glyphs_count (priv->pango_layout) > 0)
+    return;
+
   cairo_save (cr);
 
   style = gtk_widget_get_style (widget);
@@ -823,9 +881,6 @@ draw_character (GucharmapChartable *chartable,
   square_width = _gucharmap_chartable_column_width (chartable, col) - 1;
   square_height = _gucharmap_chartable_row_height (chartable, row) - 1;
 
-  n = gucharmap_unichar_to_printable_utf8 (wc, buf);
-  pango_layout_set_text (priv->pango_layout, buf, n);
-
   pango_layout_get_pixel_size (priv->pango_layout, &char_width, &char_height);
 
   /* (square_width - char_width)/2 is the smaller half */
@@ -984,6 +1039,8 @@ gucharmap_chartable_draw (GucharmapChartable *chartable,
 {
   int row, col;
 
+  gucharmap_chartable_ensure_pango_layout (chartable);
+
   for (row = start_row;  row < end_row; ++row)
     {
       for (col = start_col;  col < end_col; ++col)
@@ -1532,9 +1589,7 @@ gucharmap_chartable_style_set (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (gucharmap_chartable_parent_class)->style_set (widget, previous_style);
 
-  if (priv->pango_layout)
-    g_object_unref (priv->pango_layout);
-  priv->pango_layout = NULL;
+  gucharmap_chartable_clear_pango_layout (chartable);
 
   if (priv->font_desc == NULL) {
     GtkStyle *style;
@@ -1555,10 +1610,6 @@ gucharmap_chartable_style_set (GtkWidget *widget,
     g_assert (priv->font_desc != NULL);
   }
 
-  priv->pango_layout = gtk_widget_create_pango_layout (widget, NULL);
-  pango_layout_set_font_description (priv->pango_layout,
-                                     priv->font_desc);
-
   /* FIXME: necessary? */
   /* gtk_widget_queue_draw (widget); */
   gtk_widget_queue_resize (widget);
@@ -1834,6 +1885,7 @@ gucharmap_chartable_init (GucharmapChartable *chartable)
   priv->zoom_window = NULL;
   priv->zoom_image = NULL;
   priv->snap_pow2_enabled = FALSE;
+  priv->font_fallback = TRUE;
 
 /* This didn't fix the slow expose events either: */
 /*  gtk_widget_set_double_buffered (widget, FALSE); */
@@ -1872,8 +1924,7 @@ gucharmap_chartable_finalize (GObject *object)
   if (priv->font_desc)
     pango_font_description_free (priv->font_desc);
 
-  if (priv->pango_layout)
-    g_object_unref (priv->pango_layout);
+  gucharmap_chartable_clear_pango_layout (chartable);
 
   gtk_target_list_unref (priv->target_list);
 
@@ -1903,6 +1954,9 @@ gucharmap_chartable_set_property (GObject *object,
     case PROP_FONT_DESC:
       gucharmap_chartable_set_font_desc (chartable, g_value_get_boxed (value));
       break;
+    case PROP_FONT_FALLBACK:
+      gucharmap_chartable_set_font_fallback (chartable, g_value_get_boolean (value));
+      break;
     case PROP_SNAP_POW2:
       gucharmap_chartable_set_snap_pow2 (chartable, g_value_get_boolean (value));
       break;
@@ -1937,6 +1991,9 @@ gucharmap_chartable_get_property (GObject *object,
     case PROP_FONT_DESC:
       g_value_set_boxed (value, gucharmap_chartable_get_font_desc (chartable));
       break;
+    case PROP_FONT_FALLBACK:
+      g_value_set_boolean (value, gucharmap_chartable_get_font_fallback (chartable));
+      break;
     case PROP_SNAP_POW2:
       g_value_set_boolean (value, priv->snap_pow2_enabled);
       break;
@@ -2089,6 +2146,20 @@ gucharmap_chartable_class_init (GucharmapChartableClass *klass)
                          G_PARAM_STATIC_NICK |
                          G_PARAM_STATIC_BLURB));
 
+  /**
+   * GucharmapChartable:font-fallback:
+   *
+   * Whether font fallback is enabled.
+   *
+   * Since: 2.34
+   */
+  g_object_class_install_property
+    (object_class,
+     PROP_FONT_FALLBACK,
+     g_param_spec_boolean ("font-fallback", NULL, NULL,
+                           TRUE,
+                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_property
     (object_class,
      PROP_SNAP_POW2,
@@ -2320,6 +2391,53 @@ gucharmap_chartable_get_font_desc (GucharmapChartable *chartable)
 }
 
 /**
+ * gucharmap_chartable_set_font_fallback:
+ * @chartable: a #GucharmapChartable
+ * @enable_font_fallback: whether to enable font fallback
+ *
+ * Since: 2.34
+ */
+void 
+gucharmap_chartable_set_font_fallback (GucharmapChartable *chartable,
+                                       gboolean enable_font_fallback)
+{
+  GucharmapChartablePrivate *priv;
+  GtkWidget *widget;
+
+  g_return_if_fail (GUCHARMAP_IS_CHARTABLE (chartable));
+
+  priv = chartable->priv;
+  enable_font_fallback = enable_font_fallback != FALSE;
+  if (enable_font_fallback == priv->font_fallback)
+    return;
+
+  priv->font_fallback = enable_font_fallback;
+  g_object_notify (G_OBJECT (chartable), "font-fallback");
+
+  gucharmap_chartable_clear_pango_layout (chartable);
+
+  widget = GTK_WIDGET (chartable);
+  if (gtk_widget_get_realized (widget))
+    gtk_widget_queue_draw (widget);
+}
+
+/**
+ * gucharmap_chartable_get_font_fallback:
+ * @chartable: a #GucharmapChartable
+ *
+ * Returns: whether font fallback is enabled
+ *
+ * Since: 2.34
+ */
+gboolean
+gucharmap_chartable_get_font_fallback (GucharmapChartable *chartable)
+{
+  g_return_val_if_fail (GUCHARMAP_IS_CHARTABLE (chartable), FALSE);
+
+  return chartable->priv->font_fallback;
+}
+
+/**
  * gucharmap_chartable_get_active_character:
  * @chartable: a #GucharmapChartable
  *
diff --git a/gucharmap/gucharmap-chartable.h b/gucharmap/gucharmap-chartable.h
index ad632fa..7a948f4 100644
--- a/gucharmap/gucharmap-chartable.h
+++ b/gucharmap/gucharmap-chartable.h
@@ -72,6 +72,9 @@ GtkWidget * gucharmap_chartable_new (void);
 void gucharmap_chartable_set_font_desc (GucharmapChartable *chartable,
                                         PangoFontDescription *font_desc);
 PangoFontDescription * gucharmap_chartable_get_font_desc (GucharmapChartable *chartable);
+void gucharmap_chartable_set_font_fallback (GucharmapChartable *chartable,
+                                            gboolean enable_font_fallback);
+gboolean gucharmap_chartable_get_font_fallback (GucharmapChartable *chartable);
 gunichar gucharmap_chartable_get_active_character (GucharmapChartable *chartable);
 void gucharmap_chartable_set_active_character (GucharmapChartable *chartable, 
                                                gunichar uc);
diff --git a/gucharmap/gucharmap-private.h b/gucharmap/gucharmap-private.h
index 4f34e86..f035bd1 100644
--- a/gucharmap/gucharmap-private.h
+++ b/gucharmap/gucharmap-private.h
@@ -72,6 +72,7 @@ struct _GucharmapChartablePrivate {
   /* Settings */
   guint snap_pow2_enabled : 1;
   guint zoom_mode_enabled : 1;
+  guint font_fallback     : 1;
 };
 
 gint _gucharmap_chartable_cell_column	(GucharmapChartable *chartable,
diff --git a/gucharmap/gucharmap-window.c b/gucharmap/gucharmap-window.c
index 84383ac..feb6665 100644
--- a/gucharmap/gucharmap-window.c
+++ b/gucharmap/gucharmap-window.c
@@ -336,6 +336,16 @@ snap_cols_pow2 (GtkAction        *action,
 }
 
 static void
+no_font_fallback_toggled_cb (GtkAction       *action, 
+                             GucharmapWindow *guw)
+{
+  gboolean is_active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+
+  gucharmap_charmap_set_font_fallback (guw->charmap, !is_active);
+/*  gucharmap_settings_set_font_fallback (is_active); */
+}
+
+static void
 open_url (GtkWindow *parent,
           const char *uri,
           guint32 user_time)
@@ -637,6 +647,7 @@ static const char ui_info [] =
       "<menuitem action='ByScript' />"
       "<menuitem action='ByUnicodeBlock' />"
       "<separator />"
+      "<menuitem action='ShowOnlyGlyphsInFont' />"
       "<menuitem action='SnapColumns' />"
       "<separator />"
       "<menuitem action='ZoomIn' />"
@@ -851,6 +862,9 @@ gucharmap_window_init (GucharmapWindow *guw)
   };
   const GtkToggleActionEntry toggle_menu_entries[] =
   {
+    { "ShowOnlyGlyphsInFont", NULL, N_("Sho_w only glyphs from this font"), NULL,
+      NULL,
+      G_CALLBACK (no_font_fallback_toggled_cb), FALSE },
     { "SnapColumns", NULL, N_("Snap _Columns to Power of Two"), NULL,
       NULL,
       G_CALLBACK (snap_cols_pow2), FALSE },



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