Extend TextOverlay to accept font size

Fix TextRenderer color rendering
This commit is contained in:
Stojce Slavkovski 2014-06-07 22:24:35 +02:00
parent 9810335adf
commit 6365f6b857
4 changed files with 26 additions and 10 deletions

View file

@ -26,9 +26,9 @@ Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int wid
}
TextRenderer::TextRenderer(const char* family, int pointSize, int weight,
bool italic, EffectType effectType, int effectThickness)
bool italic, EffectType effectType, int effectThickness, QColor color)
: _font(family, pointSize, weight, italic), _metrics(_font), _effectType(effectType),
_effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0) {
_effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0), _color(color) {
_font.setKerning(false);
}
@ -157,7 +157,7 @@ const Glyph& TextRenderer::getGlyph(char c) {
// render the glyph into an image and copy it into the texture
QImage image(bounds.width(), bounds.height(), QImage::Format_ARGB32);
if (c == SOLID_BLOCK_CHAR) {
image.fill(QColor(255, 255, 255));
image.fill(_color);
} else {
image.fill(0);
@ -180,7 +180,7 @@ const Glyph& TextRenderer::getGlyph(char c) {
painter.setRenderHint(QPainter::Antialiasing);
painter.drawPath(path);
}
painter.setPen(QColor(255, 255, 255));
painter.setPen(_color);
painter.drawText(-bounds.x(), -bounds.y(), ch);
}
glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, bounds.width(), bounds.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());

View file

@ -12,6 +12,7 @@
#ifndef hifi_TextRenderer_h
#define hifi_TextRenderer_h
#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QHash>
@ -41,7 +42,8 @@ public:
enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT };
TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
EffectType effect = NO_EFFECT, int effectThickness = 1);
EffectType effect = NO_EFFECT, int effectThickness = 1,
QColor color = QColor(255, 255, 255));
~TextRenderer();
const QFontMetrics& metrics() const { return _metrics; }
@ -85,6 +87,9 @@ private:
// the list of all texture ids for which we're responsible
QVector<GLuint> _allTextureIDs;
// text color
QColor _color;
};
class Glyph {

View file

@ -19,7 +19,8 @@
TextOverlay::TextOverlay() :
_leftMargin(DEFAULT_MARGIN),
_topMargin(DEFAULT_MARGIN)
_topMargin(DEFAULT_MARGIN),
_fontSize(DEFAULT_FONTSIZE)
{
}
@ -32,7 +33,7 @@ void TextOverlay::render() {
}
const float MAX_COLOR = 255;
glColor4f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, _alpha);
glColor4f(0 / MAX_COLOR, 0 / MAX_COLOR, 0 / MAX_COLOR, _alpha);
glBegin(GL_QUADS);
glVertex2f(_bounds.left(), _bounds.top());
@ -43,8 +44,9 @@ void TextOverlay::render() {
//TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
// EffectType effect = NO_EFFECT, int effectThickness = 1);
TextRenderer textRenderer(SANS_FONT_FAMILY, 11, 50);
TextRenderer textRenderer(SANS_FONT_FAMILY, _fontSize, 50, false, TextRenderer::NO_EFFECT, 1,
QColor(_color.red, _color.green, _color.blue));
const int leftAdjust = -1; // required to make text render relative to left edge of bounds
const int topAdjust = -2; // required to make text render relative to top edge of bounds
int x = _bounds.left() + _leftMargin + leftAdjust;
@ -67,6 +69,13 @@ void TextOverlay::render() {
void TextOverlay::setProperties(const QScriptValue& properties) {
Overlay2D::setProperties(properties);
QScriptValue font = properties.property("font");
if (font.isObject()) {
if (font.property("size").isValid()) {
setFontSize(font.property("size").toInt32());
}
}
QScriptValue text = properties.property("text");
if (text.isValid()) {

View file

@ -29,6 +29,7 @@
#include "Overlay2D.h"
const int DEFAULT_MARGIN = 10;
const int DEFAULT_FONTSIZE = 11;
class TextOverlay : public Overlay2D {
Q_OBJECT
@ -47,6 +48,7 @@ public:
void setText(const QString& text) { _text = text; }
void setLeftMargin(int margin) { _leftMargin = margin; }
void setTopMargin(int margin) { _topMargin = margin; }
void setFontSize(int fontSize) { _fontSize = fontSize; }
virtual void setProperties(const QScriptValue& properties);
@ -55,7 +57,7 @@ private:
QString _text;
int _leftMargin;
int _topMargin;
int _fontSize;
};