Restore variadic macros in DevToolsEmbedderMessageDispatcher
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-buffer-serialize.cc
blob8a22224a5a250ad1fb405aa85cdaf823ee1a7c7a
1 /*
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
16 * DAMAGE.
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[] = {
31 "text",
32 "json",
33 NULL
36 /**
37 * hb_buffer_serialize_list_formats:
41 * Return value: (transfer none):
43 * Since: 1.0
44 **/
45 const char **
46 hb_buffer_serialize_list_formats (void)
48 return serialize_formats;
51 /**
52 * hb_buffer_serialize_format_from_string:
53 * @str:
54 * @len:
58 * Return value:
60 * Since: 1.0
61 **/
62 hb_buffer_serialize_format_t
63 hb_buffer_serialize_format_from_string (const char *str, int len)
65 /* Upper-case it. */
66 return (hb_buffer_serialize_format_t) (hb_tag_from_string (str, len) & ~0x20202020u);
69 /**
70 * hb_buffer_serialize_format_to_string:
71 * @format:
75 * Return value:
77 * Since: 1.0
78 **/
79 const char *
80 hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format)
82 switch (format)
84 case HB_BUFFER_SERIALIZE_FORMAT_TEXT: return serialize_formats[0];
85 case HB_BUFFER_SERIALIZE_FORMAT_JSON: return serialize_formats[1];
86 default:
87 case HB_BUFFER_SERIALIZE_FORMAT_INVALID: return NULL;
91 static unsigned int
92 _hb_buffer_serialize_glyphs_json (hb_buffer_t *buffer,
93 unsigned int start,
94 unsigned int end,
95 char *buf,
96 unsigned int buf_size,
97 unsigned int *buf_consumed,
98 hb_font_t *font,
99 hb_buffer_serialize_flags_t flags)
101 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
102 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
103 NULL : hb_buffer_get_glyph_positions (buffer, NULL);
105 *buf_consumed = 0;
106 for (unsigned int i = start; i < end; i++)
108 char b[1024];
109 char *p = b;
111 /* In the following code, we know b is large enough that no overflow can happen. */
113 #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END
115 if (i)
116 *p++ = ',';
118 *p++ = '{';
120 APPEND ("\"g\":");
121 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
123 char g[128];
124 hb_font_glyph_to_string (font, info[i].codepoint, g, sizeof (g));
125 *p++ = '"';
126 for (char *q = g; *q; q++) {
127 if (*q == '"')
128 *p++ = '\\';
129 *p++ = *q;
131 *p++ = '"';
133 else
134 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
136 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
137 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"cl\":%u", info[i].cluster));
140 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
142 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"dx\":%d,\"dy\":%d",
143 pos[i].x_offset, pos[i].y_offset);
144 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"ax\":%d,\"ay\":%d",
145 pos[i].x_advance, pos[i].y_advance);
148 *p++ = '}';
150 unsigned int l = p - b;
151 if (buf_size > l)
153 memcpy (buf, b, l);
154 buf += l;
155 buf_size -= l;
156 *buf_consumed += l;
157 *buf = '\0';
158 } else
159 return i - start;
162 return end - start;
165 static unsigned int
166 _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer,
167 unsigned int start,
168 unsigned int end,
169 char *buf,
170 unsigned int buf_size,
171 unsigned int *buf_consumed,
172 hb_font_t *font,
173 hb_buffer_serialize_flags_t flags)
175 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
176 hb_glyph_position_t *pos = (flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS) ?
177 NULL : hb_buffer_get_glyph_positions (buffer, NULL);
179 *buf_consumed = 0;
180 for (unsigned int i = start; i < end; i++)
182 char b[1024];
183 char *p = b;
185 /* In the following code, we know b is large enough that no overflow can happen. */
187 if (i)
188 *p++ = '|';
190 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
192 hb_font_glyph_to_string (font, info[i].codepoint, p, 128);
193 p += strlen (p);
195 else
196 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint));
198 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
199 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "=%u", info[i].cluster));
202 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
204 if (pos[i].x_offset || pos[i].y_offset)
205 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "@%d,%d", pos[i].x_offset, pos[i].y_offset));
207 *p++ = '+';
208 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "%d", pos[i].x_advance));
209 if (pos[i].y_advance)
210 p += MAX (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",%d", pos[i].y_advance));
213 unsigned int l = p - b;
214 if (buf_size > l)
216 memcpy (buf, b, l);
217 buf += l;
218 buf_size -= l;
219 *buf_consumed += l;
220 *buf = '\0';
221 } else
222 return i - start;
225 return end - start;
228 /* Returns number of items, starting at start, that were serialized. */
230 * hb_buffer_serialize_glyphs:
231 * @buffer: a buffer.
232 * @start:
233 * @end:
234 * @buf: (array length=buf_size):
235 * @buf_size:
236 * @buf_consumed: (out):
237 * @font:
238 * @format:
239 * @flags:
243 * Return value:
245 * Since: 1.0
247 unsigned int
248 hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
249 unsigned int start,
250 unsigned int end,
251 char *buf,
252 unsigned int buf_size,
253 unsigned int *buf_consumed, /* May be NULL */
254 hb_font_t *font, /* May be NULL */
255 hb_buffer_serialize_format_t format,
256 hb_buffer_serialize_flags_t flags)
258 assert (start <= end && end <= buffer->len);
260 unsigned int sconsumed;
261 if (!buf_consumed)
262 buf_consumed = &sconsumed;
263 *buf_consumed = 0;
265 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
266 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
268 if (unlikely (start == end))
269 return 0;
271 if (!font)
272 font = hb_font_get_empty ();
274 switch (format)
276 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
277 return _hb_buffer_serialize_glyphs_text (buffer, start, end,
278 buf, buf_size, buf_consumed,
279 font, flags);
281 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
282 return _hb_buffer_serialize_glyphs_json (buffer, start, end,
283 buf, buf_size, buf_consumed,
284 font, flags);
286 default:
287 case HB_BUFFER_SERIALIZE_FORMAT_INVALID:
288 return 0;
294 static hb_bool_t
295 parse_uint (const char *pp, const char *end, uint32_t *pv)
297 char buf[32];
298 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
299 strncpy (buf, pp, len);
300 buf[len] = '\0';
302 char *p = buf;
303 char *pend = p;
304 uint32_t v;
306 errno = 0;
307 v = strtol (p, &pend, 10);
308 if (errno || p == pend || pend - p != end - pp)
309 return false;
311 *pv = v;
312 return true;
315 static hb_bool_t
316 parse_int (const char *pp, const char *end, int32_t *pv)
318 char buf[32];
319 unsigned int len = MIN (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - pp));
320 strncpy (buf, pp, len);
321 buf[len] = '\0';
323 char *p = buf;
324 char *pend = p;
325 int32_t v;
327 errno = 0;
328 v = strtol (p, &pend, 10);
329 if (errno || p == pend || pend - p != end - pp)
330 return false;
332 *pv = v;
333 return true;
336 #include "hb-buffer-deserialize-json.hh"
337 #include "hb-buffer-deserialize-text.hh"
340 * hb_buffer_deserialize_glyphs:
341 * @buffer: a buffer.
342 * @buf: (array length=buf_len):
343 * @buf_len:
344 * @end_ptr: (out):
345 * @font:
346 * @format:
350 * Return value:
352 * Since: 1.0
354 hb_bool_t
355 hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
356 const char *buf,
357 int buf_len, /* -1 means nul-terminated */
358 const char **end_ptr, /* May be NULL */
359 hb_font_t *font, /* May be NULL */
360 hb_buffer_serialize_format_t format)
362 const char *end;
363 if (!end_ptr)
364 end_ptr = &end;
365 *end_ptr = buf;
367 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
368 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
370 if (buf_len == -1)
371 buf_len = strlen (buf);
373 if (!buf_len)
375 *end_ptr = buf;
376 return false;
379 hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
381 if (!font)
382 font = hb_font_get_empty ();
384 switch (format)
386 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
387 return _hb_buffer_deserialize_glyphs_text (buffer,
388 buf, buf_len, end_ptr,
389 font);
391 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
392 return _hb_buffer_deserialize_glyphs_json (buffer,
393 buf, buf_len, end_ptr,
394 font);
396 default:
397 case HB_BUFFER_SERIALIZE_FORMAT_INVALID:
398 return false;