GCC Middle and Back End API Reference
types.h
Go to the documentation of this file.
1/* Types for drawing 2d "text art".
2 Copyright (C) 2023-2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_TEXT_ART_TYPES_H
22#define GCC_TEXT_ART_TYPES_H
23
24/* This header uses std::vector, but <vector> can't be directly
25 included due to issues with macros. Hence it must be included from
26 system.h by defining INCLUDE_VECTOR in any source file using it. */
27
28#ifndef INCLUDE_VECTOR
29# error "You must define INCLUDE_VECTOR before including system.h to use text-art/types.h"
30#endif
31
32#include "cpplib.h"
33#include "pretty-print.h"
34
35namespace text_art {
36
37/* Forward decls. */
38
39class canvas;
40class table;
41class theme;
42
43/* Classes for geometry.
44 We use templates to avoid mixing up e.g. canvas coordinates
45 with table coordinates. */
46
47template <typename CoordinateSystem>
48struct size
49{
50 size (int w_, int h_) : w (w_), h (h_) {}
51 int w;
52 int h;
53};
54
55template <typename CoordinateSystem>
56struct coord
57{
58 coord (int x_, int y_) : x (x_), y (y_) {}
59 int x;
60 int y;
61};
62
63template <typename CoordinateSystem>
69
70/* A half-open range [start, next) of int. */
71
72template <typename CoordinateSystem>
73struct range
74{
75 range (int start_, int next_)
76 : start (start_), next (next_)
77 {}
78
79 int get_min () const { return start; }
80 int get_max () const { return next - 1; }
81 int get_next () const { return next; }
82 int get_size () const { return next - start; }
83
84 int get_midpoint () const { return get_min () + get_size () / 2; }
85
86 int start;
87 int next;
88};
89
90/* A rectangle area within CoordinateSystem. */
91
92template <typename CoordinateSystem>
93struct rect
94{
97 : m_top_left (top_left),
98 m_size (size)
99 {
100 }
101
104 : m_top_left (x_range.get_min (), y_range.get_min ()),
105 m_size (x_range.get_size (), y_range.get_size ())
106 {
107 }
108
109 int get_min_x () const { return m_top_left.x; }
110 int get_min_y () const { return m_top_left.y; }
111 int get_max_x () const { return m_top_left.x + m_size.w - 1; }
112 int get_max_y () const { return m_top_left.y + m_size.h - 1; }
113 int get_next_x () const { return m_top_left.x + m_size.w; }
114 int get_next_y () const { return m_top_left.y + m_size.h; }
115
124
125 int get_width () const { return m_size.w; }
126 int get_height () const { return m_size.h; }
127
130};
131
132template <typename CoordinateSystem>
135{
136 return rect<CoordinateSystem> (r.m_top_left + offset, r.m_size);
137}
138
139template <typename ElementType, typename SizeType, typename CoordType>
141{
142 public:
143 typedef ElementType element_t;
144 typedef SizeType size_t;
145 typedef CoordType coord_t;
146
147 array2 (size_t sz)
148 : m_size (sz),
149 m_elements (sz.w * sz.h)
150 {
151 }
152 array2 (array2 &&other)
153 : m_size (other.m_size),
154 m_elements (std::move (other.m_elements))
155 {
156 }
157
158 /* Move assignment not implemented or used. */
159 array2 &operator== (array2 &&other) = delete;
160
161 /* No copy ctor or assignment op. */
162 array2 (const array2 &other) = delete;
163 array2 &operator= (const array2 &other) = delete;
164
165
166 const size_t &get_size () const { return m_size; }
167
168 void add_row (const element_t &element)
169 {
170 m_size.h++;
171 m_elements.insert (m_elements.end (), m_size.w, element);
172 }
173
174 const element_t &get (const coord_t &coord) const
175 {
176 ::size_t idx = get_idx (coord);
177 return m_elements[idx];
178 }
179
180 void set (const coord_t &coord, const element_t &element)
181 {
182 ::size_t idx = get_idx (coord);
183 m_elements[idx] = element;
184 }
185
186 void set (const coord_t &coord, element_t &&element)
187 {
188 ::size_t idx = get_idx (coord);
189 m_elements[idx] = std::move (element);
190 }
191
192 void fill (element_t element)
193 {
194 for (int y = 0; y < m_size.h; y++)
195 for (int x = 0; x < m_size.w; x++)
196 set (coord_t (x, y), element);
197 }
198
199 private:
201 {
202 gcc_assert (coord.x >= 0);
203 gcc_assert (coord.x < m_size.w);
204 gcc_assert (coord.y >= 0);
205 gcc_assert (coord.y < m_size.h);
206 return (coord.y * m_size.w) + coord.x;
207 }
208
209 size_t m_size;
210 std::vector<element_t> m_elements;
211};
212
213/* A combination of attributes describing how to style a text cell.
214 We only support those attributes mentioned in invoke.texi:
215 - bold,
216 - underscore,
217 - blink,
218 - inverse,
219 - colors for foreground and background:
220 - default color
221 - named colors
222 - 16-color mode colors (the "bright" variants)
223 - 88-color mode
224 - 256-color mode
225 plus URLs. */
226
227struct style
228{
229 typedef unsigned char id_t;
230 static const id_t id_plain = 0;
231
232 /* Colors. */
233 enum class named_color
234 {
235 DEFAULT,
236 // ANSI order
237 BLACK,
238 RED,
239 GREEN,
240 YELLOW,
241 BLUE,
242 MAGENTA,
243 CYAN,
244 WHITE
245 };
246
247
248 struct color
249 {
250 enum class kind
251 {
252 NAMED,
253 BITS_8,
254 BITS_24,
256
257 union
258 {
259 struct {
260 enum named_color m_name;
261 bool m_bright;
262 } m_named;
263 uint8_t m_8bit;
264 struct {
265 uint8_t r;
266 uint8_t g;
267 uint8_t b;
268 } m_24bit;
269 } u;
270
271 /* Constructor for named colors. */
273 bool bright = false)
274 : m_kind (kind::NAMED)
275 {
276 u.m_named.m_name = name;
277 u.m_named.m_bright = bright;
278 }
279
280 /* Constructor for 8-bit colors. */
281 color (uint8_t col_val)
282 : m_kind (kind::BITS_8)
283 {
284 u.m_8bit = col_val;
285 }
286
287 /* Constructor for 24-bit colors. */
288 color (uint8_t r, uint8_t g, uint8_t b)
289 : m_kind (kind::BITS_24)
290 {
291 u.m_24bit.r = r;
292 u.m_24bit.g = g;
293 u.m_24bit.b = b;
294 }
295
296 bool operator== (const color &other) const;
297 bool operator!= (const color &other) const
298 {
299 return !(*this == other);
300 }
301
302 void print_sgr (pretty_printer *pp, bool fg, bool &need_separator) const;
303 };
304
306 : m_bold (false),
308 m_blink (false),
310 m_fg_color (named_color::DEFAULT),
311 m_bg_color (named_color::DEFAULT),
312 m_url ()
313 {}
314
315 bool operator== (const style &other) const
316 {
317 return (m_bold == other.m_bold
318 && m_underscore == other.m_underscore
319 && m_blink == other.m_blink
320 && m_reverse == other.m_reverse
321 && m_fg_color == other.m_fg_color
322 && m_bg_color == other.m_bg_color
323 && m_url == other.m_url);
324 }
325
326 style &set_style_url (const char *url);
327
328 static void print_changes (pretty_printer *pp,
329 const style &old_style,
330 const style &new_style);
331
332 bool m_bold;
338 std::vector<cppchar_t> m_url; // empty = no URL
339};
340
341extern style get_style_from_color_cap_name (const char *name);
342
343/* A class to keep track of all the styles in use in a drawing, so that
344 we can refer to them via the compact style::id_t type, rather than
345 via e.g. pointers. */
346
348{
349 public:
350 style_manager ();
352 const style &get_style (style::id_t id) const
353 {
354 return m_styles[id];
355 }
357 style::id_t old_id,
358 style::id_t new_id) const;
359 unsigned get_num_styles () const { return m_styles.size (); }
360
361private:
362 std::vector<style> m_styles;
363};
364
366{
367 public:
368 friend class styled_string;
369
370 explicit styled_unichar ()
371 : m_code (0),
372 m_style_id (style::id_plain)
373 {}
374 explicit styled_unichar (cppchar_t ch)
375 : m_code (ch),
377 m_style_id (style::id_plain)
378 {}
379 explicit styled_unichar (cppchar_t ch, bool emoji, style::id_t style_id)
380 : m_code (ch),
381 m_emoji_variant_p (emoji),
382 m_style_id (style_id)
383 {
384 gcc_assert (style_id <= 0x7f);
385 }
386
387 cppchar_t get_code () const { return m_code; }
388 bool emoji_variant_p () const { return m_emoji_variant_p; }
390
391 bool double_width_p () const
392 {
393 int width = cpp_wcwidth (get_code ());
394 gcc_assert (width == 1 || width == 2);
395 return width == 2;
396 }
397
398 bool operator== (const styled_unichar &other) const
399 {
400 return (m_code == other.m_code
401 && m_emoji_variant_p == other.m_emoji_variant_p
402 && m_style_id == other.m_style_id);
403 }
404
406
407 int get_canvas_width () const
408 {
409 return cpp_wcwidth (m_code);
410 }
411
412 void add_combining_char (cppchar_t ch)
413 {
414 m_combining_chars.push_back (ch);
415 }
416
417 const std::vector<cppchar_t> get_combining_chars () const
418 {
419 return m_combining_chars;
420 }
421
422private:
423 cppchar_t m_code : 24;
426 std::vector<cppchar_t> m_combining_chars;
427};
428
430{
431 public:
432 explicit styled_string () = default;
433 explicit styled_string (style_manager &sm, const char *str);
434 explicit styled_string (cppchar_t cppchar, bool emoji = false);
435
438
439 /* No copy ctor or assignment op. */
440 styled_string (const styled_string &) = delete;
442
443 /* For the few cases where copying is required, spell it out explicitly. */
445 {
446 styled_string result;
447 result.m_chars = m_chars;
448 return result;
449 }
450
451 bool operator== (const styled_string &other) const
452 {
453 return m_chars == other.m_chars;
454 }
455
457 printer_fn format_decoder,
458 const char *fmt, ...)
461 printer_fn format_decoder,
462 const char *fmt,
463 va_list *args)
465
466 size_t size () const { return m_chars.size (); }
467 styled_unichar operator[] (size_t idx) const { return m_chars[idx]; }
468
469 std::vector<styled_unichar>::const_iterator begin () const
470 {
471 return m_chars.begin ();
472 }
473 std::vector<styled_unichar>::const_iterator end () const
474 {
475 return m_chars.end ();
476 }
477
478 int calc_canvas_width () const;
479
480 void append (const styled_string &suffix);
481
482 void set_url (style_manager &sm, const char *url);
483
484private:
485 std::vector<styled_unichar> m_chars;
486};
487
488enum class x_align
489{
493};
494
495enum class y_align
496{
500};
501
502/* A set of cardinal directions within a canvas or table. */
503
505{
506public:
507 directions (bool up, bool down, bool left, bool right)
508 : m_up (up), m_down (down), m_left (left), m_right (right)
509 {
510 }
511
512 size_t as_index () const
513 {
514 return (m_up << 3) | (m_down << 2) | (m_left << 1) | m_right;
515 }
516
517 bool m_up: 1;
518 bool m_down: 1;
519 bool m_left: 1;
520 bool m_right: 1;
521};
522
523} // namespace text_art
524
525#endif /* GCC_TEXT_ART_TYPES_H */
Definition pretty-print.h:241
CoordType coord_t
Definition types.h:145
SizeType size_t
Definition types.h:144
void set(const coord_t &coord, const element_t &element)
Definition types.h:180
array2 & operator==(array2 &&other)=delete
array2(const array2 &other)=delete
ElementType element_t
Definition types.h:143
const size_t & get_size() const
Definition types.h:166
void set(const coord_t &coord, element_t &&element)
Definition types.h:186
void add_row(const element_t &element)
Definition types.h:168
size_t m_size
Definition types.h:209
const element_t & get(const coord_t &coord) const
Definition types.h:174
array2 & operator=(const array2 &other)=delete
std::vector< element_t > m_elements
Definition types.h:210
array2(size_t sz)
Definition types.h:147
array2(array2 &&other)
Definition types.h:152
void fill(element_t element)
Definition types.h:192
::size_t get_idx(const coord_t &coord) const
Definition types.h:200
Definition canvas.h:38
Definition types.h:348
void print_any_style_changes(pretty_printer *pp, style::id_t old_id, style::id_t new_id) const
Definition style.cc:307
const style & get_style(style::id_t id) const
Definition types.h:352
style::id_t get_or_create_id(const style &style)
Definition style.cc:284
style_manager()
Definition style.cc:277
unsigned get_num_styles() const
Definition types.h:359
std::vector< style > m_styles
Definition types.h:362
Definition types.h:430
styled_string(const styled_string &)=delete
styled_string(styled_string &&)=default
int calc_canvas_width() const
Definition styled-string.cc:588
styled_string & operator=(styled_string &&)=default
bool operator==(const styled_string &other) const
Definition types.h:451
std::vector< styled_unichar > m_chars
Definition types.h:485
styled_string copy() const
Definition types.h:444
void set_url(style_manager &sm, const char *url)
Definition styled-string.cc:605
std::vector< styled_unichar >::const_iterator begin() const
Definition types.h:469
void append(const styled_string &suffix)
Definition styled-string.cc:597
static styled_string static styled_string from_fmt_va(style_manager &sm, printer_fn format_decoder, const char *fmt, va_list *args) ATTRIBUTE_GCC_PPDIAG(3
Definition styled-string.cc:559
styled_unichar operator[](size_t idx) const
Definition types.h:467
static styled_string static styled_string size_t size() const
Definition types.h:466
static styled_string from_fmt(style_manager &sm, printer_fn format_decoder, const char *fmt,...) ATTRIBUTE_GCC_PPDIAG(3
Definition styled-string.cc:576
std::vector< styled_unichar >::const_iterator end() const
Definition types.h:473
Definition types.h:366
bool m_emoji_variant_p
Definition types.h:424
bool emoji_variant_p() const
Definition types.h:388
styled_unichar(cppchar_t ch)
Definition types.h:374
int get_canvas_width() const
Definition types.h:407
const std::vector< cppchar_t > get_combining_chars() const
Definition types.h:417
style::id_t m_style_id
Definition types.h:425
styled_unichar(cppchar_t ch, bool emoji, style::id_t style_id)
Definition types.h:379
void set_emoji_variant()
Definition types.h:405
style::id_t get_style_id() const
Definition types.h:389
bool operator==(const styled_unichar &other) const
Definition types.h:398
styled_unichar()
Definition types.h:370
cppchar_t m_code
Definition types.h:423
void add_combining_char(cppchar_t ch)
Definition types.h:412
friend class styled_string
Definition types.h:368
cppchar_t get_code() const
Definition types.h:387
std::vector< cppchar_t > m_combining_chars
Definition types.h:426
bool double_width_p() const
Definition types.h:391
Definition table.h:77
Definition theme.h:30
gcc::context * g
Definition context.cc:31
#define BOTTOM
Definition ipa.cc:1386
Definition analyzer-logging.h:28
coord< CoordinateSystem > operator+(coord< CoordinateSystem > a, coord< CoordinateSystem > b)
Definition types.h:64
style get_style_from_color_cap_name(const char *name)
Definition style.cc:263
y_align
Definition types.h:496
@ TOP
Definition types.h:497
x_align
Definition types.h:489
@ RIGHT
Definition types.h:492
@ LEFT
Definition types.h:490
@ CENTER
Definition types.h:491
poly_int< N, C > r
Definition poly-int.h:774
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
Ca & a
Definition poly-int.h:770
#define ATTRIBUTE_GCC_PPDIAG(m, n)
Definition pretty-print.h:582
bool(*) printer_fn(pretty_printer *, text_info *, const char *, int, bool, bool, bool, bool *, pp_token_list &)
Definition pretty-print.h:190
Definition collect2.cc:168
Definition ira-emit.cc:158
Definition types.h:57
int x
Definition types.h:59
int y
Definition types.h:60
coord(int x_, int y_)
Definition types.h:58
bool m_right
Definition types.h:520
bool m_up
Definition types.h:517
bool m_left
Definition types.h:519
size_t as_index() const
Definition types.h:512
bool m_down
Definition types.h:518
directions(bool up, bool down, bool left, bool right)
Definition types.h:507
Definition types.h:74
int start
Definition types.h:86
int get_size() const
Definition types.h:82
range(int start_, int next_)
Definition types.h:75
int get_max() const
Definition types.h:80
int get_midpoint() const
Definition types.h:84
int next
Definition types.h:87
int get_next() const
Definition types.h:81
int get_min() const
Definition types.h:79
Definition types.h:94
int get_min_y() const
Definition types.h:110
int get_max_x() const
Definition types.h:111
int get_width() const
Definition types.h:125
range< CoordinateSystem > get_y_range() const
Definition types.h:120
int get_min_x() const
Definition types.h:109
int get_next_x() const
Definition types.h:113
int get_next_y() const
Definition types.h:114
size< class canvas > m_size
Definition types.h:129
rect(coord< CoordinateSystem > top_left, size< CoordinateSystem > size)
Definition types.h:95
range< CoordinateSystem > get_x_range() const
Definition types.h:116
int get_height() const
Definition types.h:126
rect(range< CoordinateSystem > x_range, range< CoordinateSystem > y_range)
Definition types.h:102
coord< class canvas > m_top_left
Definition types.h:128
int get_max_y() const
Definition types.h:112
Definition types.h:49
int w
Definition types.h:51
size(int w_, int h_)
Definition types.h:50
int h
Definition types.h:52
Definition types.h:249
color(enum named_color name=named_color::DEFAULT, bool bright=false)
Definition types.h:272
union text_art::style::color::@117042154020367337343243002032317004337077075210 u
kind
Definition types.h:251
void print_sgr(pretty_printer *pp, bool fg, bool &need_separator) const
Definition style.cc:79
color(uint8_t r, uint8_t g, uint8_t b)
Definition types.h:288
bool operator!=(const color &other) const
Definition types.h:297
color(uint8_t col_val)
Definition types.h:281
enum text_art::style::color::kind m_kind
bool operator==(const color &other) const
Definition style.cc:50
Definition types.h:228
bool m_bold
Definition types.h:332
style()
Definition types.h:305
bool operator==(const style &other) const
Definition types.h:315
static void print_changes(pretty_printer *pp, const style &old_style, const style &new_style)
Definition style.cc:178
bool m_underscore
Definition types.h:333
unsigned char id_t
Definition types.h:229
named_color
Definition types.h:234
@ DEFAULT
Definition types.h:235
bool m_blink
Definition types.h:334
color m_bg_color
Definition types.h:337
style & set_style_url(const char *url)
Definition style.cc:39
static const id_t id_plain
Definition types.h:230
std::vector< cppchar_t > m_url
Definition types.h:338
color m_fg_color
Definition types.h:336
bool m_reverse
Definition types.h:335
#define gcc_assert(EXPR)
Definition system.h:819
#define false
Definition system.h:893
struct text_art::style::color::@117042154020367337343243002032317004337077075210::@274032352342362202072121330210337243021024243342 m_named
struct text_art::style::color::@117042154020367337343243002032317004337077075210::@270270064322315304315134350212261177253034247243 m_24bit
const T2 & y
Definition wide-int.h:3870