From df44f9344616e9d6e7009fcfedb6620087c1ff22 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Wed, 14 Dec 2022 15:57:48 +0100 Subject: Add lineHeightRatio to let text decide the line spacing --- src/graphics/text.cpp | 10 ++++++---- src/graphics/text.h | 8 +++++--- src/ui.cpp | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/graphics/text.cpp b/src/graphics/text.cpp index 7b135d6..51b71ae 100644 --- a/src/graphics/text.cpp +++ b/src/graphics/text.cpp @@ -20,24 +20,26 @@ void Text::arrange(TextureFont &font){ if(text_[i] == '\n') continue; } + // Position the character in the text vertices_[4*i+0].position = sf::Vector2f( col*glyph.advance + glyph.bounds.left, - (glyph.bounds.top) + row * glyph.bounds.height + (glyph.bounds.top) + row * font.getLineSpacing() * lineHeightRatio_ ); vertices_[4*i+1].position = sf::Vector2f( col*glyph.advance + glyph.bounds.left+glyph.bounds.width, - (glyph.bounds.top) + row * glyph.bounds.height + (glyph.bounds.top) + row * font.getLineSpacing() * lineHeightRatio_ ); vertices_[4*i+2].position = sf::Vector2f( col*glyph.advance + glyph.bounds.left+glyph.bounds.width, - (glyph.bounds.top+glyph.bounds.height) + row * glyph.bounds.height + (glyph.bounds.top+glyph.bounds.height) + row * font.getLineSpacing() * lineHeightRatio_ ); vertices_[4*i+3].position = sf::Vector2f( col*glyph.advance + glyph.bounds.left, - (glyph.bounds.top+glyph.bounds.height) + row * glyph.bounds.height + (glyph.bounds.top+glyph.bounds.height) + row * font.getLineSpacing() * lineHeightRatio_ ); col++; + // Choose the texture rectangle for this character vertices_[4*i+0].texCoords = sf::Vector2f( glyph.textureRect.left, glyph.textureRect.top diff --git a/src/graphics/text.h b/src/graphics/text.h index f60980c..a390e8a 100644 --- a/src/graphics/text.h +++ b/src/graphics/text.h @@ -10,13 +10,14 @@ namespace Graphics { Font &font_; const std::string text_; sf::VertexArray vertices_; + float lineHeightRatio_; unsigned int size_, maxWidth_, maxHeight_; void arrange(Font &font); // Specialize for sf::Font and TextureFont public: - Text(Font &font, const std::string &text, unsigned int maxWidth=0, - unsigned int maxHeight=0); + Text(Font &font, const std::string &text, float lineHeightRatio=1., + unsigned int maxWidth=0, unsigned int maxHeight=0); void setFont(Font &font); void setText(const std::string &text); @@ -26,11 +27,12 @@ namespace Graphics { }; template - Text::Text(Font &font, const std::string &text, + Text::Text(Font &font, const std::string &text, float lineHeightRatio, unsigned int maxWidth, unsigned int maxHeight) : font_(font) , text_(text) , vertices_(sf::Quads, text.size()*4) + , lineHeightRatio_(lineHeightRatio) , size_(0) , maxWidth_(maxWidth) , maxHeight_(maxHeight) diff --git a/src/ui.cpp b/src/ui.cpp index 722eb1b..02b50f2 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -185,7 +185,7 @@ bool UI::radioButton(sf::RenderWindow *win, Graphics::TextureFont font, } win->draw(radio); - Graphics::Text textGraph {font, text, size.x - 30, size.y}; + Graphics::Text textGraph {font, text, 1.4, size.x - 30, size.y}; textGraph.setPosition(sf::Vector2f(pos) + sf::Vector2f{30, size.y/2}); win->draw(textGraph); -- cgit v1.2.3