michael@0: /* michael@0: * Copyright © 2013 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_BUFFER_DESERIALIZE_JSON_HH michael@0: #define HB_BUFFER_DESERIALIZE_JSON_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: %%{ michael@0: michael@0: machine deserialize_json; michael@0: alphtype unsigned char; michael@0: write data; michael@0: michael@0: action clear_item { michael@0: memset (&info, 0, sizeof (info)); michael@0: memset (&pos , 0, sizeof (pos )); michael@0: } michael@0: michael@0: action add_item { michael@0: buffer->add_info (info); michael@0: if (buffer->in_error) michael@0: return false; michael@0: buffer->pos[buffer->len - 1] = pos; michael@0: *end_ptr = p; michael@0: } michael@0: michael@0: action tok { michael@0: tok = p; michael@0: } michael@0: michael@0: action parse_glyph { michael@0: if (!hb_font_glyph_from_string (font, michael@0: tok, p - tok, michael@0: &info.codepoint)) michael@0: return false; michael@0: } michael@0: michael@0: action parse_gid { if (!parse_uint (tok, p, &info.codepoint)) return false; } michael@0: action parse_cluster { if (!parse_uint (tok, p, &info.cluster )) return false; } michael@0: action parse_x_offset { if (!parse_int (tok, p, &pos.x_offset )) return false; } michael@0: action parse_y_offset { if (!parse_int (tok, p, &pos.y_offset )) return false; } michael@0: action parse_x_advance { if (!parse_int (tok, p, &pos.x_advance)) return false; } michael@0: action parse_y_advance { if (!parse_int (tok, p, &pos.y_advance)) return false; } michael@0: michael@0: unum = '0' | [1-9] digit*; michael@0: num = '-'? unum; michael@0: michael@0: comma = space* ',' space*; michael@0: colon = space* ':' space*; michael@0: michael@0: glyph_id = unum; michael@0: glyph_name = alpha (alnum|'_'|'.'|'-')*; michael@0: michael@0: glyph_string = '"' (glyph_name >tok %parse_glyph) '"'; michael@0: glyph_number = (glyph_id >tok %parse_gid); michael@0: michael@0: glyph = "\"g\"" colon (glyph_string | glyph_number); michael@0: cluster = "\"cl\"" colon (unum >tok %parse_cluster); michael@0: xoffset = "\"dx\"" colon (num >tok %parse_x_offset); michael@0: yoffset = "\"dy\"" colon (num >tok %parse_y_offset); michael@0: xadvance= "\"ax\"" colon (num >tok %parse_x_advance); michael@0: yadvance= "\"ay\"" colon (num >tok %parse_y_advance); michael@0: michael@0: element = glyph | cluster | xoffset | yoffset | xadvance | yadvance; michael@0: item = michael@0: ( '{' space* element (comma element)* space* '}') michael@0: >clear_item michael@0: @add_item michael@0: ; michael@0: michael@0: main := space* item (comma item)* space* (','|']')?; michael@0: michael@0: }%% michael@0: michael@0: static hb_bool_t michael@0: _hb_buffer_deserialize_glyphs_json (hb_buffer_t *buffer, michael@0: const char *buf, michael@0: unsigned int buf_len, michael@0: const char **end_ptr, michael@0: hb_font_t *font) michael@0: { michael@0: const char *p = buf, *pe = buf + buf_len; michael@0: michael@0: /* Ensure we have positions. */ michael@0: (void) hb_buffer_get_glyph_positions (buffer, NULL); michael@0: michael@0: while (p < pe && ISSPACE (*p)) michael@0: p++; michael@0: if (p < pe && *p == (buffer->len ? ',' : '[')) michael@0: { michael@0: *end_ptr = ++p; michael@0: } michael@0: michael@0: const char *tok = NULL; michael@0: int cs; michael@0: hb_glyph_info_t info; michael@0: hb_glyph_position_t pos; michael@0: %%{ michael@0: write init; michael@0: write exec; michael@0: }%% michael@0: michael@0: *end_ptr = p; michael@0: michael@0: return p == pe && *(p-1) != ']'; michael@0: } michael@0: michael@0: #endif /* HB_BUFFER_DESERIALIZE_JSON_HH */