diff --git a/util/gpu/web/build-webgpu.sh b/util/gpu/web/build-webgpu.sh index 6b21752a9..21af97d0f 100755 --- a/util/gpu/web/build-webgpu.sh +++ b/util/gpu/web/build-webgpu.sh @@ -26,7 +26,7 @@ em++ \ "$SRCDIR/util/gpu/web/hb-gpu-web-webgpu.cc" \ --use-port=emdawnwebgpu \ -sALLOW_MEMORY_GROWTH=1 \ - -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_request_redraw","_web_cancel_gesture","_malloc","_free"]' \ + -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_set_variations","_web_request_redraw","_web_cancel_gesture","_malloc","_free"]' \ -sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8"]' \ --shell-file "$SRCDIR/util/gpu/web/shell.html" \ -o "$OUTDIR/webgpu.html" diff --git a/util/gpu/web/build.sh b/util/gpu/web/build.sh index 5d8a8a9c2..5d6408e5d 100755 --- a/util/gpu/web/build.sh +++ b/util/gpu/web/build.sh @@ -30,7 +30,7 @@ em++ \ -sUSE_WEBGL2=1 \ -sFULL_ES3=1 \ -sALLOW_MEMORY_GROWTH=1 \ - -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_toggle_animation","_web_reset","_web_request_redraw","_web_cancel_gesture","_web_pinch","_malloc","_free"]' \ + -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_web_set_variations","_web_toggle_animation","_web_reset","_web_request_redraw","_web_cancel_gesture","_web_pinch","_malloc","_free"]' \ -sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8"]' \ --shell-file "$SRCDIR/util/gpu/web/shell.html" \ -o "$OUTDIR/index.html" diff --git a/util/gpu/web/config-override.h b/util/gpu/web/config-override.h index 91df7e5c4..aa343d6dd 100644 --- a/util/gpu/web/config-override.h +++ b/util/gpu/web/config-override.h @@ -4,3 +4,4 @@ #undef HB_NO_METRICS /* Glyph advances / extents */ #undef HB_NO_COLOR /* COLR palette / layers for paint encoder */ #undef HB_NO_PAINT /* Paint pipeline the paint encoder walks */ +#undef HB_NO_VAR /* hb_font_set_variations for live axis updates */ diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 4a2b78e32..96854947f 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -358,6 +358,27 @@ web_set_text (const char *utf8) rebuild_buffer (utf8); } +EMSCRIPTEN_KEEPALIVE void +web_set_variations (const char *settings) +{ + if (!current_font) return; + + hb_variation_t vars[32]; + unsigned n = 0; + const char *p = settings; + while (p && *p && n < (sizeof vars / sizeof vars[0])) + { + const char *end = strchr (p, ','); + int len = end ? (int) (end - p) : (int) strlen (p); + if (hb_variation_from_string (p, len, &vars[n])) + n++; + p = end ? end + 1 : nullptr; + } + hb_font_set_variations (current_font, vars, n); + + rebuild_buffer (custom_text ? current_text : default_text_en); +} + EMSCRIPTEN_KEEPALIVE const char * web_get_text () { diff --git a/util/gpu/web/hb-gpu-web.cc b/util/gpu/web/hb-gpu-web.cc index fa97691e2..233c691e0 100644 --- a/util/gpu/web/hb-gpu-web.cc +++ b/util/gpu/web/hb-gpu-web.cc @@ -120,6 +120,32 @@ web_set_text (const char *utf8) rebuild_buffer (utf8); } +/* Apply a comma-separated list of variation settings + * ("wght=400,wdth=100") to the current font, then rebuild + * the buffer so the new axis values take effect. Cheap + * to call repeatedly: suitable for live slider updates. */ +EMSCRIPTEN_KEEPALIVE void +web_set_variations (const char *settings) +{ + if (!current_font) return; + + /* Caps the axis count: no font ships close to this. */ + hb_variation_t vars[32]; + unsigned n = 0; + const char *p = settings; + while (p && *p && n < (sizeof vars / sizeof vars[0])) + { + const char *end = strchr (p, ','); + int len = end ? (int) (end - p) : (int) strlen (p); + if (hb_variation_from_string (p, len, &vars[n])) + n++; + p = end ? end + 1 : nullptr; + } + hb_font_set_variations (current_font, vars, n); + + rebuild_buffer (custom_text ? current_text : default_text_en); +} + EMSCRIPTEN_KEEPALIVE const char * web_get_text () { diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 0fa28896e..ce321a18b 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -691,6 +691,15 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset _free(buf); } + function applyVariations(settings) { + var len = lengthBytesUTF8(settings) + 1; + var buf = _malloc(len); + stringToUTF8(settings, buf, len); + _web_set_variations(buf); + _free(buf); + requestRedraw(); + } + /* Embed-mode message protocol: host page drives the * demo with { kind: 'text', value } and * { kind: 'font', bytes: ArrayBuffer }. No origin @@ -702,6 +711,8 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset applyText(d.value); else if (d.kind === 'font' && d.bytes) applyFontBytes(d.bytes); + else if (d.kind === 'variations' && typeof d.value === 'string') + applyVariations(d.value); }); /* Tell any parent frame we're ready to receive