The Battle for Wesnoth  1.19.21+dev
attributes.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2025 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "font/font_options.hpp"
18 
19 #include <pango/pango-layout.h>
20 #include <utility>
21 
22 struct color_t;
23 
24 namespace font
25 {
26 /** Helper class to encapsulate the management of a PangoAttrList. */
28 {
29 public:
31  : attributes_(pango_attr_list_new())
32  {
33  }
34 
36  : attributes_(std::exchange(o.attributes_, nullptr))
37  {
38  }
39 
41  {
42  if(attributes_) {
43  pango_attr_list_unref(attributes_);
44  }
45  }
46 
47  attribute_list(const attribute_list&) = delete;
49 
51  {
52  attributes_ = std::exchange(o.attributes_, nullptr);
53  return *this;
54  };
55 
56  void insert(PangoAttribute* attr)
57  {
58  pango_attr_list_insert(attributes_, attr);
59  }
60 
61  void modify(PangoAttribute* attr)
62  {
63  pango_attr_list_change(attributes_, attr);
64  }
65 
66  void apply_to(PangoLayout* layout) const
67  {
68  pango_layout_set_attributes(layout, attributes_);
69  }
70 
71  void splice_into(PangoAttrList* target) const
72  {
73  pango_attr_list_splice(target, attributes_, 0, 0);
74  }
75 
76  void splice_into(const font::attribute_list& target, const unsigned attr_start = 0) const
77  {
78  pango_attr_list_splice(attributes_, target.attributes_, attr_start, 0);
79  }
80 
81 private:
82  PangoAttrList* attributes_;
83 };
84 
85 //
86 // The following free functions are thin wrappers around the corresponding
87 // pango_attr_new methods. For more details, refer to the Pango docs.
88 //
89 
90 /**
91  * Add Pango font weight attribute to a specific portion of text. This changes the font weight
92  * of the corresponding part of the text.
93  *
94  * @param list The attribute list to which to append this attribute.
95  * @param offset_start Byte index of the cursor where font weight change starts
96  * @param offset_end Byte index of the cursor where font weight change ends
97  * @param weight Pango font weight
98  */
99 void add_attribute_weight(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoWeight weight);
100 
101 /**
102  * Add Pango font style attribute to a specific portion of text, used to set italic/oblique text
103  *
104  * @param list The attribute list to which to append this attribute.
105  * @param offset_start Byte index of the cursor where font style change starts
106  * @param offset_end Byte index of the cursor where font style change ends
107  * @param style Pango font style (normal/italic/oblique)
108  */
109 void add_attribute_style(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoStyle style);
110 
111 /**
112  * Add Pango underline attribute to a specific portion of text. This adds an underline to the
113  * corresponding part of the text.
114  *
115  * @param list The attribute list to which to append this attribute.
116  * @param offset_start Byte index of the cursor where underline starts
117  * @param offset_end Byte index of the cursor where underline change ends
118  * @param underline Pango underline style
119  */
120 void add_attribute_underline(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoUnderline underline);
121 
122 /**
123  * Add Pango fg color attribute to a specific portion of text. This changes the foreground
124  * color of the corresponding part of the text.
125  *
126  * @param list The attribute list to which to append this attribute.
127  * @param offset_start Byte index of the cursor where color change starts
128  * @param offset_end Byte index of the cursor where color change ends
129  * @param color Foreground color
130  */
131 void add_attribute_fg_color(attribute_list& list, unsigned offset_start, unsigned offset_end, const color_t& color);
132 
133 /**
134  * Mark a specific portion of text for highlighting. Used for selection box.
135  * BGColor is set in set_text(), this just marks the area to be colored.
136  * Markup not used because the user may enter their own markup or special characters
137  *
138  * @param list The attribute list to which to append this attribute.
139  * @param offset_start Byte index of the cursor where selection/highlight starts
140  * @param offset_end Byte index of the cursor where selection/highlight ends
141  * @param color Highlight/Background color
142  */
143 void add_attribute_bg_color(attribute_list& list, unsigned offset_start, unsigned offset_end, const color_t& color);
144 
145 /**
146  * Add Pango font size attribute to a specific portion of text. This changes the font size
147  * of the corresponding part of the text.
148  *
149  * @param list The attribute list to which to append this attribute.
150  * @param offset_start Byte index of the cursor where size change starts
151  * @param offset_end Byte index of the cursor where size change ends
152  * @param size Font size
153  */
154 void add_attribute_size(attribute_list& list, unsigned offset_start, unsigned offset_end, int size);
155 
156 /**
157  * Add Pango font family attribute to a specific portion of text. This changes
158  * the font family of the corresponding part of the text.
159  *
160  * @param list The attribute list to which to append this attribute.
161  * @param offset_start Byte index of the cursor where size change starts
162  * @param offset_end Byte index of the cursor where size change ends
163  * @param family The font family
164  */
165 void add_attribute_font_family(attribute_list& list, unsigned offset_start, unsigned offset_end, font::family_class family);
166 
167 /**
168  * Add Pango line height attribute to a specific portion of text. This changes
169  * the line height of the corresponding part of the text.
170  *
171  * @param list The attribute list to which to append this attribute.
172  * @param offset_start Byte index of the cursor where size change starts
173  * @param offset_end Byte index of the cursor where size change ends
174  * @param factor The line height factor, i.e., `new_line_height/old_line_height`
175  */
176 void add_attribute_line_height(attribute_list& list, unsigned offset_start, unsigned offset_end, const double factor);
177 
178 /**
179  * Add Pango shape attribute to a specific portion of text. This replaces
180  * the text within the start and end offsets with an inline image.
181  * The caller should ensure that the portion of text between the given offset
182  * range is filled with dummy characters, such as
183  *
184  * @param list The attribute list to which to append this attribute.
185  * @param offset_start Byte index of the cursor where size change starts
186  * @param offset_end Byte index of the cursor where size change ends
187  * @param image_path Path to the image to be shown inline
188  */
189 void add_attribute_image_shape(attribute_list& list, unsigned offset_start, unsigned offset_end, const std::string& image_path);
190 
191 } // namespace font
Helper class to encapsulate the management of a PangoAttrList.
Definition: attributes.hpp:28
void splice_into(PangoAttrList *target) const
Definition: attributes.hpp:71
attribute_list(attribute_list &&o)
Definition: attributes.hpp:35
attribute_list & operator=(const attribute_list &)=delete
void splice_into(const font::attribute_list &target, const unsigned attr_start=0) const
Definition: attributes.hpp:76
void modify(PangoAttribute *attr)
Definition: attributes.hpp:61
void apply_to(PangoLayout *layout) const
Definition: attributes.hpp:66
void insert(PangoAttribute *attr)
Definition: attributes.hpp:56
attribute_list(const attribute_list &)=delete
PangoAttrList * attributes_
Definition: attributes.hpp:82
attribute_list & operator=(attribute_list &&o)
Definition: attributes.hpp:50
static void layout()
Graphical text output.
void add_attribute_image_shape(attribute_list &list, unsigned offset_start, unsigned offset_end, const std::string &image_path)
Add Pango shape attribute to a specific portion of text.
Definition: attributes.cpp:180
void add_attribute_size(attribute_list &list, unsigned offset_start, unsigned offset_end, int size)
Add Pango font size attribute to a specific portion of text.
Definition: attributes.cpp:64
void add_attribute_bg_color(attribute_list &list, unsigned offset_start, unsigned offset_end, const color_t &color)
Mark a specific portion of text for highlighting.
Definition: attributes.cpp:135
void add_attribute_weight(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoWeight weight)
Add Pango font weight attribute to a specific portion of text.
Definition: attributes.cpp:80
void add_attribute_line_height(attribute_list &list, unsigned offset_start, unsigned offset_end, const double factor)
Add Pango line height attribute to a specific portion of text.
Definition: attributes.cpp:166
void add_attribute_underline(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoUnderline underline)
Add Pango underline attribute to a specific portion of text.
Definition: attributes.cpp:106
void add_attribute_font_family(attribute_list &list, unsigned offset_start, unsigned offset_end, font::family_class family)
Add Pango font family attribute to a specific portion of text.
Definition: attributes.cpp:150
void add_attribute_style(attribute_list &list, unsigned offset_start, unsigned offset_end, PangoStyle style)
Add Pango font style attribute to a specific portion of text, used to set italic/oblique text.
Definition: attributes.cpp:93
void add_attribute_fg_color(attribute_list &list, unsigned offset_start, unsigned offset_end, const color_t &color)
Add Pango fg color attribute to a specific portion of text.
Definition: attributes.cpp:119
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:81
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:51