2 * Copyright © 2012,2013 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-buffer-private.hh"
30 static const char *serialize_formats[] = {
37 * hb_buffer_serialize_list_formats:
39 * Returns a list of supported buffer serialization formats.
41 * Return value: (transfer none):
42 * A string array of buffer serialization formats. Should not be freed.
47 hb_buffer_serialize_list_formats (void)
49 return serialize_formats;
53 * hb_buffer_serialize_format_from_string:
54 * @str: (array length=len) (element-type uint8_t): a string to parse
55 * @len: length of @str, or -1 if string is %NULL terminated
57 * Parses a string into an #hb_buffer_serialize_format_t. Does not check if
58 * @str is a valid buffer serialization format, use
59 * hb_buffer_serialize_list_formats() to get the list of supported formats.
62 * The parsed #hb_buffer_serialize_format_t.
66 hb_buffer_serialize_format_t
67 hb_buffer_serialize_format_from_string (const char *str, int len)
70 return (hb_buffer_serialize_format_t) (hb_tag_from_string (str, len) & ~0x20202020u);
74 * hb_buffer_serialize_format_to_string:
75 * @format: an #hb_buffer_serialize_format_t to convert.
77 * Converts @format to the string corresponding it, or %NULL if it is not a valid
78 * #hb_buffer_serialize_format_t.
80 * Return value: (transfer none):
81 * A %NULL terminated string corresponding to @format. Should not be freed.
86 hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format)
90 case HB_BUFFER_SERIALIZE_FORMAT_TEXT: return serialize_formats[0];
91 case HB_BUFFER_SERIALIZE_FORMAT_JSON: return serialize_formats[1];
93 case HB_BUFFER_SERIALIZE_FORMAT_INVALID: return nullptr;
98 _hb_buffer_serialize_glyphs_json (hb_buffer_t *buffer,
102 unsigned int buf_size,
103 unsigned int *buf_consumed,
105 hb_buffer_serialize_flags_t flags)
107 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
108 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
109 nullptr : hb_buffer_get_glyph_positions (buffer, nullptr);
112 hb_position_t x = 0, y = 0;
113 for (unsigned int i = start; i < end; i++)
118 /* In the following code, we know b is large enough that no overflow can happen. */
120 #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END
128 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
131 hb_font_glyph_to_string (font, info[i].codepoint, g, sizeof (g));
133 for (char *q = g; *q; q++) {
141 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
143 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
144 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"cl\":%u", info[i].cluster));
147 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
149 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"dx\":%d,\"dy\":%d",
150 x+pos[i].x_offset, y+pos[i].y_offset));
151 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES))
152 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"ax\":%d,\"ay\":%d",
153 pos[i].x_advance, pos[i].y_advance));
156 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_FLAGS)
158 if (info[i].mask & HB_GLYPH_FLAG_DEFINED)
159 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"fl\":%u", info[i].mask & HB_GLYPH_FLAG_DEFINED));
162 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS)
164 hb_glyph_extents_t extents;
165 hb_font_get_glyph_extents(font, info[i].codepoint, &extents);
166 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"xb\":%d,\"yb\":%d",
167 extents.x_bearing, extents.y_bearing));
168 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"w\":%d,\"h\":%d",
169 extents.width, extents.height));
174 unsigned int l = p - b;
185 if (pos && (flags & HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES))
187 x += pos[i].x_advance;
188 y += pos[i].y_advance;
196 _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer,
200 unsigned int buf_size,
201 unsigned int *buf_consumed,
203 hb_buffer_serialize_flags_t flags)
205 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
206 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
207 nullptr : hb_buffer_get_glyph_positions (buffer, nullptr);
210 hb_position_t x = 0, y = 0;
211 for (unsigned int i = start; i < end; i++)
216 /* In the following code, we know b is large enough that no overflow can happen. */
221 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
223 hb_font_glyph_to_string (font, info[i].codepoint, p, 128);
227 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
229 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
230 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "=%u", info[i].cluster));
233 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
235 if (x+pos[i].x_offset || y+pos[i].y_offset)
236 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "@%d,%d", x+pos[i].x_offset, y+pos[i].y_offset));
238 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES))
241 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%d", pos[i].x_advance));
242 if (pos[i].y_advance)
243 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",%d", pos[i].y_advance));
247 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_FLAGS)
249 if (info[i].mask &HB_GLYPH_FLAG_DEFINED)
250 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "#%X", info[i].mask &HB_GLYPH_FLAG_DEFINED));
253 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS)
255 hb_glyph_extents_t extents;
256 hb_font_get_glyph_extents(font, info[i].codepoint, &extents);
257 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "<%d,%d,%d,%d>", extents.x_bearing, extents.y_bearing, extents.width, extents.height));
260 unsigned int l = p - b;
271 if (pos && (flags & HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES))
273 x += pos[i].x_advance;
274 y += pos[i].y_advance;
282 * hb_buffer_serialize_glyphs:
283 * @buffer: an #hb_buffer_t buffer.
284 * @start: the first item in @buffer to serialize.
285 * @end: the last item in @buffer to serialize.
286 * @buf: (out) (array length=buf_size) (element-type uint8_t): output string to
287 * write serialized buffer into.
288 * @buf_size: the size of @buf.
289 * @buf_consumed: (out) (allow-none): if not %NULL, will be set to the number of byes written into @buf.
290 * @font: (allow-none): the #hb_font_t used to shape this buffer, needed to
291 * read glyph names and extents. If %NULL, and empty font will be used.
292 * @format: the #hb_buffer_serialize_format_t to use for formatting the output.
293 * @flags: the #hb_buffer_serialize_flags_t that control what glyph properties
296 * Serializes @buffer into a textual representation of its glyph content,
297 * useful for showing the contents of the buffer, for example during debugging.
298 * There are currently two supported serialization formats:
301 * A human-readable, plain text format.
302 * The serialized glyphs will look something like:
305 * [uni0651=0@518,0+0|uni0628=0+1897]
307 * - The serialized glyphs are delimited with `[` and `]`.
308 * - Glyphs are separated with `|`
309 * - Each glyph starts with glyph name, or glyph index if
310 * #HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES flag is set. Then,
311 * - If #HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS is not set, `=` then #hb_glyph_info_t.cluster.
312 * - If #HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS is not set, the #hb_glyph_position_t in the format:
313 * - If both #hb_glyph_position_t.x_offset and #hb_glyph_position_t.y_offset are not 0, `@x_offset,y_offset`. Then,
314 * - `+x_advance`, then `,y_advance` if #hb_glyph_position_t.y_advance is not 0. Then,
315 * - If #HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS is set, the
316 * #hb_glyph_extents_t in the format
317 * `<x_bearing,y_bearing,width,height>`
323 * The number of serialized items.
328 hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
332 unsigned int buf_size,
333 unsigned int *buf_consumed,
335 hb_buffer_serialize_format_t format,
336 hb_buffer_serialize_flags_t flags)
338 assert (start <= end && end <= buffer->len);
340 unsigned int sconsumed;
342 buf_consumed = &sconsumed;
347 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
348 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
350 if (!buffer->have_positions)
351 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS;
353 if (unlikely (start == end))
357 font = hb_font_get_empty ();
361 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
362 return _hb_buffer_serialize_glyphs_text (buffer, start, end,
363 buf, buf_size, buf_consumed,
366 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
367 return _hb_buffer_serialize_glyphs_json (buffer, start, end,
368 buf, buf_size, buf_consumed,
372 case HB_BUFFER_SERIALIZE_FORMAT_INVALID:
380 parse_uint (const char *pp, const char *end, uint32_t *pv)
383 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
384 strncpy (buf, pp, len);
392 v = strtol (p, &pend, 10);
393 if (errno || p == pend || pend - p != end - pp)
401 parse_int (const char *pp, const char *end, int32_t *pv)
404 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
405 strncpy (buf, pp, len);
413 v = strtol (p, &pend, 10);
414 if (errno || p == pend || pend - p != end - pp)
421 #include "hb-buffer-deserialize-json.hh"
422 #include "hb-buffer-deserialize-text.hh"
425 * hb_buffer_deserialize_glyphs:
426 * @buffer: an #hb_buffer_t buffer.
427 * @buf: (array length=buf_len):
440 hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
442 int buf_len, /* -1 means nul-terminated */
443 const char **end_ptr, /* May be nullptr */
444 hb_font_t *font, /* May be nullptr */
445 hb_buffer_serialize_format_t format)
452 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
453 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
456 buf_len = strlen (buf);
464 hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
467 font = hb_font_get_empty ();
471 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
472 return _hb_buffer_deserialize_glyphs_text (buffer,
473 buf, buf_len, end_ptr,
476 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
477 return _hb_buffer_deserialize_glyphs_json (buffer,
478 buf, buf_len, end_ptr,
482 case HB_BUFFER_SERIALIZE_FORMAT_INVALID: