Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(engine-render): handle the case that one word would be too long in break-line algo (#5286)

ZhangWeie4ea4595b958
diff --git a/packages/engine-render/src/components/docs/layout/doc-simple-skeleton.ts b/packages/engine-render/src/components/docs/layout/doc-simple-skeleton.ts
index d3423dd689..ab51181584 100644
--- a/packages/engine-render/src/components/docs/layout/doc-simple-skeleton.ts
+++ b/packages/engine-render/src/components/docs/layout/doc-simple-skeleton.ts
@@ -28,6 +28,7 @@ export class DocSimpleSkeleton {
private _lineBreaker: LineBreaker;
private _lines: ILineInfo[] = [];
private _dirty = true;
+ private _lastBreakLength = 0; // Cache for the last successful break length
constructor(
private _text: string,
@@ -87,28 +88,141 @@ export class DocSimpleSkeleton {
// Check if adding this text would exceed the width
if ((textSize.width + currentLine.width) > this._width) {
- if (currentLine.text.length > 0) {
- // Push the current line first
- this._lines.push({ ...currentLine });
- totalHeight += currentLine.height;
- if (totalHeight > this._height) {
- break;
+ if (textSize.width > this._width) {
+ // Push current line if it has content
+ if (currentLine.text.length > 0) {
+ this._lines.push(currentLine);
+ totalHeight += currentLine.height;
+ if (totalHeight > this._height) {
+ break;
+ }
+ // Reset current line
+ currentLine = {
+ text: '',
+ width: 0,
+ height: 0,
+ baseline: 0,
+ };
+ }
+
+ // Character-based line breaking processing
+ let remainingText = text;
+ while (remainingText.length > 0) {
+ let charCount = 0;
+ let lineText = '';
+ let lineWidth = 0;
+
+ // Heuristic search starting from cached break length
+ let startGuess = Math.min(this._lastBreakLength, remainingText.length);
+ if (startGuess === 0) {
+ startGuess = Math.min(10, remainingText.length); // Default guess for first time
+ }
+
+ // First, try the cached length
+ let testText = remainingText.slice(0, startGuess);
+ let testSize = FontCache.getMeasureText(testText, this._fontStyle);
+
+ if (testSize.width + currentLine.width <= this._width) {
+ // The guess fits, search forward for the maximum
+ charCount = startGuess;
+ for (let i = startGuess + 1; i <= remainingText.length; i++) {
+ testText = remainingText.slice(0, i);
+ testSize = FontCache.getMeasureText(testText, this._fontStyle);
+
+ if (testSize.width + currentLine.width <= this._width) {
+ charCount = i;
+ } else {
+ break;
+ }
+ }
+ } else {
+ // The guess is too big, search backward
+ charCount = 0;
+ for (let i = startGuess - 1; i >= 1; i--) {
+ testText = remainingText.slice(0, i);
+ testSize = FontCache.getMeasureText(testText, this._fontStyle);
+
+ if (testSize.width + currentLine.width <= this._width) {
+ charCount = i;
+ break;
+ }
+ }
+ }
+
+ // Update the cache with the successful break length
+ if (charCount > 0) {
+ this._lastBreakLength = charCount;
+ }
+
+ if (charCount > 0) {
+ lineText = remainingText.slice(0, charCount);
+ const textSize = FontCache.getMeasureText(lineText, this._fontStyle);
+ lineWidth = textSize.width;
+ }
+
+ // If no character can fit, force add one character to avoid infinite loop
+ if (charCount === 0 && currentLine.text.length === 0) {
+ charCount = 1;
+ lineText = remainingText[0];
+ const charSize = FontCache.getMeasureText(lineText, this._fontStyle);
+ lineWidth = charSize.width;
+ }
+
+ if (charCount > 0) {
+ // Add to current line
+ currentLine.text += lineText;
+ currentLine.width += lineWidth;
+ const textSize = FontCache.getMeasureText(currentLine.text, this._fontStyle);
+ currentLine.height = textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent;
+ currentLine.baseline = textSize.fontBoundingBoxAscent;
+
+ // Remove processed characters
+ remainingText = remainingText.slice(charCount);
+
+ // If there's remaining text, need to wrap to next line
+ if (remainingText.length > 0) {
+ this._lines.push(currentLine);
+ totalHeight += currentLine.height;
+ if (totalHeight > this._height) {
+ break;
+ }
+ // Reset current line
+ currentLine = {
+ text: '',
+ width: 0,
+ height: 0,
+ baseline: 0,
+ };
+ }
+ } else {
+ // Cannot continue processing, exit loop
+ break;
+ }
}
- // Reset current line and add the new text
- currentLine = {
- text,
- width: textSize.width,
- height: textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent,
- baseline: textSize.fontBoundingBoxAscent,
- };
} else {
- // If current line is empty, we have to add this text anyway (single word too long)
- currentLine = {
- text,
- width: textSize.width,
- height: textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent,
- baseline: textSize.fontBoundingBoxAscent,
- };
+ if (currentLine.text.length > 0) {
+ // Push the current line first
+ this._lines.push(currentLine);
+ totalHeight += currentLine.height;
+ if (totalHeight > this._height) {
+ break;
+ }
+ // Reset current line and add the new text
+ currentLine = {
+ text,
+ width: textSize.width,
+ height: textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent,
+ baseline: textSize.fontBoundingBoxAscent,
+ };
+ } else {
+ // If current line is empty, we have to add this text anyway (single word too long)
+ currentLine = {
+ text,
+ width: textSize.width,
+ height: textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent,
+ baseline: textSize.fontBoundingBoxAscent,
+ };
+ }
}
} else {
// Add text to current line
@@ -123,7 +237,7 @@ export class DocSimpleSkeleton {
// Don't forget to add the last line if it has content
if (currentLine.text.length > 0) {
- this._lines.push({ ...currentLine });
+ this._lines.push(currentLine);
}
return this._lines;
@@ -143,5 +257,6 @@ export class DocSimpleSkeleton {
makeDirty() {
this._dirty = true;
+ this._lastBreakLength = 0; // Reset cache when content changes
}
}