-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement wrapWord parameter (#268) #345
base: dev
Are you sure you want to change the base?
Changes from 6 commits
da89c2e
98b45c9
8a939ad
4f47680
c7f8c9c
856cc7c
3c98b21
dab52d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,6 +323,7 @@ export class LightningTextTextureRenderer { | |
wordWrapWidth, | ||
letterSpacing, | ||
textIndent, | ||
this._settings.wordBreak, | ||
); | ||
} else { | ||
linesInfo = { l: this._settings.text.split(/(?:\r\n|\r|\n)/), n: [] }; | ||
|
@@ -347,6 +348,7 @@ export class LightningTextTextureRenderer { | |
wordWrapWidth - w, | ||
letterSpacing, | ||
textIndent, | ||
this._settings.wordBreak, | ||
); | ||
usedLines[usedLines.length - 1] = `${al.l[0]!}${ | ||
this._settings.overflowSuffix | ||
|
@@ -681,6 +683,7 @@ export class LightningTextTextureRenderer { | |
wordWrapWidth: number, | ||
letterSpacing: number, | ||
indent = 0, | ||
wordBreak: boolean, | ||
) { | ||
// Greedy wrapping algorithm that will wrap words as the line grows longer. | ||
// than its horizontal bounds. | ||
|
@@ -696,6 +699,16 @@ export class LightningTextTextureRenderer { | |
const wordWidth = this.measureText(words[j]!, letterSpacing); | ||
const wordWidthWithSpace = | ||
wordWidth + this.measureText(' ', letterSpacing); | ||
if (wordWidthWithSpace > wordWrapWidth && wordBreak) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the algorithm here is different from SDF. Isn't the goal agreed to cut the word so the beginning of the word fits at the end of the line? e.g. keep what fits within |
||
let remainder = ''; | ||
while (this.measureText(words[j]!) > wordWrapWidth) { | ||
remainder = words[j]!.slice(-1) + remainder; | ||
words[j] = words[j]!.slice(0, -1); | ||
} | ||
if (remainder.length > 0) { | ||
words.splice(j + 1, 0, remainder); | ||
} | ||
} | ||
if (j === 0 || wordWidthWithSpace > spaceLeft) { | ||
// Skip printing the newline if it's the first word of the line that is. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is where you cut the word to fix within the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but what remains is cut in next iteration of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok it can work here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW I know that this project tends to use mostly visual regression tests, but this is a case where some unit tests would be greatly beneficial @wouterlucas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
theres a vitest infrastructure as well, just add |
||
// greater than the word wrap width. | ||
|
@@ -720,7 +733,6 @@ export class LightningTextTextureRenderer { | |
realNewlines.push(allLines.length); | ||
} | ||
} | ||
|
||
return { l: allLines, n: realNewlines }; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ export function layoutText( | |
scrollable: TrProps['scrollable'], | ||
overflowSuffix: TrProps['overflowSuffix'], | ||
maxLines: TrProps['maxLines'], | ||
wrapWord: TrProps['wrapWord'], | ||
): { | ||
bufferNumFloats: number; | ||
bufferNumQuads: number; | ||
|
@@ -292,6 +293,24 @@ export function layoutText( | |
maxX = Math.max(maxX, quadX + glyph.width); | ||
curX += glyph.xAdvance; | ||
} | ||
if ( | ||
marcel-danilewicz-consult-red marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wrapWord == 'break' && | ||
charEndX + glyph.width >= lineVertexW && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that correct? Wrap-word I think is meant to only wrap a word when this word alone is larger than the wrapping width. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it should be wrapped if the width is exceeded. So if a word doesn't fit it should be moved to the next line and then if still doesn't fit it should be wrapped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be silly to break every word. The point is that sometimes (as reported in #268) you may have a single word larger than the target width, in which case you may prefer to break it. In L2, that's what I suggest again to make sure that the requirements are non-ambiguous - ask @wouterlucas for confirmation. Make a diagram/drawing to explain what you are going to do. In any case it is very wise to have proposed an early draft. |
||
contain != 'none' | ||
) { | ||
if (curLineBufferStart !== -1 && lineIsWithinWindow) { | ||
bufferLineInfos.push({ | ||
bufferStart: curLineBufferStart, | ||
bufferEnd: bufferOffset, | ||
}); | ||
curLineBufferStart = -1; | ||
} | ||
curX = 0; | ||
curY += vertexLineHeight; | ||
curLineIndex++; | ||
lastWord.codepointIndex = -1; | ||
xStartLastWordBoundary = 0; | ||
} | ||
} else { | ||
// Unmapped character | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put conditions in narrowing order: