The Battle for Wesnoth  1.19.0-dev
sdl_ttf_compat.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2024
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 struct rect;
33 class texture;
34 
35 namespace font {
36 
37 /**
38  * Returns a SDL texture containing the rendered text.
39  */
40 texture 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);
41 
42 /**
43  * Determine the width and height of a line of text given a certain font size.
44  */
45 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);
46 
47 /**
48  * Determine the width of a line of text given a certain font size.
49  */
50 inline int pango_line_width(const std::string& line, int font_size, font::pango_text::FONT_STYLE font_style = font::pango_text::STYLE_NORMAL)
51 {
52  return pango_line_size(line, font_size, font_style).first;
53 }
54 
55 /**
56  * If the text exceeds the specified max width, end it with an ellipsis (...)
57  */
58 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);
59 
60 /**
61  * Uses Pango to word wrap text.
62  */
63 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);
64 
65 /**
66  * Draws text on the screen.
67  *
68  * The text will be clipped to area. If the text runs outside of area
69  * horizontally, an ellipsis will be displayed at the end of it.
70  * If area is empty, the text will not be clipped.
71  *
72  * If use_tooltips is true, then text with an ellipsis will have a tooltip
73  * set for it equivalent to the entire contents of the text.
74  *
75  * A bounding rectangle of the text is returned. If actually_draw is true
76  * the text will also be drawn to the screen. Otherwise only the bounding
77  * rectangle is returned.
78  */
79 rect pango_draw_text(bool actually_draw, const 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);
80 
81 } // end namespace font
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:180
Collection of helper functions relating to Pango formatting.
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.
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 (...)
rect pango_draw_text(bool actually_draw, const 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::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.
texture 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 texture containing the rendered text.
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.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47