The Battle for Wesnoth  1.19.7+dev
text.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
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 #include "font/font_options.hpp"
19 #include "color.hpp"
20 #include "sdl/surface.hpp"
21 #include "sdl/texture.hpp"
23 
24 #include <pango/pangocairo.h>
25 
26 
27 #include <functional>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 /***
33  * Note: This is the cairo-pango code path, not the SDL_TTF code path.
34  */
35 
36 struct point;
37 
38 namespace font {
39 
40 /** Flush the rendered text cache. */
41 void flush_texture_cache();
42 
43 // add background color and also font markup.
44 
45 /**
46  * Text class.
47  *
48  * This class represents text which is rendered using Pango.
49  *
50  * It takes text, as a utf-8 std::string, plus formatting options including
51  * font and color. It provides a surface object which holds the rendered text.
52  *
53  * Besides this, it can do some additional calculations using the font layout.
54  *
55  * It can take an index into the text, and convert it to pixel coordinates,
56  * so that if we want to draw a cursor in an editbox, we know where to draw it.
57  *
58  * It can also take a pixel coordinate with respect to the text layout, and
59  * translate it back to an index into the original text. This is useful if the
60  * user clicks on the text, and we want to know where to move the cursor.
61  *
62  * The get_token method takes a pixel coordinate, which we assume represents a
63  * click position, and gets the corresponding "token" from the string. The default
64  * token delimiters are whitespace " \n\r\t". So, this returns the "word" that the
65  * user clicked on.
66  *
67  * Finally, the get_link method represents special support for hyperlinks in text.
68  * A token "looks like a link" if it begins "http://" or "https://".
69  * If a text has link_aware enabled, then any such token is rendered with an
70  * underline and in a special color, see `link_color`.
71  * The get_link method calls get_token and further checks if the clicked token
72  * looks like a link.
73  *
74  * This class stores the text to draw and uses pango with the cairo backend to
75  * render the text. See http://pango.org for more info.
76  *
77  */
79 {
80 public:
81  pango_text();
82 
83  pango_text(const pango_text&) = delete;
84  pango_text& operator=(const pango_text&) = delete;
85 
86  /**
87  * Returns the cached texture, or creates a new one otherwise.
88  *
89  * texture::w() and texture::h() methods will return the expected
90  * width and height of the texture in draw space. This may differ
91  * from the real value returned by texture::get_info().
92  *
93  * In almost all cases, use w() and h() to get the size of the
94  * rendered text for drawing.
95  */
97 
98 private:
99  /**
100  * Wrapper around render_surface which sets texture::w() and texture::h()
101  * in the same way that render_and_get_texture does.
102  *
103  * The viewport rect is interpreted at the scale of render-space, not
104  * drawing-space. This function has only been made private to preserve
105  * the drawing-space encapsulation.
106  */
107  texture render_texture(const SDL_Rect& viewport);
108 
109  /**
110  * Returns the rendered text.
111  *
112  * The viewport rect is interpreted at the scale of render-space, not
113  * drawing-space. This function has only been made private to preserve
114  * the drawing-space encapsulation.
115  *
116  * @param viewport Only this area needs to be drawn - the returned
117  * surface's origin will correspond to viewport.x and viewport.y, the
118  * width and height will be at least viewport.w and viewport.h (although
119  * they may be larger).
120  */
121  surface render_surface(const SDL_Rect& viewport);
122 
123 public:
124  /** Returns the size of the text, in drawing coordinates. */
125  point get_size();
126 
127  /** Has the text been truncated? This happens if it exceeds max width or height. */
128  bool is_truncated() const;
129 
130  /**
131  * Inserts UTF-8 text.
132  *
133  * @param offset The position to insert the text.
134  * @param text The UTF-8 text to insert.
135  * @param use_markup If the text is formatted or not.
136  *
137  * @returns The number of characters inserted.
138  */
139  unsigned insert_text(const unsigned offset, const std::string& text, const bool use_markup = false);
140 
141  /***** ***** ***** ***** Font flags ***** ***** ***** *****/
142 
143  // NOTE: these values must be powers of 2 in order to be bit-unique
144  enum FONT_STYLE {
149  };
150 
151  /***** ***** ***** ***** Query details ***** ***** ***** *****/
152 
153  /**
154  * Returns the maximum glyph height of a font, in drawing coordinates.
155  *
156  * @returns The height of the tallest possible glyph for the selected
157  * font. More specifically, the result is the sum of the maximum
158  * ascent and descent lengths.
159  */
160  int get_max_glyph_height() const;
161 
162  /**
163  * Gets the location for the cursor, in drawing coordinates.
164  *
165  * @param column The column character index of the cursor.
166  * @param line The line character index of the cursor.
167  *
168  * @returns The position of the top of the cursor. It the
169  * requested location is out of range 0,0 is
170  * returned.
171  */
173  const unsigned column, const unsigned line = 0) const;
174 
175  /**
176  * Gets the location for the cursor, in drawing coordinates.
177  *
178  * @param offset The column byte index of the cursor.
179  *
180  * @returns The position of the top of the cursor. It the
181  * requested location is out of range 0,0 is
182  * returned.
183  */
184  point get_cursor_pos_from_index(const unsigned offset) const;
185 
186  /**
187  * Get maximum length.
188  *
189  * @returns The maximum length of the text. The length of text
190  * should not exceed this value.
191  */
192  std::size_t get_maximum_length() const;
193 
194  /**
195  * Gets the largest collection of characters, including the token at position,
196  * and not including any characters from the delimiters set.
197  *
198  * @param position The pixel position in the text area.
199  * @param delimiters
200  *
201  * @returns The token containing position, and none of the
202  * delimiter characters. If position is out of bounds,
203  * it returns the empty string.
204  */
205  std::string get_token(const point & position, const char * delimiters = " \n\r\t") const;
206 
207  /**
208  * Checks if position points to a character in a link in the text, returns it
209  * if so, empty string otherwise. Link-awareness must be enabled to get results.
210  * @param position The pixel position in the text area.
211  *
212  * @returns The link if one is found, the empty string otherwise.
213  */
214  std::string get_link(const point & position) const;
215 
216  /**
217  * Gets the column of line of the character at the position.
218  *
219  * @param position The pixel position in the text area.
220  *
221  * @returns A point with the x value the column and the y
222  * value the line of the character found (or last
223  * character if not found.
224  */
225  point get_column_line(const point& position) const;
226 
227  int xy_to_index(const point& position) const;
228 
229  /**
230  * Retrieves a list of strings with contents for each rendered line.
231  *
232  * This method is not const because it requires rendering the text.
233  *
234  * @note This is only intended for renderer implementation details. This
235  * is a rather expensive function because it copies everything at
236  * least once.
237  */
238  std::vector<std::string> get_lines() const;
239 
240  /**
241  * Get a specific line from the pango layout
242  *
243  * @param index the line number of the line to retrieve
244  *
245  * @returns the PangoLayoutLine* corresponding to line number index
246  */
247  PangoLayoutLine* get_line(int index);
248 
249  /**
250  * Given a byte index, find out at which line the corresponding character
251  * is located.
252  *
253  * @param offset the byte index
254  *
255  * @returns the line number corresponding to the given index
256  */
257  int get_line_num_from_offset(const unsigned offset);
258 
259  /**
260  * Get number of lines in the text.
261  *
262  * @returns The number of lines in the text.
263  *
264  */
265  unsigned get_lines_count() const { return pango_layout_get_line_count(layout_.get()); };
266 
267  /**
268  * Gets the length of the text in bytes.
269  *
270  * The text set is UTF-8 so the length of the string might not be the length
271  * of the text.
272  */
273  std::size_t get_length() const { return length_; }
274 
275  /**
276  * Sets the text to render.
277  *
278  * @param text The text to render.
279  * @param markedup Should the text be rendered with pango
280  * markup. If the markup is invalid it's
281  * rendered as text without markup.
282  *
283  * @returns The status, if rendered as markup and the
284  * markup contains errors, false is returned
285  * else true.
286  */
287  bool set_text(const std::string& text, const bool markedup);
288 
289  /***** ***** ***** ***** Setters / getters ***** ***** ***** *****/
290 
291  const std::string& text() const { return text_; }
292 
294 
295  pango_text& set_font_size(unsigned font_size);
296 
297  pango_text& set_font_style(const FONT_STYLE font_style);
298 
299  pango_text& set_foreground_color(const color_t& color);
300 
301  pango_text& set_maximum_width(int width);
302 
303  pango_text& set_characters_per_line(const unsigned characters_per_line);
304 
305  pango_text& set_maximum_height(int height, bool multiline);
306 
307  pango_text& set_ellipse_mode(const PangoEllipsizeMode ellipse_mode);
308 
309  pango_text& set_alignment(const PangoAlignment alignment);
310 
311  pango_text& set_maximum_length(const std::size_t maximum_length);
312 
313  bool link_aware() const { return link_aware_; }
314 
315  pango_text& set_link_aware(bool b);
316 
317  pango_text& set_link_color(const color_t& color);
318 
319  pango_text& set_add_outline(bool do_add);
320 
321  // The following add attribute methods are thin wrappers around the corresponding pango
322  // add attribute methods. For more details, refer to the Pango docs.
323 
324  /**
325  * Add pango font weight attribute to a specific portion of text. This changes the font weight
326  * of the corresponding part of the text.
327  * @param start_offset Byte index of the cursor where font weight change starts
328  * @param end_offset Byte index of the cursor where font weight change ends
329  * @param weight Pango font weight
330  */
331  void add_attribute_weight(const unsigned start_offset, const unsigned end_offset, PangoWeight weight);
332 
333  /**
334  * Add pango font style attribute to a specific portion of text, used to set italic/oblique text
335  * @param start_offset Byte index of the cursor where font style change starts
336  * @param end_offset Byte index of the cursor where font style change ends
337  * @param style Pango font style (normal/italic/oblique)
338  */
339  void add_attribute_style(const unsigned start_offset, const unsigned end_offset, PangoStyle style);
340 
341  /**
342  * Add pango underline attribute to a specific portion of text. This adds an underline to the
343  * corresponding part of the text.
344  * @param start_offset Byte index of the cursor where underline starts
345  * @param end_offset Byte index of the cursor where underline change ends
346  * @param underline Pango underline style
347  */
348  void add_attribute_underline(const unsigned start_offset, const unsigned end_offset, PangoUnderline underline);
349 
350  /**
351  * Add pango fg color attribute to a specific portion of text. This changes the foreground
352  * color of the corresponding part of the text.
353  * @param start_offset Byte index of the cursor where color change starts
354  * @param end_offset Byte index of the cursor where color change ends
355  * @param color Foreground color
356  */
357  void add_attribute_fg_color(const unsigned start_offset, const unsigned end_offset, const color_t& color);
358 
359  /**
360  * Mark a specific portion of text for highlighting. Used for selection box.
361  * BGColor is set in set_text(), this just marks the area to be colored.
362  * Markup not used because the user may enter their own markup or special characters
363  * @param start_offset Byte index of the cursor where selection/highlight starts
364  * @param end_offset Byte index of the cursor where selection/highlight ends
365  * @param color Highlight/Background color
366  */
367  void add_attribute_bg_color(const unsigned start_offset, const unsigned end_offset, const color_t& color);
368 
369  /**
370  * Add pango font size attribute to a specific portion of text. This changes the font size
371  * of the corresponding part of the text.
372  * @param start_offset Byte index of the cursor where size change starts
373  * @param end_offset Byte index of the cursor where size change ends
374  * @param size Font size
375  */
376  void add_attribute_size(const unsigned start_offset, const unsigned end_offset, int size);
377 
378  /**
379  * Add pango font family attribute to a specific portion of text. This changes
380  * the font family of the corresponding part of the text.
381  * @param start_offset Byte index of the cursor where size change starts
382  * @param end_offset Byte index of the cursor where size change ends
383  * @param family The font family
384  */
385  void add_attribute_font_family(const unsigned start_offset, const unsigned end_offset, const std::string& family);
386 
387  /** Clears all attributes from the global attribute list */
388  void clear_attribute_list();
389 
390 private:
391 
392  /***** ***** ***** ***** Pango variables ***** ***** ***** *****/
393  std::unique_ptr<PangoContext, std::function<void(void*)>> context_;
394  std::unique_ptr<PangoLayout, std::function<void(void*)>> layout_;
395  mutable PangoRectangle rect_;
396 
397  /** The text to draw (stored as UTF-8). */
398  std::string text_;
399 
400  /** Does the text contain pango markup? If different render routines must be used. */
402 
403  /** Are hyperlinks in the text marked-up, and will get_link return them. */
405 
406  /**
407  * The color to render links in.
408  *
409  * Links are formatted using pango &lt;span> as follows:
410  *
411  * &lt;span underline="single" color=" + link_color_ + ">
412  */
414 
415  /** The font family class used. */
417 
418  /** The font size to draw. */
419  unsigned font_size_;
420 
421  /** The style of the font, this is an orred mask of the font flags. */
423 
424  /** The foreground color. */
426 
427  /** Whether to add an outline effect. */
429 
430  /**
431  * The maximum width of the text.
432  *
433  * Values less or equal to 0 mean no maximum and are internally stored as
434  * -1, since that's the value pango uses for it.
435  *
436  * See @ref characters_per_line_.
437  */
439 
440  /**
441  * The number of characters per line.
442  *
443  * This can be used as an alternative of @ref maximum_width_. The user can
444  * select a number of characters on a line for wrapping. When the value is
445  * non-zero it determines the maximum width based on the average character
446  * width.
447  *
448  * If both @ref maximum_width_ and @ref characters_per_line_ are set the
449  * minimum of the two will be the maximum.
450  *
451  * @note Long lines are often harder to read, setting this value can
452  * automatically wrap on a number of characters regardless of the font
453  * size. Often 66 characters is considered the optimal value for a one
454  * column text.
455  */
457 
458  /**
459  * The maximum height of the text.
460  *
461  * Values less or equal to 0 mean no maximum and are internally stored as
462  * -1, since that's the value pango uses for it.
463  */
465 
466  /** The way too long text is shown depends on this mode. */
467  PangoEllipsizeMode ellipse_mode_;
468 
469  /** The alignment of the text. */
470  PangoAlignment alignment_;
471 
472  /** The maximum length of the text. */
473  std::size_t maximum_length_;
474 
475  /**
476  * The text has two dirty states:
477  * - The setting of the state and the size calculations.
478  * - The rendering of the surface.
479  */
480 
481  /** The dirty state of the calculations. */
482  mutable bool calculation_dirty_;
483 
484  /** Length of the text. */
485  mutable std::size_t length_;
486 
490 
491  /**
492  * Global pango attribute list. All attributes in this list
493  * will be applied one by one to the text
494  */
495  PangoAttrList* global_attribute_list_;
496 
497  /** The pixel scale, used to render high-DPI text. */
499 
500  /** Recalculates the text layout. */
501  void recalculate() const;
502 
503  /** Calculates surface size. */
504  PangoRectangle calculate_size(PangoLayout& layout) const;
505 
506  /**
507  * Equivalent to create_surface(viewport), where the viewport's top-left is
508  * at (0,0) and the area is large enough to contain the full text.
509  *
510  * The top-left of the viewport will be at (0,0), regardless of the values
511  * of x and y in the rect_ member variable. If the x or y co-ordinates are
512  * non-zero, then x columns and y rows of blank space are included in the
513  * amount of memory allocated.
514  */
516 
517  /**
518  * Renders the text to a surface that uses surface_buffer_ as its data store,
519  * the buffer will be allocated or reallocated as necessary.
520  *
521  * The surface's origin will correspond to viewport.x and viewport.y, the
522  * width and height will be at least viewport.w and viewport.h (although
523  * they may be larger).
524  *
525  * @param viewport The area to draw, which can be a subset of the text. This
526  * rectangle's coordinates use render-space's scale.
527  */
528  surface create_surface(const SDL_Rect& viewport);
529 
530  /**
531  * This is part of create_surface(viewport). The separation is a legacy
532  * from workarounds to the size limits of cairo_surface_t.
533  */
534  void render(PangoLayout& layout, const SDL_Rect& viewport, const unsigned stride);
535 
536  /**
537  * Buffer to store the image on.
538  *
539  * We use a cairo surface to draw on this buffer and then use the buffer as
540  * data source for the SDL_Surface. This means the buffer needs to be stored
541  * in the object, since SDL_Surface doesn't own its buffer.
542  */
543  mutable std::vector<uint8_t> surface_buffer_;
544 
545  /**
546  * Sets the markup'ed text.
547  *
548  * It tries to set the text as markup. If the markup is invalid it will try
549  * a bit harder to recover from the errors and still set the markup.
550  *
551  * @param text The text to set as markup.
552  * @param layout
553  *
554  * @returns Whether the markup was set or an
555  * unrecoverable error occurred and the text is
556  * set as plain text with an error message.
557  */
558  bool set_markup(std::string_view text, PangoLayout& layout);
559 
560  bool validate_markup(std::string_view text, char** raw_text, std::string& semi_escaped) const;
561 
562  static void copy_layout_properties(PangoLayout& src, PangoLayout& dst);
563 
564  std::string format_links(std::string_view text) const;
565 
566  /**
567  * Adjust a texture's draw-width and height according to pixel scale.
568  *
569  * As fonts are rendered at output-scale, we need to do this just
570  * before returning the rendered texture. These attributes are stored
571  * as part of the returned texture object.
572  */
573  texture with_draw_scale(const texture& t) const;
574 
575  /** Scale the given render-space size to draw-space, rounding up. */
576  int to_draw_scale(int s) const;
577 
578  /** Scale the given render-space point to draw-space, rounding up. */
579  point to_draw_scale(const point& p) const;
580 
581  /** Update pixel scale, if necessary. */
582  void update_pixel_scale();
583 };
584 
585 /**
586  * Returns a reference to a static pango_text object.
587  *
588  * Since the class is essentially a render pipeline, there's no need for individual
589  * areas of the game to own their own renderers. Not to mention it isn't a trivial
590  * class; constructing one is likely to be expensive.
591  */
593 
594 /**
595  * Returns the maximum glyph height of a font, in pixels.
596  *
597  * @param size Desired font size in pixels.
598  * @param fclass Font family to use for measurement.
599  * @param style Font style to select the correct variant for measurement.
600  *
601  * @returns The height of the tallest possible glyph for the selected
602  * font. More specifically, the result is the sum of the maximum
603  * ascent and descent lengths.
604  */
606 
607 /* Returns the default line spacing factor
608  * For now hardcoded here */
609 constexpr float get_line_spacing_factor() { return 1.3f; };
610 
611 } // namespace font
double t
Definition: astarsearch.cpp:63
Text class.
Definition: text.hpp:79
unsigned get_lines_count() const
Get number of lines in the text.
Definition: text.hpp:265
int pixel_scale_
The pixel scale, used to render high-DPI text.
Definition: text.hpp:498
int xy_to_index(const point &position) const
Definition: text.cpp:320
pango_text & operator=(const pango_text &)=delete
pango_text & set_font_style(const FONT_STYLE font_style)
Definition: text.cpp:544
PangoEllipsizeMode ellipse_mode_
The way too long text is shown depends on this mode.
Definition: text.hpp:467
void add_attribute_weight(const unsigned start_offset, const unsigned end_offset, PangoWeight weight)
Add pango font weight attribute to a specific portion of text.
Definition: text.cpp:351
static void copy_layout_properties(PangoLayout &src, PangoLayout &dst)
Definition: text.cpp:1091
bool add_outline_
Whether to add an outline effect.
Definition: text.hpp:428
bool validate_markup(std::string_view text, char **raw_text, std::string &semi_escaped) const
Definition: text.cpp:1053
bool set_markup(std::string_view text, PangoLayout &layout)
Sets the markup'ed text.
Definition: text.cpp:982
std::size_t get_length() const
Gets the length of the text in bytes.
Definition: text.hpp:273
pango_text & set_maximum_length(const std::size_t maximum_length)
Definition: text.cpp:645
void render(PangoLayout &layout, const SDL_Rect &viewport, const unsigned stride)
This is part of create_surface(viewport).
Definition: text.cpp:881
surface create_surface()
Equivalent to create_surface(viewport), where the viewport's top-left is at (0,0) and the area is lar...
Definition: text.cpp:932
PangoAlignment alignment_
The alignment of the text.
Definition: text.hpp:470
PangoRectangle rect_
Definition: text.hpp:395
int maximum_height_
The maximum height of the text.
Definition: text.hpp:464
point get_size()
Returns the size of the text, in drawing coordinates.
Definition: text.cpp:150
pango_text & set_characters_per_line(const unsigned characters_per_line)
Definition: text.cpp:579
color_t link_color_
The color to render links in.
Definition: text.hpp:413
unsigned insert_text(const unsigned offset, const std::string &text, const bool use_markup=false)
Inserts UTF-8 text.
Definition: text.cpp:165
int get_line_num_from_offset(const unsigned offset)
Given a byte index, find out at which line the corresponding character is located.
Definition: text.cpp:1129
void recalculate() const
Recalculates the text layout.
Definition: text.cpp:728
color_t foreground_color_
The foreground color.
Definition: text.hpp:425
point get_column_line(const point &position) const
Gets the column of line of the character at the position.
Definition: text.cpp:286
std::unique_ptr< PangoContext, std::function< void(void *)> > context_
Definition: text.hpp:393
bool link_aware_
Are hyperlinks in the text marked-up, and will get_link return them.
Definition: text.hpp:404
pango_text & set_foreground_color(const color_t &color)
Definition: text.cpp:554
int to_draw_scale(int s) const
Scale the given render-space size to draw-space, rounding up.
Definition: text.cpp:139
std::string format_links(std::string_view text) const
Replaces all instances of URLs in a given string with formatted links and returns the result.
Definition: text.cpp:1015
pango_text(const pango_text &)=delete
PangoLayoutLine * get_line(int index)
Get a specific line from the pango layout.
Definition: text.cpp:1124
unsigned characters_per_line_
The number of characters per line.
Definition: text.hpp:456
bool markedup_text_
Does the text contain pango markup? If different render routines must be used.
Definition: text.hpp:401
pango_text & set_family_class(font::family_class fclass)
Definition: text.cpp:522
font::family_class font_class_
The font family class used.
Definition: text.hpp:416
std::vector< std::string > get_lines() const
Retrieves a list of strings with contents for each rendered line.
Definition: text.cpp:1098
void add_attribute_fg_color(const unsigned start_offset, const unsigned end_offset, const color_t &color)
Add pango fg color attribute to a specific portion of text.
Definition: text.cpp:419
void update_pixel_scale()
Update pixel scale, if necessary.
Definition: text.cpp:707
bool link_aware() const
Definition: text.hpp:313
void add_attribute_font_family(const unsigned start_offset, const unsigned end_offset, const std::string &family)
Add pango font family attribute to a specific portion of text.
Definition: text.cpp:439
unsigned font_size_
The font size to draw.
Definition: text.hpp:419
void add_attribute_size(const unsigned start_offset, const unsigned end_offset, int size)
Add pango font size attribute to a specific portion of text.
Definition: text.cpp:332
texture render_texture(const SDL_Rect &viewport)
Wrapper around render_surface which sets texture::w() and texture::h() in the same way that render_an...
Definition: text.cpp:113
surface render_surface(const SDL_Rect &viewport)
Returns the rendered text.
Definition: text.cpp:125
std::vector< uint8_t > surface_buffer_
Buffer to store the image on.
Definition: text.hpp:543
pango_text & set_add_outline(bool do_add)
Definition: text.cpp:677
unsigned attribute_end_offset_
Definition: text.hpp:488
pango_text & set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
Definition: text.cpp:615
void add_attribute_bg_color(const unsigned start_offset, const unsigned end_offset, const color_t &color)
Mark a specific portion of text for highlighting.
Definition: text.cpp:458
PangoRectangle calculate_size(PangoLayout &layout) const
Calculates surface size.
Definition: text.cpp:742
color_t highlight_color_
Definition: text.hpp:489
void add_attribute_underline(const unsigned start_offset, const unsigned end_offset, PangoUnderline underline)
Add pango underline attribute to a specific portion of text.
Definition: text.cpp:388
pango_text & set_alignment(const PangoAlignment alignment)
Definition: text.cpp:635
std::string text_
The text to draw (stored as UTF-8).
Definition: text.hpp:398
point get_cursor_position(const unsigned column, const unsigned line=0) const
Gets the location for the cursor, in drawing coordinates.
Definition: text.cpp:185
bool calculation_dirty_
The text has two dirty states:
Definition: text.hpp:482
std::unique_ptr< PangoLayout, std::function< void(void *)> > layout_
Definition: text.hpp:394
pango_text & set_font_size(unsigned font_size)
Definition: text.cpp:532
pango_text & set_link_aware(bool b)
Definition: text.cpp:658
FONT_STYLE font_style_
The style of the font, this is an orred mask of the font flags.
Definition: text.hpp:422
unsigned attribute_start_offset_
Definition: text.hpp:487
std::string get_token(const point &position, const char *delimiters=" \n\r\t") const
Gets the largest collection of characters, including the token at position, and not including any cha...
Definition: text.cpp:239
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
Definition: text.cpp:484
bool is_truncated() const
Has the text been truncated? This happens if it exceeds max width or height.
Definition: text.cpp:158
void add_attribute_style(const unsigned start_offset, const unsigned end_offset, PangoStyle style)
Add pango font style attribute to a specific portion of text, used to set italic/oblique text.
Definition: text.cpp:369
texture with_draw_scale(const texture &t) const
Adjust a texture's draw-width and height according to pixel scale.
Definition: text.cpp:132
std::size_t maximum_length_
The maximum length of the text.
Definition: text.hpp:473
point get_cursor_pos_from_index(const unsigned offset) const
Gets the location for the cursor, in drawing coordinates.
Definition: text.cpp:225
pango_text & set_maximum_height(int height, bool multiline)
Definition: text.cpp:590
pango_text & set_maximum_width(int width)
Definition: text.cpp:563
std::size_t get_maximum_length() const
Get maximum length.
Definition: text.cpp:234
PangoAttrList * global_attribute_list_
Global pango attribute list.
Definition: text.hpp:495
texture render_and_get_texture()
Returns the cached texture, or creates a new one otherwise.
Definition: text.cpp:118
pango_text & set_link_color(const color_t &color)
Definition: text.cpp:667
std::string get_link(const point &position) const
Checks if position points to a character in a link in the text, returns it if so, empty string otherw...
Definition: text.cpp:271
void clear_attribute_list()
Clears all attributes from the global attribute list.
Definition: text.cpp:479
const std::string & text() const
Definition: text.hpp:291
std::size_t length_
Length of the text.
Definition: text.hpp:485
int get_max_glyph_height() const
Returns the maximum glyph height of a font, in drawing coordinates.
Definition: text.cpp:687
int maximum_width_
The maximum width of the text.
Definition: text.hpp:438
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
static void layout()
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:180
Graphical text output.
family_class
Font classes for get_font_families().
@ FONT_SANS_SERIF
int get_max_height(unsigned size, font::family_class fclass, pango_text::FONT_STYLE style)
Returns the maximum glyph height of a font, in pixels.
Definition: text.cpp:1142
pango_text & get_text_renderer()
Returns a reference to a static pango_text object.
Definition: text.cpp:1136
constexpr float get_line_spacing_factor()
Definition: text.hpp:609
void flush_texture_cache()
Flush the rendered text cache.
Definition: text.cpp:61
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Holds a 2D point.
Definition: point.hpp:25
mock_party p
static map_location::direction s
#define b