Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright  2013  Google, Inc.
      3  *
      4  *  This is part of HarfBuzz, a text shaping library.
      5  *
      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.
     11  *
     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.
     17  *
     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.
     23  *
     24  * Google Author(s): Behdad Esfahbod
     25  */
     26 
     27 #ifndef HB_BUFFER_DESERIALIZE_JSON_HH
     28 #define HB_BUFFER_DESERIALIZE_JSON_HH
     29 
     30 #include "hb-private.hh"
     31 
     32 %%{
     33 
     34 machine deserialize_json;
     35 alphtype unsigned char;
     36 write data;
     37 
     38 action clear_item {
     39 	memset (&info, 0, sizeof (info));
     40 	memset (&pos , 0, sizeof (pos ));
     41 }
     42 
     43 action add_item {
     44 	buffer->add_info (info);
     45 	if (buffer->in_error)
     46 	  return false;
     47 	buffer->pos[buffer->len - 1] = pos;
     48 	*end_ptr = p;
     49 }
     50 
     51 action tok {
     52 	tok = p;
     53 }
     54 
     55 action parse_glyph {
     56 	if (!hb_font_glyph_from_string (font,
     57 					tok, p - tok,
     58 					&info.codepoint))
     59 	  return false;
     60 }
     61 
     62 action parse_gid       { if (!parse_uint (tok, p, &info.codepoint)) return false; }
     63 action parse_cluster   { if (!parse_uint (tok, p, &info.cluster )) return false; }
     64 action parse_x_offset  { if (!parse_int  (tok, p, &pos.x_offset )) return false; }
     65 action parse_y_offset  { if (!parse_int  (tok, p, &pos.y_offset )) return false; }
     66 action parse_x_advance { if (!parse_int  (tok, p, &pos.x_advance)) return false; }
     67 action parse_y_advance { if (!parse_int  (tok, p, &pos.y_advance)) return false; }
     68 
     69 unum	= '0' | [1-9] digit*;
     70 num	= '-'? unum;
     71 
     72 comma = space* ',' space*;
     73 colon = space* ':' space*;
     74 
     75 glyph_id = unum;
     76 glyph_name = alpha (alnum|'_'|'.'|'-')*;
     77 
     78 glyph_string   = '"' (glyph_name >tok %parse_glyph) '"';
     79 glyph_number = (glyph_id >tok %parse_gid);
     80 
     81 glyph	= "\"g\""  colon (glyph_string | glyph_number);
     82 cluster	= "\"cl\"" colon (unum >tok %parse_cluster);
     83 xoffset	= "\"dx\"" colon (num >tok %parse_x_offset);
     84 yoffset	= "\"dy\"" colon (num >tok %parse_y_offset);
     85 xadvance= "\"ax\"" colon (num >tok %parse_x_advance);
     86 yadvance= "\"ay\"" colon (num >tok %parse_y_advance);
     87 
     88 element = glyph | cluster | xoffset | yoffset | xadvance | yadvance;
     89 item	=
     90 	( '{' space* element (comma element)* space* '}')
     91 	>clear_item
     92 	@add_item
     93 	;
     94 
     95 main := space* item (comma item)* space* (','|']')?;
     96 
     97 }%%
     98 
     99 static hb_bool_t
    100 _hb_buffer_deserialize_glyphs_json (hb_buffer_t *buffer,
    101 				    const char *buf,
    102 				    unsigned int buf_len,
    103 				    const char **end_ptr,
    104 				    hb_font_t *font)
    105 {
    106   const char *p = buf, *pe = buf + buf_len;
    107 
    108   /* Ensure we have positions. */
    109   (void) hb_buffer_get_glyph_positions (buffer, NULL);
    110 
    111   while (p < pe && ISSPACE (*p))
    112     p++;
    113   if (p < pe && *p == (buffer->len ? ',' : '['))
    114   {
    115     *end_ptr = ++p;
    116   }
    117 
    118   const char *tok = NULL;
    119   int cs;
    120   hb_glyph_info_t info = {0};
    121   hb_glyph_position_t pos = {0};
    122   %%{
    123     write init;
    124     write exec;
    125   }%%
    126 
    127   *end_ptr = p;
    128 
    129   return p == pe && *(p-1) != ']';
    130 }
    131 
    132 #endif /* HB_BUFFER_DESERIALIZE_JSON_HH */
    133