From 93bb16384a8028b7a29935f155e92547c911d772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Du=C5=A1ek?= Date: Tue, 17 Mar 2015 23:03:23 +0100 Subject: Fix QAccessibleTextWidget::characterRect for off-cursor positions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current code retrieved the character format at position where the cursor currently was, irrespective of the position actually passed to the characterRect function. This is now fixed and we retrieve the character format for the QTextFragment containing the specified offset. This fixes display of visual bounds around text by screen reader in some cases. E.g. on OS X, when searching text using VoiceOver, VoiceOver first queries the visual bounds of the found text (and thus also characterRect), and only then it moves cursor position to the found text. If before the change of position the cursor was on some text with different metrics (i.e. a bigger font), the visual bounds for the found text reflected those metrics, not the metrics of the actual found text for which the bounds were drawn. This resulted in smaller/bigger rectangles around the found text than was actually correct. Change-Id: Ie2a4dfc714504b7923cdaf8ff875c438aeccddee Reviewed-by: Jan Arve Sæther --- src/widgets/accessible/qaccessiblewidgets.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/widgets') diff --git a/src/widgets/accessible/qaccessiblewidgets.cpp b/src/widgets/accessible/qaccessiblewidgets.cpp index 77958d65a5..a2bf14b25b 100644 --- a/src/widgets/accessible/qaccessiblewidgets.cpp +++ b/src/widgets/accessible/qaccessiblewidgets.cpp @@ -667,7 +667,20 @@ QRect QAccessibleTextWidget::characterRect(int offset) const if (line.isValid()) { qreal x = line.cursorToX(relativeOffset); - QFontMetrics fm(textCursor().charFormat().font()); + + QTextCharFormat format; + QTextBlock::iterator iter = block.begin(); + if (iter.atEnd()) + format = block.charFormat(); + else { + while (!iter.atEnd() && !iter.fragment().contains(offset)) + ++iter; + if (iter.atEnd()) // newline should have same format as preceding character + --iter; + format = iter.fragment().charFormat(); + } + + QFontMetrics fm(format.font()); const QString ch = text(offset, offset + 1); if (!ch.isEmpty()) { int w = fm.width(ch); -- cgit v1.2.3