[pango/serialization-improvements: 15/19] Add pango_font_serialize




commit 580ad90e9737b13a941f981c9fa2b2d8e5fae120
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Nov 24 12:26:50 2021 -0500

    Add pango_font_serialize
    
    Another debug api. This function produces a serialization
    of a font that is enough to uniquely identify the font.
    This is more detailed than what pango_font_describe
    creates.

 pango/pango-font.h |  3 +++
 pango/serializer.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
---
diff --git a/pango/pango-font.h b/pango/pango-font.h
index c56fb792..bfe4bc31 100644
--- a/pango/pango-font.h
+++ b/pango/pango-font.h
@@ -634,6 +634,9 @@ hb_font_t *           pango_font_get_hb_font       (PangoFont        *font);
 PANGO_AVAILABLE_IN_1_50
 PangoLanguage **      pango_font_get_languages     (PangoFont        *font);
 
+PANGO_AVAILABLE_IN_1_50
+GBytes *              pango_font_serialize         (PangoFont        *font);
+
 /**
  * PANGO_GLYPH_EMPTY:
  *
diff --git a/pango/serializer.c b/pango/serializer.c
index a4383aed..5c2bdf77 100644
--- a/pango/serializer.c
+++ b/pango/serializer.c
@@ -793,6 +793,20 @@ layout_to_json (PangoLayout               *layout,
   return root;
 }
 
+static JsonNode *
+font_to_json (PangoFont *font)
+{
+  JsonBuilder *builder;
+  JsonNode *root;
+
+  builder = json_builder_new_immutable ();
+  add_font (builder, font);
+  root = json_builder_get_root (builder);
+  g_object_unref (builder);
+
+  return root;
+}
+
 /* }}} */
 /* {{{ Deserialization */
 
@@ -1569,6 +1583,47 @@ pango_layout_deserialize (PangoContext                 *context,
   return layout;
 }
 
+/**
+ * pango_font_serialize:
+ * @font: a `PangoFont`
+ *
+ * Serializes the @font in a way that can be uniquely identified.
+ *
+ * There are no guarantees about the format of the output across different
+ * versions of Pango.
+ *
+ * The intended use of this function is testing, benchmarking and debugging.
+ * The format is not meant as a permanent storage format.
+ *
+ * Returns: a `GBytes` containing the serialized form of @font
+ *
+ * Since: 1.50
+ */
+GBytes *
+pango_font_serialize (PangoFont *font)
+{
+  JsonGenerator *generator;
+  JsonNode *node;
+  char *data;
+  gsize size;
+
+  g_return_val_if_fail (PANGO_IS_FONT (font), NULL);
+
+  node = font_to_json (font);
+
+  generator = json_generator_new ();
+  json_generator_set_pretty (generator, TRUE);
+  json_generator_set_indent (generator, 2);
+
+  json_generator_set_root (generator, node);
+  data = json_generator_to_data (generator, &size);
+
+  json_node_free (node);
+  g_object_unref (generator);
+
+  return g_bytes_new_take (data, size);
+}
+
 /* }}} */
 
 /* vim:set foldmethod=marker expandtab: */


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