mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 07:21:06 +01:00
[util/gpu] Web demos: URL ?text= and ?font=; wider default font
Two user-visible features plus a font swap.
### URL parameters
Forwards ?text= and ?font= from the query string into the C main
as --text=... and --font=... via Module.arguments, so the initial
text is applied before the first rendered frame (no flash of
default demo text). ?font= is still fetched asynchronously on
the JS side; once loaded, the text is re-applied to the new font.
If neither parameter is present, behaviour is unchanged: the
baked-in default text renders in the baked-in default font.
Data URLs (?font=data:font/ttf;base64,...) work naturally via
fetch(). Remote URLs need CORS; raw.githubusercontent.com and
jsdelivr both work, github.com/.../raw/... does not.
### Wider default font
The baked-in default-font.hh was a narrow subset that didn't
include uppercase Latin, so `?text=Hello` shaped to .notdef for
H and W. Re-subset Parastoo-Regular.ttf via hb-subset with:
--unicodes='U+0020-007E,U+0600-06FF,U+FB50-FDFF,U+FE70-FEFF'
covering printable ASCII plus the Arabic block and the two
Arabic presentation-forms blocks.
Font size:
source Parastoo-Regular.ttf 109152
new subset 49940
Wasm size impact (webgl build, Oz+lto):
before 443628 181597 gz
after 466657 196209 gz
delta +23029 +14612 gz
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
44a0fb1cd5
commit
c85b453e90
4 files changed
+2689
-1415
No files matched your search
+2617
-1406
File diff suppressed because it is too large.
Load diff
@@ -360,6 +360,8 @@ static hb_face_t *current_face;
|
||||
static hb_font_t *current_font;
|
||||
static char *current_text;
|
||||
static bool custom_text;
|
||||
static const char *arg_text = nullptr;
|
||||
static const char *arg_font = nullptr;
|
||||
|
||||
|
||||
/* ---- Buffer rebuild ---- */
|
||||
@@ -1019,7 +1021,9 @@ init_demo ()
|
||||
current_demo_font = demo_font_create (current_font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, 0, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
current_text = strdup (default_text_combined);
|
||||
current_text = strdup (arg_text ? arg_text : default_text_combined);
|
||||
if (arg_text)
|
||||
custom_text = true;
|
||||
|
||||
buffer = demo_buffer_create ();
|
||||
demo_point_t top_left = {0, 0};
|
||||
@@ -1064,8 +1068,16 @@ init_demo ()
|
||||
|
||||
|
||||
int
|
||||
main ()
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (!strncmp (argv[i], "--text=", 7))
|
||||
arg_text = argv[i] + 7;
|
||||
else if (!strncmp (argv[i], "--font=", 7))
|
||||
arg_font = argv[i] + 7;
|
||||
}
|
||||
|
||||
emscripten_get_canvas_element_size ("#canvas", &canvas_w, &canvas_h);
|
||||
double css_w_d, css_h_d;
|
||||
emscripten_get_element_css_size ("#canvas", &css_w_d, &css_h_d);
|
||||
|
||||
@@ -188,9 +188,22 @@ static void
|
||||
cursor_func (GLFWwindow *window, double x, double y)
|
||||
{ demo_view_motion_func (vu, x, y); }
|
||||
|
||||
static const char *arg_text = nullptr;
|
||||
static const char *arg_font = nullptr;
|
||||
|
||||
int
|
||||
main ()
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
/* Accept --text=... and --font=... so the JS shell can forward
|
||||
* URL parameters. Everything else is ignored. */
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (!strncmp (argv[i], "--text=", 7))
|
||||
arg_text = argv[i] + 7;
|
||||
else if (!strncmp (argv[i], "--font=", 7))
|
||||
arg_font = argv[i] + 7;
|
||||
}
|
||||
|
||||
if (!glfwInit ())
|
||||
{
|
||||
fprintf (stderr, "Failed to initialize GLFW\n");
|
||||
@@ -239,7 +252,9 @@ main ()
|
||||
current_demo_font = demo_font_create (current_font, renderer->get_atlas ());
|
||||
{ float palette[256*4] = {}; unsigned n = demo_font_get_palette (current_demo_font, 0, palette, 256); if (n) renderer->set_palette (palette, n); }
|
||||
|
||||
current_text = strdup (default_text_combined);
|
||||
current_text = strdup (arg_text ? arg_text : default_text_combined);
|
||||
if (arg_text)
|
||||
custom_text = true;
|
||||
|
||||
buffer = demo_buffer_create ();
|
||||
demo_point_t top_left = {0, 0};
|
||||
|
||||
+41
-5
@@ -536,8 +536,17 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset</span>
|
||||
window.addEventListener('keyup', blockKeysIfModal, true);
|
||||
window.addEventListener('keypress', blockKeysIfModal, true);
|
||||
|
||||
/* URL parameters: forwarded to C main() as argv so main can
|
||||
* apply them before first frame (no default-demo flash). */
|
||||
var urlParams = new URLSearchParams(location.search);
|
||||
var moduleArgs = [];
|
||||
var urlText = urlParams.get('text');
|
||||
var urlFont = urlParams.get('font');
|
||||
if (urlText) moduleArgs.push('--text=' + urlText);
|
||||
|
||||
var Module = {
|
||||
canvas: canvas,
|
||||
arguments: moduleArgs,
|
||||
onRuntimeInitialized: function () {
|
||||
if (!window._webgpuError && !window._webglError) {
|
||||
document.getElementById('loading').style.display = 'none';
|
||||
@@ -588,11 +597,7 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset</span>
|
||||
document.getElementById('text-apply').addEventListener('click', function () {
|
||||
var text = textArea.value.trim();
|
||||
if (!text) { textModal.style.display = 'none'; return; }
|
||||
var len = lengthBytesUTF8(text) + 1;
|
||||
var buf = _malloc(len);
|
||||
stringToUTF8(text, buf, len);
|
||||
_web_set_text(buf);
|
||||
_free(buf);
|
||||
applyText(text);
|
||||
textModal.style.display = 'none';
|
||||
});
|
||||
|
||||
@@ -613,6 +618,37 @@ tap: animate · dbl-tap+drag: zoom · long-press: reset</span>
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function applyText(text) {
|
||||
var len = lengthBytesUTF8(text) + 1;
|
||||
var buf = _malloc(len);
|
||||
stringToUTF8(text, buf, len);
|
||||
_web_set_text(buf);
|
||||
_free(buf);
|
||||
requestRedraw();
|
||||
}
|
||||
|
||||
/* ?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. */
|
||||
if (urlFont) {
|
||||
fetch(urlFont)
|
||||
.then(function (r) {
|
||||
if (!r.ok) throw new Error('HTTP ' + r.status);
|
||||
return r.arrayBuffer();
|
||||
})
|
||||
.then(function (ab) {
|
||||
var data = new Uint8Array(ab);
|
||||
var buf = _malloc(data.length);
|
||||
HEAPU8.set(data, buf);
|
||||
_web_load_font(buf, data.length);
|
||||
_free(buf);
|
||||
if (urlText) applyText(urlText);
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.warn('?font= fetch failed: ' + err.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user