EvenHub: Fix an issue where indentation was incorrectly dropped in text containers

This caused text to overlap with icons in Iris Pro and Ocuclaw
This commit is contained in:
James Babcock committed 2026-11-07 20:57:55 -08:00
1 parent c6fa3647ae
commit c713815694
9 files changed
+610 -86

No files matched your search

+6 -61
View File
@@ -12,7 +12,8 @@
* - Text SIZING / KERNING / WRAPPING: @evenrealities/pretext, whose advance
* tables the bitmaps were validated against. Using pretext for measurement
* means our line breaks match exactly where EvenHub apps (which measure
* with the same library) expect them.
* with the same library) expect them. The one exception is leading spaces
* after a '\n', which stock keeps and pretext drops (see lvgl-wrap.ts).
*
* Bitmaps are 4bpp, high-nibble-first, row stride floor(boxW/2)+1. The UI
* fonts are effectively 1-bit (nibbles 0 or 0xF); we keep the nibble value so
@@ -23,6 +24,7 @@ import { File, knownFolders } from "@nativescript/core";
import { getTextWidth } from "@evenrealities/pretext";
import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts";
import { lvglMetrics, lvglGlyph } from "../native/lvgl-font";
import { wrapLvglText } from "./lvgl-wrap";
import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image";
import { TtfFont } from "./ttf-font";
import { decodeBase64 } from "../util/base64";
@@ -200,63 +202,11 @@ export class EvenHubFont implements FwTextFont {
}
/**
* Wrap text to a pixel width, returning the actual line strings. Ports
* pretext's measureTextWrap (LVGL lv_text_get_next_line semantics) so the
* breaks land exactly where the measurement library reports them.
* Wrap text to a pixel width, returning the actual line strings: pretext's
* breaks, but keeping leading spaces after a '\n' (see lvgl-wrap.ts).
*/
wrap(text: string, maxWidth: number): string[] {
if (!text) return [""];
const cps = Array.from(text).map((c) => c.codePointAt(0)!);
const lines: string[] = [];
let line: number[] = [];
let lineWidth = 0;
let lastBreak = -1; // index within `line` after which we may wrap
for (let i = 0; i < cps.length; i++) {
const cp = cps[i]!;
if (cp === 10) {
lines.push(String.fromCodePoint(...line));
line = [];
lineWidth = 0;
lastBreak = -1;
continue;
}
// LVGL drops leading spaces at the start of a wrapped line.
if (line.length === 0 && cp === 32) continue;
const adv = this.advanceOf(cp, cps[i + 1] ?? 0);
if (lineWidth + adv > maxWidth) {
if (cp === 32) {
// The overflowing char is a space: break here, discard it.
lines.push(String.fromCodePoint(...line));
line = [];
lineWidth = 0;
lastBreak = -1;
} else if (lastBreak !== -1) {
// Wrap at the last break opportunity; carry the remainder forward.
const carry = line.slice(lastBreak + 1);
line = line.slice(0, lastBreak + 1);
const trimmed = line[line.length - 1] === 32 ? line.slice(0, -1) : line;
lines.push(String.fromCodePoint(...trimmed));
line = carry;
lineWidth = carry.reduce((sum, c, j) => sum + this.advanceOf(c, carry[j + 1] ?? 0), 0);
lastBreak = -1;
line.push(cp);
lineWidth += adv;
} else {
// No break opportunity: hard break before the overflowing char.
lines.push(String.fromCodePoint(...line));
line = [cp];
lineWidth = adv;
lastBreak = -1;
}
} else {
line.push(cp);
lineWidth += adv;
if (isBreakable(cp)) lastBreak = line.length - 1;
}
}
lines.push(String.fromCodePoint(...line));
return lines;
return wrapLvglText(text, maxWidth, kernedAdvance);
}
/**
@@ -446,8 +396,3 @@ export class EvenHubFont implements FwTextFont {
}
}
function isBreakable(cp: number): boolean {
if (cp === 32 || cp === 45) return true; // space, hyphen
// CJK ranges (pretext parity) — breakable between any two ideographs.
return (cp >= 0x2e80 && cp <= 0x9fff) || (cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0xac00 && cp <= 0xd7af);
}
+86
View File
@@ -0,0 +1,86 @@
/**
* Word wrap for EvenHub text containers, after LVGL's lv_text_get_next_line
* as ported by @evenrealities/pretext's measureTextWrap, so soft breaks land
* where apps that measure with pretext expect them. The loop mirrors
* measureTextWrap's, including rescanning from the break point when it wraps
* at an earlier opportunity.
*
* One deliberate difference from pretext: pretext skips leading spaces at the
* start of every line, but stock keeps them at the start of the text and after
* a '\n'; only a soft-wrapped continuation line loses them. Apps indent with
* spaces to clear image containers placed beside their text: Iris Pro indents a
* line 11 spaces (44px) past a 40px glyph. Stripping that indent slid the text
* under the icon.
*/
/** Wrap opportunities: space, hyphen, and between CJK ideographs (pretext parity). */
export function isLvglBreakable(cp: number): boolean {
if (cp === 32 || cp === 45) return true;
return (cp >= 0x2e80 && cp <= 0x9fff) || (cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0xac00 && cp <= 0xd7af);
}
/**
* Split text into display lines no wider than maxWidth.
* `advanceOf(cp, next)` is the kerned pixel advance of cp before next (0 at the end).
*/
export function wrapLvglText(text: string, maxWidth: number, advanceOf: (cp: number, next: number) => number): string[] {
if (!text) return [""];
const cps = Array.from(text).map((c) => c.codePointAt(0)!);
const lines: string[] = [];
let line: number[] = [];
let lineWidth = 0;
// The last break opportunity on this line, as an index into cps, and how
// many of `line`'s codepoints a wrap there keeps (a breaking space is dropped).
let lastBreak = -1;
let lastBreakKeep = 0;
// Whether `line` continues a soft-wrapped line, whose leading spaces are dropped.
let continuation = false;
const endLine = (keep: number[], next: number[], nextWidth: number) => {
lines.push(String.fromCodePoint(...keep));
line = next;
lineWidth = nextWidth;
lastBreak = -1;
continuation = true;
};
let i = 0;
while (i < cps.length) {
const cp = cps[i]!;
if (cp === 10) {
endLine(line, [], 0);
continuation = false;
i++;
continue;
}
if (continuation && line.length === 0 && cp === 32) {
i++;
continue;
}
const adv = advanceOf(cp, cps[i + 1] ?? 0);
if (lineWidth + adv <= maxWidth) {
line.push(cp);
lineWidth += adv;
if (isLvglBreakable(cp)) {
lastBreak = i;
lastBreakKeep = cp === 32 ? line.length - 1 : line.length;
}
i++;
} else if (cp === 32) {
// The overflowing char is a space: break here and discard it.
endLine(line, [], 0);
i++;
} else if (lastBreak !== -1) {
// Wrap at the last break opportunity and rescan from just after it.
const restart = lastBreak + 1;
endLine(line.slice(0, lastBreakKeep), [], 0);
i = restart;
} else {
// No break opportunity: hard break before the overflowing char.
endLine(line, [cp], adv);
i++;
}
}
lines.push(String.fromCodePoint(...line));
return lines;
}