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)