File size: 17,541 Bytes
e079a73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | import { TextMeasurer } from "../measurer/TextMeasurer";
import { isCJKCharacter, needsHyphenForBreak } from "../measurer/script-detection";
import type { WrapOptions, WrapResult, LineMetrics, BreakMode } from "./types";
import { DEFAULT_WRAP_OPTIONS } from "./types";
/**
* Wraps text to fit display constraints.
*
* Supports multiple break modes:
* - 'character': Break mid-word with hyphen for 100% line utilization
* - 'word': Break at word boundaries, hyphenate only if word > line width
* - 'strict-word': Break at word boundaries only, no hyphenation
*
* Key features:
* - Pixel-accurate wrapping (no abstract units)
* - Hyphen-aware breaking (accounts for hyphen width)
* - CJK support (breaks anywhere without hyphen)
* - Preserves explicit newlines
* - Respects byte limits for BLE transmission
*/
export class TextWrapper {
private readonly measurer: TextMeasurer;
private readonly defaultOptions: Required<WrapOptions>;
constructor(measurer: TextMeasurer, defaultOptions?: WrapOptions) {
this.measurer = measurer;
const profile = measurer.getProfile();
this.defaultOptions = {
maxWidthPx: profile.displayWidthPx,
maxLines: profile.maxLines,
maxBytes: profile.maxPayloadBytes,
...DEFAULT_WRAP_OPTIONS,
...defaultOptions,
};
}
/**
* Wrap text to fit within constraints.
*
* @param text - Text to wrap (may contain \n for explicit breaks)
* @param options - Override default options
* @returns Wrap result with lines and metadata
*/
wrap(text: string, options?: WrapOptions): WrapResult {
const originalText = text;
text = this.measurer.getProfile().normalizeText?.(text) ?? text;
const opts = this.mergeOptions(options);
const { maxWidthPx, maxLines, maxBytes, breakMode, preserveNewlines } = opts;
if (!text || text.length === 0) {
return this.createEmptyResult(opts);
}
// Split by explicit newlines if preserving them
const paragraphs = preserveNewlines ? text.split("\n") : [text];
const allLines: string[] = [];
const allMetrics: LineMetrics[] = [];
let totalBytes = 0;
let truncated = false;
for (let pIndex = 0; pIndex < paragraphs.length; pIndex++) {
const paragraph = paragraphs[pIndex];
const isFromNewline = pIndex > 0;
// Wrap this paragraph
const paragraphLines = this.wrapParagraph(paragraph, maxWidthPx, breakMode, opts);
for (let lIndex = 0; lIndex < paragraphLines.length; lIndex++) {
const line = paragraphLines[lIndex];
const lineBytes = this.measurer.getByteSize(line);
// Check if we've exceeded limits
if (allLines.length >= maxLines) {
truncated = true;
break;
}
if (totalBytes + lineBytes > maxBytes) {
truncated = true;
break;
}
const widthPx = this.measurer.measureText(line);
const metrics: LineMetrics = {
text: line,
widthPx,
bytes: lineBytes,
utilizationPercent: Math.round((widthPx / maxWidthPx) * 100),
endsWithHyphen: line.endsWith(opts.hyphenChar) && lIndex < paragraphLines.length - 1,
fromExplicitNewline: isFromNewline && lIndex === 0,
};
allLines.push(line);
allMetrics.push(metrics);
totalBytes += lineBytes;
// Add newline byte between lines
if (allLines.length < maxLines) {
totalBytes += 1; // \n is 1 byte
}
}
if (truncated) break;
}
// Find max line width
const maxLineWidthPx = allMetrics.reduce((max, m) => Math.max(max, m.widthPx), 0);
return {
lines: allLines,
truncated,
maxLineWidthPx,
totalBytes,
lineMetrics: allMetrics,
originalText,
breakMode,
};
}
/**
* Simple wrap returning just lines (convenience method).
*
* @param text - Text to wrap
* @param options - Override default options
* @returns Array of wrapped lines
*/
wrapToLines(text: string, options?: WrapOptions): string[] {
return this.wrap(text, options).lines;
}
/**
* Check if text needs wrapping.
*
* @param text - Text to check
* @param maxWidthPx - Optional width override
* @returns true if text exceeds single line
*/
needsWrap(text: string, maxWidthPx?: number): boolean {
const width = maxWidthPx ?? this.defaultOptions.maxWidthPx;
return this.measurer.measureText(text) > width || text.includes("\n");
}
/**
* Get current default options.
*/
getOptions(): Required<WrapOptions> {
return { ...this.defaultOptions };
}
/**
* Get the measurer instance.
*/
getMeasurer(): TextMeasurer {
return this.measurer;
}
/**
* Wrap a single paragraph (no newlines) according to break mode.
*/
private wrapParagraph(
paragraph: string,
maxWidthPx: number,
breakMode: BreakMode,
opts: Required<WrapOptions>,
): string[] {
const trimmed = opts.trimLines ? paragraph.trim() : paragraph;
if (!trimmed) {
return [""];
}
// Check if it fits on one line
if (this.measurer.fitsInWidth(trimmed, maxWidthPx)) {
return [trimmed];
}
switch (breakMode) {
case "character":
return this.wrapCharacterMode(trimmed, maxWidthPx, opts);
case "character-no-hyphen":
return this.wrapCharacterNoHyphenMode(trimmed, maxWidthPx, opts);
case "word":
return this.wrapWordMode(trimmed, maxWidthPx, opts);
case "strict-word":
return this.wrapStrictWordMode(trimmed, maxWidthPx, opts);
default:
return this.wrapCharacterMode(trimmed, maxWidthPx, opts);
}
}
/**
* Character break mode: Break mid-word with hyphen for 100% line utilization.
*/
private wrapCharacterMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
return this.wrapCharacterModeInternal(text, maxWidthPx, opts, true);
}
/**
* Character break mode without hyphen: Break mid-word cleanly without adding hyphen.
*/
private wrapCharacterNoHyphenMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
return this.wrapCharacterModeInternal(text, maxWidthPx, opts, false);
}
/**
* Internal character break implementation with configurable hyphenation.
*/
private wrapCharacterModeInternal(
text: string,
maxWidthPx: number,
opts: Required<WrapOptions>,
useHyphen: boolean,
): string[] {
const lines: string[] = [];
const hyphenWidth = this.measurer.measureChar(opts.hyphenChar);
let currentLine = "";
let currentWidth = 0;
const chars = Array.from(text);
for (let i = 0; i < chars.length; i++) {
const char = chars[i];
const charWidth = this.measurer.measureChar(char);
// Would this character fit?
if (currentWidth + charWidth <= maxWidthPx) {
currentLine += char;
currentWidth += charWidth;
} else {
// Character doesn't fit - need to break
// Determine if we need a hyphen
const prevChar = currentLine.length > 0 ? currentLine[currentLine.length - 1] : "";
const needsHyphen =
useHyphen && currentLine.length >= opts.minCharsBeforeHyphen && needsHyphenForBreak(prevChar, char);
if (needsHyphen) {
// Back off characters until hyphen fits
const result = this.backoffForHyphen(currentLine, currentWidth, maxWidthPx, hyphenWidth, opts);
if (result.skipHyphen) {
// Found a natural word boundary while backing off - no hyphen needed
lines.push(result.line);
} else {
lines.push(result.line + opts.hyphenChar);
}
// Start new line with backed-off chars + current char
currentLine = result.remainder + char;
currentWidth = this.measurer.measureText(currentLine);
} else if (useHyphen === false && !isCJKCharacter(char) && char !== " ") {
// No-hyphen mode: just break cleanly without hyphen
const trimmedLine = opts.trimLines ? currentLine.trimEnd() : currentLine;
if (trimmedLine) {
lines.push(trimmedLine);
}
currentLine = char;
currentWidth = charWidth;
} else {
// No hyphen needed (CJK, space, punctuation)
const trimmedLine = opts.trimLines ? currentLine.trimEnd() : currentLine;
if (trimmedLine) {
lines.push(trimmedLine);
}
currentLine = char === " " ? "" : char;
currentWidth = char === " " ? 0 : charWidth;
}
}
}
// Add remaining text
if (currentLine) {
const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine;
if (trimmedLine) {
lines.push(trimmedLine);
}
}
return lines.length > 0 ? lines : [""];
}
/**
* Word break mode: Break at word boundaries, hyphenate only if word > line width.
*/
private wrapWordMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
const lines: string[] = [];
const words = this.splitIntoWords(text);
const spaceWidth = this.measurer.getSpaceWidth();
let currentLine = "";
let currentWidth = 0;
for (const word of words) {
const wordWidth = this.measurer.measureText(word);
const needsSpace = currentLine.length > 0;
const totalWidth = currentWidth + (needsSpace ? spaceWidth : 0) + wordWidth;
if (totalWidth <= maxWidthPx) {
// Word fits on current line
if (needsSpace) {
currentLine += " ";
currentWidth += spaceWidth;
}
currentLine += word;
currentWidth += wordWidth;
} else {
// Word doesn't fit
if (currentLine.length > 0) {
// Push current line and start new one
lines.push(opts.trimLines ? currentLine.trim() : currentLine);
currentLine = "";
currentWidth = 0;
}
// Check if word itself exceeds line width
if (wordWidth > maxWidthPx) {
// Word is too long - hyphenate it
const hyphenatedLines = this.hyphenateLongWord(word, maxWidthPx, opts);
for (let i = 0; i < hyphenatedLines.length - 1; i++) {
lines.push(hyphenatedLines[i]);
}
// Last piece becomes start of current line
currentLine = hyphenatedLines[hyphenatedLines.length - 1];
currentWidth = this.measurer.measureText(currentLine);
} else {
// Word fits on new line
currentLine = word;
currentWidth = wordWidth;
}
}
}
// Add remaining text
if (currentLine) {
const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine;
if (trimmedLine) {
lines.push(trimmedLine);
}
}
return lines.length > 0 ? lines : [""];
}
/**
* Strict word break mode: Break at word boundaries only, no hyphenation.
* Long words will overflow the line.
*/
private wrapStrictWordMode(text: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
const lines: string[] = [];
const words = this.splitIntoWords(text);
const spaceWidth = this.measurer.getSpaceWidth();
let currentLine = "";
let currentWidth = 0;
for (const word of words) {
const wordWidth = this.measurer.measureText(word);
const needsSpace = currentLine.length > 0;
const totalWidth = currentWidth + (needsSpace ? spaceWidth : 0) + wordWidth;
if (totalWidth <= maxWidthPx) {
// Word fits on current line
if (needsSpace) {
currentLine += " ";
currentWidth += spaceWidth;
}
currentLine += word;
currentWidth += wordWidth;
} else {
// Word doesn't fit
if (currentLine.length > 0) {
// Push current line
lines.push(opts.trimLines ? currentLine.trim() : currentLine);
}
// Start new line with word (even if it overflows)
currentLine = word;
currentWidth = wordWidth;
}
}
// Add remaining text
if (currentLine) {
const trimmedLine = opts.trimLines ? currentLine.trim() : currentLine;
if (trimmedLine) {
lines.push(trimmedLine);
}
}
return lines.length > 0 ? lines : [""];
}
/**
* Split text into words, handling CJK characters specially.
* CJK characters are treated as individual "words" since they can break anywhere.
*/
private splitIntoWords(text: string): string[] {
const words: string[] = [];
let currentWord = "";
for (const char of text) {
if (char === " " || char === "\t") {
if (currentWord) {
words.push(currentWord);
currentWord = "";
}
} else if (isCJKCharacter(char)) {
// CJK characters are individual words
if (currentWord) {
words.push(currentWord);
currentWord = "";
}
words.push(char);
} else {
currentWord += char;
}
}
if (currentWord) {
words.push(currentWord);
}
return words;
}
/**
* Hyphenate a word that's too long to fit on a single line.
*/
private hyphenateLongWord(word: string, maxWidthPx: number, opts: Required<WrapOptions>): string[] {
const lines: string[] = [];
const hyphenWidth = this.measurer.measureChar(opts.hyphenChar);
const chars = Array.from(word);
let currentLine = "";
let currentWidth = 0;
for (let i = 0; i < chars.length; i++) {
const char = chars[i];
const charWidth = this.measurer.measureChar(char);
const isLastChar = i === chars.length - 1;
// Check if we need to break (leaving room for hyphen if not last char)
const widthNeeded = isLastChar ? charWidth : charWidth + hyphenWidth;
if (currentWidth + widthNeeded <= maxWidthPx) {
currentLine += char;
currentWidth += charWidth;
} else {
// Need to break - back off if needed to fit hyphen
if (currentLine.length >= opts.minCharsBeforeHyphen) {
const result = this.backoffForHyphen(currentLine, currentWidth, maxWidthPx, hyphenWidth, opts);
if (result.skipHyphen) {
lines.push(result.line);
} else {
lines.push(result.line + opts.hyphenChar);
}
currentLine = result.remainder + char;
currentWidth = this.measurer.measureText(currentLine);
} else {
// Not enough chars for hyphen, just break
if (currentLine) {
lines.push(currentLine);
}
currentLine = char;
currentWidth = charWidth;
}
}
}
if (currentLine) {
lines.push(currentLine);
}
return lines.length > 0 ? lines : [word];
}
/**
* Back off characters from line end until hyphen fits.
* Returns null if we back off to a space (natural break point - no hyphen needed).
*/
private backoffForHyphen(
line: string,
lineWidth: number,
maxWidthPx: number,
hyphenWidth: number,
opts: Required<WrapOptions>,
): { line: string; remainder: string; skipHyphen: boolean } {
let adjustedLine = line;
let adjustedWidth = lineWidth;
let remainder = "";
// Remove characters until hyphen fits
while (adjustedWidth + hyphenWidth > maxWidthPx && adjustedLine.length > opts.minCharsBeforeHyphen) {
const glyphs = Array.from(adjustedLine);
const lastChar = glyphs[glyphs.length - 1];
const lastCharWidth = this.measurer.measureChar(lastChar);
adjustedLine = adjustedLine.slice(0, -lastChar.length);
adjustedWidth -= lastCharWidth;
remainder = lastChar + remainder;
// If we've backed off to a space, we've found a natural word boundary
// No hyphen is needed - just trim the space and break there
if (adjustedLine.length > 0 && adjustedLine[adjustedLine.length - 1] === " ") {
// Trim trailing space from the line
adjustedLine = adjustedLine.trimEnd();
return { line: adjustedLine, remainder, skipHyphen: true };
}
}
return { line: adjustedLine, remainder, skipHyphen: false };
}
/**
* Merge user options with defaults.
*/
private mergeOptions(options?: WrapOptions): Required<WrapOptions> {
return {
...this.defaultOptions,
...options,
};
}
/**
* Create an empty result for empty input.
*/
private createEmptyResult(opts: Required<WrapOptions>): WrapResult {
return {
lines: [""],
truncated: false,
maxLineWidthPx: 0,
totalBytes: 0,
lineMetrics: [
{
text: "",
widthPx: 0,
bytes: 0,
utilizationPercent: 0,
endsWithHyphen: false,
fromExplicitNewline: false,
},
],
originalText: "",
breakMode: opts.breakMode,
};
}
}
|