Skip to content

Commit 43c533f

Browse files
authored
fix: terminate immediately when the line width is too small to display any characters (#1956)
1 parent f90ebe5 commit 43c533f

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

.changeset/green-jobs-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@antv/g-lite': patch
3+
---
4+
5+
fix: terminate immediately when the line width is too small to display any characters

__tests__/unit/display-objects/text.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,22 @@ describe('Text', () => {
251251
text.style.wordWrap = false;
252252
expect(text.isOverflowing()).toBe(false);
253253
});
254+
255+
it('should terminate when line width is too small to display any character', async () => {
256+
await canvas.ready;
257+
258+
const text = new Text({
259+
style: {
260+
text: '测试文本',
261+
fontSize: 16,
262+
wordWrap: true,
263+
wordWrapWidth: 0,
264+
maxLines: 3,
265+
},
266+
});
267+
const lineRects = text.getLineBoundingRects();
268+
269+
expect(lineRects.length).toBe(1);
270+
expect(text.isOverflowing()).toBe(true);
271+
});
254272
});

packages/g-lite/src/services/TextService.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,17 @@ export class TextService {
464464
continue;
465465
}
466466

467+
// If current line is empty and char width exceeds max width, no need to continue
468+
if (currentLineWidth === 0 && charWidth > maxWidth) {
469+
// Only add ellipsis if it can fit in the line
470+
if (ellipsis && ellipsisWidth <= maxWidth) {
471+
lines[currentLineIndex] = ellipsis;
472+
} else {
473+
lines[currentLineIndex] = '';
474+
}
475+
parsedStyle.isOverflowing = true;
476+
break;
477+
}
467478
if (currentLineWidth > 0 && currentLineWidth + charWidth > maxWidth) {
468479
const result = findCharIndexClosestWidthThreshold(
469480
lines[currentLineIndex],

0 commit comments

Comments
 (0)