From 425fc7f3ee257b7aee9a481a04d368c4ccf57c4d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 22 Feb 2023 12:19:06 -0700 Subject: [PATCH 001/149] [wasm] Add wasm shaper skeleton --- meson.build | 2 + src/Makefile.sources | 1 + src/harfbuzz-subset.cc | 1 + src/harfbuzz.cc | 1 + src/hb-shaper-list.hh | 5 +++ src/hb-wasm-shape.cc | 100 +++++++++++++++++++++++++++++++++++++++++ src/meson.build | 1 + 7 files changed, 111 insertions(+) create mode 100644 src/hb-wasm-shape.cc diff --git a/meson.build b/meson.build index 6bf8d05da..95dbf2795 100644 --- a/meson.build +++ b/meson.build @@ -229,6 +229,8 @@ if chafa_dep.found() conf.set('HAVE_CHAFA', 1) endif +conf.set('HAVE_WASM', 1) + if graphite2_dep.found() or graphite_dep.found() conf.set('HAVE_GRAPHITE2', 1) endif diff --git a/src/Makefile.sources b/src/Makefile.sources index fd7c3b55e..4edea33c8 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -255,6 +255,7 @@ HB_BASE_sources = \ hb-unicode.hh \ hb-utf.hh \ hb-vector.hh \ + hb-wasm-shape.cc \ hb-priority-queue.hh \ hb.hh \ $(NULL) diff --git a/src/harfbuzz-subset.cc b/src/harfbuzz-subset.cc index c0e23b3eb..743a3539d 100644 --- a/src/harfbuzz-subset.cc +++ b/src/harfbuzz-subset.cc @@ -60,3 +60,4 @@ #include "hb-subset.cc" #include "hb-ucd.cc" #include "hb-unicode.cc" +#include "hb-wasm-shape.cc" diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index d7e8a93f3..5b8196126 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -58,3 +58,4 @@ #include "hb-ucd.cc" #include "hb-unicode.cc" #include "hb-uniscribe.cc" +#include "hb-wasm-shape.cc" diff --git a/src/hb-shaper-list.hh b/src/hb-shaper-list.hh index 4158b7094..f079caf4d 100644 --- a/src/hb-shaper-list.hh +++ b/src/hb-shaper-list.hh @@ -33,6 +33,11 @@ /* v--- Add new shapers in the right place here. */ +#ifdef HAVE_WASM +/* Only picks up fonts that have a "Wasm" table. */ +HB_SHAPER_IMPLEMENT (wasm) +#endif + #ifdef HAVE_GRAPHITE2 /* Only picks up fonts that have a "Silf" table. */ HB_SHAPER_IMPLEMENT (graphite2) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc new file mode 100644 index 000000000..e67b880dc --- /dev/null +++ b/src/hb-wasm-shape.cc @@ -0,0 +1,100 @@ +/* + * Copyright © 2011 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include "hb-shaper-impl.hh" + +#ifdef HAVE_WASM + +/* + * shaper face data + */ + +#define HB_WASM_TAG_WASM HB_TAG('W','a','s','m') + +struct hb_wasm_face_data_t { + hb_blob_t *blob; +}; + +hb_wasm_face_data_t * +_hb_wasm_shaper_face_data_create (hb_face_t *face) +{ + hb_blob_t *wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); + if (!hb_blob_get_length (wasm_blob)) + { + hb_blob_destroy (wasm_blob); + return nullptr; + } + + hb_wasm_face_data_t *data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); + if (unlikely (!data)) + return nullptr; + + data->blob = wasm_blob; + + return data; +} + +void +_hb_wasm_shaper_face_data_destroy (hb_wasm_face_data_t *data) +{ + hb_blob_destroy (data->blob); + hb_free (data); +} + + +/* + * shaper font data + */ + +struct hb_wasm_font_data_t {}; + +hb_wasm_font_data_t * +_hb_wasm_shaper_font_data_create (hb_font_t *font HB_UNUSED) +{ + return (hb_wasm_font_data_t *) HB_SHAPER_DATA_SUCCEEDED; +} + +void +_hb_wasm_shaper_font_data_destroy (hb_wasm_font_data_t *data HB_UNUSED) +{ +} + + +/* + * shaper + */ + +hb_bool_t +_hb_wasm_shape (hb_shape_plan_t *shape_plan, + hb_font_t *font, + hb_buffer_t *buffer, + const hb_feature_t *features, + unsigned int num_features) +{ + return false; +} + +#endif diff --git a/src/meson.build b/src/meson.build index 30b99cf34..263677f84 100644 --- a/src/meson.build +++ b/src/meson.build @@ -256,6 +256,7 @@ hb_base_sources = files( 'hb-unicode.hh', 'hb-utf.hh', 'hb-vector.hh', + 'hb-wasm-shape.cc', 'hb.hh', ) From fcc8be409b8a8034af54caa0ccd0a91206fdbf6b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 22 Feb 2023 12:57:19 -0700 Subject: [PATCH 002/149] [wasm] Add meson build option Autotools support missing. --- meson.build | 5 +++- meson_options.txt | 2 ++ src/hb-wasm-shape.cc | 55 +++++++++++++++++++++++++++++++++++++------- src/meson.build | 10 +++++++- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/meson.build b/meson.build index 95dbf2795..63ded12f9 100644 --- a/meson.build +++ b/meson.build @@ -114,6 +114,7 @@ glib_dep = dependency('glib-2.0', required: get_option('glib')) gobject_dep = dependency('gobject-2.0', required: get_option('gobject')) graphite2_dep = dependency('graphite2', required: get_option('graphite2')) graphite_dep = dependency('graphite2', required: get_option('graphite')) +wasm_dep = cpp.find_library('iwasm', required: get_option('wasm')) if meson.version().version_compare('>=0.60.0') # pkg-config: icu-uc, cmake: ICU but with components @@ -229,7 +230,9 @@ if chafa_dep.found() conf.set('HAVE_CHAFA', 1) endif -conf.set('HAVE_WASM', 1) +if wasm_dep.found() + conf.set('HAVE_WASM', 1) +endif if graphite2_dep.found() or graphite_dep.found() conf.set('HAVE_GRAPHITE2', 1) diff --git a/meson_options.txt b/meson_options.txt index 195d556e8..5cdfd8fef 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -21,6 +21,8 @@ option('directwrite', type: 'feature', value: 'disabled', description: 'Enable DirectWrite shaper backend on Windows (experimental)') option('coretext', type: 'feature', value: 'disabled', description: 'Enable CoreText shaper backend on macOS') +option('wasm', type: 'feature', value: 'disabled', + description: 'Enable WebAssembly shaper backend') # Common feature options option('tests', type: 'feature', value: 'enabled', yield: true, diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index e67b880dc..4e87dbd3a 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -28,6 +28,25 @@ #ifdef HAVE_WASM +#include + +#define own // wasm-micro-runtime wasm-c-api/hello.c example has this; no idea why :)) + +static wasm_store_t * +get_wasm_store () +{ + + static wasm_store_t *store; + if (!store) + { + static wasm_engine_t *engine = wasm_engine_new(); + store = wasm_store_new (engine); + } + + return store; +} + + /* * shaper face data */ @@ -36,30 +55,50 @@ struct hb_wasm_face_data_t { hb_blob_t *blob; + wasm_module_t *mod; }; hb_wasm_face_data_t * _hb_wasm_shaper_face_data_create (hb_face_t *face) { - hb_blob_t *wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); - if (!hb_blob_get_length (wasm_blob)) - { - hb_blob_destroy (wasm_blob); - return nullptr; - } + hb_blob_t *wasm_blob = nullptr; + own wasm_module_t *wasm_module = nullptr; + hb_wasm_face_data_t *data = nullptr; - hb_wasm_face_data_t *data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); + wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); + unsigned length = hb_blob_get_length (wasm_blob); + if (!length) + goto fail; + + wasm_byte_vec_t binary; + wasm_byte_vec_new_uninitialized (&binary, length); + memcpy (binary.data, hb_blob_get_data (wasm_blob, nullptr), length); + wasm_module = wasm_module_new (get_wasm_store (), &binary); + if (!wasm_module) + goto fail; + wasm_byte_vec_delete(&binary); + + data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); if (unlikely (!data)) - return nullptr; + goto fail; data->blob = wasm_blob; + data->mod = wasm_module; return data; + +fail: + if (wasm_module) + wasm_module_delete (wasm_module); + hb_blob_destroy (wasm_blob); + hb_free (data); + return nullptr; } void _hb_wasm_shaper_face_data_destroy (hb_wasm_face_data_t *data) { + wasm_module_delete (data->mod); hb_blob_destroy (data->blob); hb_free (data); } diff --git a/src/meson.build b/src/meson.build index 263677f84..f5a1ea975 100644 --- a/src/meson.build +++ b/src/meson.build @@ -256,7 +256,6 @@ hb_base_sources = files( 'hb-unicode.hh', 'hb-utf.hh', 'hb-vector.hh', - 'hb-wasm-shape.cc', 'hb.hh', ) @@ -325,6 +324,9 @@ hb_glib_headers = files('hb-glib.h') hb_graphite2_sources = files('hb-graphite2.cc') hb_graphite2_headers = files('hb-graphite2.h') +hb_wasm_sources = files('hb-wasm-shape.cc') +hb_wasm_headers = files() + # System-dependent sources and headers hb_coretext_sources = files('hb-coretext.cc') @@ -460,6 +462,12 @@ if conf.get('HAVE_GRAPHITE2', 0) == 1 harfbuzz_deps += [graphite2_dep, graphite_dep] endif +if conf.get('HAVE_WASM', 0) == 1 + hb_sources += hb_wasm_sources + hb_headers += hb_wasm_headers + harfbuzz_deps += wasm_dep +endif + if conf.get('HAVE_UNISCRIBE', 0) == 1 hb_sources += hb_uniscribe_sources hb_headers += hb_uniscribe_headers From db8e51e01b972aaf6d32309ef5aa381964950af4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 22 Feb 2023 13:33:34 -0700 Subject: [PATCH 003/149] [wasm] More boilerplate --- src/hb-wasm-shape.cc | 69 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 4e87dbd3a..e3cc7aa24 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -56,38 +56,59 @@ get_wasm_store () struct hb_wasm_face_data_t { hb_blob_t *blob; wasm_module_t *mod; + unsigned shape_func_idx; }; hb_wasm_face_data_t * _hb_wasm_shaper_face_data_create (hb_face_t *face) { + wasm_store_t *wasm_store = nullptr; hb_blob_t *wasm_blob = nullptr; own wasm_module_t *wasm_module = nullptr; hb_wasm_face_data_t *data = nullptr; + wasm_exporttype_vec_t exports = {}; + unsigned shape_func_idx = (unsigned) -1; wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); unsigned length = hb_blob_get_length (wasm_blob); if (!length) goto fail; + wasm_store = get_wasm_store (); // Do before others to initialize + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized (&binary, length); memcpy (binary.data, hb_blob_get_data (wasm_blob, nullptr), length); - wasm_module = wasm_module_new (get_wasm_store (), &binary); + wasm_module = wasm_module_new (wasm_store, &binary); if (!wasm_module) goto fail; wasm_byte_vec_delete(&binary); + wasm_module_exports (wasm_module, &exports); + for (unsigned i = 0; i < exports.size; i++) + { + auto *name = wasm_exporttype_name(exports.data[i]); + if (0 == memcmp ("shape", name->data, name->size)) + { + shape_func_idx = i; + break; + } + } + if (shape_func_idx == (unsigned) -1) + goto fail; + data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); if (unlikely (!data)) goto fail; data->blob = wasm_blob; data->mod = wasm_module; + data->shape_func_idx = shape_func_idx; return data; fail: + wasm_exporttype_vec_delete (&exports); if (wasm_module) wasm_module_delete (wasm_module); hb_blob_destroy (wasm_blob); @@ -133,7 +154,51 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, const hb_feature_t *features, unsigned int num_features) { - return false; + bool ret = true; + const hb_wasm_face_data_t *face_data = font->face->data.wasm; + own wasm_instance_t *instance = nullptr; + const wasm_func_t *run_func = nullptr; + wasm_trap_t *trap = nullptr; + own wasm_extern_vec_t exports = {}; + + wasm_val_t as[1] = { WASM_I32_VAL(6) }; + wasm_val_t rs[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC (as); + wasm_val_vec_t results = WASM_ARRAY_VEC (rs); + + instance = wasm_instance_new (get_wasm_store (), face_data->mod, nullptr/*imports*/, nullptr); + if (!instance) + goto fail; + + wasm_instance_exports (instance, &exports); + if (exports.size == 0) + goto fail; + + run_func = wasm_extern_as_func (exports.data[face_data->shape_func_idx]); + if (!run_func) + goto fail; + + trap = wasm_func_call (run_func, &args, &results); + if (trap) + { + goto fail; + } + + ret = bool (rs[0].of.i32); + + if (0) + { +fail: + ret = false; + } + + wasm_extern_vec_delete (&exports); + if (instance) + wasm_instance_delete(instance); + if (trap) + wasm_trap_delete (trap); + + return ret; } #endif From bdbc1568bac50371d4b552e179ecfc338f12fd43 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 22 Feb 2023 23:30:28 -0700 Subject: [PATCH 004/149] [wasm] Port from c_api to wasm-micro-runtime wasm_runtime API --- src/hb-debug.hh | 4 + src/hb-wasm-shape.cc | 196 ++++++++++++++++++++++++++----------------- 2 files changed, 122 insertions(+), 78 deletions(-) diff --git a/src/hb-debug.hh b/src/hb-debug.hh index 0ac4515fa..6055fce96 100644 --- a/src/hb-debug.hh +++ b/src/hb-debug.hh @@ -389,6 +389,10 @@ struct hb_no_trace_t { #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0) #endif +#ifndef HB_DEBUG_WASM +#define HB_DEBUG_WASM (HB_DEBUG+0) +#endif + /* * With tracing. */ diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index e3cc7aa24..3ae2cbfec 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -24,27 +24,13 @@ * Google Author(s): Behdad Esfahbod */ +#define HB_DEBUG_WASM 1 + #include "hb-shaper-impl.hh" #ifdef HAVE_WASM -#include - -#define own // wasm-micro-runtime wasm-c-api/hello.c example has this; no idea why :)) - -static wasm_store_t * -get_wasm_store () -{ - - static wasm_store_t *store; - if (!store) - { - static wasm_engine_t *engine = wasm_engine_new(); - store = wasm_store_new (engine); - } - - return store; -} +#include /* @@ -54,63 +40,103 @@ get_wasm_store () #define HB_WASM_TAG_WASM HB_TAG('W','a','s','m') struct hb_wasm_face_data_t { - hb_blob_t *blob; - wasm_module_t *mod; - unsigned shape_func_idx; + hb_blob_t *wasm_blob; + wasm_module_t wasm_module; }; +static bool +init_wasm () +{ + static bool initialized; + if (initialized) + return true; + + RuntimeInitArgs init_args; + memset (&init_args, 0, sizeof (RuntimeInitArgs)); + + // Define an array of NativeSymbol for the APIs to be exported. + // Note: the array must be static defined since runtime + // will keep it after registration + // For the function signature specifications, goto the link: + // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md + + static NativeSymbol native_symbols[] = { +#if 0 + { + "intToStr", // the name of WASM function name + intToStr, // the native function pointer + "(i*~i)i", // the function prototype signature, avoid to use i32 + NULL // attachment is NULL + }, + { + "get_pow", // the name of WASM function name + get_pow, // the native function pointer + "(ii)i", // the function prototype signature, avoid to use i32 + NULL // attachment is NULL + }, + { "calculate_native", calculate_native, "(iii)i", NULL } +#endif + }; + + init_args.mem_alloc_type = Alloc_With_Allocator; + init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; + init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; + init_args.mem_alloc_option.allocator.free_func = (void *) hb_free; + + init_args.mem_alloc_type = Alloc_With_System_Allocator; + + // Native symbols need below registration phase + init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol); + init_args.native_module_name = "env"; + init_args.native_symbols = native_symbols; + + if (!wasm_runtime_full_init (&init_args)) + { + DEBUG_MSG (WASM, nullptr, "Init runtime environment failed."); + return false; + } + + + initialized = true; + return true; +} + hb_wasm_face_data_t * _hb_wasm_shaper_face_data_create (hb_face_t *face) { - wasm_store_t *wasm_store = nullptr; - hb_blob_t *wasm_blob = nullptr; - own wasm_module_t *wasm_module = nullptr; hb_wasm_face_data_t *data = nullptr; - wasm_exporttype_vec_t exports = {}; - unsigned shape_func_idx = (unsigned) -1; + hb_blob_t *wasm_blob = nullptr; + wasm_module_t wasm_module = nullptr; wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); unsigned length = hb_blob_get_length (wasm_blob); if (!length) goto fail; - wasm_store = get_wasm_store (); // Do before others to initialize + if (!init_wasm ()) + goto fail; - wasm_byte_vec_t binary; - wasm_byte_vec_new_uninitialized (&binary, length); - memcpy (binary.data, hb_blob_get_data (wasm_blob, nullptr), length); - wasm_module = wasm_module_new (wasm_store, &binary); + wasm_module = wasm_runtime_load ((uint8_t *) hb_blob_get_data_writable (wasm_blob, nullptr), + length, nullptr, 0); if (!wasm_module) - goto fail; - wasm_byte_vec_delete(&binary); - - wasm_module_exports (wasm_module, &exports); - for (unsigned i = 0; i < exports.size; i++) { - auto *name = wasm_exporttype_name(exports.data[i]); - if (0 == memcmp ("shape", name->data, name->size)) - { - shape_func_idx = i; - break; - } - } - if (shape_func_idx == (unsigned) -1) + DEBUG_MSG (WASM, nullptr, "Load wasm module failed."); goto fail; + } + data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); if (unlikely (!data)) goto fail; - data->blob = wasm_blob; - data->mod = wasm_module; - data->shape_func_idx = shape_func_idx; + data->wasm_blob = wasm_blob; + data->wasm_module = wasm_module; return data; fail: - wasm_exporttype_vec_delete (&exports); if (wasm_module) - wasm_module_delete (wasm_module); + wasm_runtime_unload (wasm_module); hb_blob_destroy (wasm_blob); hb_free (data); return nullptr; @@ -119,8 +145,8 @@ fail: void _hb_wasm_shaper_face_data_destroy (hb_wasm_face_data_t *data) { - wasm_module_delete (data->mod); - hb_blob_destroy (data->blob); + wasm_runtime_unload (data->wasm_module); + hb_blob_destroy (data->wasm_blob); hb_free (data); } @@ -156,35 +182,50 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, { bool ret = true; const hb_wasm_face_data_t *face_data = font->face->data.wasm; - own wasm_instance_t *instance = nullptr; - const wasm_func_t *run_func = nullptr; - wasm_trap_t *trap = nullptr; - own wasm_extern_vec_t exports = {}; + constexpr uint32_t stack_size = 8092, heap_size = 8092; - wasm_val_t as[1] = { WASM_I32_VAL(6) }; - wasm_val_t rs[1] = { WASM_INIT_VAL }; - wasm_val_vec_t args = WASM_ARRAY_VEC (as); - wasm_val_vec_t results = WASM_ARRAY_VEC (rs); + wasm_module_inst_t module_inst = nullptr; + wasm_exec_env_t exec_env = nullptr; + wasm_function_inst_t shape_func = nullptr; - instance = wasm_instance_new (get_wasm_store (), face_data->mod, nullptr/*imports*/, nullptr); - if (!instance) - goto fail; - - wasm_instance_exports (instance, &exports); - if (exports.size == 0) - goto fail; - - run_func = wasm_extern_as_func (exports.data[face_data->shape_func_idx]); - if (!run_func) - goto fail; - - trap = wasm_func_call (run_func, &args, &results); - if (trap) + module_inst = wasm_runtime_instantiate(face_data->wasm_module, + stack_size, heap_size, + nullptr, 0); + if (!module_inst) { + DEBUG_MSG (WASM, face_data->wasm_module, "Instantiate wasm module failed."); goto fail; } - ret = bool (rs[0].of.i32); + exec_env = wasm_runtime_create_exec_env (module_inst, stack_size); + if (!exec_env) { + DEBUG_MSG (WASM, module_inst, "Create wasm execution environment failed."); + goto fail; + } + + if (!(shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr))) { + DEBUG_MSG (WASM, module_inst, "Shape function not found."); + } + + wasm_val_t results[1]; + wasm_val_t arguments[1]; + + results[0].kind = WASM_I32; + arguments[0].kind = WASM_I32; + arguments[0].of.i32 = 41; + + if (!wasm_runtime_call_wasm_a (exec_env, shape_func, 1, results, 1, arguments)) + { + DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", + wasm_runtime_get_exception(module_inst)); + goto fail; + } + + printf ("%i\n", results[0].of.i32); + + + if (!face_data) + goto fail; if (0) { @@ -192,11 +233,10 @@ fail: ret = false; } - wasm_extern_vec_delete (&exports); - if (instance) - wasm_instance_delete(instance); - if (trap) - wasm_trap_delete (trap); + if (exec_env) + wasm_runtime_destroy_exec_env (exec_env); + if (module_inst) + wasm_runtime_deinstantiate (module_inst); return ret; } From 05bf9842128376db20b1c1da91efe0257b23a351 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 14:55:49 +0000 Subject: [PATCH 005/149] Add a simple callback native function --- src/hb-wasm-shape.cc | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 3ae2cbfec..9bbef473b 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -44,6 +44,12 @@ struct hb_wasm_face_data_t { wasm_module_t wasm_module; }; +extern "C" { +static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { + printf("%*s", len, the_string); +} +} + static bool init_wasm () { @@ -61,21 +67,12 @@ init_wasm () // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md static NativeSymbol native_symbols[] = { -#if 0 - { - "intToStr", // the name of WASM function name - intToStr, // the native function pointer - "(i*~i)i", // the function prototype signature, avoid to use i32 - NULL // attachment is NULL - }, - { - "get_pow", // the name of WASM function name - get_pow, // the native function pointer - "(ii)i", // the function prototype signature, avoid to use i32 - NULL // attachment is NULL - }, - { "calculate_native", calculate_native, "(iii)i", NULL } -#endif + { + "debugprint", + (void *)debugprint, + "($i)", + NULL + } }; init_args.mem_alloc_type = Alloc_With_Allocator; From 11c6d46086f5758a71df8e58059e087cd9c1766f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 08:16:49 -0700 Subject: [PATCH 006/149] [wasm] Pass font & buff to shape() function --- src/hb-wasm-shape.cc | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 9bbef473b..be289fa1b 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -200,18 +200,34 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, goto fail; } - if (!(shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr))) { + if (!(shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr))) + { DEBUG_MSG (WASM, module_inst, "Shape function not found."); + goto fail; + } + + uint32_t font_ref; + uint32_t buffer_ref; + // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work + if (!wasm_externref_obj2ref (module_inst, font, &font_ref) || + !wasm_externref_obj2ref (module_inst, buffer, &buffer_ref)) + { + DEBUG_MSG (WASM, module_inst, "Failed to register objects."); + goto fail; } wasm_val_t results[1]; - wasm_val_t arguments[1]; + wasm_val_t arguments[2]; results[0].kind = WASM_I32; arguments[0].kind = WASM_I32; - arguments[0].of.i32 = 41; + arguments[0].of.i32 = font_ref; + arguments[1].kind = WASM_I32; + arguments[1].of.i32 = buffer_ref; - if (!wasm_runtime_call_wasm_a (exec_env, shape_func, 1, results, 1, arguments)) + if (!wasm_runtime_call_wasm_a (exec_env, shape_func, + ARRAY_LENGTH (results), results, + ARRAY_LENGTH (arguments), arguments)) { DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", wasm_runtime_get_exception(module_inst)); From 829ba7428476da0616d6b92b88189224acc36cf0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 08:32:14 -0700 Subject: [PATCH 007/149] [wasm] Add hb-wasm-api.h --- src/hb-wasm-api.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/hb-wasm-api.h diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h new file mode 100644 index 000000000..8cfb24ae1 --- /dev/null +++ b/src/hb-wasm-api.h @@ -0,0 +1,70 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_H +#define HB_WASM_API_H + +/* +#include "hb.h" +*/ + +#include + + +#ifndef HB_BEGIN_DECLS +# ifdef __cplusplus +# define HB_BEGIN_DECLS extern "C" { +# define HB_END_DECLS } +# else /* !__cplusplus */ +# define HB_BEGIN_DECLS +# define HB_END_DECLS +# endif /* !__cplusplus */ +#endif + + +HB_BEGIN_DECLS + +#ifndef bool_t +#define bool_t uint32_t +#endif +typedef uint32_t face_t +typedef uint32_t font_t +typedef uint32_t buffer_t + + +/* font */ + +face_t +font_get_face (font_t); + + +/* shape interface */ + +bool_t +shape (font_t, buffer_t); + + +HB_END_DECLS + +#endif /* HB_WASM_API_H */ From e79a7318c4956fa9a8974812332480ccc5a13991 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 09:03:06 -0700 Subject: [PATCH 008/149] [wasm] Start adding wasm-api --- src/harfbuzz-subset.cc | 1 - src/harfbuzz.cc | 1 - src/hb-wasm-api.h | 29 ++++++++++++++++----------- src/hb-wasm-api.hh | 45 ++++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-font.hh | 44 +++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-shape.cc | 4 ++-- 6 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 src/hb-wasm-api.hh create mode 100644 src/hb-wasm-font.hh diff --git a/src/harfbuzz-subset.cc b/src/harfbuzz-subset.cc index 743a3539d..c0e23b3eb 100644 --- a/src/harfbuzz-subset.cc +++ b/src/harfbuzz-subset.cc @@ -60,4 +60,3 @@ #include "hb-subset.cc" #include "hb-ucd.cc" #include "hb-unicode.cc" -#include "hb-wasm-shape.cc" diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index 5b8196126..d7e8a93f3 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -58,4 +58,3 @@ #include "hb-ucd.cc" #include "hb-unicode.cc" #include "hb-uniscribe.cc" -#include "hb-wasm-shape.cc" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 8cfb24ae1..805e89322 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -32,39 +32,44 @@ #include -#ifndef HB_BEGIN_DECLS +#ifndef HB_WASM_BEGIN_DECLS # ifdef __cplusplus -# define HB_BEGIN_DECLS extern "C" { -# define HB_END_DECLS } +# define HB_WASM_BEGIN_DECLS extern "C" { +# define HB_WASM_END_DECLS } # else /* !__cplusplus */ -# define HB_BEGIN_DECLS -# define HB_END_DECLS +# define HB_WASM_BEGIN_DECLS +# define HB_WASM_END_DECLS # endif /* !__cplusplus */ #endif -HB_BEGIN_DECLS +HB_WASM_BEGIN_DECLS + +#ifndef HB_WASM_API +#define HB_WASM_API(x) x +#endif + #ifndef bool_t #define bool_t uint32_t #endif -typedef uint32_t face_t -typedef uint32_t font_t -typedef uint32_t buffer_t +typedef uint32_t face_t; +typedef uint32_t font_t; +typedef uint32_t buffer_t; /* font */ face_t -font_get_face (font_t); +HB_WASM_API (font_get_face) (font_t); /* shape interface */ bool_t -shape (font_t, buffer_t); +HB_WASM_API (shape) (font_t, buffer_t); -HB_END_DECLS +HB_WASM_END_DECLS #endif /* HB_WASM_API_H */ diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh new file mode 100644 index 000000000..0f63a1051 --- /dev/null +++ b/src/hb-wasm-api.hh @@ -0,0 +1,45 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_HH +#define HB_WASM_API_HH + +#include "hb.h" + + +#define HB_WASM_API(x) HB_INTERNAL x +#define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm { +#define HB_WASM_END_DECLS }} + +#include "hb-wasm-api.h" + +#undef HB_WASM_API +#undef HB_WASM_BEGIN_DECLS +#undef HB_WASM_END_DECLS + + +#include "hb-wasm-font.hh" + + +#endif /* HB_WASM_API_HH */ diff --git a/src/hb-wasm-font.hh b/src/hb-wasm-font.hh new file mode 100644 index 000000000..0223e676b --- /dev/null +++ b/src/hb-wasm-font.hh @@ -0,0 +1,44 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_FONT_HH +#define HB_WASM_FONT_HH + +#include + +namespace hb { +namespace wasm { + + +face_t +font_get_face (font_t) +{ + return 0; +} + + + +}} + +#endif /* HB_WASM_FONT_HH */ diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index be289fa1b..ccc337b1c 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -30,6 +30,8 @@ #ifdef HAVE_WASM +#include "hb-wasm-api.hh" + #include @@ -44,11 +46,9 @@ struct hb_wasm_face_data_t { wasm_module_t wasm_module; }; -extern "C" { static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { printf("%*s", len, the_string); } -} static bool init_wasm () From 9f4dc2e103290cd7447ab24b2561c52d0f66344b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 09:18:25 -0700 Subject: [PATCH 009/149] [wasm] Bind native API --- src/hb-wasm-api.h | 10 ++++++++-- src/hb-wasm-api.hh | 27 +++++++++++++++++++++++++++ src/hb-wasm-font.hh | 3 ++- src/hb-wasm-shape.cc | 25 ++----------------------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 805e89322..371f53d84 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -48,6 +48,9 @@ HB_WASM_BEGIN_DECLS #ifndef HB_WASM_API #define HB_WASM_API(x) x #endif +#ifndef HB_WASM_EXEC_ENV +#define HB_WASM_EXEC_ENV +#endif #ifndef bool_t @@ -61,13 +64,16 @@ typedef uint32_t buffer_t; /* font */ face_t -HB_WASM_API (font_get_face) (font_t); +HB_WASM_API (font_get_face) (HB_WASM_EXEC_ENV + font_t); /* shape interface */ bool_t -HB_WASM_API (shape) (font_t, buffer_t); +HB_WASM_API (shape) (HB_WASM_EXEC_ENV + font_t, + buffer_t); HB_WASM_END_DECLS diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 0f63a1051..e0360d100 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -27,11 +27,14 @@ #include "hb.h" +#include #define HB_WASM_API(x) HB_INTERNAL x #define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm { #define HB_WASM_END_DECLS }} +#define HB_WASM_EXEC_ENV wasm_exec_env_t exec_env, + #include "hb-wasm-api.h" #undef HB_WASM_API @@ -41,5 +44,29 @@ #include "hb-wasm-font.hh" +static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { + printf("%*s", len, the_string); +} + + + /* Define an array of NativeSymbol for the APIs to be exported. + * Note: the array must be static defined since runtime will keep it after registration + * For the function signature specifications, goto the link: + * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md + */ + +#define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} + static NativeSymbol _hb_wasm_native_symbols[] = + { + NATIVE_SYMBOL ("(r)r", font_get_face), + { + "debugprint", + (void *) debugprint, + "($i)", + NULL + } + }; +#undef NATIVE_SYMBOL + #endif /* HB_WASM_API_HH */ diff --git a/src/hb-wasm-font.hh b/src/hb-wasm-font.hh index 0223e676b..7fde34791 100644 --- a/src/hb-wasm-font.hh +++ b/src/hb-wasm-font.hh @@ -32,7 +32,8 @@ namespace wasm { face_t -font_get_face (font_t) +font_get_face (HB_WASM_EXEC_ENV + font_t) { return 0; } diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index ccc337b1c..3aa77b5ba 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -32,8 +32,6 @@ #include "hb-wasm-api.hh" -#include - /* * shaper face data @@ -46,10 +44,6 @@ struct hb_wasm_face_data_t { wasm_module_t wasm_module; }; -static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { - printf("%*s", len, the_string); -} - static bool init_wasm () { @@ -60,21 +54,6 @@ init_wasm () RuntimeInitArgs init_args; memset (&init_args, 0, sizeof (RuntimeInitArgs)); - // Define an array of NativeSymbol for the APIs to be exported. - // Note: the array must be static defined since runtime - // will keep it after registration - // For the function signature specifications, goto the link: - // https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md - - static NativeSymbol native_symbols[] = { - { - "debugprint", - (void *)debugprint, - "($i)", - NULL - } - }; - init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; @@ -83,9 +62,9 @@ init_wasm () init_args.mem_alloc_type = Alloc_With_System_Allocator; // Native symbols need below registration phase - init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol); + init_args.n_native_symbols = ARRAY_LENGTH (_hb_wasm_native_symbols); init_args.native_module_name = "env"; - init_args.native_symbols = native_symbols; + init_args.native_symbols = _hb_wasm_native_symbols; if (!wasm_runtime_full_init (&init_args)) { From fd1f7f46f46cd9dba29ad4975f09f6d4073edae4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 09:25:05 -0700 Subject: [PATCH 010/149] [wasm-api] Implement font_get_face --- src/hb-wasm-api.hh | 7 ++++++- src/hb-wasm-font.hh | 12 ++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index e0360d100..2bc93cb0e 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -25,7 +25,7 @@ #ifndef HB_WASM_API_HH #define HB_WASM_API_HH -#include "hb.h" +#include "hb.hh" #include @@ -41,9 +41,14 @@ #undef HB_WASM_BEGIN_DECLS #undef HB_WASM_END_DECLS +#define module_inst wasm_runtime_get_module_inst (exec_env) + #include "hb-wasm-font.hh" + +#undef module_inst + static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { printf("%*s", len, the_string); } diff --git a/src/hb-wasm-font.hh b/src/hb-wasm-font.hh index 7fde34791..9a2272ebc 100644 --- a/src/hb-wasm-font.hh +++ b/src/hb-wasm-font.hh @@ -33,9 +33,17 @@ namespace wasm { face_t font_get_face (HB_WASM_EXEC_ENV - font_t) + font_t fontref) { - return 0; + hb_font_t *font; + if (unlikely (!wasm_externref_ref2obj (fontref, (void **) &font))) + return 0; + + hb_face_t *face = hb_font_get_face (font); + + uint32_t faceref = 0; + (void) wasm_externref_obj2ref (module_inst, face, &faceref); + return faceref; } From f0200445d0606019cec868dfc92b1cde5015e6d5 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 16:38:39 +0000 Subject: [PATCH 011/149] Initial WASM docs and Rust example --- docs/wasm-shaper.md | 116 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 docs/wasm-shaper.md diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md new file mode 100644 index 000000000..6ffbe3615 --- /dev/null +++ b/docs/wasm-shaper.md @@ -0,0 +1,116 @@ +# The web assembly shaper + +If the standard OpenType shaping engine doesn't give you enough flexibility, Harfbuzz allows you to write your own shaping engine in WebAssembly and embed it into your font! Any font which contains a `Wasm` table will be passed to the WebAssembly shaper. + +## How to write a shaping engine in Rust + +Here are the steps to create an example shaping engine in Rust: + +* First, install wasm-pack, which helps us to generate optimized WASM files. It writes some Javascript bridge code that we don't need, but it makes the build and deployment process much easier: + +``` +cargo install wasm-pack +``` + +* Now let's create a new library: + +``` +cargo new --lib my-shaper +``` + +* We need the target to be a dynamic library, and we're going to use `bindgen` to export our Rust function to WASM, so let's put these lines in the `Cargo.toml`: + +```toml +[lib] +crate-type = ["cdylib"] +[dependencies] +wasm-bindgen = "0.2" +``` + +* And now we'll create our shaper code. In `src/lib.rs`: + +``` +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn shape(_font_ref: i32, _buf_ref: i32) -> i32 { + 1 +} +``` + +This exports a shaping function which takes two arguments, tokens representing the font and the buffer, and returns a status value. We can pass these tokens back to Harfbuzz in order to use its native functions on the font and buffer objects. More on native functions later - let's get this shaper compiled and added into a font: + +* To compile the shaper, run `wasm-pack build --target nodejs`: + +``` +INFO]: 🎯 Checking for the Wasm target... +[INFO]: 🌀 Compiling to Wasm... + Compiling my-shaper v0.1.0 (...) + Finished release [optimized] target(s) in 0.20s +[WARN]: ⚠️ origin crate has no README +[INFO]: ⬇️ Installing wasm-bindgen... +[INFO]: Optimizing wasm binaries with `wasm-opt`... +[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended +[INFO]: ✨ Done in 0.40s +``` + +You'll find the output WASM file in `pkg/my_shaper_bg.wasm` + +* Now we need to get it into a font. + +One way to do this is to use [otfsurgeon](https://github.com/simoncozens/font-engineering/blob/master/otfsurgeon), a tool for manipulating binary font tables: + +``` +% otfsurgeon -i test.ttf add -o test-wasm.ttf Wasm < pkg/my_shaper_bg.wasm +``` + +And now we can run it! + +``` +% hb-shape test-wasm.ttf abc --shapers=wasm +1 +[cent=0|sterling=1|fraction=2] +``` + +(The `--shapers=wasm` isn't necessary, as any font with a `Wasm` table will be sent to the WASM shaper if it's enabled, but it proves the point.) + +Congratulations! Our shaper did nothing, but in Rust! Now let's do something - it's time for the Hello World of WASM shaping. + +* To say hello world, we're going to have to use a native function. + +In debugging builds of Harfbuzz, we can print some output from the web assembly module to the host's standard output using the `debugprint` function, which takes an array of characters and a length: + +``` +// We don't use #[wasm_bindgen] here because that makes +// assumptions about Javascript calling conventions. We +// really do just want to import some C symbols and run +// them in unsafe-land! +extern "C" { + pub fn debugprint(s: *const u8, len: i8); +} +``` + +And now let's add a function on the Rust side which makes this a bit more ergonomic to use: + +``` +fn print(s: &str) { + unsafe { + debugprint(s.as_ptr(), s.len().try_into().unwrap()); + }; +} + +#[wasm_bindgen] +pub fn shape(font_ref: i32, buf_ref: i32) -> i32 { + print("Hello from Rust!\n"); + 1 +} +``` + +With this compiled into a WASM module, and installed into our font again, finally our fonts can talk to us!s + +``` +$ hb-shape test-wasm.ttf abc +Hello from Rust! +1 +[cent=0|sterling=1|fraction=2] +``` From 89c50b0ccb60fc0d73f2b9d65ccbdaebe6fef5b1 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 16:41:04 +0000 Subject: [PATCH 012/149] Fix debugprint format string --- src/hb-wasm-api.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 2bc93cb0e..2430ae7de 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -50,7 +50,7 @@ #undef module_inst static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { - printf("%*s", len, the_string); + printf("%.*s", len, the_string); } From ed1a70c7e8ddf78ea3fd3e6208e683870ff7e893 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 16:41:17 +0000 Subject: [PATCH 013/149] Only export debugprint when HB_DEBUG_WASM --- src/hb-wasm-api.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 2430ae7de..122bc8270 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -64,12 +64,14 @@ static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) static NativeSymbol _hb_wasm_native_symbols[] = { NATIVE_SYMBOL ("(r)r", font_get_face), +#ifdef HB_DEBUG_WASM { "debugprint", (void *) debugprint, "($i)", NULL } +#endif }; #undef NATIVE_SYMBOL From bd28d01a6a1e562dc611e386544075f6caeacf21 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 09:49:46 -0700 Subject: [PATCH 014/149] [wasm-api] Add HB_WASM_INTERFACE --- src/hb-wasm-api.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 371f53d84..fa703be88 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -48,6 +48,9 @@ HB_WASM_BEGIN_DECLS #ifndef HB_WASM_API #define HB_WASM_API(x) x #endif +#ifndef HB_WASM_INTERFACE +#define HB_WASM_INTERFACE(x) x +#endif #ifndef HB_WASM_EXEC_ENV #define HB_WASM_EXEC_ENV #endif @@ -71,9 +74,9 @@ HB_WASM_API (font_get_face) (HB_WASM_EXEC_ENV /* shape interface */ bool_t -HB_WASM_API (shape) (HB_WASM_EXEC_ENV - font_t, - buffer_t); +HB_WASM_INTERFACE (shape) (HB_WASM_EXEC_ENV + font_t, + buffer_t); HB_WASM_END_DECLS From 63904538c8215312489afd0277d78199c88a512a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:11:22 -0700 Subject: [PATCH 015/149] [wasm-api] Use i32 instead of externref in API spec Oh well... --- src/hb-wasm-api.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 122bc8270..43f5d217a 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -63,7 +63,7 @@ static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) #define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} static NativeSymbol _hb_wasm_native_symbols[] = { - NATIVE_SYMBOL ("(r)r", font_get_face), + NATIVE_SYMBOL ("(i)i", font_get_face), #ifdef HB_DEBUG_WASM { "debugprint", From 4c8a414a101c9657fda42d2fdb9789407b034c99 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:22:12 -0700 Subject: [PATCH 016/149] [wasm-api] Clean up debugprint --- src/hb-wasm-api.hh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 43f5d217a..d7bfd322d 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -46,12 +46,20 @@ #include "hb-wasm-font.hh" +#ifdef HB_DEBUG_WASM +namespace hb { namespace wasm { +static void +debugprint (HB_WASM_EXEC_ENV + char *the_string, uint32_t len) +{ + printf("%.*s", (int) len, the_string); +} +}} +#endif #undef module_inst +#undef HB_WASM_EXEC_ENV -static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { - printf("%.*s", len, the_string); -} /* Define an array of NativeSymbol for the APIs to be exported. @@ -65,12 +73,7 @@ static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) { NATIVE_SYMBOL ("(i)i", font_get_face), #ifdef HB_DEBUG_WASM - { - "debugprint", - (void *) debugprint, - "($i)", - NULL - } + NATIVE_SYMBOL ("($i)", debugprint), #endif }; #undef NATIVE_SYMBOL From 62f3c7cf67c8b468b49380346a0dbd22c0ad174e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:29:04 -0700 Subject: [PATCH 017/149] [wasm-api] Add macros for ref handling --- src/hb-wasm-api.hh | 13 ++++++++++++- src/hb-wasm-font.hh | 9 ++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index d7bfd322d..376ba1a7b 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -41,7 +41,15 @@ #undef HB_WASM_BEGIN_DECLS #undef HB_WASM_END_DECLS + +#define nullref 0 #define module_inst wasm_runtime_get_module_inst (exec_env) +#define HB_REF2OBJ(obj) \ + hb_##obj##_t *obj = nullptr; \ + (void) wasm_externref_ref2obj (obj##ref, (void **) &obj) +#define HB_OBJ2REF(obj) \ + uint32_t obj##ref = nullref; \ + (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) #include "hb-wasm-font.hh" @@ -57,9 +65,12 @@ debugprint (HB_WASM_EXEC_ENV }} #endif + +#undef nullref #undef module_inst #undef HB_WASM_EXEC_ENV - +#undef HB_REF2OBJ +#undef HB_OBJ2REF /* Define an array of NativeSymbol for the APIs to be exported. diff --git a/src/hb-wasm-font.hh b/src/hb-wasm-font.hh index 9a2272ebc..788261bf6 100644 --- a/src/hb-wasm-font.hh +++ b/src/hb-wasm-font.hh @@ -35,14 +35,13 @@ face_t font_get_face (HB_WASM_EXEC_ENV font_t fontref) { - hb_font_t *font; - if (unlikely (!wasm_externref_ref2obj (fontref, (void **) &font))) - return 0; + HB_REF2OBJ (font); + if (unlikely (!font)) + return nullref; hb_face_t *face = hb_font_get_face (font); - uint32_t faceref = 0; - (void) wasm_externref_obj2ref (module_inst, face, &faceref); + HB_OBJ2REF (face); return faceref; } From 3bc0ecf28c97a5891a9f2cfb371c41505640beef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:30:35 -0700 Subject: [PATCH 018/149] [wasm-api] Add ref_t --- src/hb-wasm-api.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index fa703be88..59228f977 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -59,9 +59,12 @@ HB_WASM_BEGIN_DECLS #ifndef bool_t #define bool_t uint32_t #endif -typedef uint32_t face_t; -typedef uint32_t font_t; -typedef uint32_t buffer_t; +#ifndef ref_t +#define ref_t uint32_t +#endif +typedef ref_t face_t; +typedef ref_t font_t; +typedef ref_t buffer_t; /* font */ From 36dd27bf3f4c35fde6041a91aeb275d4c8624624 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 17:33:50 +0000 Subject: [PATCH 019/149] Just printf, don't take length --- src/hb-wasm-api.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 376ba1a7b..dbca32464 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -58,9 +58,9 @@ namespace hb { namespace wasm { static void debugprint (HB_WASM_EXEC_ENV - char *the_string, uint32_t len) + char *the_string) { - printf("%.*s", (int) len, the_string); + printf("%s", the_string); } }} #endif @@ -84,7 +84,7 @@ debugprint (HB_WASM_EXEC_ENV { NATIVE_SYMBOL ("(i)i", font_get_face), #ifdef HB_DEBUG_WASM - NATIVE_SYMBOL ("($i)", debugprint), + NATIVE_SYMBOL ("($)", debugprint), #endif }; #undef NATIVE_SYMBOL From 11fc83c0ba349a6f376a8409971b5698bba39b19 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 17:34:02 +0000 Subject: [PATCH 020/149] [Docs] Pass a C string from Rust to HB --- docs/wasm-shaper.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index 6ffbe3615..1d101c1b1 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -33,7 +33,7 @@ wasm-bindgen = "0.2" use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(_font_ref: i32, _buf_ref: i32) -> i32 { +pub fn shape(_font_ref: u32, _buf_ref: u32) -> i32 { 1 } ``` @@ -78,7 +78,7 @@ Congratulations! Our shaper did nothing, but in Rust! Now let's do something - i * To say hello world, we're going to have to use a native function. -In debugging builds of Harfbuzz, we can print some output from the web assembly module to the host's standard output using the `debugprint` function, which takes an array of characters and a length: +In debugging builds of Harfbuzz, we can print some output from the web assembly module to the host's standard output using the `debugprint` function: ``` // We don't use #[wasm_bindgen] here because that makes @@ -86,16 +86,19 @@ In debugging builds of Harfbuzz, we can print some output from the web assembly // really do just want to import some C symbols and run // them in unsafe-land! extern "C" { - pub fn debugprint(s: *const u8, len: i8); + pub fn debugprint(s: *const u8); } ``` And now let's add a function on the Rust side which makes this a bit more ergonomic to use: ``` +use std::ffi::CString; + fn print(s: &str) { + let c_s = CString::new(s).unwrap(); unsafe { - debugprint(s.as_ptr(), s.len().try_into().unwrap()); + debugprint(c_s.as_ptr() as *const u8); }; } From bb8a04cbadc16c8195b87e8fd307a2403d2212fa Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:37:44 -0700 Subject: [PATCH 021/149] [wasm] Remove the print(returnval) and use it to fail shaper --- docs/wasm-shaper.md | 2 -- src/hb-wasm-shape.cc | 8 +------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index 1d101c1b1..0bf1a0b90 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -68,7 +68,6 @@ And now we can run it! ``` % hb-shape test-wasm.ttf abc --shapers=wasm -1 [cent=0|sterling=1|fraction=2] ``` @@ -114,6 +113,5 @@ With this compiled into a WASM module, and installed into our font again, finall ``` $ hb-shape test-wasm.ttf abc Hello from Rust! -1 [cent=0|sterling=1|fraction=2] ``` diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 3aa77b5ba..1a57915ab 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -213,13 +213,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, goto fail; } - printf ("%i\n", results[0].of.i32); - - - if (!face_data) - goto fail; - - if (0) + if (!results[0].of.i32) { fail: ret = false; From 65efad6b59f6252b2b314e5e16b958bf1b8f4e80 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:49:02 -0700 Subject: [PATCH 022/149] [wasm] Make debugprint use debug API --- src/hb-wasm-api.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index dbca32464..63b9d37c6 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -60,7 +60,7 @@ static void debugprint (HB_WASM_EXEC_ENV char *the_string) { - printf("%s", the_string); + DEBUG_MSG (WASM, exec_env, "%s", the_string); } }} #endif From 980706441b58878cb168543eef70247eccc6abf6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 10:58:22 -0700 Subject: [PATCH 023/149] [wasm] Add hb-wasm-api-list.hh --- src/hb-wasm-api-list.hh | 61 +++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api.hh | 30 +------------------- 2 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 src/hb-wasm-api-list.hh diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh new file mode 100644 index 000000000..a20782b7e --- /dev/null +++ b/src/hb-wasm-api-list.hh @@ -0,0 +1,61 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_LIST_HH +#define HB_WASM_API_LIST_HH + +#include "hb-wasm-api.hh" + +#include "hb-wasm-font.hh" + +#ifdef HB_DEBUG_WASM +namespace hb { namespace wasm { +static void +debugprint (HB_WASM_EXEC_ENV + char *the_string) +{ + DEBUG_MSG (WASM, exec_env, "%s", the_string); +} +}} +#endif + +#define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} +/* Note: the array must be static defined since runtime will keep it after registration + * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md */ +static NativeSymbol _hb_wasm_native_symbols[] = +{ + + /* font */ + NATIVE_SYMBOL ("(i)i", font_get_face), + + /* debug */ +#ifdef HB_DEBUG_WASM + NATIVE_SYMBOL ("($)", debugprint), +#endif + +}; +#undef NATIVE_SYMBOL + + +#endif /* HB_WASM_API_LIST_HH */ diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 63b9d37c6..27e8e7890 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -52,18 +52,7 @@ (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) -#include "hb-wasm-font.hh" - -#ifdef HB_DEBUG_WASM -namespace hb { namespace wasm { -static void -debugprint (HB_WASM_EXEC_ENV - char *the_string) -{ - DEBUG_MSG (WASM, exec_env, "%s", the_string); -} -}} -#endif +#include "hb-wasm-api-list.hh" #undef nullref @@ -73,21 +62,4 @@ debugprint (HB_WASM_EXEC_ENV #undef HB_OBJ2REF - /* Define an array of NativeSymbol for the APIs to be exported. - * Note: the array must be static defined since runtime will keep it after registration - * For the function signature specifications, goto the link: - * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md - */ - -#define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} - static NativeSymbol _hb_wasm_native_symbols[] = - { - NATIVE_SYMBOL ("(i)i", font_get_face), -#ifdef HB_DEBUG_WASM - NATIVE_SYMBOL ("($)", debugprint), -#endif - }; -#undef NATIVE_SYMBOL - - #endif /* HB_WASM_API_HH */ From 42d121ae79ce547f1bbe82b9fa771725604546d3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 11:05:19 -0700 Subject: [PATCH 024/149] [wasm] Add hb-wasm-api.cc --- src/hb-wasm-api-list.hh | 1 - src/hb-wasm-api.cc | 45 +++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api.hh | 20 ------------------ src/hb-wasm-shape.cc | 1 + src/meson.build | 2 +- 5 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 src/hb-wasm-api.cc diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index a20782b7e..11ec2b618 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -27,7 +27,6 @@ #include "hb-wasm-api.hh" -#include "hb-wasm-font.hh" #ifdef HB_DEBUG_WASM namespace hb { namespace wasm { diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc new file mode 100644 index 000000000..64537b50c --- /dev/null +++ b/src/hb-wasm-api.cc @@ -0,0 +1,45 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "hb-wasm-api.hh" + + +#define nullref 0 +#define module_inst wasm_runtime_get_module_inst (exec_env) +#define HB_REF2OBJ(obj) \ + hb_##obj##_t *obj = nullptr; \ + (void) wasm_externref_ref2obj (obj##ref, (void **) &obj) +#define HB_OBJ2REF(obj) \ + uint32_t obj##ref = nullref; \ + (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) + + +#include "hb-wasm-font.hh" + + +#undef nullref +#undef module_inst +#undef HB_WASM_EXEC_ENV +#undef HB_REF2OBJ +#undef HB_OBJ2REF diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 27e8e7890..c696a9471 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -42,24 +42,4 @@ #undef HB_WASM_END_DECLS -#define nullref 0 -#define module_inst wasm_runtime_get_module_inst (exec_env) -#define HB_REF2OBJ(obj) \ - hb_##obj##_t *obj = nullptr; \ - (void) wasm_externref_ref2obj (obj##ref, (void **) &obj) -#define HB_OBJ2REF(obj) \ - uint32_t obj##ref = nullref; \ - (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) - - -#include "hb-wasm-api-list.hh" - - -#undef nullref -#undef module_inst -#undef HB_WASM_EXEC_ENV -#undef HB_REF2OBJ -#undef HB_OBJ2REF - - #endif /* HB_WASM_API_HH */ diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 1a57915ab..ecb42dbe1 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -31,6 +31,7 @@ #ifdef HAVE_WASM #include "hb-wasm-api.hh" +#include "hb-wasm-api-list.hh" /* diff --git a/src/meson.build b/src/meson.build index f5a1ea975..1842bf44a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -324,7 +324,7 @@ hb_glib_headers = files('hb-glib.h') hb_graphite2_sources = files('hb-graphite2.cc') hb_graphite2_headers = files('hb-graphite2.h') -hb_wasm_sources = files('hb-wasm-shape.cc') +hb_wasm_sources = files('hb-wasm-shape.cc', 'hb-wasm-api.cc') hb_wasm_headers = files() # System-dependent sources and headers From 6b72a18c7b981ac0126ea3b585f16449ce3dd25b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 11:06:48 -0700 Subject: [PATCH 025/149] [wasm-api] Rename file --- src/{hb-wasm-font.hh => hb-wasm-api-font.hh} | 0 src/hb-wasm-api.cc | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{hb-wasm-font.hh => hb-wasm-api-font.hh} (100%) diff --git a/src/hb-wasm-font.hh b/src/hb-wasm-api-font.hh similarity index 100% rename from src/hb-wasm-font.hh rename to src/hb-wasm-api-font.hh diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index 64537b50c..37264900f 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -35,7 +35,7 @@ (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) -#include "hb-wasm-font.hh" +#include "hb-wasm-api-font.hh" #undef nullref From 747dcf561d710d324b02249806fa0b113178c3d2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 11:53:08 -0700 Subject: [PATCH 026/149] [wasm] Strong typing for object references Such that wasm cannot crash us by passing wrong object refs. https://github.com/bytecodealliance/wasm-micro-runtime/discussions/1987 It still is unsafe if some code in the process other than HarfBuzz registers refs with wasm-micro-runtime, since wasm_externref_ref2obj() takes no context variable and looks up refs globally :(. Maybe I fix that later by keeping a hash table of ref->obj-type instead. --- src/hb-wasm-api.cc | 11 ++--------- src/hb-wasm-api.hh | 32 ++++++++++++++++++++++++++++++++ src/hb-wasm-shape.cc | 41 +++++++++++++++++++++-------------------- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index 37264900f..74b18435d 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -24,22 +24,15 @@ #include "hb-wasm-api.hh" - -#define nullref 0 #define module_inst wasm_runtime_get_module_inst (exec_env) -#define HB_REF2OBJ(obj) \ - hb_##obj##_t *obj = nullptr; \ - (void) wasm_externref_ref2obj (obj##ref, (void **) &obj) -#define HB_OBJ2REF(obj) \ - uint32_t obj##ref = nullref; \ - (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref) #include "hb-wasm-api-font.hh" -#undef nullref #undef module_inst #undef HB_WASM_EXEC_ENV #undef HB_REF2OBJ #undef HB_OBJ2REF + +hb_user_data_key_t _hb_wasm_ref_type_key = {}; diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index c696a9471..a53f3f5b0 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -42,4 +42,36 @@ #undef HB_WASM_END_DECLS +enum { + hb_wasm_ref_type_none, + hb_wasm_ref_type_face, + hb_wasm_ref_type_font, + hb_wasm_ref_type_buffer, +}; + +HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; + +#define nullref 0 + +#define HB_REF2OBJ(obj) \ + hb_##obj##_t *obj = nullptr; \ + HB_STMT_START { \ + (void) wasm_externref_ref2obj (obj##ref, (void **) &obj); \ + /* Check object type. */ \ + /* This works because all our objects have the same hb_object_t layout. */ \ + if (unlikely (hb_##obj##_get_user_data (obj, &_hb_wasm_ref_type_key) != \ + (void *) hb_wasm_ref_type_##obj)) \ + obj = nullptr; \ + } HB_STMT_END + +#define HB_OBJ2REF(obj) \ + uint32_t obj##ref = nullref; \ + HB_STMT_START { \ + hb_##obj##_set_user_data (obj, &_hb_wasm_ref_type_key, \ + (void *) hb_wasm_ref_type_##obj, \ + nullptr, false); \ + (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref); \ + } HB_STMT_END + + #endif /* HB_WASM_API_HH */ diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index ecb42dbe1..ea31cce70 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -67,7 +67,7 @@ init_wasm () init_args.native_module_name = "env"; init_args.native_symbols = _hb_wasm_native_symbols; - if (!wasm_runtime_full_init (&init_args)) + if (unlikely (!wasm_runtime_full_init (&init_args))) { DEBUG_MSG (WASM, nullptr, "Init runtime environment failed."); return false; @@ -95,7 +95,7 @@ _hb_wasm_shaper_face_data_create (hb_face_t *face) wasm_module = wasm_runtime_load ((uint8_t *) hb_blob_get_data_writable (wasm_blob, nullptr), length, nullptr, 0); - if (!wasm_module) + if (unlikely (!wasm_module)) { DEBUG_MSG (WASM, nullptr, "Load wasm module failed."); goto fail; @@ -168,46 +168,47 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, module_inst = wasm_runtime_instantiate(face_data->wasm_module, stack_size, heap_size, nullptr, 0); - if (!module_inst) + if (unlikely (!module_inst)) { DEBUG_MSG (WASM, face_data->wasm_module, "Instantiate wasm module failed."); + return false; + } + + // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work + HB_OBJ2REF (font); + HB_OBJ2REF (buffer); + if (unlikely (!fontref || !bufferref)) + { + DEBUG_MSG (WASM, module_inst, "Failed to register objects."); goto fail; } exec_env = wasm_runtime_create_exec_env (module_inst, stack_size); - if (!exec_env) { + if (unlikely (!exec_env)) { DEBUG_MSG (WASM, module_inst, "Create wasm execution environment failed."); goto fail; } - if (!(shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr))) + shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr); + if (unlikely (!shape_func)) { DEBUG_MSG (WASM, module_inst, "Shape function not found."); goto fail; } - uint32_t font_ref; - uint32_t buffer_ref; - // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work - if (!wasm_externref_obj2ref (module_inst, font, &font_ref) || - !wasm_externref_obj2ref (module_inst, buffer, &buffer_ref)) - { - DEBUG_MSG (WASM, module_inst, "Failed to register objects."); - goto fail; - } - wasm_val_t results[1]; wasm_val_t arguments[2]; results[0].kind = WASM_I32; arguments[0].kind = WASM_I32; - arguments[0].of.i32 = font_ref; + arguments[0].of.i32 = fontref; arguments[1].kind = WASM_I32; - arguments[1].of.i32 = buffer_ref; + arguments[1].of.i32 = bufferref; - if (!wasm_runtime_call_wasm_a (exec_env, shape_func, - ARRAY_LENGTH (results), results, - ARRAY_LENGTH (arguments), arguments)) + ret = wasm_runtime_call_wasm_a (exec_env, shape_func, + ARRAY_LENGTH (results), results, + ARRAY_LENGTH (arguments), arguments); + if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", wasm_runtime_get_exception(module_inst)); From e03726d26948b6f9cf2c64df75230c3e15433f44 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 12:00:42 -0700 Subject: [PATCH 027/149] [wasm] Ignore API in the docs --- docs/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/meson.build b/docs/meson.build index faf558a77..45ca93ad3 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -41,6 +41,7 @@ ignore_headers = [ 'hb-gobject-enums.h', 'hb-gobject-enums-tmp.h', 'hb-gobject-structs.h', + 'hb-wasm-api.h', ] gnome.gtkdoc('harfbuzz', From e87b1b3ec3a67bf42edac41236e1e70a56ff7072 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 12:23:52 -0700 Subject: [PATCH 028/149] [wasm-api] Try to add face_reference_table --- src/hb-wasm-api-face.hh | 51 +++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.cc | 4 +--- src/hb-wasm-api.h | 41 +++++++++++++++++++++++++-------- src/hb-wasm-api.hh | 19 +++++++++++++-- 5 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 src/hb-wasm-api-face.hh diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh new file mode 100644 index 000000000..08e6671c8 --- /dev/null +++ b/src/hb-wasm-api-face.hh @@ -0,0 +1,51 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_FACE_HH +#define HB_WASM_FACE_HH + +#include + +namespace hb { +namespace wasm { + + +void +face_reference_table (HB_WASM_EXEC_ENV_COMPOUND + face_t faceref, + tag_t table_tag) +{ + HB_RETURN_TYPE (blob_t, blob); + + HB_REF2OBJ (face); + if (unlikely (!face)) + return; + + blob.length = 1; +} + + +}} + +#endif /* HB_WASM_FACE_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 11ec2b618..35beb807b 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -45,6 +45,9 @@ debugprint (HB_WASM_EXEC_ENV static NativeSymbol _hb_wasm_native_symbols[] = { + /* face */ + NATIVE_SYMBOL ("(iii)", face_reference_table), + /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index 74b18435d..de55bd1a7 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -27,12 +27,10 @@ #define module_inst wasm_runtime_get_module_inst (exec_env) +#include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" #undef module_inst -#undef HB_WASM_EXEC_ENV -#undef HB_REF2OBJ -#undef HB_OBJ2REF hb_user_data_key_t _hb_wasm_ref_type_key = {}; diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 59228f977..7f25b996a 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -46,14 +46,20 @@ HB_WASM_BEGIN_DECLS #ifndef HB_WASM_API -#define HB_WASM_API(x) x +#define HB_WASM_API(ret_t, name) ret_t name +#endif +#ifndef HB_WASM_API_COMPOUND /* compound return type */ +#define HB_WASM_API_COMPOUND(ret_t, name) HB_WASM_API(ret_t, name) #endif #ifndef HB_WASM_INTERFACE -#define HB_WASM_INTERFACE(x) x +#define HB_WASM_INTERFACE(ret_t, name) ret_t name #endif #ifndef HB_WASM_EXEC_ENV #define HB_WASM_EXEC_ENV #endif +#ifndef HB_WASM_EXEC_ENV_COMPOUND +#define HB_WASM_EXEC_ENV_COMPOUND HB_WASM_EXEC_ENV +#endif #ifndef bool_t @@ -62,24 +68,41 @@ HB_WASM_BEGIN_DECLS #ifndef ref_t #define ref_t uint32_t #endif +#ifndef ptr_t +#define ptr_t uint32_t +#endif + +typedef uint32_t tag_t; +#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))) + typedef ref_t face_t; typedef ref_t font_t; typedef ref_t buffer_t; +typedef struct { + ref_t data; + uint32_t length; +} blob_t; + + +/* face */ + +HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND + face_t faceref, + tag_t table_tag); + /* font */ -face_t -HB_WASM_API (font_get_face) (HB_WASM_EXEC_ENV - font_t); +HB_WASM_API (face_t, font_get_face) (HB_WASM_EXEC_ENV + font_t fontref); /* shape interface */ -bool_t -HB_WASM_INTERFACE (shape) (HB_WASM_EXEC_ENV - font_t, - buffer_t); +HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV + font_t fontref, + buffer_t bufferref); HB_WASM_END_DECLS diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index a53f3f5b0..8f9c7d8ce 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -29,11 +29,14 @@ #include -#define HB_WASM_API(x) HB_INTERNAL x #define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm { #define HB_WASM_END_DECLS }} +#define HB_WASM_API(ret_t, name) HB_INTERNAL ret_t name +#define HB_WASM_API_COMPOUND(ret_t, name) HB_INTERNAL void name + #define HB_WASM_EXEC_ENV wasm_exec_env_t exec_env, +#define HB_WASM_EXEC_ENV_COMPOUND wasm_exec_env_t exec_env, ptr_t retptr, #include "hb-wasm-api.h" @@ -63,7 +66,6 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; (void *) hb_wasm_ref_type_##obj)) \ obj = nullptr; \ } HB_STMT_END - #define HB_OBJ2REF(obj) \ uint32_t obj##ref = nullref; \ HB_STMT_START { \ @@ -73,5 +75,18 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref); \ } HB_STMT_END +#define HB_RETURN_TYPE(type, name) \ + type *_name_ptr = nullptr; \ + { \ + if (likely (wasm_runtime_validate_app_addr (module_inst, \ + retptr, sizeof (type)))) \ + { \ + _name_ptr = (type *) wasm_runtime_addr_app_to_native (module_inst, retptr); \ + if (unlikely (!_name_ptr)) \ + return; \ + } \ + } \ + type &name = *_name_ptr + #endif /* HB_WASM_API_HH */ From 69b1707d82b498eb88a097c72595a848c3e0007f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:00:06 -0700 Subject: [PATCH 029/149] [wasm] Return empty object when ref2obj fails --- src/hb-wasm-api-face.hh | 2 -- src/hb-wasm-api-font.hh | 3 --- src/hb-wasm-api.hh | 2 +- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 08e6671c8..f1495e75a 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -39,8 +39,6 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND HB_RETURN_TYPE (blob_t, blob); HB_REF2OBJ (face); - if (unlikely (!face)) - return; blob.length = 1; } diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 788261bf6..9ac51a61b 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -36,8 +36,6 @@ font_get_face (HB_WASM_EXEC_ENV font_t fontref) { HB_REF2OBJ (font); - if (unlikely (!font)) - return nullref; hb_face_t *face = hb_font_get_face (font); @@ -46,7 +44,6 @@ font_get_face (HB_WASM_EXEC_ENV } - }} #endif /* HB_WASM_FONT_HH */ diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 8f9c7d8ce..b245222f7 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -64,7 +64,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; /* This works because all our objects have the same hb_object_t layout. */ \ if (unlikely (hb_##obj##_get_user_data (obj, &_hb_wasm_ref_type_key) != \ (void *) hb_wasm_ref_type_##obj)) \ - obj = nullptr; \ + obj = hb_##obj##_get_empty (); \ } HB_STMT_END #define HB_OBJ2REF(obj) \ uint32_t obj##ref = nullref; \ From 108995bbc67aa4a45657127eab6b9f89171355fd Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:04:38 -0700 Subject: [PATCH 030/149] [wasm-api] Finish face_reference_table --- src/hb-wasm-api-face.hh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index f1495e75a..a250df0e2 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -36,11 +36,18 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND face_t faceref, tag_t table_tag) { - HB_RETURN_TYPE (blob_t, blob); - + HB_RETURN_TYPE (blob_t, ret); HB_REF2OBJ (face); - blob.length = 1; + hb_blob_t *blob = hb_face_reference_table (face, table_tag); + + unsigned length; + const char *data = hb_blob_get_data (blob, &length); + + ret.data = wasm_runtime_module_dup_data (module_inst, data, length); + ret.length = length; + + hb_blob_destroy (blob); } From d7a6671676f9ec5b6b2e8f3a964d1ef9f85f9e3a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:16:49 -0700 Subject: [PATCH 031/149] [wasm-api] Add debugprint1/2/3/4 --- src/hb-wasm-api-list.hh | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 35beb807b..ee41235b9 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -30,12 +30,18 @@ #ifdef HB_DEBUG_WASM namespace hb { namespace wasm { -static void -debugprint (HB_WASM_EXEC_ENV - char *the_string) -{ - DEBUG_MSG (WASM, exec_env, "%s", the_string); -} + +static void debugprint (HB_WASM_EXEC_ENV char *str) +{ DEBUG_MSG (WASM, exec_env, "%s", str); } +static void debugprint1 (HB_WASM_EXEC_ENV char *str, int32_t i1) +{ DEBUG_MSG (WASM, exec_env, "%s: %d", str, i1); } +static void debugprint2 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2) +{ DEBUG_MSG (WASM, exec_env, "%s: %d, %d", str, i1, i2); } +static void debugprint3 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int32_t i3) +{ DEBUG_MSG (WASM, exec_env, "%s: %d, %d, %d", str, i1, i2, i3); } +static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int32_t i3, int32_t i4) +{ DEBUG_MSG (WASM, exec_env, "%s: %d, %d, %d, %d", str, i1, i2, i3, i4); } + }} #endif @@ -54,6 +60,10 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* debug */ #ifdef HB_DEBUG_WASM NATIVE_SYMBOL ("($)", debugprint), + NATIVE_SYMBOL ("($i)", debugprint1), + NATIVE_SYMBOL ("($ii)", debugprint2), + NATIVE_SYMBOL ("($iii)", debugprint3), + NATIVE_SYMBOL ("($iiii)", debugprint4), #endif }; From 3b88bd9742b06e5f0ebd90b39f1fdce6b2129e91 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:27:46 -0700 Subject: [PATCH 032/149] [wasm-api] Make ptr_t a pointer on the wasm side --- src/hb-wasm-api.h | 7 ++++--- src/hb-wasm-api.hh | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 7f25b996a..5af0ecd6b 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -69,7 +69,7 @@ HB_WASM_BEGIN_DECLS #define ref_t uint32_t #endif #ifndef ptr_t -#define ptr_t uint32_t +#define ptr_t(type_t) type_t * #endif typedef uint32_t tag_t; @@ -79,8 +79,9 @@ typedef ref_t face_t; typedef ref_t font_t; typedef ref_t buffer_t; -typedef struct { - ref_t data; +typedef struct +{ + ptr_t(char) data; uint32_t length; } blob_t; diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index b245222f7..a005024c9 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -36,7 +36,9 @@ #define HB_WASM_API_COMPOUND(ret_t, name) HB_INTERNAL void name #define HB_WASM_EXEC_ENV wasm_exec_env_t exec_env, -#define HB_WASM_EXEC_ENV_COMPOUND wasm_exec_env_t exec_env, ptr_t retptr, +#define HB_WASM_EXEC_ENV_COMPOUND wasm_exec_env_t exec_env, ptr_t() retptr, + +#define ptr_t(type_t) uint32_t #include "hb-wasm-api.h" From 743cd2c46ee0c81958e2c360d56391da679107f6 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 20:35:16 +0000 Subject: [PATCH 033/149] Fix function signature --- src/hb-wasm-api-list.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index ee41235b9..a28342808 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -52,7 +52,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = { /* face */ - NATIVE_SYMBOL ("(iii)", face_reference_table), + NATIVE_SYMBOL ("(ii)i", face_reference_table), /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), From 50b22368d04893afa9227ac2c3321ef43c5ae638 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:41:41 -0700 Subject: [PATCH 034/149] Revert "Fix function signature" This reverts commit d70ebf98b0c696f3c66a20b1243cb347e3e3abc8. Nope. A struct return is not returned that way. --- src/hb-wasm-api-list.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index a28342808..ee41235b9 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -52,7 +52,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = { /* face */ - NATIVE_SYMBOL ("(ii)i", face_reference_table), + NATIVE_SYMBOL ("(iii)", face_reference_table), /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), From 5cecfe865919ff2c72a0409e34265d79f78750d0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 13:58:40 -0700 Subject: [PATCH 035/149] [wasm-api] Bind buffer_copy_contents --- src/hb-wasm-api-buffer.hh | 58 +++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-face.hh | 8 +++--- src/hb-wasm-api-font.hh | 8 +++--- src/hb-wasm-api-list.hh | 3 ++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 43 ++++++++++++++++++++++++++++- src/meson.build | 9 +++++- 7 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 src/hb-wasm-api-buffer.hh diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh new file mode 100644 index 000000000..2130167b9 --- /dev/null +++ b/src/hb-wasm-api-buffer.hh @@ -0,0 +1,58 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_BUFFER_HH +#define HB_WASM_API_BUFFER_HH + +#include "hb-wasm-api.hh" + +#include "hb-buffer.hh" + +namespace hb { +namespace wasm { + + +void +buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND + buffer_t bufferref) +{ + HB_RETURN_TYPE (buffer_contents_t, ret); + HB_REF2OBJ (buffer); + + if (buffer->have_output) + buffer->sync (); + + static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); + static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); + + unsigned length = buffer->len; + ret.length = length; + ret.info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); + ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); +} + + +}} + +#endif /* HB_WASM_API_BUFFER_HH */ diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index a250df0e2..1e6dcece1 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -22,10 +22,10 @@ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ -#ifndef HB_WASM_FACE_HH -#define HB_WASM_FACE_HH +#ifndef HB_WASM_API_FACE_HH +#define HB_WASM_API_FACE_HH -#include +#include "hb-wasm-api.hh" namespace hb { namespace wasm { @@ -53,4 +53,4 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND }} -#endif /* HB_WASM_FACE_HH */ +#endif /* HB_WASM_API_FACE_HH */ diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 9ac51a61b..d077b4857 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -22,10 +22,10 @@ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ -#ifndef HB_WASM_FONT_HH -#define HB_WASM_FONT_HH +#ifndef HB_WASM_API_FONT_HH +#define HB_WASM_API_FONT_HH -#include +#include "hb-wasm-api.hh" namespace hb { namespace wasm { @@ -46,4 +46,4 @@ font_get_face (HB_WASM_EXEC_ENV }} -#endif /* HB_WASM_FONT_HH */ +#endif /* HB_WASM_API_FONT_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index ee41235b9..26357f197 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -51,6 +51,9 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int static NativeSymbol _hb_wasm_native_symbols[] = { + /* buffer */ + NATIVE_SYMBOL ("(ii)", buffer_copy_contents), + /* face */ NATIVE_SYMBOL ("(iii)", face_reference_table), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index de55bd1a7..20720f0b3 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -27,6 +27,7 @@ #define module_inst wasm_runtime_get_module_inst (exec_env) +#include "hb-wasm-api-buffer.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 5af0ecd6b..98cc1e4a9 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -72,6 +72,9 @@ HB_WASM_BEGIN_DECLS #define ptr_t(type_t) type_t * #endif +typedef uint32_t hb_codepoint_t; +typedef int32_t hb_position_t; +typedef uint32_t hb_mask_t; typedef uint32_t tag_t; #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))) @@ -79,12 +82,50 @@ typedef ref_t face_t; typedef ref_t font_t; typedef ref_t buffer_t; + +/* blob */ + typedef struct { - ptr_t(char) data; uint32_t length; + ptr_t(char) data; } blob_t; +/* TODO blob_free */ + + +/* buffer */ + +typedef struct +{ + uint32_t codepoint; + uint32_t mask; + uint32_t cluster; + uint32_t var1; + uint32_t var2; +} glyph_info_t; + +typedef struct +{ + hb_position_t x_advance; + hb_position_t y_advance; + hb_position_t x_offset; + hb_position_t y_offset; + uint32_t var; +} glyph_position_t; + +typedef struct +{ + uint32_t length; + ptr_t(glyph_info_t) info; + ptr_t(glyph_position_t) pos; +} buffer_contents_t; + +/* TODO buffer_contents_free */ + +HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND + buffer_t bufferref); + /* face */ diff --git a/src/meson.build b/src/meson.build index 1842bf44a..db99bd79e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -324,7 +324,14 @@ hb_glib_headers = files('hb-glib.h') hb_graphite2_sources = files('hb-graphite2.cc') hb_graphite2_headers = files('hb-graphite2.h') -hb_wasm_sources = files('hb-wasm-shape.cc', 'hb-wasm-api.cc') +hb_wasm_sources = files( + 'hb-wasm-api.cc', + 'hb-wasm-api.hh', + 'hb-wasm-api-buffer.hh', + 'hb-wasm-api-face.hh', + 'hb-wasm-api-font.hh', + 'hb-wasm-shape.cc', +) hb_wasm_headers = files() # System-dependent sources and headers From d45a13f1018a18a8ea797b717253cbf6f42f9fe8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 14:28:16 -0700 Subject: [PATCH 036/149] [wasm] Add wasm-sample/ --- src/wasm-sample/Makefile | 18 ++++++++++++++++++ src/wasm-sample/addTable.py | 11 +++++++++++ src/wasm-sample/shape.c | 25 +++++++++++++++++++++++++ src/wasm-sample/test.ttf | Bin 0 -> 22116 bytes 4 files changed, 54 insertions(+) create mode 100644 src/wasm-sample/Makefile create mode 100644 src/wasm-sample/addTable.py create mode 100644 src/wasm-sample/shape.c create mode 100644 src/wasm-sample/test.ttf diff --git a/src/wasm-sample/Makefile b/src/wasm-sample/Makefile new file mode 100644 index 000000000..e80d481e9 --- /dev/null +++ b/src/wasm-sample/Makefile @@ -0,0 +1,18 @@ +all: test.wasm.ttf + +%.so: %.c ../hb-wasm-api.h + clang \ + --target=wasm32-unknown-wasi \ + -Wl,--no-entry \ + -fvisibility=hidden \ + -Wl,--allow-undefined \ + -nostdlib \ + -I .. \ + $< \ + -o $@ + +%.wasm.ttf: %.ttf shape.so addTable.py + python addTable.py + +clean: + $(RM) test.wasm.ttf shape.so diff --git a/src/wasm-sample/addTable.py b/src/wasm-sample/addTable.py new file mode 100644 index 000000000..f8e38858e --- /dev/null +++ b/src/wasm-sample/addTable.py @@ -0,0 +1,11 @@ +from fontTools.ttLib import TTFont +from fontTools.ttLib.tables.DefaultTable import DefaultTable + +font = TTFont("test.ttf") + +wasm_table = DefaultTable("Wasm") +wasm_table.data = open("shape.so", "rb").read() + +font["Wasm"] = wasm_table + +font.save("test.wasm.ttf") diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c new file mode 100644 index 000000000..a95c80a44 --- /dev/null +++ b/src/wasm-sample/shape.c @@ -0,0 +1,25 @@ +#define HB_WASM_INTERFACE(ret_t, name) __attribute__((export_name(#name))) ret_t name + +#include + +void debugprint1 (char *s, int32_t); +void debugprint2 (char *s, int32_t, int32_t); + +bool_t +shape (font_t font, buffer_t buffer) +{ + face_t face = font_get_face (font); + + blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); + + debugprint1 ("cmap length", blob.length); + + buffer_contents_t contents = buffer_copy_contents (buffer); + + debugprint1 ("buffer length", contents.length); + + for (unsigned i = 0; i < contents.length; i++) + debugprint2 ("Codepoint", i, contents.info[i].codepoint); + + return 1; +} diff --git a/src/wasm-sample/test.ttf b/src/wasm-sample/test.ttf new file mode 100644 index 0000000000000000000000000000000000000000..2ba04f611b660189a9f8023047a0e7c7e52985f3 GIT binary patch literal 22116 zcmbtc2Y6IP*PfYscQ>25yXgs#O%@V*LMokr^crazyEulfAicjbLY;SbLO-;b8ZMFgjjHr ziM3njE?sF$@&_Sw3u@E5<@6g6R}i?DkihPQ7{_%RFsM`Jt$?=)@p}zzPWKy-oI1_0 zpb;SqWin#aq>+W>5Gh3259Pk2il@sXcITz)DV+)^}G~><)^r!z7@4x-j$fF+=$i58@SIkooH-5^ z{)hE=MrWd2f83;LGwl-^?Iy$@Wm-M4VAROV!$0he_t)b2-AN;77P8kQBc8QH**baT zq%jt`nGEDtqAskkVCppIBr+QJ9A0Z-(U`&)$KL&b5Z%kD>p>`CfR{O)Bn0<_%pv6@ zf(#@&Q6eN5m=ac>=uwvN96#rH-rqxhc;7Hl1?`MMX)G= zN+<`4whUu5AnkGAg*<`tQ)E2abGVqza_*7YjfaP{{fk8NsUYO|ujc0b`Wfy2=iOMx zXT}u2sVExphW6uk9O^Kfc`sq$iPpIKbDTvxMuNyZV9$U2-imv)bpGx9UcBKNia#Ty zG1*GylTtF1oCgK^k|AU%89@e%)({ar(dp}ye}!vsUA;kw^*e&>zp z#%0D&{LTK|{SO3?fY^Y(0WSrd3Jec?G;n9&A3=FR2ZIxW`vjK<9}Ni$$q#uxhbw})N~YY;XkY(uyd-Zy+}1dA9Q@nU3fLi@CrCkhMa=0A9vqVaD9&Zo$dfpx;b?}^7)Z=?GLeiVMc~UZI8P<{cs3clkV=}979@>ikv5`~ zf>PFf?|Aq32;M6sv(QIAdL4%`cpv<&7P#w%rv<2=h*7Mh2Syl${;i}VY9`{^zxJ)E zqBI7OaCnMw9!;7nflUy!quCgRtGyhXdWX&xVj+qYP5_m&OC zoz(x4^ac^UPJW_K+a}TcPM6s8A|qni|XR469~5-rC0R{N_EC(<+AF83%UB zOccHN7mw(bS(n!ZEF9g})h_T0d+DgQye=rr6lm1*yWqB|Np;NpE+jj>QC;z_DW{_g zDq^<;7=VDa%iR2^&h`#5bgF$gKRv@w$uV@Ay&pdn@KZTI?c*mWKgGq+>HLl9{Eg}S zjp_D~di-5}y2Vd%_0VoOKb7;-C;a5(rw;YdxPYIMt!P((6PQ3DO?xTEoZJdZpUXQ$ zor{%4bt{b0@F$unw5iqFCBL&mM>JDdQ-wB;Z>C61tz8wVLD&AdwmfU8wY10RQt;`x zk)sv4fwQL1usm8#CM@p*a86n!m>fR)ozKx=5K=o>dv z{9HW@$W^jq6_Qt4%KICTYl~OrmzKts0t2mFBqvnSj7flttE#{pqsmnV;R&`$u2o$B|~y68j;Sim86mMW9PhPLOc7DGV(2v zJ9q6pkQ9xYRWy+lJykSj0x6m}a@u4PLwXWJ$01#;r0pa92jbkde@826-lt!0ocneh zjC22feQ+MwzZagub~ynR*ZUH;E43x&Q<72Qt@v}7mCzCW=*b0>NY2=zk)uf0iTO{B zB-!GeF3wFSPoGo-4a9p!zrr+8TwP9% zq^^LDk+JwAczYUZwQ(ul9w5r0Zpi&m%ErMxfd!HmqMWl|r zjP#|eNnMsf8n9ThQtC}CXg?Ll9vs7PJb@zv$0i($aP+}Zh+{PUl`NqZq>*eU-R03_ zquiV}Ji#Tk?34p_5VHm+aQp z;2G+5^GUHTP+TVg_FeK(k|UoaEp#cQOczP&7@|o#ywgfPLqg=!WH5^(tEKLwA+AO# zmt;#@h|Ff;`4BQmo=eK)6!N&dkPMgGkzwo~qz7}wv(7K22V z1D-N?uS3OqWn?%zfU*_Wt;8tjlM?A060es?6X`aD=mQdgYcASk(?7^QTtjtJ$U=6D zbi&ad$86~((ng+4;^gI|t^U00;LpKrIq)?z$xztJ)}$Y-SdLGL^Nc^Zw!K?6V}xwR z2-%Er%cdqng1;t=DVhu((PV-1BUr#t2fZ+z;3fSK-HYJaDAap?oPY6ieg~h^^)tJ# z54|{la#cHjaDL{#yWzaz4t8=68bLY>z?EKkzOI-hra~Qw-?|vS5wD!(-w7Z90U0Cxc z+MA$mElv-<;r!$NI_K}cHLez}J|5ar2ygFP=lhnZtDTVtpLuGW|8`z=UdNn@nRH(C zJh}hQS;cFdM}g5roWF3s=zPn0)479~iN$%+`7LHFNVK?4%+6-0`v}j7bF1@v=Ng<} za8@|WorloEL`=>y=keMZev^ba6HyoCG@-qickLPP??3;XKRGd{IRDH?cmD97TcgFl zo!3Nr!OhOA|7-hT=edVJbAI>mdRMLUCcnDQ|EZn!%*~&!w=N2sqCIk5K@;@1SgUmN z1Ng>S3~m#%U;FGTrv?A1h%BH*OMd>(KM%)h?VLyXo%1}8CODsVo>qT$dg%pKLG2VA zkGk4BPvHKh^F5qPoM&qLKr8q2`#94@aq!&F&d;1*i#Ay6-~Z$II*;F9>nej+KX~wo z^GEm7^PED?bKtrII14TGkk)Z%^W}r}57syb`_xgA1X~>d9=0H2kcd)4(ug$0DyTV0 z7ORw0taZ{!OVSFuy$$@LcF^NpNLQ>}dXV1aQTQVR5a$?-mD%HDn20v$NgiZK`+4rd z5E1$Bbd?@2bDhtBu4|;L;4bsJU|0hq3CFC42>5Fo;0AFa;Y|o@;`Km+ry=9u^<01J zgq5-29E3U!w`UyBT`XGi8l(?a?L&7qxW z3av+K$lv59`4@SOY$F@VYEpt@9(t&Q9yk{SqW=cqfkueY@Yzo%8L;Fn#mw`0?*Koo zALcq2vpp2EJsh*s9J-+u4hD_42wHLx`s*p^IRLfXvkO7{RJ1pP-rZ2w9Y+rwy>JY| z;g7LRVh)2qqmIC<8@RtajvhFA;TVKtBw*vC{esbc!5BlK8~upC^IMD+MuMH6h&Ow} zZV!gm&&8F$A>(NXo&=)yC)5Os8a|2{HQ$Oh;0e4j*m(ebo&v-zoeF6UJ=V@yjn*yU z9khl=(FTyVbAE-9a|P9hI$su0925FK0+>gT29U1i>K#xljC65c0UkdAkDq|YPr&0R zG62UQ9D||P@&Mrw;57^fBRzrBP}E2y2+uk?|0E+};U!{&Cd$R#Q0Nv)LY+Jog;*FQ z(E>?~;1Gav6i{A7FFTxn(Wi9^+5}?u6w)PEVac%-tX}&(Q3yaGz|uaiLa$T&o8-n{ zjQumuPW8-eX_eA~+N3loJu@{TOrMZQhwcpe*9Tb>Teh5-MK{agcVBPTuHBF}ZMZy^ z$QEXUh|1uM^?Gu4FrgR8oVYHOjmS4 zBH^&16we&$ot*NVTh!P^RcYP+<^8E&|CRkey4L0Dy_}WXs9zWL4&9`VrJI(~CzsP5 z>Ii-;SGO%w$1)pzlIy6+&J@|K-+~xUz6*aN5Ho5hC!mnPv1AEGPGF-+39RO46F*mk zQV+h&R1xh}OfV^N)e6pu{9Mt<^9XpQCnhB(W~66kWu%2iM1eG|Q&Kfmi+xXpQLXRFbUpu*c{XDK`^PNBG7V1tw8e?E(xILNU z!dC){l7_Dh1sk7Y@Q{DC;-6e$mOUs+riv)9qRph##k?geHd94oull;CiWZ)tJ;Nq= zGb7oaCD}L;vyxL?zId+3m5xYX`G7JE?xxPPK;DbRYa!k8XLYA)lSfmp3llv$v$%qMbXJ8~ToU za!`-HW5@QdIlFN8uI1ep?%ugv-*3#QK|TA89yRcfPI3{CS&nhul>g8jM_fM%Hh1Ym z_@NHyQ%4hlC={YNb0pN^%wcbAtivx-LSue0HBR6cv#T=H)g;<=i7-dwnNr785$E9x zdwiUk*MhHFcxnZyvIMtqf`K~>G!wjGh_of?LA?69)GTV?%p-+GW@ToAh9K3L>936J zbbQzP<9P)WXy?v5pRT?#vgfI%K7*KizGk+%HGb#w3B|>oQ%CpiKY}jJSEd)Q?Dp#0 z`xfV}>)%hEyI{L>*FRG`b@{eeA>AGI^n&8&q#s7C9FX!@`$zH$ATv+N@x&qYkIAKd zbZ+efV7b0=gg;By=~k&LDc6~c)y8a{eggEpkto5*iY%tzV#$g`0N-E=jfgZPCb7k{ zetkA^^${aoF=%??;#oIYzi;UdIx6hd`As-l}Ug^GY`OZ~iPUGzcERF@SgW!7$f#pEdwN}!(>dlB} zL24BJk;QG`W37Z`D<)?#hjm=kF@QP-aDN5!jGEFSi`y@7X+q-4Rz+>v6tx zynTD#hsC*2;yXwZ`~pDdV;)mlMA~Rt#dbDg+gyD!Zsn#z8vP-SL7-F{*O>ko(_hn7 zLUD0f^fy(QJ+MP{RYZA;K*hvt2~5f`S<+H1p-DnM3}&HmvSj}&H?MyqU-|i`D^lg6 z^5+(?Wy_Z>me?fqhGKO$)QdN-p8p=0>;ZsZ=-z_g)Y=7^ z4v^_I$OsCAD+UB7xkSs!6_N)cJCk^AN{e`#S(niujeT)gUCu0Z<+ICnokHKkV3X5; zcO*PLK_VX`to2FH5n)pp4g$%itTDvVwaOx#}C!y`?u0pl~MrXgT->I;QJX}p|P z)0N&S98@~B^!Z9EeevN<^%r$E`*JZ$d1m{dqSaf=KDhH)#pmkhY98Qw!g*8r2k<2# z#wp+n0=hvO?$IbjbKFUg&~YT${t2PO?Ar;QVo&CG!wD@?f=m_OJRf9&aKZRjBzl0i zhb9t^QnX2_Q;oB?uEeaS0TUCq3O1K1fo9~^Q?}N3TTW=gBpY3_eBpZM%<@XdduMJu zJ$~jwCsD_$&co~HEqQLsn&na|TT(>HvMI0saOq#e?M)MvXO92-%E76nWecAv!6Jvj z|0Z${c-9|!p{EO99}ht&363u{${bpnT){jzDDZ!%NeQU7`{@Jl*d8QWaqz=!LcnPn zjW-*lC8tjxcI30N6OL!;i3oaqgZc*TKS}znrWHHgkju^{NJBHgQJ0K%K|w)is!?4` z0ha}8s*pXy+YLdK17%3Ai1Dh`8De-Xh;FXtGZE%_rWm+Uhu#Q{5095%qb=cK5TC@P zvfCU)AOB(?T!3e-2+zV0wUdK}eNyBY= z*-#5I$;dE~22ujtDVKMVo+$N8xacHdQ%}A7-Ct+Usx|a6I^f*!9dR$tnp?g~SFu&T zb9IsW=lQGZ@3f<%8(mFz>l}qegFElPd}!VJN|cry5Bvdi`oc{SP0q#3#pHXFY2D-aLjSIMjgR+oYRtw z;e7g%AX!Otn9s~gF-dr24@nXe3{S$#zfi(W6$w5BbBl~{!QhN)PKLzYFhMgaywcQ6 zknZNxCyN$4tIs)}EgD&P^H|l-8#dqFux8te+LO}a?HTWUbM@`w zcUz?FnRw{p#hMppZ`ye0xpFyX>9m4n%dV7x3r0Jg(j9?meXN}X6~(eh!!r~}hjKin zP%bijc|#;|7wXHZzKR};32$t{58OQxIB}jZhntAbmI1BNAQdK2^SGtxMlH3{k(DFB-23Wb<(o_P^h_C*HV8*o1OKE|YjbN__p=A=?<>rUR2X3IUnXAxd}hNn>Vu+>0L7Y_3jPW@O>%^7I1MULb2>D6>L zLR^e&q~+2j=@W330b@b>;ZuN&6~r_9kaKTEfeyK^fkV1f^RCp1A5!xd9926w*DoQ* zrOR?J4>V#W$f40yD>@F1fF(nUbxe>3*X&`XRC-rkvRYkS1!#IX|B{L^-%*hI0WNHO zESeSbZN+?7#CjYFJBj5c%PyOQAm7p8Bw2-=2w&ZdBj%thL9hZS)s( z8(;@IVFyo1b$Km*fZ2~rx>RisN%iAw8j@_+`GxR1KT}03R_jYC;nhm2sUp^^1`FQC z)k@>!3Y%ADh^ZpmQ{?Omy(?V&#H>sU$0}0NLj`A9NPL3K40?;qOOav`z(Oq+m~+`e z^%1-_E0nJS^rr(a9ypSbxxD9dt3yL(9nF7qeolJmnki-aP_mNG$jeEZbKbqUNpgwKhGJE>-^zYx*A9oEJ@Z-m?P-$(G z4vq`gzS&O~(~YOplE3e$YmPOwO-)L@N_Wy&8cN@~^^H12UHRO`vBQn@cHLh%S0c|t zR~Nd|09!iH?jI1yY2|N1puoix9(P-dns5ShA<~1|dae=tWFBi!{Crs1?k{RN3rlbz zxsH{jj~&~Von%dSv1TV6edw(_bdI`6r`*k9F>E*F0`9Y10$73}UvY>hiD7&!s^(|e zBZ)Afk;xSy9`UjJg$Q>GhRWor<<{3r;ij}mGsnBuY+_w+qbR~@iEqF!&_MCK>E)kq zO&eXjO#M+kMcXaetbVIj(S&&$%F5O6b;|Lov0IzOSI$5F4cqPbbNOs)*fekA%t?^Q z8L$=?Ayah_1%6P2)_TT>uY~X@ONGS?NT^NkKm}I%b@+-yFWaodoRQA?h(y{F!9Wb_ zb2!bJS@OkL_3AIGs(!GF`tSRZMnxWr-nstJ*^~Aid+X3M*Y43GnpyTCO@Bpo{P4yW z_1AkVepY{6wI8$2DSZ-@4nSPh?ZrQo&YX6EUQmLvL7t+El(Hof-iU0F%uwiv7OZC#$=9(nRrq;*^7R-Lj< zZK}bL3>b{iCt~Iv%Jz066W$~ib@##G=gax=)@N@)P2l! zbbS6!n*QN4vLa~S? zhcVfH%Seaq|HSyUkxj9e8~G{)7542$TI4<9V!6i24VgBl>HLm45~`}$m1CIQ;X1|f z94q3p`xZ_!0F?wkT@2?F{nY>weWlSERaIz;w=%Gz`Utlni1~}RL_>`)g^S4{#tI3J zrI(O^0n*H_2%0Ncm?Z@qyLa}B->a4_n6ZZHlzac2y?N#Rxn=8@x+$g;6r1K6#m9FQ z%Xw``pGdD3v$kXJA99gx?upHZ@klx-MLNtLa+vrHv$x_4JH3u;=j_l-eOLWp{aAgm zeu;j){#E@!y>S>oH3;HizNwAH+CjXfKRVuH zxgdS0jCe~%N{a?u4ASU$X0KUsY%Ao=)sMbE?t-ze^dMb*rbZKf(X7_GbBPi0aBh5% z0|fN!2G_-3TvA#XSsL&Ph1ChgmA-(jpUeMLS=59zsx1`rd+%}iWtg2THu~Ned81>y z;{yQXo(;~wM$+3({kePlkVz4MXp^FGj{s2Wt6`X6OXBKK5C^*AfEDEAQ17)SvHa3yRn!aktBX{$5&=V+(ByVawg zF)Gn;^{2ll+*7a85C``dxb5H~;*WSyKf8aJSoXkU2y}Z4f(ROUw8fV_KoSP~KJ^W& z=Vb)Jjvy0s69NV{w>eIVr}R2Dh zZmgZNbS1WrDanCtE5a-!lF@dO)UZ~9d<=oMO{M606b9#tU=K9*#28`e6Mff}F=Fc` z=Kj?(UrO?j+5P)9Tnx0`n)F0A`1VyAdU^d%&mPcuR<-o_tM4} z*Ry5aKH2!%)oCBiTC`zqLEh72pMJTZ;rea?k_saHcqWFP-_o*#jd4Y7aCSeEgw_k6WP-2jyJ`) z++dH~kU)}P2wia%v%H6@&dX`H)bH85Z@lo*>pG<-;f#9M#3=h#`lcpv+rBrpNnZj6 ztN>&OV9=A6c4FYGCYR3haR_~^QmvXJly5OW%?k%VjbS(Moa6gFjvvlC<%+vJK!^U@ z;p>&31&(fg@1r1ml&O!13U5L91km>(5Z(}>?Y1g|hLE-}ZBeMWPWEOT+Z8XV% zQyTrQP9ti4kw!O+`}UZ+eV=+hGE)6upSq)pepvOobf?BoIWB#FH%D$zP;l={z(BE$T;tCs1X*YDO>>QN;T z&fj)qf(znNVSITMil(teblA7pOZfbIb(6aBJ9<$~yDBYU%^l4h39O~#eRhQ*U5WDi z2>A#g^#fl%2&s?Pph4;e4z$8}+$g58F?7x))xS#hzsx>kU)B^juCh3374IL}F!F5l zZ$w6m=-;PHjoZCh9Z_|$3Pc1$=OxQVok1YU9f1rU%?PKhq>lG4OZ94QNw>FdT`4cv zwt|niT791d=;uSe>JxHAeM)x=@UZm<(FeWCeT1UMTP-kdJ#GieWS0 zUKr_Yb;+?~`uTUdZ`7CYk+azs5_~2|Qj(7(c^JOhPO(mcUBWOrt{dEI<2~=bMw>iC z8}F5msP)+Ajz;KBcIHbPkyxtqg)vbq&r#be>Mts*) zi8Luzx8;e1AV%^a6DO<2TOJy75oM9WB?}N2tZneR&nn##AXe$HL0-#pz8gXqWH*pIZ+LG zY>RD``r0&W|0M^O?>;hUR7IuK6t@GnEYxfTn6CCK{+3@WzudN{vp2zQ#41D2| zX|Kx%je9B!Y3bF<)yv+>8MJ@#T4uhZe!6O|o;coHu3k_bx_8d( zQJd~LgBgDuGj>~Q0NAw|rwI8+CZ=bIC#N@Bkd(*yHd+;~EeSYQnD>iRhICIFbU889dC2`yS zF1hqZ{?ZStY)4LWDh~&ozk)I(_cON=?#ZXezWw+lC&u$>KBC@Ev%kjNf?d)J>dmMxq3r&^x1JDr&I1(>GVr$ zf>&(j+6v!tu+#c*U^K=a62V2%#Ww;It6Rc-W<;3h`*Gx~?X@k$FV? zV?_w`0~5@MfzLpAINxo%ws-I0J=q=nlQV`qdF`6Cx4htucg^dK`A-&=*9-#PN2r6P z-$D0i*o7tbga(P6YmmSb;MPYz018>u02C|aFgG4TZ3!urIHvZHNU?VTrQ;P`v4@L? zTwi$wPB?--J2Wn?%5pFAk<$RF&B)*kmc@N97k`boaqMJ)arZyxrhn6B#EjRLZYZdF z_ou__m+tF7XwOnGnu9hen|be>bH9&%tYFQ?(qZ#y>K|{N-A;emd=7I{1_k~#WW`K+ z+Wjq|Vs1<>84<=;!%_1Fg^UD4H3xGXT_FK($NarDD$Pu@^5rlOBI?DupLTir1bs}Z zr1=FyN)jt8rMEVya~&D%qv?es`quDd63hTM&*R|H^X$0}c70GVp8+`T_@r12>jRL+ z`54@ts-*YqAGjhco(+IOm6h+i0{(Jq8rQ6S`yp*PQ#y~Ty=Wdr3`86lIxRAaj|eqI z+#^1yj4{pLfj{U49^em8t}uCzPR*wHgrp2HUuJ-TWrIlS>!tqCF5Q~mzGG-sMkZ4G zGUe9ydZ_PP*7`j%Kt4j_ns#tOMma6yK#(O6-xdkvg$2}v#YYEdPLgQmiPrduXiRu( zY+L)rYfck?c#H1G4+d*a>is?88t&}EbRV8pSviV6dQ}}tFML7&nx!t%lbYc(=xDXA zqZC}U1v57Y(E7%`G-$P{a?J|GWbpZ>WI6oxnZlmTw98)h#7EM6P!&CCEUBR?@D2k@ zhJhtTAr^&L&Y#qe3m*)okU=O6?`VA#cZ(u1H<7S9f?wUaAvSZmErGKwZLOPe`n&#_ zuNTwN`pWThpDGDBeC^;Pm2&HuWpDHyp)PeaVW+1}eR`ZDm7Tb`x#k8wlh$Mkc$vvS zdw^S}xD(@pmk%nMX&2CYtw6-`6j~v`N4fyBuXxcm`Z@1=iI?2EWQ(_W0W5|L)|<#Y z>I(u4E4)zUo^SWR#jkcOe^f7wyqr7$wxY6V0n(!<>=p>@0@d+vpo%?n9 zSJXV~>gvntt;0LZR=m2qY}qcBv|TM#&!|D$?v>Hh8sq*;U!AglbqUawsUzfi0=gJd zXb*MS>`+gv$43&yI#mNz&`#67g2Y1m3g^hnNBU3J+vOu+!qYfA_&Q1TzCCtgO66|j zln>tfu5wGsu7LygF5b$_x7BlV9k+FtW|pa!)VuOq=hr#zt^Jg1vEk|n>6ROlN%wgl z4{(8x9TBXqVWQ1|uOH$w9}jmzU@mNCZXY}PbT^U7Gii$&zr*KOGfVkrE)HfJfI|@JW|xEcuBJ-~36awX?<1MD_5p^@D*?uC z3~(hoQJ4Z@353A04UJnI>{z*98oD#HjlnS_IzE%G zC1Tnokx|K0a)U%BowtC4KHeeBIwmE)nkQY>_mQUb#QIw1!~L1KB7eY@`696?>a|z; zUEO!+hr`c4Ii_el4S#LGkChAFpHit?UX=d~jq5d_?Z9a{ix0iKw)f=RZjW?sKX}$- ztM)zq%7|f;264QM&YNtoE*oCZNPDoC7bKSc8cjv=gI3TYY}~aG`FwnZ=#@#vb9cNp zq2NC7gdPi5Ygn_|M0_SI9ITL-!95}~U3&U-W=Ctw9$n_lJ8?pnt==tnjO^GkaDCYN zQntMu`3RJh!27%meSyz@?TCNW&b$w!YFQ0JUHOtu6=eqcd3*ZnDBfbm{4Na4KR#&Pg)~o+G$$ItVq0eg8Ii)uDPD<%DAIYt2E=Y}m zdn4d}8n6XuSra}$i5QWFw9o3;6C?5$t7EK_w3(d*a~0r| zmxwg~AkL-$g4vRiG9r1-0s#4?=A_zZ%w=1b)F<*28maS6(-5i2y*TxEDQLaid(32f z5)4>!L76uJi*L+G8&~5;?*-_8A2VvrX4SmH208Xi=^HkbNYT$Pp4}v|O+%$Wf0m)c;&o>VCP`JM0yAG(AjC5N@YDNZ9RU-9fq^BUk-4e-W zR=j!sJlpqX;@O=$r8Da4)F0kH^~b8;tKYhsHuVwp->1LJ8}h>$_2*82U>}mwI%~0} z2Xbe5kcxI*w@&?C|1O6&6z~?g;nlg}MR+7C*=`OHKCU_09%lx;7B6^p7Cx;=#LYws zbrxVYVZ{-FE%&tW1a9LEp&uF!95|a;VW;QMAJ})VBlW+0ZD9ZFAJUjdrlwx4Ja=o= zAE)2`HWlM9q^spyQUdlAI@`@+Pr)Tu!nKLwURt#hBBB9dKDn`|ykDaQa6aLioXrs; z(1#m@GaV5Lj>(X39a+|W{_dWQ`wo2J(DL48N_O4e!zJUkFEZBFpYR0RaVGl(e>M&I zrlb?w%=YSzz{?+O_Y+>e3m(C++D?t|$Rgn1M|kpZ5XG`f!4aT~5)9D19t4ZD&aeo+ zNCAXAwbm0n!UqgtAuOKlb?l~1CuO(o)otre>bGgaK!ujG%@b+N*e)&F^=?(P_L<4! zCiGhc2-~xjY!e_%BxQE1m(}xuP$bs^LOY2TYa^0;uz);+|8yh`vv)v(?ZFW2>Lv4% zNU{aQ1`ot`|6o@}F0yRBKF$FBsK0Rw(*3jri<>Tkh zo6xLn+q5bCzZ}eFo9QhP$?S-TrDsn-Xpj46#yg?f17&SOHKf3Rglc+g>qR=sf+^2y zHJ|UOv>}tF@rZ6n&=@@LoF~v_;aifI#<5f5CX0_h;J4umPU&M2rHCer?S8J<9O5pK zZvR-A0c}a+ZPY-n;jccMA8$&7CqM z|EsT9kHd!%&v^X6>2_OECro$(@eF835qFWU1EUCh6)GUG00N7Kf{#Vll1qI7wfOjQ zSc+E|Jg$>#KTQed@|kFZhH#}c^Uw=l7U*ExQ|9!3|Jt?6y?bd@#TeS8Qa38>+yp=e zV2fWr)b6gx3dTZQp9(0GkoUUX1*f2t2B#0*t-!#lQCFa&6?~R2jnS;J_M{OCjX2%8 z_iaN#eBs0kNhl=oCstEMyccE-khRUfLi33)#RAa>BTMlQ#F?-KXXbO{9dDv5UDH)G zBmc^kO0`g4jSz@e1j;pa<0tZ|llF?~bIqTiOhYYu$A=X006t#Bu^|c#IVSNwAte6w z88C?#-`U9zpd_M-N<)(ZgkyLrxXAr}7?SQ80k#kK)|2iL=S*=mzCjPad?>*d)-8<%X^g%m=C$8&gnFJS_=bZk zEAb5n{~&^Ykj0l>X%QNo4TY)niuzJS$DZ@ccPyE;Q@`2&*^b@&_UzGy!gX7;r|z;@ zb5_k8GpbkT_I;XwX9olFy;42o;l%lTxuLV~xH?{(_rrq+akgZj;0{U2C=qEP>@n5dtkE@9bYs7Cal!3 z-^pH4UwV}eQ`@i!?1-ZaTeAROL_bhtJESF4>|M@vNsMp;wBBpig7x(x>8F13!LRGz zs_(2X4LcZ8KdycSmBS+N`EczQ9BlcF4e%7qX-8wy^b!+--MoROREg#PUAM|kk!`19ONTb+fQbcT%7%%oJ$ zcN_HYE@O&Uy!7m{Es{DlYyG09pJ?2#ZJXXbX?Dlbd9!xBvbLQ%I*dN^)@NOr3B<_;(r7LeY z6#eZK?FvOZldC^r8qiLB^D!80_UaCzuPNSr3A@Jo;`>#+uhtmz710+GNdcR?FU<`W tef2|M+XehnMVn|Z&f|SWR)an8*}1F79&8m`Ct6SQ9$Cy6Zyn9s{ST)Fy6pe} literal 0 HcmV?d00001 From 3bec8dca1ca108a9c64af36c768eda58bdec2d95 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 14:35:01 -0700 Subject: [PATCH 037/149] [wasm-sample] Free blob data for now --- src/wasm-sample/shape.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index a95c80a44..480701771 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -2,6 +2,8 @@ #include +void free (void*); + void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); @@ -14,6 +16,8 @@ shape (font_t font, buffer_t buffer) debugprint1 ("cmap length", blob.length); + free (blob.data); + buffer_contents_t contents = buffer_copy_contents (buffer); debugprint1 ("buffer length", contents.length); From cbd5c554fb28d3aa2ac834c28e6d89600e969f62 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 14:54:46 -0700 Subject: [PATCH 038/149] [wasm-api] Add blob_free --- src/hb-wasm-api-blob.hh | 49 ++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 3 ++- src/meson.build | 1 + src/wasm-sample/Makefile | 2 ++ src/wasm-sample/shape.c | 2 +- 7 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/hb-wasm-api-blob.hh diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh new file mode 100644 index 000000000..8a8f9e2a9 --- /dev/null +++ b/src/hb-wasm-api-blob.hh @@ -0,0 +1,49 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_BLOB_HH +#define HB_WASM_API_BLOB_HH + +#include "hb-wasm-api.hh" + +namespace hb { +namespace wasm { + + +void +blob_free (HB_WASM_EXEC_ENV + ptr_t(blob_t) blobptr) +{ + if (unlikely (!validate_app_addr (blobptr, sizeof (blob_t)))) + return; + + blob_t *blob = (blob_t *) addr_app_to_native (blobptr); + + module_free (blob->data); +} + + +}} + +#endif /* HB_WASM_API_BLOB_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 26357f197..6609c6860 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -51,6 +51,9 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int static NativeSymbol _hb_wasm_native_symbols[] = { + /* blob */ + NATIVE_SYMBOL ("(i)", blob_free), + /* buffer */ NATIVE_SYMBOL ("(ii)", buffer_copy_contents), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index 20720f0b3..f09b3831d 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -27,6 +27,7 @@ #define module_inst wasm_runtime_get_module_inst (exec_env) +#include "hb-wasm-api-blob.hh" #include "hb-wasm-api-buffer.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 98cc1e4a9..f919fc0d8 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -91,7 +91,8 @@ typedef struct ptr_t(char) data; } blob_t; -/* TODO blob_free */ +HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV + ptr_t(blob_t) blob); /* buffer */ diff --git a/src/meson.build b/src/meson.build index db99bd79e..32076c278 100644 --- a/src/meson.build +++ b/src/meson.build @@ -327,6 +327,7 @@ hb_graphite2_headers = files('hb-graphite2.h') hb_wasm_sources = files( 'hb-wasm-api.cc', 'hb-wasm-api.hh', + 'hb-wasm-api-blob.hh', 'hb-wasm-api-buffer.hh', 'hb-wasm-api-face.hh', 'hb-wasm-api-font.hh', diff --git a/src/wasm-sample/Makefile b/src/wasm-sample/Makefile index e80d481e9..1f1704e6a 100644 --- a/src/wasm-sample/Makefile +++ b/src/wasm-sample/Makefile @@ -16,3 +16,5 @@ all: test.wasm.ttf clean: $(RM) test.wasm.ttf shape.so + +.PRECIOUS: shape.so diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index 480701771..89ea28242 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -16,7 +16,7 @@ shape (font_t font, buffer_t buffer) debugprint1 ("cmap length", blob.length); - free (blob.data); + blob_free (&blob); buffer_contents_t contents = buffer_copy_contents (buffer); From d38f02ab30e3696614c295f88beadb655110599a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:03:03 -0700 Subject: [PATCH 039/149] [wasm-api] Make ref types actual pointers on the wasm side --- src/hb-wasm-api-buffer.hh | 2 +- src/hb-wasm-api-face.hh | 2 +- src/hb-wasm-api-font.hh | 4 ++-- src/hb-wasm-api.h | 25 ++++++++++++------------- src/wasm-sample/shape.c | 4 ++-- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 2130167b9..38ebf7c76 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -35,7 +35,7 @@ namespace wasm { void buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND - buffer_t bufferref) + ptr_t(buffer_t) bufferref) { HB_RETURN_TYPE (buffer_contents_t, ret); HB_REF2OBJ (buffer); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 1e6dcece1..bd7d0bb02 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -33,7 +33,7 @@ namespace wasm { void face_reference_table (HB_WASM_EXEC_ENV_COMPOUND - face_t faceref, + ptr_t(face_t) faceref, tag_t table_tag) { HB_RETURN_TYPE (blob_t, ret); diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index d077b4857..e495e8acd 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -31,9 +31,9 @@ namespace hb { namespace wasm { -face_t +ptr_t(face_t) font_get_face (HB_WASM_EXEC_ENV - font_t fontref) + ptr_t(font_t) fontref) { HB_REF2OBJ (font); diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index f919fc0d8..25bd0fc30 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -65,9 +65,6 @@ HB_WASM_BEGIN_DECLS #ifndef bool_t #define bool_t uint32_t #endif -#ifndef ref_t -#define ref_t uint32_t -#endif #ifndef ptr_t #define ptr_t(type_t) type_t * #endif @@ -78,10 +75,6 @@ typedef uint32_t hb_mask_t; typedef uint32_t tag_t; #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))) -typedef ref_t face_t; -typedef ref_t font_t; -typedef ref_t buffer_t; - /* blob */ @@ -124,28 +117,34 @@ typedef struct /* TODO buffer_contents_free */ +typedef struct buffer_t buffer_t; + HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND - buffer_t bufferref); + ptr_t(buffer_t)); /* face */ +typedef struct face_t face_t; + HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND - face_t faceref, + ptr_t(face_t), tag_t table_tag); /* font */ -HB_WASM_API (face_t, font_get_face) (HB_WASM_EXEC_ENV - font_t fontref); +typedef struct font_t font_t; + +HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV + ptr_t(font_t)); /* shape interface */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV - font_t fontref, - buffer_t bufferref); + ptr_t(font_t), + ptr_t(buffer_t)); HB_WASM_END_DECLS diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index 89ea28242..c2b15ea9a 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -8,9 +8,9 @@ void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); bool_t -shape (font_t font, buffer_t buffer) +shape (font_t *font, buffer_t *buffer) { - face_t face = font_get_face (font); + face_t *face = font_get_face (font); blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); From 851ef1380aec550e51a4d8055a9c8aef2b57aeeb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:06:18 -0700 Subject: [PATCH 040/149] [wasm-api] Add buffer-contents-free --- src/hb-wasm-api-buffer.hh | 13 +++++++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 5 +++-- src/wasm-sample/shape.c | 2 ++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 38ebf7c76..139899a8f 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -33,6 +33,19 @@ namespace hb { namespace wasm { +void +buffer_contents_free (HB_WASM_EXEC_ENV + ptr_t(buffer_contents_t) contentsptr) +{ + if (unlikely (!validate_app_addr (contentsptr, sizeof (buffer_contents_t)))) + return; + + buffer_contents_t *contents = (buffer_contents_t *) addr_app_to_native (contentsptr); + + module_free (contents->info); + module_free (contents->pos); +} + void buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND ptr_t(buffer_t) bufferref) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 6609c6860..14a1f60ab 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -55,6 +55,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)", blob_free), /* buffer */ + NATIVE_SYMBOL ("(i)", buffer_contents_free), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), /* face */ diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 25bd0fc30..1adb4e2c6 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -85,7 +85,7 @@ typedef struct } blob_t; HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV - ptr_t(blob_t) blob); + ptr_t(blob_t)); /* buffer */ @@ -115,7 +115,8 @@ typedef struct ptr_t(glyph_position_t) pos; } buffer_contents_t; -/* TODO buffer_contents_free */ +HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV + ptr_t(buffer_contents_t)); typedef struct buffer_t buffer_t; diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index c2b15ea9a..80486b2b5 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -25,5 +25,7 @@ shape (font_t *font, buffer_t *buffer) for (unsigned i = 0; i < contents.length; i++) debugprint2 ("Codepoint", i, contents.info[i].codepoint); + buffer_contents_free (&contents); + return 1; } From 099a0150e1ba3af96d52629a04fe5def489c4b34 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:15:06 -0700 Subject: [PATCH 041/149] [wasm] Add HB_STRUCT_TYPE --- src/hb-wasm-api-blob.hh | 5 ++--- src/hb-wasm-api-buffer.hh | 5 ++--- src/hb-wasm-api.hh | 9 +++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index 8a8f9e2a9..e9aff8968 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -35,11 +35,10 @@ void blob_free (HB_WASM_EXEC_ENV ptr_t(blob_t) blobptr) { - if (unlikely (!validate_app_addr (blobptr, sizeof (blob_t)))) + HB_STRUCT_TYPE (blob_t, blob); + if (unlikely (!blob)) return; - blob_t *blob = (blob_t *) addr_app_to_native (blobptr); - module_free (blob->data); } diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 139899a8f..949195167 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -37,11 +37,10 @@ void buffer_contents_free (HB_WASM_EXEC_ENV ptr_t(buffer_contents_t) contentsptr) { - if (unlikely (!validate_app_addr (contentsptr, sizeof (buffer_contents_t)))) + HB_STRUCT_TYPE (buffer_contents_t, contents); + if (unlikely (!contents)) return; - buffer_contents_t *contents = (buffer_contents_t *) addr_app_to_native (contentsptr); - module_free (contents->info); module_free (contents->pos); } diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index a005024c9..4fc95fd94 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -68,6 +68,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; (void *) hb_wasm_ref_type_##obj)) \ obj = hb_##obj##_get_empty (); \ } HB_STMT_END + #define HB_OBJ2REF(obj) \ uint32_t obj##ref = nullref; \ HB_STMT_START { \ @@ -90,5 +91,13 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; } \ type &name = *_name_ptr +#define HB_STRUCT_TYPE(type, name) \ + type *name = nullptr; \ + HB_STMT_START { \ + if (likely (wasm_runtime_validate_app_addr (module_inst, \ + name##ptr, sizeof (type)))) \ + name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \ + } HB_STMT_END + #endif /* HB_WASM_API_HH */ From af1f41a43ee7b7bb06e43e7000101019ae24f66a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:17:18 -0700 Subject: [PATCH 042/149] [wasm-api] Clear structs in _free() --- src/hb-wasm-api-blob.hh | 3 +++ src/hb-wasm-api-buffer.hh | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index e9aff8968..fbff0ff1f 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -40,6 +40,9 @@ blob_free (HB_WASM_EXEC_ENV return; module_free (blob->data); + + blob->data = nullref; + blob->length = 0; } diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 949195167..07d7ab418 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -43,6 +43,10 @@ buffer_contents_free (HB_WASM_EXEC_ENV module_free (contents->info); module_free (contents->pos); + + contents->info = nullref; + contents->pos = nullref; + contents->length = 0; } void From e0fec1dda01f1060dd474537dcbe5084ef46a88f Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 23 Feb 2023 22:18:22 +0000 Subject: [PATCH 043/149] [wasm-api] Wrap some of hb-font --- src/hb-wasm-api-font.hh | 31 +++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 5 ++++- src/hb-wasm-api.h | 11 +++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index e495e8acd..2b1921cc8 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -43,6 +43,37 @@ font_get_face (HB_WASM_EXEC_ENV return faceref; } +hb_codepoint_t +font_get_glyph (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + hb_codepoint_t unicode, + hb_codepoint_t variation_selector) +{ + HB_REF2OBJ (font); + hb_codepoint_t glyph; + + hb_font_get_glyph (font, unicode, variation_selector, &glyph); + return glyph; +} + + +hb_position_t +font_get_glyph_h_advance (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + hb_codepoint_t glyph) +{ + HB_REF2OBJ (font); + return hb_font_get_glyph_h_advance (font, glyph); +} + +hb_position_t +font_get_glyph_v_advance (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + hb_codepoint_t glyph) +{ + HB_REF2OBJ (font); + return hb_font_get_glyph_v_advance (font, glyph); +} }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 14a1f60ab..df39ad17b 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -62,7 +62,10 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(iii)", face_reference_table), /* font */ - NATIVE_SYMBOL ("(i)i", font_get_face), + NATIVE_SYMBOL ("(i)i", font_get_face), + NATIVE_SYMBOL ("(iii)i", font_get_glyph), + NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), + NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), /* debug */ #ifdef HB_DEBUG_WASM diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 1adb4e2c6..66d5b64c0 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -140,7 +140,18 @@ typedef struct font_t font_t; HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV ptr_t(font_t)); +HB_WASM_API (hb_codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV + ptr_t(font_t), + hb_codepoint_t unicode, + hb_codepoint_t variation_selector); +HB_WASM_API (hb_position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV + ptr_t(font_t), + hb_codepoint_t glyph); + +HB_WASM_API (hb_position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV + ptr_t(font_t), + hb_codepoint_t glyph); /* shape interface */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV From c1dc112121e8ce7e3c75fae0c9e3589176bbc2f8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:47:56 -0700 Subject: [PATCH 044/149] [wasm-api] Bind buffer_set_contents --- src/hb-buffer.hh | 7 +++++++ src/hb-wasm-api-buffer.hh | 34 +++++++++++++++++++++++++++++++--- src/hb-wasm-api-list.hh | 7 ++++--- src/hb-wasm-api.h | 4 ++++ src/wasm-sample/shape.c | 8 +++++++- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 5a43cabcb..d50641ef2 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -493,6 +493,13 @@ struct hb_buffer_t HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size); + HB_NODISCARD bool resize (unsigned length) + { + assert (!have_output); + if (unlikely (!ensure (length))) return false; + len = length; + return true; + } HB_NODISCARD bool ensure (unsigned int size) { return likely (!size || size < allocated) ? true : enlarge (size); } diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 07d7ab418..3164b7617 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -32,6 +32,8 @@ namespace hb { namespace wasm { +static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); +static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); void buffer_contents_free (HB_WASM_EXEC_ENV @@ -59,9 +61,6 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND if (buffer->have_output) buffer->sync (); - static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); - static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); - unsigned length = buffer->len; ret.length = length; ret.info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); @@ -69,6 +68,35 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND } +bool_t +buffer_set_contents (HB_WASM_EXEC_ENV + ptr_t(buffer_t) bufferref, + ptr_t(const buffer_contents_t) contentsptr) +{ + HB_REF2OBJ (buffer); + HB_STRUCT_TYPE (buffer_contents_t, contents); + if (unlikely (!contents)) + return false; + + unsigned length = contents->length; + unsigned bytes; + if (unlikely (hb_unsigned_mul_overflows (length, sizeof (buffer->info[0]), &bytes))) + return false; + + if (unlikely (!buffer->resize (length))) + return false; + + glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr); + glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr); + + buffer->clear_positions (); /* This is wasteful, but we need it to set have_positions=true. */ + memcpy (buffer->info, info, bytes); + memcpy (buffer->pos, pos, bytes); + buffer->len = length; + + return true; +} + }} #endif /* HB_WASM_API_BUFFER_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index df39ad17b..dc29b2f8f 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -57,15 +57,16 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* buffer */ NATIVE_SYMBOL ("(i)", buffer_contents_free), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), + NATIVE_SYMBOL ("(ii)i", buffer_set_contents), /* face */ NATIVE_SYMBOL ("(iii)", face_reference_table), /* font */ - NATIVE_SYMBOL ("(i)i", font_get_face), + NATIVE_SYMBOL ("(i)i", font_get_face), NATIVE_SYMBOL ("(iii)i", font_get_glyph), - NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), - NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), + NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), + NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), /* debug */ #ifdef HB_DEBUG_WASM diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 66d5b64c0..e5f6700d1 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -123,6 +123,10 @@ typedef struct buffer_t buffer_t; HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND ptr_t(buffer_t)); +HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV + ptr_t(buffer_t), + ptr_t(const buffer_contents_t)); + /* face */ diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index 80486b2b5..c558114c9 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -23,9 +23,15 @@ shape (font_t *font, buffer_t *buffer) debugprint1 ("buffer length", contents.length); for (unsigned i = 0; i < contents.length; i++) + { debugprint2 ("Codepoint", i, contents.info[i].codepoint); + contents.info[i].codepoint++; + contents.pos[i].x_advance = 256 * 64; + } + + bool_t ret = buffer_set_contents (buffer, &contents); buffer_contents_free (&contents); - return 1; + return ret; } From 926f8a326e42b557d2991bddf5e234289677c362 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 15:52:13 -0700 Subject: [PATCH 045/149] [wasm-sample] Actually shape text! --- src/wasm-sample/shape.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index c558114c9..69efaca93 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -25,8 +25,8 @@ shape (font_t *font, buffer_t *buffer) for (unsigned i = 0; i < contents.length; i++) { debugprint2 ("Codepoint", i, contents.info[i].codepoint); - contents.info[i].codepoint++; - contents.pos[i].x_advance = 256 * 64; + contents.info[i].codepoint = font_get_glyph (font, contents.info[i].codepoint, 0); + contents.pos[i].x_advance = font_get_glyph_h_advance (font, contents.info[i].codepoint); } bool_t ret = buffer_set_contents (buffer, &contents); From 551528a6e6bef10b3bfbc254c949d7247fba8176 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 16:06:39 -0700 Subject: [PATCH 046/149] [wasm-sample] Remove unused prototype --- src/wasm-sample/shape.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape.c index 69efaca93..1b2f4f44f 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape.c @@ -2,8 +2,6 @@ #include -void free (void*); - void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); From 5ab7f7a7d41720bb8ea189df0c5c47db1a22d550 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 23 Feb 2023 16:11:54 -0700 Subject: [PATCH 047/149] [wasm-sample] Make addTable take args --- src/wasm-sample/Makefile | 2 +- src/wasm-sample/addTable.py | 11 ++++++++--- src/wasm-sample/test.ttf | Bin 22116 -> 23468 bytes 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wasm-sample/Makefile b/src/wasm-sample/Makefile index 1f1704e6a..d072e1e50 100644 --- a/src/wasm-sample/Makefile +++ b/src/wasm-sample/Makefile @@ -12,7 +12,7 @@ all: test.wasm.ttf -o $@ %.wasm.ttf: %.ttf shape.so addTable.py - python addTable.py + python addTable.py $< $@ shape.so clean: $(RM) test.wasm.ttf shape.so diff --git a/src/wasm-sample/addTable.py b/src/wasm-sample/addTable.py index f8e38858e..103f292dd 100644 --- a/src/wasm-sample/addTable.py +++ b/src/wasm-sample/addTable.py @@ -1,11 +1,16 @@ +import sys from fontTools.ttLib import TTFont from fontTools.ttLib.tables.DefaultTable import DefaultTable -font = TTFont("test.ttf") +if len(sys.argv) == 1: + print("usage: python addTable.py input.ttf output.ttf Wasm.bin") + sys.exit(1) + +font = TTFont(sys.argv[1]) wasm_table = DefaultTable("Wasm") -wasm_table.data = open("shape.so", "rb").read() +wasm_table.data = open(sys.argv[3], "rb").read() font["Wasm"] = wasm_table -font.save("test.wasm.ttf") +font.save(sys.argv[2]) diff --git a/src/wasm-sample/test.ttf b/src/wasm-sample/test.ttf index 2ba04f611b660189a9f8023047a0e7c7e52985f3..2a5535f5f704ba31179af8dcd072c6f77364c43f 100644 GIT binary patch delta 1818 zcmZ`(U2GIp6h3!$_J7MRZGi$UJ*Qn@OWUO_u;r(~PN6M@{+IyjliS(d*$D#KV;||+dncYw!`lLhkWeaI`-J~;r62M9k!zY?#$TXqr;Kk zyw?GaTm}%|OdsAnU^q{8@%|de&pe@)^Jjj#`5eIG27rAqkymxN20AC_8>SM+4eY+` zd=0?)728KrdfLoC`{xV`$e(kP>uI&DgA1Bika6&(bMvXIuYbY`-7Nso4>QU9+$-l# zI{|FB89$rR)THxF#|7pNu^q{h|&hoIe-fDZ(e|PQQKe+o`Wj5U)Szf-l;m+JYKB$}& z>ngiMd*wapsp_2Pdr@hyovV1I$?6@)by0oGJ|QYkN=GXfY=2eXUr~)#&&$p15uI*{ z8ztSl2mZ>dVn=n-mvmR(b=}|tn?`zj(Yjts1lCaBmp$j4GHUW~u#qCk9o|V1MM#+)a^2O4;1om=9 z)iv;VF4@IJyO=UNPw)jKDSc!^32nznMmbn>qKpucdUz2LDTH!J>LJsP_CTHDV89WP zf))@Yr_w?$_PDa-MmKqsJ70Ws$w6N9pcB1;la`7V*+M?$;#o_w0UOX4IAsCMUB=vH zS@NTw0vHIquoxer<%)|M8Ewo`6PiubjLo<_aC$MoMJtqZwS+5iC9YUV2(*eRt0Iyt zMQu?_Sf&f^GIeo^P1uE-a@36>?9NgT_E;Hu z6`(L~#9j;sUb5JYxgs80Q+$uM_bVB!!23bid%7WM5;Bkhql$jBlYk>4&RQ0 zxIITZFoHX>v=et)I=gTi?h2f#DGXV$0oq-s>|x4Y+=F{_6vf>b&60u&4lM}CG|Vm9 zhr_rpOXhxV>wa#l54>KB9ikCFpHcRXW@!w^=m3u3fdKb}Uu2x!!r#I>MdbE zz>9nMMab6gO?)Q2@bJKkN6xymWKjEU4<=lGFkee%+rAKOT7QKB$A3oJ`M=Zbdek7h zA4n`2VNZQFos9zgu~^wq6USn;*VamIj{s-5R7|SFh6=mgI&WtaTB$62yiI83vX<

CZ_z!x&)MGya42zWdH@ZcCi8F7%PB$m5khyicQ&?D;XHL zfPBUaIr+(ncj8a`0QFA+%KyqutSDew!^{bkvjFlH@)E6bQ-xS`S%BW302CA{$S*GW zpUaQ}WP@C+P*9Xwuqf@ Date: Thu, 23 Feb 2023 21:36:17 -0700 Subject: [PATCH 048/149] [graphite] Remove script handling https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 --- src/hb-graphite2.cc | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index 9e068f8d8..541c1269f 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -263,16 +263,8 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, /* TODO ensure_native_direction. */ - hb_tag_t script_tag[HB_OT_MAX_TAGS_PER_SCRIPT]; - unsigned int count = HB_OT_MAX_TAGS_PER_SCRIPT; - hb_ot_tags_from_script_and_language (hb_buffer_get_script (buffer), - HB_LANGUAGE_INVALID, - &count, - script_tag, - nullptr, nullptr); - seg = gr_make_seg (nullptr, grface, - count ? script_tag[count - 1] : HB_OT_TAG_DEFAULT_SCRIPT, + HB_TAG_NONE, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 feats, gr_utf32, chars, buffer->len, 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0)); From 07ece174954a3345f05be3c23766119e6554dbf5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 08:08:18 -0700 Subject: [PATCH 049/149] [wasm-graphite] Initial code --- src/hb-wasm-api-face.hh | 2 +- src/hb-wasm-shape.cc | 2 +- src/wasm-graphite/Makefile | 22 ++++ src/wasm-graphite/addTable.py | 16 +++ src/wasm-graphite/shape.cc | 210 ++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 src/wasm-graphite/Makefile create mode 100644 src/wasm-graphite/addTable.py create mode 100644 src/wasm-graphite/shape.cc diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index bd7d0bb02..30d21aa21 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -44,8 +44,8 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND unsigned length; const char *data = hb_blob_get_data (blob, &length); - ret.data = wasm_runtime_module_dup_data (module_inst, data, length); ret.length = length; + ret.data = wasm_runtime_module_dup_data (module_inst, data, length); hb_blob_destroy (blob); } diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index ea31cce70..fd8612126 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -159,7 +159,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, { bool ret = true; const hb_wasm_face_data_t *face_data = font->face->data.wasm; - constexpr uint32_t stack_size = 8092, heap_size = 8092; + constexpr uint32_t stack_size = 8092, heap_size = 2 * 1024 * 1024; wasm_module_inst_t module_inst = nullptr; wasm_exec_env_t exec_env = nullptr; diff --git a/src/wasm-graphite/Makefile b/src/wasm-graphite/Makefile new file mode 100644 index 000000000..6997a1956 --- /dev/null +++ b/src/wasm-graphite/Makefile @@ -0,0 +1,22 @@ +all: test.wasm.ttf + +%.wasm: %.cc ../hb-wasm-api.h + emcc \ + -I .. \ + -I ~/graphite/include/ \ + -fvisibility=hidden \ + -Wl,--allow-undefined \ + -Wl,--no-entry \ + -sERROR_ON_UNDEFINED_SYMBOLS=0 \ + ~/graphite/src/libgraphite2.a \ + ~/wasm/wasi-sdk-19.0/share/wasi-sysroot/lib/wasm32-wasi/libc.a \ + $< \ + -o $@ + +%.wasm.ttf: %.ttf shape.wasm addTable.py + python addTable.py $< $@ shape.wasm + +clean: + $(RM) test.wasm.ttf shape.wasm + +.PRECIOUS: shape.wasm diff --git a/src/wasm-graphite/addTable.py b/src/wasm-graphite/addTable.py new file mode 100644 index 000000000..103f292dd --- /dev/null +++ b/src/wasm-graphite/addTable.py @@ -0,0 +1,16 @@ +import sys +from fontTools.ttLib import TTFont +from fontTools.ttLib.tables.DefaultTable import DefaultTable + +if len(sys.argv) == 1: + print("usage: python addTable.py input.ttf output.ttf Wasm.bin") + sys.exit(1) + +font = TTFont(sys.argv[1]) + +wasm_table = DefaultTable("Wasm") +wasm_table.data = open(sys.argv[3], "rb").read() + +font["Wasm"] = wasm_table + +font.save(sys.argv[2]) diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc new file mode 100644 index 000000000..546cfd5dc --- /dev/null +++ b/src/wasm-graphite/shape.cc @@ -0,0 +1,210 @@ +#define HB_WASM_INTERFACE(ret_t, name) __attribute__((export_name(#name))) ret_t name + +#include + +#include + +extern "C" { +void *malloc(size_t size); +void *realloc(void *ptr, size_t size); +void free(void *ptr); +void abort(); +void *memset(void *s, int c, size_t n); +} + +void debugprint1 (char *s, int32_t); +void debugprint2 (char *s, int32_t, int32_t); + +static const void *copy_table (const void *data, unsigned int tag, size_t *len) +{ + face_t *face = (face_t *) data; + blob_t blob = face_reference_table (face, tag); + + *len = blob.length; + return blob.data; +} + +static void free_table (const void *data, const void *table_data) +{ + free ((void *) table_data); +} + +bool_t +shape (font_t *font, buffer_t *buffer) +{ + face_t *face = font_get_face (font); + + blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); + + blob_free (&blob); + + buffer_contents_t contents = buffer_copy_contents (buffer); + + const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, nullptr};//&free_table}; + gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); + + gr_segment *seg = nullptr; + const gr_slot *is; + unsigned int ci = 0, ic = 0; + unsigned int curradvx = 0, curradvy = 0; + unsigned length = contents.length; + + uint32_t *chars = (uint32_t *) malloc (length * sizeof (uint32_t)); + for (unsigned int i = 0; i < contents.length; ++i) + chars[i] = contents.info[i].codepoint; + + /* TODO ensure_native_direction. */ + + seg = gr_make_seg (nullptr, grface, + 0, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 + nullptr, + gr_utf32, chars, contents.length, + 2/* | (buffer_get_direction (buffer) == DIRECTION_RTL ? 1 : 0)*/); + + free (chars); + + if (!seg) + return false; + + unsigned int glyph_count = gr_seg_n_slots (seg); + + struct cluster_t { + unsigned int base_char; + unsigned int num_chars; + unsigned int base_glyph; + unsigned int num_glyphs; + unsigned int cluster; + int advance; + }; + + length = glyph_count; + //contents.info = (glyph_info_t *) realloc (contents.info, length * sizeof (glyph_info_t)); + //contents.pos = (glyph_position_t *) realloc (contents.pos, length * sizeof (glyph_position_t)); + cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); + uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); + + memset (clusters, 0, sizeof (clusters[0]) * length); + hb_codepoint_t *pg = gids; + clusters[0].cluster = contents.info[0].cluster; + unsigned int upem = 2048;//hb_face_get_upem (face); + float xscale = 10;//(float) font->x_scale / upem; + float yscale = 10;//(float) font->y_scale / upem; + yscale *= yscale / xscale; + unsigned int curradv = 0; + if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + { + curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale; + clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv; + } + else + clusters[0].advance = 0; + for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++) + { + unsigned int before = gr_slot_before (is); + unsigned int after = gr_slot_after (is); + *pg = gr_slot_gid (is); + pg++; + while (clusters[ci].base_char > before && ci) + { + clusters[ci-1].num_chars += clusters[ci].num_chars; + clusters[ci-1].num_glyphs += clusters[ci].num_glyphs; + clusters[ci-1].advance += clusters[ci].advance; + ci--; + } + + if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars) + { + cluster_t *c = clusters + ci + 1; + c->base_char = clusters[ci].base_char + clusters[ci].num_chars; + c->cluster = contents.info[c->base_char].cluster; + c->num_chars = before - c->base_char; + c->base_glyph = ic; + c->num_glyphs = 0; + if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + { + c->advance = curradv - gr_slot_origin_X(is) * xscale; + curradv -= c->advance; + } + else + { + c->advance = 0; + clusters[ci].advance += gr_slot_origin_X(is) * xscale - curradv; + curradv += clusters[ci].advance; + } + ci++; + } + clusters[ci].num_glyphs++; + + if (clusters[ci].base_char + clusters[ci].num_chars < after + 1) + clusters[ci].num_chars = after + 1 - clusters[ci].base_char; + } + + if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + clusters[ci].advance += curradv; + else + clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv; + ci++; + + for (unsigned int i = 0; i < ci; ++i) + { + for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j) + { + glyph_info_t *info = &contents.info[clusters[i].base_glyph + j]; + info->codepoint = gids[clusters[i].base_glyph + j]; + info->cluster = clusters[i].cluster; + info->var1 = clusters[i].advance; // all glyphs in the cluster get the same advance + } + } + contents.length = glyph_count; + + /* Positioning. */ + unsigned int currclus = 0xFFFFFFFF; + const glyph_info_t *info = contents.info; + glyph_position_t *pPos = contents.pos; + if (1)//!HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + { + curradvx = 0; + for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is)) + { + pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx; + pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; + if (info->cluster != currclus) { + pPos->x_advance = info->var1; + curradvx += pPos->x_advance; + currclus = info->cluster; + } else + pPos->x_advance = 0.; + + pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale; + curradvy += pPos->y_advance; + } + } + else + { + curradvx = gr_seg_advance_X(seg) * xscale; + for (is = gr_seg_first_slot (seg); is; pPos++, info++, is = gr_slot_next_in_segment (is)) + { + if (info->cluster != currclus) + { + pPos->x_advance = info->var1; + curradvx -= pPos->x_advance; + currclus = info->cluster; + } else + pPos->x_advance = 0.; + + pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale; + curradvy -= pPos->y_advance; + pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1 - curradvx + pPos->x_advance; + pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; + } + //hb_buffer_reverse_clusters (buffer); + } + + gr_seg_destroy (seg); + + buffer_set_contents (buffer, &contents); + + bool ret = glyph_count; + + return ret; +} From 2d295183b8ed47fa3efa4b83aff1d7781d97ab4f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 09:20:42 -0700 Subject: [PATCH 050/149] [wasm-api] Add buffer_contents_realloc --- src/hb-wasm-api-buffer.hh | 33 ++++++++++++++++++++++++++++++++- src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 4 ++++ src/wasm-graphite/shape.cc | 3 +-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 3164b7617..a5560bc25 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -51,6 +51,38 @@ buffer_contents_free (HB_WASM_EXEC_ENV contents->length = 0; } +void + buffer_contents_realloc (HB_WASM_EXEC_ENV + ptr_t(buffer_contents_t) contentsptr, + uint32_t size) +{ + HB_STRUCT_TYPE (buffer_contents_t, contents); + if (unlikely (!contents)) + return; + + if (size <= contents->length) + return; + + unsigned bytes; + if (hb_unsigned_mul_overflows (size, sizeof (glyph_info_t), &bytes)) + return; + + // TODO bounds check? + uint32_t infoptr = contents->info; + uint32_t posptr = contents->pos; + const char *info = (const char *) addr_app_to_native (infoptr); + const char *pos = (const char *) addr_app_to_native (posptr); + + contents->info = wasm_runtime_module_dup_data (module_inst, info, bytes); + contents->pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); + + module_free (infoptr); + module_free (posptr); + + // TODO Check success + contents->length = size; +} + void buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND ptr_t(buffer_t) bufferref) @@ -67,7 +99,6 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); } - bool_t buffer_set_contents (HB_WASM_EXEC_ENV ptr_t(buffer_t) bufferref, diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index dc29b2f8f..cb60ee76a 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -56,6 +56,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* buffer */ NATIVE_SYMBOL ("(i)", buffer_contents_free), + NATIVE_SYMBOL ("(ii)", buffer_contents_realloc), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index e5f6700d1..405189ed3 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -115,6 +115,10 @@ typedef struct ptr_t(glyph_position_t) pos; } buffer_contents_t; +HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_t(buffer_contents_t), + uint32_t size); + HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV ptr_t(buffer_contents_t)); diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 546cfd5dc..963a00651 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -78,8 +78,7 @@ shape (font_t *font, buffer_t *buffer) }; length = glyph_count; - //contents.info = (glyph_info_t *) realloc (contents.info, length * sizeof (glyph_info_t)); - //contents.pos = (glyph_position_t *) realloc (contents.pos, length * sizeof (glyph_position_t)); + buffer_contents_realloc (&contents, length); cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); From 149199ee26cfac1ff211b2283f8c79b1c7b8cdd0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 09:39:25 -0700 Subject: [PATCH 051/149] [wasm-api] Bind face_get_upem --- src/hb-wasm-api-face.hh | 9 +++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 5 +++++ src/wasm-graphite/shape.cc | 4 ++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 30d21aa21..cdbc08d6d 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -50,6 +50,15 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND hb_blob_destroy (blob); } +unsigned +face_get_upem (HB_WASM_EXEC_ENV + ptr_t(face_t) faceref) +{ + HB_REF2OBJ (face); + + return hb_face_get_upem (face); +} + }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index cb60ee76a..7a833d900 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -62,6 +62,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* face */ NATIVE_SYMBOL ("(iii)", face_reference_table), + NATIVE_SYMBOL ("(i)i", face_get_upem), /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 405189ed3..f7162fbe7 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -140,6 +140,9 @@ HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND ptr_t(face_t), tag_t table_tag); +HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV + ptr_t(face_t)); + /* font */ @@ -160,6 +163,8 @@ HB_WASM_API (hb_position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV HB_WASM_API (hb_position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV ptr_t(font_t), hb_codepoint_t glyph); + + /* shape interface */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 963a00651..a4a5e8302 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -85,7 +85,7 @@ shape (font_t *font, buffer_t *buffer) memset (clusters, 0, sizeof (clusters[0]) * length); hb_codepoint_t *pg = gids; clusters[0].cluster = contents.info[0].cluster; - unsigned int upem = 2048;//hb_face_get_upem (face); + unsigned int upem = face_get_upem (face); float xscale = 10;//(float) font->x_scale / upem; float yscale = 10;//(float) font->y_scale / upem; yscale *= yscale / xscale; @@ -196,7 +196,7 @@ shape (font_t *font, buffer_t *buffer) pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1 - curradvx + pPos->x_advance; pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; } - //hb_buffer_reverse_clusters (buffer); + //buffer_reverse_clusters (buffer); } gr_seg_destroy (seg); From 23b58b5667aa6ffe2780860e7a9202149e05eac3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 09:50:34 -0700 Subject: [PATCH 052/149] [wasm-api] Bind font_get_scale --- src/hb-wasm-api-buffer.hh | 2 +- src/hb-wasm-api-face.hh | 2 +- src/hb-wasm-api-font.hh | 15 ++++++++++++++- src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 5 +++++ src/hb-wasm-api.hh | 2 +- src/wasm-graphite/shape.cc | 6 ++++-- 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index a5560bc25..39c65c5cf 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -87,7 +87,7 @@ void buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND ptr_t(buffer_t) bufferref) { - HB_RETURN_TYPE (buffer_contents_t, ret); + HB_RETURN_STRUCT (buffer_contents_t, ret); HB_REF2OBJ (buffer); if (buffer->have_output) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index cdbc08d6d..918a08682 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -36,7 +36,7 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND ptr_t(face_t) faceref, tag_t table_tag) { - HB_RETURN_TYPE (blob_t, ret); + HB_RETURN_STRUCT (blob_t, ret); HB_REF2OBJ (face); hb_blob_t *blob = hb_face_reference_table (face, table_tag); diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 2b1921cc8..ba80d6609 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -43,6 +43,20 @@ font_get_face (HB_WASM_EXEC_ENV return faceref; } +void +font_get_scale (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + ptr_t(int32_t) x_scaleptr, + ptr_t(int32_t) y_scaleptr) +{ + HB_REF2OBJ (font); + + HB_STRUCT_TYPE(int32_t, x_scale); + HB_STRUCT_TYPE(int32_t, y_scale); + + hb_font_get_scale (font, x_scale, y_scale); +} + hb_codepoint_t font_get_glyph (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, @@ -56,7 +70,6 @@ font_get_glyph (HB_WASM_EXEC_ENV return glyph; } - hb_position_t font_get_glyph_h_advance (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 7a833d900..5f834dddb 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -66,6 +66,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), + NATIVE_SYMBOL ("(iii)", font_get_scale), NATIVE_SYMBOL ("(iii)i", font_get_glyph), NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index f7162fbe7..10bd92a74 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -151,6 +151,11 @@ typedef struct font_t font_t; HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV ptr_t(font_t)); +HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV + ptr_t(font_t), + ptr_t(int32_t) x_scale, + ptr_t(int32_t) y_scale); + HB_WASM_API (hb_codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV ptr_t(font_t), hb_codepoint_t unicode, diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 4fc95fd94..646f50aad 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -78,7 +78,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref); \ } HB_STMT_END -#define HB_RETURN_TYPE(type, name) \ +#define HB_RETURN_STRUCT(type, name) \ type *_name_ptr = nullptr; \ { \ if (likely (wasm_runtime_validate_app_addr (module_inst, \ diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index a4a5e8302..08e564d18 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -86,8 +86,10 @@ shape (font_t *font, buffer_t *buffer) hb_codepoint_t *pg = gids; clusters[0].cluster = contents.info[0].cluster; unsigned int upem = face_get_upem (face); - float xscale = 10;//(float) font->x_scale / upem; - float yscale = 10;//(float) font->y_scale / upem; + int32_t font_x_scale, font_y_scale; + font_get_scale (font, &font_x_scale, &font_y_scale); + float xscale = (float) font_x_scale / upem; + float yscale = (float) font_y_scale / upem; yscale *= yscale / xscale; unsigned int curradv = 0; if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) From a5c844a1de2a6f5c0acda723d4367c41d7a7b90c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 09:52:38 -0700 Subject: [PATCH 053/149] [wasm] Rename macro --- src/hb-wasm-api-blob.hh | 2 +- src/hb-wasm-api-buffer.hh | 6 +++--- src/hb-wasm-api-font.hh | 4 ++-- src/hb-wasm-api.hh | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index fbff0ff1f..e3997d168 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -35,7 +35,7 @@ void blob_free (HB_WASM_EXEC_ENV ptr_t(blob_t) blobptr) { - HB_STRUCT_TYPE (blob_t, blob); + HB_OUT_PARAM (blob_t, blob); if (unlikely (!blob)) return; diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 39c65c5cf..d4f517421 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -39,7 +39,7 @@ void buffer_contents_free (HB_WASM_EXEC_ENV ptr_t(buffer_contents_t) contentsptr) { - HB_STRUCT_TYPE (buffer_contents_t, contents); + HB_OUT_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return; @@ -56,7 +56,7 @@ void ptr_t(buffer_contents_t) contentsptr, uint32_t size) { - HB_STRUCT_TYPE (buffer_contents_t, contents); + HB_OUT_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return; @@ -105,7 +105,7 @@ buffer_set_contents (HB_WASM_EXEC_ENV ptr_t(const buffer_contents_t) contentsptr) { HB_REF2OBJ (buffer); - HB_STRUCT_TYPE (buffer_contents_t, contents); + HB_OUT_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return false; diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index ba80d6609..b1bc1fbd9 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -51,8 +51,8 @@ font_get_scale (HB_WASM_EXEC_ENV { HB_REF2OBJ (font); - HB_STRUCT_TYPE(int32_t, x_scale); - HB_STRUCT_TYPE(int32_t, y_scale); + HB_OUT_PARAM(int32_t, x_scale); + HB_OUT_PARAM(int32_t, y_scale); hb_font_get_scale (font, x_scale, y_scale); } diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 646f50aad..19570698e 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -91,7 +91,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; } \ type &name = *_name_ptr -#define HB_STRUCT_TYPE(type, name) \ +#define HB_OUT_PARAM(type, name) \ type *name = nullptr; \ HB_STMT_START { \ if (likely (wasm_runtime_validate_app_addr (module_inst, \ From 0a51ed31b0bba05727210f27548ebd3b55052fe3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 10:07:59 -0700 Subject: [PATCH 054/149] [wasm-api] Bind buffer_get_direction --- src/hb-wasm-api-buffer.hh | 10 ++++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 17 +++++++++++++++++ src/wasm-graphite/shape.cc | 11 ++++++----- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index d4f517421..bb78cd924 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -128,6 +128,16 @@ buffer_set_contents (HB_WASM_EXEC_ENV return true; } +direction_t +buffer_get_direction (HB_WASM_EXEC_ENV + ptr_t(buffer_t) bufferref) +{ + HB_REF2OBJ (buffer); + + return (direction_t) hb_buffer_get_direction (buffer); +} + + }} #endif /* HB_WASM_API_BUFFER_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 5f834dddb..53faca01e 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -59,6 +59,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)", buffer_contents_realloc), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), + NATIVE_SYMBOL ("(i)i", buffer_get_direction), /* face */ NATIVE_SYMBOL ("(iii)", face_reference_table), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 10bd92a74..a0ab24058 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -75,6 +75,20 @@ typedef uint32_t hb_mask_t; typedef uint32_t tag_t; #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))) +typedef enum { + DIRECTION_INVALID = 0, + DIRECTION_LTR = 4, + DIRECTION_RTL, + DIRECTION_TTB, + DIRECTION_BTT +} direction_t; +#define DIRECTION_IS_VALID(dir) ((((unsigned int) (dir)) & ~3U) == 4) +#define DIRECTION_IS_HORIZONTAL(dir) ((((unsigned int) (dir)) & ~1U) == 4) +#define DIRECTION_IS_VERTICAL(dir) ((((unsigned int) (dir)) & ~1U) == 6) +#define DIRECTION_IS_FORWARD(dir) ((((unsigned int) (dir)) & ~2U) == 4) +#define DIRECTION_IS_BACKWARD(dir) ((((unsigned int) (dir)) & ~2U) == 5) +#define DIRECTION_REVERSE(dir) ((direction_t) (((unsigned int) (dir)) ^ 1)) + /* blob */ @@ -131,6 +145,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV ptr_t(buffer_t), ptr_t(const buffer_contents_t)); +HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV + ptr_t(buffer_t)); + /* face */ diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 08e564d18..b8e848a66 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -39,6 +39,7 @@ shape (font_t *font, buffer_t *buffer) blob_free (&blob); buffer_contents_t contents = buffer_copy_contents (buffer); + direction_t direction = buffer_get_direction (buffer); const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, nullptr};//&free_table}; gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); @@ -59,7 +60,7 @@ shape (font_t *font, buffer_t *buffer) 0, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 nullptr, gr_utf32, chars, contents.length, - 2/* | (buffer_get_direction (buffer) == DIRECTION_RTL ? 1 : 0)*/); + 2 | (direction == DIRECTION_RTL ? 1 : 0)); free (chars); @@ -92,7 +93,7 @@ shape (font_t *font, buffer_t *buffer) float yscale = (float) font_y_scale / upem; yscale *= yscale / xscale; unsigned int curradv = 0; - if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (DIRECTION_IS_BACKWARD (direction)) { curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale; clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv; @@ -121,7 +122,7 @@ shape (font_t *font, buffer_t *buffer) c->num_chars = before - c->base_char; c->base_glyph = ic; c->num_glyphs = 0; - if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (DIRECTION_IS_BACKWARD (direction)) { c->advance = curradv - gr_slot_origin_X(is) * xscale; curradv -= c->advance; @@ -140,7 +141,7 @@ shape (font_t *font, buffer_t *buffer) clusters[ci].num_chars = after + 1 - clusters[ci].base_char; } - if (0)//HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (DIRECTION_IS_BACKWARD (direction)) clusters[ci].advance += curradv; else clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv; @@ -162,7 +163,7 @@ shape (font_t *font, buffer_t *buffer) unsigned int currclus = 0xFFFFFFFF; const glyph_info_t *info = contents.info; glyph_position_t *pPos = contents.pos; - if (1)//!HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (!DIRECTION_IS_BACKWARD (direction)) { curradvx = 0; for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is)) From a08dbf41cd20bf36828b58a4e4b448671ee036c1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 10:13:21 -0700 Subject: [PATCH 055/149] [wasm-api] Bind buffer_reverse_clusters --- src/hb-wasm-api-buffer.hh | 8 ++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 2 ++ src/wasm-graphite/Makefile | 2 +- src/wasm-graphite/shape.cc | 6 +++--- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index bb78cd924..1f66f49e1 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -137,6 +137,14 @@ buffer_get_direction (HB_WASM_EXEC_ENV return (direction_t) hb_buffer_get_direction (buffer); } +void +buffer_reverse_clusters (HB_WASM_EXEC_ENV + ptr_t(buffer_t) bufferref) +{ + HB_REF2OBJ (buffer); + + hb_buffer_reverse_clusters (buffer); +} }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 53faca01e..e857e4d9e 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -60,6 +60,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), + NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), /* face */ NATIVE_SYMBOL ("(iii)", face_reference_table), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index a0ab24058..3f3b035d3 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -148,6 +148,8 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV ptr_t(buffer_t)); +HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV + ptr_t(buffer_t)); /* face */ diff --git a/src/wasm-graphite/Makefile b/src/wasm-graphite/Makefile index 6997a1956..7b486ea6b 100644 --- a/src/wasm-graphite/Makefile +++ b/src/wasm-graphite/Makefile @@ -1,4 +1,4 @@ -all: test.wasm.ttf +all: CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf %.wasm: %.cc ../hb-wasm-api.h emcc \ diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index b8e848a66..d537e34d8 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -180,6 +180,7 @@ shape (font_t *font, buffer_t *buffer) pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale; curradvy += pPos->y_advance; } + buffer_set_contents (buffer, &contents); } else { @@ -199,13 +200,12 @@ shape (font_t *font, buffer_t *buffer) pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1 - curradvx + pPos->x_advance; pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; } - //buffer_reverse_clusters (buffer); + buffer_set_contents (buffer, &contents); + buffer_reverse_clusters (buffer); } gr_seg_destroy (seg); - buffer_set_contents (buffer, &contents); - bool ret = glyph_count; return ret; From 2bde2f66f1391ca4d1c0e38142a6ea0f175173ab Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 11:03:13 -0700 Subject: [PATCH 056/149] [wasm-api] Bind shape_with --- src/hb-wasm-api-list.hh | 3 ++ src/hb-wasm-api-shape.hh | 50 +++++++++++++++++++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 8 +++ src/meson.build | 1 + src/wasm-sample/Makefile | 15 +++--- .../{shape.c => shape-fallback.cc} | 6 ++- src/wasm-sample/shape-ot.cc | 14 ++++++ 8 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/hb-wasm-api-shape.hh rename src/wasm-sample/{shape.c => shape-fallback.cc} (88%) create mode 100644 src/wasm-sample/shape-ot.cc diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index e857e4d9e..5cd0ea6ea 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -73,6 +73,9 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), + /* shape */ + NATIVE_SYMBOL ("(ii$)i", shape_with), + /* debug */ #ifdef HB_DEBUG_WASM NATIVE_SYMBOL ("($)", debugprint), diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh new file mode 100644 index 000000000..625ff9fca --- /dev/null +++ b/src/hb-wasm-api-shape.hh @@ -0,0 +1,50 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_SHAPE_HH +#define HB_WASM_API_SHAPE_HH + +#include "hb-wasm-api.hh" + +namespace hb { +namespace wasm { + + +bool_t +shape_with (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + ptr_t(buffer_t) bufferref, + const char *shaper) +{ + HB_REF2OBJ (font); + HB_REF2OBJ (buffer); + + const char * shaper_list[] = {shaper, nullptr}; + return hb_shape_full (font, buffer, nullptr, 0, shaper_list); +} + + +}} + +#endif /* HB_WASM_API_SHAPE_HH */ diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index f09b3831d..bc3babd56 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -31,6 +31,7 @@ #include "hb-wasm-api-buffer.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" +#include "hb-wasm-api-shape.hh" #undef module_inst diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 3f3b035d3..946e1aa92 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -189,6 +189,14 @@ HB_WASM_API (hb_position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV hb_codepoint_t glyph); +/* shape */ + +HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV + ptr_t(font_t), + ptr_t(buffer_t), + const char *shaper); + + /* shape interface */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV diff --git a/src/meson.build b/src/meson.build index 32076c278..74998ce42 100644 --- a/src/meson.build +++ b/src/meson.build @@ -331,6 +331,7 @@ hb_wasm_sources = files( 'hb-wasm-api-buffer.hh', 'hb-wasm-api-face.hh', 'hb-wasm-api-font.hh', + 'hb-wasm-api-shape.hh', 'hb-wasm-shape.cc', ) hb_wasm_headers = files() diff --git a/src/wasm-sample/Makefile b/src/wasm-sample/Makefile index d072e1e50..60606e359 100644 --- a/src/wasm-sample/Makefile +++ b/src/wasm-sample/Makefile @@ -1,6 +1,6 @@ -all: test.wasm.ttf +all: test-fallback.wasm.ttf test-ot.wasm.ttf -%.so: %.c ../hb-wasm-api.h +%.wasm: %.cc ../hb-wasm-api.h clang \ --target=wasm32-unknown-wasi \ -Wl,--no-entry \ @@ -11,10 +11,13 @@ all: test.wasm.ttf $< \ -o $@ -%.wasm.ttf: %.ttf shape.so addTable.py - python addTable.py $< $@ shape.so +test-fallback.wasm.ttf: test.ttf shape-fallback.wasm addTable.py + python addTable.py $< $@ shape-fallback.wasm + +test-ot.wasm.ttf: test.ttf shape-ot.wasm addTable.py + python addTable.py $< $@ shape-ot.wasm clean: - $(RM) test.wasm.ttf shape.so + $(RM) test-fallback.wasm.ttf test-ot.wasm.ttf shape-fallback.wasm shape-ot.wasm -.PRECIOUS: shape.so +.PRECIOUS: *.wasm diff --git a/src/wasm-sample/shape.c b/src/wasm-sample/shape-fallback.cc similarity index 88% rename from src/wasm-sample/shape.c rename to src/wasm-sample/shape-fallback.cc index 1b2f4f44f..344ab0e8d 100644 --- a/src/wasm-sample/shape.c +++ b/src/wasm-sample/shape-fallback.cc @@ -2,8 +2,10 @@ #include -void debugprint1 (char *s, int32_t); -void debugprint2 (char *s, int32_t, int32_t); +extern "C" { +void debugprint1 (const char *s, int32_t); +void debugprint2 (const char *s, int32_t, int32_t); +} bool_t shape (font_t *font, buffer_t *buffer) diff --git a/src/wasm-sample/shape-ot.cc b/src/wasm-sample/shape-ot.cc new file mode 100644 index 000000000..10e1e723d --- /dev/null +++ b/src/wasm-sample/shape-ot.cc @@ -0,0 +1,14 @@ +#define HB_WASM_INTERFACE(ret_t, name) __attribute__((export_name(#name))) ret_t name + +#include + +extern "C" { +void debugprint1 (const char *s, int32_t); +void debugprint2 (const char *s, int32_t, int32_t); +} + +bool_t +shape (font_t *font, buffer_t *buffer) +{ + return shape_with (font, buffer, "ot"); +} From 5738851b1ca0471b4ac270b89549466202fddf10 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 11:05:59 -0700 Subject: [PATCH 057/149] [wasm-api] Disallow "wasm" shaper in shape_with --- src/hb-wasm-api-shape.hh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index 625ff9fca..952aeb229 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -40,6 +40,9 @@ shape_with (HB_WASM_EXEC_ENV HB_REF2OBJ (font); HB_REF2OBJ (buffer); + if (0 == strcmp (shaper, "wasm")) + return false; + const char * shaper_list[] = {shaper, nullptr}; return hb_shape_full (font, buffer, nullptr, 0, shaper_list); } From d7f76f30b0dcfda3f4322b3e240be30b4a141cc7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 11:31:04 -0700 Subject: [PATCH 058/149] [wasm-graphite] Memory cleanup --- src/wasm-graphite/shape.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index d537e34d8..9f171d6c2 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -26,7 +26,10 @@ static const void *copy_table (const void *data, unsigned int tag, size_t *len) static void free_table (const void *data, const void *table_data) { - free ((void *) table_data); + blob_t blob; + blob.length = 0; // Doesn't matter + blob.data = (char *) table_data; + blob_free (&blob); } bool_t @@ -41,7 +44,7 @@ shape (font_t *font, buffer_t *buffer) buffer_contents_t contents = buffer_copy_contents (buffer); direction_t direction = buffer_get_direction (buffer); - const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, nullptr};//&free_table}; + const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, &free_table}; gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); gr_segment *seg = nullptr; @@ -205,6 +208,8 @@ shape (font_t *font, buffer_t *buffer) } gr_seg_destroy (seg); + free (clusters); + free (gids); bool ret = glyph_count; From 7537d48f081f25e3bd78dcacf9218535a1e40c2f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 11:47:17 -0700 Subject: [PATCH 059/149] [wasm] Remove a few lingering hb_ --- src/hb-wasm-api-font.hh | 12 ++++++------ src/hb-wasm-api.h | 32 ++++++++++++++++---------------- src/wasm-graphite/shape.cc | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index b1bc1fbd9..2956109c9 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -57,14 +57,14 @@ font_get_scale (HB_WASM_EXEC_ENV hb_font_get_scale (font, x_scale, y_scale); } -hb_codepoint_t +codepoint_t font_get_glyph (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, - hb_codepoint_t unicode, - hb_codepoint_t variation_selector) + codepoint_t unicode, + codepoint_t variation_selector) { HB_REF2OBJ (font); - hb_codepoint_t glyph; + codepoint_t glyph; hb_font_get_glyph (font, unicode, variation_selector, &glyph); return glyph; @@ -73,7 +73,7 @@ font_get_glyph (HB_WASM_EXEC_ENV hb_position_t font_get_glyph_h_advance (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, - hb_codepoint_t glyph) + codepoint_t glyph) { HB_REF2OBJ (font); return hb_font_get_glyph_h_advance (font, glyph); @@ -82,7 +82,7 @@ font_get_glyph_h_advance (HB_WASM_EXEC_ENV hb_position_t font_get_glyph_v_advance (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, - hb_codepoint_t glyph) + codepoint_t glyph) { HB_REF2OBJ (font); return hb_font_get_glyph_v_advance (font, glyph); diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 946e1aa92..45d881cd0 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -69,9 +69,9 @@ HB_WASM_BEGIN_DECLS #define ptr_t(type_t) type_t * #endif -typedef uint32_t hb_codepoint_t; -typedef int32_t hb_position_t; -typedef uint32_t hb_mask_t; +typedef uint32_t codepoint_t; +typedef int32_t position_t; +typedef uint32_t mask_t; typedef uint32_t tag_t; #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))) @@ -115,10 +115,10 @@ typedef struct typedef struct { - hb_position_t x_advance; - hb_position_t y_advance; - hb_position_t x_offset; - hb_position_t y_offset; + position_t x_advance; + position_t y_advance; + position_t x_offset; + position_t y_offset; uint32_t var; } glyph_position_t; @@ -175,18 +175,18 @@ HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV ptr_t(int32_t) x_scale, ptr_t(int32_t) y_scale); -HB_WASM_API (hb_codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV +HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV ptr_t(font_t), - hb_codepoint_t unicode, - hb_codepoint_t variation_selector); + codepoint_t unicode, + codepoint_t variation_selector); -HB_WASM_API (hb_position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV - ptr_t(font_t), - hb_codepoint_t glyph); +HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV + ptr_t(font_t), + codepoint_t glyph); -HB_WASM_API (hb_position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV - ptr_t(font_t), - hb_codepoint_t glyph); +HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV + ptr_t(font_t), + codepoint_t glyph); /* shape */ diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 9f171d6c2..b4da4c463 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -87,7 +87,7 @@ shape (font_t *font, buffer_t *buffer) uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); memset (clusters, 0, sizeof (clusters[0]) * length); - hb_codepoint_t *pg = gids; + codepoint_t *pg = gids; clusters[0].cluster = contents.info[0].cluster; unsigned int upem = face_get_upem (face); int32_t font_x_scale, font_y_scale; From 1acff90b0337d9a0158c4b8afddaf485a8037b67 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 11:53:47 -0700 Subject: [PATCH 060/149] [wasm-api] Bind font_glyph_to_string --- src/hb-wasm-api-font.hh | 12 ++++++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 5 +++++ src/wasm-graphite/shape.cc | 3 +++ src/wasm-sample/shape-fallback.cc | 8 +++++++- 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 2956109c9..319814cea 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -88,6 +88,18 @@ font_get_glyph_v_advance (HB_WASM_EXEC_ENV return hb_font_get_glyph_v_advance (font, glyph); } +void +font_glyph_to_string (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + codepoint_t glyph, + char *s, uint32_t size) +{ + HB_REF2OBJ (font); + + hb_font_glyph_to_string (font, glyph, s, size); +} + + }} #endif /* HB_WASM_API_FONT_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 5cd0ea6ea..25f02f908 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -72,6 +72,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(iii)i", font_get_glyph), NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), + NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), /* shape */ NATIVE_SYMBOL ("(ii$)i", shape_with), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 45d881cd0..758f5aa86 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -188,6 +188,11 @@ HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV ptr_t(font_t), codepoint_t glyph); +HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV + ptr_t(font_t), + codepoint_t glyph, + char *s, uint32_t size); + /* shape */ diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index b4da4c463..fcf87c00e 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -15,6 +15,9 @@ void *memset(void *s, int c, size_t n); void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); +__attribute__((visibility("default"))) +uint32_t heap_size = 2 * 1024 * 1024; + static const void *copy_table (const void *data, unsigned int tag, size_t *len) { face_t *face = (face_t *) data; diff --git a/src/wasm-sample/shape-fallback.cc b/src/wasm-sample/shape-fallback.cc index 344ab0e8d..11853c402 100644 --- a/src/wasm-sample/shape-fallback.cc +++ b/src/wasm-sample/shape-fallback.cc @@ -3,6 +3,7 @@ #include extern "C" { +void debugprint (const char *s); void debugprint1 (const char *s, int32_t); void debugprint2 (const char *s, int32_t, int32_t); } @@ -24,7 +25,12 @@ shape (font_t *font, buffer_t *buffer) for (unsigned i = 0; i < contents.length; i++) { - debugprint2 ("Codepoint", i, contents.info[i].codepoint); + char name[64]; + + font_glyph_to_string (font, contents.info[i].codepoint, name, sizeof (name)); + + debugprint (name); + contents.info[i].codepoint = font_get_glyph (font, contents.info[i].codepoint, 0); contents.pos[i].x_advance = font_get_glyph_h_advance (font, contents.info[i].codepoint); } From 863ec70e1240379875fe72baeada79c0d9f79fe4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 12:00:29 -0700 Subject: [PATCH 061/149] [wasm-shape] Add TODO items --- src/hb-wasm-shape.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index fd8612126..781a8d6da 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -161,6 +161,8 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, const hb_wasm_face_data_t *face_data = font->face->data.wasm; constexpr uint32_t stack_size = 8092, heap_size = 2 * 1024 * 1024; + /* TODO ensure_native_direction. */ + wasm_module_inst_t module_inst = nullptr; wasm_exec_env_t exec_env = nullptr; wasm_function_inst_t shape_func = nullptr; @@ -215,6 +217,9 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, goto fail; } + /* TODO Regularize clusters accordint to direction & cluster level, + * such that client doesn't crash with unmet expectations. */ + if (!results[0].of.i32) { fail: From b3b6e8da86b8bda05311fe9524ac169040ba9cf3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 12:03:53 -0700 Subject: [PATCH 062/149] [wasm-api] Bind buffer_reverse --- src/hb-wasm-api-buffer.hh | 9 +++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 3 +++ 3 files changed, 13 insertions(+) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 1f66f49e1..1e6edd57e 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -137,6 +137,15 @@ buffer_get_direction (HB_WASM_EXEC_ENV return (direction_t) hb_buffer_get_direction (buffer); } +void +buffer_reverse (HB_WASM_EXEC_ENV + ptr_t(buffer_t) bufferref) +{ + HB_REF2OBJ (buffer); + + hb_buffer_reverse (buffer); +} + void buffer_reverse_clusters (HB_WASM_EXEC_ENV ptr_t(buffer_t) bufferref) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 25f02f908..f3cb29f76 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -60,6 +60,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), + NATIVE_SYMBOL ("(i)", buffer_reverse), NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), /* face */ diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 758f5aa86..2180b882a 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -148,6 +148,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV ptr_t(buffer_t)); +HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV + ptr_t(buffer_t)); + HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV ptr_t(buffer_t)); From fe557e2f21108181496b8f23c92595e0d24d7d20 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 12:20:31 -0700 Subject: [PATCH 063/149] [wasm-api] Bind font_get_glyph_extents Untested. --- src/hb-wasm-api-font.hh | 17 +++++++++++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 319814cea..b4b0548dd 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -88,6 +88,23 @@ font_get_glyph_v_advance (HB_WASM_EXEC_ENV return hb_font_get_glyph_v_advance (font, glyph); } +static_assert (sizeof (glyph_extents_t) == sizeof (hb_glyph_extents_t), ""); + +bool_t +font_get_glyph_extents (HB_WASM_EXEC_ENV + ptr_t(font_t) fontref, + codepoint_t glyph, + ptr_t(glyph_extents_t) extentsptr) +{ + HB_REF2OBJ (font); + HB_OUT_PARAM (glyph_extents_t, extents); + if (unlikely (!extents)) + return false; + + return hb_font_get_glyph_extents (font, glyph, + (hb_glyph_extents_t *) extents); +} + void font_glyph_to_string (HB_WASM_EXEC_ENV ptr_t(font_t) fontref, diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index f3cb29f76..475bf06e2 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -73,6 +73,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(iii)i", font_get_glyph), NATIVE_SYMBOL ("(ii)i", font_get_glyph_h_advance), NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), + NATIVE_SYMBOL ("(iii)i", font_get_glyph_extents), NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), /* shape */ diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 2180b882a..622398f24 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -191,6 +191,18 @@ HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV ptr_t(font_t), codepoint_t glyph); +typedef struct { + position_t x_bearing; + position_t y_bearing; + position_t width; + position_t height; +} glyph_extents_t; + +HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV + ptr_t(font_t), + codepoint_t glyph, + ptr_t(glyph_extents_t) extents); + HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV ptr_t(font_t), codepoint_t glyph, From fea3ffe03147c35180be09b42caee90a96c61a34 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 12:31:32 -0700 Subject: [PATCH 064/149] [wasm-api] Beautify internal API --- src/hb-wasm-api-blob.hh | 2 +- src/hb-wasm-api-buffer.hh | 16 ++++++------ src/hb-wasm-api-face.hh | 4 +-- src/hb-wasm-api-font.hh | 20 +++++++-------- src/hb-wasm-api-shape.hh | 4 +-- src/hb-wasm-api.h | 53 +++++++++++++++++++++------------------ src/hb-wasm-api.hh | 3 ++- 7 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index e3997d168..afcbd2672 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -33,7 +33,7 @@ namespace wasm { void blob_free (HB_WASM_EXEC_ENV - ptr_t(blob_t) blobptr) + ptr_d(blob_t, blob)) { HB_OUT_PARAM (blob_t, blob); if (unlikely (!blob)) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 1e6edd57e..d75cb6231 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -37,7 +37,7 @@ static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); void buffer_contents_free (HB_WASM_EXEC_ENV - ptr_t(buffer_contents_t) contentsptr) + ptr_d(buffer_contents_t, contents)) { HB_OUT_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) @@ -53,7 +53,7 @@ buffer_contents_free (HB_WASM_EXEC_ENV void buffer_contents_realloc (HB_WASM_EXEC_ENV - ptr_t(buffer_contents_t) contentsptr, + ptr_d(buffer_contents_t, contents), uint32_t size) { HB_OUT_PARAM (buffer_contents_t, contents); @@ -85,7 +85,7 @@ void void buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND - ptr_t(buffer_t) bufferref) + ptr_d(buffer_t, buffer)) { HB_RETURN_STRUCT (buffer_contents_t, ret); HB_REF2OBJ (buffer); @@ -101,8 +101,8 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND bool_t buffer_set_contents (HB_WASM_EXEC_ENV - ptr_t(buffer_t) bufferref, - ptr_t(const buffer_contents_t) contentsptr) + ptr_d(buffer_t, buffer), + ptr_d(const buffer_contents_t, contents)) { HB_REF2OBJ (buffer); HB_OUT_PARAM (buffer_contents_t, contents); @@ -130,7 +130,7 @@ buffer_set_contents (HB_WASM_EXEC_ENV direction_t buffer_get_direction (HB_WASM_EXEC_ENV - ptr_t(buffer_t) bufferref) + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); @@ -139,7 +139,7 @@ buffer_get_direction (HB_WASM_EXEC_ENV void buffer_reverse (HB_WASM_EXEC_ENV - ptr_t(buffer_t) bufferref) + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); @@ -148,7 +148,7 @@ buffer_reverse (HB_WASM_EXEC_ENV void buffer_reverse_clusters (HB_WASM_EXEC_ENV - ptr_t(buffer_t) bufferref) + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 918a08682..8e086d140 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -33,7 +33,7 @@ namespace wasm { void face_reference_table (HB_WASM_EXEC_ENV_COMPOUND - ptr_t(face_t) faceref, + ptr_d(face_t, face), tag_t table_tag) { HB_RETURN_STRUCT (blob_t, ret); @@ -52,7 +52,7 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND unsigned face_get_upem (HB_WASM_EXEC_ENV - ptr_t(face_t) faceref) + ptr_d(face_t, face)) { HB_REF2OBJ (face); diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index b4b0548dd..89a264bd3 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -33,7 +33,7 @@ namespace wasm { ptr_t(face_t) font_get_face (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref) + ptr_d(font_t, font)) { HB_REF2OBJ (font); @@ -45,9 +45,9 @@ font_get_face (HB_WASM_EXEC_ENV void font_get_scale (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, - ptr_t(int32_t) x_scaleptr, - ptr_t(int32_t) y_scaleptr) + ptr_d(font_t, font), + ptr_d(int32_t, x_scale), + ptr_d(int32_t, y_scale)) { HB_REF2OBJ (font); @@ -59,7 +59,7 @@ font_get_scale (HB_WASM_EXEC_ENV codepoint_t font_get_glyph (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, + ptr_d(font_t, font), codepoint_t unicode, codepoint_t variation_selector) { @@ -72,7 +72,7 @@ font_get_glyph (HB_WASM_EXEC_ENV hb_position_t font_get_glyph_h_advance (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, + ptr_d(font_t, font), codepoint_t glyph) { HB_REF2OBJ (font); @@ -81,7 +81,7 @@ font_get_glyph_h_advance (HB_WASM_EXEC_ENV hb_position_t font_get_glyph_v_advance (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, + ptr_d(font_t, font), codepoint_t glyph) { HB_REF2OBJ (font); @@ -92,9 +92,9 @@ static_assert (sizeof (glyph_extents_t) == sizeof (hb_glyph_extents_t), ""); bool_t font_get_glyph_extents (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, + ptr_d(font_t, font), codepoint_t glyph, - ptr_t(glyph_extents_t) extentsptr) + ptr_d(glyph_extents_t, extents)) { HB_REF2OBJ (font); HB_OUT_PARAM (glyph_extents_t, extents); @@ -107,7 +107,7 @@ font_get_glyph_extents (HB_WASM_EXEC_ENV void font_glyph_to_string (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, + ptr_d(font_t, font), codepoint_t glyph, char *s, uint32_t size) { diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index 952aeb229..0a65e471b 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -33,8 +33,8 @@ namespace wasm { bool_t shape_with (HB_WASM_EXEC_ENV - ptr_t(font_t) fontref, - ptr_t(buffer_t) bufferref, + ptr_d(font_t, font), + ptr_d(buffer_t, buffer), const char *shaper) { HB_REF2OBJ (font); diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 622398f24..d72f4d2ac 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -68,6 +68,9 @@ HB_WASM_BEGIN_DECLS #ifndef ptr_t #define ptr_t(type_t) type_t * #endif +#ifndef ptr_d +#define ptr_d(type_t, name) type_t *name +#endif typedef uint32_t codepoint_t; typedef int32_t position_t; @@ -99,7 +102,7 @@ typedef struct } blob_t; HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV - ptr_t(blob_t)); + ptr_d(blob_t, blob)); /* buffer */ @@ -130,40 +133,40 @@ typedef struct } buffer_contents_t; HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV - ptr_t(buffer_contents_t), + ptr_d(buffer_contents_t, contents), uint32_t size); HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV - ptr_t(buffer_contents_t)); + ptr_d(buffer_contents_t, contents)); typedef struct buffer_t buffer_t; HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND - ptr_t(buffer_t)); + ptr_d(buffer_t, buffer)); HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV - ptr_t(buffer_t), - ptr_t(const buffer_contents_t)); + ptr_d(buffer_t, buffer), + ptr_d(const buffer_contents_t, contents)); HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV - ptr_t(buffer_t)); + ptr_d(buffer_t, buffer)); HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV - ptr_t(buffer_t)); + ptr_d(buffer_t, buffer)); HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV - ptr_t(buffer_t)); + ptr_d(buffer_t, buffer)); /* face */ typedef struct face_t face_t; HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND - ptr_t(face_t), + ptr_d(face_t, face), tag_t table_tag); HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV - ptr_t(face_t)); + ptr_d(face_t, face)); /* font */ @@ -171,24 +174,24 @@ HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV typedef struct font_t font_t; HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV - ptr_t(font_t)); + ptr_d(font_t, font)); HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV - ptr_t(font_t), - ptr_t(int32_t) x_scale, - ptr_t(int32_t) y_scale); + ptr_d(font_t, font), + ptr_d(int32_t, x_scale), + ptr_d(int32_t, y_scale)); HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV - ptr_t(font_t), + ptr_d(font_t, font), codepoint_t unicode, codepoint_t variation_selector); HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV - ptr_t(font_t), + ptr_d(font_t, font), codepoint_t glyph); HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV - ptr_t(font_t), + ptr_d(font_t, font), codepoint_t glyph); typedef struct { @@ -199,12 +202,12 @@ typedef struct { } glyph_extents_t; HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV - ptr_t(font_t), + ptr_d(font_t, font), codepoint_t glyph, - ptr_t(glyph_extents_t) extents); + ptr_d(glyph_extents_t, extents)); HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV - ptr_t(font_t), + ptr_d(font_t, font), codepoint_t glyph, char *s, uint32_t size); @@ -212,16 +215,16 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV /* shape */ HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV - ptr_t(font_t), - ptr_t(buffer_t), + ptr_d(font_t, font), + ptr_d(buffer_t, buffer), const char *shaper); /* shape interface */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV - ptr_t(font_t), - ptr_t(buffer_t)); + ptr_d(font_t, font), + ptr_d(buffer_t, buffer)); HB_WASM_END_DECLS diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 19570698e..5b99c8030 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -39,6 +39,7 @@ #define HB_WASM_EXEC_ENV_COMPOUND wasm_exec_env_t exec_env, ptr_t() retptr, #define ptr_t(type_t) uint32_t +#define ptr_d(type_t, name) uint32_t name##ptr #include "hb-wasm-api.h" @@ -61,7 +62,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; #define HB_REF2OBJ(obj) \ hb_##obj##_t *obj = nullptr; \ HB_STMT_START { \ - (void) wasm_externref_ref2obj (obj##ref, (void **) &obj); \ + (void) wasm_externref_ref2obj (obj##ptr, (void **) &obj); \ /* Check object type. */ \ /* This works because all our objects have the same hb_object_t layout. */ \ if (unlikely (hb_##obj##_get_user_data (obj, &_hb_wasm_ref_type_key) != \ From 39f8703df17cc49b8630597fc250b13ef61870b8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 12:42:22 -0700 Subject: [PATCH 065/149] [wasm-api] Match interface and implementation signatures --- src/hb-wasm-api-blob.hh | 5 ++- src/hb-wasm-api-buffer.hh | 65 +++++++++++++++++---------------------- src/hb-wasm-api-face.hh | 12 +++----- src/hb-wasm-api-font.hh | 55 +++++++++++++++------------------ src/hb-wasm-api-shape.hh | 9 +++--- src/hb-wasm-api.hh | 1 - 6 files changed, 64 insertions(+), 83 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index afcbd2672..26b022bc3 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -31,9 +31,8 @@ namespace hb { namespace wasm { -void -blob_free (HB_WASM_EXEC_ENV - ptr_d(blob_t, blob)) +HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob)) { HB_OUT_PARAM (blob_t, blob); if (unlikely (!blob)) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index d75cb6231..40f5700a6 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -35,26 +35,9 @@ namespace wasm { static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); -void -buffer_contents_free (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents)) -{ - HB_OUT_PARAM (buffer_contents_t, contents); - if (unlikely (!contents)) - return; - - module_free (contents->info); - module_free (contents->pos); - - contents->info = nullref; - contents->pos = nullref; - contents->length = 0; -} - -void - buffer_contents_realloc (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents), - uint32_t size) +HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents), + uint32_t size) { HB_OUT_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) @@ -83,9 +66,23 @@ void contents->length = size; } -void -buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(buffer_t, buffer)) +HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents)) +{ + HB_OUT_PARAM (buffer_contents_t, contents); + if (unlikely (!contents)) + return; + + module_free (contents->info); + module_free (contents->pos); + + contents->info = nullref; + contents->pos = nullref; + contents->length = 0; +} + +HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND + ptr_d(buffer_t, buffer)) { HB_RETURN_STRUCT (buffer_contents_t, ret); HB_REF2OBJ (buffer); @@ -99,10 +96,9 @@ buffer_copy_contents (HB_WASM_EXEC_ENV_COMPOUND ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); } -bool_t -buffer_set_contents (HB_WASM_EXEC_ENV - ptr_d(buffer_t, buffer), - ptr_d(const buffer_contents_t, contents)) +HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer), + ptr_d(const buffer_contents_t, contents)) { HB_REF2OBJ (buffer); HB_OUT_PARAM (buffer_contents_t, contents); @@ -128,27 +124,24 @@ buffer_set_contents (HB_WASM_EXEC_ENV return true; } -direction_t -buffer_get_direction (HB_WASM_EXEC_ENV - ptr_d(buffer_t, buffer)) +HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); return (direction_t) hb_buffer_get_direction (buffer); } -void -buffer_reverse (HB_WASM_EXEC_ENV - ptr_d(buffer_t, buffer)) +HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); hb_buffer_reverse (buffer); } -void -buffer_reverse_clusters (HB_WASM_EXEC_ENV - ptr_d(buffer_t, buffer)) +HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)) { HB_REF2OBJ (buffer); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 8e086d140..1d891dadf 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -31,10 +31,9 @@ namespace hb { namespace wasm { -void -face_reference_table (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(face_t, face), - tag_t table_tag) +HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND + ptr_d(face_t, face), + tag_t table_tag) { HB_RETURN_STRUCT (blob_t, ret); HB_REF2OBJ (face); @@ -50,9 +49,8 @@ face_reference_table (HB_WASM_EXEC_ENV_COMPOUND hb_blob_destroy (blob); } -unsigned -face_get_upem (HB_WASM_EXEC_ENV - ptr_d(face_t, face)) +HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)) { HB_REF2OBJ (face); diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 89a264bd3..e5a6ff7fd 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -31,9 +31,8 @@ namespace hb { namespace wasm { -ptr_t(face_t) -font_get_face (HB_WASM_EXEC_ENV - ptr_d(font_t, font)) +HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV + ptr_d(font_t, font)) { HB_REF2OBJ (font); @@ -43,11 +42,10 @@ font_get_face (HB_WASM_EXEC_ENV return faceref; } -void -font_get_scale (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - ptr_d(int32_t, x_scale), - ptr_d(int32_t, y_scale)) +HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(int32_t, x_scale), + ptr_d(int32_t, y_scale)) { HB_REF2OBJ (font); @@ -57,11 +55,10 @@ font_get_scale (HB_WASM_EXEC_ENV hb_font_get_scale (font, x_scale, y_scale); } -codepoint_t -font_get_glyph (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - codepoint_t unicode, - codepoint_t variation_selector) +HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t unicode, + codepoint_t variation_selector) { HB_REF2OBJ (font); codepoint_t glyph; @@ -70,19 +67,17 @@ font_get_glyph (HB_WASM_EXEC_ENV return glyph; } -hb_position_t -font_get_glyph_h_advance (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - codepoint_t glyph) +HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph) { HB_REF2OBJ (font); return hb_font_get_glyph_h_advance (font, glyph); } -hb_position_t -font_get_glyph_v_advance (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - codepoint_t glyph) +HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph) { HB_REF2OBJ (font); return hb_font_get_glyph_v_advance (font, glyph); @@ -90,11 +85,10 @@ font_get_glyph_v_advance (HB_WASM_EXEC_ENV static_assert (sizeof (glyph_extents_t) == sizeof (hb_glyph_extents_t), ""); -bool_t -font_get_glyph_extents (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - codepoint_t glyph, - ptr_d(glyph_extents_t, extents)) +HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph, + ptr_d(glyph_extents_t, extents)) { HB_REF2OBJ (font); HB_OUT_PARAM (glyph_extents_t, extents); @@ -105,11 +99,10 @@ font_get_glyph_extents (HB_WASM_EXEC_ENV (hb_glyph_extents_t *) extents); } -void -font_glyph_to_string (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - codepoint_t glyph, - char *s, uint32_t size) +HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph, + char *s, uint32_t size) { HB_REF2OBJ (font); diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index 0a65e471b..d141b3c69 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -31,11 +31,10 @@ namespace hb { namespace wasm { -bool_t -shape_with (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - ptr_d(buffer_t, buffer), - const char *shaper) +HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(buffer_t, buffer), + const char *shaper) { HB_REF2OBJ (font); HB_REF2OBJ (buffer); diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index 5b99c8030..f5ef9e487 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -43,7 +43,6 @@ #include "hb-wasm-api.h" -#undef HB_WASM_API #undef HB_WASM_BEGIN_DECLS #undef HB_WASM_END_DECLS From cbc71c56bcc5eded3e2393a0b1d2b380fa5944a0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 13:15:11 -0700 Subject: [PATCH 066/149] [graphite] Ensure native direction Mirrored characters come out wrong. Oh well. Better than before though. --- src/hb-graphite2.cc | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index 541c1269f..58dfee575 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -248,6 +248,21 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, gr_fref_set_feature_value (fref, features[i].value, feats); } + hb_direction_t target_direction = buffer->props.direction; + hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script); + /* TODO vertical: + * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType + * Ogham fonts are supposed to be implemented BTT or not. Need to research that + * first. */ + if ((HB_DIRECTION_IS_HORIZONTAL (target_direction) && + target_direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) || + (HB_DIRECTION_IS_VERTICAL (target_direction) && + target_direction != HB_DIRECTION_TTB)) + { + hb_buffer_reverse_clusters (buffer); + buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); + } + gr_segment *seg = nullptr; const gr_slot *is; unsigned int ci = 0, ic = 0; @@ -261,8 +276,6 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, for (unsigned int i = 0; i < buffer->len; ++i) chars[i] = buffer->info[i].codepoint; - /* TODO ensure_native_direction. */ - seg = gr_make_seg (nullptr, grface, HB_TAG_NONE, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 feats, @@ -433,6 +446,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, buffer->clear_glyph_flags (); buffer->unsafe_to_break (); + buffer->props.direction = target_direction; return true; } From ec3270c7bb162a8e8996192922628502dab9d5a2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 13:31:10 -0700 Subject: [PATCH 067/149] [wasm-graphite] Ensure native direction here too Binds buffer_get_script and script_get_horizontal_direction. --- src/hb-wasm-api-buffer.hh | 8 +++++++ src/hb-wasm-api-common.hh | 44 ++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 11 ++++++++++ src/meson.build | 1 + src/wasm-graphite/shape.cc | 21 ++++++++++++------ 7 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/hb-wasm-api-common.hh diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 40f5700a6..1d9612e88 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -132,6 +132,14 @@ HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV return (direction_t) hb_buffer_get_direction (buffer); } +HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)) +{ + HB_REF2OBJ (buffer); + + return hb_script_to_iso15924_tag (hb_buffer_get_script (buffer)); +} + HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)) { diff --git a/src/hb-wasm-api-common.hh b/src/hb-wasm-api-common.hh new file mode 100644 index 000000000..c38ca9ced --- /dev/null +++ b/src/hb-wasm-api-common.hh @@ -0,0 +1,44 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_WASM_API_COMMON_HH +#define HB_WASM_API_COMMON_HH + +#include "hb-wasm-api.hh" + +namespace hb { +namespace wasm { + + +HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV + script_t script) +{ + return (direction_t) + hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (script)); +} + + +}} + +#endif /* HB_WASM_API_COMMON_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 475bf06e2..0ded35980 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -50,6 +50,8 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md */ static NativeSymbol _hb_wasm_native_symbols[] = { + /* common */ + NATIVE_SYMBOL ("(i)i", script_get_horizontal_direction), /* blob */ NATIVE_SYMBOL ("(i)", blob_free), @@ -60,6 +62,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), + NATIVE_SYMBOL ("(i)i", buffer_get_script), NATIVE_SYMBOL ("(i)", buffer_reverse), NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index bc3babd56..ae5c1835e 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -29,6 +29,7 @@ #include "hb-wasm-api-blob.hh" #include "hb-wasm-api-buffer.hh" +#include "hb-wasm-api-common.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" #include "hb-wasm-api-shape.hh" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index d72f4d2ac..a59b405a4 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -92,6 +92,14 @@ typedef enum { #define DIRECTION_IS_BACKWARD(dir) ((((unsigned int) (dir)) & ~2U) == 5) #define DIRECTION_REVERSE(dir) ((direction_t) (((unsigned int) (dir)) ^ 1)) +typedef tag_t script_t; /* ISO 15924 representation of Unicode scripts. */ + + +/* common */ + +HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV + script_t script); + /* blob */ @@ -151,6 +159,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)); +HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)); + HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)); diff --git a/src/meson.build b/src/meson.build index 74998ce42..f6b33159e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -329,6 +329,7 @@ hb_wasm_sources = files( 'hb-wasm-api.hh', 'hb-wasm-api-blob.hh', 'hb-wasm-api-buffer.hh', + 'hb-wasm-api-common.hh', 'hb-wasm-api-face.hh', 'hb-wasm-api-font.hh', 'hb-wasm-api-shape.hh', diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index fcf87c00e..e1a863b10 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -38,15 +38,24 @@ static void free_table (const void *data, const void *table_data) bool_t shape (font_t *font, buffer_t *buffer) { - face_t *face = font_get_face (font); - - blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); - - blob_free (&blob); + direction_t direction = buffer_get_direction (buffer); + direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer)); + /* TODO vertical: + * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType + * Ogham fonts are supposed to be implemented BTT or not. Need to research that + * first. */ + if ((DIRECTION_IS_HORIZONTAL (direction) && + direction != horiz_dir && horiz_dir != DIRECTION_INVALID) || + (DIRECTION_IS_VERTICAL (direction) && + direction != DIRECTION_TTB)) + { + buffer_reverse_clusters (buffer); + direction = DIRECTION_REVERSE (direction); + } buffer_contents_t contents = buffer_copy_contents (buffer); - direction_t direction = buffer_get_direction (buffer); + face_t *face = font_get_face (font); const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, &free_table}; gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); From b130b2b3317d296f0120acf5f9aab4bab39f2506 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 13:34:16 -0700 Subject: [PATCH 068/149] [graphite] Simplify direction handling --- src/hb-graphite2.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index 58dfee575..9bb9136c4 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -248,19 +248,19 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, gr_fref_set_feature_value (fref, features[i].value, feats); } - hb_direction_t target_direction = buffer->props.direction; + hb_direction_t direction = buffer->props.direction; hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script); /* TODO vertical: * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType * Ogham fonts are supposed to be implemented BTT or not. Need to research that * first. */ - if ((HB_DIRECTION_IS_HORIZONTAL (target_direction) && - target_direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) || - (HB_DIRECTION_IS_VERTICAL (target_direction) && - target_direction != HB_DIRECTION_TTB)) + if ((HB_DIRECTION_IS_HORIZONTAL (direction) && + direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) || + (HB_DIRECTION_IS_VERTICAL (direction) && + direction != HB_DIRECTION_TTB)) { hb_buffer_reverse_clusters (buffer); - buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); + direction = HB_DIRECTION_REVERSE (direction); } gr_segment *seg = nullptr; @@ -280,7 +280,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, HB_TAG_NONE, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 feats, gr_utf32, chars, buffer->len, - 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0)); + 2 | (direction == HB_DIRECTION_RTL ? 1 : 0)); if (unlikely (!seg)) { if (feats) gr_featureval_destroy (feats); @@ -332,7 +332,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, float yscale = (float) font->y_scale / upem; yscale *= yscale / xscale; unsigned int curradv = 0; - if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (HB_DIRECTION_IS_BACKWARD (direction)) { curradv = gr_slot_origin_X(gr_seg_first_slot(seg)) * xscale; clusters[0].advance = gr_seg_advance_X(seg) * xscale - curradv; @@ -361,7 +361,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, c->num_chars = before - c->base_char; c->base_glyph = ic; c->num_glyphs = 0; - if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (HB_DIRECTION_IS_BACKWARD (direction)) { c->advance = curradv - gr_slot_origin_X(is) * xscale; curradv -= c->advance; @@ -380,7 +380,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, clusters[ci].num_chars = after + 1 - clusters[ci].base_char; } - if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (HB_DIRECTION_IS_BACKWARD (direction)) clusters[ci].advance += curradv; else clusters[ci].advance += gr_seg_advance_X(seg) * xscale - curradv; @@ -402,7 +402,7 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, unsigned int currclus = UINT_MAX; const hb_glyph_info_t *info = buffer->info; hb_glyph_position_t *pPos = hb_buffer_get_glyph_positions (buffer, nullptr); - if (!HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) + if (!HB_DIRECTION_IS_BACKWARD (direction)) { curradvx = 0; for (is = gr_seg_first_slot (seg); is; pPos++, ++info, is = gr_slot_next_in_segment (is)) @@ -446,7 +446,6 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan HB_UNUSED, buffer->clear_glyph_flags (); buffer->unsafe_to_break (); - buffer->props.direction = target_direction; return true; } From 2327fe9d8a77c930ec5e939060c714ef3008d325 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 13:35:47 -0700 Subject: [PATCH 069/149] [hb-wasm] Remove TODO --- src/hb-wasm-shape.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 781a8d6da..4fe2ab511 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -161,8 +161,6 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, const hb_wasm_face_data_t *face_data = font->face->data.wasm; constexpr uint32_t stack_size = 8092, heap_size = 2 * 1024 * 1024; - /* TODO ensure_native_direction. */ - wasm_module_inst_t module_inst = nullptr; wasm_exec_env_t exec_env = nullptr; wasm_function_inst_t shape_func = nullptr; From 9f8ad3928afeaf10a0dac8e7d0f3df6bd822c63d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 14:16:11 -0700 Subject: [PATCH 070/149] [wasm-api] Bind shaper features --- src/hb-wasm-api-blob.hh | 2 +- src/hb-wasm-api-buffer.hh | 6 +++--- src/hb-wasm-api-font.hh | 6 +++--- src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api-shape.hh | 14 ++++++++++++-- src/hb-wasm-api.h | 18 +++++++++++++++--- src/hb-wasm-api.hh | 11 ++++++++++- src/hb-wasm-shape.cc | 12 +++++++++++- src/wasm-graphite/shape.cc | 5 ++++- src/wasm-sample/shape-fallback.cc | 5 ++++- src/wasm-sample/shape-ot.cc | 7 +++++-- 11 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index 26b022bc3..310f4023f 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -34,7 +34,7 @@ namespace wasm { HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)) { - HB_OUT_PARAM (blob_t, blob); + HB_PTR_PARAM (blob_t, blob); if (unlikely (!blob)) return; diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 1d9612e88..7cfdb62f6 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -39,7 +39,7 @@ HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV ptr_d(buffer_contents_t, contents), uint32_t size) { - HB_OUT_PARAM (buffer_contents_t, contents); + HB_PTR_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return; @@ -69,7 +69,7 @@ HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV ptr_d(buffer_contents_t, contents)) { - HB_OUT_PARAM (buffer_contents_t, contents); + HB_PTR_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return; @@ -101,7 +101,7 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV ptr_d(const buffer_contents_t, contents)) { HB_REF2OBJ (buffer); - HB_OUT_PARAM (buffer_contents_t, contents); + HB_PTR_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) return false; diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index e5a6ff7fd..e3e5453d7 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -49,8 +49,8 @@ HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV { HB_REF2OBJ (font); - HB_OUT_PARAM(int32_t, x_scale); - HB_OUT_PARAM(int32_t, y_scale); + HB_PTR_PARAM(int32_t, x_scale); + HB_PTR_PARAM(int32_t, y_scale); hb_font_get_scale (font, x_scale, y_scale); } @@ -91,7 +91,7 @@ HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV ptr_d(glyph_extents_t, extents)) { HB_REF2OBJ (font); - HB_OUT_PARAM (glyph_extents_t, extents); + HB_PTR_PARAM (glyph_extents_t, extents); if (unlikely (!extents)) return false; diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 0ded35980..8d05f2914 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -80,7 +80,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), /* shape */ - NATIVE_SYMBOL ("(ii$)i", shape_with), + NATIVE_SYMBOL ("(iiii$)i", shape_with), /* debug */ #ifdef HB_DEBUG_WASM diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index d141b3c69..7e12969f7 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -31,19 +31,29 @@ namespace hb { namespace wasm { +static_assert (sizeof (feature_t) == sizeof (hb_feature_t), ""); + HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV ptr_d(font_t, font), ptr_d(buffer_t, buffer), + ptr_d(const feature_t, features), + uint32_t num_features, const char *shaper) { + if (unlikely (0 == strcmp (shaper, "wasm"))) + return false; + HB_REF2OBJ (font); HB_REF2OBJ (buffer); - if (0 == strcmp (shaper, "wasm")) + HB_ARRAY_PARAM (const feature_t, features, num_features); + if (unlikely (!features && num_features)) return false; const char * shaper_list[] = {shaper, nullptr}; - return hb_shape_full (font, buffer, nullptr, 0, shaper_list); + return hb_shape_full (font, buffer, + (hb_feature_t *) features, num_features, + shaper_list); } diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index a59b405a4..acfe69a11 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -225,17 +225,29 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV /* shape */ +typedef struct { + tag_t tag; + uint32_t value; + uint32_t start; + uint32_t end; +} feature_t; +#define FEATURE_GLOBAL_START 0 +#define FEATURE_GLOBAL_END ((uint32_t) -1) + HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV ptr_d(font_t, font), ptr_d(buffer_t, buffer), + ptr_d(const feature_t, features), + uint32_t num_features, const char *shaper); - -/* shape interface */ +/* Implement this in your shaper. */ HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV ptr_d(font_t, font), - ptr_d(buffer_t, buffer)); + ptr_d(buffer_t, buffer), + ptr_d(const feature_t, features), + uint32_t num_features); HB_WASM_END_DECLS diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index f5ef9e487..f7e5565be 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -91,7 +91,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; } \ type &name = *_name_ptr -#define HB_OUT_PARAM(type, name) \ +#define HB_PTR_PARAM(type, name) \ type *name = nullptr; \ HB_STMT_START { \ if (likely (wasm_runtime_validate_app_addr (module_inst, \ @@ -99,5 +99,14 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \ } HB_STMT_END +#define HB_ARRAY_PARAM(type, name, length) \ + type *name = nullptr; \ + HB_STMT_START { \ + if (likely (!hb_unsigned_mul_overflows (length, sizeof (type)) && \ + wasm_runtime_validate_app_addr (module_inst, \ + name##ptr, length * sizeof (type)))) \ + name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \ + } HB_STMT_END + #endif /* HB_WASM_API_HH */ diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 4fe2ab511..317fb9cf0 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -197,17 +197,27 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, } wasm_val_t results[1]; - wasm_val_t arguments[2]; + wasm_val_t arguments[4]; results[0].kind = WASM_I32; arguments[0].kind = WASM_I32; arguments[0].of.i32 = fontref; arguments[1].kind = WASM_I32; arguments[1].of.i32 = bufferref; + arguments[2].kind = WASM_I32; + arguments[2].of.i32 = wasm_runtime_module_dup_data (module_inst, + (const char *) features, + num_features * sizeof (features[0])); + arguments[3].kind = WASM_I32; + arguments[3].of.i32 = num_features; ret = wasm_runtime_call_wasm_a (exec_env, shape_func, ARRAY_LENGTH (results), results, ARRAY_LENGTH (arguments), arguments); + + + wasm_runtime_module_free (module_inst, arguments[2].of.i32); + if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index e1a863b10..a1f140867 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -36,7 +36,10 @@ static void free_table (const void *data, const void *table_data) } bool_t -shape (font_t *font, buffer_t *buffer) +shape (font_t *font, + buffer_t *buffer, + const feature_t *features, + uint32_t num_features) { direction_t direction = buffer_get_direction (buffer); direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer)); diff --git a/src/wasm-sample/shape-fallback.cc b/src/wasm-sample/shape-fallback.cc index 11853c402..ecb90d030 100644 --- a/src/wasm-sample/shape-fallback.cc +++ b/src/wasm-sample/shape-fallback.cc @@ -9,7 +9,10 @@ void debugprint2 (const char *s, int32_t, int32_t); } bool_t -shape (font_t *font, buffer_t *buffer) +shape (font_t *font, + buffer_t *buffer, + const feature_t *features, + uint32_t num_features) { face_t *face = font_get_face (font); diff --git a/src/wasm-sample/shape-ot.cc b/src/wasm-sample/shape-ot.cc index 10e1e723d..98276ec49 100644 --- a/src/wasm-sample/shape-ot.cc +++ b/src/wasm-sample/shape-ot.cc @@ -8,7 +8,10 @@ void debugprint2 (const char *s, int32_t, int32_t); } bool_t -shape (font_t *font, buffer_t *buffer) +shape (font_t *font, + buffer_t *buffer, + const feature_t *features, + uint32_t num_features) { - return shape_with (font, buffer, "ot"); + return shape_with (font, buffer, features, num_features, "ot"); } From 1c6d640e1fbf459b3cf2ef66ad48560e8469f72e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 15:01:36 -0700 Subject: [PATCH 071/149] [wasm-shaper] Whitespace --- src/hb-wasm-shape.cc | 9 +++------ src/wasm-graphite/Makefile | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 317fb9cf0..3e2c613f5 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -73,7 +73,6 @@ init_wasm () return false; } - initialized = true; return true; } @@ -101,7 +100,6 @@ _hb_wasm_shaper_face_data_create (hb_face_t *face) goto fail; } - data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); if (unlikely (!data)) goto fail; @@ -211,10 +209,9 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, arguments[3].kind = WASM_I32; arguments[3].of.i32 = num_features; - ret = wasm_runtime_call_wasm_a (exec_env, shape_func, - ARRAY_LENGTH (results), results, - ARRAY_LENGTH (arguments), arguments); - + ret = wasm_runtime_call_wasm_a (exec_env, shape_func, + ARRAY_LENGTH (results), results, + ARRAY_LENGTH (arguments), arguments); wasm_runtime_module_free (module_inst, arguments[2].of.i32); diff --git a/src/wasm-graphite/Makefile b/src/wasm-graphite/Makefile index 7b486ea6b..593212cc1 100644 --- a/src/wasm-graphite/Makefile +++ b/src/wasm-graphite/Makefile @@ -1,4 +1,4 @@ -all: CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf +all: CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf AwamiNastaliq-Regular.wasm.ttf %.wasm: %.cc ../hb-wasm-api.h emcc \ From 8d960dfe68da0393eafd92a66fa4277b2e1d6e63 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 15:07:40 -0700 Subject: [PATCH 072/149] [wasm-sample] Remove Wasm table from test.ttf again Was added by mistake. --- src/wasm-sample/test.ttf | Bin 23468 -> 22116 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/src/wasm-sample/test.ttf b/src/wasm-sample/test.ttf index 2a5535f5f704ba31179af8dcd072c6f77364c43f..2ba04f611b660189a9f8023047a0e7c7e52985f3 100644 GIT binary patch delta 428 zcmZ3po$<*U#(D-u1_lNpAi=_*;O^q;#%RItnSp_E4p7v@J-|O$KA&eb0|So-0|RG< zdvK_e*?aCi3=AA=fNDCZ_z!x&)MGya42zWdH@ZcCi8F7%PB$m5khyicQ&?D;XHL zfPBUaIr+(ncj8a`0QFA+%KyqutSDew!^{bkvjFlH@)E6bQ-xS`S%BW302CA{$S*GW zpUaQ}WP@C+P*9Xwuqf@$D#KV;||+dncYw!`lLhkWeaI`-J~;r62M9k!zY?#$TXqr;Kk zyw?GaTm}%|OdsAnU^q{8@%|de&pe@)^Jjj#`5eIG27rAqkymxN20AC_8>SM+4eY+` zd=0?)728KrdfLoC`{xV`$e(kP>uI&DgA1Bika6&(bMvXIuYbY`-7Nso4>QU9+$-l# zI{|FB89$rR)THxF#|7pNu^q{h|&hoIe-fDZ(e|PQQKe+o`Wj5U)Szf-l;m+JYKB$}& z>ngiMd*wapsp_2Pdr@hyovV1I$?6@)by0oGJ|QYkN=GXfY=2eXUr~)#&&$p15uI*{ z8ztSl2mZ>dVn=n-mvmR(b=}|tn?`zj(Yjts1lCaBmp$j4GHUW~u#qCk9o|V1MM#+)a^2O4;1om=9 z)iv;VF4@IJyO=UNPw)jKDSc!^32nznMmbn>qKpucdUz2LDTH!J>LJsP_CTHDV89WP zf))@Yr_w?$_PDa-MmKqsJ70Ws$w6N9pcB1;la`7V*+M?$;#o_w0UOX4IAsCMUB=vH zS@NTw0vHIquoxer<%)|M8Ewo`6PiubjLo<_aC$MoMJtqZwS+5iC9YUV2(*eRt0Iyt zMQu?_Sf&f^GIeo^P1uE-a@36>?9NgT_E;Hu z6`(L~#9j;sUb5JYxgs80Q+$uM_bVB!!23bid%7WM5;Bkhql$jBlYk>4&RQ0 zxIITZFoHX>v=et)I=gTi?h2f#DGXV$0oq-s>|x4Y+=F{_6vf>b&60u&4lM}CG|Vm9 zhr_rpOXhxV>wa#l54>KB9ikCFpHcRXW@!w^=m3u3fdKb}Uu2x!!r#I>MdbE zz>9nMMab6gO?)Q2@bJKkN6xymWKjEU4<=lGFkee%+rAKOT7QKB$A3oJ`M=Zbdek7h zA4n`2VNZQFos9zgu~^wq6USn;*VamIj{s-5R7|SFh6=mgI&WtaTB$62yiI83vX<

Date: Fri, 24 Feb 2023 15:20:37 -0700 Subject: [PATCH 073/149] [wasm-shape] Use JIT running-modes if available Build wasm-micro-runtime with: $ cmake . -DWAMR_BUILD_REF_TYPES=1 -DWAMR_BUILD_FAST_JIT=1 or: $ cmake . -DWAMR_BUILD_REF_TYPES=1 -DWAMR_BUILD_JIT=1 The latter needs llvm and is harder to get working. Still trying. --- src/hb-wasm-shape.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 3e2c613f5..14dbf48fc 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -55,6 +55,11 @@ init_wasm () RuntimeInitArgs init_args; memset (&init_args, 0, sizeof (RuntimeInitArgs)); + if (wasm_runtime_is_running_mode_supported (Mode_LLVM_JIT)) + init_args.running_mode = Mode_LLVM_JIT; + else if (wasm_runtime_is_running_mode_supported (Mode_Fast_JIT)) + init_args.running_mode = Mode_Fast_JIT; + init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; @@ -157,7 +162,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, { bool ret = true; const hb_wasm_face_data_t *face_data = font->face->data.wasm; - constexpr uint32_t stack_size = 8092, heap_size = 2 * 1024 * 1024; + constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; wasm_module_inst_t module_inst = nullptr; wasm_exec_env_t exec_env = nullptr; From c5af08c0dc7388198019eab3ff16eccd614fce94 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 15:57:10 -0700 Subject: [PATCH 074/149] [wasm] Add to harfbuzz.cc and hb-features --- src/Makefile.am | 9 +++++---- src/harfbuzz.cc | 2 ++ src/hb-features.h.in | 7 +++++++ src/hb-wasm-api.cc | 6 ++++++ src/meson.build | 3 ++- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 56dc32d9a..26e56e5f6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -273,16 +273,17 @@ DISTCLEANFILES += \ hb-features.h: hb-features.h.in $(top_builddir)/config.status $(AM_V_GEN) $(SED) \ -e 's/mesondefine HB_HAS_CAIRO/$(HB_HAS_CAIRO_DEF)/' \ + -e 's/mesondefine HB_HAS_CORETEXT/$(HB_HAS_CORETEXT_DEF)/' \ + -e 's/mesondefine HB_HAS_DIRECTWRITE/$(HB_HAS_DIRECTWRITE_DEF)/' \ -e 's/mesondefine HB_HAS_FREETYPE/$(HB_HAS_FREETYPE_DEF)/' \ -e 's/mesondefine HB_HAS_GDI/$(HB_HAS_GDI_DEF)/' \ -e 's/mesondefine HB_HAS_GDI/$(HB_HAS_GDI_DEF)/' \ - -e 's/mesondefine HB_HAS_GRAPHITE/$(HB_HAS_GRAPHITE_DEF)/' \ -e 's/mesondefine HB_HAS_GLIB/$(HB_HAS_GLIB_DEF)/' \ -e 's/mesondefine HB_HAS_GOBJECT/$(HB_HAS_GOBJECT_DEF)/' \ - -e 's/mesondefine HB_HAS_UNISCRIBE/$(HB_HAS_UNISCRIBE_DEF)/' \ - -e 's/mesondefine HB_HAS_DIRECTWRITE/$(HB_HAS_DIRECTWRITE_DEF)/' \ - -e 's/mesondefine HB_HAS_CORETEXT/$(HB_HAS_CORETEXT_DEF)/' \ + -e 's/mesondefine HB_HAS_GRAPHITE/$(HB_HAS_GRAPHITE_DEF)/' \ -e 's/mesondefine HB_HAS_ICU/$(HB_HAS_ICU_DEF)/' \ + -e 's/mesondefine HB_HAS_UNISCRIBE/$(HB_HAS_UNISCRIBE_DEF)/' \ + -e 's/mesondefine HB_HAS_WASM/$(HB_HAS_WASM_DEF)/' \ "$<" > "$@" || ($(RM) "$@"; false) diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index d7e8a93f3..26e2bc145 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -58,3 +58,5 @@ #include "hb-ucd.cc" #include "hb-unicode.cc" #include "hb-uniscribe.cc" +#include "hb-wasm-api.cc" +#include "hb-wasm-shape.cc" diff --git a/src/hb-features.h.in b/src/hb-features.h.in index d85749ca9..4b27bd5e7 100644 --- a/src/hb-features.h.in +++ b/src/hb-features.h.in @@ -106,6 +106,13 @@ HB_BEGIN_DECLS */ #mesondefine HB_HAS_UNISCRIBE +/** + * HB_HAS_WASM: + * + * Defined if Harfbuzz has been built with WebAssembly support. + */ +#mesondefine HB_HAS_WASM + HB_END_DECLS diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index ae5c1835e..1da8347a0 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -22,6 +22,10 @@ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ +#include "hb.hh" + +#ifdef HAVE_WASM + #include "hb-wasm-api.hh" #define module_inst wasm_runtime_get_module_inst (exec_env) @@ -38,3 +42,5 @@ #undef module_inst hb_user_data_key_t _hb_wasm_ref_type_key = {}; + +#endif diff --git a/src/meson.build b/src/meson.build index f6b33159e..299634719 100644 --- a/src/meson.build +++ b/src/meson.build @@ -429,7 +429,7 @@ custom_target('harfbuzz.cc', output: 'harfbuzz.cc', input: hb_base_sources + hb_glib_sources + hb_ft_sources + hb_graphite2_sources + hb_uniscribe_sources + hb_gdi_sources + - hb_directwrite_sources + hb_coretext_sources, + hb_directwrite_sources + hb_coretext_sources + hb_wasm_sources, command: [find_program('gen-harfbuzzcc.py'), '@OUTPUT@', meson.current_source_dir(), '@INPUT@'], ) @@ -515,6 +515,7 @@ features = [ 'GRAPHITE', 'ICU', 'UNISCRIBE', + 'WASM', ] hb_enabled_features = configuration_data() From ed39e0766194194e0fcdd4b45cd428c4c1abd43d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 16:20:06 -0700 Subject: [PATCH 075/149] [wasm] Fix docs --- docs/harfbuzz-sections.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index abed39fee..808bddf11 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -197,6 +197,7 @@ HB_HAS_GOBJECT HB_HAS_GRAPHITE HB_HAS_ICU HB_HAS_UNISCRIBE +HB_HAS_WASM

From 99d2dab30f317058075d5a82c0dfedc898d6912d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 17:04:05 -0700 Subject: [PATCH 076/149] [wasm] Try at autotools build --- configure.ac | 23 +++++++++++++++++++++++ src/Makefile.am | 10 ++++++++++ src/Makefile.sources | 15 +++++++++++++-- src/harfbuzz.cc | 2 -- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index c863ab83b..2d402a429 100644 --- a/configure.ac +++ b/configure.ac @@ -417,6 +417,28 @@ AM_CONDITIONAL(HAVE_CORETEXT, $have_coretext) dnl =========================================================================== +AC_ARG_WITH(wasm, + [AS_HELP_STRING([--with-wasm=@<:@yes/no/auto@:>@], + [Use the wasm-micro-runtime library @<:@default=no@:>@])],, + [with_wasm=no]) +have_wasm=false +if test "x$with_wasm" = "xyes" -o "x$with_wasm" = "xauto"; then + AC_CHECK_HEADERS(wasm_export.h, have_wasm=true) +fi +if test "x$with_wasm" = "xyes" -a "x$have_wasm" != "xtrue"; then + AC_MSG_ERROR([wasm support requested but not found]) +fi +if $have_wasm; then + WASM_CFLAGS= + WASM_LIBS="-liwasm" + AC_SUBST(WASM_CFLAGS) + AC_SUBST(WASM_LIBS) + AC_DEFINE(HAVE_WASM, 1, [Have wasm-micro-runtime library]) +fi +AM_CONDITIONAL(HAVE_WASM, $have_wasm) + +dnl =========================================================================== + AC_CONFIG_FILES([ Makefile src/Makefile @@ -478,6 +500,7 @@ Platform shapers (not normally needed): DirectWrite: ${have_directwrite} GDI: ${have_gdi} Uniscribe: ${have_uniscribe} + WebAssembly: ${have_wasm} Other features: Documentation: ${enable_gtk_doc} diff --git a/src/Makefile.am b/src/Makefile.am index 26e56e5f6..4753bf1d4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -114,6 +114,16 @@ else HB_HAS_CORETEXT_DEF = undef HB_HAS_CORETEXT endif +if HAVE_WASM +HBCFLAGS += $(WASM_CFLAGS) +HBNONPCLIBS += $(WASM_LIBS) +HBSOURCES += $(HB_WASM_sources) +HBHEADERS += $(HB_WASM_headers) +HB_HAS_WASM_DEF = define HB_HAS_WASM 1 +else +HB_HAS_WASM_DEF = undef HB_HAS_WASM +endif + BUILT_SOURCES += \ hb-version.h diff --git a/src/Makefile.sources b/src/Makefile.sources index 4edea33c8..be69f467f 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -255,7 +255,6 @@ HB_BASE_sources = \ hb-unicode.hh \ hb-utf.hh \ hb-vector.hh \ - hb-wasm-shape.cc \ hb-priority-queue.hh \ hb.hh \ $(NULL) @@ -339,10 +338,22 @@ HB_GDI_headers = hb-gdi.h HB_UNISCRIBE_sources = hb-uniscribe.cc HB_UNISCRIBE_headers = hb-uniscribe.h -# Sources for libharfbuzz-gobject and libharfbuzz-icu HB_ICU_sources = hb-icu.cc HB_ICU_headers = hb-icu.h +HB_WASM_sources = \ + hb-wasm-api.cc \ + hb-wasm-api.hh \ + hb-wasm-api-blob.hh \ + hb-wasm-api-buffer.hh \ + hb-wasm-api-common.hh \ + hb-wasm-api-face.hh \ + hb-wasm-api-font.hh \ + hb-wasm-api-shape.hh \ + hb-wasm-shape.cc \ + $(NULL) +HB_WASM_headers = hb-wasm-api.h + # Sources for libharfbuzz-subset HB_SUBSET_sources = \ hb-number.cc \ diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index 26e2bc145..d7e8a93f3 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -58,5 +58,3 @@ #include "hb-ucd.cc" #include "hb-unicode.cc" #include "hb-uniscribe.cc" -#include "hb-wasm-api.cc" -#include "hb-wasm-shape.cc" From a267249930503f99a2517cd35f90556cefdea854 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 17:22:22 -0700 Subject: [PATCH 077/149] [wasm] Typo --- src/harfbuzz.cc | 2 ++ src/hb-wasm-shape.cc | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index d7e8a93f3..26e2bc145 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -58,3 +58,5 @@ #include "hb-ucd.cc" #include "hb-unicode.cc" #include "hb-uniscribe.cc" +#include "hb-wasm-api.cc" +#include "hb-wasm-shape.cc" diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 14dbf48fc..90bfcf23e 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -227,7 +227,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, goto fail; } - /* TODO Regularize clusters accordint to direction & cluster level, + /* TODO Regularize clusters according to direction & cluster level, * such that client doesn't crash with unmet expectations. */ if (!results[0].of.i32) From 73de7d4d05ab2c78e56a4b6156b52ebd2f107c8f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 17:44:33 -0700 Subject: [PATCH 078/149] [wasm-api] Add shape_plan --- src/hb-wasm-api.h | 9 +++- src/hb-wasm-shape.cc | 75 +++++++++++++++++++++++++------ src/wasm-graphite/shape.cc | 24 +++++++--- src/wasm-sample/shape-fallback.cc | 3 +- src/wasm-sample/shape-ot.cc | 3 +- 5 files changed, 93 insertions(+), 21 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index acfe69a11..515883566 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -241,14 +241,21 @@ HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV uint32_t num_features, const char *shaper); -/* Implement this in your shaper. */ +/* Implement these in your shaper. */ + +HB_WASM_INTERFACE (ptr_t(void), shape_plan_create) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)); HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV + ptr_d(void, shape_plan), ptr_d(font_t, font), ptr_d(buffer_t, buffer), ptr_d(const feature_t, features), uint32_t num_features); +HB_WASM_INTERFACE (void, shape_plan_destroy) (HB_WASM_EXEC_ENV + ptr_d(void, shape_plan)); + HB_WASM_END_DECLS diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 90bfcf23e..0bf556f9c 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -161,12 +161,14 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, unsigned int num_features) { bool ret = true; - const hb_wasm_face_data_t *face_data = font->face->data.wasm; + hb_face_t *face = font->face; + const hb_wasm_face_data_t *face_data = face->data.wasm; constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; wasm_module_inst_t module_inst = nullptr; wasm_exec_env_t exec_env = nullptr; - wasm_function_inst_t shape_func = nullptr; + wasm_function_inst_t func = nullptr; + uint32_t shape_planref = nullref; module_inst = wasm_runtime_instantiate(face_data->wasm_module, stack_size, heap_size, @@ -177,10 +179,12 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, return false; } + // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work + HB_OBJ2REF (face); HB_OBJ2REF (font); HB_OBJ2REF (buffer); - if (unlikely (!fontref || !bufferref)) + if (unlikely (!faceref || !fontref || !bufferref)) { DEBUG_MSG (WASM, module_inst, "Failed to register objects."); goto fail; @@ -192,29 +196,53 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, goto fail; } - shape_func = wasm_runtime_lookup_function (module_inst, "shape", nullptr); - if (unlikely (!shape_func)) + func = wasm_runtime_lookup_function (module_inst, "shape_plan_create", nullptr); + if (func) + { + wasm_val_t results[1]; + wasm_val_t arguments[1]; + + results[0].kind = WASM_I32; + arguments[0].kind = WASM_I32; + arguments[0].of.i32 = faceref; + ret = wasm_runtime_call_wasm_a (exec_env, func, + ARRAY_LENGTH (results), results, + ARRAY_LENGTH (arguments), arguments); + + if (unlikely (!ret)) + { + DEBUG_MSG (WASM, module_inst, "Calling shape_plan_create() failed: %s", + wasm_runtime_get_exception(module_inst)); + goto fail; + } + shape_planref = results[0].of.i32; + } + + func = wasm_runtime_lookup_function (module_inst, "shape", nullptr); + if (unlikely (!func)) { DEBUG_MSG (WASM, module_inst, "Shape function not found."); goto fail; } wasm_val_t results[1]; - wasm_val_t arguments[4]; + wasm_val_t arguments[5]; results[0].kind = WASM_I32; arguments[0].kind = WASM_I32; - arguments[0].of.i32 = fontref; + arguments[0].of.i32 = shape_planref; arguments[1].kind = WASM_I32; - arguments[1].of.i32 = bufferref; + arguments[1].of.i32 = fontref; arguments[2].kind = WASM_I32; - arguments[2].of.i32 = wasm_runtime_module_dup_data (module_inst, + arguments[2].of.i32 = bufferref; + arguments[3].kind = WASM_I32; + arguments[3].of.i32 = wasm_runtime_module_dup_data (module_inst, (const char *) features, num_features * sizeof (features[0])); - arguments[3].kind = WASM_I32; - arguments[3].of.i32 = num_features; + arguments[4].kind = WASM_I32; + arguments[4].of.i32 = num_features; - ret = wasm_runtime_call_wasm_a (exec_env, shape_func, + ret = wasm_runtime_call_wasm_a (exec_env, func, ARRAY_LENGTH (results), results, ARRAY_LENGTH (arguments), arguments); @@ -222,7 +250,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, if (unlikely (!ret)) { - DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", + DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", wasm_runtime_get_exception(module_inst)); goto fail; } @@ -236,6 +264,27 @@ fail: ret = false; } + if (ret && shape_planref) + { + func = wasm_runtime_lookup_function (module_inst, "shape_plan_destroy", nullptr); + if (func) + { + wasm_val_t arguments[1]; + + arguments[0].kind = WASM_I32; + arguments[0].of.i32 = shape_planref; + ret = wasm_runtime_call_wasm_a (exec_env, func, + 0, nullptr, + ARRAY_LENGTH (arguments), arguments); + + if (unlikely (!ret)) + { + DEBUG_MSG (WASM, module_inst, "Calling shape_plan_destroy() failed: %s", + wasm_runtime_get_exception(module_inst)); + } + } + } + if (exec_env) wasm_runtime_destroy_exec_env (exec_env); if (module_inst) diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index a1f140867..6aec37280 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -35,12 +35,30 @@ static void free_table (const void *data, const void *table_data) blob_free (&blob); } +void * +shape_plan_create (face_t *face) +{ + const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, &free_table}; + gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); + return grface; +} + +void +shape_plan_destroy (void *data) +{ + gr_face_destroy ((gr_face *) data); +} + bool_t -shape (font_t *font, +shape (void *shape_plan, + font_t *font, buffer_t *buffer, const feature_t *features, uint32_t num_features) { + face_t *face = font_get_face (font); + gr_face *grface = (gr_face *) shape_plan; + direction_t direction = buffer_get_direction (buffer); direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer)); /* TODO vertical: @@ -58,10 +76,6 @@ shape (font_t *font, buffer_contents_t contents = buffer_copy_contents (buffer); - face_t *face = font_get_face (font); - const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, &free_table}; - gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll); - gr_segment *seg = nullptr; const gr_slot *is; unsigned int ci = 0, ic = 0; diff --git a/src/wasm-sample/shape-fallback.cc b/src/wasm-sample/shape-fallback.cc index ecb90d030..a2a7090d6 100644 --- a/src/wasm-sample/shape-fallback.cc +++ b/src/wasm-sample/shape-fallback.cc @@ -9,7 +9,8 @@ void debugprint2 (const char *s, int32_t, int32_t); } bool_t -shape (font_t *font, +shape (void *shape_plan, + font_t *font, buffer_t *buffer, const feature_t *features, uint32_t num_features) diff --git a/src/wasm-sample/shape-ot.cc b/src/wasm-sample/shape-ot.cc index 98276ec49..9081cfebc 100644 --- a/src/wasm-sample/shape-ot.cc +++ b/src/wasm-sample/shape-ot.cc @@ -8,7 +8,8 @@ void debugprint2 (const char *s, int32_t, int32_t); } bool_t -shape (font_t *font, +shape (void *shape_plan, + font_t *font, buffer_t *buffer, const feature_t *features, uint32_t num_features) From cb382e489d656a3d955c15feb99ea887f6d8985a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 17:56:14 -0700 Subject: [PATCH 079/149] [wasm-shape] Cache wasm-shape-plan --- src/hb-wasm-api-buffer.hh | 3 + src/hb-wasm-shape.cc | 202 ++++++++++++++++++++++++++------------ 2 files changed, 140 insertions(+), 65 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 7cfdb62f6..a89fc3968 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -94,6 +94,9 @@ HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV ret.length = length; ret.info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); + + if (unlikely (buffer->successful && (!ret.info || !ret.pos))) + buffer->successful = false; } HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 0bf556f9c..05de4a20e 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -40,9 +40,16 @@ #define HB_WASM_TAG_WASM HB_TAG('W','a','s','m') +struct hb_wasm_shape_plan_t { + wasm_module_inst_t module_inst; + wasm_exec_env_t exec_env; + ptr_d(void, wasm_shape_plan); +}; + struct hb_wasm_face_data_t { hb_blob_t *wasm_blob; wasm_module_t wasm_module; + mutable hb_atomic_ptr_t plan; }; static bool @@ -122,9 +129,125 @@ fail: return nullptr; } +static hb_wasm_shape_plan_t * +acquire_shape_plan (hb_face_t *face, + const hb_wasm_face_data_t *face_data) +{ + constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; + + wasm_module_inst_t module_inst = nullptr; + wasm_exec_env_t exec_env = nullptr; + wasm_function_inst_t func = nullptr; + + /* Fetch cached one if available. */ + hb_wasm_shape_plan_t *plan = face_data->plan.get_acquire (); + if (likely (plan && face_data->plan.cmpexch (plan, nullptr))) + return plan; + + plan = (hb_wasm_shape_plan_t *) hb_calloc (1, sizeof (hb_wasm_shape_plan_t)); + + module_inst = plan->module_inst = wasm_runtime_instantiate(face_data->wasm_module, + stack_size, heap_size, + nullptr, 0); + if (unlikely (!module_inst)) + { + DEBUG_MSG (WASM, face_data, "Create wasm module instance failed."); + goto fail; + } + + exec_env = plan->exec_env = wasm_runtime_create_exec_env (module_inst, + stack_size); + if (unlikely (!exec_env)) { + DEBUG_MSG (WASM, face_data, "Create wasm execution environment failed."); + goto fail; + } + + func = wasm_runtime_lookup_function (module_inst, "shape_plan_create", nullptr); + if (func) + { + wasm_val_t results[1]; + wasm_val_t arguments[1]; + + HB_OBJ2REF (face); + if (unlikely (!faceref)) + { + DEBUG_MSG (WASM, face_data, "Failed to register face object."); + goto fail; + } + + results[0].kind = WASM_I32; + arguments[0].kind = WASM_I32; + arguments[0].of.i32 = faceref; + bool ret = wasm_runtime_call_wasm_a (exec_env, func, + ARRAY_LENGTH (results), results, + ARRAY_LENGTH (arguments), arguments); + + if (unlikely (!ret)) + { + DEBUG_MSG (WASM, module_inst, "Calling shape_plan_create() failed: %s", + wasm_runtime_get_exception(module_inst)); + goto fail; + } + plan->wasm_shape_planptr = results[0].of.i32; + } + + return plan; + +fail: + + if (exec_env) + wasm_runtime_destroy_exec_env (exec_env); + if (module_inst) + wasm_runtime_deinstantiate (module_inst); + hb_free (plan); + return nullptr; +} + +static void +release_shape_plan (const hb_wasm_face_data_t *face_data, + hb_wasm_shape_plan_t *plan, + bool cache = false) +{ + if (cache && face_data->plan.cmpexch (nullptr, plan)) + return; + + auto *module_inst = plan->module_inst; + auto *exec_env = plan->exec_env; + + /* Is there even any point to having a shape_plan_destroy function + * and calling it? */ + if (plan->wasm_shape_planptr) + { + + auto *func = wasm_runtime_lookup_function (module_inst, "shape_plan_destroy", nullptr); + if (func) + { + wasm_val_t arguments[1]; + + arguments[0].kind = WASM_I32; + arguments[0].of.i32 = plan->wasm_shape_planptr; + bool ret = wasm_runtime_call_wasm_a (exec_env, func, + 0, nullptr, + ARRAY_LENGTH (arguments), arguments); + + if (unlikely (!ret)) + { + DEBUG_MSG (WASM, module_inst, "Calling shape_plan_destroy() failed: %s", + wasm_runtime_get_exception(module_inst)); + } + } + } + + wasm_runtime_destroy_exec_env (exec_env); + wasm_runtime_deinstantiate (module_inst); + hb_free (plan); +} + void _hb_wasm_shaper_face_data_destroy (hb_wasm_face_data_t *data) { + if (data->plan.get_relaxed ()) + release_shape_plan (data, data->plan); wasm_runtime_unload (data->wasm_module); hb_blob_destroy (data->wasm_blob); hb_free (data); @@ -163,61 +286,28 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, bool ret = true; hb_face_t *face = font->face; const hb_wasm_face_data_t *face_data = face->data.wasm; - constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; - wasm_module_inst_t module_inst = nullptr; - wasm_exec_env_t exec_env = nullptr; wasm_function_inst_t func = nullptr; - uint32_t shape_planref = nullref; - module_inst = wasm_runtime_instantiate(face_data->wasm_module, - stack_size, heap_size, - nullptr, 0); - if (unlikely (!module_inst)) + hb_wasm_shape_plan_t *plan = acquire_shape_plan (face, face_data); + if (unlikely (!plan)) { - DEBUG_MSG (WASM, face_data->wasm_module, "Instantiate wasm module failed."); + DEBUG_MSG (WASM, face_data, "Acquiring shape-plan failed."); return false; } + auto *module_inst = plan->module_inst; + auto *exec_env = plan->exec_env; // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work - HB_OBJ2REF (face); HB_OBJ2REF (font); HB_OBJ2REF (buffer); - if (unlikely (!faceref || !fontref || !bufferref)) + if (unlikely (!fontref || !bufferref)) { DEBUG_MSG (WASM, module_inst, "Failed to register objects."); goto fail; } - exec_env = wasm_runtime_create_exec_env (module_inst, stack_size); - if (unlikely (!exec_env)) { - DEBUG_MSG (WASM, module_inst, "Create wasm execution environment failed."); - goto fail; - } - - func = wasm_runtime_lookup_function (module_inst, "shape_plan_create", nullptr); - if (func) - { - wasm_val_t results[1]; - wasm_val_t arguments[1]; - - results[0].kind = WASM_I32; - arguments[0].kind = WASM_I32; - arguments[0].of.i32 = faceref; - ret = wasm_runtime_call_wasm_a (exec_env, func, - ARRAY_LENGTH (results), results, - ARRAY_LENGTH (arguments), arguments); - - if (unlikely (!ret)) - { - DEBUG_MSG (WASM, module_inst, "Calling shape_plan_create() failed: %s", - wasm_runtime_get_exception(module_inst)); - goto fail; - } - shape_planref = results[0].of.i32; - } - func = wasm_runtime_lookup_function (module_inst, "shape", nullptr); if (unlikely (!func)) { @@ -230,7 +320,7 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, results[0].kind = WASM_I32; arguments[0].kind = WASM_I32; - arguments[0].of.i32 = shape_planref; + arguments[0].of.i32 = plan->wasm_shape_planptr; arguments[1].kind = WASM_I32; arguments[1].of.i32 = fontref; arguments[2].kind = WASM_I32; @@ -248,6 +338,12 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, wasm_runtime_module_free (module_inst, arguments[2].of.i32); + if (unlikely (buffer->in_error ())) + { + DEBUG_MSG (WASM, module_inst, "Buffer in error. Memory allocation fail?"); + goto fail; + } + if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", @@ -264,31 +360,7 @@ fail: ret = false; } - if (ret && shape_planref) - { - func = wasm_runtime_lookup_function (module_inst, "shape_plan_destroy", nullptr); - if (func) - { - wasm_val_t arguments[1]; - - arguments[0].kind = WASM_I32; - arguments[0].of.i32 = shape_planref; - ret = wasm_runtime_call_wasm_a (exec_env, func, - 0, nullptr, - ARRAY_LENGTH (arguments), arguments); - - if (unlikely (!ret)) - { - DEBUG_MSG (WASM, module_inst, "Calling shape_plan_destroy() failed: %s", - wasm_runtime_get_exception(module_inst)); - } - } - } - - if (exec_env) - wasm_runtime_destroy_exec_env (exec_env); - if (module_inst) - wasm_runtime_deinstantiate (module_inst); + release_shape_plan (face_data, plan, ret); return ret; } From 2568890d15f2a271738a2ef23ca73d173bf120df Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 19:04:39 -0700 Subject: [PATCH 080/149] [wasm-shape] Retry shaping if out-of-memory --- src/hb-wasm-shape.cc | 26 +++++++++++++++++++++++--- src/wasm-graphite/shape.cc | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 05de4a20e..b43a8c9fb 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -283,10 +283,20 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, const hb_feature_t *features, unsigned int num_features) { + if (unlikely (buffer->in_error ())) + return false; + bool ret = true; hb_face_t *face = font->face; const hb_wasm_face_data_t *face_data = face->data.wasm; + bool retried = false; + if (0) + { +retry: + DEBUG_MSG (WASM, font, "Retrying..."); + } + wasm_function_inst_t func = nullptr; hb_wasm_shape_plan_t *plan = acquire_shape_plan (face, face_data); @@ -340,15 +350,25 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan, if (unlikely (buffer->in_error ())) { - DEBUG_MSG (WASM, module_inst, "Buffer in error. Memory allocation fail?"); - goto fail; + DEBUG_MSG (WASM, module_inst, "Buffer in error. Memory allocation fail in the wasm?"); + if (retried) + goto fail; + buffer->successful = true; + retried = true; + release_shape_plan (face_data, plan); + goto retry; } if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", wasm_runtime_get_exception(module_inst)); - goto fail; + if (retried) + goto fail; + buffer->successful = true; + retried = true; + release_shape_plan (face_data, plan); + goto retry; } /* TODO Regularize clusters according to direction & cluster level, diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 6aec37280..24b1b5fe1 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -83,6 +83,8 @@ shape (void *shape_plan, unsigned length = contents.length; uint32_t *chars = (uint32_t *) malloc (length * sizeof (uint32_t)); + if (!chars) + return false; for (unsigned int i = 0; i < contents.length; ++i) chars[i] = contents.info[i].codepoint; @@ -114,6 +116,8 @@ shape (void *shape_plan, buffer_contents_realloc (&contents, length); cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); + if (!clusters || !gids) + return false; memset (clusters, 0, sizeof (clusters[0]) * length); codepoint_t *pg = gids; From 16ecb96922e2f1389cd634a2b908df0a72f8ac1f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 19:53:47 -0700 Subject: [PATCH 081/149] [wasm-api] Return success from buffer_contents_realloc --- src/hb-wasm-api-buffer.hh | 38 ++++++++++++++++++++++++++------------ src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api.h | 6 +++--- src/wasm-graphite/shape.cc | 3 ++- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index a89fc3968..78d44b572 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -35,35 +35,49 @@ namespace wasm { static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); -HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents), - uint32_t size) +HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents), + uint32_t size) { HB_PTR_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) - return; + return false; if (size <= contents->length) - return; + return true; unsigned bytes; if (hb_unsigned_mul_overflows (size, sizeof (glyph_info_t), &bytes)) - return; + return false; // TODO bounds check? uint32_t infoptr = contents->info; uint32_t posptr = contents->pos; + const char *info = (const char *) addr_app_to_native (infoptr); const char *pos = (const char *) addr_app_to_native (posptr); - contents->info = wasm_runtime_module_dup_data (module_inst, info, bytes); - contents->pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); + uint32_t new_info = wasm_runtime_module_dup_data (module_inst, info, bytes); + uint32_t new_pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); - module_free (infoptr); - module_free (posptr); + if (likely (new_info)) + { + contents->info = new_info; + module_free (infoptr); + } + if (likely (new_pos)) + { + contents->pos = new_pos; + module_free (posptr); + } - // TODO Check success - contents->length = size; + if (likely (new_info && new_pos)) + { + contents->length = size; + return true; + } + + return false; } HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 8d05f2914..14ba384fa 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -58,7 +58,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* buffer */ NATIVE_SYMBOL ("(i)", buffer_contents_free), - NATIVE_SYMBOL ("(ii)", buffer_contents_realloc), + NATIVE_SYMBOL ("(ii)i", buffer_contents_realloc), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 515883566..ea3aa6190 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -140,9 +140,9 @@ typedef struct ptr_t(glyph_position_t) pos; } buffer_contents_t; -HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents), - uint32_t size); +HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents), + uint32_t size); HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV ptr_d(buffer_contents_t, contents)); diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 24b1b5fe1..80ef1c500 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -113,7 +113,8 @@ shape (void *shape_plan, }; length = glyph_count; - buffer_contents_realloc (&contents, length); + if (!buffer_contents_realloc (&contents, length)) + return false; cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); if (!clusters || !gids) From 77f0f3c11a5b9e2d5bbf20a6e2840c19822bc134 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 20:01:22 -0700 Subject: [PATCH 082/149] [wasm-api] Make buffer_copy_contents return zero length on mem fail --- src/hb-wasm-api-buffer.hh | 4 ++-- src/hb-wasm-shape.cc | 15 ++------------- src/wasm-graphite/shape.cc | 2 ++ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 78d44b572..91b3a8720 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -109,8 +109,8 @@ HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV ret.info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); - if (unlikely (buffer->successful && (!ret.info || !ret.pos))) - buffer->successful = false; + if (!ret.info || !ret.pos) + ret.length = 0; } HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index b43a8c9fb..a44ef5e23 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -348,21 +348,10 @@ retry: wasm_runtime_module_free (module_inst, arguments[2].of.i32); - if (unlikely (buffer->in_error ())) - { - DEBUG_MSG (WASM, module_inst, "Buffer in error. Memory allocation fail in the wasm?"); - if (retried) - goto fail; - buffer->successful = true; - retried = true; - release_shape_plan (face_data, plan); - goto retry; - } - - if (unlikely (!ret)) + if (unlikely (!ret || !results[0].of.i32)) { DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", - wasm_runtime_get_exception(module_inst)); + wasm_runtime_get_exception (module_inst)); if (retried) goto fail; buffer->successful = true; diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 80ef1c500..d4abc735a 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -75,6 +75,8 @@ shape (void *shape_plan, } buffer_contents_t contents = buffer_copy_contents (buffer); + if (!contents.length) + return false; gr_segment *seg = nullptr; const gr_slot *is; From 4bdfaeecefae4136b9ceb451138f8055c7b74d02 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 20:05:35 -0700 Subject: [PATCH 083/149] [wasm-shape] Set glyph flags --- src/hb-wasm-shape.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index a44ef5e23..601fb4454 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -371,6 +371,12 @@ fail: release_shape_plan (face_data, plan, ret); + if (ret) + { + buffer->clear_glyph_flags (); + buffer->unsafe_to_break (); + } + return ret; } From 74deaa9e7888f1433c053802877911cb643221f9 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 13:33:49 +0000 Subject: [PATCH 084/149] [wasm-api] Restructure samples/libraries --- src/{wasm-sample => wasm/sample}/addTable.py | 0 src/{wasm-sample => wasm/sample/c}/Makefile | 0 .../sample/c}/shape-fallback.cc | 0 src/{wasm-sample => wasm/sample/c}/shape-ot.cc | 0 src/{wasm-sample => wasm/sample/c}/test.ttf | Bin 5 files changed, 0 insertions(+), 0 deletions(-) rename src/{wasm-sample => wasm/sample}/addTable.py (100%) rename src/{wasm-sample => wasm/sample/c}/Makefile (100%) rename src/{wasm-sample => wasm/sample/c}/shape-fallback.cc (100%) rename src/{wasm-sample => wasm/sample/c}/shape-ot.cc (100%) rename src/{wasm-sample => wasm/sample/c}/test.ttf (100%) diff --git a/src/wasm-sample/addTable.py b/src/wasm/sample/addTable.py similarity index 100% rename from src/wasm-sample/addTable.py rename to src/wasm/sample/addTable.py diff --git a/src/wasm-sample/Makefile b/src/wasm/sample/c/Makefile similarity index 100% rename from src/wasm-sample/Makefile rename to src/wasm/sample/c/Makefile diff --git a/src/wasm-sample/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc similarity index 100% rename from src/wasm-sample/shape-fallback.cc rename to src/wasm/sample/c/shape-fallback.cc diff --git a/src/wasm-sample/shape-ot.cc b/src/wasm/sample/c/shape-ot.cc similarity index 100% rename from src/wasm-sample/shape-ot.cc rename to src/wasm/sample/c/shape-ot.cc diff --git a/src/wasm-sample/test.ttf b/src/wasm/sample/c/test.ttf similarity index 100% rename from src/wasm-sample/test.ttf rename to src/wasm/sample/c/test.ttf From 514a8d58d8820b7b21e2f25933bc9b0d6ea70672 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 14:42:13 +0000 Subject: [PATCH 085/149] [wasm-api] Add ergonomic Rust interface --- src/wasm/rust/harfbuzz-wasm/Cargo.toml | 8 + src/wasm/rust/harfbuzz-wasm/src/lib.rs | 364 +++++++++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 src/wasm/rust/harfbuzz-wasm/Cargo.toml create mode 100644 src/wasm/rust/harfbuzz-wasm/src/lib.rs diff --git a/src/wasm/rust/harfbuzz-wasm/Cargo.toml b/src/wasm/rust/harfbuzz-wasm/Cargo.toml new file mode 100644 index 000000000..0dfb3e808 --- /dev/null +++ b/src/wasm/rust/harfbuzz-wasm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "harfbuzz-wasm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs new file mode 100644 index 000000000..b65538c7f --- /dev/null +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -0,0 +1,364 @@ +#![warn(missing_docs)] +#![allow(dead_code)] +//! Interface to Harfbuzz's WASM exports +//! +//! This crate is designed to make it easier to write a +//! WASM shaper for your font using Rust. It binds the +//! functions exported by Harfbuzz into the WASM runtime, +//! and wraps them in an ergonomic interface using Rust +//! structures. For example, here is a basic shaping engine: +//! +//! +//! ```rust +//! #[wasm_bindgen] +//! pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { +//! let font = Font::from_ref(font_ref); +//! let mut buffer = GlyphBuffer::from_ref(buf_ref); +//! for mut item in buffer.glyphs.iter_mut() { +//! // Map character to glyph +//! item.codepoint = font.get_glyph(codepoint, 0); +//! // Set advance width +//! item.h_advance = font.get_glyph_h_advance(item.codepoint); +//! } +//! 1 +//! } +//! ``` +use std::{ + ffi::{c_int, CStr, CString}, + mem, +}; + +// We don't use #[wasm_bindgen] here because that makes +// assumptions about Javascript calling conventions. We +// really do just want to import some C symbols and run +// them in unsafe-land! +extern "C" { + fn face_get_upem(face: u32) -> u32; + fn font_get_face(font: u32) -> u32; + fn font_get_glyph(font: u32, unicode: u32, uvs: u32) -> u32; + fn font_get_scale(font: u32, x_scale: *mut i32, y_scale: *mut i32); + fn font_get_glyph_extents(font: u32, glyph: u32, extents: *mut CGlyphExtents) -> bool; + fn font_glyph_to_string(font: u32, glyph: u32, str: *const u8, len: u32); + fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; + fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; + fn face_reference_table(font: u32, tag: u32) -> Blob; + fn buffer_copy_contents(buffer: u32) -> CBufferContents; + fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; + fn debugprint(s: *const u8); + fn shape_with( + font: u32, + buffer: u32, + features: u32, + num_features: u32, + shaper: *const u8, + ) -> i32; +} + +/// An opaque reference to a font at a given size and +/// variation. It is equivalent to the `hb_font_t` pointer +/// in Harfbuzz. +#[derive(Debug)] +pub struct Font(u32); + +impl Font { + /// Initialize a `Font` struct from the reference provided + /// by Harfbuzz to the `shape` function. + pub fn from_ref(ptr: u32) -> Self { + Self(ptr) + } + /// Call the given Harfbuzz shaper on a buffer reference. + /// + /// For example, `font.shape_with(buffer_ref, "ot")` will + /// run standard OpenType shaping, allowing you to modify + /// the buffer contents after glyph mapping, substitution + /// and positioning has taken place. + pub fn shape_with(&self, buffer_ref: u32, shaper: &str) { + let c_shaper = CString::new(shaper).unwrap(); + unsafe { + shape_with(self.0, buffer_ref, 0, 0, c_shaper.as_ptr() as *const u8); + } + } + + /// Return the font face object that this font belongs to. + pub fn get_face(&self) -> Face { + Face(unsafe { font_get_face(self.0) }) + } + + /// Map a Unicode codepoint to a glyph ID. + /// + /// The `uvs` parameter specifies a Unicode Variation + /// Selector codepoint which is used in conjunction with + /// [format 14 cmap tables](https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-14-unicode-variation-sequences) + /// to provide alternate glyph mappings for characters with + /// Unicode Variation Sequences. Generally you will pass + /// `0`. + pub fn get_glyph(&self, unicode: u32, uvs: u32) -> u32 { + unsafe { font_get_glyph(self.0, unicode, uvs) } + } + + /// Get the extents for a given glyph ID, in its design position. + pub fn get_glyph_extents(&self, glyph: u32) -> CGlyphExtents { + let mut extents = CGlyphExtents::default(); + unsafe { + font_get_glyph_extents(self.0, glyph, &mut extents); + } + extents + } + + /// Get the default advance width for a given glyph ID. + pub fn get_glyph_h_advance(&self, glyph: u32) -> i32 { + unsafe { font_get_glyph_h_advance(self.0, glyph) } + } + + /// Get the default vertical advance for a given glyph ID. + fn get_glyph_v_advance(&self, glyph: u32) -> i32 { + unsafe { font_get_glyph_v_advance(self.0, glyph) } + } + + /// Get the name of a glyph. + /// + /// If no names are provided by the font, names of the form + /// `gidXXX` are constructed. + pub fn get_glyph_name(&self, glyph: u32) -> String { + let mut s = [1u8; 32]; + unsafe { + font_glyph_to_string(self.0, glyph, s.as_mut_ptr(), 32); + } + unsafe { CStr::from_ptr(s.as_ptr() as *const _) } + .to_str() + .unwrap() + .to_string() + } + + /// Get the X and Y scale factor applied to this font. + /// + /// This should be divided by the units per em value to + /// provide a scale factor mapping from design units to + /// user units. (See [`Face::get_upem`].) + pub fn get_scale(&self) -> (i32, i32) { + let mut x_scale: i32 = 0; + let mut y_scale: i32 = 0; + unsafe { + font_get_scale( + self.0, + &mut x_scale as *mut c_int, + &mut y_scale as *mut c_int, + ) + }; + (x_scale, y_scale) + } +} + +/// An opaque reference to a font face, equivalent to the `hb_face_t` pointer +/// in Harfbuzz. +/// +/// This is generally returned from [`Font::get_face`]. +#[derive(Debug)] +pub struct Face(u32); + +impl Face { + /// Get a blob containing the contents of the given binary font table. + pub fn reference_table(&self, tag: &str) -> Blob { + let mut tag_u: u32 = 0; + let mut chars = tag.chars(); + tag_u |= (chars.next().unwrap() as u32) << 24; + tag_u |= (chars.next().unwrap() as u32) << 16; + tag_u |= (chars.next().unwrap() as u32) << 8; + tag_u |= chars.next().unwrap() as u32; + unsafe { face_reference_table(self.0, tag_u) } + } + + /// Get the face's design units per em. + pub fn get_upem(&self) -> u32 { + unsafe { face_get_upem(self.0) } + } +} + +/// Trait implemented by custom structs representing buffer items +pub trait BufferItem { + /// Construct an item in your preferred representation out of the info and position data provided by Harfbuzz. + fn from_c(info: CGlyphInfo, position: CGlyphPosition) -> Self; + /// Return info and position data to Harfbuzz. + fn to_c(self) -> (CGlyphInfo, CGlyphPosition); +} + +/// Generic representation of a Harfbuzz buffer item. +/// +/// By making this generic, we allow you to implement your own +/// representations of buffer items; for example, in your shaper, +/// you may want certain fields to keep track of the glyph's name, +/// extents, or shape, so you would want a custom struct to represent +/// buffer items. If you don't care about any of them, use the +/// supplied `GlyphBuffer` struct. +#[derive(Debug)] +pub struct Buffer { + _ptr: u32, + /// Glyphs in the buffer + pub glyphs: Vec, +} + +impl Buffer { + /// Construct a buffer from the pointer Harfbuzz provides to the WASM. + /// + /// The `Buffer` struct implements Drop, meaning that when the shaping + /// function is finished, the buffer contents are sent back to Harfbuzz. + pub fn from_ref(ptr: u32) -> Self { + let c_contents: CBufferContents = unsafe { buffer_copy_contents(ptr) }; + let positions: Vec = unsafe { + std::slice::from_raw_parts(c_contents.position, c_contents.length as usize).to_vec() + }; + let infos: Vec = unsafe { + std::slice::from_raw_parts(c_contents.info, c_contents.length as usize).to_vec() + }; + Buffer { + glyphs: infos + .into_iter() + .zip(positions.into_iter()) + .map(|(i, p)| T::from_c(i, p)) + .collect(), + _ptr: ptr, + } + } +} + +impl Drop for Buffer { + fn drop(&mut self) { + let mut positions: Vec; + let mut infos: Vec; + let glyphs = mem::take(&mut self.glyphs); + (infos, positions) = glyphs.into_iter().map(|g| g.to_c()).unzip(); + let c_contents = CBufferContents { + length: positions.len() as u32, + info: infos[..].as_mut_ptr(), + position: positions[..].as_mut_ptr(), + }; + unsafe { + buffer_set_contents(self._ptr, &c_contents); + } + } +} + +/// Some data provided by Harfbuzz. +#[derive(Debug)] +#[repr(C)] +pub struct Blob { + /// Length of the blob in bytes + pub length: u32, + /// A raw pointer to the contents + pub data: *mut u8, +} + +/// Glyph information in a buffer item provided by Harfbuzz +/// +/// You'll only need to interact with this if you're writing +/// your own buffer item structure. +#[repr(C)] +#[derive(Debug, Clone)] +pub struct CGlyphInfo { + pub codepoint: u32, + pub mask: u32, + pub cluster: u32, + pub var1: u32, + pub var2: u32, +} + +/// Glyph positioning information in a buffer item provided by Harfbuzz +/// +/// You'll only need to interact with this if you're writing +/// your own buffer item structure. +#[derive(Debug, Clone)] +#[repr(C)] +pub struct CGlyphPosition { + pub x_advance: i32, + pub y_advance: i32, + pub x_offset: i32, + pub y_offset: i32, + pub var: u32, +} + +/// Glyph extents +#[derive(Debug, Clone, Default)] +#[repr(C)] +pub struct CGlyphExtents { + /// The scaled left side bearing of the glyph + pub x_bearing: i32, + /// The scaled coordinate of the top of the glyph + pub y_bearing: i32, + /// The width of the glyph + pub width: i32, + /// The height of the glyph + pub height: i32, +} + +#[derive(Debug)] +#[repr(C)] +struct CBufferContents { + length: u32, + info: *mut CGlyphInfo, + position: *mut CGlyphPosition, +} + +/// Ergonomic representation of a Harfbuzz buffer item +/// +/// Harfbuzz buffers are normally split into two arrays, +/// one representing glyph information and the other +/// representing glyph positioning. In Rust, this would +/// require lots of zipping and unzipping, so we zip them +/// together into a single structure for you. +pub struct Glyph { + /// The Unicode codepoint or glyph ID of the item + pub codepoint: u32, + /// The index of the cluster in the input text where this came from + pub cluster: u32, + /// The horizontal advance of the glyph + pub x_advance: i32, + /// The vertical advance of the glyph + pub y_advance: i32, + /// The horizontal offset of the glyph + pub x_offset: i32, + /// The vertical offset of the glyph + pub y_offset: i32, + /// You can use this for whatever you like + pub flags: u32, +} +impl BufferItem for Glyph { + fn from_c(info: CGlyphInfo, pos: CGlyphPosition) -> Self { + Self { + codepoint: info.codepoint, + cluster: info.cluster, + x_advance: pos.x_advance, + y_advance: pos.y_advance, + x_offset: pos.x_offset, + y_offset: pos.y_offset, + flags: 0, + } + } + fn to_c(self) -> (CGlyphInfo, CGlyphPosition) { + let info = CGlyphInfo { + codepoint: self.codepoint, + cluster: self.cluster, + mask: 0, + var1: 0, + var2: 0, + }; + let pos = CGlyphPosition { + x_advance: self.x_advance, + y_advance: self.y_advance, + x_offset: self.x_offset, + y_offset: self.y_offset, + var: 0, + }; + (info, pos) + } +} + +/// Our default buffer item struct. See also [`Glyph`]. +pub type GlyphBuffer = Buffer; + +/// Write a string to the Harfbuzz debug log. +pub fn debug(s: &str) { + let c_s = CString::new(s).unwrap(); + unsafe { + debugprint(c_s.as_ptr() as *const u8); + }; +} From 4ad659a6088d4b86599a3334a9c0f7ceeec8c79e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 07:48:35 -0700 Subject: [PATCH 086/149] [wasm] Move wasm-graphite --- src/{wasm-graphite => wasm/graphite}/Makefile | 0 src/{wasm-graphite => wasm/graphite}/addTable.py | 0 src/{wasm-graphite => wasm/graphite}/shape.cc | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/{wasm-graphite => wasm/graphite}/Makefile (100%) rename src/{wasm-graphite => wasm/graphite}/addTable.py (100%) rename src/{wasm-graphite => wasm/graphite}/shape.cc (100%) diff --git a/src/wasm-graphite/Makefile b/src/wasm/graphite/Makefile similarity index 100% rename from src/wasm-graphite/Makefile rename to src/wasm/graphite/Makefile diff --git a/src/wasm-graphite/addTable.py b/src/wasm/graphite/addTable.py similarity index 100% rename from src/wasm-graphite/addTable.py rename to src/wasm/graphite/addTable.py diff --git a/src/wasm-graphite/shape.cc b/src/wasm/graphite/shape.cc similarity index 100% rename from src/wasm-graphite/shape.cc rename to src/wasm/graphite/shape.cc From 1023a80d395318e444458d0eebccab363133d5ef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 07:57:04 -0700 Subject: [PATCH 087/149] [wasm] Fix up samples --- src/{wasm/graphite => }/addTable.py | 0 src/wasm/graphite/Makefile | 17 ++++++++++------- src/wasm/sample/addTable.py | 16 ---------------- src/wasm/sample/c/Makefile | 14 ++++++++------ 4 files changed, 18 insertions(+), 29 deletions(-) rename src/{wasm/graphite => }/addTable.py (100%) delete mode 100644 src/wasm/sample/addTable.py diff --git a/src/wasm/graphite/addTable.py b/src/addTable.py similarity index 100% rename from src/wasm/graphite/addTable.py rename to src/addTable.py diff --git a/src/wasm/graphite/Makefile b/src/wasm/graphite/Makefile index 593212cc1..6584df96d 100644 --- a/src/wasm/graphite/Makefile +++ b/src/wasm/graphite/Makefile @@ -1,8 +1,11 @@ -all: CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf AwamiNastaliq-Regular.wasm.ttf +FONTS = CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf AwamiNastaliq-Regular.wasm.ttf +ADD_TABLE = ../../addTable.py -%.wasm: %.cc ../hb-wasm-api.h +all: $(FONTS) + +%.wasm: %.cc ../../hb-wasm-api.h emcc \ - -I .. \ + -I ../.. \ -I ~/graphite/include/ \ -fvisibility=hidden \ -Wl,--allow-undefined \ @@ -13,10 +16,10 @@ all: CharisSIL-R.wasm.ttf Scheherazade-R.wasm.ttf AwamiNastaliq-Regular.wasm.ttf $< \ -o $@ -%.wasm.ttf: %.ttf shape.wasm addTable.py - python addTable.py $< $@ shape.wasm +%.wasm.ttf: %.ttf shape.wasm $(ADD_TABLE) + python $(ADD_TABLE) $< $@ shape.wasm clean: - $(RM) test.wasm.ttf shape.wasm + $(RM) shape.wasm $(FONTS) -.PRECIOUS: shape.wasm +.PRECIOUS: shap.wasm diff --git a/src/wasm/sample/addTable.py b/src/wasm/sample/addTable.py deleted file mode 100644 index 103f292dd..000000000 --- a/src/wasm/sample/addTable.py +++ /dev/null @@ -1,16 +0,0 @@ -import sys -from fontTools.ttLib import TTFont -from fontTools.ttLib.tables.DefaultTable import DefaultTable - -if len(sys.argv) == 1: - print("usage: python addTable.py input.ttf output.ttf Wasm.bin") - sys.exit(1) - -font = TTFont(sys.argv[1]) - -wasm_table = DefaultTable("Wasm") -wasm_table.data = open(sys.argv[3], "rb").read() - -font["Wasm"] = wasm_table - -font.save(sys.argv[2]) diff --git a/src/wasm/sample/c/Makefile b/src/wasm/sample/c/Makefile index 60606e359..4ee073b64 100644 --- a/src/wasm/sample/c/Makefile +++ b/src/wasm/sample/c/Makefile @@ -1,21 +1,23 @@ +ADD_TABLE = ../../../addTable.py + all: test-fallback.wasm.ttf test-ot.wasm.ttf -%.wasm: %.cc ../hb-wasm-api.h +%.wasm: %.cc ../../../hb-wasm-api.h clang \ --target=wasm32-unknown-wasi \ -Wl,--no-entry \ -fvisibility=hidden \ -Wl,--allow-undefined \ -nostdlib \ - -I .. \ + -I ../../.. \ $< \ -o $@ -test-fallback.wasm.ttf: test.ttf shape-fallback.wasm addTable.py - python addTable.py $< $@ shape-fallback.wasm +test-fallback.wasm.ttf: test.ttf shape-fallback.wasm $(ADD_TABLE) + python $(ADD_TABLE) $< $@ shape-fallback.wasm -test-ot.wasm.ttf: test.ttf shape-ot.wasm addTable.py - python addTable.py $< $@ shape-ot.wasm +test-ot.wasm.ttf: test.ttf shape-ot.wasm $(ADD_TABLE) + python $(ADD_TABLE) $< $@ shape-ot.wasm clean: $(RM) test-fallback.wasm.ttf test-ot.wasm.ttf shape-fallback.wasm shape-ot.wasm From b5b577f29fd20016fab21b454403510d1098f132 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 08:12:16 -0700 Subject: [PATCH 088/149] [wasm-api] Make buffer_copy_contents return success --- src/hb-wasm-api-buffer.hh | 24 ++++++++++++++++-------- src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api.h | 5 +++-- src/wasm/graphite/shape.cc | 4 ++-- src/wasm/sample/c/shape-fallback.cc | 4 +++- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 91b3a8720..6be1fe122 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -95,22 +95,30 @@ HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV contents->length = 0; } -HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(buffer_t, buffer)) +HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer), + ptr_d(buffer_contents_t, contents)) { - HB_RETURN_STRUCT (buffer_contents_t, ret); HB_REF2OBJ (buffer); + HB_PTR_PARAM (buffer_contents_t, contents); + if (unlikely (!contents)) + return false; if (buffer->have_output) buffer->sync (); unsigned length = buffer->len; - ret.length = length; - ret.info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); - ret.pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); + contents->length = length; + contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); + contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); - if (!ret.info || !ret.pos) - ret.length = 0; + if (length && (!contents->info || !contents->pos)) + { + contents->length = 0; + return false; + } + + return true; } HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 14ba384fa..bd267bec3 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -59,7 +59,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* buffer */ NATIVE_SYMBOL ("(i)", buffer_contents_free), NATIVE_SYMBOL ("(ii)i", buffer_contents_realloc), - NATIVE_SYMBOL ("(ii)", buffer_copy_contents), + NATIVE_SYMBOL ("(ii)i", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), NATIVE_SYMBOL ("(i)i", buffer_get_script), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index ea3aa6190..4282d2cf0 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -149,8 +149,9 @@ HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV typedef struct buffer_t buffer_t; -HB_WASM_API_COMPOUND (buffer_contents_t, buffer_copy_contents) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(buffer_t, buffer)); +HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer), + ptr_d(buffer_contents_t, contents)); HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer), diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index d4abc735a..a11ad672d 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -74,8 +74,8 @@ shape (void *shape_plan, direction = DIRECTION_REVERSE (direction); } - buffer_contents_t contents = buffer_copy_contents (buffer); - if (!contents.length) + buffer_contents_t contents; + if (!buffer_copy_contents (buffer, &contents)) return false; gr_segment *seg = nullptr; diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index a2a7090d6..d554b11e3 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -23,7 +23,9 @@ shape (void *shape_plan, blob_free (&blob); - buffer_contents_t contents = buffer_copy_contents (buffer); + buffer_contents_t contents; + if (!buffer_copy_contents (buffer, &contents)) + return false; debugprint1 ("buffer length", contents.length); From 28a7c1f93247d692475ded0ea4dd6d0b5dd94044 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 08:16:31 -0700 Subject: [PATCH 089/149] [wasm-api] Rename face_reference_table to face_copy_table --- src/hb-wasm-api-face.hh | 6 +++--- src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api.h | 6 +++--- src/wasm/graphite/shape.cc | 2 +- src/wasm/rust/harfbuzz-wasm/src/lib.rs | 4 ++-- src/wasm/sample/c/shape-fallback.cc | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 1d891dadf..5f38710a8 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -31,9 +31,9 @@ namespace hb { namespace wasm { -HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(face_t, face), - tag_t table_tag) +HB_WASM_API_COMPOUND (blob_t, face_copy_table) (HB_WASM_EXEC_ENV_COMPOUND + ptr_d(face_t, face), + tag_t table_tag) { HB_RETURN_STRUCT (blob_t, ret); HB_REF2OBJ (face); diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index bd267bec3..65b982ef2 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -67,7 +67,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), /* face */ - NATIVE_SYMBOL ("(iii)", face_reference_table), + NATIVE_SYMBOL ("(iii)", face_copy_table), NATIVE_SYMBOL ("(i)i", face_get_upem), /* font */ diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 4282d2cf0..8f0748938 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -173,9 +173,9 @@ HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV typedef struct face_t face_t; -HB_WASM_API_COMPOUND (blob_t, face_reference_table) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(face_t, face), - tag_t table_tag); +HB_WASM_API_COMPOUND (blob_t, face_copy_table) (HB_WASM_EXEC_ENV_COMPOUND + ptr_d(face_t, face), + tag_t table_tag); HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV ptr_d(face_t, face)); diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index a11ad672d..7a5ca71d9 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -21,7 +21,7 @@ uint32_t heap_size = 2 * 1024 * 1024; static const void *copy_table (const void *data, unsigned int tag, size_t *len) { face_t *face = (face_t *) data; - blob_t blob = face_reference_table (face, tag); + blob_t blob = face_copy_table (face, tag); *len = blob.length; return blob.data; diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index b65538c7f..4b34e1d2d 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -41,7 +41,7 @@ extern "C" { fn font_glyph_to_string(font: u32, glyph: u32, str: *const u8, len: u32); fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; - fn face_reference_table(font: u32, tag: u32) -> Blob; + fn face_copy_table(font: u32, tag: u32) -> Blob; fn buffer_copy_contents(buffer: u32) -> CBufferContents; fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; fn debugprint(s: *const u8); @@ -165,7 +165,7 @@ impl Face { tag_u |= (chars.next().unwrap() as u32) << 16; tag_u |= (chars.next().unwrap() as u32) << 8; tag_u |= chars.next().unwrap() as u32; - unsafe { face_reference_table(self.0, tag_u) } + unsafe { face_copy_table(self.0, tag_u) } } /// Get the face's design units per em. diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index d554b11e3..ecfb6948a 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -17,7 +17,7 @@ shape (void *shape_plan, { face_t *face = font_get_face (font); - blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); + blob_t blob = face_copy_table (face, TAG ('c','m','a','p')); debugprint1 ("cmap length", blob.length); From 840b5dff734e566b258fc3a1ee556739af31b45c Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 15:20:39 +0000 Subject: [PATCH 090/149] [wasm] Improve Rust docs, refer to new crate --- docs/wasm-shaper.md | 107 +++++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 36 deletions(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index 0bf1a0b90..07705a454 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -4,37 +4,39 @@ If the standard OpenType shaping engine doesn't give you enough flexibility, Har ## How to write a shaping engine in Rust -Here are the steps to create an example shaping engine in Rust: +Here are the steps to create an example shaping engine in Rust: (These examples can also be found in `src/wasm/sample/rust`) * First, install wasm-pack, which helps us to generate optimized WASM files. It writes some Javascript bridge code that we don't need, but it makes the build and deployment process much easier: ``` -cargo install wasm-pack +$ cargo install wasm-pack ``` * Now let's create a new library: ``` -cargo new --lib my-shaper +$ cargo new --lib hello-wasm ``` -* We need the target to be a dynamic library, and we're going to use `bindgen` to export our Rust function to WASM, so let's put these lines in the `Cargo.toml`: +* We need the target to be a dynamic library, and we're going to use `bindgen` to export our Rust function to WASM, so let's put these lines in the `Cargo.toml`. The Harfbuzz sources contain a Rust crate which makes it easy to create the shaper, so we'll specify that as a dependency as well: ```toml [lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" +harfbuzz-wasm = { path = "your-harfbuzz-source/src/wasm/rust/harfbuzz-wasm"} ``` +* * And now we'll create our shaper code. In `src/lib.rs`: -``` +```rust use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(_font_ref: u32, _buf_ref: u32) -> i32 { - 1 +pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { + 1 // success! } ``` @@ -45,7 +47,7 @@ This exports a shaping function which takes two arguments, tokens representing t ``` INFO]: 🎯 Checking for the Wasm target... [INFO]: 🌀 Compiling to Wasm... - Compiling my-shaper v0.1.0 (...) + Compiling hello-wasm v0.1.0 (...) Finished release [optimized] target(s) in 0.20s [WARN]: ⚠️ origin crate has no README [INFO]: ⬇️ Installing wasm-bindgen... @@ -54,14 +56,14 @@ INFO]: 🎯 Checking for the Wasm target... [INFO]: ✨ Done in 0.40s ``` -You'll find the output WASM file in `pkg/my_shaper_bg.wasm` +You'll find the output WASM file in `pkg/hello_wasm_bg.wasm` * Now we need to get it into a font. -One way to do this is to use [otfsurgeon](https://github.com/simoncozens/font-engineering/blob/master/otfsurgeon), a tool for manipulating binary font tables: +We provide a utility to do this called `addTable.py` in the `src/` directory: ``` -% otfsurgeon -i test.ttf add -o test-wasm.ttf Wasm < pkg/my_shaper_bg.wasm +% python3 ~/harfbuzz/src/addTable.py test.ttf test-wasm.ttf pkg/hello_wasm_bg.wasm ``` And now we can run it! @@ -77,41 +79,74 @@ Congratulations! Our shaper did nothing, but in Rust! Now let's do something - i * To say hello world, we're going to have to use a native function. -In debugging builds of Harfbuzz, we can print some output from the web assembly module to the host's standard output using the `debugprint` function: +In debugging builds of Harfbuzz, we can print some output from the web assembly module to the host's standard output using the `debug` function. To make this easier, we've got the `harfbuzz-wasm` crate: -``` -// We don't use #[wasm_bindgen] here because that makes -// assumptions about Javascript calling conventions. We -// really do just want to import some C symbols and run -// them in unsafe-land! -extern "C" { - pub fn debugprint(s: *const u8); -} -``` - -And now let's add a function on the Rust side which makes this a bit more ergonomic to use: - -``` -use std::ffi::CString; - -fn print(s: &str) { - let c_s = CString::new(s).unwrap(); - unsafe { - debugprint(c_s.as_ptr() as *const u8); - }; -} +```rust +use harfbuzz_wasm::debug; #[wasm_bindgen] -pub fn shape(font_ref: i32, buf_ref: i32) -> i32 { - print("Hello from Rust!\n"); +pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { + debug("Hello from Rust!\n"); 1 } ``` -With this compiled into a WASM module, and installed into our font again, finally our fonts can talk to us!s +With this compiled into a WASM module, and installed into our font again, finally our fonts can talk to us! ``` $ hb-shape test-wasm.ttf abc Hello from Rust! [cent=0|sterling=1|fraction=2] ``` + +Now let's start to do some actual, you know, *shaping*. The first thing a shaping engine normally does is (a) map the items in the buffer from Unicode codepoints into glyphs in the font, and (b) set the advance width of the buffer items to the default advance width for those glyphs. We're going to need to interrogate the font for this information, and write back to the buffer. Harfbuzz provides us with opaque pointers to the memory for the font and buffer, but we can turn those into useful Rust structures using the `harfbuzz-wasm` crate again: + +```rust +use wasm_bindgen::prelude::*; +use harfbuzz_wasm::{Font, GlyphBuffer}; + +#[wasm_bindgen] +pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { + let font = Font::from_ref(font_ref); + let mut buffer = GlyphBuffer::from_ref(buf_ref); + for mut item in buffer.glyphs.iter_mut() { + // Map character to glyph + item.codepoint = font.get_glyph(codepoint, 0); + // Set advance width + item.h_advance = font.get_glyph_h_advance(item.codepoint); + } + 1 +} +``` + +The `GlyphBuffer`, unlike in Harfbuzz, combines positioning and information in a single structure, to save you having to zip and unzip all the time. It also takes care of marshalling the buffer back to Harfbuzz-land; when a GlyphBuffer is dropped, it writes its contents back through the reference into Harfbuzz's address space. (If you want a different representation of buffer items, you can have one: `GlyphBuffer` is implemented as a `Buffer`, and if you make your own struct which implements the `BufferItem` trait, you can make a buffer out of that instead.) + +One easy way to write your own shapers is to make use of OpenType shaping for the majority of your shaping work, and then make changes to the pre-shaped buffer afterwards. You can do this using the `Font.shape_with` method. Run this on a buffer reference, and then construct your `GlyphBuffer` object afterwards: + +```rust +use harfbuzz_wasm::{Font, GlyphBuffer}; +use tiny_rng::{Rand, Rng}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { + let mut rng = Rng::from_seed(123456); + + // Use the default OpenType shaper + let font = Font::from_ref(font_ref); + font.shape_with(buf_ref, "ot"); + + // Now we have a buffer with glyph ids, advance widths etc. + // already filled in. + let mut buffer = GlyphBuffer::from_ref(buf_ref); + for mut item in buffer.glyphs.iter_mut() { + // Randomize it! + item.x_offset = ((rng.rand_u32() as i32) >> 24) - 120; + item.y_offset = ((rng.rand_u32() as i32) >> 24) - 120; + } + + 1 +} +``` + +See the documentation for the `harfbuzz-wasm` crate for all the other From 83b9c34f0b5d9b6b6f65aae0e1bb92877972a5ed Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 15:23:22 +0000 Subject: [PATCH 091/149] [wasm] Add rust example --- src/wasm/sample/rust/hello-wasm/Cargo.toml | 13 +++++++++++++ src/wasm/sample/rust/hello-wasm/src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/wasm/sample/rust/hello-wasm/Cargo.toml create mode 100644 src/wasm/sample/rust/hello-wasm/src/lib.rs diff --git a/src/wasm/sample/rust/hello-wasm/Cargo.toml b/src/wasm/sample/rust/hello-wasm/Cargo.toml new file mode 100644 index 000000000..e8207ed35 --- /dev/null +++ b/src/wasm/sample/rust/hello-wasm/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "hello-wasm" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +#externref = "0.1.0" +wasm-bindgen = "0.2.0" +tiny-rng = "0.2.0" +harfbuzz-wasm = { path="../../rust/harfbuzz-wasm"} diff --git a/src/wasm/sample/rust/hello-wasm/src/lib.rs b/src/wasm/sample/rust/hello-wasm/src/lib.rs new file mode 100644 index 000000000..223f2699c --- /dev/null +++ b/src/wasm/sample/rust/hello-wasm/src/lib.rs @@ -0,0 +1,18 @@ +use harfbuzz_wasm::{Font, GlyphBuffer}; +use tiny_rng::{Rand, Rng}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { + let mut rng = Rng::from_seed(123456); + let font = Font::from_ref(font_ref); + font.shape_with(buf_ref, "ot"); + let mut buffer = GlyphBuffer::from_ref(buf_ref); + for mut item in buffer.glyphs.iter_mut() { + // Randomize it! + item.x_offset = ((rng.rand_u32() as i32) >> 24) - 120; + item.y_offset = ((rng.rand_u32() as i32) >> 24) - 120; + } + // Buffer is written back to HB on drop + 1 +} From 2004528cf8375b82c687e8b9eb12d485d14427f1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 08:23:14 -0700 Subject: [PATCH 092/149] [wasm-api] Change face_copy_table to return success --- src/hb-wasm-api-face.hh | 29 ++++++++++++++++++++--------- src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api.h | 7 ++++--- src/wasm/graphite/shape.cc | 4 +++- src/wasm/sample/c/shape-fallback.cc | 5 ++++- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 5f38710a8..ce767c030 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -31,22 +31,33 @@ namespace hb { namespace wasm { -HB_WASM_API_COMPOUND (blob_t, face_copy_table) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(face_t, face), - tag_t table_tag) +HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV + ptr_d(face_t, face), + tag_t table_tag, + ptr_d(blob_t, blob)) { - HB_RETURN_STRUCT (blob_t, ret); HB_REF2OBJ (face); + HB_PTR_PARAM (blob_t, blob); + if (unlikely (!blob)) + return false; - hb_blob_t *blob = hb_face_reference_table (face, table_tag); + hb_blob_t *hb_blob = hb_face_reference_table (face, table_tag); unsigned length; - const char *data = hb_blob_get_data (blob, &length); + const char *data = hb_blob_get_data (hb_blob, &length); - ret.length = length; - ret.data = wasm_runtime_module_dup_data (module_inst, data, length); + blob->length = length; + blob->data = wasm_runtime_module_dup_data (module_inst, data, length); - hb_blob_destroy (blob); + hb_blob_destroy (hb_blob); + + if (blob->length && !blob->data) + { + blob->length = 0; + return false; + } + + return true; } HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 65b982ef2..545b2f098 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -67,7 +67,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), /* face */ - NATIVE_SYMBOL ("(iii)", face_copy_table), + NATIVE_SYMBOL ("(iii)i", face_copy_table), NATIVE_SYMBOL ("(i)i", face_get_upem), /* font */ diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 8f0748938..43327cd35 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -173,9 +173,10 @@ HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV typedef struct face_t face_t; -HB_WASM_API_COMPOUND (blob_t, face_copy_table) (HB_WASM_EXEC_ENV_COMPOUND - ptr_d(face_t, face), - tag_t table_tag); +HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV + ptr_d(face_t, face), + tag_t table_tag, + ptr_d(blob_t, blob)); HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV ptr_d(face_t, face)); diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index 7a5ca71d9..94a896975 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -21,7 +21,9 @@ uint32_t heap_size = 2 * 1024 * 1024; static const void *copy_table (const void *data, unsigned int tag, size_t *len) { face_t *face = (face_t *) data; - blob_t blob = face_copy_table (face, tag); + blob_t blob; + if (!face_copy_table (face, tag, &blob)) + abort (); *len = blob.length; return blob.data; diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index ecfb6948a..85834a1c1 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -3,6 +3,7 @@ #include extern "C" { +void abort (); void debugprint (const char *s); void debugprint1 (const char *s, int32_t); void debugprint2 (const char *s, int32_t, int32_t); @@ -17,7 +18,9 @@ shape (void *shape_plan, { face_t *face = font_get_face (font); - blob_t blob = face_copy_table (face, TAG ('c','m','a','p')); + blob_t blob; + if (!face_copy_table (face, TAG ('c','m','a','p'), &blob)) + abort (); debugprint1 ("cmap length", blob.length); From 0d237d062e0826138769c405bb1c04c9ed8da247 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 08:32:35 -0700 Subject: [PATCH 093/149] [wasm-shape] No need to set default runnint mode It's detected automatically apparently. --- src/hb-wasm-shape.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 601fb4454..305db2910 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -62,11 +62,6 @@ init_wasm () RuntimeInitArgs init_args; memset (&init_args, 0, sizeof (RuntimeInitArgs)); - if (wasm_runtime_is_running_mode_supported (Mode_LLVM_JIT)) - init_args.running_mode = Mode_LLVM_JIT; - else if (wasm_runtime_is_running_mode_supported (Mode_Fast_JIT)) - init_args.running_mode = Mode_Fast_JIT; - init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; From db789eacb41ecd74ee3c32d245e7e15640c58198 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 15:35:37 +0000 Subject: [PATCH 094/149] [wasm-rust] Docs and API update --- docs/wasm-shaper.md | 8 ++++---- src/wasm/rust/harfbuzz-wasm/src/lib.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index 07705a454..ad72a1f90 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -35,12 +35,12 @@ harfbuzz-wasm = { path = "your-harfbuzz-source/src/wasm/rust/harfbuzz-wasm"} use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { +pub fn shape(_shape_plan: u32, _font_ref: u32, _buf_ref: u32, _features: u32) -> i32 { 1 // success! } ``` -This exports a shaping function which takes two arguments, tokens representing the font and the buffer, and returns a status value. We can pass these tokens back to Harfbuzz in order to use its native functions on the font and buffer objects. More on native functions later - let's get this shaper compiled and added into a font: +This exports a shaping function which takes four arguments, tokens representing the shaping plan, the font and the buffer, and returns a status value. We can pass these tokens back to Harfbuzz in order to use its native functions on the font and buffer objects. More on native functions later - let's get this shaper compiled and added into a font: * To compile the shaper, run `wasm-pack build --target nodejs`: @@ -85,7 +85,7 @@ In debugging builds of Harfbuzz, we can print some output from the web assembly use harfbuzz_wasm::debug; #[wasm_bindgen] -pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { +pub fn shape(shape_plan, font_ref: u32, buf_ref: u32, _features: u32) -> i32 { debug("Hello from Rust!\n"); 1 } @@ -106,7 +106,7 @@ use wasm_bindgen::prelude::*; use harfbuzz_wasm::{Font, GlyphBuffer}; #[wasm_bindgen] -pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { +pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32) -> i32 { let font = Font::from_ref(font_ref); let mut buffer = GlyphBuffer::from_ref(buf_ref); for mut item in buffer.glyphs.iter_mut() { diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index 4b34e1d2d..3fe1fa545 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -42,7 +42,7 @@ extern "C" { fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; fn face_copy_table(font: u32, tag: u32) -> Blob; - fn buffer_copy_contents(buffer: u32) -> CBufferContents; + fn buffer_copy_contents(buffer: u32, cbuffer: *mut CBufferContents) -> bool; fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; fn debugprint(s: *const u8); fn shape_with( @@ -203,7 +203,15 @@ impl Buffer { /// The `Buffer` struct implements Drop, meaning that when the shaping /// function is finished, the buffer contents are sent back to Harfbuzz. pub fn from_ref(ptr: u32) -> Self { - let c_contents: CBufferContents = unsafe { buffer_copy_contents(ptr) }; + let mut c_contents = CBufferContents { + info: std::ptr::null_mut(), + position: std::ptr::null_mut(), + length: 0, + }; + + unsafe { + buffer_copy_contents(ptr, &mut c_contents) || panic!("Couldn't copy buffer contents") + }; let positions: Vec = unsafe { std::slice::from_raw_parts(c_contents.position, c_contents.length as usize).to_vec() }; From 92a57b4b4aa60620040dd5a31dcb764643da9496 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 15:38:08 +0000 Subject: [PATCH 095/149] [wasm-rust] update font_copy_table API --- src/wasm/rust/harfbuzz-wasm/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index 3fe1fa545..3be27f614 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -41,7 +41,7 @@ extern "C" { fn font_glyph_to_string(font: u32, glyph: u32, str: *const u8, len: u32); fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; - fn face_copy_table(font: u32, tag: u32) -> Blob; + fn face_copy_table(font: u32, tag: u32, blob: *mut Blob) -> bool; fn buffer_copy_contents(buffer: u32, cbuffer: *mut CBufferContents) -> bool; fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; fn debugprint(s: *const u8); @@ -165,7 +165,14 @@ impl Face { tag_u |= (chars.next().unwrap() as u32) << 16; tag_u |= (chars.next().unwrap() as u32) << 8; tag_u |= chars.next().unwrap() as u32; - unsafe { face_copy_table(self.0, tag_u) } + let mut blob = Blob { + data: std::ptr::null_mut(), + length: 0, + }; + unsafe { + face_copy_table(self.0, tag_u, &mut blob); + } + blob } /// Get the face's design units per em. From 65966e0c3da0fdb5a59abcc76533dfceccbc1425 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 08:59:03 -0700 Subject: [PATCH 096/149] [wasm-api] Add font_copy_glyph_outline --- src/hb-wasm-api-font.hh | 46 +++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 1 + src/hb-wasm-api.h | 36 ++++++++++++++++++++-- src/wasm/sample/c/shape-fallback.cc | 4 +++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index e3e5453d7..d1401c7e0 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -27,6 +27,8 @@ #include "hb-wasm-api.hh" +#include "hb-outline.hh" + namespace hb { namespace wasm { @@ -109,6 +111,50 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV hb_font_glyph_to_string (font, glyph, s, size); } +static_assert (sizeof (glyph_outline_point_t) == sizeof (hb_outline_point_t), ""); +static_assert (sizeof (uint32_t) == sizeof (hb_outline_t::contours[0]), ""); + +HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph, + ptr_d(glyph_outline_t, outline)) +{ + HB_REF2OBJ (font); + HB_PTR_PARAM (glyph_outline_t, outline); + if (unlikely (!outline)) + return false; + + hb_outline_t hb_outline; + auto *funcs = hb_outline_recording_pen_get_funcs (); + + hb_font_draw_glyph (font, glyph, funcs, &hb_outline); + + if (unlikely (hb_outline.points.in_error () || + hb_outline.contours.in_error ())) + { + outline->n_points = outline->n_contours = 0; + return false; + } + + outline->n_points = hb_outline.points.length; + outline->points = wasm_runtime_module_dup_data (module_inst, + (const char *) hb_outline.points.arrayZ, + hb_outline.points.get_size ()); + outline->n_contours = hb_outline.contours.length; + outline->contours = wasm_runtime_module_dup_data (module_inst, + (const char *) hb_outline.contours.arrayZ, + hb_outline.contours.get_size ()); + + if ((outline->n_points && !outline->points) || + (!outline->n_contours && !outline->contours)) + { + outline->n_points = outline->n_contours = 0; + return false; + } + + return true; +} + }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 545b2f098..4e5172450 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -78,6 +78,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)i", font_get_glyph_v_advance), NATIVE_SYMBOL ("(iii)i", font_get_glyph_extents), NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), + NATIVE_SYMBOL ("(iii)i", font_copy_glyph_outline), /* shape */ NATIVE_SYMBOL ("(iiii$)i", shape_with), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 43327cd35..5600d431b 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -207,7 +207,8 @@ HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV ptr_d(font_t, font), codepoint_t glyph); -typedef struct { +typedef struct +{ position_t x_bearing; position_t y_bearing; position_t width; @@ -225,9 +226,40 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV char *s, uint32_t size); +/* outline */ + +enum glyph_outline_point_type_t +{ + MOVE_TO, + LINE_TO, + QUADRATIC_TO, + CUBIC_TO, +}; + +typedef struct +{ + float x; + float y; + uint32_t type; +} glyph_outline_point_t; + +typedef struct +{ + uint32_t n_points; + ptr_t(glyph_outline_point_t) points; + uint32_t n_contours; + ptr_t(uint32_t) contours; +} glyph_outline_t; + +HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + codepoint_t glyph, + ptr_d(glyph_outline_t, outline)); + /* shape */ -typedef struct { +typedef struct +{ tag_t tag; uint32_t value; uint32_t start; diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 85834a1c1..335331c2c 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -42,6 +42,10 @@ shape (void *shape_plan, contents.info[i].codepoint = font_get_glyph (font, contents.info[i].codepoint, 0); contents.pos[i].x_advance = font_get_glyph_h_advance (font, contents.info[i].codepoint); + + glyph_outline_t outline; + font_copy_glyph_outline (font, contents.info[i].codepoint, &outline); + debugprint1 ("num outline contours", outline.n_contours); } bool_t ret = buffer_set_contents (buffer, &contents); From e7540043de3f38c4f442e696b569bcc6af90587f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 09:18:40 -0700 Subject: [PATCH 097/149] [wasm-api] Make buffer_copy_contents reuse contents --- src/hb-wasm-api-buffer.hh | 26 +++++++++++++++++++++++++- src/hb-wasm-api.h | 1 + src/wasm/graphite/shape.cc | 2 +- src/wasm/sample/c/shape-fallback.cc | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 6be1fe122..8feb0ee94 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -106,8 +106,30 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV if (buffer->have_output) buffer->sync (); + if (!buffer->have_positions) + buffer->clear_positions (); unsigned length = buffer->len; + + if (length <= contents->length) + { + unsigned bytes = length * sizeof (hb_glyph_info_t); + + glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr); + glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr); + + if (unlikely (!info || !pos)) + { + contents->length = 0; + return false; + } + + memcpy (info, buffer->info, bytes); + memcpy (pos, buffer->pos, bytes); + + return true; + } + contents->length = length; contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); @@ -141,7 +163,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr); glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr); - buffer->clear_positions (); /* This is wasteful, but we need it to set have_positions=true. */ + if (!buffer->have_positions) + buffer->clear_positions (); /* This is wasteful. */ + memcpy (buffer->info, info, bytes); memcpy (buffer->pos, pos, bytes); buffer->len = length; diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 5600d431b..6c9714c44 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -139,6 +139,7 @@ typedef struct ptr_t(glyph_info_t) info; ptr_t(glyph_position_t) pos; } buffer_contents_t; +#define BUFFER_CONTENTS_INIT {0, 0, 0} HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV ptr_d(buffer_contents_t, contents), diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index 94a896975..70d966d62 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -76,7 +76,7 @@ shape (void *shape_plan, direction = DIRECTION_REVERSE (direction); } - buffer_contents_t contents; + buffer_contents_t contents = BUFFER_CONTENTS_INIT; if (!buffer_copy_contents (buffer, &contents)) return false; diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 335331c2c..5bf220928 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -26,7 +26,7 @@ shape (void *shape_plan, blob_free (&blob); - buffer_contents_t contents; + buffer_contents_t contents = BUFFER_CONTENTS_INIT; if (!buffer_copy_contents (buffer, &contents)) return false; From f2d227ad9f03038ddbdb70b5a45adb29a19f5fb0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 09:30:40 -0700 Subject: [PATCH 098/149] [wasm-api] Respect existing blob allocation in face_copy_blob --- src/hb-wasm-api-face.hh | 19 +++++++++++++++++-- src/hb-wasm-api.h | 1 + src/wasm/graphite/shape.cc | 2 +- src/wasm/sample/c/shape-fallback.cc | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index ce767c030..ece557f46 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -44,10 +44,25 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV hb_blob_t *hb_blob = hb_face_reference_table (face, table_tag); unsigned length; - const char *data = hb_blob_get_data (hb_blob, &length); + const char *hb_data = hb_blob_get_data (hb_blob, &length); + + if (length <= blob->length) + { + char *data = (char *) (validate_app_addr (blob->data, blob->length) ? addr_app_to_native (blob->data) : nullptr); + + if (unlikely (!data)) + { + blob->length = 0; + return false; + } + + memcpy (data, hb_data, length); + + return true; + } blob->length = length; - blob->data = wasm_runtime_module_dup_data (module_inst, data, length); + blob->data = wasm_runtime_module_dup_data (module_inst, hb_data, length); hb_blob_destroy (hb_blob); diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 6c9714c44..0b09f4de9 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -108,6 +108,7 @@ typedef struct uint32_t length; ptr_t(char) data; } blob_t; +#define BLOB_INIT {0, 0} HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)); diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index 70d966d62..6214a1525 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -21,7 +21,7 @@ uint32_t heap_size = 2 * 1024 * 1024; static const void *copy_table (const void *data, unsigned int tag, size_t *len) { face_t *face = (face_t *) data; - blob_t blob; + blob_t blob = BLOB_INIT; if (!face_copy_table (face, tag, &blob)) abort (); diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 5bf220928..004be0ef0 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -18,7 +18,7 @@ shape (void *shape_plan, { face_t *face = font_get_face (font); - blob_t blob; + blob_t blob = BLOB_INIT; if (!face_copy_table (face, TAG ('c','m','a','p'), &blob)) abort (); From b08026187ab7fbec7f8d6d4adca0a8dc9d02d74b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 09:34:03 -0700 Subject: [PATCH 099/149] [wasm-api] Memory house-keeping --- src/hb-wasm-api-buffer.hh | 2 ++ src/hb-wasm-api-face.hh | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 8feb0ee94..20e9b484c 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -130,6 +130,8 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV return true; } + buffer_contents_free (exec_env, contentsptr); + contents->length = length; contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index ece557f46..2a458f6df 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -61,6 +61,8 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV return true; } + blob_free (exec_env, blobptr); + blob->length = length; blob->data = wasm_runtime_module_dup_data (module_inst, hb_data, length); From 6746ca4ae2aa0623a56676eb77bb63975e1016b0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 09:41:20 -0700 Subject: [PATCH 100/149] [wasm] Minor remove undefine function --- src/wasm/sample/c/shape-fallback.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 004be0ef0..0a2ab9e09 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -3,7 +3,6 @@ #include extern "C" { -void abort (); void debugprint (const char *s); void debugprint1 (const char *s, int32_t); void debugprint2 (const char *s, int32_t, int32_t); @@ -19,8 +18,7 @@ shape (void *shape_plan, face_t *face = font_get_face (font); blob_t blob = BLOB_INIT; - if (!face_copy_table (face, TAG ('c','m','a','p'), &blob)) - abort (); + face_copy_table (face, TAG ('c','m','a','p'), &blob); debugprint1 ("cmap length", blob.length); From 41362cc339d2e6e547291cf73dcc147fe39966d1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:08:22 -0700 Subject: [PATCH 101/149] [wasm] Simplify memory cleaning --- src/hb-wasm-api-buffer.hh | 3 ++- src/hb-wasm-api-face.hh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 20e9b484c..1a1057373 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -130,7 +130,8 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV return true; } - buffer_contents_free (exec_env, contentsptr); + module_free (contents->info); + module_free (contents->pos); contents->length = length; contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 2a458f6df..d496c2f1a 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -61,7 +61,7 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV return true; } - blob_free (exec_env, blobptr); + module_free (blob->data); blob->length = length; blob->data = wasm_runtime_module_dup_data (module_inst, hb_data, length); From 7fff4a19ad0dc598f9eae271d2314003b2e44df5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:25:19 -0700 Subject: [PATCH 102/149] [wasm-api] Reuse allocation in font_copy_glyph_outline --- src/hb-wasm-api-font.hh | 19 +++++++++++++++++++ src/hb-wasm-api.h | 1 + src/wasm/sample/c/shape-fallback.cc | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index d1401c7e0..4c641e971 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -136,6 +136,25 @@ HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV return false; } + // TODO Check two buffers separately + if (hb_outline.points.length <= outline->n_points && + hb_outline.contours.length <= outline->n_contours) + { + glyph_outline_point_t *points = (glyph_outline_point_t *) (validate_app_addr (outline->points, hb_outline.points.get_size ()) ? addr_app_to_native (outline->points) : nullptr); + uint32_t *contours = (uint32_t *) (validate_app_addr (outline->contours, hb_outline.contours.get_size ()) ? addr_app_to_native (outline->contours) : nullptr); + + if (unlikely (!points || !contours)) + { + outline->n_points = outline->n_contours = 0; + return false; + } + + memcpy (points, hb_outline.points.arrayZ, hb_outline.points.get_size ()); + memcpy (contours, hb_outline.contours.arrayZ, hb_outline.contours.get_size ()); + + return true; + } + outline->n_points = hb_outline.points.length; outline->points = wasm_runtime_module_dup_data (module_inst, (const char *) hb_outline.points.arrayZ, diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 0b09f4de9..9d8e9168f 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -252,6 +252,7 @@ typedef struct uint32_t n_contours; ptr_t(uint32_t) contours; } glyph_outline_t; +#define GLYPH_OUTLINE_INIT {0, 0, 0, 0} HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV ptr_d(font_t, font), diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 0a2ab9e09..9bb3a688f 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -41,7 +41,7 @@ shape (void *shape_plan, contents.info[i].codepoint = font_get_glyph (font, contents.info[i].codepoint, 0); contents.pos[i].x_advance = font_get_glyph_h_advance (font, contents.info[i].codepoint); - glyph_outline_t outline; + glyph_outline_t outline = GLYPH_OUTLINE_INIT; font_copy_glyph_outline (font, contents.info[i].codepoint, &outline); debugprint1 ("num outline contours", outline.n_contours); } From 918df8ccaf48041994e3b107ed7d2f32d643ff03 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:29:03 -0700 Subject: [PATCH 103/149] [wasm-api] Add glyph_outline_free --- src/hb-wasm-api-font.hh | 16 ++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.h | 4 ++++ src/wasm/sample/c/shape-fallback.cc | 8 +++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 4c641e971..10f1a30c2 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -174,6 +174,22 @@ HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV return true; } +HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV + ptr_d(glyph_outline_t, outline)) +{ + HB_PTR_PARAM (glyph_outline_t, outline); + if (unlikely (!outline)) + return; + + module_free (outline->points); + module_free (outline->contours); + + outline->n_points = 0; + outline->points = nullref; + outline->n_contours = 0; + outline->contours = nullref; +} + }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 4e5172450..2f0ede178 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -80,6 +80,9 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), NATIVE_SYMBOL ("(iii)i", font_copy_glyph_outline), + /* outline */ + NATIVE_SYMBOL ("(i)", glyph_outline_free), + /* shape */ NATIVE_SYMBOL ("(iiii$)i", shape_with), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 9d8e9168f..94cf58fd4 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -254,11 +254,15 @@ typedef struct } glyph_outline_t; #define GLYPH_OUTLINE_INIT {0, 0, 0, 0} +HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV + ptr_d(glyph_outline_t, outline)); + HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV ptr_d(font_t, font), codepoint_t glyph, ptr_d(glyph_outline_t, outline)); + /* shape */ typedef struct diff --git a/src/wasm/sample/c/shape-fallback.cc b/src/wasm/sample/c/shape-fallback.cc index 9bb3a688f..7787bbae7 100644 --- a/src/wasm/sample/c/shape-fallback.cc +++ b/src/wasm/sample/c/shape-fallback.cc @@ -30,10 +30,14 @@ shape (void *shape_plan, debugprint1 ("buffer length", contents.length); + glyph_outline_t outline = GLYPH_OUTLINE_INIT; + for (unsigned i = 0; i < contents.length; i++) { char name[64]; + debugprint1 ("glyph at", i); + font_glyph_to_string (font, contents.info[i].codepoint, name, sizeof (name)); debugprint (name); @@ -41,11 +45,13 @@ shape (void *shape_plan, contents.info[i].codepoint = font_get_glyph (font, contents.info[i].codepoint, 0); contents.pos[i].x_advance = font_get_glyph_h_advance (font, contents.info[i].codepoint); - glyph_outline_t outline = GLYPH_OUTLINE_INIT; font_copy_glyph_outline (font, contents.info[i].codepoint, &outline); + debugprint1 ("num outline points", outline.n_points); debugprint1 ("num outline contours", outline.n_contours); } + glyph_outline_free (&outline); + bool_t ret = buffer_set_contents (buffer, &contents); buffer_contents_free (&contents); From 1537e252bad82ea03b2a1550d2464bddc24e6e49 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:35:15 -0700 Subject: [PATCH 104/149] [wasm] Minor blob validation --- src/hb-wasm-api-face.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index d496c2f1a..9ef6476a7 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -48,7 +48,7 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV if (length <= blob->length) { - char *data = (char *) (validate_app_addr (blob->data, blob->length) ? addr_app_to_native (blob->data) : nullptr); + char *data = (char *) (validate_app_addr (blob->data, length) ? addr_app_to_native (blob->data) : nullptr); if (unlikely (!data)) { From 4260de12c1bb271033d9cd3b0f47a4cf77190bf3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:43:27 -0700 Subject: [PATCH 105/149] [wasm] Add HB_ARRAY_APP2NATIVE --- src/hb-wasm-api-buffer.hh | 7 +++---- src/hb-wasm-api-face.hh | 2 +- src/hb-wasm-api-font.hh | 4 ++-- src/hb-wasm-api.hh | 5 +++++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 1a1057373..fd0d1dd95 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -113,10 +113,8 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV if (length <= contents->length) { - unsigned bytes = length * sizeof (hb_glyph_info_t); - - glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr); - glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr); + glyph_info_t *info = HB_ARRAY_APP2NATIVE (glyph_info_t, contents->info, length); + glyph_position_t *pos = HB_ARRAY_APP2NATIVE (glyph_position_t, contents->pos, length); if (unlikely (!info || !pos)) { @@ -124,6 +122,7 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV return false; } + unsigned bytes = length * sizeof (hb_glyph_info_t); memcpy (info, buffer->info, bytes); memcpy (pos, buffer->pos, bytes); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 9ef6476a7..e528e1f8c 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -48,7 +48,7 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV if (length <= blob->length) { - char *data = (char *) (validate_app_addr (blob->data, length) ? addr_app_to_native (blob->data) : nullptr); + char *data = HB_ARRAY_APP2NATIVE (char, blob->data, length); if (unlikely (!data)) { diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 10f1a30c2..2a5425cd0 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -140,8 +140,8 @@ HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV if (hb_outline.points.length <= outline->n_points && hb_outline.contours.length <= outline->n_contours) { - glyph_outline_point_t *points = (glyph_outline_point_t *) (validate_app_addr (outline->points, hb_outline.points.get_size ()) ? addr_app_to_native (outline->points) : nullptr); - uint32_t *contours = (uint32_t *) (validate_app_addr (outline->contours, hb_outline.contours.get_size ()) ? addr_app_to_native (outline->contours) : nullptr); + glyph_outline_point_t *points = HB_ARRAY_APP2NATIVE (glyph_outline_point_t, outline->points, hb_outline.points.length); + uint32_t *contours = HB_ARRAY_APP2NATIVE (uint32_t, outline->contours, hb_outline.contours.length); if (unlikely (!points || !contours)) { diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index f7e5565be..4ead01c1f 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -108,5 +108,10 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \ } HB_STMT_END +#define HB_ARRAY_APP2NATIVE(type, name, length) \ + ((type *) (!hb_unsigned_mul_overflows (length, sizeof (type)) && \ + validate_app_addr (name, (length) * sizeof (type)) ? \ + addr_app_to_native (name) : nullptr)) + #endif /* HB_WASM_API_HH */ From 91eb2f49dbf1e84da5e64f20a2b3c5b6f37f3c83 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:44:45 -0700 Subject: [PATCH 106/149] [wasm-api-buffer] Minor variable --- src/hb-wasm-api-buffer.hh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index fd0d1dd95..dd189463a 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -133,8 +133,9 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV module_free (contents->pos); contents->length = length; - contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, length * sizeof (buffer->info[0])); - contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, length * sizeof (buffer->pos[0])); + unsigned bytes = length * sizeof (hb_glyph_info_t); + contents->info = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->info, bytes); + contents->pos = wasm_runtime_module_dup_data (module_inst, (const char *) buffer->pos, bytes); if (length && (!contents->info || !contents->pos)) { From d894a10699c08ebf2f4854f28b6343a6b5ff7aea Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:48:41 -0700 Subject: [PATCH 107/149] [wasm-shape] Debug message --- src/hb-wasm-shape.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 305db2910..507ca1664 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -348,7 +348,10 @@ retry: DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", wasm_runtime_get_exception (module_inst)); if (retried) + { + DEBUG_MSG (WASM, font, "Giving up..."); goto fail; + } buffer->successful = true; retried = true; release_shape_plan (face_data, plan); From 65f7bac73c774e3e75efc35566b2d9466726ffd2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 11:07:31 -0700 Subject: [PATCH 108/149] Revert "[wasm-shape] No need to set default runnint mode" This reverts commit fa484fac08a868e885b3252522668824f0f0fe71. --- src/hb-wasm-shape.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 507ca1664..7c486fe51 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -62,6 +62,11 @@ init_wasm () RuntimeInitArgs init_args; memset (&init_args, 0, sizeof (RuntimeInitArgs)); + if (wasm_runtime_is_running_mode_supported (Mode_LLVM_JIT)) + init_args.running_mode = Mode_LLVM_JIT; + else if (wasm_runtime_is_running_mode_supported (Mode_Fast_JIT)) + init_args.running_mode = Mode_Fast_JIT; + init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; From f95c2cc6d5ce9db646efde99768e2847016afc38 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 11:10:28 -0700 Subject: [PATCH 109/149] [wasm] Infra for iwasm llvm build --- meson.build | 2 ++ src/meson.build | 1 + 2 files changed, 3 insertions(+) diff --git a/meson.build b/meson.build index 63ded12f9..70ba6a26c 100644 --- a/meson.build +++ b/meson.build @@ -115,6 +115,8 @@ gobject_dep = dependency('gobject-2.0', required: get_option('gobject')) graphite2_dep = dependency('graphite2', required: get_option('graphite2')) graphite_dep = dependency('graphite2', required: get_option('graphite')) wasm_dep = cpp.find_library('iwasm', required: get_option('wasm')) +# How to check whether iwasm was built, and hence requires, LLVM? +#llvm_dep = cpp.find_library('LLVM-15', required: get_option('wasm')) if meson.version().version_compare('>=0.60.0') # pkg-config: icu-uc, cmake: ICU but with components diff --git a/src/meson.build b/src/meson.build index 299634719..caf3258ae 100644 --- a/src/meson.build +++ b/src/meson.build @@ -476,6 +476,7 @@ if conf.get('HAVE_WASM', 0) == 1 hb_sources += hb_wasm_sources hb_headers += hb_wasm_headers harfbuzz_deps += wasm_dep + #harfbuzz_deps += llvm_dep endif if conf.get('HAVE_UNISCRIBE', 0) == 1 From 1f86890b7526821279c6bf2bd3ead189bf558459 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 11:55:34 -0700 Subject: [PATCH 110/149] [wasm/graphite] Typo --- src/wasm/graphite/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wasm/graphite/Makefile b/src/wasm/graphite/Makefile index 6584df96d..d509132dc 100644 --- a/src/wasm/graphite/Makefile +++ b/src/wasm/graphite/Makefile @@ -10,6 +10,7 @@ all: $(FONTS) -fvisibility=hidden \ -Wl,--allow-undefined \ -Wl,--no-entry \ + -Wl,--strip-all \ -sERROR_ON_UNDEFINED_SYMBOLS=0 \ ~/graphite/src/libgraphite2.a \ ~/wasm/wasi-sdk-19.0/share/wasi-sysroot/lib/wasm32-wasi/libc.a \ @@ -22,4 +23,4 @@ all: $(FONTS) clean: $(RM) shape.wasm $(FONTS) -.PRECIOUS: shap.wasm +.PRECIOUS: shape.wasm From d5d8fc046fbd7b758f2e31b74e1b86b06b3acf8d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:17:30 -0700 Subject: [PATCH 111/149] [wasm/graphite] Include standard headers Since we use emcc here. --- src/wasm/graphite/shape.cc | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index 6214a1525..be704b5a0 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -4,13 +4,8 @@ #include -extern "C" { -void *malloc(size_t size); -void *realloc(void *ptr, size_t size); -void free(void *ptr); -void abort(); -void *memset(void *s, int c, size_t n); -} +#include +#include void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); From faaae04359862e49d0750592d0fc1ddf8b6d4e37 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:36:57 -0700 Subject: [PATCH 112/149] [wasm-shape] Minor --- src/hb-wasm-shape.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 7c486fe51..6407244ca 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -185,7 +185,7 @@ acquire_shape_plan (hb_face_t *face, if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape_plan_create() failed: %s", - wasm_runtime_get_exception(module_inst)); + wasm_runtime_get_exception (module_inst)); goto fail; } plan->wasm_shape_planptr = results[0].of.i32; @@ -233,7 +233,7 @@ release_shape_plan (const hb_wasm_face_data_t *face_data, if (unlikely (!ret)) { DEBUG_MSG (WASM, module_inst, "Calling shape_plan_destroy() failed: %s", - wasm_runtime_get_exception(module_inst)); + wasm_runtime_get_exception (module_inst)); } } } @@ -360,6 +360,7 @@ retry: buffer->successful = true; retried = true; release_shape_plan (face_data, plan); + plan = nullptr; goto retry; } From 8215e7063232ffe3ff262c9839f1841e6d9bd7a1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:37:11 -0700 Subject: [PATCH 113/149] [wasm-graphite] Memory hygiene --- src/wasm/graphite/shape.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index be704b5a0..ca15ebc55 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -117,7 +117,11 @@ shape (void *shape_plan, cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); if (!clusters || !gids) + { + free (clusters); + free (gids); return false; + } memset (clusters, 0, sizeof (clusters[0]) * length); codepoint_t *pg = gids; From c5a88a068b933ab67ffa8c39c97ad11f2dfbd7d0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:43:22 -0700 Subject: [PATCH 114/149] [wasm-api] Minor cleanup --- src/hb-wasm-api.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 94cf58fd4..67e33e7e1 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -275,27 +275,24 @@ typedef struct #define FEATURE_GLOBAL_START 0 #define FEATURE_GLOBAL_END ((uint32_t) -1) -HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV - ptr_d(font_t, font), - ptr_d(buffer_t, buffer), - ptr_d(const feature_t, features), - uint32_t num_features, - const char *shaper); +HB_WASM_API (bool_t, shape_with) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(buffer_t, buffer), + ptr_d(const feature_t, features), + uint32_t num_features, + const char *shaper); /* Implement these in your shaper. */ -HB_WASM_INTERFACE (ptr_t(void), shape_plan_create) (HB_WASM_EXEC_ENV - ptr_d(face_t, face)); +HB_WASM_INTERFACE (ptr_t(void), shape_plan_create) (ptr_d(face_t, face)); -HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV - ptr_d(void, shape_plan), +HB_WASM_INTERFACE (bool_t, shape) (ptr_d(void, shape_plan), ptr_d(font_t, font), ptr_d(buffer_t, buffer), ptr_d(const feature_t, features), uint32_t num_features); -HB_WASM_INTERFACE (void, shape_plan_destroy) (HB_WASM_EXEC_ENV - ptr_d(void, shape_plan)); +HB_WASM_INTERFACE (void, shape_plan_destroy) (ptr_d(void, shape_plan)); HB_WASM_END_DECLS From 9b66e45481c81c9af43d3dd5c2babad2121a9f59 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:44:32 -0700 Subject: [PATCH 115/149] [wasm/graphite] Remove unused variable Wasm cannot export those? --- src/wasm/graphite/shape.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index ca15ebc55..e11044630 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -10,9 +10,6 @@ void debugprint1 (char *s, int32_t); void debugprint2 (char *s, int32_t, int32_t); -__attribute__((visibility("default"))) -uint32_t heap_size = 2 * 1024 * 1024; - static const void *copy_table (const void *data, unsigned int tag, size_t *len) { face_t *face = (face_t *) data; From 6311b72fcce18703eff35010bd20c05a748ae3e5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 12:57:57 -0700 Subject: [PATCH 116/149] [wasm-shape] Shuffle code around --- src/hb-wasm-shape.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 6407244ca..7970e4c5d 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -133,12 +133,6 @@ static hb_wasm_shape_plan_t * acquire_shape_plan (hb_face_t *face, const hb_wasm_face_data_t *face_data) { - constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; - - wasm_module_inst_t module_inst = nullptr; - wasm_exec_env_t exec_env = nullptr; - wasm_function_inst_t func = nullptr; - /* Fetch cached one if available. */ hb_wasm_shape_plan_t *plan = face_data->plan.get_acquire (); if (likely (plan && face_data->plan.cmpexch (plan, nullptr))) @@ -146,6 +140,12 @@ acquire_shape_plan (hb_face_t *face, plan = (hb_wasm_shape_plan_t *) hb_calloc (1, sizeof (hb_wasm_shape_plan_t)); + wasm_module_inst_t module_inst = nullptr; + wasm_exec_env_t exec_env = nullptr; + wasm_function_inst_t func = nullptr; + + constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; + module_inst = plan->module_inst = wasm_runtime_instantiate(face_data->wasm_module, stack_size, heap_size, nullptr, 0); From 3c599434332ba806babf4f280e491b5560ff0778 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 13:04:54 -0700 Subject: [PATCH 117/149] [wasm/graphite] Remove stale comment --- src/wasm/graphite/shape.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index e11044630..499d17be9 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -84,8 +84,6 @@ shape (void *shape_plan, for (unsigned int i = 0; i < contents.length; ++i) chars[i] = contents.info[i].codepoint; - /* TODO ensure_native_direction. */ - seg = gr_make_seg (nullptr, grface, 0, // https://github.com/harfbuzz/harfbuzz/issues/3439#issuecomment-1442650148 nullptr, From b9d4758bf9679a1ad502f0beba7db75569fbf1a4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 13:07:56 -0700 Subject: [PATCH 118/149] [wasm/graphite] Add commented out allocator export --- src/wasm/graphite/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wasm/graphite/Makefile b/src/wasm/graphite/Makefile index d509132dc..7c1f28952 100644 --- a/src/wasm/graphite/Makefile +++ b/src/wasm/graphite/Makefile @@ -17,6 +17,8 @@ all: $(FONTS) $< \ -o $@ +# -Wl,--export=malloc -Wl,--export=free \ + %.wasm.ttf: %.ttf shape.wasm $(ADD_TABLE) python $(ADD_TABLE) $< $@ shape.wasm From 196c6b6c1f8f528c5fbdce540156fb6375e03a80 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 14:05:15 -0700 Subject: [PATCH 119/149] [wasm-buffer] Fix buffer_contents_realloc --- src/hb-wasm-api-buffer.hh | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index dd189463a..93fc17373 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -50,25 +50,29 @@ HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV if (hb_unsigned_mul_overflows (size, sizeof (glyph_info_t), &bytes)) return false; - // TODO bounds check? - uint32_t infoptr = contents->info; - uint32_t posptr = contents->pos; + glyph_info_t *info = HB_ARRAY_APP2NATIVE (glyph_info_t, contents->info, contents->length); + glyph_position_t *pos = HB_ARRAY_APP2NATIVE (glyph_position_t, contents->pos, contents->length); - const char *info = (const char *) addr_app_to_native (infoptr); - const char *pos = (const char *) addr_app_to_native (posptr); + if (unlikely (!info || !pos)) + return false; - uint32_t new_info = wasm_runtime_module_dup_data (module_inst, info, bytes); - uint32_t new_pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); + glyph_info_t *new_info = nullptr; + uint32_t new_inforef = module_malloc (bytes, (void **) &new_info); + glyph_position_t *new_pos = nullptr; + uint32_t new_posref = module_malloc (bytes, (void **) &new_pos); - if (likely (new_info)) + unsigned old_bytes = contents->length * sizeof (glyph_info_t); + if (likely (new_inforef)) { - contents->info = new_info; - module_free (infoptr); + memcpy (new_info, info, old_bytes); + module_free (contents->info); + contents->info = new_inforef; } - if (likely (new_pos)) + if (likely (new_posref)) { - contents->pos = new_pos; - module_free (posptr); + memcpy (new_pos, pos, old_bytes); + module_free (contents->pos); + contents->pos = new_posref; } if (likely (new_info && new_pos)) From 7b1c5e86aea4babcecb572a9b69f04cf95f5d102 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 14:18:09 -0700 Subject: [PATCH 120/149] [wasm-shape] Minor conditionalize allocation --- src/hb-wasm-shape.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 7970e4c5d..adf24b9a6 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -336,9 +336,9 @@ retry: arguments[2].kind = WASM_I32; arguments[2].of.i32 = bufferref; arguments[3].kind = WASM_I32; - arguments[3].of.i32 = wasm_runtime_module_dup_data (module_inst, - (const char *) features, - num_features * sizeof (features[0])); + arguments[3].of.i32 = num_features ? wasm_runtime_module_dup_data (module_inst, + (const char *) features, + num_features * sizeof (features[0])) : 0; arguments[4].kind = WASM_I32; arguments[4].of.i32 = num_features; @@ -346,7 +346,8 @@ retry: ARRAY_LENGTH (results), results, ARRAY_LENGTH (arguments), arguments); - wasm_runtime_module_free (module_inst, arguments[2].of.i32); + if (num_features) + wasm_runtime_module_free (module_inst, arguments[2].of.i32); if (unlikely (!ret || !results[0].of.i32)) { From 4096115b48e88881a73218b15e4f83ea128abbad Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 14:22:03 -0700 Subject: [PATCH 121/149] [wasm-graphite] Export malloc/free Makes the wasm-micro-runtime use these instead of internal heap. --- src/wasm/graphite/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm/graphite/Makefile b/src/wasm/graphite/Makefile index 7c1f28952..db40901fe 100644 --- a/src/wasm/graphite/Makefile +++ b/src/wasm/graphite/Makefile @@ -13,11 +13,11 @@ all: $(FONTS) -Wl,--strip-all \ -sERROR_ON_UNDEFINED_SYMBOLS=0 \ ~/graphite/src/libgraphite2.a \ + -Wl,--export=malloc -Wl,--export=free \ ~/wasm/wasi-sdk-19.0/share/wasi-sysroot/lib/wasm32-wasi/libc.a \ $< \ -o $@ -# -Wl,--export=malloc -Wl,--export=free \ %.wasm.ttf: %.ttf shape.wasm $(ADD_TABLE) python $(ADD_TABLE) $< $@ shape.wasm From 3226b4342b88e44764393a6a9e1af458d70fa5fe Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 15:03:03 -0700 Subject: [PATCH 122/149] [wasm-shape] Use hb allocators --- src/hb-wasm-shape.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index adf24b9a6..acc75ba34 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -72,8 +72,6 @@ init_wasm () init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; init_args.mem_alloc_option.allocator.free_func = (void *) hb_free; - init_args.mem_alloc_type = Alloc_With_System_Allocator; - // Native symbols need below registration phase init_args.n_native_symbols = ARRAY_LENGTH (_hb_wasm_native_symbols); init_args.native_module_name = "env"; From a53690a937bc7c4273e5f46a7f87ac8945ff282b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 15:10:10 -0700 Subject: [PATCH 123/149] [wasm-shape] Minor rename --- src/hb-wasm-shape.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index acc75ba34..66d301772 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -53,7 +53,7 @@ struct hb_wasm_face_data_t { }; static bool -init_wasm () +_hb_wasm_init () { static bool initialized; if (initialized) @@ -99,7 +99,7 @@ _hb_wasm_shaper_face_data_create (hb_face_t *face) if (!length) goto fail; - if (!init_wasm ()) + if (!_hb_wasm_init ()) goto fail; wasm_module = wasm_runtime_load ((uint8_t *) hb_blob_get_data_writable (wasm_blob, nullptr), From 2482bb120b8f29c830fe1c05d9b6bc75a651eae1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 15:14:25 -0700 Subject: [PATCH 124/149] [wasm-shape] Add XXX item --- src/hb-wasm-shape.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 66d301772..5ad1b483a 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -55,6 +55,8 @@ struct hb_wasm_face_data_t { static bool _hb_wasm_init () { + /* XXX Umm. Make this threadsafe. How?! */ + static bool initialized; if (initialized) return true; From 4f537df67de6d1475de5caf14d9cfd9886621dc1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 15:29:35 -0700 Subject: [PATCH 125/149] [wasm] Disable check-libstdc++ test Since libiwasm.so links to it. --- src/meson.build | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/meson.build b/src/meson.build index caf3258ae..78b4ed0ed 100644 --- a/src/meson.build +++ b/src/meson.build @@ -930,7 +930,10 @@ if get_option('tests').enabled() env.set('HBHEADERS', ' '.join(HBHEADERS)) if cpp.get_argument_syntax() != 'msvc' and not meson.is_cross_build() # ensure the local tools are usable - dist_check_script += ['check-libstdc++', 'check-static-inits', 'check-symbols'] + dist_check_script += ['check-static-inits', 'check-symbols'] + if get_option('wasm').disabled() + dist_check_script += ['check-libstdc++'] + endif endif foreach name : dist_check_script From 0c90555e5932817f7c3d820c5883172dd86844a9 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Feb 2023 13:55:17 +0000 Subject: [PATCH 126/149] [wasm] Fix Rust docs --- docs/wasm-shaper.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index ad72a1f90..842634356 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -35,7 +35,7 @@ harfbuzz-wasm = { path = "your-harfbuzz-source/src/wasm/rust/harfbuzz-wasm"} use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(_shape_plan: u32, _font_ref: u32, _buf_ref: u32, _features: u32) -> i32 { +pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32, _num_features: u32) -> i32 { 1 // success! } ``` @@ -85,7 +85,7 @@ In debugging builds of Harfbuzz, we can print some output from the web assembly use harfbuzz_wasm::debug; #[wasm_bindgen] -pub fn shape(shape_plan, font_ref: u32, buf_ref: u32, _features: u32) -> i32 { +pub fn shape(_shape_plan:u32, _font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { debug("Hello from Rust!\n"); 1 } @@ -106,7 +106,7 @@ use wasm_bindgen::prelude::*; use harfbuzz_wasm::{Font, GlyphBuffer}; #[wasm_bindgen] -pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32) -> i32 { +pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32, _num_features: u32) -> i32 { let font = Font::from_ref(font_ref); let mut buffer = GlyphBuffer::from_ref(buf_ref); for mut item in buffer.glyphs.iter_mut() { @@ -129,7 +129,7 @@ use tiny_rng::{Rand, Rng}; use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { +pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32, _num_features: u32) -> i32 { let mut rng = Rng::from_seed(123456); // Use the default OpenType shaper From 52b11546c8b76206b97fd49d65ae2e850a7212d3 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Feb 2023 13:57:00 +0000 Subject: [PATCH 127/149] [wasm-rust] Optional kurbo dependency --- src/wasm/rust/harfbuzz-wasm/Cargo.toml | 1 + src/wasm/rust/harfbuzz-wasm/src/lib.rs | 92 ++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/wasm/rust/harfbuzz-wasm/Cargo.toml b/src/wasm/rust/harfbuzz-wasm/Cargo.toml index 0dfb3e808..fa17de36b 100644 --- a/src/wasm/rust/harfbuzz-wasm/Cargo.toml +++ b/src/wasm/rust/harfbuzz-wasm/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +kurbo = { version = "0.9.0", optional = true } diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index 3be27f614..61e9da9bf 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -23,10 +23,10 @@ //! 1 //! } //! ``` -use std::{ - ffi::{c_int, CStr, CString}, - mem, -}; +use std::ffi::{c_int, CStr, CString}; + +#[cfg(feature = "kurbo")] +use kurbo::BezPath; // We don't use #[wasm_bindgen] here because that makes // assumptions about Javascript calling conventions. We @@ -41,6 +41,7 @@ extern "C" { fn font_glyph_to_string(font: u32, glyph: u32, str: *const u8, len: u32); fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; + fn font_copy_glyph_outline(font: u32, glyph: u32, outline: *mut CGlyphOutline) -> bool; fn face_copy_table(font: u32, tag: u32, blob: *mut Blob) -> bool; fn buffer_copy_contents(buffer: u32, cbuffer: *mut CBufferContents) -> bool; fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; @@ -147,6 +148,61 @@ impl Font { }; (x_scale, y_scale) } + + #[cfg(feature = "kurbo")] + /// Get the outline of a glyph as a vector of bezier paths + pub fn get_outline(&self, glyph: u32) -> Vec { + let mut outline = CGlyphOutline { + n_points: 0, + points: std::ptr::null_mut(), + n_contours: 0, + contours: std::ptr::null_mut(), + }; + let end_pts_of_contours: &[usize] = unsafe { + font_copy_glyph_outline(self.0, glyph, &mut outline); + std::slice::from_raw_parts(outline.contours, outline.n_contours as usize) + }; + let points: &[CGlyphOutlinePoint] = + unsafe { std::slice::from_raw_parts(outline.points, outline.n_points as usize) }; + let mut results: Vec = vec![]; + let mut start_pt: usize = 0; + for end_pt in end_pts_of_contours { + let this_contour = &points[start_pt..*end_pt]; + start_pt = *end_pt; + let mut path = BezPath::new(); + let mut ix = 0; + while ix < this_contour.len() { + let point = &this_contour[ix]; + match point.pointtype { + PointType::MoveTo => path.move_to((point.x as f64, point.y as f64)), + PointType::LineTo => path.line_to((point.x as f64, point.y as f64)), + PointType::QuadraticTo => { + ix += 1; + let end_pt = &this_contour[ix]; + path.quad_to( + (point.x as f64, point.y as f64), + (end_pt.x as f64, end_pt.y as f64), + ); + } + PointType::CubicTo => { + ix += 1; + let mid_pt = &this_contour[ix]; + ix += 1; + let end_pt = &this_contour[ix]; + path.curve_to( + (point.x as f64, point.y as f64), + (mid_pt.x as f64, mid_pt.y as f64), + (end_pt.x as f64, end_pt.y as f64), + ); + } + } + ix += 1; + } + path.close_path(); + results.push(path); + } + results + } } /// An opaque reference to a font face, equivalent to the `hb_face_t` pointer @@ -240,7 +296,7 @@ impl Drop for Buffer { fn drop(&mut self) { let mut positions: Vec; let mut infos: Vec; - let glyphs = mem::take(&mut self.glyphs); + let glyphs = std::mem::take(&mut self.glyphs); (infos, positions) = glyphs.into_iter().map(|g| g.to_c()).unzip(); let c_contents = CBufferContents { length: positions.len() as u32, @@ -367,6 +423,32 @@ impl BufferItem for Glyph { } } +#[repr(C)] +#[allow(clippy::enum_variant_names)] +#[derive(Clone, Debug)] +enum PointType { + MoveTo, + LineTo, + QuadraticTo, + CubicTo, +} + +#[repr(C)] +#[derive(Clone, Debug)] +struct CGlyphOutlinePoint { + x: f32, + y: f32, + pointtype: PointType, +} + +#[repr(C)] +struct CGlyphOutline { + n_points: usize, + points: *mut CGlyphOutlinePoint, + n_contours: usize, + contours: *mut usize, +} + /// Our default buffer item struct. See also [`Glyph`]. pub type GlyphBuffer = Buffer; From d942f72c2a6325a25e213bbce5bc3a943be39861 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Feb 2023 13:57:07 +0000 Subject: [PATCH 128/149] [wasm-rust] Fix path --- src/wasm/sample/rust/hello-wasm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm/sample/rust/hello-wasm/Cargo.toml b/src/wasm/sample/rust/hello-wasm/Cargo.toml index e8207ed35..53357ea90 100644 --- a/src/wasm/sample/rust/hello-wasm/Cargo.toml +++ b/src/wasm/sample/rust/hello-wasm/Cargo.toml @@ -10,4 +10,4 @@ crate-type = ["cdylib"] #externref = "0.1.0" wasm-bindgen = "0.2.0" tiny-rng = "0.2.0" -harfbuzz-wasm = { path="../../rust/harfbuzz-wasm"} +harfbuzz-wasm = { path="../../../rust/harfbuzz-wasm"} From 7e397d8695f32c91bddbf373cd19f4c6e61fa882 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Feb 2023 13:57:34 +0000 Subject: [PATCH 129/149] [wasm-rust] Fix shape parameters --- src/wasm/sample/rust/hello-wasm/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wasm/sample/rust/hello-wasm/src/lib.rs b/src/wasm/sample/rust/hello-wasm/src/lib.rs index 223f2699c..ab8f15368 100644 --- a/src/wasm/sample/rust/hello-wasm/src/lib.rs +++ b/src/wasm/sample/rust/hello-wasm/src/lib.rs @@ -3,7 +3,13 @@ use tiny_rng::{Rand, Rng}; use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { +pub fn shape( + _shape_plan: u32, + font_ref: u32, + buf_ref: u32, + _features: u32, + _num_features: u32, +) -> i32 { let mut rng = Rng::from_seed(123456); let font = Font::from_ref(font_ref); font.shape_with(buf_ref, "ot"); From 142ceaf2466b66fc868f125b4adc6e727b981f3b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 12:18:31 -0700 Subject: [PATCH 130/149] [wasm-shape] Print module instantiation error --- src/hb-wasm-shape.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 5ad1b483a..439de4704 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -133,6 +133,8 @@ static hb_wasm_shape_plan_t * acquire_shape_plan (hb_face_t *face, const hb_wasm_face_data_t *face_data) { + char error[128]; + /* Fetch cached one if available. */ hb_wasm_shape_plan_t *plan = face_data->plan.get_acquire (); if (likely (plan && face_data->plan.cmpexch (plan, nullptr))) @@ -146,12 +148,12 @@ acquire_shape_plan (hb_face_t *face, constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; - module_inst = plan->module_inst = wasm_runtime_instantiate(face_data->wasm_module, - stack_size, heap_size, - nullptr, 0); + module_inst = plan->module_inst = wasm_runtime_instantiate (face_data->wasm_module, + stack_size, heap_size, + error, sizeof (error)); if (unlikely (!module_inst)) { - DEBUG_MSG (WASM, face_data, "Create wasm module instance failed."); + DEBUG_MSG (WASM, face_data, "Create wasm module instance failed: %s", error); goto fail; } From 6aea77c643777b7ed550edc63f65859c05430e53 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 12:28:25 -0700 Subject: [PATCH 131/149] [wasm-shape] Add (disabled) module support --- meson.build | 1 + src/hb-wasm-shape.cc | 66 +++++++++++++++++++++++++++++++++++++- src/wasm/graphite/Makefile | 2 +- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 70ba6a26c..4b2df6c58 100644 --- a/meson.build +++ b/meson.build @@ -234,6 +234,7 @@ endif if wasm_dep.found() conf.set('HAVE_WASM', 1) + conf.set('HB_WASM_MODULE_DIR', '"'+get_option('prefix')+'/'+get_option('libdir')+'/harfbuzz/wasm"') endif if graphite2_dep.found() or graphite_dep.found() diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 439de4704..b1313b3e2 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -25,14 +25,74 @@ */ #define HB_DEBUG_WASM 1 +#define HB_WASM_NO_MODULES #include "hb-shaper-impl.hh" #ifdef HAVE_WASM +/* Compile wasm-micro-runtime with: + * + * $ cmake -DWAMR_BUILD_MULTI_MODULE=1 -DWAMR_BUILD_REF_TYPES=1 -DWAMR_BUILD_FAST_JIT=1 + * $ make + * + * If you manage to build a wasm shared module successfully and want to use it, + * do the following: + * + * - Add -DWAMR_BUILD_MULTI_MODULE=1 to your cmake build for wasm-micro-runtime, + * + * - Remove the #define HB_WASM_NO_MODULES line below, + * + * - Install your shared module with name ending in .wasm in + * $(prefix)/$(libdir)/harfbuzz/wasm/ + * + * - Build your font's wasm code importing the shared modules with the desired + * name. This can be done eg.: __attribute__((import_module("graphite2"))) + * before each symbol in the the shared-module's headers. + * + * - Try shaping your font and hope for the best... + * + * I haven't been able to get this to work since emcc's support for shared libraries + * requires support from the host that seems to be missing from wasm-micro-runtime? + */ + #include "hb-wasm-api.hh" #include "hb-wasm-api-list.hh" +#define HB_WASM_NO_MODULES + +static bool HB_UNUSED +_hb_wasm_module_reader (const char *module_name, + uint8_t **p_buffer, uint32_t *p_size) +{ + char path[sizeof (HB_WASM_MODULE_DIR) + 64] = HB_WASM_MODULE_DIR "/"; + strncat (path, module_name, sizeof (path) - sizeof (HB_WASM_MODULE_DIR) - 16); + strncat (path, ".wasm", 6); + + auto *blob = hb_blob_create_from_file (path); + + unsigned length; + auto *data = hb_blob_get_data (blob, &length); + + *p_buffer = (uint8_t *) hb_malloc (length); + + if (length && !p_buffer) + return false; + + memcpy (*p_buffer, data, length); + *p_size = length; + + hb_blob_destroy (blob); + + return true; +} + +static void HB_UNUSED +_hb_wasm_module_destroyer (uint8_t *buffer, uint32_t size) +{ + hb_free (buffer); +} + /* * shaper face data @@ -85,6 +145,11 @@ _hb_wasm_init () return false; } +#ifndef HB_WASM_NO_MODULES + wasm_runtime_set_module_reader(_hb_wasm_module_reader, + _hb_wasm_module_destroyer); +#endif + initialized = true; return true; } @@ -311,7 +376,6 @@ retry: auto *module_inst = plan->module_inst; auto *exec_env = plan->exec_env; - // cmake -DWAMR_BUILD_REF_TYPES=1 for these to work HB_OBJ2REF (font); HB_OBJ2REF (buffer); if (unlikely (!fontref || !bufferref)) diff --git a/src/wasm/graphite/Makefile b/src/wasm/graphite/Makefile index db40901fe..cdd44dcb3 100644 --- a/src/wasm/graphite/Makefile +++ b/src/wasm/graphite/Makefile @@ -12,8 +12,8 @@ all: $(FONTS) -Wl,--no-entry \ -Wl,--strip-all \ -sERROR_ON_UNDEFINED_SYMBOLS=0 \ - ~/graphite/src/libgraphite2.a \ -Wl,--export=malloc -Wl,--export=free \ + ~/graphite/src/libgraphite2.a \ ~/wasm/wasi-sdk-19.0/share/wasi-sysroot/lib/wasm32-wasi/libc.a \ $< \ -o $@ From e89415b5b9f82ae1893343496ec659a0947b7055 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 13:14:37 -0700 Subject: [PATCH 132/149] [wasm-shape] Print another error message --- src/hb-wasm-shape.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index b1313b3e2..0fbd9d6c2 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -25,7 +25,6 @@ */ #define HB_DEBUG_WASM 1 -#define HB_WASM_NO_MODULES #include "hb-shaper-impl.hh" @@ -157,6 +156,7 @@ _hb_wasm_init () hb_wasm_face_data_t * _hb_wasm_shaper_face_data_create (hb_face_t *face) { + char error[128]; hb_wasm_face_data_t *data = nullptr; hb_blob_t *wasm_blob = nullptr; wasm_module_t wasm_module = nullptr; @@ -170,10 +170,10 @@ _hb_wasm_shaper_face_data_create (hb_face_t *face) goto fail; wasm_module = wasm_runtime_load ((uint8_t *) hb_blob_get_data_writable (wasm_blob, nullptr), - length, nullptr, 0); + length, error, sizeof (error)); if (unlikely (!wasm_module)) { - DEBUG_MSG (WASM, nullptr, "Load wasm module failed."); + DEBUG_MSG (WASM, nullptr, "Load wasm module failed: %s", error); goto fail; } From 5235ee68adc1bec46fb7e0cb4f327f814961d595 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 13:15:39 -0700 Subject: [PATCH 133/149] [wasm-shape] Remove explicit running-mode setting It does it automatically. --- src/hb-wasm-shape.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 0fbd9d6c2..5e43ff96b 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -123,11 +123,6 @@ _hb_wasm_init () RuntimeInitArgs init_args; memset (&init_args, 0, sizeof (RuntimeInitArgs)); - if (wasm_runtime_is_running_mode_supported (Mode_LLVM_JIT)) - init_args.running_mode = Mode_LLVM_JIT; - else if (wasm_runtime_is_running_mode_supported (Mode_Fast_JIT)) - init_args.running_mode = Mode_Fast_JIT; - init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; From 7e5064ac1b259f5f92a588fc42096337c2f37a1c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 14:01:56 -0700 Subject: [PATCH 134/149] [wasm-shape] Comment re thread-safety --- src/hb-wasm-shape.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 5e43ff96b..25b35cbcc 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -114,7 +114,14 @@ struct hb_wasm_face_data_t { static bool _hb_wasm_init () { - /* XXX Umm. Make this threadsafe. How?! */ + /* XXX + * + * Umm. Make this threadsafe. How?! + * It's clunky that we can't allocate a static mutex. + * So we have to first allocate one on the heap atomically... + * + * Also, wasm-micro-runtime uses a singleton instance. So if + * another library or client uses it, all bets are off. :-( */ static bool initialized; if (initialized) @@ -140,8 +147,8 @@ _hb_wasm_init () } #ifndef HB_WASM_NO_MODULES - wasm_runtime_set_module_reader(_hb_wasm_module_reader, - _hb_wasm_module_destroyer); + wasm_runtime_set_module_reader (_hb_wasm_module_reader, + _hb_wasm_module_destroyer); #endif initialized = true; From f5a0bd223b2194203aa7435ac09fc73468544a46 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 14:05:26 -0700 Subject: [PATCH 135/149] [wasm-shape] Comment re thread-safety --- src/hb-wasm-shape.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 25b35cbcc..30c0d193f 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -120,8 +120,12 @@ _hb_wasm_init () * It's clunky that we can't allocate a static mutex. * So we have to first allocate one on the heap atomically... * + * Do we also need to lock around module creation? + * * Also, wasm-micro-runtime uses a singleton instance. So if - * another library or client uses it, all bets are off. :-( */ + * another library or client uses it, all bets are off. :-( + * If nothing else, around HB_REF2OBJ(). + */ static bool initialized; if (initialized) From 7df9b3dd892abfcb31be072ae47193cb0482b51e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 26 Feb 2023 14:54:07 -0700 Subject: [PATCH 136/149] [wasm-api-list] Add TODO --- src/hb-wasm-api-list.hh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 2f0ede178..58fb18e28 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -47,7 +47,9 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int #define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} /* Note: the array must be static defined since runtime will keep it after registration - * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md */ + * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md + * + * TODO Allocate this lazily in _hb_wasm_init(). */ static NativeSymbol _hb_wasm_native_symbols[] = { /* common */ From 74a2f338c67a8c3cd5220b19366f0331332dd69d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 Mar 2023 10:29:01 -0700 Subject: [PATCH 137/149] [wasm-shape] Don't crash if font is bad --- src/hb-wasm-shape.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 30c0d193f..7d584c13e 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -425,6 +425,11 @@ retry: { DEBUG_MSG (WASM, module_inst, "Calling shape() failed: %s", wasm_runtime_get_exception (module_inst)); + if (!buffer->ensure_unicode ()) + { + DEBUG_MSG (WASM, font, "Shape failed but buffer is not in Unicode; failing..."); + goto fail; + } if (retried) { DEBUG_MSG (WASM, font, "Giving up..."); From e78d8653cebc1803f669c238b2e42300717af7c8 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 2 Mar 2023 19:47:33 +0000 Subject: [PATCH 138/149] [wasm-rust] panic if buffer_set_contents fails --- src/wasm/rust/harfbuzz-wasm/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index 61e9da9bf..c33479c59 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -304,7 +304,9 @@ impl Drop for Buffer { position: positions[..].as_mut_ptr(), }; unsafe { - buffer_set_contents(self._ptr, &c_contents); + if !buffer_set_contents(self._ptr, &c_contents) { + panic!("Couldn't set buffer contents"); + } } } } From b6c18144106955f30ac9ec645b0c7eac860c6a7f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 3 Mar 2023 11:31:08 -0700 Subject: [PATCH 139/149] [wasm] Comment --- src/hb-wasm-api-list.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 58fb18e28..3f952e72f 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -46,7 +46,8 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int #endif #define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL} -/* Note: the array must be static defined since runtime will keep it after registration +/* Note: the array must be static defined since runtime will keep it after registration. + * Also not const, because it modifies it (sorts it). * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md * * TODO Allocate this lazily in _hb_wasm_init(). */ From 6ff994f31d4dc0a929dffae2ff6772096a2e51fd Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 3 Mar 2023 15:33:48 -0700 Subject: [PATCH 140/149] [wasm-api-shape] Avoid a couple of crashes --- src/hb-wasm-api-shape.hh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index 7e12969f7..622ff052b 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -46,6 +46,14 @@ HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV HB_REF2OBJ (font); HB_REF2OBJ (buffer); + /* Pre-conditions that make hb_shape_full() crash should be checked here. */ + + if (unlikely (!buffer->ensure_unicode ())) + return false; + + if (unlikely (!HB_DIRECTION_IS_VALID (buffer->props.direction))) + return false; + HB_ARRAY_PARAM (const feature_t, features, num_features); if (unlikely (!features && num_features)) return false; From 0a16c60b42a03603d12affd9746da1d0a0e67adf Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Fri, 10 Mar 2023 14:31:56 +0000 Subject: [PATCH 141/149] [wasm-rust] Derive some friendly traits --- src/wasm/rust/harfbuzz-wasm/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wasm/rust/harfbuzz-wasm/src/lib.rs b/src/wasm/rust/harfbuzz-wasm/src/lib.rs index c33479c59..ea20ce507 100644 --- a/src/wasm/rust/harfbuzz-wasm/src/lib.rs +++ b/src/wasm/rust/harfbuzz-wasm/src/lib.rs @@ -378,6 +378,7 @@ struct CBufferContents { /// representing glyph positioning. In Rust, this would /// require lots of zipping and unzipping, so we zip them /// together into a single structure for you. +#[derive(Debug, Clone, Copy)] pub struct Glyph { /// The Unicode codepoint or glyph ID of the item pub codepoint: u32, From 3fc48d4ada4e4ed1535f7a38cba9b3f85d21ad9b Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Mar 2023 17:43:14 +0100 Subject: [PATCH 142/149] [wasm] [experimental] wrap hb_face_create and hb_create_font --- src/hb-wasm-api-blob.hh | 20 ++++++++++++++++++++ src/hb-wasm-api-face.hh | 9 +++++++++ src/hb-wasm-api-list.hh | 2 ++ src/hb-wasm-api.h | 5 +++++ 4 files changed, 36 insertions(+) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index 310f4023f..ee80c14ce 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -30,6 +30,26 @@ namespace hb { namespace wasm { +HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob), + unsigned int index) +{ + HB_PTR_PARAM (blob_t, blob); + hb_blob_t *hb_blob = hb_blob_create( + HB_ARRAY_APP2NATIVE(char, blob->data, blob->length), + blob->length, + (hb_memory_mode_t)0, + NULL, + NULL + ); + + hb_face_t *face = hb_face_create(hb_blob, index); + + HB_OBJ2REF (face); + return faceref; +} + + HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index e528e1f8c..b8d208066 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -85,7 +85,16 @@ HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV return hb_face_get_upem (face); } +HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)) +{ + HB_REF2OBJ (face); + hb_font_t *font = hb_font_create (face); + + HB_OBJ2REF (font); + return fontref; +} }} #endif /* HB_WASM_API_FACE_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 3f952e72f..b46044d21 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -57,6 +57,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)i", script_get_horizontal_direction), /* blob */ + NATIVE_SYMBOL ("(ii)i", blob_face_create), NATIVE_SYMBOL ("(i)", blob_free), /* buffer */ @@ -72,6 +73,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* face */ NATIVE_SYMBOL ("(iii)i", face_copy_table), NATIVE_SYMBOL ("(i)i", face_get_upem), + NATIVE_SYMBOL ("(i)i", face_create_font), /* font */ NATIVE_SYMBOL ("(i)i", font_get_face), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 67e33e7e1..6927f4d4d 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -113,6 +113,9 @@ typedef struct HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)); +HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob), + unsigned int); /* buffer */ @@ -183,6 +186,8 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV ptr_d(face_t, face)); +HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)); /* font */ From 0bfad127c3a907d5c50e59ab61a2beac69853cb1 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sun, 26 Mar 2023 17:44:55 +0100 Subject: [PATCH 143/149] [wasm] Make _hb_wasm_module_reader conditional Or else uharfbuzz doesn't build. --- src/hb-wasm-shape.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index 7d584c13e..798a09572 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -58,8 +58,12 @@ #include "hb-wasm-api.hh" #include "hb-wasm-api-list.hh" +#ifndef HB_WASM_NO_MODULES #define HB_WASM_NO_MODULES +#endif + +#ifndef HB_WASM_NO_MODULES static bool HB_UNUSED _hb_wasm_module_reader (const char *module_name, uint8_t **p_buffer, uint32_t *p_size) @@ -91,7 +95,7 @@ _hb_wasm_module_destroyer (uint8_t *buffer, uint32_t size) { hb_free (buffer); } - +#endif /* * shaper face data From 85a1fdd93fcafbfdd91bec2d16cf0ce6f3a0cd79 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Mon, 27 Mar 2023 14:42:21 +0100 Subject: [PATCH 144/149] [wasm] get/set font variation parameters --- src/hb-wasm-api-font.hh | 56 +++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 2 ++ src/hb-wasm-api.h | 14 +++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 2a5425cd0..cda3b9ea9 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -190,6 +190,62 @@ HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV outline->contours = nullref; } +HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(coords_t, coords)) +{ + HB_REF2OBJ (font); + HB_PTR_PARAM (coords_t, coords); + if (unlikely (!coords)) + return false; + + unsigned our_length; + const int* our_coords = hb_font_get_var_coords_normalized(font, &our_length); + + if (our_length <= coords->length) { + int *their_coords = HB_ARRAY_APP2NATIVE (int, coords->coords, our_length); + if (unlikely(!their_coords)) { + coords->length = 0; + return false; + } + unsigned bytes = our_length * sizeof (int); + memcpy (their_coords, our_coords, bytes); + + return true; + } + + module_free (coords->coords); + coords->length = our_length; + unsigned bytes = our_length * sizeof (int); + coords->coords = wasm_runtime_module_dup_data (module_inst, (const char *) our_coords, bytes); + if (our_length && !coords->coords) + { + coords->length = 0; + return false; + } + + return true; +} + +HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(coords_t, coords)) +{ + HB_REF2OBJ (font); + HB_PTR_PARAM (coords_t, coords); + if (unlikely (!coords)) + return false; + + unsigned length = coords->length; + unsigned bytes; + if (unlikely (hb_unsigned_mul_overflows (length, sizeof (int), &bytes))) + return false; + + const int *our_coords = (const int *) (validate_app_addr (coords->coords, bytes) ? addr_app_to_native (coords->coords) : nullptr); + hb_font_set_var_coords_normalized(font, our_coords, length); + return true; +} + }} diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index b46044d21..8674eb124 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -84,6 +84,8 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(iii)i", font_get_glyph_extents), NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), NATIVE_SYMBOL ("(iii)i", font_copy_glyph_outline), + NATIVE_SYMBOL ("(ii)i", font_copy_coords), + NATIVE_SYMBOL ("(ii)i", font_set_coords), /* outline */ NATIVE_SYMBOL ("(i)", glyph_outline_free), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 6927f4d4d..4c5421e20 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -233,6 +233,20 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV char *s, uint32_t size); +typedef struct +{ + unsigned int length; + ptr_t(int) coords; +} coords_t; + +HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(coords_t, coords)); + +HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV + ptr_d(font_t, font), + ptr_d(coords_t, coords)); + /* outline */ enum glyph_outline_point_type_t From f9dd402ef8c49bbb89d7c1311a6008a492f4182d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 31 Mar 2023 12:18:35 -0600 Subject: [PATCH 145/149] [wasm] Fix header --- src/hb-wasm-api.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 4c5421e20..8f1f04e73 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -113,10 +113,6 @@ typedef struct HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)); -HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV - ptr_d(blob_t, blob), - unsigned int); - /* buffer */ typedef struct @@ -178,6 +174,10 @@ HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV typedef struct face_t face_t; +HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob), + unsigned int); + HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV ptr_d(face_t, face), tag_t table_tag, @@ -186,13 +186,13 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV ptr_d(face_t, face)); -HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV - ptr_d(face_t, face)); - /* font */ typedef struct font_t font_t; +HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)); + HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV ptr_d(font_t, font)); From d2fb583a5a2e7839a88a8a4b3c1832459223c732 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 31 Mar 2023 12:18:41 -0600 Subject: [PATCH 146/149] [wasm-graphite] Fix advance signedness --- src/wasm/graphite/shape.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wasm/graphite/shape.cc b/src/wasm/graphite/shape.cc index 499d17be9..e523e4cbf 100644 --- a/src/wasm/graphite/shape.cc +++ b/src/wasm/graphite/shape.cc @@ -189,7 +189,7 @@ shape (void *shape_plan, glyph_info_t *info = &contents.info[clusters[i].base_glyph + j]; info->codepoint = gids[clusters[i].base_glyph + j]; info->cluster = clusters[i].cluster; - info->var1 = clusters[i].advance; // all glyphs in the cluster get the same advance + info->var1 = (unsigned) clusters[i].advance; // all glyphs in the cluster get the same advance } } contents.length = glyph_count; @@ -206,7 +206,7 @@ shape (void *shape_plan, pPos->x_offset = gr_slot_origin_X (is) * xscale - curradvx; pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; if (info->cluster != currclus) { - pPos->x_advance = info->var1; + pPos->x_advance = (int) info->var1; curradvx += pPos->x_advance; currclus = info->cluster; } else @@ -224,7 +224,7 @@ shape (void *shape_plan, { if (info->cluster != currclus) { - pPos->x_advance = info->var1; + pPos->x_advance = (int) info->var1; curradvx -= pPos->x_advance; currclus = info->cluster; } else @@ -232,7 +232,7 @@ shape (void *shape_plan, pPos->y_advance = gr_slot_advance_Y (is, grface, nullptr) * yscale; curradvy -= pPos->y_advance; - pPos->x_offset = gr_slot_origin_X (is) * xscale - info->var1 - curradvx + pPos->x_advance; + pPos->x_offset = gr_slot_origin_X (is) * xscale - (int) info->var1 - curradvx + pPos->x_advance; pPos->y_offset = gr_slot_origin_Y (is) * yscale - curradvy; } buffer_set_contents (buffer, &contents); From 2663a9b6f98a4afebe43ee213c5c14af6c2c5ce5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 31 Mar 2023 17:38:23 -0600 Subject: [PATCH 147/149] [wasm] Rename a couple APIs --- src/hb-wasm-api-blob.hh | 20 -------------------- src/hb-wasm-api-face.hh | 27 ++++++++++++++++++--------- src/hb-wasm-api-font.hh | 11 +++++++++++ src/hb-wasm-api-list.hh | 4 ++-- src/hb-wasm-api.h | 10 +++++----- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/hb-wasm-api-blob.hh b/src/hb-wasm-api-blob.hh index ee80c14ce..310f4023f 100644 --- a/src/hb-wasm-api-blob.hh +++ b/src/hb-wasm-api-blob.hh @@ -30,26 +30,6 @@ namespace hb { namespace wasm { -HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV - ptr_d(blob_t, blob), - unsigned int index) -{ - HB_PTR_PARAM (blob_t, blob); - hb_blob_t *hb_blob = hb_blob_create( - HB_ARRAY_APP2NATIVE(char, blob->data, blob->length), - blob->length, - (hb_memory_mode_t)0, - NULL, - NULL - ); - - hb_face_t *face = hb_face_create(hb_blob, index); - - HB_OBJ2REF (face); - return faceref; -} - - HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV ptr_d(blob_t, blob)) diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index b8d208066..e59cedc67 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -31,6 +31,24 @@ namespace hb { namespace wasm { +HB_WASM_API (ptr_t(face_t), face_create) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob), + unsigned int index) +{ + HB_PTR_PARAM (blob_t, blob); + hb_blob_t *hb_blob = hb_blob_create( + HB_ARRAY_APP2NATIVE (char, blob->data, blob->length), + blob->length, + HB_MEMORY_MODE_DUPLICATE, + NULL, + NULL); + + hb_face_t *face = hb_face_create(hb_blob, index); + + HB_OBJ2REF (face); + return faceref; +} + HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV ptr_d(face_t, face), tag_t table_tag, @@ -85,16 +103,7 @@ HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV return hb_face_get_upem (face); } -HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV - ptr_d(face_t, face)) -{ - HB_REF2OBJ (face); - hb_font_t *font = hb_font_create (face); - - HB_OBJ2REF (font); - return fontref; -} }} #endif /* HB_WASM_API_FACE_HH */ diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index cda3b9ea9..9c004a110 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -33,6 +33,17 @@ namespace hb { namespace wasm { +HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)) +{ + HB_REF2OBJ (face); + + hb_font_t *font = hb_font_create (face); + + HB_OBJ2REF (font); + return fontref; +} + HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV ptr_d(font_t, font)) { diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 8674eb124..5db45c74c 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -57,7 +57,6 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)i", script_get_horizontal_direction), /* blob */ - NATIVE_SYMBOL ("(ii)i", blob_face_create), NATIVE_SYMBOL ("(i)", blob_free), /* buffer */ @@ -71,11 +70,12 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), /* face */ + NATIVE_SYMBOL ("(ii)i", face_create), NATIVE_SYMBOL ("(iii)i", face_copy_table), NATIVE_SYMBOL ("(i)i", face_get_upem), - NATIVE_SYMBOL ("(i)i", face_create_font), /* font */ + NATIVE_SYMBOL ("(i)i", font_create), NATIVE_SYMBOL ("(i)i", font_get_face), NATIVE_SYMBOL ("(iii)", font_get_scale), NATIVE_SYMBOL ("(iii)i", font_get_glyph), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 8f1f04e73..f9bd98e5e 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -174,9 +174,9 @@ HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV typedef struct face_t face_t; -HB_WASM_API (ptr_t(face_t), blob_face_create) (HB_WASM_EXEC_ENV - ptr_d(blob_t, blob), - unsigned int); +HB_WASM_API (ptr_t(face_t), face_create) (HB_WASM_EXEC_ENV + ptr_d(blob_t, blob), + unsigned int); HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV ptr_d(face_t, face), @@ -190,8 +190,8 @@ HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV typedef struct font_t font_t; -HB_WASM_API (ptr_t(font_t), face_create_font) (HB_WASM_EXEC_ENV - ptr_d(face_t, face)); +HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV + ptr_d(face_t, face)); HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV ptr_d(font_t, font)); From 56af88d04848dec4d4d4ad6d713b22d92edff7e8 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Wed, 17 May 2023 12:38:59 +0100 Subject: [PATCH 148/149] [wasm] More thorough API docs --- docs/wasm-shaper.md | 368 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 367 insertions(+), 1 deletion(-) diff --git a/docs/wasm-shaper.md b/docs/wasm-shaper.md index 842634356..a4ce9e0b4 100644 --- a/docs/wasm-shaper.md +++ b/docs/wasm-shaper.md @@ -2,9 +2,375 @@ If the standard OpenType shaping engine doesn't give you enough flexibility, Harfbuzz allows you to write your own shaping engine in WebAssembly and embed it into your font! Any font which contains a `Wasm` table will be passed to the WebAssembly shaper. +## What you can and can't do: the WASM shaper's role in shaping + +The Harfbuzz shaping engine, unlike its counterparts CoreText and DirectWrite, is only responsible for a small part of the text rendering process. Specifically, Harfbuzz is purely responsible for *shaping*; although Harfbuzz does have APIs for accessing glyph outlines, typically other libraries in the free software text rendering stack are responsible for text segmentation into runs, outline scaling and rasterizing, setting text on lines, and so on. + +Harfbuzz is therefore restricted to turning a buffer of codepoints for a segmented run of the same script, language, font, and variation settings, into glyphs and positioning them. This is also all that you can do with the WASM shaper; you can influence the process of mapping a string of characters into an array of glyphs, you can determine how those glyphs are positioned and their advance widths, but you cannot manipulate outlines, variations, line breaks, or affect text layout between texts of different font, variation, language, script or OpenType feature selection. + +## The WASM shaper interface + +The WASM code inside a font is expected to export a function called `shape` which takes five int32 arguments and returns an int32 status value. (Zero for failure, any other value for success.) Three of the five arguments are tokens which can be passed to the API functions exported to your WASM code by the host shaping engine: + +* A *shape plan* token, which can largely be ignored. +* A *font* token. +* A *buffer* token. +* A *feature* array. +* The number of features. + +The general goal of WASM shaping involves receiving and manipulating a *buffer contents* structure, which is an array of *infos* and *positions* (as defined below). Initially this buffer will represent an input string in Unicode codepoints. By the end of your `shape` function, it should represent a set of glyph IDs and their positions. (User-supplied WASM code will manipulate the buffer through *buffer tokens*; the `buffer_copy_contents` and `buffer_set_contents` API functions, defined below, use these tokens to exchange buffer information with the host shaping engine.) + +* The `buffer_contents_t` structure + +| type | field | description| +| - | - | - | +| uint32 | length | Number of items (characters or glyphs) in the buffer +| glyph_info_t | infos | An array of `length` glyph infos | +| glyph_position_t | positions | An array of `length` glyph positions | + +* The `glyph_info_t` structure + +| type | field | description| +| - | - | - | +| uint32 | codepoint | (On input) A Unicode codepoint. (On output) A glyph ID. | +| uint32 | mask | Unused in WASM; can be user-defined | +| uint32 | cluster | Index of start of this graphical cluster in input string | +| uint32 | var1 | Reserved | +| uint32 | var2 | Reserved | + +The `cluster` field is used to glyphs in the output glyph stream back to characters in the input Unicode sequence for hit testing, cursor positioning, etc. It must be set to a monotonically increasing value across the buffer. + +* The `glyph_position_t` structure + +| type | field | description| +| - | - | - | +| int32 | x_advance | X advance of the glyph | +| int32 | y_advance | Y advance of the glyph | +| int32 | x_offset | X offset of the glyph | +| int32 | y_offset | Y offset of the glyph | +| uint32 | var | Reserved | + +* The `feature_t` array + +To communicated user-selected OpenType features to the user-defined WASM shaper, the host shaping engine passes an array of feature structures: + +| type | field | description| +| - | - | - | +| uint32 | tag | Byte-encoded feature tag | +| uint32 | value | Value: 0=off, 1=on, other values used for alternate selection | +| uint32 | start | Index into the input string representing start of the active region for this feature selection (0=start of string) | +| uint32 | end | Index into the input string representing end of the active region for this feature selection (-1=end of string) | + +## API functions available + +To assist the shaping code in mapping codepoints to glyphs, the WASM shaper exports the following functions. Note that these are the low level API functions; WASM authors may prefer to use higher-level abstractions around these functions, such as the `harfbuzz-wasm` Rust crate provided by Harfbuzz. + +### Sub-shaping + +* `shape_with` + +```C +bool shape_with( + uint32 font_token, + uint32 buffer_token, + feature_t* features, + uint32 num_features, + char* shaper +) +``` + +Run another shaping engine's shaping process on the given font and buffer. The only shaping engine guaranteed to be available is `ot`, the OpenType shaper, but others may also be available. This allows the WASM author to process a buffer "normally", before further manipulating it. + +### Buffer access + +* `buffer_copy_contents` + +```C +bool buffer_copy_contents( + uint32 buffer_token, + buffer_contents_t* buffer_contents +) +``` + +Retrieves the contents of the host shaping engine's buffer into the `buffer_contents` structure. This should typically be called at the beginning of shaping. + +* `buffer_set_contents` + +```C +bool buffer_set_contents( + uint32 buffer_token, + buffer_contents_t* buffer_contents +) +``` + +Copy the `buffer_contents` structure back into the host shaping engine's buffer. This should typically be called at the end of shaping. + +* `buffer_contents_free` + +```C +bool buffer_contents_free(buffer_contents_t* buffer_contents) +``` + +Releases the memory taken up by the buffer contents structure. + +* `buffer_contents_realloc` + +```C +bool buffer_contents_realloc( + buffer_contents_t* buffer_contents, + uint32 size +) +``` + +Requests that the buffer contents structure be resized to the given size. + +* `buffer_get_direction` + +```C +uint32 buffer_get_direction(uint32 buffer_token) +``` + +Returns the buffer's direction: + +* 0 = invalid +* 4 = left to right +* 5 = right to left +* 6 = top to bottom +* 7 = bottom to top + +* `buffer_get_script` + +```C +uint32 buffer_get_script(uint32 buffer_token) +``` + +Returns the byte-encoded OpenType script tag of the buffer. + +* `buffer_reverse` + +```C +void buffer_reverse(uint32 buffer_token) +``` + +Reverses the order of items in the buffer. + +* `buffer_reverse_clusters` + +```C +void buffer_reverse_clusters(uint32 buffer_token) +``` + +Reverses the order of items in the buffer while keeping items of the same cluster together. + +## Font handling functions + +(In the following functions, a *font* is a specific instantiation of a *face* at a particular scale factor and variation position.) + +* `font_create` + +```C +uint32 font_create(uint32 face_token) +``` + +Returns a new *font token* from the given *face token*. + +* `font_get_face` + +```C +uint32 font_get_face(uint32 font_token) +``` + +Creates a new *face token* from the given *font token*. + +* `font_get_scale` + +```C +void font_get_scale( + uint32 font_token, + int32* x_scale, + int32* y_scale +) +``` + +Returns the scale of the current font. + +* `font_get_glyph` + +```C +uint32 font_get_glyph( + uint32 font_token, + uint32 codepoint, + uint32 variation_selector +) +``` + +Returns the nominal glyph ID for the given codepoint, using the `cmap` table of the font to map Unicode codepoint (and variation selector) to glyph ID. + +* `font_get_glyph_h_advance`/`font_get_glyph_v_advance` + +```C +uint32 font_get_glyph_h_advance(uint32 font_token, uint32 glyph_id) +uint32 font_get_glyph_v_advance(uint32 font_token, uint32 glyph_id) +``` + +Returns the default horizontal and vertical advance respectively for the given glyph ID the current scale and variations settings. + +* `font_get_glyph_extents` + +```C +typedef struct +{ + uint32 x_bearing; + uint32 y_bearing; + uint32 width; + uint32 height; +} glyph_extents_t; + +bool font_get_glyph_extents( + uint32 font_token, + uint32 glyph_id, + glyph_extents_t* extents +) +``` + +Returns the glyph's extents for the given glyph ID at current scale and variation settings. + +* `font_glyph_to_string` + +```C +void font_glyph_to_string( + uint32 font_token, + uint32 glyph_id, + char* string, + uint32 size +) +``` + +Copies the name of the given glyph, or, if no name is available, a string of the form `gXXXX` into the given string. + +* `font_copy_glyph_outline` + +```C +typedef struct +{ + float x; + float y; + uint32_t type; +} glyph_outline_point_t; + +typedef struct +{ + uint32_t n_points; + glyph_outline_point_t* points; + uint32_t n_contours; + uint32_t* contours; +} glyph_outline_t; + +bool font_copy_glyph_outline( + uint32 font_token, + uint32 glyph_id, + glyph_outline_t* outline +); +``` + +Copies the outline of the given glyph ID, at current scale and variation settings, into the outline structure provided. The outline structure returns an array of points (specifying coordinates and whether the point is oncurve or offcurve) and an array of indexes into the points array representing the end of each contour, similar to the `glyf` table structure. + +* `font_copy_coords`/`font_set_coords` + +```C +typedef struct +{ + uint32 length; + int32* coords; +} coords_t; + +bool font_copy_coords(uint32 font_token, &coords_t coords); +bool font_set_coords(uint32 font_token, &coords_t coords); +``` + +`font_copy_coords` copies the font's variation coordinates into the given structure; the resulting structure has `length` equal to the number of variation axes, with each member of the `coords` array being a F2DOT14 encoding of the normalized variation value. + +`font_set_coords` sets the font's variation coordinates. Because the WASM shaper is only responsible for shaping and positioning, not outline drawing, the user should *not* expect this to affect the rendered outlines; the function is only useful in very limited circumstances, such as when instantiating a second variable font and sub-shaping a buffer using this new font. + +## Face handling functions + +* `face_create` + +```C +typedef struct +{ + uint32_t length; + char* data; +} blob_t; + +uint32 font_get_face(blob_t* blob) +``` + +Creates a new *face token* from the given binary data. + +* `face_copy_table` + +```C +void face_copy_table(uint32 face_token, uint32 tag, blob_t* blob) +``` + +Copies the binary data in the OpenType table referenced by `tag` into the supplied `blob` structure. + +* `face_get_upem` + +```C +uint32 font_get_upem(uint32 face_token) +``` + +Returns the units-per-em of the font face. + +### Other functions + +* `blob_free` + +```C +void blob_free(blob_t* blob) +``` + +Frees the memory allocated to a blob structure. + +* `glyph_outline_free` + +```C +void glyph_outline_free(glyph_outline_t* glyph_outline) +``` + +Frees the memory allocated to a glyph outline structure. + +* `script_get_horizontal_direction` + +```C +uint32 script_get_horizontal_direction(uint32 tag) +``` + +Returns the horizontal direction for the given ISO 15924 script tag. For return values, see `buffer_get_direction` above. + +* `debugprint` / `debugprint1` ... `debugprint4` + +```C +void debugprint(char* str) +void debugprint1(char* str, int32 arg1) +void debugprint2(char* str, int32 arg1, int32 arg2) +void debugprint3(char* str, int32 arg1, int32 arg2, int32 arg3) +void debugprint3( + char* str, + int32 arg1, + int32 arg2, + int32 arg3, + int32 arg4 +) +``` + +Produces a debugging message in the host shaper's log output; the variants `debugprint1` ... `debugprint4` suffix the message with a comma-separated list of the integer arguments. + ## How to write a shaping engine in Rust -Here are the steps to create an example shaping engine in Rust: (These examples can also be found in `src/wasm/sample/rust`) +You may write shaping engines in any language supported by WASM, by conforming to the API described above, but Rust is particularly easy, and we have one of those high-level interface wrappers which makes the process easier. Here are the steps to create an example shaping engine in Rust: (These examples can also be found in `src/wasm/sample/rust`) * First, install wasm-pack, which helps us to generate optimized WASM files. It writes some Javascript bridge code that we don't need, but it makes the build and deployment process much easier: From 6ed792ef4b00287e16e0b5eda7fe9488e6060e9a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 17 May 2023 07:58:27 -0600 Subject: [PATCH 149/149] [meson] Report WebAssembly build --- meson.build | 3 ++- meson_options.txt | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 4b2df6c58..b9e14539e 100644 --- a/meson.build +++ b/meson.build @@ -423,10 +423,11 @@ build_summary = { }, 'Additional shapers': {'Graphite2': conf.get('HAVE_GRAPHITE2', 0) == 1, + 'WebAssembly (experimental)': conf.get('HAVE_WASM', 0) == 1, }, 'Platform shapers (not normally needed)': {'CoreText': conf.get('HAVE_CORETEXT', 0) == 1, - 'DirectWrite': conf.get('HAVE_DIRECTWRITE', 0) == 1, + 'DirectWrite (experimental)': conf.get('HAVE_DIRECTWRITE', 0) == 1, 'GDI/Uniscribe': (conf.get('HAVE_GDI', 0) == 1) and (conf.get('HAVE_UNISCRIBE', 0) == 1), }, 'Other features': diff --git a/meson_options.txt b/meson_options.txt index 5cdfd8fef..2aa42a5b0 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -22,7 +22,7 @@ option('directwrite', type: 'feature', value: 'disabled', option('coretext', type: 'feature', value: 'disabled', description: 'Enable CoreText shaper backend on macOS') option('wasm', type: 'feature', value: 'disabled', - description: 'Enable WebAssembly shaper backend') + description: 'Enable WebAssembly shaper backend (experimental)') # Common feature options option('tests', type: 'feature', value: 'enabled', yield: true,