The Battle for Wesnoth  1.19.9+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 <pango/pango-layout.h>
18 
19 #include "font/font_options.hpp"
20 
21 struct color_t;
22 
23 namespace font
24 {
25 /** Helper class to encapsulate the management of a PangoAttrList. */
27 {
28 public:
30  : attributes_(pango_attr_list_new())
31  {
32  }
33 
35  {
36  pango_attr_list_unref(attributes_);
37  }
38 
39  attribute_list(const attribute_list&) = delete;
41 
42  void insert(PangoAttribute* attr)
43  {
44  pango_attr_list_insert(attributes_, attr);
45  }
46 
47  void modify(PangoAttribute* attr)
48  {
49  pango_attr_list_change(attributes_, attr);
50  }
51 
52  void apply_to(PangoLayout* layout) const
53  {
54  pango_layout_set_attributes(layout, attributes_);
55  }
56 
57  void splice_into(PangoAttrList* target) const
58  {
59  pango_attr_list_splice(target, attributes_, 0, 0);
60  }
61 
62 private:
63  PangoAttrList* attributes_;
64 };
65 
66 //
67 // The following free functions are thin wrappers around the corresponding
68 // pango_attr_new methods. For more details, refer to the Pango docs.
69 //
70 
71 /**
72  * Add Pango font weight attribute to a specific portion of text. This changes the font weight
73  * of the corresponding part of the text.
74  *
75  * @param list The attribute list to which to append this attribute.
76  * @param offset_start Byte index of the cursor where font weight change starts
77  * @param offset_end Byte index of the cursor where font weight change ends
78  * @param weight Pango font weight
79  */
80 void add_attribute_weight(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoWeight weight);
81 
82 /**
83  * Add Pango font style attribute to a specific portion of text, used to set italic/oblique text
84  *
85  * @param list The attribute list to which to append this attribute.
86  * @param offset_start Byte index of the cursor where font style change starts
87  * @param offset_end Byte index of the cursor where font style change ends
88  * @param style Pango font style (normal/italic/oblique)
89  */
90 void add_attribute_style(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoStyle style);
91 
92 /**
93  * Add Pango underline attribute to a specific portion of text. This adds an underline to the
94  * corresponding part of the text.
95  *
96  * @param list The attribute list to which to append this attribute.
97  * @param offset_start Byte index of the cursor where underline starts
98  * @param offset_end Byte index of the cursor where underline change ends
99  * @param underline Pango underline style
100  */
101 void add_attribute_underline(attribute_list& list, unsigned offset_start, unsigned offset_end, PangoUnderline underline);
102 
103 /**
104  * Add Pango fg color attribute to a specific portion of text. This changes the foreground
105  * color of the corresponding part of the text.
106  *
107  * @param list The attribute list to which to append this attribute.
108  * @param offset_start Byte index of the cursor where color change starts
109  * @param offset_end Byte index of the cursor where color change ends
110  * @param color Foreground color
111  */
112 void add_attribute_fg_color(attribute_list& list, unsigned offset_start, unsigned offset_end, const color_t& color);
113 
114 /**
115  * Mark a specific portion of text for highlighting. Used for selection box.
116  * BGColor is set in set_text(), this just marks the area to be colored.
117  * Markup not used because the user may enter their own markup or special characters
118  *
119  * @param list The attribute list to which to append this attribute.
120  * @param offset_start Byte index of the cursor where selection/highlight starts
121  * @param offset_end Byte index of the cursor where selection/highlight ends
122  * @param color Highlight/Background color
123  */
124 void add_attribute_bg_color(attribute_list& list, unsigned offset_start, unsigned offset_end, const color_t& color);
125 
126 /**
127  * Add Pango font size attribute to a specific portion of text. This changes the font size
128  * of the corresponding part of the text.
129  *
130  * @param list The attribute list to which to append this attribute.
131  * @param offset_start Byte index of the cursor where size change starts
132  * @param offset_end Byte index of the cursor where size change ends
133  * @param size Font size
134  */
135 void add_attribute_size(attribute_list& list, unsigned offset_start, unsigned offset_end, int size);
136 
137 /**
138  * Add Pango font family attribute to a specific portion of text. This changes
139  * the font family of the corresponding part of the text.
140  *
141  * @param list The attribute list to which to append this attribute.
142  * @param offset_start Byte index of the cursor where size change starts
143  * @param offset_end Byte index of the cursor where size change ends
144  * @param family The font family
145  */
146 void add_attribute_font_family(attribute_list& list, unsigned offset_start, unsigned offset_end, font::family_class family);
147 
148 } // namespace font
Helper class to encapsulate the management of a PangoAttrList.
Definition: attributes.hpp:27
void splice_into(PangoAttrList *target) const
Definition: attributes.hpp:57
attribute_list & operator=(const attribute_list &)=delete
void modify(PangoAttribute *attr)
Definition: attributes.hpp:47
void apply_to(PangoLayout *layout) const
Definition: attributes.hpp:52
void insert(PangoAttribute *attr)
Definition: attributes.hpp:42
attribute_list(const attribute_list &)=delete
PangoAttrList * attributes_
Definition: attributes.hpp:63
static void layout()
Graphical text output.
family_class
Font classes for get_font_families().
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:72
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:143
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:88
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:114
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:158
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:101
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:127
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59