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 NULL;
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, NULL);
108 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
109 NULL : hb_buffer_get_glyph_positions (buffer, NULL);
112 for (unsigned int i = start; i < end; i++)
117 /* In the following code, we know b is large enough that no overflow can happen. */
119 #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END
127 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
130 hb_font_glyph_to_string (font, info[i].codepoint, g, sizeof (g));
132 for (char *q = g; *q; q++) {
140 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
142 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
143 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"cl\":%u", info[i].cluster));
146 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
148 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"dx\":%d,\"dy\":%d",
149 pos[i].x_offset, pos[i].y_offset);
150 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"ax\":%d,\"ay\":%d",
151 pos[i].x_advance, pos[i].y_advance);
154 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS)
156 hb_glyph_extents_t extents;
157 hb_font_get_glyph_extents(font, info[i].codepoint, &extents);
158 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"xb\":%d,\"yb\":%d",
159 extents.x_bearing, extents.y_bearing));
160 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"w\":%d,\"h\":%d",
161 extents.width, extents.height));
166 unsigned int l = p - b;
182 _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer,
186 unsigned int buf_size,
187 unsigned int *buf_consumed,
189 hb_buffer_serialize_flags_t flags)
191 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
192 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
193 NULL : hb_buffer_get_glyph_positions (buffer, NULL);
196 for (unsigned int i = start; i < end; i++)
201 /* In the following code, we know b is large enough that no overflow can happen. */
206 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
208 hb_font_glyph_to_string (font, info[i].codepoint, p, 128);
212 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
214 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
215 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "=%u", info[i].cluster));
218 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
220 if (pos[i].x_offset || pos[i].y_offset)
221 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "@%d,%d", pos[i].x_offset, pos[i].y_offset));
224 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%d", pos[i].x_advance));
225 if (pos[i].y_advance)
226 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",%d", pos[i].y_advance));
229 if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS)
231 hb_glyph_extents_t extents;
232 hb_font_get_glyph_extents(font, info[i].codepoint, &extents);
233 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "<%d,%d,%d,%d>", extents.x_bearing, extents.y_bearing, extents.width, extents.height));
236 unsigned int l = p - b;
252 * hb_buffer_serialize_glyphs:
253 * @buffer: an #hb_buffer_t buffer.
254 * @start: the first item in @buffer to serialize.
255 * @end: the last item in @buffer to serialize.
256 * @buf: (out) (array length=buf_size) (element-type uint8_t): output string to
257 * write serialized buffer into.
258 * @buf_size: the size of @buf.
259 * @buf_consumed: (out) (allow-none): if not %NULL, will be set to the number of byes written into @buf.
260 * @font: (allow-none): the #hb_font_t used to shape this buffer, needed to
261 * read glyph names and extents. If %NULL, and empty font will be used.
262 * @format: the #hb_buffer_serialize_format_t to use for formatting the output.
263 * @flags: the #hb_buffer_serialize_flags_t that control what glyph properties
266 * Serializes @buffer into a textual representation of its glyph content,
267 * useful for showing the contents of the buffer, for example during debugging.
268 * There are currently two supported serialization formats:
271 * A human-readable, plain text format.
272 * The serialized glyphs will look something like:
275 * [uni0651=0@518,0+0|uni0628=0+1897]
277 * - The serialized glyphs are delimited with `[` and `]`.
278 * - Glyphs are separated with `|`
279 * - Each glyph starts with glyph name, or glyph index if
280 * #HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES flag is set. Then,
281 * - If #HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS is not set, `=` then #hb_glyph_info_t.cluster.
282 * - If #HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS is not set, the #hb_glyph_position_t in the format:
283 * - If both #hb_glyph_position_t.x_offset and #hb_glyph_position_t.y_offset are not 0, `@x_offset,y_offset`. Then,
284 * - `+x_advance`, then `,y_advance` if #hb_glyph_position_t.y_advance is not 0. Then,
285 * - If #HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS is set, the
286 * #hb_glyph_extents_t in the format
287 * `<x_bearing,y_bearing,width,height>`
293 * The number of serialized items.
298 hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
302 unsigned int buf_size,
303 unsigned int *buf_consumed,
305 hb_buffer_serialize_format_t format,
306 hb_buffer_serialize_flags_t flags)
308 assert (start <= end && end <= buffer->len);
310 unsigned int sconsumed;
312 buf_consumed = &sconsumed;
315 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
316 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
318 if (!buffer->have_positions)
319 flags |= HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS;
321 if (unlikely (start == end))
325 font = hb_font_get_empty ();
329 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
330 return _hb_buffer_serialize_glyphs_text (buffer, start, end,
331 buf, buf_size, buf_consumed,
334 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
335 return _hb_buffer_serialize_glyphs_json (buffer, start, end,
336 buf, buf_size, buf_consumed,
340 case HB_BUFFER_SERIALIZE_FORMAT_INVALID:
348 parse_uint (const char *pp, const char *end, uint32_t *pv)
351 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
352 strncpy (buf, pp, len);
360 v = strtol (p, &pend, 10);
361 if (errno || p == pend || pend - p != end - pp)
369 parse_int (const char *pp, const char *end, int32_t *pv)
372 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
373 strncpy (buf, pp, len);
381 v = strtol (p, &pend, 10);
382 if (errno || p == pend || pend - p != end - pp)
389 #include "hb-buffer-deserialize-json.hh"
390 #include "hb-buffer-deserialize-text.hh"
393 * hb_buffer_deserialize_glyphs:
394 * @buffer: an #hb_buffer_t buffer.
395 * @buf: (array length=buf_len):
408 hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
410 int buf_len, /* -1 means nul-terminated */
411 const char **end_ptr, /* May be NULL */
412 hb_font_t *font, /* May be NULL */
413 hb_buffer_serialize_format_t format)
420 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
421 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
424 buf_len = strlen (buf);
432 hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
435 font = hb_font_get_empty ();
439 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
440 return _hb_buffer_deserialize_glyphs_text (buffer,
441 buf, buf_len, end_ptr,
444 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
445 return _hb_buffer_deserialize_glyphs_json (buffer,
446 buf, buf_len, end_ptr,
450 case HB_BUFFER_SERIALIZE_FORMAT_INVALID: