[util/gpu/web] Add embed-mode + postMessage protocol to the web demo

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) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-04-15 06:09:28 -06:00
1 parent b7a7d33052
commit bdea49603c
1 file changed
+43
+43
View File
@@ -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; }
</style>
</head>
<body>
@@ -545,6 +552,16 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset</span>
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</span>
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.