blob: 382423f6c7da8310a31677e97fe46a7ad159546c [file] [log] [blame]
Behdad Esfahbod20b817a2013-02-27 18:39:37 -05001/*
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
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb.hh"
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050031
32%%{
33
34machine deserialize_json;
35alphtype unsigned char;
36write data;
37
38action clear_item {
39 memset (&info, 0, sizeof (info));
40 memset (&pos , 0, sizeof (pos ));
41}
42
43action add_item {
44 buffer->add_info (info);
Behdad Esfahbod7185b272018-05-31 20:03:00 -070045 if (unlikely (!buffer->successful))
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050046 return false;
47 buffer->pos[buffer->len - 1] = pos;
48 *end_ptr = p;
49}
50
51action tok {
52 tok = p;
53}
54
Behdad Esfahbod5ef06132020-10-15 01:54:28 -060055action ensure_glyphs { if (unlikely (!buffer->ensure_glyphs ())) return false; }
56action ensure_unicode { if (unlikely (!buffer->ensure_unicode ())) return false; }
Simon Cozens150f3912020-09-25 10:04:39 +010057
Behdad Esfahbod4a4eebc2020-10-09 21:06:20 -060058action parse_glyph_name {
Behdad Esfahbodc396e162020-10-09 21:13:10 -060059 /* TODO Unescape \" and \\ if found. */
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050060 if (!hb_font_glyph_from_string (font,
61 tok, p - tok,
62 &info.codepoint))
63 return false;
64}
65
Behdad Esfahbod4a4eebc2020-10-09 21:06:20 -060066action parse_codepoint { if (!parse_uint (tok, p, &info.codepoint)) return false; }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050067action parse_cluster { if (!parse_uint (tok, p, &info.cluster )) return false; }
68action parse_x_offset { if (!parse_int (tok, p, &pos.x_offset )) return false; }
69action parse_y_offset { if (!parse_int (tok, p, &pos.y_offset )) return false; }
70action parse_x_advance { if (!parse_int (tok, p, &pos.x_advance)) return false; }
71action parse_y_advance { if (!parse_int (tok, p, &pos.y_advance)) return false; }
72
73unum = '0' | [1-9] digit*;
74num = '-'? unum;
75
76comma = space* ',' space*;
77colon = space* ':' space*;
78
Behdad Esfahbod4a4eebc2020-10-09 21:06:20 -060079codepoint = unum;
Behdad Esfahbodc396e162020-10-09 21:13:10 -060080glyph_name = '"' ([^\\"] | '\\' [\\"])* '"';
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050081
Behdad Esfahbodc396e162020-10-09 21:13:10 -060082parse_glyph_name = (glyph_name >tok %parse_glyph_name);
Behdad Esfahbod4a4eebc2020-10-09 21:06:20 -060083parse_codepoint = (codepoint >tok %parse_codepoint);
Behdad Esfahbod778d7f82013-02-27 18:47:26 -050084
Behdad Esfahbodc396e162020-10-09 21:13:10 -060085glyph = "\"g\"" colon (parse_glyph_name | parse_codepoint);
86unicode = "\"u\"" colon parse_codepoint;
Behdad Esfahbod20b817a2013-02-27 18:39:37 -050087cluster = "\"cl\"" colon (unum >tok %parse_cluster);
88xoffset = "\"dx\"" colon (num >tok %parse_x_offset);
89yoffset = "\"dy\"" colon (num >tok %parse_y_offset);
90xadvance= "\"ax\"" colon (num >tok %parse_x_advance);
91yadvance= "\"ay\"" colon (num >tok %parse_y_advance);
92
Behdad Esfahbodc396e162020-10-09 21:13:10 -060093element = glyph @ensure_glyphs
94 | unicode @ensure_unicode
95 | cluster
96 | xoffset
97 | yoffset
98 | xadvance
99 | yadvance;
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500100item =
101 ( '{' space* element (comma element)* space* '}')
102 >clear_item
103 @add_item
104 ;
105
Simon Cozens3d3c87e2020-09-21 14:35:05 +0100106main := space* item (comma item)* space* (','|']')?;
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500107
108}%%
109
110static hb_bool_t
Simon Cozensc03a2002020-09-18 14:19:17 +0100111_hb_buffer_deserialize_json (hb_buffer_t *buffer,
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500112 const char *buf,
113 unsigned int buf_len,
114 const char **end_ptr,
115 hb_font_t *font)
116{
117 const char *p = buf, *pe = buf + buf_len;
118
119 /* Ensure we have positions. */
Bruce Mitchenera8957372018-02-04 01:31:53 +0700120 (void) hb_buffer_get_glyph_positions (buffer, nullptr);
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500121
122 while (p < pe && ISSPACE (*p))
123 p++;
124 if (p < pe && *p == (buffer->len ? ',' : '['))
125 {
126 *end_ptr = ++p;
127 }
128
Bruce Mitchenera8957372018-02-04 01:31:53 +0700129 const char *tok = nullptr;
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500130 int cs;
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700131 hb_glyph_info_t info = {0};
132 hb_glyph_position_t pos = {0};
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500133 %%{
134 write init;
135 write exec;
136 }%%
137
138 *end_ptr = p;
139
140 return p == pe && *(p-1) != ']';
141}
142
143#endif /* HB_BUFFER_DESERIALIZE_JSON_HH */