From 1e7922262c29ba29a70226cf8894645f46df3ca2 Mon Sep 17 00:00:00 2001 From: Tom Cooksey Date: Sun, 3 Jan 2010 15:35:53 +0100 Subject: Introduce new "snapToPixelGrid" flag to GL2 engine for drawText When we're rendering text, the glyphs need to be aligned to the pixel grid otherwise we get strange artifacts. Normally text is drawn at integer coordinates, however it is still possible to have a transform which translates by a non-integer offset. This patch adds a flag to the engine which can be used to snap any translate to the pixel grid. Task-number: QTBUG-7094 Reviewed-By: Kim --- .../gl2paintengineex/qpaintengineex_opengl2.cpp | 47 +++++++++++++++++++--- .../gl2paintengineex/qpaintengineex_opengl2_p.h | 2 + 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp index 0f8e945..a41d439 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp @@ -589,19 +589,28 @@ void QGL2PaintEngineExPrivate::updateMatrix() const GLfloat wfactor = 2.0f / width; const GLfloat hfactor = -2.0f / height; + GLfloat dx = transform.dx(); + GLfloat dy = transform.dy(); + + // Non-integer translates can have strange effects for some rendering operations such as + // anti-aliased text rendering. In such cases, we snap the translate to the pixel grid. + if (snapToPixelGrid && transform.type() == QTransform::TxTranslate) { + // 0.50 needs to rounded down to 0.0 for consistency with raster engine: + dx = ceilf(dx - 0.5f); + dy = ceilf(dy - 0.5f); + } if (addOffset) { - pmvMatrix[2][0] = (wfactor * (transform.dx() + 0.49f)) - transform.m33(); - pmvMatrix[2][1] = (hfactor * (transform.dy() + 0.49f)) + transform.m33(); - } else { - pmvMatrix[2][0] = (wfactor * transform.dx()) - transform.m33(); - pmvMatrix[2][1] = (hfactor * transform.dy()) + transform.m33(); + dx += 0.49f; + dy += 0.49f; } pmvMatrix[0][0] = (wfactor * transform.m11()) - transform.m13(); pmvMatrix[1][0] = (wfactor * transform.m21()) - transform.m23(); + pmvMatrix[2][0] = (wfactor * dx) - transform.m33(); pmvMatrix[0][1] = (hfactor * transform.m12()) + transform.m13(); pmvMatrix[1][1] = (hfactor * transform.m22()) + transform.m23(); + pmvMatrix[2][1] = (hfactor * dy) + transform.m33(); pmvMatrix[0][2] = transform.m13(); pmvMatrix[1][2] = transform.m23(); pmvMatrix[2][2] = transform.m33(); @@ -699,6 +708,11 @@ void QGL2PaintEngineExPrivate::drawTexture(const QGLRect& dest, const QGLRect& s matrixDirty = true; } + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + if (prepareForDraw(opaque)) shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT); @@ -856,6 +870,11 @@ void QGL2PaintEngineExPrivate::fill(const QVectorPath& path) matrixDirty = true; } + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + // Might need to call updateMatrix to re-calculate inverseScale if (matrixDirty) updateMatrix(); @@ -1249,6 +1268,11 @@ void QGL2PaintEngineExPrivate::stroke(const QVectorPath &path, const QPen &pen) matrixDirty = true; } + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + const Qt::PenStyle penStyle = qpen_style(pen); const QBrush &penBrush = qpen_brush(pen); const bool opaque = penBrush.isOpaque() && s->opacity > 0.99; @@ -1519,6 +1543,10 @@ void QGL2PaintEngineExPrivate::drawCachedGlyphs(const QPointF &p, QFontEngineGly addOffset = false; matrixDirty = true; } + if (!snapToPixelGrid) { + snapToPixelGrid = true; + matrixDirty = true; + } QBrush pensBrush = q->state()->pen.brush(); setBrush(pensBrush); @@ -1637,6 +1665,11 @@ void QGL2PaintEngineExPrivate::drawPixmaps(const QDrawPixmaps::Data *drawingData matrixDirty = true; } + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + bool allOpaque = true; for (int i = 0; i < dataCount; ++i) { @@ -1918,6 +1951,10 @@ void QGL2PaintEngineExPrivate::writeClip(const QVectorPath &path, uint value) addOffset = false; matrixDirty = true; } + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } if (matrixDirty) updateMatrix(); diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h index 9a5c447..7d5cb52 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h @@ -167,6 +167,7 @@ public: width(0), height(0), ctx(0), useSystemClip(true), + snapToPixelGrid(false), addOffset(false), inverseScale(1) { } @@ -260,6 +261,7 @@ public: GLfloat staticVertexCoordinateArray[8]; GLfloat staticTextureCoordinateArray[8]; + bool snapToPixelGrid; bool addOffset; // When enabled, adds a 0.49,0.49 offset to matrix in updateMatrix GLfloat pmvMatrix[3][3]; GLfloat inverseScale; -- cgit v0.12