[pango/simple-fontmap: 4/4] pangocairo: handle PangoHbFont




commit 62c31b223d853039a38d956ea2af54e98f2df3cd
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Aug 31 07:48:14 2021 -0400

    pangocairo: handle PangoHbFont
    
    Towards working rendering.
    
    Basic rendering is working fine with this.
    Missing:
    - matrix / gravity
    - resolution
    - hexboxes

 pango/pangocairo-fc.h     |  3 ++
 pango/pangocairo-fcfont.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++-
 pango/pangocairo-font.c   |  9 ++++-
 3 files changed, 96 insertions(+), 2 deletions(-)
---
diff --git a/pango/pangocairo-fc.h b/pango/pangocairo-fc.h
index 36529569..9c1956d3 100644
--- a/pango/pangocairo-fc.h
+++ b/pango/pangocairo-fc.h
@@ -42,6 +42,9 @@ typedef struct _PangoCairoFcFontMap PangoCairoFcFontMap;
 PANGO_AVAILABLE_IN_ALL
 GType pango_cairo_fc_font_map_get_type (void) G_GNUC_CONST;
 
+PANGO_AVAILABLE_IN_1_50
+cairo_scaled_font_t * pango_cairo_hb_font_get_scaled_font (PangoFont *font);
+
 G_END_DECLS
 
 #endif /* __PANGOCAIRO_FC_H__ */
diff --git a/pango/pangocairo-fcfont.c b/pango/pangocairo-fcfont.c
index 0019f83a..b5ef0fee 100644
--- a/pango/pangocairo-fcfont.c
+++ b/pango/pangocairo-fcfont.c
@@ -32,8 +32,10 @@
 #include "pangocairo-fc-private.h"
 #include "pangofc-private.h"
 #include "pango-impl-utils.h"
+#include "pango-hbfont-private.h"
+#include "pango-hbface-private.h"
 
-#include <hb-ot.h>
+#include <hb-ft.h>
 #include <freetype/ftmm.h>
 
 #define PANGO_TYPE_CAIRO_FC_FONT           (pango_cairo_fc_font_get_type ())
@@ -264,3 +266,85 @@ _pango_cairo_fc_font_new (PangoCairoFcFontMap *cffontmap,
 
   return (PangoFcFont *) cffont;
 }
+
+static FT_Library ft_library;
+
+static cairo_scaled_font_t *
+create_scaled_font (PangoHbFont *font)
+{
+  FT_Face ft_face;
+  cairo_font_face_t *cairo_face;
+  int size;
+
+  unsigned int blob_length;
+  const char *blob_data = hb_blob_get_data (font->face->blob, &blob_length);
+
+  if (!ft_library)
+    FT_Init_FreeType (&ft_library);
+
+
+  if (FT_New_Memory_Face (ft_library,
+                          (const FT_Byte *) blob_data,
+                          blob_length,
+                          font->face->index,
+                          &ft_face))
+    g_error ("FT_New_Memory_Face fail");
+
+  hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (font));
+  unsigned int num_coords;
+  const int *coords = hb_font_get_var_coords_normalized (hb_font, &num_coords);
+  if (num_coords)
+    {
+      FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
+      if (ft_coords)
+        {
+          for (unsigned int i = 0; i < num_coords; i++)
+            ft_coords[i] = coords[i] << 2;
+          FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
+          free (ft_coords);
+        }
+    }
+
+  cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, FT_LOAD_NO_HINTING | FT_LOAD_COLOR);
+
+  cairo_matrix_t ctm, font_matrix;
+  cairo_font_options_t *font_options;
+
+  size = pango_font_description_get_size (font->description);
+
+  cairo_matrix_init_identity (&ctm);
+  cairo_matrix_init_scale (&font_matrix,
+                           size * (96. / 72.) / PANGO_SCALE,
+                           size * (96. / 72) / PANGO_SCALE);
+  font_options = cairo_font_options_create ();
+  cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
+  cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
+
+  cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
+                                                               &font_matrix,
+                                                               &ctm,
+                                                               font_options);
+  cairo_font_options_destroy (font_options);
+  cairo_font_face_destroy (cairo_face);
+
+  return scaled_font;
+}
+
+cairo_scaled_font_t *
+pango_cairo_hb_font_get_scaled_font (PangoFont *font)
+{
+  cairo_scaled_font_t *scaled_font;
+
+  scaled_font = g_object_get_data (G_OBJECT (font), "pango-cairo-scaled-font");
+  if (scaled_font)
+    goto done;
+
+  scaled_font = create_scaled_font (PANGO_HB_FONT (font));
+
+  g_object_set_data_full (G_OBJECT (font), "pango-cairo-scaled_font",
+                          scaled_font, (GDestroyNotify)cairo_scaled_font_destroy);
+
+done:
+  return scaled_font;
+}
+
diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c
index 69c375c3..0aff0407 100644
--- a/pango/pangocairo-font.c
+++ b/pango/pangocairo-font.c
@@ -28,6 +28,8 @@
 #include "pangocairo-private.h"
 #include "pango-font-private.h"
 #include "pango-impl-utils.h"
+#include "pango-hbfont.h"
+#include "pangocairo-fc.h"
 
 #define PANGO_CAIRO_FONT_PRIVATE(font)         \
   ((PangoCairoFontPrivate *)                   \
@@ -175,7 +177,12 @@ gboolean
 _pango_cairo_font_install (PangoFont *font,
                           cairo_t   *cr)
 {
-  cairo_scaled_font_t *scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *)font);
+  cairo_scaled_font_t *scaled_font;
+
+  if (PANGO_IS_HB_FONT (font))
+    scaled_font = pango_cairo_hb_font_get_scaled_font (font);
+  else
+    scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *)font);
 
   if (G_UNLIKELY (scaled_font == NULL || cairo_scaled_font_status (scaled_font) != CAIRO_STATUS_SUCCESS))
     return FALSE;


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