This commit is contained in:
Andrzej Kapolka 2014-12-11 14:44:30 -08:00
commit 77b19241d7
13 changed files with 67 additions and 27 deletions

View file

@ -159,8 +159,8 @@ var toolBar = (function () {
visible: false visible: false
}); });
menuItemWidth = Math.max(Overlays.textWidth(loadURLMenuItem, "Model URL"), menuItemWidth = Math.max(Overlays.textSize(loadURLMenuItem, "Model URL").width,
Overlays.textWidth(loadFileMenuItem, "Model File")) + 20; Overlays.textSize(loadFileMenuItem, "Model File").width) + 20;
Overlays.editOverlay(loadURLMenuItem, { width: menuItemWidth }); Overlays.editOverlay(loadURLMenuItem, { width: menuItemWidth });
Overlays.editOverlay(loadFileMenuItem, { width: menuItemWidth }); Overlays.editOverlay(loadFileMenuItem, { width: menuItemWidth });

View file

@ -1191,8 +1191,8 @@ var toolBar = (function () {
visible: false visible: false
}); });
menuItemWidth = Math.max(Overlays.textWidth(loadURLMenuItem, "Model URL"), menuItemWidth = Math.max(Overlays.textSize(loadURLMenuItem, "Model URL").width,
Overlays.textWidth(loadFileMenuItem, "Model File")) + 20; Overlays.textSize(loadFileMenuItem, "Model File").width) + 20;
Overlays.editOverlay(loadURLMenuItem, { width: menuItemWidth }); Overlays.editOverlay(loadURLMenuItem, { width: menuItemWidth });
Overlays.editOverlay(loadFileMenuItem, { width: menuItemWidth }); Overlays.editOverlay(loadFileMenuItem, { width: menuItemWidth });

View file

