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
) & ~0x20202020);
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
= hb_buffer_get_glyph_positions (buffer
, NULL
);
105 for (unsigned int i
= start
; i
< end
; i
++)
110 /* In the following code, we know b is large enough that no overflow can happen. */
112 #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END
120 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES
))
123 hb_font_glyph_to_string (font
, info
[i
].codepoint
, g
, sizeof (g
));
125 for (char *q
= g
; *q
; q
++) {
133 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), "%u", info
[i
].codepoint
));
135 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS
)) {
136 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), ",\"cl\":%u", info
[i
].cluster
));
139 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS
))
141 p
+= snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), ",\"dx\":%d,\"dy\":%d",
142 pos
[i
].x_offset
, pos
[i
].y_offset
);
143 p
+= snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), ",\"ax\":%d,\"ay\":%d",
144 pos
[i
].x_advance
, pos
[i
].y_advance
);
149 unsigned int l
= p
- b
;
165 _hb_buffer_serialize_glyphs_text (hb_buffer_t
*buffer
,
169 unsigned int buf_size
,
170 unsigned int *buf_consumed
,
172 hb_buffer_serialize_flags_t flags
)
174 hb_glyph_info_t
*info
= hb_buffer_get_glyph_infos (buffer
, NULL
);
175 hb_glyph_position_t
*pos
= hb_buffer_get_glyph_positions (buffer
, NULL
);
178 for (unsigned int i
= start
; i
< end
; i
++)
183 /* In the following code, we know b is large enough that no overflow can happen. */
188 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES
))
190 hb_font_glyph_to_string (font
, info
[i
].codepoint
, p
, 128);
194 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), "%u", info
[i
].codepoint
));
196 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS
)) {
197 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), "=%u", info
[i
].cluster
));
200 if (!(flags
& HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS
))
202 if (pos
[i
].x_offset
|| pos
[i
].y_offset
)
203 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), "@%d,%d", pos
[i
].x_offset
, pos
[i
].y_offset
));
206 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), "%d", pos
[i
].x_advance
));
208 p
+= MAX (0, snprintf (p
, ARRAY_LENGTH (b
) - (p
- b
), ",%d", pos
[i
].y_advance
));
211 unsigned int l
= p
- b
;
226 /* Returns number of items, starting at start, that were serialized. */
228 * hb_buffer_serialize_glyphs:
232 * @buf: (array length=buf_size):
234 * @buf_consumed: (out):
246 hb_buffer_serialize_glyphs (hb_buffer_t
*buffer
,
250 unsigned int buf_size
,
251 unsigned int *buf_consumed
, /* May be NULL */
252 hb_font_t
*font
, /* May be NULL */
253 hb_buffer_serialize_format_t format
,
254 hb_buffer_serialize_flags_t flags
)
256 assert (start
<= end
&& end
<= buffer
->len
);
258 unsigned int sconsumed
;
260 buf_consumed
= &sconsumed
;
263 assert ((!buffer
->len
&& buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_INVALID
) ||
264 buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_GLYPHS
);
266 if (unlikely (start
== end
))
270 font
= hb_font_get_empty ();
274 case HB_BUFFER_SERIALIZE_FORMAT_TEXT
:
275 return _hb_buffer_serialize_glyphs_text (buffer
, start
, end
,
276 buf
, buf_size
, buf_consumed
,
279 case HB_BUFFER_SERIALIZE_FORMAT_JSON
:
280 return _hb_buffer_serialize_glyphs_json (buffer
, start
, end
,
281 buf
, buf_size
, buf_consumed
,
285 case HB_BUFFER_SERIALIZE_FORMAT_INVALID
:
293 parse_uint (const char *pp
, const char *end
, uint32_t *pv
)
296 unsigned int len
= MIN (ARRAY_LENGTH (buf
) - 1, (unsigned int) (end
- pp
));
297 strncpy (buf
, pp
, len
);
305 v
= strtol (p
, &pend
, 10);
306 if (errno
|| p
== pend
|| pend
- p
!= end
- pp
)
314 parse_int (const char *pp
, const char *end
, int32_t *pv
)
317 unsigned int len
= MIN (ARRAY_LENGTH (buf
) - 1, (unsigned int) (end
- pp
));
318 strncpy (buf
, pp
, len
);
326 v
= strtol (p
, &pend
, 10);
327 if (errno
|| p
== pend
|| pend
- p
!= end
- pp
)
334 #include "hb-buffer-deserialize-json.hh"
335 #include "hb-buffer-deserialize-text.hh"
338 * hb_buffer_deserialize_glyphs:
340 * @buf: (array length=buf_len):
353 hb_buffer_deserialize_glyphs (hb_buffer_t
*buffer
,
355 int buf_len
, /* -1 means nul-terminated */
356 const char **end_ptr
, /* May be NULL */
357 hb_font_t
*font
, /* May be NULL */
358 hb_buffer_serialize_format_t format
)
365 assert ((!buffer
->len
&& buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_INVALID
) ||
366 buffer
->content_type
== HB_BUFFER_CONTENT_TYPE_GLYPHS
);
369 buf_len
= strlen (buf
);
377 hb_buffer_set_content_type (buffer
, HB_BUFFER_CONTENT_TYPE_GLYPHS
);
380 font
= hb_font_get_empty ();
384 case HB_BUFFER_SERIALIZE_FORMAT_TEXT
:
385 return _hb_buffer_deserialize_glyphs_text (buffer
,
386 buf
, buf_len
, end_ptr
,
389 case HB_BUFFER_SERIALIZE_FORMAT_JSON
:
390 return _hb_buffer_deserialize_glyphs_json (buffer
,
391 buf
, buf_len
, end_ptr
,
395 case HB_BUFFER_SERIALIZE_FORMAT_INVALID
: