forked from Blendman974/faceclaw
Fix word-wrapping in iOS Spotify setup help text
This commit is contained in:
1 parent
daa306aa7c
commit
a82184316e
3 files changed
+50
-1
No files matched your search
@@ -11,6 +11,11 @@ export interface WrapFont {
|
|||||||
|
|
||||||
export type WrapTextOptions = {
|
export type WrapTextOptions = {
|
||||||
preserveLeadingWhitespace?: boolean;
|
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;
|
breakLongWords?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,17 +94,20 @@ function wrapParagraph(font: WrapFont, paragraph: string, targetWidth: number, o
|
|||||||
|
|
||||||
while (start < paragraph.length) {
|
while (start < paragraph.length) {
|
||||||
let best = -1;
|
let best = -1;
|
||||||
|
let overflow = -1;
|
||||||
for (const candidate of wrapOffsets) {
|
for (const candidate of wrapOffsets) {
|
||||||
if (candidate <= start) continue;
|
if (candidate <= start) continue;
|
||||||
if (measureSpan(widths, candidate) - measureSpan(widths, start) <= targetWidth) {
|
if (measureSpan(widths, candidate) - measureSpan(widths, start) <= targetWidth) {
|
||||||
best = candidate;
|
best = candidate;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
overflow = candidate;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const splitOffset = furthestFittingOffset(widths, start, targetWidth);
|
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;
|
best = splitOffset;
|
||||||
}
|
}
|
||||||
if (best < 0) {
|
if (best < 0) {
|
||||||
@@ -157,6 +165,11 @@ function measureSpan(widths: Map<number, number>, offset: number): number {
|
|||||||
return widths.get(offset) ?? 0;
|
return widths.get(offset) ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Width of the word between two wrap offsets, without surrounding whitespace. */
|
||||||
|
function wordWidth(text: string, widths: Map<number, number>, from: number, to: number): number {
|
||||||
|
return measureSpan(widths, trimEndOffset(text, to)) - measureSpan(widths, skipLeadingWhitespace(text, from));
|
||||||
|
}
|
||||||
|
|
||||||
function skipLeadingWhitespace(text: string, offset: number): number {
|
function skipLeadingWhitespace(text: string, offset: number): number {
|
||||||
let cursor = offset;
|
let cursor = offset;
|
||||||
while (cursor < text.length) {
|
while (cursor < text.length) {
|
||||||
|
|||||||
@@ -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'],
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
"../app/apps/terminal/hub-header.ts",
|
"../app/apps/terminal/hub-header.ts",
|
||||||
"../app/g2/ios-peripheral-identity.ts",
|
"../app/g2/ios-peripheral-identity.ts",
|
||||||
"../app/graphics/image.ts",
|
"../app/graphics/image.ts",
|
||||||
|
"../app/graphics/textwrap.ts",
|
||||||
"../app/graphics/battery.ts",
|
"../app/graphics/battery.ts",
|
||||||
"../app/graphics/plane.ts",
|
"../app/graphics/plane.ts",
|
||||||
"../app/graphics/shell-scene.ts",
|
"../app/graphics/shell-scene.ts",
|
||||||
|
|||||||
Reference in new issue
Block a user