[util/gpu/web] Live variations via postMessage

Add web_set_variations(const char *) to both the WebGL and
WebGPU hb-gpu-web wasm modules.  Parses a comma-separated
"wght=400,wdth=100" string via hb_variation_from_string,
applies via hb_font_set_variations on the current font, and
rebuilds the shaping buffer.  Cheap to call repeatedly --
suitable for live slider pushes once a slider UI lands.

Shell.html grows a matching applyVariations() helper and a
'variations' postMessage case.  config-override.h undefs
HB_NO_VAR so the new function actually links.  EXPORTED
_FUNCTIONS on both build scripts list _web_set_variations.

Also plumbed in a small cap (32 axes) on the stack so the
parser doesn't need hb's internal containers -- no font has
anywhere near that many.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-04-15 12:57:18 -06:00
1 parent 7fe31859ca
commit d17ff1e8d6
6 files changed
+61 -2

No files matched your search

+1 -1
View File
@@ -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"
+1 -1
View File
@@ -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"
+1
View File
@@ -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 */
+21
View File
@@ -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 ()
{
+26
View File
@@ -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 ()
{
+11
View File
@@ -691,6 +691,15 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset</span>
_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</span>
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