Skip to content

Commit e8db351

Browse files
committed
fixed text overflow with some non-wrappable texts #2846
1 parent 7620919 commit e8db351

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Added `section` node
66
- Fixed crash that occurred when using automatic page height
7+
- Fixed text overflow with some non-wrappable texts
78

89
## 0.3.0-beta.17 - 2025-04-29
910

src/LayoutBuilder.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,27 @@ class LayoutBuilder {
11201120
return newInline;
11211121
}
11221122

1123+
function findMaxFitLength(text, maxWidth, measureFn) {
1124+
let low = 1;
1125+
let high = text.length;
1126+
let bestFit = 1;
1127+
1128+
while (low <= high) {
1129+
const mid = Math.floor((low + high) / 2);
1130+
const part = text.substring(0, mid);
1131+
const width = measureFn(part);
1132+
1133+
if (width <= maxWidth) {
1134+
bestFit = mid;
1135+
low = mid + 1;
1136+
} else {
1137+
high = mid - 1;
1138+
}
1139+
}
1140+
1141+
return bestFit;
1142+
}
1143+
11231144
if (!textNode._inlines || textNode._inlines.length === 0) {
11241145
return null;
11251146
}
@@ -1135,11 +1156,9 @@ class LayoutBuilder {
11351156
isForceContinue = false;
11361157

11371158
if (!inline.noWrap && inline.text.length > 1 && inline.width > line.getAvailableWidth()) {
1138-
let widthPerChar = inline.width / inline.text.length;
1139-
let maxChars = Math.floor(line.getAvailableWidth() / widthPerChar);
1140-
if (maxChars < 1) {
1141-
maxChars = 1;
1142-
}
1159+
let maxChars = findMaxFitLength(inline.text, line.getAvailableWidth(), (txt) =>
1160+
textInlines.widthOfText(txt, inline)
1161+
);
11431162
if (maxChars < inline.text.length) {
11441163
let newInline = cloneInline(inline);
11451164

0 commit comments

Comments
 (0)