The Battle for Wesnoth  1.17.0-dev
sdl_ttf_compat.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2021
3  by Iris Morelle <shadowm@wesnoth.org>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 /**
19  * @file
20  * Transitional API for porting SDL_ttf-based code to Pango. Do NOT use in new code!
21  *
22  * @note GUI1 markup is not supported by this transitional API for cost-benefit reasons.
23  * Not only does implementing it require a lot more work to go over text line by line,
24  * it also had major design flaws -- namely, only applying to whole lines with variable
25  * spans that would be decided by the layout algorithm depending on available space,
26  * rather than on a physical line basis (markup start till EOL) or fixed span basis (e.g.
27  * the special markup used by the Help browser, or Pango markup).
28  */
29 
30 #include "font/text.hpp"
31 
32 class CVideo;
33 
34 namespace font {
35 
36 /**
37  * Returns a SDL surface containing the rendered text.
38  */
39 surface pango_render_text(const std::string& text, int size, const color_t& color, font::pango_text::FONT_STYLE style = font::pango_text::STYLE_NORMAL, bool use_markup = false, int max_width = -1);
40 
41 /**
42  * Determine the width and height of a line of text given a certain font size.
43  */
44 std::pair<int, int> pango_line_size(const std::string& line, int font_size, font::pango_text::FONT_STYLE font_style = font::pango_text::STYLE_NORMAL);
45 
46 /**
47  * Determine the width of a line of text given a certain font size.
48  */
49 inline int pango_line_width(const std::string& line, int font_size, font::pango_text::FONT_STYLE font_style = font::pango_text::STYLE_NORMAL)
50 {
51  return pango_line_size(line, font_size, font_style).first;
52 }
53 
54 /**
55  * If the text exceeds the specified max width, end it with an ellipsis (...)
56  */
57 std::string pango_line_ellipsize(const std::string& text, int font_size, int max_width, font::pango_text::FONT_STYLE font_style = font::pango_text::STYLE_NORMAL);
58 
59 /**
60  * Uses Pango to word wrap text.
61  */
62 std::string pango_word_wrap(const std::string& unwrapped_text, int font_size, int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false);
63 
64 /**
65  * Draws text on a surface.
66  *
67  * The text will be clipped to area. If the text runs outside of area
68  * horizontally, an ellipsis will be displayed at the end of it.
69  *
70  * If use_tooltips is true, then text with an ellipsis will have a tooltip
71  * set for it equivalent to the entire contents of the text.
72  *
73  * A bounding rectangle of the text is returned. If dst is nullptr, then the
74  * text will not be drawn, and a bounding rectangle only will be returned.
75  */
76 SDL_Rect pango_draw_text(surface& dst, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips = false, pango_text::FONT_STYLE style = pango_text::STYLE_NORMAL);
77 
78 /**
79  * Draws text on the screen.
80  *
81  * gui can be nullptr, in which case the bounding rectangle will still be
82  * returned.
83  */
84 SDL_Rect pango_draw_text(CVideo* gui, const SDL_Rect& area, int size, const color_t& color, const std::string& text, int x, int y, bool use_tooltips = false, pango_text::FONT_STYLE style = pango_text::STYLE_NORMAL);
85 
86 } // end namespace font
std::string pango_line_ellipsize(const std::string &text, int font_size, int max_width, font::pango_text::FONT_STYLE font_style)
If the text exceeds the specified max width, end it with an ellipsis (...)
Collection of helper functions relating to Pango formatting.
Definition: video.hpp:32
std::pair< int, int > pango_line_size(const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style)
Determine the width and height of a line of text given a certain font size.
General purpose widgets.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
SDL_Rect pango_draw_text(CVideo *gui, const SDL_Rect &area, int size, const color_t &color, const std::string &text, int x, int y, bool use_tooltips, pango_text::FONT_STYLE style)
Draws text on the screen.
std::string pango_word_wrap(const std::string &unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool)
Uses Pango to word wrap text.
surface pango_render_text(const std::string &text, int size, const color_t &color, font::pango_text::FONT_STYLE style, bool use_markup, int max_width)
Returns a SDL surface containing the rendered text.
int pango_line_width(const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
Determine the width of a line of text given a certain font size.