gfx/harfbuzz/src/hb-buffer-deserialize-json.rl

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/hb-buffer-deserialize-json.rl	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Copyright © 2013  Google, Inc.
     1.6 + *
     1.7 + *  This is part of HarfBuzz, a text shaping library.
     1.8 + *
     1.9 + * Permission is hereby granted, without written agreement and without
    1.10 + * license or royalty fees, to use, copy, modify, and distribute this
    1.11 + * software and its documentation for any purpose, provided that the
    1.12 + * above copyright notice and the following two paragraphs appear in
    1.13 + * all copies of this software.
    1.14 + *
    1.15 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
    1.16 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    1.17 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
    1.18 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    1.19 + * DAMAGE.
    1.20 + *
    1.21 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
    1.22 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 + * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.24 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
    1.25 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.26 + *
    1.27 + * Google Author(s): Behdad Esfahbod
    1.28 + */
    1.29 +
    1.30 +#ifndef HB_BUFFER_DESERIALIZE_JSON_HH
    1.31 +#define HB_BUFFER_DESERIALIZE_JSON_HH
    1.32 +
    1.33 +#include "hb-private.hh"
    1.34 +
    1.35 +%%{
    1.36 +
    1.37 +machine deserialize_json;
    1.38 +alphtype unsigned char;
    1.39 +write data;
    1.40 +
    1.41 +action clear_item {
    1.42 +	memset (&info, 0, sizeof (info));
    1.43 +	memset (&pos , 0, sizeof (pos ));
    1.44 +}
    1.45 +
    1.46 +action add_item {
    1.47 +	buffer->add_info (info);
    1.48 +	if (buffer->in_error)
    1.49 +	  return false;
    1.50 +	buffer->pos[buffer->len - 1] = pos;
    1.51 +	*end_ptr = p;
    1.52 +}
    1.53 +
    1.54 +action tok {
    1.55 +	tok = p;
    1.56 +}
    1.57 +
    1.58 +action parse_glyph {
    1.59 +	if (!hb_font_glyph_from_string (font,
    1.60 +					tok, p - tok,
    1.61 +					&info.codepoint))
    1.62 +	  return false;
    1.63 +}
    1.64 +
    1.65 +action parse_gid       { if (!parse_uint (tok, p, &info.codepoint)) return false; }
    1.66 +action parse_cluster   { if (!parse_uint (tok, p, &info.cluster )) return false; }
    1.67 +action parse_x_offset  { if (!parse_int  (tok, p, &pos.x_offset )) return false; }
    1.68 +action parse_y_offset  { if (!parse_int  (tok, p, &pos.y_offset )) return false; }
    1.69 +action parse_x_advance { if (!parse_int  (tok, p, &pos.x_advance)) return false; }
    1.70 +action parse_y_advance { if (!parse_int  (tok, p, &pos.y_advance)) return false; }
    1.71 +
    1.72 +unum	= '0' | [1-9] digit*;
    1.73 +num	= '-'? unum;
    1.74 +
    1.75 +comma = space* ',' space*;
    1.76 +colon = space* ':' space*;
    1.77 +
    1.78 +glyph_id = unum;
    1.79 +glyph_name = alpha (alnum|'_'|'.'|'-')*;
    1.80 +
    1.81 +glyph_string   = '"' (glyph_name >tok %parse_glyph) '"';
    1.82 +glyph_number = (glyph_id >tok %parse_gid);
    1.83 +
    1.84 +glyph	= "\"g\""  colon (glyph_string | glyph_number);
    1.85 +cluster	= "\"cl\"" colon (unum >tok %parse_cluster);
    1.86 +xoffset	= "\"dx\"" colon (num >tok %parse_x_offset);
    1.87 +yoffset	= "\"dy\"" colon (num >tok %parse_y_offset);
    1.88 +xadvance= "\"ax\"" colon (num >tok %parse_x_advance);
    1.89 +yadvance= "\"ay\"" colon (num >tok %parse_y_advance);
    1.90 +
    1.91 +element = glyph | cluster | xoffset | yoffset | xadvance | yadvance;
    1.92 +item	=
    1.93 +	( '{' space* element (comma element)* space* '}')
    1.94 +	>clear_item
    1.95 +	@add_item
    1.96 +	;
    1.97 +
    1.98 +main := space* item (comma item)* space* (','|']')?;
    1.99 +
   1.100 +}%%
   1.101 +
   1.102 +static hb_bool_t
   1.103 +_hb_buffer_deserialize_glyphs_json (hb_buffer_t *buffer,
   1.104 +				    const char *buf,
   1.105 +				    unsigned int buf_len,
   1.106 +				    const char **end_ptr,
   1.107 +				    hb_font_t *font)
   1.108 +{
   1.109 +  const char *p = buf, *pe = buf + buf_len;
   1.110 +
   1.111 +  /* Ensure we have positions. */
   1.112 +  (void) hb_buffer_get_glyph_positions (buffer, NULL);
   1.113 +
   1.114 +  while (p < pe && ISSPACE (*p))
   1.115 +    p++;
   1.116 +  if (p < pe && *p == (buffer->len ? ',' : '['))
   1.117 +  {
   1.118 +    *end_ptr = ++p;
   1.119 +  }
   1.120 +
   1.121 +  const char *tok = NULL;
   1.122 +  int cs;
   1.123 +  hb_glyph_info_t info;
   1.124 +  hb_glyph_position_t pos;
   1.125 +  %%{
   1.126 +    write init;
   1.127 +    write exec;
   1.128 +  }%%
   1.129 +
   1.130 +  *end_ptr = p;
   1.131 +
   1.132 +  return p == pe && *(p-1) != ']';
   1.133 +}
   1.134 +
   1.135 +#endif /* HB_BUFFER_DESERIALIZE_JSON_HH */

mercurial