1847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod/*
2847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * Copyright © 2013  Google, Inc.
3847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *
4847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *
6847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * Permission is hereby granted, without written agreement and without
7847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * software and its documentation for any purpose, provided that the
9847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * above copyright notice and the following two paragraphs appear in
10847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * all copies of this software.
11847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *
12847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * DAMAGE.
17847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *
18847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod *
24847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod * Google Author(s): Behdad Esfahbod
25847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod */
26847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
27847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod#ifndef HB_BUFFER_DESERIALIZE_TEXT_HH
28847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod#define HB_BUFFER_DESERIALIZE_TEXT_HH
29847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
30847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod#include "hb-private.hh"
31847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
32847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod%%{
33847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
3420b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodmachine deserialize_text;
3520b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodalphtype unsigned char;
3620b817ac21147e0a8edbf23ef071c2f4ce6c2b95Behdad Esfahbodwrite data;
37847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
38847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction clear_item {
39847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	memset (&info, 0, sizeof (info));
40847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	memset (&pos , 0, sizeof (pos ));
41847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}
42847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
43847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction add_item {
44847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	buffer->add_info (info);
45847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	if (buffer->in_error)
46847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	  return false;
47847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	buffer->pos[buffer->len - 1] = pos;
48847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	*end_ptr = p;
49847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}
50847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
51847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction tok {
52847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	tok = p;
53847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}
54847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
55847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_glyph {
56847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	if (!hb_font_glyph_from_string (font,
57847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod					tok, p - tok,
58847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod					&info.codepoint))
59847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	  return false;
60847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}
61847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
62847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_cluster   { if (!parse_uint (tok, p, &info.cluster )) return false; }
63847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_x_offset  { if (!parse_int  (tok, p, &pos.x_offset )) return false; }
64847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_y_offset  { if (!parse_int  (tok, p, &pos.y_offset )) return false; }
65847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_x_advance { if (!parse_int  (tok, p, &pos.x_advance)) return false; }
66847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodaction parse_y_advance { if (!parse_int  (tok, p, &pos.y_advance)) return false; }
67847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
68847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodunum	= '0' | [1-9] digit*;
69847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodnum	= '-'? unum;
70847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
71778d7f844cb330289830c95431269db06ba1c969Behdad Esfahbodglyph_id = unum;
72778d7f844cb330289830c95431269db06ba1c969Behdad Esfahbodglyph_name = alpha (alnum|'_'|'.'|'-')*;
73778d7f844cb330289830c95431269db06ba1c969Behdad Esfahbod
74778d7f844cb330289830c95431269db06ba1c969Behdad Esfahbodglyph	= (glyph_id | glyph_name) >tok %parse_glyph;
75847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodcluster	= '=' (unum >tok %parse_cluster);
76847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodoffsets	= '@' (num >tok %parse_x_offset)   ',' (num >tok %parse_y_offset );
77847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodadvances= '+' (num >tok %parse_x_advance) (',' (num >tok %parse_y_advance))?;
78847794e929831750e97525137ab5e285ccd1064eBehdad Esfahboditem	=
79847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	(
80847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod		glyph
81847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod		cluster?
82847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod		offsets?
83847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod		advances?
84847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	)
85847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	>clear_item
86847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	%add_item
87847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod	;
88847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
89847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodmain := space* item (space* '|' space* item)* space* ('|'|']')?;
90847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
91847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}%%
92847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
93847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbodstatic hb_bool_t
94847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod_hb_buffer_deserialize_glyphs_text (hb_buffer_t *buffer,
95847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod				    const char *buf,
96847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod				    unsigned int buf_len,
97847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod				    const char **end_ptr,
98847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod				    hb_font_t *font)
99847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod{
100847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  const char *p = buf, *pe = buf + buf_len;
101847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
102847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  /* Ensure we have positions. */
103847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  (void) hb_buffer_get_glyph_positions (buffer, NULL);
104847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
105847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  while (p < pe && ISSPACE (*p))
106847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod    p++;
107847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  if (p < pe && *p == (buffer->len ? '|' : '['))
108847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  {
109847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod    *end_ptr = ++p;
110847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  }
111847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
112847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  const char *eof = pe, *tok = NULL;
113847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  int cs;
114aee685086c8fde6f6c4590e483a177c19f222540Behdad Esfahbod  hb_glyph_info_t info = {0};
115aee685086c8fde6f6c4590e483a177c19f222540Behdad Esfahbod  hb_glyph_position_t pos = {0};
116847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  %%{
117847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod    write init;
118847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod    write exec;
119847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  }%%
120847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
121847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  *end_ptr = p;
122847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
123847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod  return p == pe && *(p-1) != ']';
124847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod}
125847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod
126847794e929831750e97525137ab5e285ccd1064eBehdad Esfahbod#endif /* HB_BUFFER_DESERIALIZE_TEXT_HH */
127