[wasm-api] Bind shaper features

This commit is contained in:
Behdad Esfahbod 2023-02-24 14:16:11 -07:00
parent 2327fe9d8a
commit 9f8ad3928a
11 changed files with 69 additions and 19 deletions

View File

@ -34,7 +34,7 @@ namespace wasm {
HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV
ptr_d(blob_t, blob)) ptr_d(blob_t, blob))
{ {
HB_OUT_PARAM (blob_t, blob); HB_PTR_PARAM (blob_t, blob);
if (unlikely (!blob)) if (unlikely (!blob))
return; return;

View File

@ -39,7 +39,7 @@ HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV
ptr_d(buffer_contents_t, contents), ptr_d(buffer_contents_t, contents),
uint32_t size) uint32_t size)
{ {
HB_OUT_PARAM (buffer_contents_t, contents); HB_PTR_PARAM (buffer_contents_t, contents);
if (unlikely (!contents)) if (unlikely (!contents))
return; 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 HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV
ptr_d(buffer_contents_t, contents)) ptr_d(buffer_contents_t, contents))
{ {
HB_OUT_PARAM (buffer_contents_t, contents); HB_PTR_PARAM (buffer_contents_t, contents);
if (unlikely (!contents)) if (unlikely (!contents))
return; return;
@ -101,7 +101,7 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV
ptr_d(const buffer_contents_t, contents)) ptr_d(const buffer_contents_t, contents))
{ {
HB_REF2OBJ (buffer); HB_REF2OBJ (buffer);
HB_OUT_PARAM (buffer_contents_t, contents); HB_PTR_PARAM (buffer_contents_t, contents);
if (unlikely (!contents)) if (unlikely (!contents))
return false; return false;

View File

@ -49,8 +49,8 @@ HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV
{ {
HB_REF2OBJ (font); HB_REF2OBJ (font);
HB_OUT_PARAM(int32_t, x_scale); HB_PTR_PARAM(int32_t, x_scale);
HB_OUT_PARAM(int32_t, y_scale); HB_PTR_PARAM(int32_t, y_scale);
hb_font_get_scale (font, x_scale, 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)) ptr_d(glyph_extents_t, extents))
{ {
HB_REF2OBJ (font); HB_REF2OBJ (font);
HB_OUT_PARAM (glyph_extents_t, extents); HB_PTR_PARAM (glyph_extents_t, extents);
if (unlikely (!extents)) if (unlikely (!extents))
return false; return false;

View File

@ -80,7 +80,7 @@ static NativeSymbol _hb_wasm_native_symbols[] =
NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string), NATIVE_SYMBOL ("(ii$*)", font_glyph_to_string),
/* shape */ /* shape */
NATIVE_SYMBOL ("(ii$)i", shape_with), NATIVE_SYMBOL ("(iiii$)i", shape_with),
/* debug */ /* debug */
#ifdef HB_DEBUG_WASM #ifdef HB_DEBUG_WASM

View File

@ -31,19 +31,29 @@ namespace hb {
namespace wasm { namespace wasm {
static_assert (sizeof (feature_t) == sizeof (hb_feature_t), "");
HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV
ptr_d(font_t, font), 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,
const char *shaper) const char *shaper)
{ {
if (unlikely (0 == strcmp (shaper, "wasm")))
return false;
HB_REF2OBJ (font); HB_REF2OBJ (font);
HB_REF2OBJ (buffer); HB_REF2OBJ (buffer);
if (0 == strcmp (shaper, "wasm")) HB_ARRAY_PARAM (const feature_t, features, num_features);
if (unlikely (!features && num_features))
return false; return false;
const char * shaper_list[] = {shaper, nullptr}; 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);
} }

View File

@ -225,17 +225,29 @@ HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV
/* shape */ /* 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 HB_WASM_INTERFACE (bool_t, shape_with) (HB_WASM_EXEC_ENV
ptr_d(font_t, font), 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,
const char *shaper); const char *shaper);
/* Implement this in your shaper. */
/* shape interface */
HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV HB_WASM_INTERFACE (bool_t, shape) (HB_WASM_EXEC_ENV
ptr_d(font_t, font), 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 HB_WASM_END_DECLS

View File

@ -91,7 +91,7 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key;
} \ } \
type &name = *_name_ptr type &name = *_name_ptr
#define HB_OUT_PARAM(type, name) \ #define HB_PTR_PARAM(type, name) \
type *name = nullptr; \ type *name = nullptr; \
HB_STMT_START { \ HB_STMT_START { \
if (likely (wasm_runtime_validate_app_addr (module_inst, \ 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); \ name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \
} HB_STMT_END } 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 */ #endif /* HB_WASM_API_HH */

View File

@ -197,17 +197,27 @@ _hb_wasm_shape (hb_shape_plan_t *shape_plan,
} }
wasm_val_t results[1]; wasm_val_t results[1];
wasm_val_t arguments[2]; wasm_val_t arguments[4];
results[0].kind = WASM_I32; results[0].kind = WASM_I32;
arguments[0].kind = WASM_I32; arguments[0].kind = WASM_I32;
arguments[0].of.i32 = fontref; arguments[0].of.i32 = fontref;
arguments[1].kind = WASM_I32; arguments[1].kind = WASM_I32;
arguments[1].of.i32 = bufferref; 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, ret = wasm_runtime_call_wasm_a (exec_env, shape_func,
ARRAY_LENGTH (results), results, ARRAY_LENGTH (results), results,
ARRAY_LENGTH (arguments), arguments); ARRAY_LENGTH (arguments), arguments);
wasm_runtime_module_free (module_inst, arguments[2].of.i32);
if (unlikely (!ret)) if (unlikely (!ret))
{ {
DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s", DEBUG_MSG (WASM, module_inst, "Calling shape function failed: %s",

View File

@ -36,7 +36,10 @@ static void free_table (const void *data, const void *table_data)
} }
bool_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)
{ {
direction_t direction = buffer_get_direction (buffer); direction_t direction = buffer_get_direction (buffer);
direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer)); direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer));

View File

@ -9,7 +9,10 @@ void debugprint2 (const char *s, int32_t, int32_t);
} }
bool_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); face_t *face = font_get_face (font);

View File

@ -8,7 +8,10 @@ void debugprint2 (const char *s, int32_t, int32_t);
} }
bool_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");
} }