From bd5c3582581f37513f45b518e348f443d5d57334 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Tue, 2 May 2023 20:50:52 +0200 Subject: a11y: Fix returning unpaired surrogates when retrieving characters Fix implementations of XAccessibleText's getTextAtIndex(), getTextBeforeIndex() and getTextBehindIndex() when called with AccessibleTextType::CHARACTER to return the whole code point rather than an unpaired surrogate. This is still not perfect because XAccessibleText::getCharacterCount() will return an incorrect value (code units rather than code points), but it fixes the most useful case of retrieving the character at e.g. the caret offset. This fixes the GTK3 and Windows backends as well without further changes. Qt6 also mostly works according to Michael Weghorn, but for a bug on Qt's side (https://bugreports.qt.io/browse/QTBUG-113438). MacOS backend doesn't seem to be affected in the first place. Change-Id: I53f07bcba78c6b267939257542a521b106101e96 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151303 Tested-by: Jenkins Reviewed-by: Michael Weghorn --- comphelper/source/misc/accessibletexthelper.cxx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'comphelper') diff --git a/comphelper/source/misc/accessibletexthelper.cxx b/comphelper/source/misc/accessibletexthelper.cxx index da39ac1ce252..06752ba88ded 100644 --- a/comphelper/source/misc/accessibletexthelper.cxx +++ b/comphelper/source/misc/accessibletexthelper.cxx @@ -297,9 +297,12 @@ namespace comphelper { if ( implIsValidIndex( nIndex, nLength ) ) { - aResult.SegmentText = sText.copy( nIndex, 1 ); + auto nIndexEnd = nIndex; + sText.iterateCodePoints(&nIndexEnd); + + aResult.SegmentText = sText.copy( nIndex, nIndexEnd - nIndex ); aResult.SegmentStart = nIndex; - aResult.SegmentEnd = nIndex+1; + aResult.SegmentEnd = nIndexEnd; } } break; @@ -401,9 +404,12 @@ namespace comphelper { if ( implIsValidIndex( nIndex - 1, nLength ) ) { - aResult.SegmentText = sText.copy( nIndex - 1, 1 ); - aResult.SegmentStart = nIndex-1; - aResult.SegmentEnd = nIndex; + sText.iterateCodePoints(&nIndex, -1); + auto nIndexEnd = nIndex; + sText.iterateCodePoints(&nIndexEnd); + aResult.SegmentText = sText.copy(nIndex, nIndexEnd - nIndex); + aResult.SegmentStart = nIndex; + aResult.SegmentEnd = nIndexEnd; } } break; @@ -525,9 +531,12 @@ namespace comphelper { if ( implIsValidIndex( nIndex + 1, nLength ) ) { - aResult.SegmentText = sText.copy( nIndex + 1, 1 ); - aResult.SegmentStart = nIndex+1; - aResult.SegmentEnd = nIndex+2; + sText.iterateCodePoints(&nIndex); + auto nIndexEnd = nIndex; + sText.iterateCodePoints(&nIndexEnd); + aResult.SegmentText = sText.copy(nIndex, nIndexEnd - nIndex); + aResult.SegmentStart = nIndex; + aResult.SegmentEnd = nIndexEnd; } } break; -- cgit