The Battle for Wesnoth  1.19.12+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 
24 static lg::log_domain log_font("font");
25 #define DBG_FT LOG_STREAM(debug, log_font)
26 #define LOG_FT LOG_STREAM(info, log_font)
27 #define WRN_FT LOG_STREAM(warn, log_font)
28 #define ERR_FT LOG_STREAM(err, log_font)
29 
30 namespace font {
31 
32 namespace {
33 
34 pango_text& private_renderer()
35 {
36  // Use a separate renderer for GUI1 in case of shenanigans.
37  static pango_text text_renderer;
38  return text_renderer;
39 }
40 
41 }
42 
43 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)
44 {
45  auto& ptext = private_renderer();
46 
47  ptext.set_text(text, use_markup);
48  ptext.set_family_class(font::family_class::sans_serif)
49  .set_font_size(size)
50  .set_font_style(style)
51  .set_maximum_height(-1, false)
52  .set_foreground_color(color)
53  .set_maximum_width(max_width)
54  .set_ellipse_mode(max_width > 0 ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
55 
56  return ptext.render_and_get_texture();
57 }
58 
59 std::pair<int, int> pango_line_size(const std::string& line, int font_size, font::pango_text::FONT_STYLE font_style)
60 {
61  auto& ptext = private_renderer();
62 
63  ptext.set_text(line, false);
64  ptext.set_family_class(font::family_class::sans_serif)
65  .set_font_size(font_size)
66  .set_font_style(font_style)
67  .set_maximum_height(-1, false)
68  .set_maximum_width(-1)
69  .set_ellipse_mode(PANGO_ELLIPSIZE_NONE);
70 
71  auto s = ptext.get_size();
72  return { s.x, s.y };
73 }
74 
75 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*/)
76 {
77  // FIXME: what the hell does partial_line do in the SDL_ttf version?
78 
79  if(max_lines == 0) {
80  return "";
81  }
82 
83  auto& ptext = private_renderer();
84 
85  ptext.set_text(unwrapped_text, false);
86  ptext.set_family_class(font::family_class::sans_serif)
87  .set_font_size(font_size)
88  .set_font_style(font::pango_text::STYLE_NORMAL)
89  .set_maximum_height(max_height, true)
90  .set_maximum_width(max_width)
91  .set_ellipse_mode(PANGO_ELLIPSIZE_NONE);
92 
93  std::string res;
94  const auto& lines = ptext.get_lines();
95 
96  for(const auto& line : lines) {
97  if(!res.empty())
98  res += '\n';
99  res += line;
100  if(--max_lines == 0)
101  break;
102  }
103 
104  return res;
105 }
106 
107 rect pango_draw_text(bool actually_draw, const rect& area, int size, const color_t& color, const std::string& text, int x, int y)
108 {
109  auto& ptext = private_renderer();
110 
111  ptext.set_text(text, false);
112  ptext.set_family_class(font::family_class::sans_serif)
113  .set_font_size(size)
114  .set_font_style(pango_text::STYLE_NORMAL)
115  .set_maximum_width(-1)
116  .set_foreground_color(color)
117  .set_ellipse_mode(PANGO_ELLIPSIZE_END);
118 
119  if(!area.empty()) {
120  ptext.set_maximum_height(area.h, true);
121  }
122 
123  auto extents = ptext.get_size();
124 
125  if(!area.empty() && extents.x > area.w) {
126  ptext.set_maximum_width(area.w);
127  }
128 
129  auto t = ptext.render_and_get_texture();
130 
131  SDL_Rect res = {x, y, t.w(), t.h()};
132 
133  if(actually_draw) {
134  draw::blit(t, res);
135  }
136 
137  return res;
138 }
139 
140 } // 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:380
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:189
Graphical text output.
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.
rect pango_draw_text(bool actually_draw, const rect &area, int size, const color_t &color, const std::string &text, int x, int y)
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.
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:61
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