2 * Copyright © 2023 Behdad Esfahbod
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 #ifndef HB_WASM_API_FONT_HH
26 #define HB_WASM_API_FONT_HH
28 #include "hb-wasm-api.hh"
30 #include "hb-outline.hh"
36 HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV
41 hb_font_t *font = hb_font_create (face);
47 HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV
52 hb_face_t *face = hb_font_get_face (font);
58 HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV
60 ptr_d(int32_t, x_scale),
61 ptr_d(int32_t, y_scale))
65 HB_PTR_PARAM(int32_t, x_scale);
66 HB_PTR_PARAM(int32_t, y_scale);
68 hb_font_get_scale (font, x_scale, y_scale);
71 HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV
74 codepoint_t variation_selector)
79 hb_font_get_glyph (font, unicode, variation_selector, &glyph);
83 HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV
88 return hb_font_get_glyph_h_advance (font, glyph);
91 HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV
96 return hb_font_get_glyph_v_advance (font, glyph);
99 static_assert (sizeof (glyph_extents_t) == sizeof (hb_glyph_extents_t), "");
101 HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV
104 ptr_d(glyph_extents_t, extents))
107 HB_PTR_PARAM (glyph_extents_t, extents);
108 if (unlikely (!extents))
111 return hb_font_get_glyph_extents (font, glyph,
112 (hb_glyph_extents_t *) extents);
115 HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV
118 char *s, uint32_t size)
122 hb_font_glyph_to_string (font, glyph, s, size);
125 static_assert (sizeof (glyph_outline_point_t) == sizeof (hb_outline_point_t), "");
126 static_assert (sizeof (uint32_t) == sizeof (hb_outline_t::contours[0]), "");
128 HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV
131 ptr_d(glyph_outline_t, outline))
134 HB_PTR_PARAM (glyph_outline_t, outline);
135 if (unlikely (!outline))
138 hb_outline_t hb_outline;
139 auto *funcs = hb_outline_recording_pen_get_funcs ();
141 hb_font_draw_glyph (font, glyph, funcs, &hb_outline);
143 if (unlikely (hb_outline.points.in_error () ||
144 hb_outline.contours.in_error ()))
146 outline->n_points = outline->n_contours = 0;
150 // TODO Check two buffers separately
151 if (hb_outline.points.length <= outline->n_points &&
152 hb_outline.contours.length <= outline->n_contours)
154 glyph_outline_point_t *points = HB_ARRAY_APP2NATIVE (glyph_outline_point_t, outline->points, hb_outline.points.length);
155 uint32_t *contours = HB_ARRAY_APP2NATIVE (uint32_t, outline->contours, hb_outline.contours.length);
157 if (unlikely (!points || !contours))
159 outline->n_points = outline->n_contours = 0;
163 hb_memcpy (points, hb_outline.points.arrayZ, hb_outline.points.get_size ());
164 hb_memcpy (contours, hb_outline.contours.arrayZ, hb_outline.contours.get_size ());
169 outline->n_points = hb_outline.points.length;
170 outline->points = wasm_runtime_module_dup_data (module_inst,
171 (const char *) hb_outline.points.arrayZ,
172 hb_outline.points.get_size ());
173 outline->n_contours = hb_outline.contours.length;
174 outline->contours = wasm_runtime_module_dup_data (module_inst,
175 (const char *) hb_outline.contours.arrayZ,
176 hb_outline.contours.get_size ());
178 if ((outline->n_points && !outline->points) ||
179 (!outline->n_contours && !outline->contours))
181 outline->n_points = outline->n_contours = 0;
188 HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV
189 ptr_d(glyph_outline_t, outline))
191 HB_PTR_PARAM (glyph_outline_t, outline);
192 if (unlikely (!outline))
195 module_free (outline->points);
196 module_free (outline->contours);
198 outline->n_points = 0;
199 outline->points = nullref;
200 outline->n_contours = 0;
201 outline->contours = nullref;
204 HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV
206 ptr_d(coords_t, coords))
209 HB_PTR_PARAM (coords_t, coords);
210 if (unlikely (!coords))
214 const int* our_coords = hb_font_get_var_coords_normalized(font, &our_length);
216 if (our_length <= coords->length) {
217 int *their_coords = HB_ARRAY_APP2NATIVE (int, coords->coords, our_length);
218 if (unlikely(!their_coords)) {
222 unsigned bytes = our_length * sizeof (int);
223 hb_memcpy (their_coords, our_coords, bytes);
228 module_free (coords->coords);
229 coords->length = our_length;
230 unsigned bytes = our_length * sizeof (int);
231 coords->coords = wasm_runtime_module_dup_data (module_inst, (const char *) our_coords, bytes);
232 if (our_length && !coords->coords)
241 HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV
243 ptr_d(coords_t, coords))
246 HB_PTR_PARAM (coords_t, coords);
247 if (unlikely (!coords))
250 unsigned length = coords->length;
252 if (unlikely (hb_unsigned_mul_overflows (length, sizeof (int), &bytes)))
255 const int *our_coords = (const int *) (validate_app_addr (coords->coords, bytes) ? addr_app_to_native (coords->coords) : nullptr);
256 hb_font_set_var_coords_normalized(font, our_coords, length);
263 #endif /* HB_WASM_API_FONT_HH */