[metacity] libmetacity: add meta_theme_create_title_layout



commit b061881166b1311a3b9b4e535539450b871bd121
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Jun 6 17:20:04 2016 +0300

    libmetacity: add meta_theme_create_title_layout

 libmetacity/meta-theme.c           |   88 ++++++++++++++++++++++++++++++-----
 libmetacity/meta-theme.h           |    7 ++-
 src/ui/frames.c                    |   25 +++--------
 src/ui/ui.c                        |    5 +--
 theme-viewer/theme-viewer-window.c |   12 +----
 5 files changed, 89 insertions(+), 48 deletions(-)
---
diff --git a/libmetacity/meta-theme.c b/libmetacity/meta-theme.c
index 4dc7891..056dc3f 100644
--- a/libmetacity/meta-theme.c
+++ b/libmetacity/meta-theme.c
@@ -41,6 +41,8 @@ struct _MetaTheme
 
   gchar                *gtk_theme_name;
   GHashTable           *variants;
+
+  PangoContext         *context;
 };
 
 enum
@@ -57,6 +59,32 @@ static GParamSpec *properties[LAST_PROP] = { NULL };
 G_DEFINE_TYPE (MetaTheme, meta_theme, G_TYPE_OBJECT)
 
 static void
+ensure_pango_context (MetaTheme *theme)
+{
+  GdkScreen *screen;
+  PangoFontMap *fontmap;
+  PangoContext *context;
+  const cairo_font_options_t *options;
+  gdouble dpi;
+
+  if (theme->context != NULL)
+    return;
+
+  screen = gdk_screen_get_default ();
+
+  fontmap = pango_cairo_font_map_get_default ();
+  context = pango_font_map_create_context (fontmap);
+
+  options = gdk_screen_get_font_options (screen);
+  pango_cairo_context_set_font_options (context, options);
+
+  dpi = gdk_screen_get_resolution (screen);
+  pango_cairo_context_set_resolution (context, dpi);
+
+  theme->context = context;
+}
+
+static void
 font_desc_apply_scale (PangoFontDescription *font_desc,
                        MetaTheme            *theme,
                        MetaFrameType         type,
@@ -133,6 +161,8 @@ meta_theme_dispose (GObject *object)
 
   g_clear_pointer (&theme->variants, g_hash_table_destroy);
 
+  g_clear_object (&theme->context);
+
   G_OBJECT_CLASS (meta_theme_parent_class)->dispose (object);
 }
 
@@ -263,6 +293,7 @@ void
 meta_theme_invalidate (MetaTheme *theme)
 {
   g_hash_table_remove_all (theme->variants);
+  g_clear_object (&theme->context);
 }
 
 void
@@ -377,6 +408,33 @@ meta_theme_get_frame_style (MetaTheme      *theme,
   return style;
 }
 
+/**
+ * meta_theme_create_title_layout:
+ * @theme: a #MetaTheme
+ * @title: (nullable): text to set on the layout
+ *
+ * Returns: (transfer full): the new #PangoLayout
+ */
+PangoLayout *
+meta_theme_create_title_layout (MetaTheme   *theme,
+                                const gchar *title)
+{
+  PangoLayout *layout;
+
+  ensure_pango_context (theme);
+
+  layout = pango_layout_new (theme->context);
+
+  if (title)
+    pango_layout_set_text (layout, title, -1);
+
+  pango_layout_set_auto_dir (layout, FALSE);
+  pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
+  pango_layout_set_single_paragraph_mode (layout, TRUE);
+
+  return layout;
+}
+
 PangoFontDescription*
 meta_theme_create_font_desc (MetaTheme      *theme,
                              const gchar    *variant,
@@ -407,32 +465,36 @@ meta_theme_create_font_desc (MetaTheme      *theme,
 }
 
 /**
- * Returns the height of the letters in a particular font.
+ * meta_theme_get_title_height:
+ * @theme: a #MetaTheme
+ * @font_desc: the font description to use when calculating title height
  *
- * \param font_desc  the font
- * \param context  the context of the font
- * \return  the height of the letters
+ * Returns: the height of title
  */
 gint
-meta_pango_font_desc_get_text_height (const PangoFontDescription *font_desc,
-                                      PangoContext               *context)
+meta_theme_get_title_height (MetaTheme                  *theme,
+                             const PangoFontDescription *font_desc)
 {
-  PangoFontMetrics *metrics;
   PangoLanguage *lang;
-  gint text_height;
+  PangoFontMetrics *metrics;
+  gint ascent;
+  gint descent;
+  gint title_height;
   gint scale;
 
-  lang = pango_context_get_language (context);
-  metrics = pango_context_get_metrics (context, font_desc, lang);
+  ensure_pango_context (theme);
 
-  text_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
-                              pango_font_metrics_get_descent (metrics));
+  lang = pango_context_get_language (theme->context);
+  metrics = pango_context_get_metrics (theme->context, font_desc, lang);
 
+  ascent = pango_font_metrics_get_ascent (metrics);
+  descent = pango_font_metrics_get_descent (metrics);
   pango_font_metrics_unref (metrics);
 
+  title_height = PANGO_PIXELS (ascent + descent);
   scale = get_window_scaling_factor ();
 
-  return text_height * scale;
+  return title_height * scale;
 }
 
 MetaFrameType
diff --git a/libmetacity/meta-theme.h b/libmetacity/meta-theme.h
index 64b6fc7..d3217db 100644
--- a/libmetacity/meta-theme.h
+++ b/libmetacity/meta-theme.h
@@ -165,13 +165,16 @@ MetaFrameStyle       *meta_theme_get_frame_style           (MetaTheme
                                                             MetaFrameType                type,
                                                             MetaFrameFlags               flags);
 
