From cdd7163c6071ac2020a1750cf2820ecea057abe4 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 19 Oct 2022 14:39:38 +0200 Subject: [PATCH] macOS: Fix synthetic bold on scaled painters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bold synthesizing on macOS works by redrawing the text at a subpixel offset. However, since the text position is given in user space (pre-transformed) we need to scale the offset by the current matrix, otherwise you can get large gaps between the two instances of the text. [ChangeLog][macOS] Fixed an issue with synthetic bold on fonts when the painter had a scale Fixes: QTBUG-103215 Change-Id: Idf0ae3da75a55825cbb5598561ef8fd9a512c6a5 Reviewed-by: Tor Arne Vestbø Reviewed-by: Timur Pocheptsov --- src/gui/text/coretext/qfontengine_coretext.mm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/gui/text/coretext/qfontengine_coretext.mm b/src/gui/text/coretext/qfontengine_coretext.mm index 32f7ed20949..599a7f08c30 100644 --- a/src/gui/text/coretext/qfontengine_coretext.mm +++ b/src/gui/text/coretext/qfontengine_coretext.mm @@ -362,6 +362,7 @@ bool QCoreTextFontEngine::hasColorGlyphs() const return glyphFormat == QFontEngine::Format_ARGB; } +Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp void QCoreTextFontEngine::draw(CGContextRef ctx, qreal x, qreal y, const QTextItemInt &ti, int paintDeviceHeight) { QVarLengthArray positions; @@ -406,7 +407,15 @@ void QCoreTextFontEngine::draw(CGContextRef ctx, qreal x, qreal y, const QTextIt CTFontDrawGlyphs(ctfont, cgGlyphs.data(), cgPositions.data(), glyphs.size(), ctx); if (synthesisFlags & QFontEngine::SynthesizedBold) { - CGContextSetTextPosition(ctx, positions[0].x.toReal() + 0.5 * lineThickness().toReal(), + QTransform matrix(cgMatrix.a, cgMatrix.b, cgMatrix.c, cgMatrix.d, cgMatrix.tx, cgMatrix.ty); + + qreal boldOffset = 0.5 * lineThickness().toReal(); + qreal scale; + qt_scaleForTransform(matrix, &scale); + boldOffset *= scale; + + CGContextSetTextPosition(ctx, + positions[0].x.toReal() + boldOffset, positions[0].y.toReal()); CTFontDrawGlyphs(ctfont, cgGlyphs.data(), cgPositions.data(), glyphs.size(), ctx); } @@ -753,7 +762,13 @@ QImage QCoreTextFontEngine::imageForGlyph(glyph_t glyph, const QFixedPoint &subP CTFontDrawGlyphs(ctfont, &cgGlyph, &CGPointZero, 1, ctx); if (synthesisFlags & QFontEngine::SynthesizedBold) { - CGContextSetTextPosition(ctx, pos_x + 0.5 * lineThickness().toReal(), pos_y); + qreal boldOffset = 0.5 * lineThickness().toReal(); + + qreal scale; + qt_scaleForTransform(matrix, &scale); + boldOffset *= scale; + + CGContextSetTextPosition(ctx, pos_x + boldOffset, pos_y); CTFontDrawGlyphs(ctfont, &cgGlyph, &CGPointZero, 1, ctx); } } else {