Commit c1f9c9f8 authored by Matthias Clasen's avatar Matthias Clasen
Browse files

Add flags to serialize and deserialize

This gives us some room to add more things
to these apis, e.g. a way to store not just
the input part of a layout, but also the
output.
parent 12a46f67
......@@ -351,8 +351,28 @@ GSList * pango_layout_get_lines (PangoLayout *layout);
PANGO_AVAILABLE_IN_1_16
GSList * pango_layout_get_lines_readonly (PangoLayout *layout);
/**
* PangoLayoutSerializeFlags:
* @PANGO_LAYOUT_SERIALIZE_DEFAULT: Default behavior
*
* Flags that influence the behavior of [method@Pango.Layout.serialize].
*
* New members may be added to this enumeration over time.
*/
typedef enum {
PANGO_LAYOUT_SERIALIZE_DEFAULT = 0,
} PangoLayoutSerializeFlags;
PANGO_AVAILABLE_IN_1_50
GBytes * pango_layout_serialize (PangoLayout *layout,
PangoLayoutSerializeFlags flags);
PANGO_AVAILABLE_IN_1_50
GBytes * pango_layout_serialize (PangoLayout *layout);
gboolean pango_layout_write_to_file (PangoLayout *layout,
PangoLayoutSerializeFlags flags,
const char *filename,
GError **error);
#define PANGO_LAYOUT_DESERIALIZE_ERROR (pango_layout_deserialize_error_quark ())
......@@ -379,16 +399,24 @@ typedef enum {
PANGO_AVAILABLE_IN_1_50
GQuark pango_layout_deserialize_error_quark (void);
/**
* PangoLayoutDeserializeFlags:
* @PANGO_LAYOUT_DESERIALIZE_DEFAULT: Default behavior
*
* Flags that influence the behavior of [method@Pango.Layout.deserialize].
*
* New members may be added to this enumeration over time.
*/
typedef enum {
PANGO_LAYOUT_DESERIALIZE_DEFAULT = 0,
} PangoLayoutDeserializeFlags;
PANGO_AVAILABLE_IN_1_50
PangoLayout * pango_layout_deserialize (PangoContext *context,
GBytes *bytes,
PangoLayoutDeserializeFlags flags,
GError **error);
PANGO_AVAILABLE_IN_1_50
gboolean pango_layout_write_to_file (PangoLayout *layout,
const char *filename,
GError **error);
#define PANGO_TYPE_LAYOUT_LINE (pango_layout_line_get_type ())
......
......@@ -830,6 +830,7 @@ fail:
/**
* pango_layout_serialize:
* @layout: a `PangoLayout`
* @flags: `PangoLayoutSerializeFlags`
*
* Serializes the @layout for later deserialization via [method@Pango.Layout.deserialize].
*
......@@ -845,13 +846,16 @@ fail:
* Since: 1.50
*/
GBytes *
pango_layout_serialize (PangoLayout *layout)
pango_layout_serialize (PangoLayout *layout,
PangoLayoutSerializeFlags flags)
{
JsonGenerator *generator;
JsonNode *node;
char *data;
gsize size;
g_return_val_if_fail (PANGO_IS_LAYOUT (layout), NULL);
node = layout_to_json (layout);
generator = json_generator_new ();
......@@ -870,6 +874,7 @@ pango_layout_serialize (PangoLayout *layout)
/**
* pango_layout_write_to_file:
* @layout: a `PangoLayout`
* @flags: `PangoLayoutSerializeFlags`
* @filename: (type filename): the file to save it to
* @error: Return location for a potential error
*
......@@ -886,9 +891,10 @@ pango_layout_serialize (PangoLayout *layout)
* Since: 1.50
*/
gboolean
pango_layout_write_to_file (PangoLayout *layout,
const char *filename,
GError **error)
pango_layout_write_to_file (PangoLayout *layout,
PangoLayoutSerializeFlags flags,
const char *filename,
GError **error)
{
GBytes *bytes;
gboolean result;
......@@ -897,7 +903,7 @@ pango_layout_write_to_file (PangoLayout *layout,
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
bytes = pango_layout_serialize (layout);
bytes = pango_layout_serialize (layout, flags);
result = g_file_set_contents (filename,
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
......@@ -911,6 +917,7 @@ pango_layout_write_to_file (PangoLayout *layout,
/**
* pango_layout_deserialize:
* @context: a `PangoContext`
* @flags: `PangoLayoutDeserializeFlags`
* @bytes: the bytes containing the data
* @error: return location for an error
*
......@@ -923,14 +930,17 @@ pango_layout_write_to_file (PangoLayout *layout,
* Since: 1.50
*/
PangoLayout *
pango_layout_deserialize (PangoContext *context,
GBytes *bytes,
GError **error)
pango_layout_deserialize (PangoContext *context,
GBytes *bytes,
PangoLayoutDeserializeFlags flags,
GError **error)
{
JsonParser *parser;
JsonNode *node;
PangoLayout *layout;
g_return_val_if_fail (PANGO_IS_CONTEXT (context), NULL);
parser = json_parser_new_immutable ();
if (!json_parser_load_from_data (parser,
g_bytes_get_data (bytes, NULL),
......
......@@ -236,7 +236,7 @@ test_file (const char *filename, GString *string)
bytes = g_bytes_new_take (contents, length);
layout = pango_layout_deserialize (context, bytes, &error);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
g_assert_no_error (error);
g_bytes_unref (bytes);
......
......@@ -142,7 +142,7 @@ test_serialize_layout_minimal (void)
bytes = g_bytes_new_static (test, -1);
layout = pango_layout_deserialize (context, bytes, &error);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
g_assert_no_error (error);
g_assert_true (PANGO_IS_LAYOUT (layout));
g_assert_cmpstr (pango_layout_get_text (layout), ==, "Almost nothing");
......@@ -152,7 +152,7 @@ test_serialize_layout_minimal (void)
g_assert_cmpint (pango_layout_get_alignment (layout), ==, PANGO_ALIGN_LEFT);
g_assert_cmpint (pango_layout_get_width (layout), ==, -1);
out_bytes = pango_layout_serialize (layout);
out_bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_DEFAULT);
str = g_bytes_get_data (out_bytes, NULL);
g_assert_cmpstr (str, ==, test);
......@@ -215,7 +215,7 @@ test_serialize_layout_valid (void)
bytes = g_bytes_new_static (test, -1);
layout = pango_layout_deserialize (context, bytes, &error);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
g_assert_no_error (error);
g_assert_true (PANGO_IS_LAYOUT (layout));
g_assert_cmpstr (pango_layout_get_text (layout), ==, "Some fun with layouts!");
......@@ -230,7 +230,7 @@ test_serialize_layout_valid (void)
g_assert_cmpint (pango_layout_get_width (layout), ==, 350000);
g_assert_cmpfloat_with_epsilon (pango_layout_get_line_spacing (layout), 1.5, 0.0001);
out_bytes = pango_layout_serialize (layout);
out_bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_DEFAULT);
str = g_bytes_get_data (out_bytes, NULL);
g_assert_cmpstr (str, ==, test);
......@@ -299,7 +299,7 @@ test_serialize_layout_invalid (void)
GError *error = NULL;
bytes = g_bytes_new_static (test[i].json, -1);
layout = pango_layout_deserialize (context, bytes, &error);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
g_assert_null (layout);
g_assert_error (error, PANGO_LAYOUT_DESERIALIZE_ERROR, test[i].expected_error);
g_bytes_unref (bytes);
......
......@@ -115,7 +115,7 @@ make_layout(PangoContext *context,
if (!g_file_get_contents (file_arg, &text, &len, &error))
fail ("%s\n", error->message);
bytes = g_bytes_new_take (text, size);
layout = pango_layout_deserialize (context, bytes, &error);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_DEFAULT, &error);
if (!layout)
fail ("%s\n", error->message);
g_bytes_unref (bytes);
......
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