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:
41 * Return value: (transfer none):
46 hb_buffer_serialize_list_formats (void)
48 return serialize_formats
;
52 * hb_buffer_serialize_format_from_string:
62 hb_buffer_serialize_format_t
63 hb_buffer_serialize_format_from_string (const char *str
, int len
)
66 return (hb_buffer_serialize_format_t
) (hb_tag_from_string (str
, len
) & ~0x20202020u
);
70 * hb_buffer_serialize_format_to_string:
80 hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format
)
84 case HB_BUFFER_SERIALIZE_FORMAT_TEXT
: return serialize_formats
[0];
85 case HB_BUFFER_SERIALIZE_FORMAT_JSON
: return serialize_formats
[1];
87 case HB_BUFFER_SERIALIZE_FORMAT_INVALID
: return NULL
;
92 _hb_buffer_serialize_glyphs_json (hb_buffer_t
*buffer
,
96 unsigned int buf_size
,
97 unsigned int *buf_consumed
,
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
);
106 for (unsigned int i
= start
; i
< end
; i
++)
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
121 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES
))
124 hb_font_glyph_to_string (font
, info
[i
].codepoint
, g
, sizeof (g
));
126 for (char *q
= g
; *q
; q
++) {
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
);
150 unsigned int l
= p
- b
;
166 _hb_buffer_serialize_glyphs_text (hb_buffer_t
*buffer
,
170 unsigned int buf_size
,
171 unsigned int *buf_consumed
,
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
);
180 for (unsigned int i
= start
; i
< end
; i
++)
185 /* In the following code, we know b is large enough that no overflow can happen. */
190 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES
))
192 hb_font_glyph_to_string (font
, info
[i
].codepoint
, p
, 128);
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
));
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
;
228 /* Returns number of items, starting at start, that were serialized. */
230 * hb_buffer_serialize_glyphs:
234 * @buf: (array length=buf_size):
236 * @buf_consumed: (out):
248 hb_buffer_serialize_glyphs (hb_buffer_t
*buffer
,
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
;
262 buf_consumed
= &sconsumed
;
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
))
272 font
= hb_font_get_empty ();
276 case HB_BUFFER_SERIALIZE_FORMAT_TEXT
:
277 return _hb_buffer_serialize_glyphs_text (buffer
, start
, end
,
278 buf
, buf_size
, buf_consumed
,
281 case HB_BUFFER_SERIALIZE_FORMAT_JSON
:
282 return _hb_buffer_serialize_glyphs_json (buffer
, start
, end
,
283 buf
, buf_size
, buf_consumed
,
287 case HB_BUFFER_SERIALIZE_FORMAT_INVALID
:
295 parse_uint (const char *pp
, const char *end
, uint32_t *pv
)
298 unsigned int len
= MIN (ARRAY_LENGTH (buf
) - 1, (unsigned int) (end
- pp
));
299 strncpy (buf
, pp
, len
);
307 v
= strtol (p
, &pend
, 10);
308 if (errno
|| p
== pend
|| pend
- p
!= end
- pp
)
316 parse_int (const char *pp
, const char *end
, int32_t *pv
)
319 unsigned int len
= MIN (ARRAY_LENGTH (buf
) - 1, (unsigned int) (end
- pp
));
320 strncpy (buf
, pp
, len
);
328 v
= strtol (p
, &pend
, 10);
329 if (errno
|| p
== pend
|| pend
- p
!= end
- pp
)
336 #include "hb-buffer-deserialize-json.hh"
337 #include "hb-buffer-deserialize-text.hh"
340 * hb_buffer_deserialize_glyphs:
342 * @buf: (array length=buf_len):
355 hb_buffer_deserialize_glyphs (hb_buffer_t
*buffer
,
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
)
367 assert ((!buffer
->len
&& buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_INVALID
) ||
368 buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_GLYPHS
);
371 buf_len
= strlen (buf
);
379 hb_buffer_set_content_type (buffer
, HB_BUFFER_CONTENT_TYPE_GLYPHS
);
382 font
= hb_font_get_empty ();
386 case HB_BUFFER_SERIALIZE_FORMAT_TEXT
:
387 return _hb_buffer_deserialize_glyphs_text (buffer
,
388 buf
, buf_len
, end_ptr
,
391 case HB_BUFFER_SERIALIZE_FORMAT_JSON
:
392 return _hb_buffer_deserialize_glyphs_json (buffer
,
393 buf
, buf_len
, end_ptr
,
397 case HB_BUFFER_SERIALIZE_FORMAT_INVALID
: