[pango/serializer-rewrite: 1/2] Add pango_font_deserialize




commit 73f27640b5292dd758e3daf5c88180b8e54e2804
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Dec 2 19:51:14 2021 -0500

    Add pango_font_deserialize
    
    This will make the font serialization useful
    in GTK, where we serialize fonts in node files.

 pango/pango-font.h    |  5 +++
 pango/serializer.c    | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/testserialize.c | 15 ++++++++-
 3 files changed, 103 insertions(+), 1 deletion(-)
---
diff --git a/pango/pango-font.h b/pango/pango-font.h
index bfe4bc31..f4e619a1 100644
--- a/pango/pango-font.h
+++ b/pango/pango-font.h
@@ -637,6 +637,11 @@ PangoLanguage **      pango_font_get_languages     (PangoFont        *font);
 PANGO_AVAILABLE_IN_1_50
 GBytes *              pango_font_serialize         (PangoFont        *font);
 
+PANGO_AVAILABLE_IN_1_50
+PangoFont *           pango_font_deserialize       (PangoContext     *context,
+                                                    GBytes           *bytes,
+                                                    GError          **error);
+
 /**
  * PANGO_GLYPH_EMPTY:
  *
diff --git a/pango/serializer.c b/pango/serializer.c
index a5c751fd..86e6b004 100644
--- a/pango/serializer.c
+++ b/pango/serializer.c
@@ -1320,6 +1320,51 @@ json_parser_fill_layout (GtkJsonParser               *parser,
   gtk_json_parser_end (parser);
 }
 
+enum {
+  FONT_DESCRIPTION,
+  FONT_CHECKSUM,
+  FONT_VARIATIONS,
+  FONT_FEATURES,
+  FONT_MATRIX
+};
+
+static const char *font_members[] = {
+  "description",
+  "checksum",
+  "variations",
+  "features",
+  "matrix",
+  NULL
+};
+
+static PangoFont *
+json_parser_load_font (GtkJsonParser  *parser,
+                       PangoContext   *context,
+                       GError        **error)
+{
+  PangoFont *font = NULL;
+
+  gtk_json_parser_start_object (parser);
+
+  switch (gtk_json_parser_select_member (parser, font_members))
+    {
+    case FONT_DESCRIPTION:
+      {
+        PangoFontDescription *desc = parser_get_font_description (parser);
+        font = pango_context_load_font (context, desc);
+        pango_font_description_free (desc);
+      }
+      break;
+
+    default:
+      break;
+    }
+
+  gtk_json_parser_end (parser);
+
+  return font;
+}
+
 /* }}} */
 /* {{{ Public API */
 
@@ -1421,6 +1466,10 @@ pango_layout_write_to_file (PangoLayout                *layout,
  *
  * For a discussion of the supported format, see that function.
  *
+ * Note: to verify that the returned layout is identical to
+ * the one that was serialized, you can compare @bytes to the
+ * result of serializing the layout again.
+ *
  * Returns: (nullable) (transfer full): a new `PangoLayout`
  *
  * Since: 1.50
@@ -1491,6 +1540,41 @@ pango_font_serialize (PangoFont *font)
   return g_bytes_new_take (data, size);
 }
 
+/**
+ * pango_font_deserialize:
+ * @context: a `PangoContext`
+ * @bytes: the bytes containing the data
+ * @error: return location for an error
+ *
+ * Loads data previously created via [method Pango Font.serialize].
+ *
+ * For a discussion of the supported format, see that function.
+ *
+ * Note: to verify that the returned font is identical to
+ * the one that was serialized, you can compare @bytes to the
+ * result of serializing the font again.
+ *
+ * Returns: (nullable) (transfer full): a new `PangoFont`
+ *
+ * Since: 1.50
+ */
+PangoFont *
+pango_font_deserialize (PangoContext  *context,
+                        GBytes        *bytes,
+                        GError       **error)
+{
+  PangoFont *font;
+  GtkJsonParser *parser;
+
+  g_return_val_if_fail (PANGO_IS_CONTEXT (context), NULL);
+
+  parser = gtk_json_parser_new_for_bytes (bytes);
+  font = json_parser_load_font (parser, context, error);
+  gtk_json_parser_free (parser);
+
+  return font;
+}
+
 /* }}} */
 
 /* vim:set foldmethod=marker expandtab: */
diff --git a/tests/testserialize.c b/tests/testserialize.c
index ed571b69..e480da31 100644
--- a/tests/testserialize.c
+++ b/tests/testserialize.c
@@ -135,7 +135,10 @@ test_serialize_font (void)
   PangoContext *context;
   PangoFontDescription *desc;
   PangoFont *font;
+  PangoFont *font2;
   GBytes *bytes;
+  GBytes *bytes2;
+  GError *error = NULL;
   const char *expected =
     "{\n"
     "  \"description\" : \"Cantarell 20 @wght=600\",\n"
@@ -159,10 +162,20 @@ test_serialize_font (void)
 
   bytes = pango_font_serialize (font);
   g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, expected);
-  g_bytes_unref (bytes);
+
+  font2 = pango_font_deserialize (context, bytes, &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (font2);
+
+  bytes2 = pango_font_serialize (font2);
+  g_assert_true (g_bytes_equal (bytes2, bytes));
 
   g_object_unref (font);
+  g_object_unref (font2);
   pango_font_description_free (desc);
+
+  g_bytes_unref (bytes);
+  g_bytes_unref (bytes2);
   g_object_unref (context);
 }
 


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