diff --git a/src/font/sdl_ttf.cpp b/src/font/sdl_ttf.cpp index 9fd0e7459c06..c7eddd432622 100644 --- a/src/font/sdl_ttf.cpp +++ b/src/font/sdl_ttf.cpp @@ -352,15 +352,6 @@ SDL_Rect draw_text_line(surface& gui_surface, const SDL_Rect& area, int size, return dest; } -int get_max_height(int size) -{ - // Only returns the maximal size of the first font - const auto font = sdl_ttf::get_font(font_id(0, size)); - if(font == nullptr) - return 0; - return TTF_FontHeight(font.get()); -} - int line_width(const std::string& line, int font_size, int style) { return line_size(line,font_size,style).w; diff --git a/src/font/sdl_ttf.hpp b/src/font/sdl_ttf.hpp index 1c1b72edea5f..5d15c04220a8 100644 --- a/src/font/sdl_ttf.hpp +++ b/src/font/sdl_ttf.hpp @@ -35,9 +35,6 @@ SDL_Rect draw_text_line(surface& gui_surface, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips, int style); -// Returns the maximum height of a font, in pixels -int get_max_height(int size); - /** * Determine the width of a line of text given a certain font size. * The font type used is the default wesnoth font type. diff --git a/src/font/text.cpp b/src/font/text.cpp index d2f9801e6f4e..0146be429259 100644 --- a/src/font/text.cpp +++ b/src/font/text.cpp @@ -477,6 +477,26 @@ pango_text& pango_text::set_add_outline(bool do_add) return *this; } +int pango_text::get_max_glyph_height() const +{ + p_font font{ get_font_families(font_class_), font_size_, font_style_ }; + + PangoFont* f = pango_font_map_load_font( + pango_cairo_font_map_get_default(), + context_.get(), + font.get()); + + PangoFontMetrics* m = pango_font_get_metrics(f, nullptr); + + auto ascent = pango_font_metrics_get_ascent(m); + auto descent = pango_font_metrics_get_descent(m); + + pango_font_metrics_unref(m); + g_object_unref(f); + + return ceil(pango_units_to_double(ascent + descent)); +} + void pango_text::recalculate(const bool force) const { if(calculation_dirty_ || force) { @@ -900,4 +920,14 @@ pango_text& get_text_renderer() return text_renderer; } +int get_max_height(unsigned size, font::family_class fclass, pango_text::FONT_STYLE style) +{ + // Reset metrics to defaults + return get_text_renderer() + .set_family_class(fclass) + .set_font_style(style) + .set_font_size(size) + .get_max_glyph_height(); +} + } // namespace font diff --git a/src/font/text.hpp b/src/font/text.hpp index 64e202fa05e0..f70d1a4d1bea 100644 --- a/src/font/text.hpp +++ b/src/font/text.hpp @@ -143,6 +143,15 @@ class pango_text /***** ***** ***** ***** Query details ***** ***** ***** *****/ + /** + * Returns the maximum glyph height of a font, in pixels. + * + * @returns The height of the tallest possible glyph for the selected + * font. More specifically, the result is the sum of the maximum + * ascent and descent lengths. + */ + int get_max_glyph_height() const; + /** * Gets the location for the cursor. * @@ -431,4 +440,17 @@ class pango_text */ pango_text& get_text_renderer(); +/** + * Returns the maximum glyph height of a font, in pixels. + * + * @param size Desired font size in pixels. + * @param fclass Font family to use for measurement. + * @param style Font style to select the correct variant for measurement. + * + * @returns The height of the tallest possible glyph for the selected + * font. More specifically, the result is the sum of the maximum + * ascent and descent lengths. + */ +int get_max_height(unsigned size, font::family_class fclass = font::FONT_SANS_SERIF, pango_text::FONT_STYLE style = pango_text::STYLE_NORMAL); + } // namespace font