feat(web): lazy per-script font loading for international CAD text

The web build has no system-font access, so CAD text in non-Latin scripts
(Cyrillic, Greek, CJK, …) rendered as nothing — build_fallback was a hard
None on wasm (#141). Instead, ship per-script Noto subsets under web/fonts
(one alphabet per file, SIL OFL 1.1) and fetch them lazily over HTTP the
first time a drawing uses that script, then outline glyphs with ttf-parser.

- web_font: Script enum + script_of (char → script font), a per-script
  store with lazy fetch, and a pending queue drained by the app loop.
- CJK is split by language (chinese/japanese/korean). Han ideographs share
  code points but differ by language, so the shared block is routed by the
  document's $DWGCODEPAGE (932→JP, 949→KR, GB/936→CN); kana is always
  Japanese, Hangul always Korean. Re-tessellates when the language changes.
- build_fallback (wasm) outlines from the fetched subset; clear_fallback_cache
  lets glyphs that missed while a font was in flight reappear on load.
- PollWebFonts subscription (web only) + WebFontLoaded message drive fetches.
- Trunk copy-dir serves web/fonts; nothing is bundled into the native binary
  (desktop keeps using system fonts).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-06-20 16:13:26 +03:00
commit 54106ca9b0
21 changed files with 453 additions and 5 deletions

102
web/fonts/OFL.txt Normal file
View file

@ -0,0 +1,102 @@
The fonts in this directory are subsets of Google's Noto fonts, generated by
generate.sh. They are redistributed under the SIL Open Font License v1.1.
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/latin-greek-cyrillic)
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/arabic)
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/hebrew)
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/thai)
Copyright 2022 The Noto Project Authors (https://github.com/notofonts/devanagari)
Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'. (Noto Sans CJK)
Noto is a trademark of Google LLC. Noto has no Reserved Font Name.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
https://openfontlicense.org
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

BIN
web/fonts/arabic.ttf Normal file

Binary file not shown.

BIN
web/fonts/chinese.ttf Normal file

Binary file not shown.

BIN
web/fonts/cyrillic.ttf Normal file

Binary file not shown.

BIN
web/fonts/devanagari.ttf Normal file

Binary file not shown.

46
web/fonts/generate.sh Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env bash
# Regenerate the per-script Noto subsets served to the web build.
# Each script lives in its own TTF so a browser only fetches the alphabets a
# drawing actually uses. Source: Noto fonts (SIL OFL 1.1 — see OFL.txt).
#
# Requires: pip install --break-system-packages fonttools brotli
set -euo pipefail
RAW="https://github.com/notofonts/notofonts.github.io/raw/main/fonts"
sub() { python3 -m fontTools.subset "$1" --unicodes="$2" --output-file="$3" \
--no-hinting --desubroutinize --layout-features='*' --notdef-outline \
--name-IDs='*' --recalc-bounds 2>/dev/null; }
dl() { curl -sL -o "$1" "$2"; }
# Latin / Cyrillic / Greek share one Noto source.
dl /tmp/NotoSans.ttf "$RAW/NotoSans/hinted/ttf/NotoSans-Regular.ttf"
sub /tmp/NotoSans.ttf "U+0000-024F,U+1E00-1EFF,U+2000-206F,U+20A0-20BF,U+2122,U+2190-21FF,U+2200-22FF" latin.ttf
sub /tmp/NotoSans.ttf "U+0400-04FF,U+0500-052F,U+2DE0-2DFF,U+A640-A69F" cyrillic.ttf
sub /tmp/NotoSans.ttf "U+0370-03FF,U+1F00-1FFF" greek.ttf
# One source per remaining script.
dl /tmp/NotoArabic.ttf "$RAW/NotoSansArabic/hinted/ttf/NotoSansArabic-Regular.ttf"
sub /tmp/NotoArabic.ttf "U+0600-06FF,U+0750-077F,U+08A0-08FF,U+FB50-FDFF,U+FE70-FEFF" arabic.ttf
dl /tmp/NotoHebrew.ttf "$RAW/NotoSansHebrew/hinted/ttf/NotoSansHebrew-Regular.ttf"
sub /tmp/NotoHebrew.ttf "U+0590-05FF,U+FB1D-FB4F" hebrew.ttf
dl /tmp/NotoThai.ttf "$RAW/NotoSansThai/hinted/ttf/NotoSansThai-Regular.ttf"
sub /tmp/NotoThai.ttf "U+0E00-0E7F" thai.ttf
dl /tmp/NotoDeva.ttf "$RAW/NotoSansDevanagari/hinted/ttf/NotoSansDevanagari-Regular.ttf"
sub /tmp/NotoDeva.ttf "U+0900-097F,U+A8E0-A8FF" devanagari.ttf
# CJK comes from the noto-cjk repo (CFF/OTF; ttf-parser reads CFF outlines) and
# is split by language: Chinese, Japanese and Korean each get their own file.
# Han ideographs share code points but differ in glyph shape, so each language
# ships its own. These are the heavy ones — lazy-loaded per language only when a
# drawing in that language is opened.
CJK="https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF"
dl /tmp/NotoSC.otf "$CJK/SimplifiedChinese/NotoSansCJKsc-Regular.otf"
sub /tmp/NotoSC.otf "U+3000-303F,U+4E00-9FFF,U+FF00-FFEF" chinese.ttf
dl /tmp/NotoJP.otf "$CJK/Japanese/NotoSansCJKjp-Regular.otf"
sub /tmp/NotoJP.otf "U+3000-303F,U+3040-30FF,U+31F0-31FF,U+4E00-9FFF,U+FF00-FFEF" japanese.ttf
dl /tmp/NotoKR.otf "$CJK/Korean/NotoSansCJKkr-Regular.otf"
sub /tmp/NotoKR.otf "U+1100-11FF,U+3130-318F,U+AC00-D7A3" korean.ttf

BIN
web/fonts/greek.ttf Normal file

Binary file not shown.

BIN
web/fonts/hebrew.ttf Normal file

Binary file not shown.

BIN
web/fonts/japanese.ttf Normal file

Binary file not shown.

BIN
web/fonts/korean.ttf Normal file

Binary file not shown.

BIN
web/fonts/latin.ttf Normal file

Binary file not shown.

BIN
web/fonts/thai.ttf Normal file

Binary file not shown.