From a82184316ece5e1e4df449d2a6bf877854022550 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 25 Oct 2026 16:09:12 -0700 Subject: [PATCH] Fix word-wrapping in iOS Spotify setup help text --- app/graphics/textwrap.ts | 15 ++++++++++++++- tests/textwrap.test.cjs | 35 +++++++++++++++++++++++++++++++++++ tests/tsconfig.json | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/textwrap.test.cjs diff --git a/app/graphics/textwrap.ts b/app/graphics/textwrap.ts index 3d2d4ff5..3f5622e4 100644 --- a/app/graphics/textwrap.ts +++ b/app/graphics/textwrap.ts @@ -11,6 +11,11 @@ export interface WrapFont { export type WrapTextOptions = { preserveLeadingWhitespace?: boolean; + /** + * A word too wide for a line of its own starts on the current line and is + * split at the width, rather than first being pushed to a line of its own. + * Words that fit on a line still wrap whole. + */ breakLongWords?: boolean; }; @@ -89,17 +94,20 @@ function wrapParagraph(font: WrapFont, paragraph: string, targetWidth: number, o while (start < paragraph.length) { let best = -1; + let overflow = -1; for (const candidate of wrapOffsets) { if (candidate <= start) continue; if (measureSpan(widths, candidate) - measureSpan(widths, start) <= targetWidth) { best = candidate; continue; } + overflow = candidate; break; } const splitOffset = furthestFittingOffset(widths, start, targetWidth); - if (opts.breakLongWords && splitOffset > start && (best < 0 || splitOffset > trimEndOffset(paragraph, best))) { + if (opts.breakLongWords && splitOffset > start && splitOffset > best + && (best < 0 || (overflow >= 0 && wordWidth(paragraph, widths, best, overflow) > targetWidth))) { best = splitOffset; } if (best < 0) { @@ -157,6 +165,11 @@ function measureSpan(widths: Map, offset: number): number { return widths.get(offset) ?? 0; } +/** Width of the word between two wrap offsets, without surrounding whitespace. */ +function wordWidth(text: string, widths: Map, from: number, to: number): number { + return measureSpan(widths, trimEndOffset(text, to)) - measureSpan(widths, skipLeadingWhitespace(text, from)); +} + function skipLeadingWhitespace(text: string, offset: number): number { let cursor = offset; while (cursor < text.length) { diff --git a/tests/textwrap.test.cjs b/tests/textwrap.test.cjs new file mode 100644 index 00000000..08a00b6e --- /dev/null +++ b/tests/textwrap.test.cjs @@ -0,0 +1,35 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { wrapText } = require('../.test-build/app/graphics/textwrap.js'); + +// Monospace: every character is one unit wide, so widths count characters. +const mono = { + measureText: (text) => [...text].length, + getGlyph: () => ({ dwidthX: 1 }), +}; + +test('breakLongWords keeps words that fit on a line whole', () => { + const text = 'Spotify plays on whichever of your devices is open'; + const expected = ['Spotify plays on', 'whichever of your', 'devices is open']; + assert.deepEqual(wrapText(mono, text, 18), expected); + assert.deepEqual(wrapText(mono, text, 18, { breakLongWords: true }), expected); +}); + +test('breakLongWords splits a word wider than a line, starting it on the current line', () => { + assert.deepEqual( + wrapText(mono, 'see abcdefghijklmnop end', 8, { breakLongWords: true }), + ['see abcd', 'efghijkl', 'mnop end'], + ); +}); + +test('a word wider than a line is split when nothing else fits', () => { + assert.deepEqual(wrapText(mono, 'abcdefghij', 4), ['abcd', 'efgh', 'ij']); + assert.deepEqual(wrapText(mono, 'abcdefghij', 4, { breakLongWords: true }), ['abcd', 'efgh', 'ij']); +}); + +test('wrapping still breaks after slashes and hyphens', () => { + assert.deepEqual( + wrapText(mono, 'URI: http://127.0.0.1:43823/spotify-callback', 20, { breakLongWords: true }), + ['URI: http://', '127.0.0.1:43823/', 'spotify-callback'], + ); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 843e6875..a403f3ee 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -20,6 +20,7 @@ "../app/apps/terminal/hub-header.ts", "../app/g2/ios-peripheral-identity.ts", "../app/graphics/image.ts", + "../app/graphics/textwrap.ts", "../app/graphics/battery.ts", "../app/graphics/plane.ts", "../app/graphics/shell-scene.ts",