The Battle for Wesnoth  1.19.5+dev
sdl_ttf_compat.cpp
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 #include "font/sdl_ttf_compat.hpp"
17 
18 #include "draw.hpp"
19 #include "log.hpp"
20 #include "sdl/point.hpp"
21 #include "sdl/texture.hpp"
23 #include "tooltips.hpp"
24 
25 static lg::log_domain log_font("font");
26 #define DBG_FT LOG_STREAM(debug, log_font)
27 #define LOG_FT LOG_STREAM(info, log_font)
28 #define WRN_FT LOG_STREAM(warn, log_font)
29 #define ERR_FT LOG_STREAM(err, log_font)
30 
31 namespace font {
32 
33 namespace {
34 
35 pango_text& private_renderer()
36 {
37  // Use a separate renderer for GUI1 in case of shenanigans.
38  static pango_text text_renderer;
39  return text_renderer;
40 }
41 
42 }
43 
44 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)
45 {
46  auto& ptext = private_renderer();
47 
48  ptext.set_text(text, use_markup);
49  ptext.set_family_class(font::FONT_SANS_SERIF)
50  .set_font_size(size)
51  .set_font_style(style)
52  .set_maximum_height(-1, false)
53  .set_foreground_color(color)
54  .set_maximum_width(max_width)
55  .set_ellipse_mode(max_width > 0 ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
56 
57  return ptext.render_and_get_texture();
58 }
59 
60 std::pair<int, int> pango_line_size(const std::string& line, int font_size, font::pango_text::FONT_STYLE font_style)
61 {
62  auto& ptext = private_renderer();
63 
64  ptext.set_text(line, false);
65  ptext.set_family_class(font::FONT_SANS_SERIF)
66  .set_font_size(font_size)
67  .set_font_style(font_style)
68  .set_maximum_height(-1, false)
69  .set_maximum_width(-1)
70  .set_ellipse_mode(PANGO_ELLIPSIZE_NONE);
71 
72  auto s = ptext.get_size();
73  return { s.x, s.y };
74 }
75 
76 std::string pango_line_ellipsize(const std::string& text, int font_size, int max_width, font::pango_text::FONT_STYLE font_style)
77 {
78  if(pango_line_width(text, font_size, font_style) <= max_width) {
79  return text;
80  }
81  if(pango_line_width(font::ellipsis, font_size, font_style) > max_width) {
82  return "";
83  }
84 
85  std::string current_substring;
86 
87  try {
88  utf8::iterator itor(text);
89  for(; itor != utf8::iterator::end(text); ++itor) {
90  std::string tmp = current_substring;
91  tmp.append(itor.substr().first, itor.substr().second);
92 
93  if(pango_line_width(tmp + font::ellipsis, font_size, font_style) > max_width) {
94  return current_substring + font::ellipsis;
95  }
96 
97  current_substring = std::move(tmp);
98  }
99  } catch(const utf8::invalid_utf8_exception&) {
100  WRN_FT << "Invalid UTF-8 string: \"" << text << "\"";
101  return "";
102  }
103 
104  return text; // Should not happen
105 }
106 
107 std::string pango_word_wrap(const std::string& unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool /*partial_line*/)
108 {
109  // FIXME: what the hell does partial_line do in the SDL_ttf version?
110 
111  if(max_lines == 0) {
112  return "";
113  }
114 
115  auto& ptext = private_renderer();
116 
117  ptext.set_text(unwrapped_text, false);
118  ptext.set_family_class(font::FONT_SANS_SERIF)
119  .set_font_size(font_size)
120  .set_font_style(font::pango_text::STYLE_NORMAL)
121  .set_maximum_height(max_height, true)
122  .set_maximum_width(max_width)
123  .set_ellipse_mode(PANGO_ELLIPSIZE_NONE);
124 
125  std::string res;
126  const auto& lines = ptext.get_lines();
127 
128  for(const auto& line : lines) {
129  if(!res.empty())
130  res += '\n';
131  res += line;
132  if(--max_lines == 0)
133  break;
134  }
135 
136  return res;
137 }
138 
139 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)
140 {
141  auto& ptext = private_renderer();
142 
143  ptext.set_text(text, false);
144  ptext.set_family_class(font::FONT_SANS_SERIF)
145  .set_font_size(size)
146  .set_font_style(style)
147  .set_maximum_width(-1)
148  .set_foreground_color(color)
149  .set_ellipse_mode(PANGO_ELLIPSIZE_END);
150 
151  if(!area.empty()) {
152  ptext.set_maximum_height(area.h, true);
153  }
154 
155  auto extents = ptext.get_size();
156  bool ellipsized = false;
157 
158  if(!area.empty() && extents.x > area.w) {
159  ptext.set_maximum_width(area.w);
160  ellipsized = true;
161  }
162 
163  auto t = ptext.render_and_get_texture();
164 
165  SDL_Rect res = {x, y, t.w(), t.h()};
166 
167  if(actually_draw) {
168  draw::blit(t, res);
169  }
170 
171  if(ellipsized && use_tooltips) {
172  tooltips::add_tooltip(res, text);
173  }
174 
175  return res;
176 }
177 
178 } // end namespace font
double t
Definition: astarsearch.cpp:63
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
static iterator_base end(const string_type &str)
const std::pair< typename string_type::const_iterator, typename string_type::const_iterator > & substr() const
Thrown by operations encountering invalid UTF-8 data.
Drawing functions, for drawing things on the screen.
Standard logging facilities (interface).
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:310
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:180
Graphical text output.
@ FONT_SANS_SERIF
const std::string ellipsis
Definition: constants.cpp:39
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.
int add_tooltip(const SDL_Rect &origin, const std::string &message, const std::string &action)
Definition: tooltips.cpp:299
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
static lg::log_domain log_font("font")
#define WRN_FT
Transitional API for porting SDL_ttf-based code to Pango.
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
bool empty() const
False if both w and h are > 0, true otherwise.
Definition: rect.cpp:48
static map_location::direction s