diff --git a/scwx-qt/source/scwx/qt/gl/text_shader.cpp b/scwx-qt/source/scwx/qt/gl/text_shader.cpp index 1ef02af1..e8e83bb8 100644 --- a/scwx-qt/source/scwx/qt/gl/text_shader.cpp +++ b/scwx-qt/source/scwx/qt/gl/text_shader.cpp @@ -55,11 +55,12 @@ bool TextShader::Initialize() void TextShader::RenderText(const std::string& text, float x, float y, - float scale, + float pointSize, const glm::mat4& projection, const boost::gil::rgba8_pixel_t& color, std::shared_ptr font, - GLuint textureId) + GLuint textureId, + TextAlign align) { OpenGLFunctions& gl = p->gl_; @@ -74,9 +75,26 @@ void TextShader::RenderText(const std::string& text, gl.glActiveTexture(GL_TEXTURE0); gl.glBindTexture(GL_TEXTURE_2D, textureId); + switch (align) + { + case TextAlign::Left: + // Do nothing + break; + + case TextAlign::Center: + // X position is the center of text, subtract half length + x -= font->TextLength(text, pointSize) * 0.5f; + break; + + case TextAlign::Right: + // X position is the end of text, subtract length + x -= font->TextLength(text, pointSize); + break; + } + std::shared_ptr buffer = std::make_shared(); - font->BufferText(buffer, text, x, y, scale, color); + font->BufferText(buffer, text, x, y, pointSize, color); buffer->Render(gl); } diff --git a/scwx-qt/source/scwx/qt/gl/text_shader.hpp b/scwx-qt/source/scwx/qt/gl/text_shader.hpp index 19655bbe..8f1926a8 100644 --- a/scwx-qt/source/scwx/qt/gl/text_shader.hpp +++ b/scwx-qt/source/scwx/qt/gl/text_shader.hpp @@ -15,6 +15,13 @@ namespace qt namespace gl { +enum class TextAlign +{ + Left, + Center, + Right +}; + class TextShaderImpl; class TextShader : public ShaderProgram @@ -37,7 +44,8 @@ public: const glm::mat4& projection, const boost::gil::rgba8_pixel_t& color, std::shared_ptr font, - GLuint textureId); + GLuint textureId, + TextAlign align = TextAlign::Left); void SetProjection(const glm::mat4& projection); private: diff --git a/scwx-qt/source/scwx/qt/util/font.cpp b/scwx-qt/source/scwx/qt/util/font.cpp index 3a478a05..f9c24f1f 100644 --- a/scwx-qt/source/scwx/qt/util/font.cpp +++ b/scwx-qt/source/scwx/qt/util/font.cpp @@ -62,6 +62,9 @@ static const std::string CODEPOINTS = static const std::string logPrefix_ = "[scwx::qt::util::font] "; +static constexpr float BASE_POINT_SIZE = 72.0f; +static constexpr float POINT_SCALE = 1.0f / BASE_POINT_SIZE; + static std::unordered_map> fontMap_; class FontImpl @@ -96,11 +99,13 @@ float Font::BufferText(std::shared_ptr buffer, const std::string& text, float x, float y, - float scale, + float pointSize, boost::gil::rgba8_pixel_t color) const { static constexpr float colorScale = 1.0f / 255.0f; + const float scale = pointSize * POINT_SCALE; + float r = color[0] * colorScale; float g = color[1] * colorScale; float b = color[2] * colorScale; @@ -154,8 +159,10 @@ float Font::Kerning(char c1, char c2) const return 0.0f; } -float Font::TextLength(const std::string& text, float scale) const +float Font::TextLength(const std::string& text, float pointSize) const { + const float scale = pointSize * POINT_SCALE; + float x = 0.0f; for (size_t i = 0; i < text.length(); ++i) @@ -232,7 +239,7 @@ std::shared_ptr Font::Create(const std::string& resource) font->p->atlas_ = ftgl::texture_atlas_new(512, 512, 1); ftgl::texture_font_t* textureFont = ftgl::texture_font_new_from_memory( - font->p->atlas_, 72, fontData.constData(), fontData.size()); + font->p->atlas_, BASE_POINT_SIZE, fontData.constData(), fontData.size()); textureFont->rendermode = ftgl::RENDER_SIGNED_DISTANCE_FIELD; diff --git a/scwx-qt/source/scwx/qt/util/font.hpp b/scwx-qt/source/scwx/qt/util/font.hpp index 0617b628..789135c7 100644 --- a/scwx-qt/source/scwx/qt/util/font.hpp +++ b/scwx-qt/source/scwx/qt/util/font.hpp @@ -33,10 +33,10 @@ public: const std::string& text, float x, float y, - float scale, + float pointSize, boost::gil::rgba8_pixel_t color) const; float Kerning(char c1, char c2) const; - float TextLength(const std::string& text, float scale) const; + float TextLength(const std::string& text, float pointSize) const; GLuint GenerateTexture(OpenGLFunctions& gl);