Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-wasm-api-face.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_FACE_HH
26 #define HB_WASM_API_FACE_HH
27
28 #include "hb-wasm-api.hh"
29
30 namespace hb {
31 namespace wasm {
32
33
34 HB_WASM_API (ptr_t(face_t), face_create) (HB_WASM_EXEC_ENV
35                                           ptr_d(blob_t, blob),
36                                           unsigned int index)
37 {
38   HB_PTR_PARAM (blob_t, blob);
39   hb_blob_t *hb_blob = hb_blob_create(
40                                       HB_ARRAY_APP2NATIVE (char, blob->data, blob->length),
41                                       blob->length,
42                                       HB_MEMORY_MODE_DUPLICATE,
43                                       NULL,
44                                       NULL);
45
46   hb_face_t *face = hb_face_create(hb_blob, index);
47
48   HB_OBJ2REF (face);
49   return faceref;
50 }
51
52 HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV
53                                        ptr_d(face_t, face),
54                                        tag_t table_tag,
55                                        ptr_d(blob_t, blob))
56 {
57   HB_REF2OBJ (face);
58   HB_PTR_PARAM (blob_t, blob);
59   if (unlikely (!blob))
60     return false;
61
62   hb_blob_t *hb_blob = hb_face_reference_table (face, table_tag);
63
64   unsigned length;
65   const char *hb_data = hb_blob_get_data (hb_blob, &length);
66
67   if (length <= blob->length)
68   {
69     char *data = HB_ARRAY_APP2NATIVE (char, blob->data, length);
70
71     if (unlikely (!data))
72     {
73       blob->length = 0;
74       return false;
75     }
76
77     hb_memcpy (data, hb_data, length);
78
79     return true;
80   }
81
82   module_free (blob->data);
83
84   blob->length = length;
85   blob->data = wasm_runtime_module_dup_data (module_inst, hb_data, length);
86
87   hb_blob_destroy (hb_blob);
88
89   if (blob->length && !blob->data)
90   {
91     blob->length = 0;
92     return false;
93   }
94
95   return true;
96 }
97
98 HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV
99                                        ptr_d(face_t, face))
100 {
101   HB_REF2OBJ (face);
102
103   return hb_face_get_upem (face);
104 }
105
106
107 }}
108
109 #endif /* HB_WASM_API_FACE_HH */