Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-wasm-api-font.hh
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_FONT_HH
26 #define HB_WASM_API_FONT_HH
27
28 #include "hb-wasm-api.hh"
29
30 #include "hb-outline.hh"
31
32 namespace hb {
33 namespace wasm {
34
35
36 HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV
37                                           ptr_d(face_t, face))
38 {
39   HB_REF2OBJ (face);
40
41   hb_font_t *font = hb_font_create (face);
42
43   HB_OBJ2REF (font);
44   return fontref;
45 }
46
47 HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV
48                                             ptr_d(font_t, font))
49 {
50   HB_REF2OBJ (font);
51
52   hb_face_t *face = hb_font_get_face (font);
53
54   HB_OBJ2REF (face);
55   return faceref;
56 }
57
58 HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV
59                                     ptr_d(font_t, font),
60                                     ptr_d(int32_t, x_scale),
61                                     ptr_d(int32_t, y_scale))
62 {
63   HB_REF2OBJ (font);
64
65   HB_PTR_PARAM(int32_t, x_scale);
66   HB_PTR_PARAM(int32_t, y_scale);
67
68   hb_font_get_scale (font, x_scale, y_scale);
69 }
70
71 HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV
72                                               ptr_d(font_t, font),
73                                               codepoint_t unicode,
74                                               codepoint_t variation_selector)
75 {
76   HB_REF2OBJ (font);
77   codepoint_t glyph;
78
79   hb_font_get_glyph (font, unicode, variation_selector, &glyph);
80   return glyph;
81 }
82
83 HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV
84                                                     ptr_d(font_t, font),
85                                                     codepoint_t glyph)
86 {
87   HB_REF2OBJ (font);
88   return hb_font_get_glyph_h_advance (font, glyph);
89 }
90
91 HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV
92                                                     ptr_d(font_t, font),
93                                                     codepoint_t glyph)
94 {
95   HB_REF2OBJ (font);
96   return hb_font_get_glyph_v_advance (font, glyph);
97 }
98
99 static_assert (sizeof (glyph_extents_t) == sizeof (hb_glyph_extents_t), "");
100
101 HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV
102                                               ptr_d(font_t, font),
103                                               codepoint_t glyph,
104                                               ptr_d(glyph_extents_t, extents))
105 {
106   HB_REF2OBJ (font);
107   HB_PTR_PARAM (glyph_extents_t, extents);
108   if (unlikely (!extents))
109     return false;
110
111   return hb_font_get_glyph_extents (font, glyph,
112                                     (hb_glyph_extents_t *) extents);
113 }
114
115 HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV
116                                           ptr_d(font_t, font),
117                                           codepoint_t glyph,
118                                           char *s, uint32_t size)
119 {
120   HB_REF2OBJ (font);
121
122   hb_font_glyph_to_string (font, glyph, s, size);
123 }
124
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]), "");
127
128 HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV
129                                                ptr_d(font_t, font),
130                                                codepoint_t glyph,
131                                                ptr_d(glyph_outline_t, outline))
132 {
133   HB_REF2OBJ (font);
134   HB_PTR_PARAM (glyph_outline_t, outline);
135   if (unlikely (!outline))
136     return false;
137
138   hb_outline_t hb_outline;
139   auto *funcs = hb_outline_recording_pen_get_funcs ();
140
141   hb_font_draw_glyph (font, glyph, funcs, &hb_outline);
142
143   if (unlikely (hb_outline.points.in_error () ||
144                 hb_outline.contours.in_error ()))
145   {
146     outline->n_points = outline->n_contours = 0;
147     return false;
148   }
149
150   // TODO Check two buffers separately
151   if (hb_outline.points.length <= outline->n_points &&
152       hb_outline.contours.length <= outline->n_contours)
153   {
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);
156
157     if (unlikely (!points || !contours))
158     {
159       outline->n_points = outline->n_contours = 0;
160       return false;
161     }
162
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 ());
165
166     return true;
167   }
168
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 ());
177
178   if ((outline->n_points && !outline->points) ||
179       (!outline->n_contours && !outline->contours))
180   {
181     outline->n_points = outline->n_contours = 0;
182     return false;
183   }
184
185   return true;
186 }
187
188 HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV
189                                         ptr_d(glyph_outline_t, outline))
190 {
191   HB_PTR_PARAM (glyph_outline_t, outline);
192   if (unlikely (!outline))
193     return;
194
195   module_free (outline->points);
196   module_free (outline->contours);
197
198   outline->n_points = 0;
199   outline->points = nullref;
200   outline->n_contours = 0;
201   outline->contours = nullref;
202 }
203
204 HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV
205                                           ptr_d(font_t, font),
206                                           ptr_d(coords_t, coords))
207 {
208   HB_REF2OBJ (font);
209   HB_PTR_PARAM (coords_t, coords);
210   if (unlikely (!coords))
211     return false;
212
213   unsigned our_length;
214   const int* our_coords = hb_font_get_var_coords_normalized(font, &our_length);
215
216   if (our_length <= coords->length) {
217     int *their_coords = HB_ARRAY_APP2NATIVE (int, coords->coords, our_length);
218     if (unlikely(!their_coords)) {
219         coords->length = 0;
220         return false;
221     }
222                 unsigned bytes = our_length * sizeof (int);
223     hb_memcpy (their_coords, our_coords, bytes);
224
225     return true;
226   }
227
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)
233           {
234     coords->length = 0;
235     return false;
236   }
237
238   return true;
239 }
240
241 HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV
242                                           ptr_d(font_t, font),
243                                           ptr_d(coords_t, coords))
244 {
245   HB_REF2OBJ (font);
246   HB_PTR_PARAM (coords_t, coords);
247   if (unlikely (!coords))
248     return false;
249
250   unsigned length = coords->length;
251   unsigned bytes;
252   if (unlikely (hb_unsigned_mul_overflows (length, sizeof (int), &bytes)))
253     return false;
254
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);
257   return true;
258 }
259
260
261 }}
262
263 #endif /* HB_WASM_API_FONT_HH */