From 114257d1a870e211c94854c77578fe01b0f7fe74 Mon Sep 17 00:00:00 2001 From: EzraEn <7076802+ezra-en@users.noreply.github.com> Date: Wed, 8 Apr 2026 03:06:30 +0800 Subject: [PATCH] demo: add Knuth-Plass optimal line-breaking with fitness classification --- pages/demos/index.html | 5 + pages/demos/optimal-line-breaking.html | 217 ++++++++++++++ pages/demos/optimal-line-breaking.model.ts | 328 +++++++++++++++++++++ pages/demos/optimal-line-breaking.ts | 135 +++++++++ pages/demos/optimal-line-breaking.ui.ts | 124 ++++++++ 5 files changed, 809 insertions(+) create mode 100644 pages/demos/optimal-line-breaking.html create mode 100644 pages/demos/optimal-line-breaking.model.ts create mode 100644 pages/demos/optimal-line-breaking.ts create mode 100644 pages/demos/optimal-line-breaking.ui.ts diff --git a/pages/demos/index.html b/pages/demos/index.html index c03d4a80..9fdeee6e 100644 --- a/pages/demos/index.html +++ b/pages/demos/index.html @@ -141,6 +141,11 @@
A text-card occlusion demo where height prediction comes from Pretext instead of DOM reads.
+ + +Full Knuth-Plass with badness, penalties, fitness classification, and river detection.
+ + + ++ A full Knuth-Plass implementation showing dynamic programming for optimal line breaks, + badness calculation, fitness classification, and typography river detection. +
+ ++ The Knuth-Plass algorithm finds the optimal line breaks by treating text layout as a + dynamic programming problem. It evaluates all possible break points and chooses the + sequence that minimizes total "badness" — a measure of how far each line deviates + from the ideal inter-word spacing. +
+
+ Badness formula: (slack / lineWidth)³ × 1000 — cubic
+ penalty that heavily disfavors very tight or very loose lines.
+
+ Penalties: River gaps (+5000 when spaces align vertically), + tight lines (+3000), and hyphenation (+50) all increase badness. +
++ Fitness classification: Lines are categorized as tight (≤65% stretch), + decent, loose (100-150%), or very loose (>150%, indicating potential rivers). +
+diff --git a/pages/demos/optimal-line-breaking.html b/pages/demos/optimal-line-breaking.html new file mode 100644 index 00000000..96756084 --- /dev/null +++ b/pages/demos/optimal-line-breaking.html @@ -0,0 +1,217 @@ + + +
+ + + +
+ + +
+