Skip to content

Commit

Permalink
docs: add some comments to TextWrap
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber authored and danyx23 committed Nov 17, 2023
1 parent 332525f commit 91b6115
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ interface WrapLine {
height: number
}

const HTML_OPENING_TAG_REGEX = /<(\w+)[^>]*>/
const HTML_CLOSING_TAG_REGEX = /<\/(\w)+>/

function startsWithNewline(text: string): boolean {
return /^\n/.test(text)
}
Expand Down Expand Up @@ -106,12 +109,14 @@ export class TextWrap {
startsWithNewline(word) ||
(nextBounds.width + 10 > maxWidth && line.length >= 1)
) {
const wordWithoutNewline = word.replace(/^\n/, "")
// Introduce a newline _before_ this word
lines.push({
text: line.join(" "),
width: lineBounds.width,
height: lineBounds.height,
})
// ... and start a new line with this word (with a potential leading newline stripped)
const wordWithoutNewline = word.replace(/^\n/, "")
line = [wordWithoutNewline]
lineBounds = Bounds.forText(wordWithoutNewline, {
fontSize,
Expand All @@ -122,6 +127,8 @@ export class TextWrap {
lineBounds = nextBounds
}
})

// Push the last line
if (line.length > 0)
lines.push({
text: line.join(" "),
Expand Down

0 comments on commit 91b6115

Please sign in to comment.