[pango/private-vfuncs: 5/5] Clean up rounding code




commit a2e14a48b21f22db08237c20d2f9d1521417c6e3
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 31 14:50:09 2021 -0400

    Clean up rounding code
    
    Add private api to get the necessary information from
    the font backend, and drop the hack that was added in
    d4356779945855f7cc.

 pango/fonts.c              | 31 ++++++++++++++++++
 pango/pango-font-private.h | 12 +++++++
 pango/pangofc-font.c       | 24 ++++++++++++++
 pango/shape.c              | 80 +++++++++++++++-------------------------------
 4 files changed, 93 insertions(+), 54 deletions(-)
---
diff --git a/pango/fonts.c b/pango/fonts.c
index 27a973f8..cd30811c 100644
--- a/pango/fonts.c
+++ b/pango/fonts.c
@@ -1713,16 +1713,33 @@ pango_font_default_get_languages (PangoFont *font)
   return NULL;
 }
 
+static gboolean
+pango_font_default_is_hinted (PangoFont *font)
+{
+  return FALSE;
+}
+
+static void
+pango_font_default_get_scale_factors (PangoFont *font,
+                                      double    *x_scale,
+                                      double    *y_scale)
+{
+  *x_scale = *y_scale = 1.0;
+}
+
 static void
 pango_font_class_init (PangoFontClass *class G_GNUC_UNUSED)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoFontClassPrivate *pclass;
 
   object_class->finalize = pango_font_finalize;
 
   pclass = g_type_class_get_private ((GTypeClass *) class, PANGO_TYPE_FONT);
 
   pclass->get_languages = pango_font_default_get_languages;
+  pclass->is_hinted = pango_font_default_is_hinted;
+  pclass->get_scale_factors = pango_font_default_get_scale_factors;
 }
 
 static void
@@ -2667,3 +2684,17 @@ pango_font_get_languages (PangoFont *font)
 
   return pclass->get_languages (font);
 }
+
+gboolean
+pango_font_is_hinted (PangoFont *font)
+{
+  return PANGO_FONT_GET_CLASS_PRIVATE (font)->is_hinted (font);
+}
+
+void
+pango_font_get_scale_factors (PangoFont *font,
+                              double    *x_scale,
+                              double    *y_scale)
+{
+  PANGO_FONT_GET_CLASS_PRIVATE (font)->get_scale_factors (font, x_scale, y_scale);
+}
diff --git a/pango/pango-font-private.h b/pango/pango-font-private.h
index 54de2c01..d9845fab 100644
--- a/pango/pango-font-private.h
+++ b/pango/pango-font-private.h
@@ -35,8 +35,20 @@ PangoFontMetrics *pango_font_metrics_new (void);
 
 typedef struct {
   PangoLanguage ** (* get_languages) (PangoFont *font);
+
+  gboolean         (* is_hinted) (PangoFont *font);
+
+  void             (* get_scale_factors) (PangoFont *font,
+                                          double    *x_scale,
+                                          double    *y_scale);
+
 } PangoFontClassPrivate;
 
+gboolean pango_font_is_hinted         (PangoFont *font);
+void     pango_font_get_scale_factors (PangoFont *font,
+                                       double    *x_scale,
+                                       double    *y_scale);
+
 G_END_DECLS
 
 #endif /* __PANGO_FONT_PRIVATE_H__ */
diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c
index 2546e663..8543b85e 100644
--- a/pango/pangofc-font.c
+++ b/pango/pangofc-font.c
@@ -71,6 +71,10 @@ static void                  pango_fc_font_get_features (PangoFont        *font,
                                                          guint            *num_features);
 static hb_font_t *           pango_fc_font_create_hb_font (PangoFont        *font);
 static PangoLanguage **     _pango_fc_font_get_languages  (PangoFont        *font);
+static gboolean             _pango_fc_font_is_hinted      (PangoFont        *font);
+static void                 _pango_fc_font_get_scale_factors (PangoFont     *font,
+                                                              double        *x_scale,
+                                                              double        *y_scale);
 
 #define PANGO_FC_FONT_LOCK_FACE(font)  (PANGO_FC_FONT_GET_CLASS (font)->lock_face (font))
 #define PANGO_FC_FONT_UNLOCK_FACE(font)        (PANGO_FC_FONT_GET_CLASS (font)->unlock_face (font))
@@ -104,6 +108,8 @@ pango_fc_font_class_init (PangoFcFontClass *class)
   pclass = g_type_class_get_private ((GTypeClass *) class, PANGO_TYPE_FONT);
 
   pclass->get_languages = _pango_fc_font_get_languages;
