From 5dfa22f3fde991a9f0ee2f225d938194baea788c Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 8 Sep 2014 15:11:25 -0700 Subject: [PATCH] Adjusting font sizes. --- interface/src/ui/TextRenderer.cpp | 39 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/interface/src/ui/TextRenderer.cpp b/interface/src/ui/TextRenderer.cpp index d6eaec9608..4ced8c1f2e 100644 --- a/interface/src/ui/TextRenderer.cpp +++ b/interface/src/ui/TextRenderer.cpp @@ -40,14 +40,6 @@ TextRenderer::TextRenderer(const char* family, int pointSize, int weight, bool i _color(color) { _font.setKerning(false); - - // double the font size for "Retina" displays - float ratio = QApplication::desktop()->windowHandle()->devicePixelRatio(); - if (ratio != 1.0f && false) { - _font.setPointSize(_font.pointSize() * ratio); - _metrics = QFontMetrics(_font); - _effectThickness *= ratio; - } } TextRenderer::~TextRenderer() { @@ -91,7 +83,7 @@ int TextRenderer::draw(int x, int y, const char* str) { int bottom = y + glyph.bounds().y(); int top = y + glyph.bounds().y() + glyph.bounds().height(); - float scale = 1.0 / IMAGE_SIZE; + float scale = QApplication::desktop()->windowHandle()->devicePixelRatio() / IMAGE_SIZE; float ls = glyph.location().x() * scale; float rs = (glyph.location().x() + glyph.bounds().width()) * scale; float bt = glyph.location().y() * scale; @@ -137,21 +129,25 @@ const Glyph& TextRenderer::getGlyph(char c) { } // we use 'J' as a representative size for the solid block character QChar ch = (c == SOLID_BLOCK_CHAR) ? QChar('J') : QChar(c); - QRect bounds = _metrics.boundingRect(ch); - if (bounds.isEmpty()) { + QRect baseBounds = _metrics.boundingRect(ch); + if (baseBounds.isEmpty()) { glyph = Glyph(0, QPoint(), QRect(), _metrics.width(ch)); return glyph; } // grow the bounds to account for effect, if any if (_effectType == SHADOW_EFFECT) { - bounds.adjust(-_effectThickness, 0, 0, _effectThickness); + baseBounds.adjust(-_effectThickness, 0, 0, _effectThickness); } else if (_effectType == OUTLINE_EFFECT) { - bounds.adjust(-_effectThickness, -_effectThickness, _effectThickness, _effectThickness); + baseBounds.adjust(-_effectThickness, -_effectThickness, _effectThickness, _effectThickness); } // grow the bounds to account for antialiasing - bounds.adjust(-1, -1, 1, 1); + baseBounds.adjust(-1, -1, 1, 1); + + // adjust bounds for device pixel scaling + float ratio = QApplication::desktop()->windowHandle()->devicePixelRatio(); + QRect bounds(baseBounds.x() * ratio, baseBounds.y() * ratio, baseBounds.width() * ratio, baseBounds.height() * ratio); if (_x + bounds.width() > IMAGE_SIZE) { // we can't fit it on the current row; move to next @@ -180,9 +176,16 @@ const Glyph& TextRenderer::getGlyph(char c) { } else { image.fill(0); QPainter painter(&image); - painter.setFont(_font); + QFont font = _font; + if (ratio == 1.0f) { + painter.setFont(_font); + } else { + QFont enlargedFont = _font; + enlargedFont.setPointSize(_font.pointSize() * ratio); + painter.setFont(enlargedFont); + } if (_effectType == SHADOW_EFFECT) { - for (int i = 0; i < _effectThickness; i++) { + for (int i = 0; i < _effectThickness * ratio; i++) { painter.drawText(-bounds.x() - 1 - i, -bounds.y() + 1 + i, ch); } } else if (_effectType == OUTLINE_EFFECT) { @@ -191,7 +194,7 @@ const Glyph& TextRenderer::getGlyph(char c) { font.setStyleStrategy(QFont::ForceOutline); path.addText(-bounds.x() - 0.5, -bounds.y() + 0.5, font, ch); QPen pen; - pen.setWidth(_effectThickness); + pen.setWidth(_effectThickness * ratio); pen.setJoinStyle(Qt::RoundJoin); pen.setCapStyle(Qt::RoundCap); painter.setPen(pen); @@ -203,7 +206,7 @@ const Glyph& TextRenderer::getGlyph(char c) { } glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, bounds.width(), bounds.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.constBits()); - glyph = Glyph(_currentTextureID, QPoint(_x, _y), bounds, _metrics.width(ch)); + glyph = Glyph(_currentTextureID, QPoint(_x / ratio, _y / ratio), baseBounds, _metrics.width(ch)); _x += bounds.width(); _rowHeight = qMax(_rowHeight, bounds.height());