From 67ecdf8da70ee23e1ed61813ae36645e0c61ac6e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 29 Mar 2026 12:48:44 -0600 Subject: [PATCH] [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) --- util/gpu/demo-font.cc | 8 ++++ util/gpu/demo-font.h | 3 ++ util/gpu/web/build.sh | 3 +- util/gpu/web/hb-gpu-web.cc | 37 ++++++++++++++---- util/gpu/web/shell.html | 78 +++++++++++++++++++++++++++++++++++++- 5 files changed, 119 insertions(+), 10 deletions(-) diff --git a/util/gpu/demo-font.cc b/util/gpu/demo-font.cc index edc565f4c..6a5054eb8 100644 --- a/util/gpu/demo-font.cc +++ b/util/gpu/demo-font.cc @@ -114,6 +114,14 @@ demo_font_lookup_glyph (demo_font_t *font, *glyph_info = (*font->glyph_cache)[glyph_index]; } +void +demo_font_clear_cache (demo_font_t *font) +{ + font->glyph_cache->clear (); + font->num_glyphs = 0; + font->sum_bytes = 0; +} + void demo_font_print_stats (demo_font_t *font) { diff --git a/util/gpu/demo-font.h b/util/gpu/demo-font.h index 4d211f4a7..9e9d24656 100644 --- a/util/gpu/demo-font.h +++ b/util/gpu/demo-font.h @@ -39,6 +39,9 @@ demo_font_lookup_glyph (demo_font_t *font, unsigned int glyph_index, glyph_info_t *glyph_info); +void +demo_font_clear_cache (demo_font_t *font); + void demo_font_print_stats (demo_font_t *font); diff --git a/util/gpu/web/build.sh b/util/gpu/web/build.sh index 51433ab02..d94f9c977 100755 --- a/util/gpu/web/build.sh +++ b/util/gpu/web/build.sh @@ -30,7 +30,8 @@ em++ \ -sUSE_WEBGL2=1 \ -sFULL_ES3=1 \ -sALLOW_MEMORY_GROWTH=1 \ - -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_malloc","_free"]' \ + -sEXPORTED_FUNCTIONS='["_main","_web_load_font","_web_set_text","_web_get_text","_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/hb-gpu-web.cc b/util/gpu/web/hb-gpu-web.cc index 8ef0a2cf3..e51a62cf0 100644 --- a/util/gpu/web/hb-gpu-web.cc +++ b/util/gpu/web/hb-gpu-web.cc @@ -60,10 +60,18 @@ static demo_font_t *current_demo_font; static hb_blob_t *current_blob; static hb_face_t *current_face; static hb_font_t *current_font; +static char *current_text; +static bool custom_text; static void rebuild_buffer (const char *text) { + free (current_text); + current_text = strdup (text); + + demo_font_clear_cache (current_demo_font); + demo_atlas_clear (demo_glstate_get_atlas (st)); + demo_buffer_clear (buffer); demo_point_t top_left = {0, 0}; demo_buffer_move_to (buffer, &top_left); @@ -83,12 +91,6 @@ web_load_font (const char *data, int len) hb_face_t *face = hb_face_create (blob, 0); hb_font_t *font = hb_font_create (face); - /* Flush old state */ - demo_font_destroy (current_demo_font); - demo_atlas_clear (demo_glstate_get_atlas (st)); - - current_demo_font = demo_font_create (font, demo_glstate_get_atlas (st)); - hb_blob_destroy (current_blob); hb_face_destroy (current_face); hb_font_destroy (current_font); @@ -97,10 +99,27 @@ web_load_font (const char *data, int len) current_face = face; current_font = font; - rebuild_buffer (default_text_en); + /* Flush old font state */ + demo_font_destroy (current_demo_font); + current_demo_font = demo_font_create (font, demo_glstate_get_atlas (st)); + + rebuild_buffer (custom_text ? current_text : default_text_en); demo_font_print_stats (current_demo_font); } +EMSCRIPTEN_KEEPALIVE void +web_set_text (const char *utf8) +{ + custom_text = true; + rebuild_buffer (utf8); +} + +EMSCRIPTEN_KEEPALIVE const char * +web_get_text () +{ + return current_text; +} + } /* extern "C" */ static void @@ -186,10 +205,12 @@ main () current_font = hb_font_create (current_face); current_demo_font = demo_font_create (current_font, demo_glstate_get_atlas (st)); + current_text = strdup (default_text_combined); + buffer = demo_buffer_create (); demo_point_t top_left = {0, 0}; demo_buffer_move_to (buffer, &top_left); - demo_buffer_add_text (buffer, default_text_combined, current_demo_font, 1); + demo_buffer_add_text (buffer, current_text, current_demo_font, 1); demo_font_print_stats (current_demo_font); diff --git a/util/gpu/web/shell.html b/util/gpu/web/shell.html index 59ad2466f..6ac6c0911 100644 --- a/util/gpu/web/shell.html +++ b/util/gpu/web/shell.html @@ -12,25 +12,57 @@ #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; } + #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); }
+
drop font file
+
+
+ +
+ + +
+
+
drag: pan · scroll/pinch: zoom · click/tap: animate · right-drag: rotate
space: animate · b: dark mode · g: gamma · r: reset · =/-: zoom @@ -89,6 +121,18 @@ space: animate · b: dark mode · g: gamma · r: reset · =/-: zoom } }, {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 () { @@ -118,6 +162,38 @@ space: animate · b: dark mode · g: gamma · r: reset · =/-: zoom 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 () {