[gtk/color-glyph-fixes: 5/6] gsk: Use harfbuzz for color fonts




commit a18fbd50eabf50489a20ba5f1496f1918c92af93
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 31 15:22:14 2021 -0400

    gsk: Use harfbuzz for color fonts
    
    harfbuzz has all the information we need, so we
    can avoid poking directly at freetype apis.

 gsk/gskrendernodeimpl.c | 73 ++++++++++++++++++++++---------------------------
 1 file changed, 33 insertions(+), 40 deletions(-)
---
diff --git a/gsk/gskrendernodeimpl.c b/gsk/gskrendernodeimpl.c
index c44ce88784..fbb0b0d4cd 100644
--- a/gsk/gskrendernodeimpl.c
+++ b/gsk/gskrendernodeimpl.c
@@ -30,7 +30,7 @@
 #include "gdk/gdktextureprivate.h"
 #include "gdk/gdk-private.h"
 
-#include <cairo-ft.h>
+#include <hb-ot.h>
 
 static inline void
 gsk_cairo_rectangle (cairo_t               *cr,
@@ -4425,44 +4425,47 @@ gsk_text_node_diff (GskRenderNode  *node1,
 }
 
 static gboolean
-font_has_color_glyphs (const PangoFont *font)
+font_has_color_glyphs (PangoFont *font)
 {
-  cairo_scaled_font_t *scaled_font;
-  gboolean has_color = FALSE;
+  hb_face_t *face = hb_font_get_face (pango_font_get_hb_font (font));
 
-  scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *)font);
-  if (cairo_scaled_font_get_type (scaled_font) == CAIRO_FONT_TYPE_FT)
-    {
-      FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
-      has_color = (FT_HAS_COLOR (ft_face) != 0);
-      cairo_ft_scaled_font_unlock_face (scaled_font);
-    }
-
-  return has_color;
+  return hb_ot_color_has_layers (face) ||
+         hb_ot_color_has_png (face) ||
+         hb_ot_color_has_svg (face);
 }
 
 static gboolean
-glyph_has_color (FT_Face face,
-                 guint   glyph)
+glyph_has_color (PangoFont *font,
+                 guint      glyph)
 {
-  FT_Error error;
+  hb_font_t *hb_font = pango_font_get_hb_font (font);
+  hb_face_t *face = hb_font_get_face (hb_font);
+  hb_blob_t *blob;
 
-  error = FT_Load_Glyph (face, glyph, FT_LOAD_COLOR);
-  if (error != 0)
-    return FALSE;
+  if (hb_ot_color_glyph_get_layers (face, glyph, 0, NULL, NULL) > 0)
+    return TRUE;
 
-  error = FT_Render_Glyph (face->glyph, FT_RENDER_MODE_NORMAL);
-  if (error != 0)
-    return FALSE;
+  blob = hb_ot_color_glyph_reference_png (hb_font, glyph);
+  if (blob)
+    {
+      guint length = hb_blob_get_length (blob);
+      hb_blob_destroy (blob);
+      return length > 0;
+    }
 
-  if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA)
-    return TRUE;
+  blob = hb_ot_color_glyph_reference_svg (face, glyph);
+  if (blob)
+    {
+      guint length = hb_blob_get_length (blob);
+      hb_blob_destroy (blob);
+      return length > 0;
+    }
 
   return FALSE;
 }
 
 static GHashTable *
-ensure_color_glyph_cache (const PangoFont *font)
+ensure_color_glyph_cache (PangoFont *font)
 {
   GHashTable *cache;
 
@@ -4502,22 +4505,15 @@ insert_color_glyph_cache (GHashTable *cache,
 }
 
 static void
-mark_color_glyphs (const PangoFont *font,
-                   PangoGlyphInfo  *glyphs,
-                   int              num_glyphs)
+mark_color_glyphs (PangoFont      *font,
+                   PangoGlyphInfo *glyphs,
+                   int             num_glyphs)
 {
-  cairo_scaled_font_t *scaled_font;
-  FT_Face ft_face;
   GHashTable *cache;
 
-  scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *)font);
-  if (cairo_scaled_font_get_type (scaled_font) != CAIRO_FONT_TYPE_FT)
+  if (!font_has_color_glyphs (font))
     return;
 
-  ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
-  if (!FT_HAS_COLOR (ft_face))
-    goto out;
-
   cache = ensure_color_glyph_cache (font);
 
   for (int i = 0; i < num_glyphs; i++)
@@ -4526,16 +4522,13 @@ mark_color_glyphs (const PangoFont *font,
 
       if (!lookup_color_glyph_cache (cache, glyphs[i].glyph, &has_color))
         {
-          has_color = glyph_has_color (ft_face, glyphs[i].glyph);
+          has_color = glyph_has_color (font, glyphs[i].glyph);
           insert_color_glyph_cache (cache, glyphs[i].glyph, has_color);
         }
 
       if (has_color)
         GLYPH_SET_COLOR (&glyphs[i]);
     }
-
-out:
-  cairo_ft_scaled_font_unlock_face (scaled_font);
 }
 
 /**


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