michael@0: /* michael@0: * Copyright © 2012,2013 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-buffer-private.hh" michael@0: michael@0: michael@0: static const char *serialize_formats[] = { michael@0: "text", michael@0: "json", michael@0: NULL michael@0: }; michael@0: michael@0: /** michael@0: * hb_buffer_serialize_list_formats: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char ** michael@0: hb_buffer_serialize_list_formats (void) michael@0: { michael@0: return serialize_formats; michael@0: } michael@0: michael@0: /** michael@0: * hb_buffer_serialize_format_from_string: michael@0: * @str: michael@0: * @len: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_buffer_serialize_format_t michael@0: hb_buffer_serialize_format_from_string (const char *str, int len) michael@0: { michael@0: /* Upper-case it. */ michael@0: return (hb_buffer_serialize_format_t) (hb_tag_from_string (str, len) & ~0x20202020); michael@0: } michael@0: michael@0: /** michael@0: * hb_buffer_serialize_format_to_string: michael@0: * @format: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: const char * michael@0: hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format) michael@0: { michael@0: switch (format) michael@0: { michael@0: case HB_BUFFER_SERIALIZE_FORMAT_TEXT: return serialize_formats[0]; michael@0: case HB_BUFFER_SERIALIZE_FORMAT_JSON: return serialize_formats[1]; michael@0: default: michael@0: case HB_BUFFER_SERIALIZE_FORMAT_INVALID: return NULL; michael@0: } michael@0: } michael@0: michael@0: static unsigned int michael@0: _hb_buffer_serialize_glyphs_json (hb_buffer_t *buffer, michael@0: unsigned int start, michael@0: unsigned int end, michael@0: char *buf, michael@0: unsigned int buf_size, michael@0: unsigned int *buf_consumed, michael@0: hb_font_t *font, michael@0: hb_buffer_serialize_flags_t flags) michael@0: { michael@0: hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); michael@0: hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL); michael@0: michael@0: *buf_consumed = 0; michael@0: for (unsigned int i = start; i < end; i++) michael@0: { michael@0: char b[1024]; michael@0: char *p = b; michael@0: michael@0: /* In the following code, we know b is large enough that no overflow can happen. */ michael@0: michael@0: #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END michael@0: michael@0: if (i) michael@0: *p++ = ','; michael@0: michael@0: *p++ = '{'; michael@0: michael@0: APPEND ("\"g\":"); michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES)) michael@0: { michael@0: char g[128]; michael@0: hb_font_glyph_to_string (font, info[i].codepoint, g, sizeof (g)); michael@0: *p++ = '"'; michael@0: for (char *q = g; *q; q++) { michael@0: if (*q == '"') michael@0: *p++ = '\\'; michael@0: *p++ = *q; michael@0: } michael@0: *p++ = '"'; michael@0: } michael@0: else michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint)); michael@0: michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) { michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"cl\":%u", info[i].cluster)); michael@0: } michael@0: michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS)) michael@0: { michael@0: p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"dx\":%d,\"dy\":%d", michael@0: pos[i].x_offset, pos[i].y_offset); michael@0: p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"ax\":%d,\"ay\":%d", michael@0: pos[i].x_advance, pos[i].y_advance); michael@0: } michael@0: michael@0: *p++ = '}'; michael@0: michael@0: unsigned int l = p - b; michael@0: if (buf_size > l) michael@0: { michael@0: memcpy (buf, b, l); michael@0: buf += l; michael@0: buf_size -= l; michael@0: *buf_consumed += l; michael@0: *buf = '\0'; michael@0: } else michael@0: return i - start; michael@0: } michael@0: michael@0: return end - start; michael@0: } michael@0: michael@0: static unsigned int michael@0: _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer, michael@0: unsigned int start, michael@0: unsigned int end, michael@0: char *buf, michael@0: unsigned int buf_size, michael@0: unsigned int *buf_consumed, michael@0: hb_font_t *font, michael@0: hb_buffer_serialize_flags_t flags) michael@0: { michael@0: hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); michael@0: hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL); michael@0: michael@0: *buf_consumed = 0; michael@0: for (unsigned int i = start; i < end; i++) michael@0: { michael@0: char b[1024]; michael@0: char *p = b; michael@0: michael@0: /* In the following code, we know b is large enough that no overflow can happen. */ michael@0: michael@0: if (i) michael@0: *p++ = '|'; michael@0: michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES)) michael@0: { michael@0: hb_font_glyph_to_string (font, info[i].codepoint, p, 128); michael@0: p += strlen (p); michael@0: } michael@0: else michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint)); michael@0: michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) { michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "=%u", info[i].cluster)); michael@0: } michael@0: michael@0: if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS)) michael@0: { michael@0: if (pos[i].x_offset || pos[i].y_offset) michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "@%d,%d", pos[i].x_offset, pos[i].y_offset)); michael@0: michael@0: *p++ = '+'; michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%d", pos[i].x_advance)); michael@0: if (pos[i].y_advance) michael@0: p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",%d", pos[i].y_advance)); michael@0: } michael@0: michael@0: unsigned int l = p - b; michael@0: if (buf_size > l) michael@0: { michael@0: memcpy (buf, b, l); michael@0: buf += l; michael@0: buf_size -= l; michael@0: *buf_consumed += l; michael@0: *buf = '\0'; michael@0: } else michael@0: return i - start; michael@0: } michael@0: michael@0: return end - start; michael@0: } michael@0: michael@0: /* Returns number of items, starting at start, that were serialized. */ michael@0: /** michael@0: * hb_buffer_serialize_glyphs: michael@0: * @buffer: a buffer. michael@0: * @start: michael@0: * @end: michael@0: * @buf: (array length=buf_size): michael@0: * @buf_size: michael@0: * @buf_consumed: (out): michael@0: * @font: michael@0: * @format: michael@0: * @flags: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: unsigned int michael@0: hb_buffer_serialize_glyphs (hb_buffer_t *buffer, michael@0: unsigned int start, michael@0: unsigned int end, michael@0: char *buf, michael@0: unsigned int buf_size, michael@0: unsigned int *buf_consumed, /* May be NULL */ michael@0: hb_font_t *font, /* May be NULL */ michael@0: hb_buffer_serialize_format_t format, michael@0: hb_buffer_serialize_flags_t flags) michael@0: { michael@0: assert (start <= end && end <= buffer->len); michael@0: michael@0: unsigned int sconsumed; michael@0: if (!buf_consumed) michael@0: buf_consumed = &sconsumed; michael@0: *buf_consumed = 0; michael@0: michael@0: assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) || michael@0: buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS); michael@0: michael@0: if (unlikely (start == end)) michael@0: return 0; michael@0: michael@0: if (!font) michael@0: font = hb_font_get_empty (); michael@0: michael@0: switch (format) michael@0: { michael@0: case HB_BUFFER_SERIALIZE_FORMAT_TEXT: michael@0: return _hb_buffer_serialize_glyphs_text (buffer, start, end, michael@0: buf, buf_size, buf_consumed, michael@0: font, flags); michael@0: michael@0: case HB_BUFFER_SERIALIZE_FORMAT_JSON: michael@0: return _hb_buffer_serialize_glyphs_json (buffer, start, end, michael@0: buf, buf_size, buf_consumed, michael@0: font, flags); michael@0: michael@0: default: michael@0: case HB_BUFFER_SERIALIZE_FORMAT_INVALID: michael@0: return 0; michael@0: michael@0: } michael@0: } michael@0: michael@0: michael@0: static hb_bool_t michael@0: parse_uint (const char *pp, const char *end, uint32_t *pv) michael@0: { michael@0: char buf[32]; michael@0: unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp)); michael@0: strncpy (buf, pp, len); michael@0: buf[len] = '\0'; michael@0: michael@0: char *p = buf; michael@0: char *pend = p; michael@0: uint32_t v; michael@0: michael@0: errno = 0; michael@0: v = strtol (p, &pend, 10); michael@0: if (errno || p == pend || pend - p != end - pp) michael@0: return false; michael@0: michael@0: *pv = v; michael@0: return true; michael@0: } michael@0: michael@0: static hb_bool_t michael@0: parse_int (const char *pp, const char *end, int32_t *pv) michael@0: { michael@0: char buf[32]; michael@0: unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp)); michael@0: strncpy (buf, pp, len); michael@0: buf[len] = '\0'; michael@0: michael@0: char *p = buf; michael@0: char *pend = p; michael@0: int32_t v; michael@0: michael@0: errno = 0; michael@0: v = strtol (p, &pend, 10); michael@0: if (errno || p == pend || pend - p != end - pp) michael@0: return false; michael@0: michael@0: *pv = v; michael@0: return true; michael@0: } michael@0: michael@0: #include "hb-buffer-deserialize-json.hh" michael@0: #include "hb-buffer-deserialize-text.hh" michael@0: michael@0: /** michael@0: * hb_buffer_deserialize_glyphs: michael@0: * @buffer: a buffer. michael@0: * @buf: (array length=buf_len): michael@0: * @buf_len: michael@0: * @end_ptr: (out): michael@0: * @font: michael@0: * @format: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_buffer_deserialize_glyphs (hb_buffer_t *buffer, michael@0: const char *buf, michael@0: int buf_len, /* -1 means nul-terminated */ michael@0: const char **end_ptr, /* May be NULL */ michael@0: hb_font_t *font, /* May be NULL */ michael@0: hb_buffer_serialize_format_t format) michael@0: { michael@0: const char *end; michael@0: if (!end_ptr) michael@0: end_ptr = &end; michael@0: *end_ptr = buf; michael@0: michael@0: assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) || michael@0: buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS); michael@0: michael@0: if (buf_len == -1) michael@0: buf_len = strlen (buf); michael@0: michael@0: if (!buf_len) michael@0: { michael@0: *end_ptr = buf; michael@0: return false; michael@0: } michael@0: michael@0: hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS); michael@0: michael@0: if (!font) michael@0: font = hb_font_get_empty (); michael@0: michael@0: switch (format) michael@0: { michael@0: case HB_BUFFER_SERIALIZE_FORMAT_TEXT: michael@0: return _hb_buffer_deserialize_glyphs_text (buffer, michael@0: buf, buf_len, end_ptr, michael@0: font); michael@0: michael@0: case HB_BUFFER_SERIALIZE_FORMAT_JSON: michael@0: return _hb_buffer_deserialize_glyphs_json (buffer, michael@0: buf, buf_len, end_ptr, michael@0: font); michael@0: michael@0: default: michael@0: case HB_BUFFER_SERIALIZE_FORMAT_INVALID: michael@0: return false; michael@0: michael@0: } michael@0: }