From d79a9b1a4f694a227ce62ccab9b44685a9755916 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Fri, 15 Jan 2021 09:11:07 +0100 Subject: Implement vertical subpixel positioning where available For some use cases, vertical subpixel positioning may be useful, as it allows you to vertically align text with other painting primitives. This does impose an overhead, so we make it opt-int with a render hint on the painter. Note that this is only supported on Freetype currently. It might be possible to support on older macOS versions, prior to Mojave (which has disabled subpixel positioning entirely), but since it would have limited usefulness and Freetype is cross-platform anyway, I skipped that. Note: This drive-by-fixes an issue with subpixel positioning where glyphs would always be offset by 1/64, because we added the aliasing offset *after* we had determined the closest subpixel position. The idea of this, as far as I can understand, is rather to snap to nearest subpixel position upwards, not to add an offset to all glyphs, so it should be added before finding the correct position. It had a subtle visual effect when animating the position. It might be that we could get rid of it entirely, as I haven't been able to reproduce any issues with that, but I have moved it instead, to match what I believe the intention was. [ChangeLog][QtGui][Text] Added render hint flag QPainter::VerticalSubpixelPositioning which will position text at subpixel positions vertically whenever supported. In absence of this, text position will be rounded vertically as before. Fixes: QTBUG-35682 Change-Id: I8ce7a72a64e5a0924dac7c244e3e07c2938bfd09 Reviewed-by: Qt CI Bot Reviewed-by: Allan Sandfeld Jensen --- src/gui/painting/qpaintengine_raster.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/gui/painting/qpaintengine_raster.cpp') diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index ca1c4f831e..26f8de5b8b 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -2812,6 +2812,9 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, Q_D(QRasterPaintEngine); QRasterPaintEngineState *s = state(); + bool verticalSubPixelPositions = fontEngine->supportsVerticalSubPixelPositions() + && (s->renderHints & QPainter::VerticalSubpixelPositioning) != 0; + if (fontEngine->hasInternalCaching()) { QFontEngine::GlyphFormat neededFormat = painter()->device()->devType() == QInternal::Widget @@ -2822,7 +2825,9 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, neededFormat = QFontEngine::Format_Mono; for (int i = 0; i < numGlyphs; i++) { - QFixed spp = fontEngine->subPixelPositionForX(positions[i].x); + QFixedPoint spp = fontEngine->subPixelPositionFor(positions[i]); + if (!verticalSubPixelPositions) + spp.y = 0; const QFontEngine::Glyph *alphaMap = fontEngine->glyphData(glyphs[i], spp, neededFormat, s->matrix); if (!alphaMap) @@ -2847,9 +2852,13 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, Q_UNREACHABLE(); }; + QFixed y = verticalSubPixelPositions + ? qFloor(positions[i].y) + : qRound(positions[i].y); + alphaPenBlt(alphaMap->data, bytesPerLine, depth, qFloor(positions[i].x) + alphaMap->x, - qRound(positions[i].y) - alphaMap->y, + qFloor(y) - alphaMap->y, alphaMap->width, alphaMap->height, fontEngine->expectsGammaCorrectedBlending()); } @@ -2864,7 +2873,7 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, fontEngine->setGlyphCache(nullptr, cache); } - cache->populate(fontEngine, numGlyphs, glyphs, positions); + cache->populate(fontEngine, numGlyphs, glyphs, positions, s->renderHints); cache->fillInPendingGlyphs(); const QImage &image = cache->image(); @@ -2881,15 +2890,20 @@ bool QRasterPaintEngine::drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, int margin = fontEngine->glyphMargin(glyphFormat); const uchar *bits = image.bits(); for (int i=0; isubPixelPositionFor(positions[i]); + if (!verticalSubPixelPositions) + subPixelPosition.y = 0; - QFixed subPixelPosition = fontEngine->subPixelPositionForX(positions[i].x); QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphs[i], subPixelPosition); const QTextureGlyphCache::Coord &c = cache->coords[glyph]; if (c.isNull()) continue; int x = qFloor(positions[i].x) + c.baseLineX - margin; - int y = qRound(positions[i].y) - c.baseLineY - margin; + int y = (verticalSubPixelPositions + ? qFloor(positions[i].y) + : qRound(positions[i].y)); + y -= c.baseLineY + margin; // printf("drawing [%d %d %d %d] baseline [%d %d], glyph: %d, to: %d %d, pos: %d %d\n", // c.x, c.y, -- cgit v1.2.3