Merge pull request #4271 from huffman/fix-overlays-text-size

Fix overlays text size
This commit is contained in:
Andrew Meadows 2015-02-12 09:35:37 -08:00
commit 7304585103
3 changed files with 21 additions and 36 deletions

View file

@ -20,6 +20,7 @@ const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 };
const float DEFAULT_BACKGROUND_ALPHA = 0.7f;
const float DEFAULT_MARGIN = 0.1f;
const int FIXED_FONT_POINT_SIZE = 40;
const int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 40.0f; // this is a ratio determined through experimentation
const float LINE_SCALE_RATIO = 1.2f;
Text3DOverlay::Text3DOverlay() :
@ -104,8 +105,6 @@ void Text3DOverlay::render(RenderArgs* args) {
glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND);
DependencyManager::get<GeometryCache>()->renderQuad(topLeft, bottomRight, quadColor);
const int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 40.0f; // this is a ratio determined through experimentation
// Same font properties as textSize()
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
float maxHeight = (float)textRenderer->computeExtent("Xy").y * LINE_SCALE_RATIO;
@ -229,22 +228,12 @@ Text3DOverlay* Text3DOverlay::createClone() const {
}
QSizeF Text3DOverlay::textSize(const QString& text) const {
auto textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
auto extents = textRenderer->computeExtent(text);
QFont font(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); // Same font properties as render()
QFontMetrics fontMetrics(font);
const float TEXT_SCALE_ADJUST = 1.025f; // Experimentally detemined for the specified font
const int TEXT_HEIGHT_ADJUST = -10;
float scaleFactor = _lineHeight * TEXT_SCALE_ADJUST * LINE_SCALE_RATIO / (float)FIXED_FONT_POINT_SIZE;
float maxHeight = (float)textRenderer->computeExtent("Xy").y * LINE_SCALE_RATIO;
float pointToWorldScale = (maxHeight / FIXED_FONT_SCALING_RATIO) * _lineHeight;
QStringList lines = text.split(QRegExp("\r\n|\r|\n"));
float width = 0.0f;
for (int i = 0; i < lines.count(); i += 1) {
width = std::max(width, scaleFactor * (float)fontMetrics.width(qPrintable(lines[i])));
}
float height = lines.count() * scaleFactor * (float)(fontMetrics.height() + TEXT_HEIGHT_ADJUST);
return QSizeF(width, height);
return QSizeF(extents.x, extents.y) * pointToWorldScale;
}

View file

@ -165,19 +165,8 @@ QScriptValue TextOverlay::getProperty(const QString& property) {
}
QSizeF TextOverlay::textSize(const QString& text) const {
auto textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT);
auto extents = textRenderer->computeExtent(text);
QFont font(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); // Same font properties as render()
QFontMetrics fontMetrics(font);
const int TEXT_HEIGHT_ADJUST = -2; // Experimentally determined for the specified font
QStringList lines = text.split(QRegExp("\r\n|\r|\n"));
int width = 0;
for (int i = 0; i < lines.count(); i += 1) {
width = std::max(width, fontMetrics.width(qPrintable(lines[i])));
}
int height = lines.count() * (fontMetrics.height() + TEXT_HEIGHT_ADJUST);
return QSizeF(width, height);
return QSizeF(extents.x, extents.y);
}

View file

@ -136,10 +136,12 @@ public:
glm::vec2 drawString(float x, float y, const QString & str,
const glm::vec4& color, TextRenderer::EffectType effectType,
const glm::vec2& bound) const;
const glm::vec2& bound);
private:
QStringList tokenizeForWrapping(const QString & str) const;
bool _initialized;
};
static QHash<QString, Font*> LOADED_FONTS;
@ -186,7 +188,7 @@ Font* loadFont(const QString& family) {
return LOADED_FONTS[family];
}
Font::Font() {
Font::Font() : _initialized(false) {
static bool fontResourceInitComplete = false;
if (!fontResourceInitComplete) {
Q_INIT_RESOURCE(fonts);
@ -255,8 +257,6 @@ void Font::read(QIODevice& in) {
// store in the character to glyph hash
_glyphs[g.c] = g;
};
setupGL();
}
struct TextureVertex {
@ -291,6 +291,11 @@ QRectF Glyph::textureBounds(const glm::vec2 & textureSize) const {
}
void Font::setupGL() {
if (_initialized) {
return;
}
_initialized = true;
_texture = TexturePtr(
new QOpenGLTexture(_image, QOpenGLTexture::GenerateMipMaps));
_program = ProgramPtr(new QOpenGLShaderProgram());
@ -411,7 +416,9 @@ glm::vec2 Font::computeExtent(const QString & str) const {
// even without explicit line feeds.
glm::vec2 Font::drawString(float x, float y, const QString & str,
const glm::vec4& color, TextRenderer::EffectType effectType,
const glm::vec2& bounds) const {
const glm::vec2& bounds) {
setupGL();
// Stores how far we've moved from the start of the string, in DTP units
glm::vec2 advance(0, -_rowHeight - _descent);