Commit 73f27640 authored by Matthias Clasen's avatar Matthias Clasen
Browse files

Add pango_font_deserialize

This will make the font serialization useful
in GTK, where we serialize fonts in node files.
parent 2c0aa8b8
......@@ -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:
*
......
......@@ -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: */
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment