From e16dcb7e9d8b85f56d724692a0281811549079e4 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 27 Mar 2021 11:20:33 +0200 Subject: GmDocument: Finding position in source The returned position at a source location is now a range, pointing to the left and right edges of the contacted character. --- src/gmdocument.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/gmdocument.c') diff --git a/src/gmdocument.c b/src/gmdocument.c index 3cf00a4b..30f5169a 100644 --- a/src/gmdocument.c +++ b/src/gmdocument.c @@ -1528,12 +1528,12 @@ const iGmRun *findRun_GmDocument(const iGmDocument *d, iInt2 pos) { return last; } -const char *findLoc_GmDocument(const iGmDocument *d, iInt2 pos) { +iRangecc findLoc_GmDocument(const iGmDocument *d, iInt2 pos) { const iGmRun *run = findRun_GmDocument(d, pos); if (run) { return findLoc_GmRun(run, pos); } - return NULL; + return iNullRange; } const iGmRun *findRunAtLoc_GmDocument(const iGmDocument *d, const char *textCStr) { @@ -1662,16 +1662,25 @@ iChar siteIcon_GmDocument(const iGmDocument *d) { return d->siteIcon; } -const char *findLoc_GmRun(const iGmRun *d, iInt2 pos) { +iRangecc findLoc_GmRun(const iGmRun *d, iInt2 pos) { if (pos.y < top_Rect(d->bounds)) { - return d->text.start; + return (iRangecc){ d->text.start, d->text.start }; } const int x = pos.x - left_Rect(d->bounds); if (x <= 0) { - return d->text.start; + return (iRangecc){ d->text.start, d->text.start }; + } + iRangecc loc; + tryAdvanceNoWrap_Text(d->font, d->text, x, &loc.start); + loc.end = loc.start; + iChar ch; + if (d->text.end != loc.start) { + int chLen = decodeBytes_MultibyteChar(loc.start, d->text.end - loc.start, &ch); + if (chLen > 0) { + /* End after the character. */ + loc.end += chLen; + } } - const char *loc; - tryAdvanceNoWrap_Text(d->font, d->text, x, &loc); return loc; } -- cgit v1.2.3