diff --git a/src/hb-buffer-serialize.cc b/src/hb-buffer-serialize.cc index bac43a33a..d9e9e7bc0 100644 --- a/src/hb-buffer-serialize.cc +++ b/src/hb-buffer-serialize.cc @@ -592,6 +592,35 @@ hb_buffer_serialize_unicode (hb_buffer_t *buffer, } } + +static unsigned int +_hb_buffer_serialize_invalid (hb_buffer_t *buffer, + unsigned int start, + unsigned int end, + char *buf, + unsigned int buf_size, + unsigned int *buf_consumed, + hb_buffer_serialize_format_t format, + hb_buffer_serialize_flags_t flags) +{ + unsigned int sconsumed; + if (!buf_consumed) + buf_consumed = &sconsumed; + if (buf_size < 3) + return 0; + if (format == HB_BUFFER_SERIALIZE_FORMAT_JSON) { + *buf++ = '['; + *buf++ = ']'; + *buf = '\0'; + } else if (format == HB_BUFFER_SERIALIZE_FORMAT_TEXT) { + *buf++ = '!'; + *buf++ = '!'; + *buf = '\0'; + } + *buf_consumed = 2; + return 0; +} + /** * hb_buffer_serialize: * @buffer: an #hb_buffer_t buffer. @@ -629,12 +658,22 @@ hb_buffer_serialize (hb_buffer_t *buffer, hb_buffer_serialize_format_t format, hb_buffer_serialize_flags_t flags) { - if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) - return hb_buffer_serialize_glyphs(buffer, start, end, buf, buf_size, - buf_consumed, font, format, flags); - else - return hb_buffer_serialize_unicode(buffer, start, end, buf, buf_size, + switch (buffer->content_type) + { + + case HB_BUFFER_CONTENT_TYPE_GLYPHS: + return hb_buffer_serialize_glyphs(buffer, start, end, buf, buf_size, + buf_consumed, font, format, flags); + + case HB_BUFFER_CONTENT_TYPE_UNICODE: + return hb_buffer_serialize_unicode(buffer, start, end, buf, buf_size, buf_consumed, format, flags); + + case HB_BUFFER_CONTENT_TYPE_INVALID: + default: + return _hb_buffer_serialize_invalid(buffer, start, end, buf, buf_size, + buf_consumed, format, flags); + } } static bool diff --git a/test/api/test-buffer.c b/test/api/test-buffer.c index b3cd1bf40..6904b36bb 100644 --- a/test/api/test-buffer.c +++ b/test/api/test-buffer.c @@ -898,14 +898,28 @@ test_buffer_serialize_deserialize (void) num_glyphs = hb_buffer_get_length (b); g_assert_cmpint (num_glyphs, ==, test->num_items); - hb_buffer_serialize_unicode(b, 0, num_glyphs, round_trip, - sizeof(round_trip), &consumed, test->format, - HB_BUFFER_SERIALIZE_FLAG_DEFAULT); + hb_buffer_serialize_unicode (b, 0, num_glyphs, round_trip, + sizeof(round_trip), &consumed, test->format, + HB_BUFFER_SERIALIZE_FLAG_DEFAULT); g_assert_cmpstr (round_trip, ==, test->contents); hb_buffer_destroy (b); } + + char test[1024]; + unsigned int num_glyphs, consumed; + hb_buffer_t *indeterminate = hb_buffer_get_empty (); + hb_buffer_serialize (indeterminate, 0, hb_buffer_get_length (indeterminate), + test, sizeof(test), &consumed, NULL, HB_BUFFER_SERIALIZE_FORMAT_JSON, + HB_BUFFER_SERIALIZE_FLAG_DEFAULT); + g_assert_cmpstr ( test, ==, "[]"); + + hb_buffer_serialize (indeterminate, 0, hb_buffer_get_length (indeterminate), + test, sizeof(test), &consumed, NULL, HB_BUFFER_SERIALIZE_FORMAT_TEXT, + HB_BUFFER_SERIALIZE_FLAG_DEFAULT); + g_assert_cmpstr ( test, ==, "!!"); + } int