#include #include #include #include const QRect WINDOW_RECT{0, 0, 1600, 1000}; const QSize FONT_RECT_SIZE{300, 230}; const int FONT_SPACING = 20; const int COLUMNS = std::floor(WINDOW_RECT.width() / (FONT_RECT_SIZE.width() + FONT_SPACING)); const QStringList FONTS{ // Font with correct result "Arial", // Fonts with wrong results "Sitka Small", "Sitka Subheading", "Sitka Text", "PMingLiU-ExtB", "MingLiU-ExtB", "Liberation Sans Narrow", "Gabriola", "DejaVu Math TeX Gyre", "Bahnschrift", "Bahnschrift Condensed", "Bahnschrift SemiBold", "Amiri", }; class Paintable final : public QQuickPaintedItem { public: explicit Paintable(QQuickItem* parent = nullptr) : QQuickPaintedItem(parent) { setOpaquePainting(true); } void paintFontDemo(QPainter* painter, QString fontName) { const QString text("Übgo"); // Get font QFont font(fontName); font.setPixelSize(70); // Calculate bounding rect and baseline position QFontMetricsF fontMetrics(font); QRectF boundingRect = fontMetrics.boundingRect(text); QLine baseline(0, 0, boundingRect.width(), 0); // the bounding rect is aligned such that the baseline is at y=0 // Draw bounding rect painter->fillRect(boundingRect, Qt::GlobalColor::yellow); // Draw baseline QPen pen1(Qt::GlobalColor::red, 1); painter->setPen(pen1); painter->drawLine(baseline); // Draw text QPen pen2(Qt::GlobalColor::black, 1); painter->setPen(pen2); painter->setFont(font); painter->drawText(boundingRect, Qt::TextDontClip, text); } void paint(QPainter* painter) override { painter->fillRect(WINDOW_RECT, Qt::GlobalColor::white); for (int i = 0; i < FONTS.size(); i++) { const int col = std::floor(i % COLUMNS); const int row = std::floor(i / COLUMNS); const QTransform transform = QTransform::fromTranslate( FONT_RECT_SIZE.width() / 4 + col * (FONT_RECT_SIZE.width() + FONT_SPACING), FONT_RECT_SIZE.height() * 0.75 + row * (FONT_RECT_SIZE.height() + FONT_SPACING)); painter->setWorldTransform(transform); paintFontDemo(painter, FONTS[i]); } } }; int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); auto* qquickWindow = new QQuickWindow(); qquickWindow->setTitle("Windows bounding rect demo"); qquickWindow->setWidth(WINDOW_RECT.width()); qquickWindow->setHeight(WINDOW_RECT.height()); auto* paintable = new Paintable(qquickWindow->contentItem()); paintable->setSize(WINDOW_RECT.size()); paintable->setVisible(true); qquickWindow->show(); return app.exec(); }