Add text overlay textWidth() method

Is accessible from JavaScript.
This commit is contained in:
David Rowe 2014-11-07 08:58:31 -08:00
parent 02f6f6769b
commit 7e6d616f20
6 changed files with 40 additions and 2 deletions

View file

@ -9,6 +9,7 @@
// //
#include <limits> #include <limits>
#include <typeinfo>
#include <Application.h> #include <Application.h>
#include <Menu.h> #include <Menu.h>
@ -357,3 +358,18 @@ bool Overlays::isLoaded(unsigned int id) {
return overlay->isLoaded(); return overlay->isLoaded();
} }
float Overlays::textWidth(unsigned int id, const QString& text) {
if (_overlays2D.contains(id)) {
Overlay* thisOverlay = _overlays2D[id];
if (typeid(*thisOverlay) == typeid(TextOverlay)) {
return static_cast<TextOverlay*>(thisOverlay)->textWidth(text);
}
}
if (_overlays3D.contains(id)) {
Overlay* thisOverlay = _overlays3D[id];
if (typeid(*thisOverlay) == typeid(Text3DOverlay)) {
return static_cast<Text3DOverlay*>(thisOverlay)->textWidth(text);
}
}
return 0.f;
}

View file

@ -65,6 +65,10 @@ public slots:
/// returns whether the overlay's assets are loaded or not /// returns whether the overlay's assets are loaded or not
bool isLoaded(unsigned int id); bool isLoaded(unsigned int id);
/// returns the width of the given text in the specified overlay if it is a text overlay: in pixels if it is a 2D text
/// overlay; in meters if it is a 3D text overlay
float textWidth(unsigned int id, const QString& text);
private: private:
QMap<unsigned int, Overlay*> _overlays2D; QMap<unsigned int, Overlay*> _overlays2D;
QMap<unsigned int, Overlay*> _overlays3D; QMap<unsigned int, Overlay*> _overlays3D;

View file

@ -17,6 +17,8 @@
const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 }; const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 };
const float DEFAULT_MARGIN = 0.1f; const float DEFAULT_MARGIN = 0.1f;
const int FIXED_FONT_POINT_SIZE = 40;
const float LINE_SCALE_RATIO = 1.2f;
Text3DOverlay::Text3DOverlay() : Text3DOverlay::Text3DOverlay() :
_backgroundColor(DEFAULT_BACKGROUND_COLOR), _backgroundColor(DEFAULT_BACKGROUND_COLOR),
@ -87,11 +89,9 @@ void Text3DOverlay::render(RenderArgs* args) {
glVertex3f(-halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); glVertex3f(-halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND);
glEnd(); glEnd();
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 int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 40.0f; // this is a ratio determined through experimentation
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
float LINE_SCALE_RATIO = 1.2f;
float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO; float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO;
float scaleFactor = (maxHeight / FIXED_FONT_SCALING_RATIO) * _lineHeight; float scaleFactor = (maxHeight / FIXED_FONT_SCALING_RATIO) * _lineHeight;
@ -179,4 +179,11 @@ void Text3DOverlay::setProperties(const QScriptValue& properties) {
} }
float Text3DOverlay::textWidth(const QString& text) {
const int TEXT3DOVERLAY_TEXTWIDTH_RENDERER = 2; // Separate to that used for drawing text so that doesn't interfere.
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE, -1, false,
TextRenderer::NO_EFFECT, 1, QColor(255, 255, 255), TEXT3DOVERLAY_TEXTWIDTH_RENDERER);
float scaleFactor = _lineHeight * LINE_SCALE_RATIO / (float)textRenderer->calculateHeight("X");
return scaleFactor * (float)textRenderer->computeWidth(qPrintable(text));
}

View file

@ -48,6 +48,8 @@ public:
virtual void setProperties(const QScriptValue& properties); virtual void setProperties(const QScriptValue& properties);
float textWidth(const QString& text); // Meters
private: private:
void enableClipPlane(GLenum plane, float x, float y, float z, float w); void enableClipPlane(GLenum plane, float x, float y, float z, float w);

View file

@ -126,3 +126,10 @@ void TextOverlay::setProperties(const QScriptValue& properties) {
} }
float TextOverlay::textWidth(const QString& text) {
const int TEXTOVERLAY_GETTEXTWIDTH_TEXTRENDERER = 1; // Separate to that used for drawing text so that doesn't interfere.
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, -1, FALSE, TextRenderer::NO_EFFECT, 1,
QColor(255, 255, 255), TEXTOVERLAY_GETTEXTWIDTH_TEXTRENDERER);
return textRenderer->computeWidth(qPrintable(text));
}

View file

@ -53,6 +53,8 @@ public:
virtual void setProperties(const QScriptValue& properties); virtual void setProperties(const QScriptValue& properties);
float textWidth(const QString& text); // Pixels
private: private:
QString _text; QString _text;