From bdea49603ce3ac6f74d3d5207112ecb7ccc60f10 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 15 Apr 2026 06:09:28 -0600 Subject: [PATCH] [util/gpu/web] Add embed-mode + postMessage protocol to the web demo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lets a host page embed hb-gpu-demo as an iframe and drive it from outside. harfbuzz-world.cc uses this to share its text/font controls with the GPU tab without reinventing the GPU stack. Shell additions: - ?embed=1 URL param hides the demo's own chrome (font picker, text button, backend selector, FPS counter, keyboard-help overlay) by setting html.embed. - Message protocol: { kind: 'text', value: '…' } -> applyText { kind: 'font', bytes: ArrayBuffer } -> _web_load_font On runtime init the shell posts { kind: 'ready' } to its parent so the host can send font bytes (which can't ride on the URL for file-uploaded fonts). Existing ?text= and ?font= URL params still work and are useful for "link to a specific demo" sharing. Co-Authored-By: Claude Opus 4.6 (1M context) --- util/gpu/web/shell.html | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 3332d82c1..b0f92ba55 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -67,6 +67,13 @@ else #help-modal pre { background: #333; color: #ccc; font: 14px monospace; padding: 24px 32px; border-radius: 8px; border: 1px solid #666; line-height: 1.8; } + /* Embed mode (?embed=1): hide UI chrome; host page drives + * the demo via postMessage. */ + html.embed #controls, + html.embed #fps, + html.embed #help, + html.embed #help-mobile, + html.embed #text-btn { display: none !important; } @@ -545,6 +552,16 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset if (urlText) moduleArgs.push('--text=' + urlText); if (urlFont) moduleArgs.push('--font=' + urlFont); + /* Embed mode: hide the demo's own chrome (font picker, + * text button, backend selector, help overlays) so a host + * page embedding this in an iframe can drive it purely + * through postMessage. The canvas-only view keeps the + * FPS counter and the loading overlay. */ + var embedMode = urlParams.get('embed') === '1'; + if (embedMode) { + document.documentElement.classList.add('embed'); + } + var Module = { canvas: canvas, arguments: moduleArgs, @@ -637,6 +654,32 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset requestRedraw(); } + function applyFontBytes(bytes) { + var data = new Uint8Array(bytes); + var buf = _malloc(data.length); + HEAPU8.set(data, buf); + _web_load_font(buf, data.length); + _free(buf); + } + + /* Embed-mode message protocol: host page drives the + * demo with { kind: 'text', value } and + * { kind: 'font', bytes: ArrayBuffer }. No origin + * check -- hb-gpu-demo is read-only w.r.t. the host. */ + window.addEventListener('message', function (e) { + var d = e.data; + if (!d || typeof d !== 'object') return; + if (d.kind === 'text' && typeof d.value === 'string') + applyText(d.value); + else if (d.kind === 'font' && d.bytes) + applyFontBytes(d.bytes); + }); + + /* Tell any parent frame we're ready to receive + * messages. Host may have queued them during load. */ + if (window.parent && window.parent !== window) + window.parent.postMessage({ kind: 'ready' }, '*'); + /* ?text= is applied pre-main via Module.arguments; here we * handle the async ?font= fetch. Text (if any) is re-applied * after the font loads.