summaryrefslogtreecommitdiffstats
path: root/src/platformsupport
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@digia.com>2014-02-11 13:50:38 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-19 19:36:34 +0100
commitd0784ae43bafbb577ce1d30dcd3327e9271ead2f (patch)
treef40c938e2e741f2e34f9296d0830a45689644437 /src/platformsupport
parente380a48af72575edd86f6aeb33d55ee49e30fbda (diff)
Prevent top/left clipping of anti-aliased glyphs in CoreText font engine
Padding the bounding rect was not enough, as we failed to shift the glyph accordingly so that it would end up in the center of the bounding rect. We also didn't take subpixel-positioning into account, which may shift the position of the glyph too far to the right to be within the image size that we reserve. There are still cases where the glyphs seem clipped compared to the same text rendered with CoreText, but that's because we end up shaping the text slightly differently, resulting in different subpixel positions than what CoreText chooses. Change-Id: Icb88c829f86457b16bdecbc4c24b3f1c23448261 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/platformsupport')
-rw-r--r--src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
index 1036a95a92..4fe78cb568 100644
--- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
+++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
@@ -460,8 +460,46 @@ glyph_metrics_t QCoreTextFontEngine::alphaMapBoundingBox(glyph_t glyph, QFixed s
glyph_metrics_t br = boundingBox(glyph);
qcoretextfontengine_scaleMetrics(br, matrix);
- br.width = qAbs(qRound(br.width)) + 2;
- br.height = qAbs(qRound(br.height)) + 2;
+ // Normalize width and height
+ if (br.width < 0)
+ br.width = -br.width;
+ if (br.height < 0)
+ br.height = -br.height;
+
+ if (format == QFontEngine::Format_A8 || format == QFontEngine::Format_A32) {
+ // Drawing a glyph at x-position 0 with anti-aliasing enabled
+ // will potentially fill the pixel to the left of 0, as the
+ // coordinates are not aligned to the center of pixels. To
+ // prevent clipping of this pixel we need to shift the glyph
+ // in the bitmap one pixel to the right. The shift needs to
+ // be reflected in the glyph metrics as well, so that the final
+ // position of the glyph is correct, which is why doing the
+ // shift in imageForGlyph() is not enough.
+ br.x -= 1;
+
+ // As we've shifted the glyph one pixel to the right, we need
+ // to expand the width of the alpha map bounding box as well.
+ br.width += 1;
+
+ // But we have the same anti-aliasing problem on the right
+ // hand side of the glyph, eg. if the width of the glyph
+ // results in the bounding rect landing between two pixels.
+ // We pad the bounding rect again to account for the possible
+ // anti-aliased drawing.
+ br.width += 1;
+
+ // We also shift the glyph to right right based on the subpixel
+ // position, so we pad the bounding box to take account for the
+ // subpixel positions that may result in the glyph being drawn
+ // one pixel to the right of the 0-subpixel position.
+ br.width += 1;
+
+ // The same same logic as for the x-position needs to be applied
+ // to the y-position, except we don't need to compensate for
+ // the subpixel positioning.
+ br.y -= 1;
+ br.height += 2;
+ }
return br;
}
@@ -474,7 +512,7 @@ QImage QCoreTextFontEngine::imageForGlyph(glyph_t glyph, QFixed subPixelPosition
bool isColorGlyph = glyphFormat == QFontEngine::Format_ARGB;
QImage::Format imageFormat = isColorGlyph ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
- QImage im(br.width.toInt(), br.height.toInt(), imageFormat);
+ QImage im(br.width.ceil().toInt(), br.height.ceil().toInt(), imageFormat);
im.fill(0);
#ifndef Q_OS_IOS