+PangoLayout          *meta_theme_create_title_layout       (MetaTheme                   *theme,
+                                                            const gchar                 *title);
+
 PangoFontDescription *meta_theme_create_font_desc          (MetaTheme                   *theme,
                                                             const gchar                 *variant,
                                                             MetaFrameType                type,
                                                             MetaFrameFlags               flags);
 
-gint                  meta_pango_font_desc_get_text_height (const PangoFontDescription  *font_desc,
-                                                            PangoContext                *context);
+gint                  meta_theme_get_title_height          (MetaTheme                   *theme,
+                                                            const PangoFontDescription  *font_desc);
 
 MetaFrameType         meta_frame_type_from_string          (const gchar                 *str);
 
diff --git a/src/ui/frames.c b/src/ui/frames.c
index a5bbbcf..1d99456 100644
--- a/src/ui/frames.c
+++ b/src/ui/frames.c
@@ -418,21 +418,18 @@ static void
 meta_frames_ensure_layout (MetaFrames  *frames,
                            MetaUIFrame *frame)
 {
-  GtkWidget *widget;
   MetaFrameFlags flags;
   MetaFrameType type;
+  MetaTheme *theme;
   MetaFrameStyle *style;
 
-  g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (frames)));
-
-  widget = GTK_WIDGET (frames);
-
   meta_core_get (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), frame->xwindow,
                  META_CORE_GET_FRAME_FLAGS, &flags,
                  META_CORE_GET_FRAME_TYPE, &type,
                  META_CORE_GET_END);
 
-  style = meta_theme_get_frame_style (meta_ui_get_theme (), type, flags);
+  theme = meta_ui_get_theme ();
+  style = meta_theme_get_frame_style (theme, type, flags);
 
   if (style != frame->cache_style)
     g_clear_object (&frame->text_layout);
@@ -442,19 +439,11 @@ meta_frames_ensure_layout (MetaFrames  *frames,
   if (frame->text_layout == NULL)
     {
       gpointer key, value;
-      MetaTheme *current;
       PangoFontDescription *font_desc;
       int size;
 
-      frame->text_layout = gtk_widget_create_pango_layout (widget, frame->title);
-
-      pango_layout_set_ellipsize (frame->text_layout, PANGO_ELLIPSIZE_END);
-      pango_layout_set_auto_dir (frame->text_layout, FALSE);
-      pango_layout_set_single_paragraph_mode (frame->text_layout, TRUE);
-
-      current = meta_ui_get_theme ();
-
-      font_desc = meta_theme_create_font_desc (current, frame->theme_variant,
+      frame->text_layout = meta_theme_create_title_layout (theme, frame->title);
+      font_desc = meta_theme_create_font_desc (theme, frame->theme_variant,
                                                type, flags);
 
       size = pango_font_description_get_size (font_desc);
@@ -467,9 +456,7 @@ meta_frames_ensure_layout (MetaFrames  *frames,
         }
       else
         {
-          frame->text_height =
-            meta_pango_font_desc_get_text_height (font_desc,
-                                                  gtk_widget_get_pango_context (widget));
+          frame->text_height = meta_theme_get_title_height (theme, font_desc);
 
           g_hash_table_replace (frames->text_heights,
                                 GINT_TO_POINTER (size),
diff --git a/src/ui/ui.c b/src/ui/ui.c
index f917c1d..2fa3012 100644
--- a/src/ui/ui.c
+++ b/src/ui/ui.c
@@ -683,16 +683,13 @@ meta_ui_theme_get_frame_borders (MetaUI           *ui,
   MetaTheme *theme;
   const gchar *theme_variant;
   PangoFontDescription *font_desc;
-  PangoContext *context;
   gint text_height;
 
   theme = meta_ui_get_theme ();
   theme_variant = NULL;
 
   font_desc = meta_theme_create_font_desc (theme, theme_variant, type, flags);
-  context = gtk_widget_get_pango_context (GTK_WIDGET (ui->frames));
-
-  text_height = meta_pango_font_desc_get_text_height (font_desc, context);
+  text_height = meta_theme_get_title_height (theme, font_desc);
   pango_font_description_free (font_desc);
 
   meta_theme_get_frame_borders (theme, theme_variant, type,
diff --git a/theme-viewer/theme-viewer-window.c b/theme-viewer/theme-viewer-window.c
index 7dcd041..11af971 100644
--- a/theme-viewer/theme-viewer-window.c
+++ b/theme-viewer/theme-viewer-window.c
@@ -236,25 +236,17 @@ update_button_layout (ThemeViewerWindow *window)
 static void
 update_title_layout (ThemeViewerWindow *window)
 {
-  GtkWidget *widget;
   PangoLayout *layout;
   PangoFontDescription *font_desc;
-  PangoContext *context;
   gint height;
 
-  widget = GTK_WIDGET (window);
-
-  layout = gtk_widget_create_pango_layout (widget, "Metacity Theme Viewer");
+  layout = meta_theme_create_title_layout (window->theme, "Metacity Theme Viewer");
   font_desc = meta_theme_create_font_desc (window->theme, window->theme_variant,
                                            window->frame_type, window->frame_flags);
 
-  context = gtk_widget_get_pango_context (widget);
-  height = meta_pango_font_desc_get_text_height (font_desc, context);
+  height = meta_theme_get_title_height (window->theme, font_desc);
 
-  pango_layout_set_auto_dir (layout, FALSE);
-  pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
   pango_layout_set_font_description (layout, font_desc);
-  pango_layout_set_single_paragraph_mode (layout, TRUE);
 
   if (window->title_layout)
     g_object_unref (window->title_layout);


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