From ad41d5af4c2b0379871d3ab295d57c51ae89d843 Mon Sep 17 00:00:00 2001 From: ennewalker Date: Thu, 24 Jul 2008 00:48:02 +0000 Subject: [2021067] Fixing font centering issues. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@6655 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/tilefont.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'crawl-ref/source/tilefont.cc') diff --git a/crawl-ref/source/tilefont.cc b/crawl-ref/source/tilefont.cc index fec21c1d18..f2c3c07e0f 100644 --- a/crawl-ref/source/tilefont.cc +++ b/crawl-ref/source/tilefont.cc @@ -376,6 +376,32 @@ static void _draw_box(int x_pos, int y_pos, float width, float height, } +unsigned int FTFont::string_width(const char *text) +{ + unsigned int base_width = std::max(-m_min_offset, 0); + unsigned int max_width = 0; + + unsigned int width = 0; + unsigned int adjust = 0; + for (const char *itr = text; *itr; itr++) + { + if (*itr == '\n') + { + max_width = std::max(width + adjust, max_width); + width = base_width; + adjust = 0; + } + else + { + width += m_glyphs[*itr].advance; + adjust = std::max(0, m_glyphs[*itr].width - m_glyphs[*itr].advance); + } + } + + max_width = std::max(width + adjust, max_width); + return max_width; +} + void FTFont::render_string(unsigned int px, unsigned int py, const char *text, const coord_def &min_pos, const coord_def &max_pos, @@ -429,7 +455,7 @@ void FTFont::render_string(unsigned int px, unsigned int py, // Find a suitable location on screen const int buffer = 5; // additional buffer size from edges - int wx = max_cols * char_width(); + int wx = string_width(text); int wy = max_rows * char_height(); // text starting location -- cgit v1.2.3-54-g00ecf