Files
harfbuzz/util/gpu/web/shell.html
T
Behdad EsfahbodandClaude Opus 4.6 67ecdf8da7 [gpu] Add text editing modal to web demo
Add a "text" button that opens a modal textarea with the current
text.  Apply sends new text to WASM, flushing the glyph cache and
atlas.  Custom text is preserved across font changes.

Also adds demo_font_clear_cache() and demo_atlas_clear() for
flushing state without full destroy/recreate.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 12:48:44 -06:00

214 lines
8.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>HarfBuzz GPU Demo</title>
<style>
body { margin: 0; background: #222; overflow: hidden; touch-action: none; }
canvas { display: block; width: 100vw; height: 100vh; }
#fps { position: fixed; top: 8px; right: 12px; color: #999;
font: 14px monospace; pointer-events: none; z-index: 1; }
#help { position: fixed; bottom: 12px; left: 12px; color: #999;
font: 12px monospace; pointer-events: none; z-index: 1;
line-height: 1.6; }
#controls { position: fixed; top: 8px; left: 12px; z-index: 1;
display: flex; gap: 6px; }
#controls label { color: #fff; font: 12px monospace; cursor: pointer;
padding: 4px 8px; border: 1px solid #888; border-radius: 4px;
background: rgba(128,128,128,0.5);
text-shadow: 0 1px 2px rgba(0,0,0,0.8); }
#controls label:hover { border-color: #ccc; background: rgba(128,128,128,0.7); }
#controls input[type=file] { display: none; }
#controls button { color: #fff; font: 12px monospace; cursor: pointer;
padding: 4px 8px; border: 1px solid #888; border-radius: 4px;
background: rgba(128,128,128,0.5);
text-shadow: 0 1px 2px rgba(0,0,0,0.8); }
#controls button:hover { border-color: #ccc; background: rgba(128,128,128,0.7); }
#drop-overlay { display: none; position: fixed; inset: 0; z-index: 10;
background: rgba(0,0,0,0.6); color: #fff;
font: 24px monospace; justify-content: center;
align-items: center; }
#text-modal { display: none; position: fixed; inset: 0; z-index: 20;
background: rgba(0,0,0,0.7); justify-content: center;
align-items: center; }
#text-modal-inner { background: #333; border: 1px solid #666; border-radius: 8px;
padding: 16px; width: 90vw; max-width: 1600px; }
#text-modal textarea { width: 100%; height: 70vh; background: #222; color: #ccc;
border: 1px solid #555; border-radius: 4px;
font: 14px monospace; padding: 8px; resize: vertical;
outline: none; box-sizing: border-box; }
#text-modal textarea:focus { border-color: #999; }
#text-modal-buttons { margin-top: 10px; text-align: right; }
#text-modal-buttons button { color: #fff; font: 13px monospace; cursor: pointer;
padding: 6px 16px; border: 1px solid #888;
border-radius: 4px; background: rgba(128,128,128,0.5);
margin-left: 8px; }
#text-modal-buttons button:hover { border-color: #ccc; background: rgba(128,128,128,0.7); }
</style>
</head>
<body>
<div id="fps"></div>
<div id="controls">
<label>font <input type="file" id="font-input" accept=".ttf,.otf"></label>
<button id="text-btn">text</button>
</div>
<div id="drop-overlay">drop font file</div>
<div id="text-modal">
<div id="text-modal-inner">
<textarea id="text-area"></textarea>
<div id="text-modal-buttons">
<button id="text-cancel">cancel</button>
<button id="text-apply">apply</button>
</div>
</div>
</div>
<div id="help">
drag: pan · scroll/pinch: zoom · click/tap: animate · right-drag: rotate<br>
space: animate · b: dark mode · g: gamma · r: reset · =/-: zoom
</div>
<canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex="-1"></canvas>
<script>
var canvas = document.getElementById('canvas');
function resizeCanvas () {
var dpr = window.devicePixelRatio || 1;
canvas.width = Math.round(canvas.clientWidth * dpr);
canvas.height = Math.round(canvas.clientHeight * dpr);
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
var fpsEl = document.getElementById('fps');
var frames = 0, lastTime = performance.now();
(function fpsLoop () {
requestAnimationFrame(fpsLoop);
frames++;
var now = performance.now();
if (now - lastTime >= 1000) {
fpsEl.textContent = Math.round(frames * 1000 / (now - lastTime)) + ' fps';
frames = 0;
lastTime = now;
}
})();
/* Pinch-to-zoom: translate two-finger pinch to wheel events. */
var pinchDist = 0;
canvas.addEventListener('touchstart', function (e) {
if (e.touches.length === 2) {
e.preventDefault();
var t = e.touches;
pinchDist = Math.hypot(t[1].clientX - t[0].clientX,
t[1].clientY - t[0].clientY);
}
}, {passive: false});
canvas.addEventListener('touchmove', function (e) {
if (e.touches.length === 2) {
e.preventDefault();
var t = e.touches;
var dist = Math.hypot(t[1].clientX - t[0].clientX,
t[1].clientY - t[0].clientY);
var change = dist - pinchDist;
/* Dead zone: ignore jitter below 2 pixels. */
if (Math.abs(change) < 2) return;
pinchDist = dist;
/* Scale relative to finger distance for consistent feel. */
var delta = change / dist * 5;
canvas.dispatchEvent(new WheelEvent('wheel', {
deltaY: -delta * 120,
clientX: (t[0].clientX + t[1].clientX) / 2,
clientY: (t[0].clientY + t[1].clientY) / 2
}));
}
}, {passive: false});
/* Block Emscripten/GLFW from eating key events while the text modal is open.
* Registered before Module so it runs before Emscripten's capture-phase handlers. */
var textModalEl = document.getElementById('text-modal');
function blockKeysIfModal(e) {
if (textModalEl.style.display !== 'flex') return;
if (e.key === 'Escape') { textModalEl.style.display = 'none'; e.stopImmediatePropagation(); return; }
e.stopImmediatePropagation();
}
window.addEventListener('keydown', blockKeysIfModal, true);
window.addEventListener('keyup', blockKeysIfModal, true);
window.addEventListener('keypress', blockKeysIfModal, true);
var Module = {
canvas: canvas,
onRuntimeInitialized: function () {
console.log('HarfBuzz GPU Demo loaded');
/* Font file input */
var fontInput = document.getElementById('font-input');
fontInput.addEventListener('change', function () {
if (fontInput.files.length)
loadFontFile(fontInput.files[0]);
});
/* Drag and drop */
var overlay = document.getElementById('drop-overlay');
document.addEventListener('dragover', function (e) {
e.preventDefault();
overlay.style.display = 'flex';
});
document.addEventListener('dragleave', function (e) {
if (e.relatedTarget === null)
overlay.style.display = 'none';
});
document.addEventListener('drop', function (e) {
e.preventDefault();
overlay.style.display = 'none';
if (e.dataTransfer.files.length)
loadFontFile(e.dataTransfer.files[0]);
});
/* Text editor modal */
var textModal = document.getElementById('text-modal');
var textArea = document.getElementById('text-area');
var textBtn = document.getElementById('text-btn');
textBtn.addEventListener('click', function () {
var ptr = _web_get_text();
textArea.value = UTF8ToString(ptr);
textModal.style.display = 'flex';
textArea.focus();
});
document.getElementById('text-cancel').addEventListener('click', function () {
textModal.style.display = 'none';
});
document.getElementById('text-apply').addEventListener('click', function () {
var text = textArea.value;
var len = lengthBytesUTF8(text) + 1;
var buf = _malloc(len);
stringToUTF8(text, buf, len);
_web_set_text(buf);
_free(buf);
textModal.style.display = 'none';
});
/* Prevent mouse events on the modal from reaching the canvas/GLFW. */
textModal.addEventListener('mousedown', function (e) { e.stopPropagation(); });
textModal.addEventListener('mouseup', function (e) { e.stopPropagation(); });
textModal.addEventListener('click', function (e) { e.stopPropagation(); });
function loadFontFile(file) {
var reader = new FileReader();
reader.onload = function () {
var data = new Uint8Array(reader.result);
var buf = _malloc(data.length);
HEAPU8.set(data, buf);
_web_load_font(buf, data.length);
_free(buf);
};
reader.readAsArrayBuffer(file);
}
}
};
</script>
{{{ SCRIPT }}}
</body>
</html>