From f1a3069afb7fcb79646ee068be4b23dddb3aeefd Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 14 Oct 2021 15:49:03 +0200 Subject: [PATCH] Unify QFontEngine::boundingRect for coretext and Windows They were both using the same algorithm, and also both had the same mistake of not handling left-bearing. This unifies them and adds left bearing handling. Change-Id: Id06392abde0bfeb8b49faf4632082b2e25352497 Reviewed-by: Qt CI Bot Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/gui/text/coretext/qfontengine_coretext.mm | 8 ------- .../text/coretext/qfontengine_coretext_p.h | 1 - src/gui/text/qfontengine.cpp | 21 +++++++++++++++++++ src/gui/text/qfontengine_p.h | 3 ++- src/gui/text/windows/qwindowsfontengine.cpp | 12 ----------- src/gui/text/windows/qwindowsfontengine_p.h | 1 - 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/gui/text/coretext/qfontengine_coretext.mm b/src/gui/text/coretext/qfontengine_coretext.mm index 89166489926..e4556207874 100644 --- a/src/gui/text/coretext/qfontengine_coretext.mm +++ b/src/gui/text/coretext/qfontengine_coretext.mm @@ -301,14 +301,6 @@ bool QCoreTextFontEngine::stringToCMap(const QChar *str, int len, QGlyphLayout * return true; } -glyph_metrics_t QCoreTextFontEngine::boundingBox(const QGlyphLayout &glyphs) -{ - QFixed w; - for (int i = 0; i < glyphs.numGlyphs; ++i) - w += glyphs.effectiveAdvance(i); - return glyph_metrics_t(0, -(ascent()), w - lastRightBearing(glyphs), ascent()+descent(), w, 0); -} - glyph_metrics_t QCoreTextFontEngine::boundingBox(glyph_t glyph) { glyph_metrics_t ret; diff --git a/src/gui/text/coretext/qfontengine_coretext_p.h b/src/gui/text/coretext/qfontengine_coretext_p.h index f81e84fbc01..665b827f118 100644 --- a/src/gui/text/coretext/qfontengine_coretext_p.h +++ b/src/gui/text/coretext/qfontengine_coretext_p.h @@ -41,7 +41,6 @@ public: bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, ShaperFlags flags) const override; void recalcAdvances(QGlyphLayout *, ShaperFlags) const override; - glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) override; glyph_metrics_t boundingBox(glyph_t glyph) override; QFixed capHeight() const override; diff --git a/src/gui/text/qfontengine.cpp b/src/gui/text/qfontengine.cpp index 6814b9f2b5f..61705842dbd 100644 --- a/src/gui/text/qfontengine.cpp +++ b/src/gui/text/qfontengine.cpp @@ -562,6 +562,16 @@ qreal QFontEngine::minRightBearing() const return m_minRightBearing; } +glyph_metrics_t QFontEngine::boundingBox(const QGlyphLayout &glyphs) +{ + QFixed w; + for (int i = 0; i < glyphs.numGlyphs; ++i) + w += glyphs.effectiveAdvance(i); + const QFixed leftBearing = firstLeftBearing(glyphs); + const QFixed rightBearing = lastRightBearing(glyphs); + return glyph_metrics_t(leftBearing, -(ascent()), w - leftBearing - rightBearing, ascent() + descent(), w, 0); +} + glyph_metrics_t QFontEngine::tightBoundingBox(const QGlyphLayout &glyphs) { glyph_metrics_t overall; @@ -1452,6 +1462,17 @@ bool QFontEngine::hasUnreliableGlyphOutline() const return glyphFormat == QFontEngine::Format_ARGB; } +QFixed QFontEngine::firstLeftBearing(const QGlyphLayout &glyphs) +{ + if (glyphs.numGlyphs >= 1) { + glyph_t glyph = glyphs.glyphs[0]; + glyph_metrics_t gi = boundingBox(glyph); + if (gi.isValid()) + return gi.leftBearing(); + } + return 0; +} + QFixed QFontEngine::lastRightBearing(const QGlyphLayout &glyphs) { if (glyphs.numGlyphs >= 1) { diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index 6289afa9d0c..0e3196d75b9 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -189,7 +189,7 @@ public: virtual void removeGlyphFromCache(glyph_t); - virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) = 0; + virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs); virtual glyph_metrics_t boundingBox(glyph_t glyph) = 0; virtual glyph_metrics_t boundingBox(glyph_t glyph, const QTransform &matrix); glyph_metrics_t tightBoundingBox(const QGlyphLayout &glyphs); @@ -332,6 +332,7 @@ public: protected: explicit QFontEngine(Type type); + QFixed firstLeftBearing(const QGlyphLayout &glyphs); QFixed lastRightBearing(const QGlyphLayout &glyphs); QFixed calculatedCapHeight() const; diff --git a/src/gui/text/windows/qwindowsfontengine.cpp b/src/gui/text/windows/qwindowsfontengine.cpp index 545b5459e2e..5aef72eab34 100644 --- a/src/gui/text/windows/qwindowsfontengine.cpp +++ b/src/gui/text/windows/qwindowsfontengine.cpp @@ -352,18 +352,6 @@ void QWindowsFontEngine::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::Shape } } -glyph_metrics_t QWindowsFontEngine::boundingBox(const QGlyphLayout &glyphs) -{ - if (glyphs.numGlyphs == 0) - return glyph_metrics_t(); - - QFixed w = 0; - for (int i = 0; i < glyphs.numGlyphs; ++i) - w += glyphs.effectiveAdvance(i); - - return glyph_metrics_t(0, -tm.tmAscent, w - lastRightBearing(glyphs), tm.tmHeight, w, 0); -} - bool QWindowsFontEngine::getOutlineMetrics(glyph_t glyph, const QTransform &t, glyph_metrics_t *metrics) const { Q_ASSERT(metrics != 0); diff --git a/src/gui/text/windows/qwindowsfontengine_p.h b/src/gui/text/windows/qwindowsfontengine_p.h index c5ef40761d2..afe8ee4ca58 100644 --- a/src/gui/text/windows/qwindowsfontengine_p.h +++ b/src/gui/text/windows/qwindowsfontengine_p.h @@ -57,7 +57,6 @@ public: HGDIOBJ selectDesignFont() const; - glyph_metrics_t boundingBox(const QGlyphLayout &glyphs) override; glyph_metrics_t boundingBox(glyph_t g) override { return boundingBox(g, QTransform()); } glyph_metrics_t boundingBox(glyph_t g, const QTransform &t) override;