xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-wasm-api.h (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2023  Behdad Esfahbod
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 
25 #ifndef HB_WASM_API_H
26 #define HB_WASM_API_H
27 
28 /*
29 #include "hb.h"
30 
31 HB_BEGIN_DECLS
32 HB_END_DECLS
33 */
34 
35 #include <stdint.h>
36 
37 
38 #ifndef HB_WASM_BEGIN_DECLS
39 # ifdef __cplusplus
40 #  define HB_WASM_BEGIN_DECLS	extern "C" {
41 #  define HB_WASM_END_DECLS	}
42 # else /* !__cplusplus */
43 #  define HB_WASM_BEGIN_DECLS
44 #  define HB_WASM_END_DECLS
45 # endif /* !__cplusplus */
46 #endif
47 
48 
49 HB_WASM_BEGIN_DECLS
50 
51 #ifndef HB_WASM_API
52 #define HB_WASM_API(ret_t, name) ret_t name
53 #endif
54 #ifndef HB_WASM_API_COMPOUND /* compound return type */
55 #define HB_WASM_API_COMPOUND(ret_t, name) HB_WASM_API(ret_t, name)
56 #endif
57 #ifndef HB_WASM_INTERFACE
58 #define HB_WASM_INTERFACE(ret_t, name) ret_t name
59 #endif
60 #ifndef HB_WASM_EXEC_ENV
61 #define HB_WASM_EXEC_ENV
62 #endif
63 #ifndef HB_WASM_EXEC_ENV_COMPOUND
64 #define HB_WASM_EXEC_ENV_COMPOUND HB_WASM_EXEC_ENV
65 #endif
66 
67 
68 #ifndef bool_t
69 #define bool_t uint32_t
70 #endif
71 #ifndef ptr_t
72 #define ptr_t(type_t) type_t *
73 #endif
74 #ifndef ptr_d
75 #define ptr_d(type_t, name) type_t *name
76 #endif
77 
78 typedef uint32_t codepoint_t;
79 typedef int32_t position_t;
80 typedef uint32_t mask_t;
81 typedef uint32_t tag_t;
82 #define TAG(c1,c2,c3,c4) ((tag_t)((((uint32_t)(c1)&0xFF)<<24)|(((uint32_t)(c2)&0xFF)<<16)|(((uint32_t)(c3)&0xFF)<<8)|((uint32_t)(c4)&0xFF)))
83 
84 typedef enum {
85   DIRECTION_INVALID = 0,
86   DIRECTION_LTR = 4,
87   DIRECTION_RTL,
88   DIRECTION_TTB,
89   DIRECTION_BTT
90 } direction_t;
91 #define DIRECTION_IS_VALID(dir)		((((unsigned int) (dir)) & ~3U) == 4)
92 #define DIRECTION_IS_HORIZONTAL(dir)	((((unsigned int) (dir)) & ~1U) == 4)
93 #define DIRECTION_IS_VERTICAL(dir)	((((unsigned int) (dir)) & ~1U) == 6)
94 #define DIRECTION_IS_FORWARD(dir)	((((unsigned int) (dir)) & ~2U) == 4)
95 #define DIRECTION_IS_BACKWARD(dir)	((((unsigned int) (dir)) & ~2U) == 5)
96 #define DIRECTION_REVERSE(dir)		((direction_t) (((unsigned int) (dir)) ^ 1))
97 
98 typedef tag_t script_t; /* ISO 15924 representation of Unicode scripts. */
99 
100 
101 /* common */
102 
103 HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV
104 							    script_t script);
105 
106 
107 /* blob */
108 
109 typedef struct
110 {
111   uint32_t length;
112   ptr_t(char) data;
113 } blob_t;
114 #define BLOB_INIT {0, 0}
115 
116 HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV
117 			       ptr_d(blob_t, blob));
118 
119 /* buffer */
120 
121 typedef struct
122 {
123   uint32_t codepoint;
124   uint32_t mask;
125   uint32_t cluster;
126   uint32_t var1;
127   uint32_t var2;
128 } glyph_info_t;
129 
130 typedef struct
131 {
132   position_t x_advance;
133   position_t y_advance;
134   position_t x_offset;
135   position_t y_offset;
136   uint32_t var;
137 } glyph_position_t;
138 
139 typedef struct
140 {
141   uint32_t length;
142   ptr_t(glyph_info_t) info;
143   ptr_t(glyph_position_t) pos;
144 } buffer_contents_t;
145 #define BUFFER_CONTENTS_INIT {0, 0, 0}
146 
147 HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV
148 					       ptr_d(buffer_contents_t, contents),
149 					       uint32_t size);
150 
151 HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV
152 					  ptr_d(buffer_contents_t, contents));
153 
154 typedef struct buffer_t buffer_t;
155 
156 HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV
157 					    ptr_d(buffer_t, buffer),
158 					    ptr_d(buffer_contents_t, contents));
159 
160 HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV
161 					   ptr_d(buffer_t, buffer),
162 					   ptr_d(const buffer_contents_t, contents));
163 
164 HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV
165 						 ptr_d(buffer_t, buffer));
166 
167 HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV
168 					   ptr_d(buffer_t, buffer));
169 
170 HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV
171 				    ptr_d(buffer_t, buffer));
172 
173 HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV
174 					     ptr_d(buffer_t, buffer));
175 
176 /* face */
177 
178 typedef struct face_t face_t;
179 
180 HB_WASM_API (ptr_t(face_t), face_create) (HB_WASM_EXEC_ENV
181 					  ptr_d(blob_t, blob),
182 					  unsigned int);
183 
184 HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV
185 				       ptr_d(face_t, face),
186 				       tag_t table_tag,
187 				       ptr_d(blob_t, blob));
188 
189 HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV
190 				       ptr_d(face_t, face));
191 
192 /* font */
193 
194 typedef struct font_t font_t;
195 
196 HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV
197 					  ptr_d(face_t, face));
198 
199 HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV
200 					    ptr_d(font_t, font));
201 
202 HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV
203 				    ptr_d(font_t, font),
204 				    ptr_d(int32_t, x_scale),
205 				    ptr_d(int32_t, y_scale));
206 
207 HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV
208 					      ptr_d(font_t, font),
209 					      codepoint_t unicode,
210 					      codepoint_t variation_selector);
211 
212 HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV
213 						    ptr_d(font_t, font),
214 						    codepoint_t glyph);
215 
216 HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV
217 						    ptr_d(font_t, font),
218 						    codepoint_t glyph);
219 
220 typedef struct
221 {
222   position_t x_bearing;
223   position_t y_bearing;
224   position_t width;
225   position_t height;
226 } glyph_extents_t;
227 
228 HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV
229 					      ptr_d(font_t, font),
230 					      codepoint_t glyph,
231 					      ptr_d(glyph_extents_t, extents));
232 
233 HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV
234 					  ptr_d(font_t, font),
235 					  codepoint_t glyph,
236 					  char *s, uint32_t size);
237 
238 
239 typedef struct
240 {
241   unsigned int length;
242   ptr_t(int) coords;
243 } coords_t;
244 
245 HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV
246 					  ptr_d(font_t, font),
247 					  ptr_d(coords_t, coords));
248 
249 HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV
250 					  ptr_d(font_t, font),
251 					  ptr_d(coords_t, coords));
252 
253 /* outline */
254 
255 enum glyph_outline_point_type_t
256 {
257   MOVE_TO,
258   LINE_TO,
259   QUADRATIC_TO,
260   CUBIC_TO,
261 };
262 
263 typedef struct
264 {
265   float x;
266   float y;
267   uint32_t type;
268 } glyph_outline_point_t;
269 
270 typedef struct
271 {
272   uint32_t n_points;
273   ptr_t(glyph_outline_point_t) points;
274   uint32_t n_contours;
275   ptr_t(uint32_t) contours;
276 } glyph_outline_t;
277 #define GLYPH_OUTLINE_INIT {0, 0, 0, 0}
278 
279 HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV
280 					ptr_d(glyph_outline_t, outline));
281 
282 HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV
283 					       ptr_d(font_t, font),
284 					       codepoint_t glyph,
285 					       ptr_d(glyph_outline_t, outline));
286 
287 
288 /* shape */
289 
290 typedef struct
291 {
292   tag_t    tag;
293   uint32_t value;
294   uint32_t start;
295   uint32_t end;
296 } feature_t;
297 #define FEATURE_GLOBAL_START	0
298 #define FEATURE_GLOBAL_END	((uint32_t) -1)
299 
300 HB_WASM_API (bool_t, shape_with) (HB_WASM_EXEC_ENV
301 				  ptr_d(font_t, font),
302 				  ptr_d(buffer_t, buffer),
303 				  ptr_d(const feature_t, features),
304 				  uint32_t num_features,
305 				  const char *shaper);
306 
307 /* Implement these in your shaper. */
308 
309 HB_WASM_INTERFACE (ptr_t(void), shape_plan_create) (ptr_d(face_t, face));
310 
311 HB_WASM_INTERFACE (bool_t, shape) (ptr_d(void, shape_plan),
312 				   ptr_d(font_t, font),
313 				   ptr_d(buffer_t, buffer),
314 				   ptr_d(const feature_t, features),
315 				   uint32_t num_features);
316 
317 HB_WASM_INTERFACE (void, shape_plan_destroy) (ptr_d(void, shape_plan));
318 
319 
320 HB_WASM_END_DECLS
321 
322 #endif /* HB_WASM_API_H */
323