From 7e6d616f207fa34acb725f8915f1e98b1e8148e5 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Fri, 7 Nov 2014 08:58:31 -0800 Subject: [PATCH] Add text overlay textWidth() method Is accessible from JavaScript. --- interface/src/ui/overlays/Overlays.cpp | 16 ++++++++++++++++ interface/src/ui/overlays/Overlays.h | 4 ++++ interface/src/ui/overlays/Text3DOverlay.cpp | 11 +++++++++-- interface/src/ui/overlays/Text3DOverlay.h | 2 ++ interface/src/ui/overlays/TextOverlay.cpp | 7 +++++++ interface/src/ui/overlays/TextOverlay.h | 2 ++ 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/interface/src/ui/overlays/Overlays.cpp b/interface/src/ui/overlays/Overlays.cpp index df7a5fbcea..b65fe6d286 100644 --- a/interface/src/ui/overlays/Overlays.cpp +++ b/interface/src/ui/overlays/Overlays.cpp @@ -9,6 +9,7 @@ // #include +#include #include #include @@ -357,3 +358,18 @@ bool Overlays::isLoaded(unsigned int id) { 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(thisOverlay)->textWidth(text); + } + } + if (_overlays3D.contains(id)) { + Overlay* thisOverlay = _overlays3D[id]; + if (typeid(*thisOverlay) == typeid(Text3DOverlay)) { + return static_cast(thisOverlay)->textWidth(text); + } + } + return 0.f; +} diff --git a/interface/src/ui/overlays/Overlays.h b/interface/src/ui/overlays/Overlays.h index 686b998267..dcda9a8ed7 100644 --- a/interface/src/ui/overlays/Overlays.h +++ b/interface/src/ui/overlays/Overlays.h @@ -65,6 +65,10 @@ public slots: /// returns whether the overlay's assets are loaded or not 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: QMap _overlays2D; QMap _overlays3D; diff --git a/interface/src/ui/overlays/Text3DOverlay.cpp b/interface/src/ui/overlays/Text3DOverlay.cpp index d8febbf0eb..3f225bc756 100644 --- a/interface/src/ui/overlays/Text3DOverlay.cpp +++ b/interface/src/ui/overlays/Text3DOverlay.cpp @@ -17,6 +17,8 @@ const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 }; const float DEFAULT_MARGIN = 0.1f; +const int FIXED_FONT_POINT_SIZE = 40; +const float LINE_SCALE_RATIO = 1.2f; Text3DOverlay::Text3DOverlay() : _backgroundColor(DEFAULT_BACKGROUND_COLOR), @@ -87,11 +89,9 @@ void Text3DOverlay::render(RenderArgs* args) { glVertex3f(-halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); 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 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 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)); +} diff --git a/interface/src/ui/overlays/Text3DOverlay.h b/interface/src/ui/overlays/Text3DOverlay.h index 45e311c554..f0369b4206 100644 --- a/interface/src/ui/overlays/Text3DOverlay.h +++ b/interface/src/ui/overlays/Text3DOverlay.h @@ -48,6 +48,8 @@ public: virtual void setProperties(const QScriptValue& properties); + float textWidth(const QString& text); // Meters + private: void enableClipPlane(GLenum plane, float x, float y, float z, float w); diff --git a/interface/src/ui/overlays/TextOverlay.cpp b/interface/src/ui/overlays/TextOverlay.cpp index 530b30a856..455884c13e 100644 --- a/interface/src/ui/overlays/TextOverlay.cpp +++ b/interface/src/ui/overlays/TextOverlay.cpp @@ -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)); +} diff --git a/interface/src/ui/overlays/TextOverlay.h b/interface/src/ui/overlays/TextOverlay.h index f7ff83e542..fae9e7c582 100644 --- a/interface/src/ui/overlays/TextOverlay.h +++ b/interface/src/ui/overlays/TextOverlay.h @@ -53,6 +53,8 @@ public: virtual void setProperties(const QScriptValue& properties); + float textWidth(const QString& text); // Pixels + private: QString _text;