Skip to content

Commit

Permalink
fix(troika-three-text): selection rects no longer clip off trailing w…
Browse files Browse the repository at this point in the history
…hitespace

Fixes issue described in #78. We were clipping to the blockBounds, which
broke caret positioning for trailing whitespace. Removed that clipping
and added a different fix for overly wide trailing whitespace characters
when justifying.
  • Loading branch information
lojjic committed Feb 17, 2021
1 parent b4e0ea1 commit 158305c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/troika-three-text/src/selectionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function getSelectionRects(textRenderInfo, start, end) {
return prevResult.rects
}

const {caretPositions, caretHeight, blockBounds} = textRenderInfo
const {caretPositions, caretHeight} = textRenderInfo

// Normalize
if (end < start) {
Expand All @@ -80,8 +80,8 @@ export function getSelectionRects(textRenderInfo, start, end) {
row = {left: x1, right: x2, bottom: y, top: y + caretHeight}
rows.set(y, row)
} else {
row.left = Math.max(Math.min(row.left, x1), blockBounds[0])
row.right = Math.min(Math.max(row.right, x2), blockBounds[2])
row.left = Math.min(row.left, x1)
row.right = Math.max(row.right, x2)
}
}
rects = []
Expand Down
19 changes: 15 additions & 4 deletions packages/troika-three-text/src/worker/FontProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,27 @@ export function createFontProcessor(fontParser, sdfGenerator, config) {
}

for (let i = 0; i < lineGlyphCount; i++) {
const glyphInfo = line.glyphAt(i)
let glyphInfo = line.glyphAt(i)
const glyphObj = glyphInfo.glyphObj

// Apply position adjustments
if (lineXOffset) glyphInfo.x += lineXOffset

// Expand whitespaces for justify alignment
// Expand non-trailing whitespaces for justify alignment
if (justifyAdjust !== 0 && glyphObj.isWhitespace) {
lineXOffset += justifyAdjust
glyphInfo.width += justifyAdjust
let isTrailingWhitespace = true
for (let j = i + 1; j < lineGlyphCount; j++) {
glyphInfo = line.glyphAt(j)
if (!glyphInfo.glyphObj.isWhitespace) {
isTrailingWhitespace = false
break
}
}
glyphInfo = line.glyphAt(i) //restore flyweight
if (!isTrailingWhitespace) {
lineXOffset += justifyAdjust
glyphInfo.width += justifyAdjust
}
}

// Add caret positions
Expand Down

0 comments on commit 158305c

Please sign in to comment.