From 62bfab962d8c05c3bd9a1817fc07ce030e937e51 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Tue, 4 Jun 2024 20:30:38 -0700 Subject: [PATCH] fix _spaceWidth, update docs to note only png supported, possible fix for text scrambling --- .../entities/src/EntityItemProperties.cpp | 2 +- libraries/render-utils/src/text/Font.cpp | 35 +++++++++---------- libraries/render-utils/src/text/Font.h | 1 + .../create/assets/data/createAppTooltips.json | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index f3f5479b15..a679caba7f 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -1328,7 +1328,7 @@ EntityPropertyFlags EntityItemProperties::getChangedProperties() const { * @property {boolean} unlit=false - true if the entity is unaffected by lighting, false if it is lit * by the key light and local lights. * @property {string} font="" - The font to render the text with. It can be one of the following: "Courier", - * "Inconsolata", "Roboto", "Timeless", or a path to an MTSDF .arfont file generated + * "Inconsolata", "Roboto", "Timeless", or a path to a PNG MTSDF .arfont file generated * by the msdf-atlas-gen tool (https://github.com/Chlumsky/msdf-atlas-gen). * @property {Entities.TextEffect} textEffect="none" - The effect that is applied to the text. * @property {Color} textEffectColor=255,255,255 - The color of the effect. diff --git a/libraries/render-utils/src/text/Font.cpp b/libraries/render-utils/src/text/Font.cpp index f77037b523..1a92ccf8ec 100644 --- a/libraries/render-utils/src/text/Font.cpp +++ b/libraries/render-utils/src/text/Font.cpp @@ -131,7 +131,7 @@ void Font::read(QIODevice& in) { _distanceRange = glm::vec2(arteryFont.variants[0].metrics.distanceRange); _fontSize = arteryFont.variants[0].metrics.ascender + fabs(arteryFont.variants[0].metrics.descender); _leading = arteryFont.variants[0].metrics.lineHeight; - _spaceWidth = 0.5f * arteryFont.variants[0].metrics.emSize; + _spaceWidth = 0.5f * arteryFont.variants[0].metrics.emSize; // We use half the emSize as a first guess for _spaceWidth if (arteryFont.variants[0].glyphs.length() == 0) { qDebug() << "Font" << _family << "has 0 glyphs."; @@ -151,6 +151,11 @@ void Font::read(QIODevice& in) { glyph.size = glm::vec2(g.planeBounds.r, g.planeBounds.t) - glyph.offset; glyph.d = g.advance.h; glyphs.push_back(glyph); + + // If we find the space character, we save its size in _spaceWidth for later + if (glyph.c == ' ') { + _spaceWidth = glyph.d; + } } if (arteryFont.images.length() == 0) { @@ -163,8 +168,8 @@ void Font::read(QIODevice& in) { return; } - if (arteryFont.images[0].encoding != artery_font::ImageEncoding::IMAGE_PNG && arteryFont.images[0].encoding != artery_font::ImageEncoding::IMAGE_BMP) { - qDebug() << "Font" << _family << "has the wrong encoding. Expected BMP (4) or PNG (8), got" << arteryFont.images[0].encoding; + if (arteryFont.images[0].encoding != artery_font::ImageEncoding::IMAGE_PNG) { + qDebug() << "Font" << _family << "has the wrong encoding. Expected PNG (8), got" << arteryFont.images[0].encoding; return; } @@ -180,20 +185,8 @@ void Font::read(QIODevice& in) { // read image data QImage image; - QString format; - switch (arteryFont.images[0].encoding) { - case artery_font::ImageEncoding::IMAGE_PNG: - format = "PNG"; - break; - case artery_font::ImageEncoding::IMAGE_BMP: - format = "BMP"; - break; - default: - format = "PNG"; - break; - } - if (!image.loadFromData((const unsigned char*)arteryFont.images[0].data, arteryFont.images[0].data.length(), format.toStdString().c_str())) { - qDebug() << "Failed to read" << format << "image for font" << _family; + if (!image.loadFromData((const unsigned char*)arteryFont.images[0].data, arteryFont.images[0].data.length(), "PNG")) { + qDebug() << "Failed to read image for font" << _family; return; } @@ -229,6 +222,7 @@ void Font::read(QIODevice& in) { _texture->setImportant(true); _loaded = true; + _needsParamsUpdate = true; } static QHash LOADED_FONTS; @@ -541,8 +535,9 @@ void Font::drawString(gpu::Batch& batch, Font::DrawInfo& drawInfo, const QString setupGPU(); - if (!drawInfo.paramsBuffer || boundsChanged || drawInfo.params.color != color || drawInfo.params.effectColor != effectColor || - drawInfo.params.effectThickness != effectThickness || drawInfo.params.effect != textEffect) { + if (!drawInfo.paramsBuffer || boundsChanged || _needsParamsUpdate || drawInfo.params.color != color || + drawInfo.params.effectColor != effectColor || drawInfo.params.effectThickness != effectThickness || + drawInfo.params.effect != textEffect) { drawInfo.params.color = color; drawInfo.params.effectColor = effectColor; drawInfo.params.effectThickness = effectThickness; @@ -560,6 +555,8 @@ void Font::drawString(gpu::Batch& batch, Font::DrawInfo& drawInfo, const QString drawInfo.paramsBuffer = std::make_shared(sizeof(DrawParams), nullptr); } drawInfo.paramsBuffer->setSubData(0, sizeof(DrawParams), (const gpu::Byte*)&gpuDrawParams); + + _needsParamsUpdate = false; } batch.setPipeline(_pipelines[std::make_tuple(color.a < 1.0f, unlit, forward)]); diff --git a/libraries/render-utils/src/text/Font.h b/libraries/render-utils/src/text/Font.h index b91de95a5f..523d58e0fd 100644 --- a/libraries/render-utils/src/text/Font.h +++ b/libraries/render-utils/src/text/Font.h @@ -102,6 +102,7 @@ private: TextAlignment _alignment { TextAlignment::LEFT }; bool _loaded { false }; + bool _needsParamsUpdate { false }; gpu::TexturePointer _texture; gpu::BufferStreamPointer _stream; diff --git a/scripts/system/create/assets/data/createAppTooltips.json b/scripts/system/create/assets/data/createAppTooltips.json index 3fb431e0c5..eaefd08a0d 100644 --- a/scripts/system/create/assets/data/createAppTooltips.json +++ b/scripts/system/create/assets/data/createAppTooltips.json @@ -27,7 +27,7 @@ "tooltip": "The height of each line of text. This determines the size of the text." }, "font": { - "tooltip": "The font to render the text. Supported values: \"Courier\", \"Inconsolata\", \"Roboto\", \"Timeless\", or a URL to a MTSDF .arfont file." + "tooltip": "The font to render the text. Supported values: \"Courier\", \"Inconsolata\", \"Roboto\", \"Timeless\", or a URL to a PNG MTSDF .arfont file." }, "textEffect": { "tooltip": "The effect that is applied to the text."