From 40524f21627ca5b00698f0d51ced210ecdf6bcca Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Mon, 3 Aug 2026 15:57:53 +0100 Subject: [PATCH] Add WASM API to wrap features --- src/hb-wasm-api-list.hh | 2 ++ src/hb-wasm-api-shape.hh | 46 ++++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api.h | 15 +++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 1799937db..6b866ceb2 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -93,6 +93,8 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* shape */ NATIVE_SYMBOL ("(iiii$)i", shape_with), + NATIVE_SYMBOL ("(iii)i", feature_copy_contents), + NATIVE_SYMBOL ("(i)", features_free), /* debug */ #ifdef HB_DEBUG_WASM diff --git a/src/hb-wasm-api-shape.hh b/src/hb-wasm-api-shape.hh index 622ff052b..7bd45d0f8 100644 --- a/src/hb-wasm-api-shape.hh +++ b/src/hb-wasm-api-shape.hh @@ -64,6 +64,52 @@ HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV shaper_list); } +HB_WASM_API (bool_t, feature_copy_contents) (HB_WASM_EXEC_ENV + ptr_d(const feature_t, features), + uint32_t num_features, + ptr_d(features_t, output)) +{ + HB_PTR_PARAM (features_t, output); + if (unlikely (!output)) + return false; + + unsigned bytes; + if (unlikely (hb_unsigned_mul_overflows (num_features, sizeof (feature_t), &bytes))) + return false; + + if (!num_features) + { + output->length = 0; + output->features = nullref; + return true; + } + + HB_ARRAY_PARAM (const feature_t, features, num_features); + if (unlikely (!features)) + return false; + + feature_t *dst = nullptr; + uint32_t dstref = module_malloc (bytes, (void **) &dst); + if (unlikely (!dstref)) + return false; + + hb_memcpy (dst, features, bytes); + output->length = num_features; + output->features = dstref; + return true; +} + +HB_WASM_API (void, features_free) (HB_WASM_EXEC_ENV + ptr_d(features_t, features)) +{ + HB_PTR_PARAM (features_t, features); + if (unlikely (!features)) + return; + + module_free (features->features); + features->features = nullref; + features->length = 0; +} }} diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 49452d148..c89a915e0 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -303,6 +303,21 @@ typedef struct #define FEATURE_GLOBAL_START 0 #define FEATURE_GLOBAL_END ((uint32_t) -1) +typedef struct +{ + uint32_t length; + ptr_t(feature_t) features; +} features_t; +#define FEATURES_INIT {0, 0} + +HB_WASM_API (bool_t, feature_copy_contents) (HB_WASM_EXEC_ENV + ptr_d(const feature_t, features), + uint32_t num_features, + ptr_d(features_t, output)); + +HB_WASM_API (void, features_free) (HB_WASM_EXEC_ENV + ptr_d(features_t, features)); + HB_WASM_API (bool_t, shape_with) (HB_WASM_EXEC_ENV ptr_d(font_t, font), ptr_d(buffer_t, buffer),