Make QAccessibleTextWidget::attributes respect default document font

QTextFormat::font{Weight,PointSize}() etc. return the font trait explicitly
set on the QTextFormat, while QTextFormat::font() returns font which also
uses the QTextDocument::defaultFont() as a default for any font traits not
explicitly set on the QTextFormat. Accessibility support for text attributes
used the former, which was wrong; this commit fixes it to use the latter.

Also includes tests to verify the fix.

Change-Id: Iab7f2be1b68adaad847d1f29c9e5af2195416035
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
bb10
Boris Dušek 2015-01-01 19:23:16 +01:00
parent 9f3bd7542c
commit e6bbfc4d1a
2 changed files with 83 additions and 39 deletions

View File

@ -743,7 +743,7 @@ QString QAccessibleTextWidget::attributes(int offset, int *startOffset, int *end
QTextBlockFormat blockFormat = cursor.blockFormat();
QMap<QByteArray, QString> attrs;
QString family = charFormat.fontFamily();
QString family = charFormat.font().family();
if (!family.isEmpty()) {
family = family.replace('\\',QStringLiteral("\\\\"));
family = family.replace(':',QStringLiteral("\\:"));
@ -754,12 +754,12 @@ QString QAccessibleTextWidget::attributes(int offset, int *startOffset, int *end
attrs["font-family"] = QString::fromLatin1("\"%1\"").arg(family);
}
int fontSize = int(charFormat.fontPointSize());
int fontSize = int(charFormat.font().pointSize());
if (fontSize)
attrs["font-size"] = QString::fromLatin1("%1pt").arg(fontSize);
//Different weight values are not handled
attrs["font-weight"] = QString::fromLatin1(charFormat.fontWeight() > QFont::Normal ? "bold" : "normal");
attrs["font-weight"] = QString::fromLatin1(charFormat.font().weight() > QFont::Normal ? "bold" : "normal");
QFont::Style style = charFormat.font().style();
attrs["font-style"] = QString::fromLatin1((style == QFont::StyleItalic) ? "italic" : ((style == QFont::StyleOblique) ? "oblique": "normal"));

View File

@ -650,13 +650,25 @@ void tst_QAccessibility::accessibleName()
// note: color should probably always be part of the attributes
void tst_QAccessibility::textAttributes_data()
{
QTest::addColumn<QFont>("defaultFont");
QTest::addColumn<QString>("text");
QTest::addColumn<int>("offset");
QTest::addColumn<int>("startOffsetResult");
QTest::addColumn<int>("endOffsetResult");
QTest::addColumn<QStringList>("attributeResult");
static QStringList defaults = QString("font-style:normal;font-weight:normal;text-align:left;text-position:baseline;text-underline-style:none").split(';');
static QFont defaultFont;
defaultFont.setFamily("");
defaultFont.setPointSize(13);
static QFont defaultComplexFont = defaultFont;
defaultComplexFont.setFamily("Arial");
defaultComplexFont.setPointSize(20);
defaultComplexFont.setWeight(QFont::Bold);
defaultComplexFont.setStyle(QFont::StyleItalic);
defaultComplexFont.setUnderline(true);
static QStringList defaults = QString("font-style:normal;font-weight:normal;text-align:left;text-position:baseline;text-underline-style:none;font-size:13pt").split(';');
static QStringList bold = defaults;
bold[1] = QString::fromLatin1("font-weight:bold");
@ -671,7 +683,7 @@ void tst_QAccessibility::textAttributes_data()
monospace.append(QLatin1String("font-family:\"monospace\""));
static QStringList font8pt = defaults;
font8pt.append(QLatin1String("font-size:8pt"));
font8pt[5] = (QLatin1String("font-size:8pt"));
static QStringList color = defaults;
color << QLatin1String("color:rgb(240,241,242)") << QLatin1String("background-color:rgb(20,240,30)");
@ -679,22 +691,42 @@ void tst_QAccessibility::textAttributes_data()
static QStringList rightAlign = defaults;
rightAlign[2] = QStringLiteral("text-align:right");
QTest::newRow("defaults 1") << "hello" << 0 << 0 << 5 << defaults;
QTest::newRow("defaults 2") << "hello" << 1 << 0 << 5 << defaults;
QTest::newRow("defaults 3") << "hello" << 4 << 0 << 5 << defaults;
QTest::newRow("defaults 4") << "hello" << 5 << 0 << 5 << defaults;
QTest::newRow("offset -1 length") << "hello" << -1 << 0 << 5 << defaults;
QTest::newRow("offset -2 cursor pos") << "hello" << -2 << 0 << 5 << defaults;
QTest::newRow("offset -3") << "hello" << -3 << -1 << -1 << QStringList();
QTest::newRow("invalid offset 2") << "hello" << 6 << -1 << -1 << QStringList();
QTest::newRow("invalid offset 3") << "" << 1 << -1 << -1 << QStringList();
static QStringList defaultFontDifferent = defaults;
defaultFontDifferent[0] = QString::fromLatin1("font-style:italic");
defaultFontDifferent[1] = QString::fromLatin1("font-weight:bold");
defaultFontDifferent[4] = QString::fromLatin1("text-underline-style:solid");
defaultFontDifferent[5] = QString::fromLatin1("font-size:20pt");
defaultFontDifferent.append("font-family:\"Arial\"");
static QStringList defaultFontDifferentBoldItalic = defaultFontDifferent;
defaultFontDifferentBoldItalic[0] = QString::fromLatin1("font-style:italic");
defaultFontDifferentBoldItalic[1] = QString::fromLatin1("font-weight:bold");
static QStringList defaultFontDifferentMonospace = defaultFontDifferent;
defaultFontDifferentMonospace[6] = (QLatin1String("font-family:\"monospace\""));
static QStringList defaultFontDifferentFont8pt = defaultFontDifferent;
defaultFontDifferentFont8pt[5] = (QLatin1String("font-size:8pt"));
static QStringList defaultFontDifferentColor = defaultFontDifferent;
defaultFontDifferentColor << QLatin1String("color:rgb(240,241,242)") << QLatin1String("background-color:rgb(20,240,30)");
QTest::newRow("defaults 1") << defaultFont << "hello" << 0 << 0 << 5 << defaults;
QTest::newRow("defaults 2") << defaultFont << "hello" << 1 << 0 << 5 << defaults;
QTest::newRow("defaults 3") << defaultFont << "hello" << 4 << 0 << 5 << defaults;
QTest::newRow("defaults 4") << defaultFont << "hello" << 5 << 0 << 5 << defaults;
QTest::newRow("offset -1 length") << defaultFont << "hello" << -1 << 0 << 5 << defaults;
QTest::newRow("offset -2 cursor pos") << defaultFont << "hello" << -2 << 0 << 5 << defaults;
QTest::newRow("offset -3") << defaultFont << "hello" << -3 << -1 << -1 << QStringList();
QTest::newRow("invalid offset 2") << defaultFont << "hello" << 6 << -1 << -1 << QStringList();
QTest::newRow("invalid offset 3") << defaultFont << "" << 1 << -1 << -1 << QStringList();
QString boldText = QLatin1String("<html><b>bold</b>text");
QTest::newRow("bold 0") << boldText << 0 << 0 << 4 << bold;
QTest::newRow("bold 2") << boldText << 2 << 0 << 4 << bold;
QTest::newRow("bold 3") << boldText << 3 << 0 << 4 << bold;
QTest::newRow("bold 4") << boldText << 4 << 4 << 8 << defaults;
QTest::newRow("bold 6") << boldText << 6 << 4 << 8 << defaults;
QTest::newRow("bold 0") << defaultFont << boldText << 0 << 0 << 4 << bold;
QTest::newRow("bold 2") << defaultFont << boldText << 2 << 0 << 4 << bold;
QTest::newRow("bold 3") << defaultFont << boldText << 3 << 0 << 4 << bold;
QTest::newRow("bold 4") << defaultFont << boldText << 4 << 4 << 8 << defaults;
QTest::newRow("bold 6") << defaultFont << boldText << 6 << 4 << 8 << defaults;
QString longText = QLatin1String("<html>"
"Hello, <b>this</b> is an <i><b>example</b> text</i>."
@ -702,36 +734,47 @@ void tst_QAccessibility::textAttributes_data()
"Multiple <span style=\"font-size: 8pt\">text sizes</span> are used."
"Let's give some color to <span style=\"color:#f0f1f2; background-color:#14f01e\">Qt</span>.");
QTest::newRow("default 5") << longText << 6 << 0 << 7 << defaults;
QTest::newRow("default 6") << longText << 7 << 7 << 11 << bold;
QTest::newRow("bold 7") << longText << 10 << 7 << 11 << bold;
QTest::newRow("bold 8") << longText << 10 << 7 << 11 << bold;
QTest::newRow("bold italic") << longText << 18 << 18 << 25 << boldItalic;
QTest::newRow("monospace") << longText << 34 << 31 << 55 << monospace;
QTest::newRow("8pt") << longText << 65 << 64 << 74 << font8pt;
QTest::newRow("color") << longText << 110 << 109 << 111 << color;
QTest::newRow("default 5") << defaultFont << longText << 6 << 0 << 7 << defaults;
QTest::newRow("default 6") << defaultFont << longText << 7 << 7 << 11 << bold;
QTest::newRow("bold 7") << defaultFont << longText << 10 << 7 << 11 << bold;
QTest::newRow("bold 8") << defaultFont << longText << 10 << 7 << 11 << bold;
QTest::newRow("bold italic") << defaultFont << longText << 18 << 18 << 25 << boldItalic;
QTest::newRow("monospace") << defaultFont << longText << 34 << 31 << 55 << monospace;
QTest::newRow("8pt") << defaultFont << longText << 65 << 64 << 74 << font8pt;
QTest::newRow("color") << defaultFont << longText << 110 << 109 << 111 << color;
// make sure unset font properties default to those of document's default font
QTest::newRow("defaultFont default 5") << defaultComplexFont << longText << 6 << 0 << 7 << defaultFontDifferent;
QTest::newRow("defaultFont default 6") << defaultComplexFont << longText << 7 << 7 << 11 << defaultFontDifferent;
QTest::newRow("defaultFont bold 7") << defaultComplexFont << longText << 10 << 7 << 11 << defaultFontDifferent;
QTest::newRow("defaultFont bold 8") << defaultComplexFont << longText << 10 << 7 << 11 << defaultFontDifferent;
QTest::newRow("defaultFont bold italic") << defaultComplexFont << longText << 18 << 18 << 25 << defaultFontDifferentBoldItalic;
QTest::newRow("defaultFont monospace") << defaultComplexFont << longText << 34 << 31 << 55 << defaultFontDifferentMonospace;
QTest::newRow("defaultFont 8pt") << defaultComplexFont << longText << 65 << 64 << 74 << defaultFontDifferentFont8pt;
QTest::newRow("defaultFont color") << defaultComplexFont << longText << 110 << 109 << 111 << defaultFontDifferentColor;
QString rightAligned = QLatin1String("<html><p align=\"right\">right</p>");
QTest::newRow("right aligned 1") << rightAligned << 0 << 0 << 5 << rightAlign;
QTest::newRow("right aligned 2") << rightAligned << 1 << 0 << 5 << rightAlign;
QTest::newRow("right aligned 3") << rightAligned << 5 << 0 << 5 << rightAlign;
QTest::newRow("right aligned 1") << defaultFont << rightAligned << 0 << 0 << 5 << rightAlign;
QTest::newRow("right aligned 2") << defaultFont << rightAligned << 1 << 0 << 5 << rightAlign;
QTest::newRow("right aligned 3") << defaultFont << rightAligned << 5 << 0 << 5 << rightAlign;
// left \n right \n left, make sure bold and alignment borders coincide
QString leftRightLeftAligned = QLatin1String("<html><p><b>left</b></p><p align=\"right\">right</p><p><b>left</b></p>");
QTest::newRow("left right left aligned 1") << leftRightLeftAligned << 1 << 0 << 4 << bold;
QTest::newRow("left right left aligned 3") << leftRightLeftAligned << 3 << 0 << 4 << bold;
QTest::newRow("left right left aligned 4") << leftRightLeftAligned << 4 << 4 << 5 << defaults;
QTest::newRow("left right left aligned 5") << leftRightLeftAligned << 5 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 8") << leftRightLeftAligned << 8 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 9") << leftRightLeftAligned << 9 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 10") << leftRightLeftAligned << 10 << 10 << 11 << rightAlign;
QTest::newRow("left right left aligned 11") << leftRightLeftAligned << 11 << 11 << 15 << bold;
QTest::newRow("left right left aligned 15") << leftRightLeftAligned << 15 << 11 << 15 << bold;
QTest::newRow("left right left aligned 1") << defaultFont << leftRightLeftAligned << 1 << 0 << 4 << bold;
QTest::newRow("left right left aligned 3") << defaultFont << leftRightLeftAligned << 3 << 0 << 4 << bold;
QTest::newRow("left right left aligned 4") << defaultFont << leftRightLeftAligned << 4 << 4 << 5 << defaults;
QTest::newRow("left right left aligned 5") << defaultFont << leftRightLeftAligned << 5 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 8") << defaultFont << leftRightLeftAligned << 8 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 9") << defaultFont << leftRightLeftAligned << 9 << 5 << 10 << rightAlign;
QTest::newRow("left right left aligned 10") << defaultFont << leftRightLeftAligned << 10 << 10 << 11 << rightAlign;
QTest::newRow("left right left aligned 11") << defaultFont << leftRightLeftAligned << 11 << 11 << 15 << bold;
QTest::newRow("left right left aligned 15") << defaultFont << leftRightLeftAligned << 15 << 11 << 15 << bold;
}
void tst_QAccessibility::textAttributes()
{
{
QFETCH(QFont, defaultFont);
QFETCH(QString, text);
QFETCH(int, offset);
QFETCH(int, startOffsetResult);
@ -739,6 +782,7 @@ void tst_QAccessibility::textAttributes()
QFETCH(QStringList, attributeResult);
QTextEdit textEdit;
textEdit.document()->setDefaultFont(defaultFont);
textEdit.setText(text);
if (textEdit.document()->characterCount() > 1)
textEdit.textCursor().setPosition(1);