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