From 9ab619bd3c57d681a3906abf0dc10d5376d01319 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 21 Jul 2021 08:55:12 +0300 Subject: Text: Finding the advance position of a character --- src/ui/text.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/ui/text.c') diff --git a/src/ui/text.c b/src/ui/text.c index d6a45c69..ce383ccf 100644 --- a/src/ui/text.c +++ b/src/ui/text.c @@ -1380,8 +1380,9 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { /* TODO: Duplicated args? */ iAssert(equalRange_Rangecc(wrap->text, args->text)); /* Initialize the wrap range. */ - wrap->wrapRange_ = args->text; - wrap->hitChar_out = NULL; + wrap->wrapRange_ = args->text; + wrap->hitAdvance_out = zero_I2(); + wrap->hitChar_out = NULL; wrap->hitGlyphNormX_out = 0.0f; } const iChar *logicalText = constData_Array(&attrText.logical); @@ -1427,6 +1428,7 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { const int layoutBound = (wrap ? wrap->maxWidth : 0); iBool isFirst = iTrue; const iBool checkHitPoint = wrap && !isEqual_I2(wrap->hitPoint, zero_I2()); + const iBool checkHitChar = wrap && wrap->hitChar; while (!isEmpty_Range(&wrapRuns)) { if (isFirst) { isFirst = iFalse; @@ -1446,6 +1448,11 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { for (size_t runIndex = wrapRuns.start; runIndex < wrapRuns.end; runIndex++) { const iAttributedRun *run = at_Array(&attrText.runs, runIndex); if (run->flags.isLineBreak) { + if (checkHitChar) { + if (wrap->hitChar == sourcePtr_AttributedText_(&attrText, run->logical.start)) { + wrap->hitAdvance_out = init_I2(wrapAdvance, yCursor); + } + } wrapPosRange.end = run->logical.start; wrapResumePos = run->logical.end; wrapRuns.end = runIndex; @@ -1468,6 +1475,11 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { if (logPos < wrapPosRange.start || logPos >= wrapPosRange.end) { continue; } + if (checkHitChar) { + if (wrap->hitChar == sourcePtr_AttributedText_(&attrText, logPos)) { + wrap->hitAdvance_out = init_I2(wrapAdvance, yCursor); + } + } /* Check if the hit point is on the left side of this line. */ if (isHitPointOnThisLine && !wrap->hitChar_out && wrap->hitPoint.x < orig.x) { wrap->hitChar_out = sourcePtr_AttributedText_(&attrText, logPos); @@ -1723,6 +1735,9 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { wrapPosRange.start = wrapResumePos; wrapPosRange.end = textLen; } + if (checkHitChar && wrap->hitChar == args->text.end) { + wrap->hitAdvance_out = init_I2(xCursor, yCursor); + } if (endsWith_Rangecc(args->text, "\n")) { /* FIXME: This is a kludge, the wrap loop should handle this case, too. */ /* The last wrap is an empty newline wrap. */ -- cgit v1.2.3