1/* Classes for abstracting ascii vs unicode output.
2 Copyright (C) 2023-2024 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 under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for 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#include "config.h"
22#define INCLUDE_VECTOR
23#include "system.h"
24#include "coretypes.h"
25#include "pretty-print.h"
26#include "selftest.h"
27#include "text-art/selftests.h"
28#include "text-art/ruler.h"
29#include "text-art/theme.h"
30
31using namespace text_art;
32
33/* class theme. */
34
35void
36theme::paint_y_arrow (canvas &canvas,
37 int canvas_x,
38 canvas::range_t y_range,
39 y_arrow_dir dir,
40 style::id_t style_id) const
41{
42 int canvas_y;
43 int delta_y;
44 const canvas::cell_t head (get_cppchar (kind: dir == y_arrow_dir::UP
45 ? cell_kind::Y_ARROW_UP_HEAD
46 : cell_kind::Y_ARROW_DOWN_HEAD),
47 false, style_id);
48 const canvas::cell_t tail (get_cppchar (kind: dir == y_arrow_dir::UP
49 ? cell_kind::Y_ARROW_UP_TAIL
50 : cell_kind::Y_ARROW_DOWN_TAIL),
51 false, style_id);
52 if (dir == y_arrow_dir::UP)
53 {
54 canvas_y = y_range.get_max ();
55 delta_y = -1;
56 }
57 else
58 {
59 canvas_y = y_range.get_min ();
60 delta_y = 1;
61 }
62 for (int len = y_range.get_size (); len; len--)
63 {
64 const canvas::cell_t cell = (len > 1) ? tail : head;
65 canvas.paint (coord: canvas::coord_t (canvas_x, canvas_y), c: cell);
66 canvas_y += delta_y;
67 }
68}
69
70/* class ascii_theme : public theme. */
71
72canvas::cell_t
73ascii_theme::get_line_art (directions line_dirs) const
74{
75 if (line_dirs.m_up
76 && line_dirs.m_down
77 && !(line_dirs.m_left || line_dirs.m_right))
78 return canvas::cell_t ('|');
79 if (line_dirs.m_left
80 && line_dirs.m_right
81 && !(line_dirs.m_up || line_dirs.m_down))
82 return canvas::cell_t ('-');
83 if (line_dirs.m_up
84 || line_dirs.m_down
85 || line_dirs.m_left
86 || line_dirs.m_right)
87 return canvas::cell_t ('+');
88 return canvas::cell_t (' ');
89}
90
91cppchar_t
92ascii_theme::get_cppchar (enum cell_kind kind) const
93{
94 switch (kind)
95 {
96 default:
97 gcc_unreachable ();
98 case cell_kind::X_RULER_LEFT_EDGE:
99 return '|';
100 case cell_kind::X_RULER_MIDDLE:
101 return '~';
102 case cell_kind::X_RULER_INTERNAL_EDGE:
103 return '|';
104 case cell_kind::X_RULER_CONNECTOR_TO_LABEL_BELOW:
105 case cell_kind::X_RULER_CONNECTOR_TO_LABEL_ABOVE:
106 return '+';
107 case cell_kind::X_RULER_RIGHT_EDGE:
108 return '|';
109 case cell_kind::X_RULER_VERTICAL_CONNECTOR:
110 return '|';
111
112 case cell_kind::TEXT_BORDER_HORIZONTAL:
113 return '-';
114 case cell_kind::TEXT_BORDER_VERTICAL:
115 return '|';
116 case cell_kind::TEXT_BORDER_TOP_LEFT:
117 case cell_kind::TEXT_BORDER_TOP_RIGHT:
118 case cell_kind::TEXT_BORDER_BOTTOM_LEFT:
119 case cell_kind::TEXT_BORDER_BOTTOM_RIGHT:
120 return '+';
121
122 case cell_kind::Y_ARROW_UP_HEAD: return '^';
123 case cell_kind::Y_ARROW_DOWN_HEAD: return 'v';
124
125 case cell_kind::Y_ARROW_UP_TAIL:
126 case cell_kind::Y_ARROW_DOWN_TAIL:
127 return '|';
128 }
129}
130
131/* class unicode_theme : public theme. */
132
133canvas::cell_t
134unicode_theme::get_line_art (directions line_dirs) const
135{
136 return canvas::cell_t (get_box_drawing_char (line_dirs));
137}
138
139cppchar_t
140unicode_theme::get_cppchar (enum cell_kind kind) const
141{
142 switch (kind)
143 {
144 default:
145 gcc_unreachable ();
146 case cell_kind::X_RULER_LEFT_EDGE:
147 return 0x251C; /* "├": U+251C: BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
148 case cell_kind::X_RULER_MIDDLE:
149 return 0x2500; /* "─": U+2500: BOX DRAWINGS LIGHT HORIZONTAL */
150 case cell_kind::X_RULER_INTERNAL_EDGE:
151 return 0x253C; /* "┼": U+253C: BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */
152 case cell_kind::X_RULER_CONNECTOR_TO_LABEL_BELOW:
153 return 0x252C; /* "┬": U+252C: BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */
154 case cell_kind::X_RULER_CONNECTOR_TO_LABEL_ABOVE:
155 return 0x2534; /* "┴": U+2534: BOX DRAWINGS LIGHT UP AND HORIZONTAL */
156 case cell_kind::X_RULER_RIGHT_EDGE:
157 return 0x2524; /* "┤": U+2524: BOX DRAWINGS LIGHT VERTICAL AND LEFT */
158 case cell_kind::X_RULER_VERTICAL_CONNECTOR:
159 return 0x2502; /* "│": U+2502: BOX DRAWINGS LIGHT VERTICAL */
160
161 case cell_kind::TEXT_BORDER_HORIZONTAL:
162 return 0x2500; /* "─": U+2500: BOX DRAWINGS LIGHT HORIZONTAL */
163 case cell_kind::TEXT_BORDER_VERTICAL:
164 return 0x2502; /* "│": U+2502: BOX DRAWINGS LIGHT VERTICAL */
165
166 /* Round corners. */
167 case cell_kind::TEXT_BORDER_TOP_LEFT:
168 return 0x256D; /* "╭": U+256D BOX DRAWINGS LIGHT ARC DOWN AND RIGHT. */
169 case cell_kind::TEXT_BORDER_TOP_RIGHT:
170 return 0x256E; /* "╮": U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT. */
171 case cell_kind::TEXT_BORDER_BOTTOM_LEFT:
172 return 0x2570; /* "╰": U+2570 BOX DRAWINGS LIGHT ARC UP AND RIGHT. */
173 case cell_kind::TEXT_BORDER_BOTTOM_RIGHT:
174 return 0x256F; /* "╯": U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT. */
175
176 case cell_kind::Y_ARROW_UP_HEAD:
177 return '^';
178 case cell_kind::Y_ARROW_DOWN_HEAD:
179 return 'v';
180 case cell_kind::Y_ARROW_UP_TAIL:
181 case cell_kind::Y_ARROW_DOWN_TAIL:
182 return 0x2502; /* "│": U+2502: BOX DRAWINGS LIGHT VERTICAL */
183 }
184}
185

source code of gcc/text-art/theme.cc