The Battle for Wesnoth  1.19.10+dev
sdl_ttf_compat.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2025
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::family_class::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::family_class::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_word_wrap(const std::string& unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool /*partial_line*/)
77 {
78  // FIXME: what the hell does partial_line do in the SDL_ttf version?
79 
80  if(max_lines == 0) {
81  return "";
82  }
83 
84  auto& ptext = private_renderer();
85 
86  ptext.set_text(unwrapped_text, false);
87  ptext.set_family_class(font::family_class::sans_serif)
88  .set_font_size(font_size)
89  .set_font_style(font::pango_text::STYLE_NORMAL)
90  .set_maximum_height(max_height, true)
91  .set_maximum_width(max_width)
92  .set_ellipse_mode(PANGO_ELLIPSIZE_NONE);
93 
94  std::string res;
95  const auto& lines = ptext.get_lines();
96 
97  for(const auto& line : lines) {
98  if(!res.empty())
99  res += '\n';
100  res += line;
101  if(--max_lines == 0)
102  break;
103  }
104 
105  return res;
106 }
107 
108 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)
109 {
110  auto& ptext = private_renderer();
111 
112  ptext.set_text(text, false);
113  ptext.set_family_class(font::family_class::sans_serif)
114  .set_font_size(size)
115  .set_font_style(style)
116  .set_maximum_width(-1)
117  .set_foreground_color(color)
118  .set_ellipse_mode(PANGO_ELLIPSIZE_END);
119 
120  if(!area.empty()) {
121  ptext.set_maximum_height(area.h, true);
122  }
123 
124  auto extents = ptext.get_size();
125  bool ellipsized = false;
126 
127  if(!area.empty() && extents.x > area.w) {
128  ptext.set_maximum_width(area.w);
129  ellipsized = true;
130  }
131 
132  auto t = ptext.render_and_get_texture();
133 
134  SDL_Rect res = {x, y, t.w(), t.h()};
135 
136  if(actually_draw) {
137  draw::blit(t, res);
138  }
139 
140  if(ellipsized && use_tooltips) {
141  tooltips::add_tooltip(res, text);
142  }
143 
144  return res;
145 }
146 
147 } // end namespace font
double t
Definition: astarsearch.cpp:63
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
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:381
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:190
Graphical text output.
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(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
static lg::log_domain log_font("font")
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:49
bool empty() const
False if both w and h are > 0, true otherwise.
Definition: rect.cpp:49
static map_location::direction s