Freeciv21
Develop your civilization from humble roots to a global empire
featured_text.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include <QString>
16 
17 /*
18  * Some words about the featured text module.
19  *
20  * Bored to have black only chat, players used to hack their clients, to
21  * obtain colored chat based patterns. Also for strategic communication,
22  * they added some clickable links on particular city or location.
23  * The present code is not based on old warclient code, but it also contains
24  * the same tools. Whereas colors where determinated in client side only,
25  * here the server also has the possibility to choose what color to display
26  * to the clients. Such embellishments are performed using escape sequences
27  * (which are described bellow) in the strings.
28  *
29  * Plain text vs featured text.
30  *
31  * What is called plain text in those files is actually a string without any
32  * escape sequence. It is the text which should basically displayed in the
33  * client, without taking account of the features. On the other side, what
34  * is called featured text is also a string, but which could include one of
35  * many escape sequences.
36  *
37  * Text tag.
38  *
39  * The text_tag type is a structure which tag a particular modification for
40  * a string. It contains many informations, which the type of modification,
41  * the start of the modification in bytes from the start of the string, and
42  * the stop of it. It also can contains many others fields, according to
43  * sequence type.
44  * Note that all offset datas are calculated in bytes and not in character
45  * number, so the client must translate those offsets before using them.
46  *
47  * Escape sequences.
48  *
49  * - Bold.
50  * Full name sequence: [bold] ... [/bold]
51  * Abbreviation sequence: [b] ... [/b]
52  * Text tag type: TTT_BOLD
53  *
54  * - Italic.
55  * Full name sequence: [italic] ... [/italic]
56  * Abbreviation sequence: [i] ... [/i]
57  * Text tag type: TTT_ITALIC
58  *
59  * - Strike.
60  * Full name sequence: [strike] ... [/strike]
61  * Abbreviation sequence: [s] ... [/s]
62  * Text tag type: TTT_STRIKE
63  *
64  * - Underline.
65  * Full name sequence: [underline] ... [/underline]
66  * Abbreviation sequence: [u] ... [/u]
67  * Text tag type: TTT_UNDERLINE
68  *
69  * - Color.
70  * Full name sequence: [color] ... [/color]
71  * Abbreviation sequence: [c] ... [/c]
72  * Text tag type: TTT_COLOR
73  * Option: foreground (abbreviation fg): a color name or a hex specification
74  * such as #3050b2 or #35b.
75  * Option: background (abbreviation bg): same type of data.
76  *
77  * - Link.
78  * Full name sequence: [link] ... [/link] or [link/]
79  * Abbreviation sequence: [l] ... [/l] or [l/]
80  * Text tag type: TTT_LINK
81  * Option: target (abbreviation tgt): absolutely needed. It is set to
82  * "city" (then link type is TLT_CITY), "tile" (TLT_TILE) or "unit"
83  * (TLT_UNIT).
84  * Option: id: if target type is TLT_CITY or TLT_UNIT, it is the id of
85  * the target.
86  * Option: name: if target type is TLT_CITY or TLT_UNIT, it is an additional
87  * string to display if the target is not known by the receiver.
88  * Options: x and y: if target type is TLT_TILE, they are the coordinates
89  * of the target.
90  */
91 
92 // Offset type (in bytes).
93 typedef int ft_offset_t;
94 // Unset offset value.
95 #define FT_OFFSET_UNSET ((ft_offset_t) -1)
96 
97 // Opaque type.
98 struct text_tag;
99 
100 // Define struct text_tag_list.
101 #define SPECLIST_TAG text_tag
102 #define SPECLIST_TYPE struct text_tag
103 #include "speclist.h"
104 
105 #define text_tag_list_iterate(tags, ptag) \
106  TYPED_LIST_ITERATE(struct text_tag, tags, ptag)
107 #define text_tag_list_iterate_end LIST_ITERATE_END
108 
109 /* The different text_tag types.
110  * Chaning the order doesn't break the network compatiblity. */
112  TTT_BOLD = 0,
119 };
120 
121 /* The different text_tag link types.
122  * Chaning the order doesn't break the network compatiblity. */
128 };
129 
130 // Default maximal link size
131 #define MAX_LEN_LINK 128
132 
133 // Simplification for colors.
134 struct ft_color {
135  const char *foreground;
136  const char *background;
137 };
138 #define FT_COLOR(fg, bg) \
139  { \
140  fg, bg \
141  }
142 /**************************************************************************
143  Constructor.
144 **************************************************************************/
145 static inline struct ft_color ft_color_construct(const char *foreground,
146  const char *background)
147 {
148  struct ft_color color = FT_COLOR(foreground, background);
149 
150  return color;
151 }
152 
153 /**************************************************************************
154  Returns whether a color is requested.
155 **************************************************************************/
156 static inline bool ft_color_requested(const struct ft_color color)
157 {
158  return ((nullptr != color.foreground && '\0' != color.foreground[0])
159  || (nullptr != color.background && '\0' != color.background[0]));
160 }
161 
162 // Predefined colors.
163 extern const struct ft_color ftc_any;
164 
165 extern const struct ft_color ftc_warning;
166 extern const struct ft_color ftc_log;
167 extern const struct ft_color ftc_server;
168 extern const struct ft_color ftc_client;
169 extern const struct ft_color ftc_editor;
170 extern const struct ft_color ftc_command;
171 extern struct ft_color ftc_changed;
172 extern const struct ft_color ftc_server_prompt;
173 extern const struct ft_color ftc_player_lost;
174 extern const struct ft_color ftc_game_start;
175 
176 extern const struct ft_color ftc_chat_public;
177 extern const struct ft_color ftc_chat_ally;
178 extern const struct ft_color ftc_chat_private;
179 extern const struct ft_color ftc_chat_luaconsole;
180 
181 extern const struct ft_color ftc_vote_public;
182 extern const struct ft_color ftc_vote_team;
183 extern const struct ft_color ftc_vote_passed;
184 extern const struct ft_color ftc_vote_failed;
185 extern const struct ft_color ftc_vote_yes;
186 extern const struct ft_color ftc_vote_no;
187 extern const struct ft_color ftc_vote_abstain;
188 
189 extern const struct ft_color ftc_luaconsole_input;
190 extern const struct ft_color ftc_luaconsole_error;
191 extern const struct ft_color ftc_luaconsole_warn;
192 extern const struct ft_color ftc_luaconsole_normal;
193 extern const struct ft_color ftc_luaconsole_verbose;
194 
195 // Main functions.
196 size_t featured_text_to_plain_text(const char *featured_text,
197  char *plain_text, size_t plain_text_len,
198  struct text_tag_list **tags,
199  bool replace_link_text);
200 size_t featured_text_apply_tag(const char *text_source, char *featured_text,
201  size_t featured_text_len,
202  enum text_tag_type tag_type,
203  ft_offset_t start_offset,
204  ft_offset_t stop_offset, ...);
205 
206 // Text tag list functions. NB: Overwrite the ones defined in speclist.h.
207 #define text_tag_list_new() text_tag_list_new_full(text_tag_destroy)
208 #define text_tag_list_copy(tags) \
209  text_tag_list_copy_full(tags, text_tag_copy, text_tag_destroy)
210 
211 // Text tag functions.
212 struct text_tag *text_tag_new(enum text_tag_type tag_type,
214  ft_offset_t stop_offset, ...);
215 struct text_tag *text_tag_copy(const struct text_tag *ptag);
216 void text_tag_destroy(struct text_tag *ptag);
217 
218 enum text_tag_type text_tag_type(const struct text_tag *ptag);
219 ft_offset_t text_tag_start_offset(const struct text_tag *ptag);
220 ft_offset_t text_tag_stop_offset(const struct text_tag *ptag);
221 
222 // Specific TTT_COLOR text tag type functions.
223 QString text_tag_color_foreground(const struct text_tag *ptag);
224 QString text_tag_color_background(const struct text_tag *ptag);
225 
226 // Specific TTT_LINK text tag type functions.
227 enum text_link_type text_tag_link_type(const struct text_tag *ptag);
228 int text_tag_link_id(const struct text_tag *ptag);
229 
230 // Tools.
231 const char *city_link(const struct city *pcity);
232 const char *city_tile_link(const struct city *pcity);
233 const char *tile_link(const struct tile *ptile);
234 const char *unit_link(const struct unit *punit);
235 const char *unit_tile_link(const struct unit *punit);
236 
237 const char *unit_veteran_level_and_bonus(const struct unit *punit);
238 const char *unit_veteran_level_string(const struct unit *punit);
239 const char *unit_achieved_rank_string(const struct unit *punit);
240 const char *unit_tired_attack_string(const struct unit *punit);
241 const char *unit_firepower_if_not_one(int firepower);
struct text_tag * text_tag_copy(const struct text_tag *ptag)
This function returns a new pointer to a text_tag which is similar to the 'ptag' argument.
const struct ft_color ftc_chat_private
size_t featured_text_apply_tag(const char *text_source, char *featured_text, size_t featured_text_len, enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
Apply a tag to a text.
const char * unit_firepower_if_not_one(int firepower)
Get string of unit's firepower text, i.e.
const char * unit_veteran_level_and_bonus(const struct unit *punit)
const char * city_tile_link(const struct city *pcity)
Get a text link to a city tile (make a clickable link to a tile with the city name as text).
const struct ft_color ftc_vote_abstain
enum text_link_type text_tag_link_type(const struct text_tag *ptag)
Return the link target type suggested by this text tag.
static bool ft_color_requested(const struct ft_color color)
const struct ft_color ftc_log
const struct ft_color ftc_player_lost
size_t featured_text_to_plain_text(const char *featured_text, char *plain_text, size_t plain_text_len, struct text_tag_list **tags, bool replace_link_text)
Separate the text from the text features.
const struct ft_color ftc_luaconsole_verbose
const char * tile_link(const struct tile *ptile)
Get a text link to a tile.
struct text_tag * text_tag_new(enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
Returns a new text_tag or nullptr on error.
const struct ft_color ftc_command
QString text_tag_color_background(const struct text_tag *ptag)
Return the background color suggested by this text tag.
int ft_offset_t
Definition: featured_text.h:93
text_link_type
@ TLT_INVALID
@ TLT_TILE
@ TLT_UNIT
@ TLT_CITY
const char * unit_tired_attack_string(const struct unit *punit)
Get string of unit's attack would be a tired attack or not.
QString text_tag_color_foreground(const struct text_tag *ptag)
Return the foreground color suggested by this text tag.
const char * unit_tile_link(const struct unit *punit)
Get a text link to a unit tile (make a clickable link to a tile with the unit type name as text).
ft_offset_t text_tag_stop_offset(const struct text_tag *ptag)
Return the stop offset (in bytes) of this text tag.
const char * unit_veteran_level_string(const struct unit *punit)
Get a text of a unit's vet level.
const struct ft_color ftc_server
const struct ft_color ftc_vote_failed
void text_tag_destroy(struct text_tag *ptag)
Free a text_tag structure.
const struct ft_color ftc_vote_yes
const struct ft_color ftc_luaconsole_error
const struct ft_color ftc_warning
text_tag_type
@ TTT_LINK
@ TTT_BOLD
@ TTT_ITALIC
@ TTT_INVALID
@ TTT_STRIKE
@ TTT_COLOR
@ TTT_UNDERLINE
const struct ft_color ftc_client
const struct ft_color ftc_luaconsole_normal
const struct ft_color ftc_editor
#define FT_COLOR(fg, bg)
const char * unit_achieved_rank_string(const struct unit *punit)
Get string of when unit gets upgraded to new veteran level.
const struct ft_color ftc_any
const struct ft_color ftc_vote_no
const char * city_link(const struct city *pcity)
Get a text link to a city.
const struct ft_color ftc_vote_passed
const struct ft_color ftc_luaconsole_warn
int text_tag_link_id(const struct text_tag *ptag)
Return the link target id suggested by this text tag (city id, tile index or unit id).
const struct ft_color ftc_chat_luaconsole
ft_offset_t text_tag_start_offset(const struct text_tag *ptag)
Return the start offset (in bytes) of this text tag.
const struct ft_color ftc_vote_team
const char * unit_link(const struct unit *punit)
Get a text link to an unit.
const struct ft_color ftc_game_start
const struct ft_color ftc_server_prompt
struct ft_color ftc_changed
const struct ft_color ftc_vote_public
static struct ft_color ft_color_construct(const char *foreground, const char *background)
const struct ft_color ftc_luaconsole_input
const struct ft_color ftc_chat_public
const struct ft_color ftc_chat_ally
Definition: city.h:291
const char * background
const char * foreground
ft_offset_t stop_offset
ft_offset_t start_offset
Definition: tile.h:42
Definition: unit.h:134