/* Types for drawing 2d "text art". Copyright (C) 2023-2025 Free Software Foundation, Inc. Contributed by David Malcolm . This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see . */ #ifndef GCC_TEXT_ART_TYPES_H #define GCC_TEXT_ART_TYPES_H /* This header uses std::vector, but can't be directly included due to issues with macros. Hence it must be included from system.h by defining INCLUDE_VECTOR in any source file using it. */ #ifndef INCLUDE_VECTOR # error "You must define INCLUDE_VECTOR before including system.h to use text-art/types.h" #endif #include "cpplib.h" #include "pretty-print.h" namespace text_art { /* Forward decls. */ class canvas; class table; class theme; /* Classes for geometry. We use templates to avoid mixing up e.g. canvas coordinates with table coordinates. */ template struct size { size (int w_, int h_) : w (w_), h (h_) {} int w; int h; }; template struct coord { coord (int x_, int y_) : x (x_), y (y_) {} int x; int y; }; template coord operator+ (coord a, coord b) { return coord (a.x + b.x, a.y + b.y); } /* A half-open range [start, next) of int. */ template struct range { range (int start_, int next_) : start (start_), next (next_) {} int get_min () const { return start; } int get_max () const { return next - 1; } int get_next () const { return next; } int get_size () const { return next - start; } int get_midpoint () const { return get_min () + get_size () / 2; } int start; int next; }; /* A rectangle area within CoordinateSystem. */ template struct rect { rect (coord top_left, size size) : m_top_left (top_left), m_size (size) { } rect (range x_range, range y_range) : m_top_left (x_range.get_min (), y_range.get_min ()), m_size (x_range.get_size (), y_range.get_size ()) { } int get_min_x () const { return m_top_left.x; } int get_min_y () const { return m_top_left.y; } int get_max_x () const { return m_top_left.x + m_size.w - 1; } int get_max_y () const { return m_top_left.y + m_size.h - 1; } int get_next_x () const { return m_top_left.x + m_size.w; } int get_next_y () const { return m_top_left.y + m_size.h; } range get_x_range () const { return range (get_min_x (), get_next_x ()); } range get_y_range () const { return range (get_min_y (), get_next_y ()); } int get_width () const { return m_size.w; } int get_height () const { return m_size.h; } coord m_top_left; size m_size; }; template rect operator+ (rect r, coord offset) { return rect (r.m_top_left + offset, r.m_size); } template class array2 { public: typedef ElementType element_t; typedef SizeType size_t; typedef CoordType coord_t; array2 (size_t sz) : m_size (sz), m_elements (sz.w * sz.h) { } array2 (array2 &&other) : m_size (other.m_size), m_elements (std::move (other.m_elements)) { } /* Move assignment not implemented or used. */ array2 &operator== (array2 &&other) = delete; /* No copy ctor or assignment op. */ array2 (const array2 &other) = delete; array2 &operator= (const array2 &other) = delete; const size_t &get_size () const { return m_size; } void add_row (const element_t &element) { m_size.h++; m_elements.insert (m_elements.end (), m_size.w, element); } const element_t &get (const coord_t &coord) const { ::size_t idx = get_idx (coord); return m_elements[idx]; } void set (const coord_t &coord, const element_t &element) { ::size_t idx = get_idx (coord); m_elements[idx] = element; } void fill (element_t element) { for (int y = 0; y < m_size.h; y++) for (int x = 0; x < m_size.w; x++) set (coord_t (x, y), element); } private: ::size_t get_idx (const coord_t &coord) const { gcc_assert (coord.x >= 0); gcc_assert (coord.x < m_size.w); gcc_assert (coord.y >= 0); gcc_assert (coord.y < m_size.h); return (coord.y * m_size.w) + coord.x; } size_t m_size; std::vector m_elements; }; /* A combination of attributes describing how to style a text cell. We only support those attributes mentioned in invoke.texi: - bold, - underscore, - blink, - inverse, - colors for foreground and background: - default color - named colors - 16-color mode colors (the "bright" variants) - 88-color mode - 256-color mode plus URLs. */ struct style { typedef unsigned char id_t; static const id_t id_plain = 0; /* Colors. */ enum class named_color { DEFAULT, // ANSI order BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; struct color { enum class kind { NAMED, BITS_8, BITS_24, } m_kind; union { struct { enum named_color m_name; bool m_bright; } m_named; uint8_t m_8bit; struct { uint8_t r; uint8_t g; uint8_t b; } m_24bit; } u; /* Constructor for named colors. */ color (enum named_color name = named_color::DEFAULT, bool bright = false) : m_kind (kind::NAMED) { u.m_named.m_name = name; u.m_named.m_bright = bright; } /* Constructor for 8-bit colors. */ color (uint8_t col_val) : m_kind (kind::BITS_8) { u.m_8bit = col_val; } /* Constructor for 24-bit colors. */ color (uint8_t r, uint8_t g, uint8_t b) : m_kind (kind::BITS_24) { u.m_24bit.r = r; u.m_24bit.g = g; u.m_24bit.b = b; } bool operator== (const color &other) const; bool operator!= (const color &other) const { return !(*this == other); } void print_sgr (pretty_printer *pp, bool fg, bool &need_separator) const; }; style () : m_bold (false), m_underscore (false), m_blink (false), m_reverse (false), m_fg_color (named_color::DEFAULT), m_bg_color (named_color::DEFAULT), m_url () {} bool operator== (const style &other) const { return (m_bold == other.m_bold && m_underscore == other.m_underscore && m_blink == other.m_blink && m_reverse == other.m_reverse && m_fg_color == other.m_fg_color && m_bg_color == other.m_bg_color && m_url == other.m_url); } style &set_style_url (const char *url); static void print_changes (pretty_printer *pp, const style &old_style, const style &new_style); bool m_bold; bool m_underscore; bool m_blink; bool m_reverse; color m_fg_color; color m_bg_color; std::vector m_url; // empty = no URL }; extern style get_style_from_color_cap_name (const char *name); /* A class to keep track of all the styles in use in a drawing, so that we can refer to them via the compact style::id_t type, rather than via e.g. pointers. */ class style_manager { public: style_manager (); style::id_t get_or_create_id (const style &style); const style &get_style (style::id_t id) const { return m_styles[id]; } void print_any_style_changes (pretty_printer *pp, style::id_t old_id, style::id_t new_id) const; unsigned get_num_styles () const { return m_styles.size (); } private: std::vector