+  pclass->is_hinted = _pango_fc_font_is_hinted;
+  pclass->get_scale_factors = _pango_fc_font_get_scale_factors;
 
   /**
    * PangoFcFont:pattern:
@@ -1093,3 +1099,21 @@ pango_fc_font_get_pattern (PangoFcFont *font)
 {
   return font->font_pattern;
 }
+
+gboolean
+_pango_fc_font_is_hinted (PangoFont *font)
+{
+  PangoFcFont *fcfont = PANGO_FC_FONT (font);
+
+  return fcfont->is_hinted;
+}
+
+void
+_pango_fc_font_get_scale_factors (PangoFont *font,
+                                  double    *x_scale,
+                                  double    *y_scale)
+{
+  PangoFcFont *fcfont = PANGO_FC_FONT (font);
+
+  pango_matrix_get_font_scale_factors (&fcfont->matrix, x_scale, y_scale);
+}
diff --git a/pango/shape.c b/pango/shape.c
index b870ecad..f8ac3691 100644
--- a/pango/shape.c
+++ b/pango/shape.c
@@ -25,6 +25,7 @@
 #include "pango-glyph.h"
 
 #include "pangohb-private.h"
+#include "pango-font-private.h"
 
 #include <string.h>
 
@@ -151,32 +152,6 @@ fallback_shape (const char          *text,
     pango_glyph_string_reverse_range (glyphs, 0, glyphs->num_glyphs);
 }
 
-/* FIXME: This is very ugly. We are out of room for vfuncs, so we can't
- * easily add api to get is_hinted and the matrix from the PangoFcFont.
- *
- * Keep in sync with pangofc-font.h!
- */
-struct _PangoFcFont
-{
-  PangoFont parent_instance;
-
-  gpointer font_pattern;          /* fully resolved pattern */
-  PangoFontMap *fontmap;            /* associated map */
-  gpointer priv;                    /* used internally */
-  PangoMatrix matrix;               /* used internally */
-  PangoFontDescription *description;
-
-  GSList *metrics_by_lang;
-
-  guint is_hinted : 1;
-  guint is_transformed : 1;
-};
-typedef struct _PangoFcFont PangoFcFont;
-
-#define PANGO_IS_FC_FONT(obj) \
-  g_type_is_a (((GTypeInstance*)obj)->g_class->g_type, \
-               g_type_from_name ("PangoFcFont"))
-
 /**
  * pango_shape_with_flags:
  * @item_text: valid UTF-8 text to shape
@@ -320,31 +295,29 @@ pango_shape_with_flags (const gchar         *item_text,
 
   if (flags & PANGO_SHAPE_ROUND_POSITIONS)
     {
-      if (PANGO_IS_FC_FONT (analysis->font))
+      if (pango_font_is_hinted (analysis->font))
         {
-          PangoFcFont *fc_font = (PangoFcFont *)analysis->font;
-          if (fc_font->is_hinted)
+          double x_scale_inv, y_scale_inv;
+          double x_scale, y_scale;
+
+          pango_font_get_scale_factors (analysis->font, &x_scale_inv, &y_scale_inv);
+
+          if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity))
             {
-              double x_scale_inv, y_scale_inv;
-              double x_scale, y_scale;
+              x_scale_inv = -x_scale_inv;
+              y_scale_inv = -y_scale_inv;
+            }
 
-              x_scale_inv = y_scale_inv = 1.0;
-              pango_matrix_get_font_scale_factors (&fc_font->matrix, &x_scale_inv, &y_scale_inv);
-              if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity))
-                {
-                  x_scale_inv = -x_scale_inv;
-                  y_scale_inv = -y_scale_inv;
-                }
-              x_scale = 1. / x_scale_inv;
-              y_scale = 1. / y_scale_inv;
+          x_scale = 1.0 / x_scale_inv;
+          y_scale = 1.0 / y_scale_inv;
 
-              if (x_scale == 1.0 && y_scale == 1.0)
-                {
-                  for (i = 0; i < glyphs->num_glyphs; i++)
-                    glyphs->glyphs[i].geometry.width = PANGO_UNITS_ROUND (glyphs->glyphs[i].geometry.width);
-                }
-              else
-                {
+          if (x_scale == 1.0 && y_scale == 1.0)
+            {
+              for (i = 0; i < glyphs->num_glyphs; i++)
+                glyphs->glyphs[i].geometry.width = PANGO_UNITS_ROUND (glyphs->glyphs[i].geometry.width);
+            }
+          else
+            {
     #if 0
                   if (PANGO_GRAVITY_IS_VERTICAL (analysis->gravity))
                     {
@@ -357,16 +330,15 @@ pango_shape_with_flags (const gchar         *item_text,
     #define HINT(value, scale_inv, scale) (PANGO_UNITS_ROUND ((int) ((value) * scale)) * scale_inv)
     #define HINT_X(value) HINT ((value), x_scale, x_scale_inv)
     #define HINT_Y(value) HINT ((value), y_scale, y_scale_inv)
-                  for (i = 0; i < glyphs->num_glyphs; i++)
-                    {
-                      glyphs->glyphs[i].geometry.width    = HINT_X (glyphs->glyphs[i].geometry.width);
-                      glyphs->glyphs[i].geometry.x_offset = HINT_X (glyphs->glyphs[i].geometry.x_offset);
-                      glyphs->glyphs[i].geometry.y_offset = HINT_Y (glyphs->glyphs[i].geometry.y_offset);
-                    }
+              for (i = 0; i < glyphs->num_glyphs; i++)
+                {
+                  glyphs->glyphs[i].geometry.width    = HINT_X (glyphs->glyphs[i].geometry.width);
+                  glyphs->glyphs[i].geometry.x_offset = HINT_X (glyphs->glyphs[i].geometry.x_offset);
+                  glyphs->glyphs[i].geometry.y_offset = HINT_Y (glyphs->glyphs[i].geometry.y_offset);
+                }
     #undef HINT_Y
     #undef HINT_X
     #undef HINT
-                }
             }
         }
     }


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