@ -323,7 +323,7 @@ function handleLookAt(pickRay) {
} else { } else {
currentTestLine = allWords[currentTestWord]; currentTestLine = allWords[currentTestWord];
} }
var lineLength = Overlays.textWidth(descriptionText, currentTestLine); var lineLength = Overlays.textSize(descriptionText, currentTestLine).width;
if (lineLength < textWidth || wordsOnLine == 0) { if (lineLength < textWidth || wordsOnLine == 0) {
wordsFormated++; wordsFormated++;
currentTestWord++; currentTestWord++;

View file

@ -80,7 +80,7 @@ function updateTextOverlay() {
var textLines = textText.split("\n"); var textLines = textText.split("\n");
var maxLineWidth = 0; var maxLineWidth = 0;
for (textLine in textLines) { for (textLine in textLines) {
var lineWidth = Overlays.textWidth(text, textLines[textLine]); var lineWidth = Overlays.textSize(text, textLines[textLine]).width;
if (lineWidth > maxLineWidth) { if (lineWidth > maxLineWidth) {
maxLineWidth = lineWidth; maxLineWidth = lineWidth;
} }
@ -92,7 +92,7 @@ function updateTextOverlay() {
Overlays.editOverlay(text, {text: textText, font: {size: textFontSize}, topMargin: topMargin}); Overlays.editOverlay(text, {text: textText, font: {size: textFontSize}, topMargin: topMargin});
var maxLineWidth = 0; var maxLineWidth = 0;
for (textLine in textLines) { for (textLine in textLines) {
var lineWidth = Overlays.textWidth(text, textLines[textLine]); var lineWidth = Overlays.textSize(text, textLines[textLine]).width;
if (lineWidth > maxLineWidth) { if (lineWidth > maxLineWidth) {
maxLineWidth = lineWidth; maxLineWidth = lineWidth;
} }
@ -122,18 +122,18 @@ keyboard.onKeyRelease = function(event) {
var textLines = textText.split("\n"); var textLines = textText.split("\n");
var maxLineWidth = 0; var maxLineWidth = 0;
for (textLine in textLines) { for (textLine in textLines) {
var lineWidth = Overlays.textWidth(textSizeMeasureOverlay, textLines[textLine]); var lineWidth = Overlays.textSize(textSizeMeasureOverlay, textLines[textLine]).width;
if (lineWidth > maxLineWidth) { if (lineWidth > maxLineWidth) {
maxLineWidth = lineWidth; maxLineWidth = lineWidth;
} }
} }
var usernameLine = "--" + GlobalServices.myUsername; var usernameLine = "--" + GlobalServices.myUsername;
var usernameWidth = Overlays.textWidth(textSizeMeasureOverlay, usernameLine); var usernameWidth = Overlays.textSize(textSizeMeasureOverlay, usernameLine).width;
if (maxLineWidth < usernameWidth) { if (maxLineWidth < usernameWidth) {
maxLineWidth = usernameWidth; maxLineWidth = usernameWidth;
} else { } else {
var spaceableWidth = maxLineWidth - usernameWidth; var spaceableWidth = maxLineWidth - usernameWidth;
var spaceWidth = Overlays.textWidth(textSizeMeasureOverlay, " "); var spaceWidth = Overlays.textSize(textSizeMeasureOverlay, " ").width;
var numberOfSpaces = Math.floor(spaceableWidth / spaceWidth); var numberOfSpaces = Math.floor(spaceableWidth / spaceWidth);
for (var i = 0; i < numberOfSpaces; i++) { for (var i = 0; i < numberOfSpaces; i++) {
usernameLine = " " + usernameLine; usernameLine = " " + usernameLine;

View file

@ -63,7 +63,7 @@ void RenderableTextEntityItem::render(RenderArgs* args) {
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
// Same font properties as textWidth() // Same font properties as textSize()
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO; float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO;

View file

@ -432,19 +432,19 @@ bool Overlays::isLoaded(unsigned int id) {
return thisOverlay->isLoaded(); return thisOverlay->isLoaded();
} }
float Overlays::textWidth(unsigned int id, const QString& text) const { QSizeF Overlays::textSize(unsigned int id, const QString& text) const {
Overlay* thisOverlay = _overlays2D[id]; Overlay* thisOverlay = _overlays2D[id];
if (thisOverlay) { if (thisOverlay) {
if (typeid(*thisOverlay) == typeid(TextOverlay)) { if (typeid(*thisOverlay) == typeid(TextOverlay)) {
return static_cast<TextOverlay*>(thisOverlay)->textWidth(text); return static_cast<TextOverlay*>(thisOverlay)->textSize(text);
} }
} else { } else {
thisOverlay = _overlays3D[id]; thisOverlay = _overlays3D[id];
if (thisOverlay) { if (thisOverlay) {
if (typeid(*thisOverlay) == typeid(Text3DOverlay)) { if (typeid(*thisOverlay) == typeid(Text3DOverlay)) {
return static_cast<Text3DOverlay*>(thisOverlay)->textWidth(text); return static_cast<Text3DOverlay*>(thisOverlay)->textSize(text);
} }
} }
} }
return 0.0f; return QSizeF(0.0f, 0.0f);
} }

View file

@ -85,9 +85,9 @@ 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 /// returns the size 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 /// overlay; in meters if it is a 3D text overlay
float textWidth(unsigned int id, const QString& text) const; QSizeF textSize(unsigned int id, const QString& text) const;
private: private:
QMap<unsigned int, Overlay*> _overlays2D; QMap<unsigned int, Overlay*> _overlays2D;

View file

@ -108,7 +108,7 @@ void Text3DOverlay::render(RenderArgs* args) {
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
// Same font properties as textWidth() // Same font properties as textSize()
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE);
float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO; float maxHeight = (float)textRenderer->calculateHeight("Xy") * LINE_SCALE_RATIO;
@ -236,11 +236,23 @@ Text3DOverlay* Text3DOverlay::createClone() const {
return new Text3DOverlay(this);; return new Text3DOverlay(this);;
} }
float Text3DOverlay::textWidth(const QString& text) const { QSizeF Text3DOverlay::textSize(const QString& text) const {
QFont font(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); // Same font properties as render() QFont font(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); // Same font properties as render()
QFontMetrics fontMetrics(font); QFontMetrics fontMetrics(font);
float scaleFactor = _lineHeight * LINE_SCALE_RATIO / (float)FIXED_FONT_POINT_SIZE; const float TEXT_SCALE_ADJUST = 1.02f; // Experimentally detemined for the specified font
return scaleFactor * (float)fontMetrics.width(qPrintable(text)); const int TEXT_HEIGHT_ADJUST = -6;
float scaleFactor = _lineHeight * TEXT_SCALE_ADJUST * LINE_SCALE_RATIO / (float)FIXED_FONT_POINT_SIZE;
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);
} }

View file

@ -51,7 +51,7 @@ public:
virtual void setProperties(const QScriptValue& properties); virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property); virtual QScriptValue getProperty(const QString& property);
float textWidth(const QString& text) const; // Meters QSizeF textSize(const QString& test) const; // Meters
virtual Text3DOverlay* createClone() const; virtual Text3DOverlay* createClone() const;

View file

@ -77,7 +77,7 @@ void TextOverlay::render(RenderArgs* args) {
glVertex2f(_bounds.left(), _bounds.bottom()); glVertex2f(_bounds.left(), _bounds.bottom());
glEnd(); glEnd();
// Same font properties as textWidth() // Same font properties as textSize()
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT);
const int leftAdjust = -1; // required to make text render relative to left edge of bounds const int leftAdjust = -1; // required to make text render relative to left edge of bounds
@ -169,8 +169,20 @@ QScriptValue TextOverlay::getProperty(const QString& property) {
return Overlay2D::getProperty(property); return Overlay2D::getProperty(property);
} }
float TextOverlay::textWidth(const QString& text) const { QSizeF TextOverlay::textSize(const QString& text) const {
QFont font(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); // Same font properties as render() QFont font(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); // Same font properties as render()
QFontMetrics fontMetrics(font); QFontMetrics fontMetrics(font);
return fontMetrics.width(qPrintable(text)); 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);
} }

View file

@ -59,7 +59,7 @@ public:
virtual TextOverlay* createClone() const; virtual TextOverlay* createClone() const;
virtual QScriptValue getProperty(const QString& property); virtual QScriptValue getProperty(const QString& property);
float textWidth(const QString& text) const; // Pixels QSizeF textSize(const QString& test) const; // Pixels
private: private:
QString _text; QString _text;

View file

@ -39,6 +39,7 @@ void registerMetaTypes(QScriptEngine* engine) {
qScriptRegisterMetaType(engine, pickRayToScriptValue, pickRayFromScriptValue); qScriptRegisterMetaType(engine, pickRayToScriptValue, pickRayFromScriptValue);
qScriptRegisterMetaType(engine, collisionToScriptValue, collisionFromScriptValue); qScriptRegisterMetaType(engine, collisionToScriptValue, collisionFromScriptValue);
qScriptRegisterMetaType(engine, quuidToScriptValue, quuidFromScriptValue); qScriptRegisterMetaType(engine, quuidToScriptValue, quuidFromScriptValue);
qScriptRegisterMetaType(engine, qSizeFToScriptValue, qSizeFFromScriptValue);
} }
QScriptValue vec4toScriptValue(QScriptEngine* engine, const glm::vec4& vec4) { QScriptValue vec4toScriptValue(QScriptEngine* engine, const glm::vec4& vec4) {
@ -206,3 +207,14 @@ void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid) {
uuid = fromString; uuid = fromString;
} }
QScriptValue qSizeFToScriptValue(QScriptEngine* engine, const QSizeF& qSizeF) {
QScriptValue obj = engine->newObject();
obj.setProperty("width", qSizeF.width());
obj.setProperty("height", qSizeF.height());
return obj;
}
void qSizeFFromScriptValue(const QScriptValue& object, QSizeF& qSizeF) {
qSizeF.setWidth(object.property("width").toVariant().toFloat());
qSizeF.setHeight(object.property("height").toVariant().toFloat());
}

View file

@ -81,4 +81,8 @@ void collisionFromScriptValue(const QScriptValue &object, Collision& collision);
QScriptValue quuidToScriptValue(QScriptEngine* engine, const QUuid& uuid); QScriptValue quuidToScriptValue(QScriptEngine* engine, const QUuid& uuid);
void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid); void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid);
//Q_DECLARE_METATYPE(QSizeF) // Don't need to to this becase it's arleady a meta type
QScriptValue qSizeFToScriptValue(QScriptEngine* engine, const QSizeF& qSizeF);
void qSizeFFromScriptValue(const QScriptValue& object, QSizeF& qSizeF);
#endif // hifi_RegisteredMetaTypes_h #endif // hifi_RegisteredMetaTypes_h