Debian Codegraph @

tests/auto/gui/text/qfontmetrics/tst_qfontmetrics.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4
5 #include <QTest>
6 #include <qfont.h>
7 #include <qfontmetrics.h>
8 #include <qfontdatabase.h>
9 #include <private/qfontengine_p.h>
10 #include <qstringlist.h>
11 #include <qlist.h>
12
13 class tst_QFontMetrics : public QObject
14 {
15 Q_OBJECT
16
17 private slots:
18 void same();
19 void metrics();
20 void boundingRect();
21 void boundingRect2();
22 void elidedText_data();
23 void elidedText();
24 void veryNarrowElidedText();
25 void averageCharWidth();
26 void bypassShaping();
27 void elidedMultiLength();
28 void elidedMultiLengthF();
29 void inFontUcs4();
30 void lineWidth();
31 void mnemonicTextWidth();
32 void leadingBelowLine();
33 void elidedMetrics();
34 void zeroWidthMetrics();
35 void verticalMetrics_data();
36 void verticalMetrics();
37 void largeText_data();
38 void largeText(); // QTBUG-123339
39 void typoLineMetrics();
40 void defaultIgnorableHorizontalAdvance_data();
41 void defaultIgnorableHorizontalAdvance();
42 void hugeFontMetrics();
43 };
44
45 void tst_QFontMetrics::same()
46 {
47 QFont font;
48 font.setBold(true);
49 QFontMetrics fm(font);
50 const QString text = QLatin1String("Some stupid STRING");
51 QCOMPARE(fm.size(0, text), fm.size(0, text)) ;
52
53 for (int i = 10; i <= 32; ++i) {
54 font.setPixelSize(i);
55 QFontMetrics fm1(font);
56 QCOMPARE(fm1.size(0, text), fm1.size(0, text));
57 }
58
59 {
60 QImage image;
61 QFontMetrics fm2(font, &image);
62 QString text2 = QLatin1String("Foo Foo");
63 QCOMPARE(fm2.size(0, text2), fm2.size(0, text2)); //used to crash
64 }
65
66 {
67 QImage image;
68 QFontMetricsF fm3(font, &image);
69 QString text2 = QLatin1String("Foo Foo");
70 QCOMPARE(fm3.size(0, text2), fm3.size(0, text2)); //used to crash
71 }
72 }
73
74
75 void tst_QFontMetrics::metrics()
76 {
77 QFont font;
78
79 // Query the QFontDatabase for a specific font, store the
80 // result in family, style and size.
81 QStringList families = QFontDatabase::families();
82 if (families.isEmpty())
83 return;
84
85 QStringList::ConstIterator f_it, f_end = families.end();
86 for (f_it = families.begin(); f_it != f_end; ++f_it) {
87 const QString &family = *f_it;
88
89 QStringList styles = QFontDatabase::styles(family);
90 QStringList::ConstIterator s_it, s_end = styles.end();
91 for (s_it = styles.begin(); s_it != s_end; ++s_it) {
92 const QString &style = *s_it;
93
94 if (QFontDatabase::isSmoothlyScalable(family, style)) {
95 // smoothly scalable font... don't need to load every pointsize
96 font = QFontDatabase::font(family, style, 12);
97
98 QFontMetrics fontmetrics(font);
99 QCOMPARE(fontmetrics.ascent() + fontmetrics.descent(),
100 fontmetrics.height());
101
102 QCOMPARE(fontmetrics.height() + fontmetrics.leading(),
103 fontmetrics.lineSpacing());
104 } else {
105 QList<int> sizes = QFontDatabase::pointSizes(family, style);
106 QVERIFY(!sizes.isEmpty());
107 QList<int>::ConstIterator z_it, z_end = sizes.end();
108 for (z_it = sizes.begin(); z_it != z_end; ++z_it) {
109 const int size = *z_it;
110
111 // Initialize the font, and check if it is an exact match
112 font = QFontDatabase::font(family, style, size);
113
114 QFontMetrics fontmetrics(font);
115 QCOMPARE(fontmetrics.ascent() + fontmetrics.descent(),
116 fontmetrics.height());
117 QCOMPARE(fontmetrics.height() + fontmetrics.leading(),
118 fontmetrics.lineSpacing());
119 }
120 }
121 }
122 }
123 }
124
125 void tst_QFontMetrics::boundingRect()
126 {
127 QFont f;
128 f.setPointSize(24);
129 QFontMetrics fm(f);
130 QRect r = fm.boundingRect(QChar('Y'));
131 QVERIFY(r.top() < 0);
132 r = fm.boundingRect(QString("Y"));
133 QVERIFY(r.top() < 0);
134 }
135
136 void tst_QFontMetrics::boundingRect2()
137 {
138 QFont f;
139 f.setPixelSize(16);
140 QFontMetricsF fm(f);
141 QString str("AVAVAVA vvvvvvvvvv fffffffff file");
142 QRectF br = fm.boundingRect(str);
143 QRectF tbr = fm.tightBoundingRect(str);
144 qreal advance = fm.horizontalAdvance(str);
145 // Bounding rect plus bearings should be similar to advance
146 qreal bearings = fm.leftBearing(QChar('A')) + fm.rightBearing(QChar('e'));
147 QVERIFY(qAbs(br.width() + bearings - advance) < fm.averageCharWidth()/2.0);
148 QVERIFY(qAbs(tbr.width() + bearings - advance) < fm.averageCharWidth()/2.0);
149 }
150
151 void tst_QFontMetrics::elidedText_data()
152 {
153 QTest::addColumn<QFont>("font");
154 QTest::addColumn<QString>("text");
155
156 QTest::newRow("helvetica hello") << QFont("helvetica",10) << QString("hello") ;
157 QTest::newRow("helvetica hello &Bye") << QFont("helvetica",10) << QString("hello&Bye") ;
158 }
159
160
161 void tst_QFontMetrics::elidedText()
162 {
163 QFETCH(QFont, font);
164 QFETCH(QString, text);
165 QFontMetrics fm(font);
166 int w = fm.horizontalAdvance(text);
167 QString newtext = fm.elidedText(text,Qt::ElideRight,w+1, 0);
168 QCOMPARE(text,newtext); // should not elide
169 newtext = fm.elidedText(text,Qt::ElideRight,w-1, 0);
170 QVERIFY(text != newtext); // should elide
171 }
172
173 void tst_QFontMetrics::veryNarrowElidedText()
174 {
175 QFont f;
176 QFontMetrics fm(f);
177 QString text("hello");
178 QCOMPARE(fm.elidedText(text, Qt::ElideRight, 0), QString());
179 }
180
181 void tst_QFontMetrics::averageCharWidth()
182 {
183 QFont f;
184 QFontMetrics fm(f);
185 QVERIFY(fm.averageCharWidth() != 0);
186 QFontMetricsF fmf(f);
187 QVERIFY(fmf.averageCharWidth() != 0);
188 }
189
190 void tst_QFontMetrics::bypassShaping()
191 {
192 QFont f;
193 f.setStyleStrategy(QFont::PreferNoShaping);
194 f.setKerning(false);
195
196 QFontMetricsF fm(f);
197 QString text = " A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z";
198 qreal textWidth = fm.horizontalAdvance(text);
199 QVERIFY(textWidth != 0);
200 qreal charsWidth = 0;
201 for (int i = 0; i < text.size(); ++i)
202 charsWidth += fm.horizontalAdvance(text[i]);
203 QCOMPARE(textWidth, charsWidth);
204 }
205
206 template<class FontMetrics, typename PrimitiveType> void elidedMultiLength_helper()
207 {
208 QString text1 = QLatin1String("Long Text 1\x9cShorter\x9csmall");
209 QString text1_long = "Long Text 1";
210 QString text1_short = "Shorter";
211 QString text1_small = "small";
212 FontMetrics fm = FontMetrics(QFont());
213 PrimitiveType width_long = fm.size(0, text1_long).width();
214 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long);
215 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long);
216 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short);
217 PrimitiveType width_short = fm.size(0, text1_short).width();
218 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short + 1), text1_short);
219 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short - 1), text1_small);
220
221 // Not even wide enough for "small" - should use ellipsis
222 QChar ellipsisChar(0x2026);
223 QString text1_el = QString::fromLatin1("s") + ellipsisChar;
224 PrimitiveType width_small = fm.horizontalAdvance(text1_el);
225 QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el);
226 }
227
228 void tst_QFontMetrics::elidedMultiLength()
229 {
230 elidedMultiLength_helper<QFontMetrics, int>();
231 }
232
233 void tst_QFontMetrics::elidedMultiLengthF()
234 {
235 elidedMultiLength_helper<QFontMetricsF, qreal>();
236 }
237
238 void tst_QFontMetrics::inFontUcs4()
239 {
240 int id = QFontDatabase::addApplicationFont(":/fonts/ucs4font.ttf");
241 QVERIFY(id >= 0);
242
243 QFont font("QtTestUcs4");
244 {
245 QFontMetrics fm(font);
246 QVERIFY(fm.inFontUcs4(0x1D7FF));
247 }
248
249 {
250 QFontMetricsF fm(font);
251 QVERIFY(fm.inFontUcs4(0x1D7FF));
252 }
253
254 {
255 QFontEngine *engine = QFontPrivate::get(font)->engineForScript(QChar::Script_Common);
256 QGlyphLayout glyphs;
257 glyphs.numGlyphs = 3;
258 uint buf[3];
259 glyphs.glyphs = buf;
260
261 QString string;
262 {
263 string.append(QChar::highSurrogate(0x1D7FF));
264 string.append(QChar::lowSurrogate(0x1D7FF));
265
266 glyphs.numGlyphs = 3;
267 glyphs.glyphs[0] = 0;
268 QVERIFY(engine->stringToCMap(string.constData(), string.size(),
269 &glyphs, &glyphs.numGlyphs,
270 QFontEngine::GlyphIndicesOnly) > 0);
271 QCOMPARE(glyphs.numGlyphs, 1);
272 QCOMPARE(glyphs.glyphs[0], uint(1));
273 }
274 {
275 string.clear();
276 string.append(QChar::ObjectReplacementCharacter);
277
278 glyphs.numGlyphs = 3;
279 glyphs.glyphs[0] = 0;
280 QVERIFY(engine->stringToCMap(string.constData(), string.size(),
281 &glyphs, &glyphs.numGlyphs,
282 QFontEngine::GlyphIndicesOnly) >= 0);
283 QVERIFY(glyphs.glyphs[0] != 1);
284 }
285 }
286
287 QFontDatabase::removeApplicationFont(id);
288 }
289
290 void tst_QFontMetrics::lineWidth()
291 {
292 // QTBUG-13009, QTBUG-13011
293 QFont smallFont;
294 smallFont.setPointSize(8);
295 smallFont.setWeight(QFont::Light);
296 const QFontMetrics smallFontMetrics(smallFont);
297
298 QFont bigFont;
299 bigFont.setPointSize(40);
300 bigFont.setWeight(QFont::Black);
301 const QFontMetrics bigFontMetrics(bigFont);
302
303 QVERIFY(smallFontMetrics.lineWidth() >= 1);
304 QVERIFY(smallFontMetrics.lineWidth() < bigFontMetrics.lineWidth());
305 }
306
307 void tst_QFontMetrics::mnemonicTextWidth()
308 {
309 // QTBUG-41593
310 QFont f;
311 QFontMetrics fm(f);
312 const QString f1 = "File";
313 const QString f2 = "&File";
314
315 QCOMPARE(fm.size(Qt::TextShowMnemonic, f1), fm.size(Qt::TextShowMnemonic, f2));
316 QCOMPARE(fm.size(Qt::TextHideMnemonic, f1), fm.size(Qt::TextHideMnemonic, f2));
317 }
318
319 void tst_QFontMetrics::leadingBelowLine()
320 {
321 QScriptLine line;
322 line.leading = 10;
323 line.leadingIncluded = true;
324 line.ascent = 5;
325 QCOMPARE(line.height(), line.ascent + line.descent + line.leading);
326 QCOMPARE(line.base(), line.ascent);
327 }
328
329 void tst_QFontMetrics::elidedMetrics()
330 {
331 QString testFont = QFINDTESTDATA("fonts/testfont.ttf");
332 QVERIFY(!testFont.isEmpty());
333
334 int id = QFontDatabase::addApplicationFont(testFont);
335 QVERIFY(id >= 0);
336
337 QFont font(QFontDatabase::applicationFontFamilies(id).at(0));
338 font.setPixelSize(12.0);
339
340 QFontMetricsF metrics(font);
341 QString s = QStringLiteral("VeryLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongText");
342
343 QRectF boundingRect = metrics.boundingRect(s);
344
345 QString elided = metrics.elidedText(s, Qt::ElideRight, boundingRect.width() / 2.0);
346
347 QRectF elidedBoundingRect = metrics.boundingRect(elided);
348
349 QCOMPARE(boundingRect.height(), elidedBoundingRect.height());
350 QCOMPARE(boundingRect.y(), elidedBoundingRect.y());
351
352 QFontDatabase::removeApplicationFont(id);
353 }
354
355 void tst_QFontMetrics::zeroWidthMetrics()
356 {
357 QString zwnj(QChar(0x200c));
358 QString zwsp(QChar(0x200b));
359
360 QFont font;
361 QFontMetricsF fm(font);
362 QCOMPARE(fm.horizontalAdvance(zwnj), 0);
363 QCOMPARE(fm.horizontalAdvance(zwsp), 0);
364 QCOMPARE(fm.boundingRect(zwnj).width(), 0);
365 QCOMPARE(fm.boundingRect(zwsp).width(), 0);
366
367 QString string1 = QStringLiteral("(") + zwnj + QStringLiteral(")");
368 QString string2 = QStringLiteral("(") + zwnj + zwnj + QStringLiteral(")");
369 QString string3 = QStringLiteral("(") + zwsp + QStringLiteral(")");
370 QString string4 = QStringLiteral("(") + zwsp + zwsp + QStringLiteral(")");
371
372 QCOMPARE(fm.horizontalAdvance(string1), fm.horizontalAdvance(string2));
373 QCOMPARE(fm.horizontalAdvance(string3), fm.horizontalAdvance(string4));
374 QCOMPARE(fm.boundingRect(string1).width(), fm.boundingRect(string2).width());
375 QCOMPARE(fm.boundingRect(string3).width(), fm.boundingRect(string4).width());
376 QCOMPARE(fm.tightBoundingRect(string1).width(), fm.tightBoundingRect(string2).width());
377 QCOMPARE(fm.tightBoundingRect(string3).width(), fm.tightBoundingRect(string4).width());
378 }
379
380 void tst_QFontMetrics::verticalMetrics_data()
381 {
382 QTest::addColumn<QFont>("font");
383 QStringList families = QFontDatabase::families();
384 for (const QString &family : families) {
385 QFont font(family);
386 QTest::newRow(family.toUtf8()) << font;
387 }
388 }
389
390 void tst_QFontMetrics::verticalMetrics()
391 {
392 QFETCH(QFont, font);
393 QFontMetrics fm(font);
394 QVERIFY(fm.ascent() != 0 || fm.descent() != 0);
395 }
396
397 void tst_QFontMetrics::largeText_data()
398 {
399 QTest::addColumn<qsizetype>("size");
400 for (int i = 1; i < 20; ++i) {
401 qsizetype size = qsizetype(1) << i;
402 QByteArray rowText = QByteArray::number(size);
403 QTest::newRow(rowText.constData()) << size;
404 }
405 }
406
407 void tst_QFontMetrics::largeText()
408 {
409 QFont font;
410 QFontMetrics fm(font);
411 QFETCH(qsizetype, size);
412 QString string(size, QLatin1Char('A'));
413 QRect boundingRect = fm.boundingRect(string);
414 QVERIFY(boundingRect.isValid());
415 }
416
417 void tst_QFontMetrics::typoLineMetrics()
418 {
419 QString testFont = QFINDTESTDATA("fonts/testfont_linemetrics.otf");
420 QVERIFY(!testFont.isEmpty());
421
422 int id = QFontDatabase::addApplicationFont(testFont);
423 QVERIFY(id >= 0);
424
425 {
426 auto cleanup = qScopeGuard([&id] {
427 if (id >= 0)
428 QFontDatabase::removeApplicationFont(id);
429 });
430
431 QImage img(100, 100, QImage::Format_ARGB32);
432 img.setDevicePixelRatio(1.0);
433 QFont font(QFontDatabase::applicationFontFamilies(id).at(0), &img);
434 font.setPixelSize(18);
435
436 const qreal unitsPerEm = 1000.0;
437
438 QFontMetrics defaultFm(font);
439 const int defaultAscent = defaultFm.ascent();
440 const int defaultDescent = defaultFm.descent();
441 const int defaultLeading = defaultFm.leading();
442
443 QCOMPARE(defaultAscent, qRound(1234.0 / unitsPerEm * font.pixelSize()));
444 QCOMPARE(defaultDescent, qRound(5678.0 / unitsPerEm * font.pixelSize()));
445 QCOMPARE(defaultLeading, 0.0);
446
447 font.setStyleStrategy(QFont::PreferTypoLineMetrics);
448 const QFontMetrics typoFm(font);
449
450 const int typoAscent = typoFm.ascent();
451 const int typoDescent = typoFm.descent();
452 const int typoLeading = typoFm.leading();
453
454 QCOMPARE(typoAscent, qRound(2000.0 / unitsPerEm * font.pixelSize()));
455 QCOMPARE(typoDescent, qRound(3000.0 / unitsPerEm * font.pixelSize()));
456 QCOMPARE(typoLeading, qRound(1000.0 / unitsPerEm * font.pixelSize()));
457 }
458 }
459
460 void tst_QFontMetrics::defaultIgnorableHorizontalAdvance_data()
461 {
462 QTest::addColumn<bool>("withShaping");
463
464 QTest::newRow("With Text Shaping") << true;
465 QTest::newRow("Without Text Shaping") << false;
466 }
467
468 void tst_QFontMetrics::defaultIgnorableHorizontalAdvance()
469 {
470 QFETCH(bool, withShaping);
471
472 // testfont_bidimarks.ttf is a version of testfont.ttf with additional
473 // glyphs for U+200E (LRM) and U+200F (RLM)
474 QString testFont = QFINDTESTDATA("fonts/testfont_bidimarks.ttf");
475 QVERIFY(!testFont.isEmpty());
476
477 int id = QFontDatabase::addApplicationFont(testFont);
478 QVERIFY(id >= 0);
479
480 auto cleanup = qScopeGuard([&id] {
481 if (id >= 0)
482 QFontDatabase::removeApplicationFont(id);
483 });
484
485 QFont withoutSupport;
486 QFont withSupport(QFontDatabase::applicationFontFamilies(id).at(0));
487
488 if (!withShaping) {
489 withoutSupport.setStyleStrategy(QFont::PreferNoShaping);
490 withSupport.setStyleStrategy(QFont::PreferNoShaping);
491 }
492
493 QFontMetrics withoutSupportMetrics(withoutSupport);
494 QFontMetrics withSupportMetrics(withSupport);
495
496 QTextOption opt;
497 opt.setFlags(QTextOption::ShowDefaultIgnorables);
498
499 const QChar LRM = (ushort)0x200e;
500 const QString str = QStringLiteral("[") + LRM + QStringLiteral("]");
501
502 int withoutSupportWithoutIgnorablesAdvance = withoutSupportMetrics.horizontalAdvance(str);
503 int withoutSupportWithIgnorablesAdvance = withoutSupportMetrics.horizontalAdvance(str, opt);
504
505 QCOMPARE_GT(withoutSupportWithoutIgnorablesAdvance, 0);
506 QCOMPARE_EQ(withoutSupportWithIgnorablesAdvance, withoutSupportWithoutIgnorablesAdvance);
507
508 int withSupportWithoutIgnorablesAdvance = withSupportMetrics.horizontalAdvance(str);
509 int withSupportWithIgnorablesAdvance = withSupportMetrics.horizontalAdvance(str, opt);
510
511 QCOMPARE_GT(withSupportWithoutIgnorablesAdvance, 0);
512 QCOMPARE_GT(withSupportWithIgnorablesAdvance, withSupportWithoutIgnorablesAdvance);
513 }
514
515 void tst_QFontMetrics::hugeFontMetrics()
516 {
517 QFont bigFont;
518 bigFont.setPixelSize(10000);
519
520 QFont hugeFont;
521 hugeFont.setPixelSize(32000);
522
523 QFont hugeFontWithTypoLineMetrics;
524 hugeFontWithTypoLineMetrics.setStyleStrategy(QFont::PreferTypoLineMetrics);
525 hugeFontWithTypoLineMetrics.setPixelSize(30000);
526
527 QVERIFY(QFontMetricsF(bigFont).height() < QFontMetricsF(hugeFont).height());
528 QVERIFY(QFontMetricsF(bigFont).height() < QFontMetricsF(hugeFontWithTypoLineMetrics).height());
529 }
530
531 QTEST_MAIN(tst_QFontMetrics)
532 #include "tst_qfontmetrics.moc"