From 56d9ec81afc59b1cf6aaae7675ff460a6bc664a8 Mon Sep 17 00:00:00 2001 From: Mika Salmela Date: Tue, 28 Oct 2014 12:28:02 +0200 Subject: Squeeze labels on ES2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the label text with slightly smaller font would fit into smaller texture, force to use the smaller one. Task-number: QTRD-3395 Change-Id: Ib70f6722ea8403e4aa25ed190b21c059d9d0e062 Reviewed-by: Miikka Heikkinen Reviewed-by: Tomi Korpipää --- src/datavisualization/utils/texturehelper.cpp | 5 ++--- src/datavisualization/utils/utils.cpp | 31 +++++++++++++++++++++------ src/datavisualization/utils/utils_p.h | 2 +- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/datavisualization/utils/texturehelper.cpp b/src/datavisualization/utils/texturehelper.cpp index 2c40eb6a..939ce901 100644 --- a/src/datavisualization/utils/texturehelper.cpp +++ b/src/datavisualization/utils/texturehelper.cpp @@ -59,9 +59,8 @@ GLuint TextureHelper::create2DTexture(const QImage &image, bool useTrilinearFilt QImage texImage = image; #if defined(QT_OPENGL_ES_2) - GLuint temp; - GLuint imageWidth = Utils::getNearestPowerOfTwo(image.width(), temp); - GLuint imageHeight = Utils::getNearestPowerOfTwo(image.height(), temp); + GLuint imageWidth = Utils::getNearestPowerOfTwo(image.width()); + GLuint imageHeight = Utils::getNearestPowerOfTwo(image.height()); if (smoothScale) { texImage = image.scaled(imageWidth, imageHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); diff --git a/src/datavisualization/utils/utils.cpp b/src/datavisualization/utils/utils.cpp index 2767451e..534c79f9 100644 --- a/src/datavisualization/utils/utils.cpp +++ b/src/datavisualization/utils/utils.cpp @@ -27,11 +27,10 @@ QT_BEGIN_NAMESPACE_DATAVISUALIZATION static GLint maxTextureSize = 0; // Safe, as all instances have the same texture size -GLuint Utils::getNearestPowerOfTwo(GLuint value, GLuint &padding) +GLuint Utils::getNearestPowerOfTwo(GLuint value) { GLuint powOfTwoValue = MIN_POWER; NUM_IN_POWER(powOfTwoValue, value); - padding = powOfTwoValue - value; return powOfTwoValue; } @@ -60,7 +59,9 @@ QImage Utils::printTextToImage(const QFont &font, const QString &text, const QCo glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); GLuint paddingWidth = 20; +#if !defined(QT_OPENGL_ES_2) // TODO fix ifdef for dynamic OpenGL GLuint paddingHeight = 20; +#endif // Calculate text dimensions QFont valueFont = font; valueFont.setPointSize(textureFontSize); @@ -78,23 +79,41 @@ QImage Utils::printTextToImage(const QFont &font, const QString &text, const QCo QSize labelSize; qreal fontRatio = 1.0; +#if defined(QT_OPENGL_ES_2) // TODO fix ifdef for dynamic OpenGL + // Test if text with slighly smaller font would fit into one step smaller texture + // ie. if the text is just exceeded the smaller texture boundary, it would + // make a label with large empty space + GLuint prePadding = 20; + GLint targetWidth = maxTextureSize; + uint testWidth = getNearestPowerOfTwo(valueStrWidth + prePadding) >> 1; + int diffToFit = (valueStrWidth + prePadding) - testWidth; + int maxSqueeze = int((valueStrWidth + prePadding) * 0.1f); + if (diffToFit < maxSqueeze && maxTextureSize > GLint(testWidth)) + targetWidth = testWidth; +#endif + bool sizeOk = false; int currentFontSize = textureFontSize; do { -#if defined(QT_OPENGL_ES_2) +#if defined(QT_OPENGL_ES_2) // TODO fix ifdef for dynamic OpenGL // ES2 can't handle textures with dimensions not in power of 2. Resize labels accordingly. // Add some padding before converting to power of two to avoid too tight fit - GLuint prePadding = 5; labelSize = QSize(valueStrWidth + prePadding, valueStrHeight + prePadding); - labelSize.setWidth(getNearestPowerOfTwo(labelSize.width(), paddingWidth)); - labelSize.setHeight(getNearestPowerOfTwo(labelSize.height(), paddingHeight)); + labelSize.setWidth(getNearestPowerOfTwo(labelSize.width())); + labelSize.setHeight(getNearestPowerOfTwo(labelSize.height())); #else if (!labelBackground) labelSize = QSize(valueStrWidth, valueStrHeight); else labelSize = QSize(valueStrWidth + paddingWidth * 2, valueStrHeight + paddingHeight * 2); #endif + +#if defined(QT_OPENGL_ES_2) + if (!maxTextureSize || (labelSize.width() <= maxTextureSize + && labelSize.width() <= targetWidth)) { +#else if (!maxTextureSize || labelSize.width() <= maxTextureSize) { +#endif // Make sure the label is not too wide sizeOk = true; } else if (--currentFontSize == 4) { diff --git a/src/datavisualization/utils/utils_p.h b/src/datavisualization/utils/utils_p.h index 805c41a7..fdcb0da0 100644 --- a/src/datavisualization/utils/utils_p.h +++ b/src/datavisualization/utils/utils_p.h @@ -45,7 +45,7 @@ public: ParamTypeReal }; - static GLuint getNearestPowerOfTwo(GLuint value, GLuint &padding); + static GLuint getNearestPowerOfTwo(GLuint value); static QVector4D vectorFromColor(const QColor &color); static QColor colorFromVector(const QVector3D &colorVector); static QColor colorFromVector(const QVector4D &colorVector); -- cgit v1.2.3