1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
34 #ifdef GWINSZ_IN_SYS_IOCTL
35 # include <sys/ioctl.h>
38 #include "data/file-name.h"
39 #include "data/file-handle-def.h"
40 #include "data/settings.h"
41 #include "libpspp/assertion.h"
42 #include "libpspp/cast.h"
43 #include "libpspp/compiler.h"
44 #include "libpspp/message.h"
45 #include "libpspp/start-date.h"
46 #include "libpspp/string-map.h"
47 #include "libpspp/u8-line.h"
48 #include "libpspp/version.h"
49 #include "output/ascii.h"
51 #include "output/cairo-chart.h"
53 #include "output/chart-item-provider.h"
54 #include "output/driver-provider.h"
55 #include "output/message-item.h"
56 #include "output/options.h"
57 #include "output/render.h"
58 #include "output/table-item.h"
59 #include "output/text-item.h"
61 #include "gl/minmax.h"
62 #include "gl/xalloc.h"
66 #define _(msgid) gettext (msgid)
68 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
83 ucs4_t c[ASCII_N_LINES][ASCII_N_LINES][ASCII_N_LINES][ASCII_N_LINES];
86 static const struct box_chars *
91 d = ASCII_LINE_DASHED,
92 S = ASCII_LINE_SINGLE,
93 D = ASCII_LINE_DOUBLE,
96 static const struct box_chars ascii_box =
98 /* r b l t: _ d S D */
99 .c[_][_][_] = { ' ', '|', '|', '#', },
100 .c[_][_][d] = { '-', '+', '+', '#', },
101 .c[_][_][S] = { '-', '+', '+', '#', },
102 .c[_][_][D] = { '=', '#', '#', '#', },
103 .c[_][d][_] = { '|', '|', '|', '#', },
104 .c[_][d][d] = { '+', '+', '+', '#', },
105 .c[_][d][S] = { '+', '+', '+', '#', },
106 .c[_][d][D] = { '#', '#', '#', '#', },
107 .c[_][S][_] = { '|', '|', '|', '#', },
108 .c[_][S][d] = { '+', '+', '+', '#', },
109 .c[_][S][S] = { '+', '+', '+', '#', },
110 .c[_][S][D] = { '#', '#', '#', '#', },
111 .c[_][D][_] = { '#', '#', '#', '#', },
112 .c[_][D][d] = { '#', '#', '#', '#', },
113 .c[_][D][S] = { '#', '#', '#', '#', },
114 .c[_][D][D] = { '#', '#', '#', '#', },
115 .c[d][_][_] = { '-', '+', '+', '#', },
116 .c[d][_][d] = { '-', '+', '+', '#', },
117 .c[d][_][S] = { '-', '+', '+', '#', },
118 .c[d][_][D] = { '#', '#', '#', '#', },
119 .c[d][d][_] = { '+', '+', '+', '#', },
120 .c[d][d][d] = { '+', '+', '+', '#', },
121 .c[d][d][S] = { '+', '+', '+', '#', },
122 .c[d][d][D] = { '#', '#', '#', '#', },
123 .c[d][S][_] = { '+', '+', '+', '#', },
124 .c[d][S][d] = { '+', '+', '+', '#', },
125 .c[d][S][S] = { '+', '+', '+', '#', },
126 .c[d][S][D] = { '#', '#', '#', '#', },
127 .c[d][D][_] = { '#', '#', '#', '#', },
128 .c[d][D][d] = { '#', '#', '#', '#', },
129 .c[d][D][S] = { '#', '#', '#', '#', },
130 .c[d][D][D] = { '#', '#', '#', '#', },
131 .c[S][_][_] = { '-', '+', '+', '#', },
132 .c[S][_][d] = { '-', '+', '+', '#', },
133 .c[S][_][S] = { '-', '+', '+', '#', },
134 .c[S][_][D] = { '#', '#', '#', '#', },
135 .c[S][d][_] = { '+', '+', '+', '#', },
136 .c[S][d][d] = { '+', '+', '+', '#', },
137 .c[S][d][S] = { '+', '+', '+', '#', },
138 .c[S][d][D] = { '#', '#', '#', '#', },
139 .c[S][S][_] = { '+', '+', '+', '#', },
140 .c[S][S][d] = { '+', '+', '+', '#', },
141 .c[S][S][S] = { '+', '+', '+', '#', },
142 .c[S][S][D] = { '#', '#', '#', '#', },
143 .c[S][D][_] = { '#', '#', '#', '#', },
144 .c[S][D][d] = { '#', '#', '#', '#', },
145 .c[S][D][S] = { '#', '#', '#', '#', },
146 .c[S][D][D] = { '#', '#', '#', '#', },
147 .c[D][_][_] = { '=', '#', '#', '#', },
148 .c[D][_][d] = { '#', '#', '#', '#', },
149 .c[D][_][S] = { '#', '#', '#', '#', },
150 .c[D][_][D] = { '=', '#', '#', '#', },
151 .c[D][d][_] = { '#', '#', '#', '#', },
152 .c[D][d][d] = { '#', '#', '#', '#', },
153 .c[D][d][S] = { '#', '#', '#', '#', },
154 .c[D][d][D] = { '#', '#', '#', '#', },
155 .c[D][S][_] = { '#', '#', '#', '#', },
156 .c[D][S][d] = { '#', '#', '#', '#', },
157 .c[D][S][S] = { '#', '#', '#', '#', },
158 .c[D][S][D] = { '#', '#', '#', '#', },
159 .c[D][D][_] = { '#', '#', '#', '#', },
160 .c[D][D][d] = { '#', '#', '#', '#', },
161 .c[D][D][S] = { '#', '#', '#', '#', },
162 .c[D][D][D] = { '#', '#', '#', '#', },
167 static const struct box_chars *
168 get_unicode_box (void)
172 d = ASCII_LINE_DASHED,
173 S = ASCII_LINE_SINGLE,
174 D = ASCII_LINE_DOUBLE,
177 static const struct box_chars unicode_box =
179 /* r b l t: _ d S D */
180 .c[_][_][_] = { 0x0020, 0x2575, 0x2575, 0x2551, }, /* ╵╵║ */
181 .c[_][_][d] = { 0x2574, 0x256f, 0x256f, 0x255c, }, /* ╴╯╯╜ */
182 .c[_][_][S] = { 0x2574, 0x256f, 0x256f, 0x255c, }, /* ╴╯╯╜ */
183 .c[_][_][D] = { 0x2550, 0x255b, 0x255b, 0x255d, }, /* ═╛╛╝ */
184 .c[_][S][_] = { 0x2577, 0x2502, 0x2502, 0x2551, }, /* ╷││║ */
185 .c[_][S][d] = { 0x256e, 0x2524, 0x2524, 0x2562, }, /* ╮┤┤╢ */
186 .c[_][S][S] = { 0x256e, 0x2524, 0x2524, 0x2562, }, /* ╮┤┤╢ */
187 .c[_][S][D] = { 0x2555, 0x2561, 0x2561, 0x2563, }, /* ╕╡╡╣ */
188 .c[_][d][_] = { 0x2577, 0x250a, 0x2502, 0x2551, }, /* ╷┊│║ */
189 .c[_][d][d] = { 0x256e, 0x2524, 0x2524, 0x2562, }, /* ╮┤┤╢ */
190 .c[_][d][S] = { 0x256e, 0x2524, 0x2524, 0x2562, }, /* ╮┤┤╢ */
191 .c[_][d][D] = { 0x2555, 0x2561, 0x2561, 0x2563, }, /* ╕╡╡╣ */
192 .c[_][D][_] = { 0x2551, 0x2551, 0x2551, 0x2551, }, /* ║║║║ */
193 .c[_][D][d] = { 0x2556, 0x2562, 0x2562, 0x2562, }, /* ╖╢╢╢ */
194 .c[_][D][S] = { 0x2556, 0x2562, 0x2562, 0x2562, }, /* ╖╢╢╢ */
195 .c[_][D][D] = { 0x2557, 0x2563, 0x2563, 0x2563, }, /* ╗╣╣╣ */
196 .c[d][_][_] = { 0x2576, 0x2570, 0x2570, 0x2559, }, /* ╶╰╰╙ */
197 .c[d][_][d] = { 0x254c, 0x2534, 0x2534, 0x2568, }, /* ╌┴┴╨ */
198 .c[d][_][S] = { 0x2500, 0x2534, 0x2534, 0x2568, }, /* ─┴┴╨ */
199 .c[d][_][D] = { 0x2550, 0x2567, 0x2567, 0x2569, }, /* ═╧╧╩ */
200 .c[d][d][_] = { 0x256d, 0x251c, 0x251c, 0x255f, }, /* ╭├├╟ */
201 .c[d][d][d] = { 0x252c, 0x002b, 0x253c, 0x256a, }, /* ┬+┼╪ */
202 .c[d][d][S] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
203 .c[d][d][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
204 .c[d][S][_] = { 0x256d, 0x251c, 0x251c, 0x255f, }, /* ╭├├╟ */
205 .c[d][S][d] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
206 .c[d][S][S] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
207 .c[d][S][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
208 .c[d][D][_] = { 0x2553, 0x255f, 0x255f, 0x255f, }, /* ╓╟╟╟ */
209 .c[d][D][d] = { 0x2565, 0x256b, 0x256b, 0x256b, }, /* ╥╫╫╫ */
210 .c[d][D][S] = { 0x2565, 0x256b, 0x256b, 0x256b, }, /* ╥╫╫╫ */
211 .c[d][D][D] = { 0x2566, 0x256c, 0x256c, 0x256c, }, /* ╦╬╬╬ */
212 .c[S][_][_] = { 0x2576, 0x2570, 0x2570, 0x2559, }, /* ╶╰╰╙ */
213 .c[S][_][d] = { 0x2500, 0x2534, 0x2534, 0x2568, }, /* ─┴┴╨ */
214 .c[S][_][S] = { 0x2500, 0x2534, 0x2534, 0x2568, }, /* ─┴┴╨ */
215 .c[S][_][D] = { 0x2550, 0x2567, 0x2567, 0x2569, }, /* ═╧╧╩ */
216 .c[S][d][_] = { 0x256d, 0x251c, 0x251c, 0x255f, }, /* ╭├├╟ */
217 .c[S][d][d] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
218 .c[S][d][S] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
219 .c[S][d][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
220 .c[S][S][_] = { 0x256d, 0x251c, 0x251c, 0x255f, }, /* ╭├├╟ */
221 .c[S][S][d] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
222 .c[S][S][S] = { 0x252c, 0x253c, 0x253c, 0x256a, }, /* ┬┼┼╪ */
223 .c[S][S][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
224 .c[S][D][_] = { 0x2553, 0x255f, 0x255f, 0x255f, }, /* ╓╟╟╟ */
225 .c[S][D][d] = { 0x2565, 0x256b, 0x256b, 0x256b, }, /* ╥╫╫╫ */
226 .c[S][D][S] = { 0x2565, 0x256b, 0x256b, 0x256b, }, /* ╥╫╫╫ */
227 .c[S][D][D] = { 0x2566, 0x256c, 0x256c, 0x256c, }, /* ╦╬╬╬ */
228 .c[D][_][_] = { 0x2550, 0x2558, 0x2558, 0x255a, }, /* ═╘╘╚ */
229 .c[D][_][d] = { 0x2550, 0x2567, 0x2567, 0x2569, }, /* ═╧╧╩ */
230 .c[D][_][S] = { 0x2550, 0x2567, 0x2567, 0x2569, }, /* ═╧╧╩ */
231 .c[D][_][D] = { 0x2550, 0x2567, 0x2567, 0x2569, }, /* ═╧╧╩ */
232 .c[D][d][_] = { 0x2552, 0x255e, 0x255e, 0x2560, }, /* ╒╞╞╠ */
233 .c[D][d][d] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
234 .c[D][d][S] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
235 .c[D][d][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
236 .c[D][S][_] = { 0x2552, 0x255e, 0x255e, 0x2560, }, /* ╒╞╞╠ */
237 .c[D][S][d] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
238 .c[D][S][S] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
239 .c[D][S][D] = { 0x2564, 0x256a, 0x256a, 0x256c, }, /* ╤╪╪╬ */
240 .c[D][D][_] = { 0x2554, 0x2560, 0x2560, 0x2560, }, /* ╔╠╠╠ */
241 .c[D][D][d] = { 0x2560, 0x256c, 0x256c, 0x256c, }, /* ╠╬╬╬ */
242 .c[D][D][S] = { 0x2560, 0x256c, 0x256c, 0x256c, }, /* ╠╬╬╬ */
243 .c[D][D][D] = { 0x2566, 0x256c, 0x256c, 0x256c, }, /* ╦╬╬╬ */
249 ascii_line_from_render_line (int render_line)
253 case RENDER_LINE_NONE:
254 return ASCII_LINE_NONE;
256 case RENDER_LINE_DASHED:
257 return ASCII_LINE_DASHED;
259 case RENDER_LINE_SINGLE:
260 case RENDER_LINE_THICK:
261 case RENDER_LINE_THIN:
262 return ASCII_LINE_SINGLE;
264 case RENDER_LINE_DOUBLE:
265 return ASCII_LINE_DOUBLE;
268 return ASCII_LINE_NONE;
274 box_get (const struct box_chars *box,
275 int left_, int right_, int top_, int bottom_)
277 bool rtl = render_direction_rtl ();
278 int left = ascii_line_from_render_line (rtl ? right_ : left_);
279 int right = ascii_line_from_render_line (rtl ? left_ : right_);
280 int top = ascii_line_from_render_line (top_);
281 int bottom = ascii_line_from_render_line (bottom_);
283 return box->c[right][bottom][left][top];
286 /* ASCII output driver. */
289 struct output_driver driver;
291 /* User parameters. */
292 bool append; /* Append if output file already exists? */
293 bool emphasis; /* Enable bold and underline in output? */
294 char *chart_file_name; /* Name of files used for charts. */
297 /* Colours for charts */
298 struct cell_color fg;
299 struct cell_color bg;
302 /* How the page width is determined: */
304 FIXED_WIDTH, /* Specified by configuration. */
305 VIEW_WIDTH, /* From SET WIDTH. */
306 TERMINAL_WIDTH /* From the terminal's width. */
308 int width; /* Page width. */
310 int min_hbreak; /* Min cell size to break across pages. */
312 const struct box_chars *box; /* Line & box drawing characters. */
314 /* Internal state. */
315 struct file_handle *handle;
316 FILE *file; /* Output file. */
317 bool error; /* Output error? */
318 struct u8_line *lines; /* Page content. */
319 int allocated_lines; /* Number of lines allocated. */
320 int chart_cnt; /* Number of charts so far. */
321 int object_cnt; /* Number of objects so far. */
322 struct render_params params;
325 static const struct output_driver_class ascii_driver_class;
327 static void ascii_submit (struct output_driver *, const struct output_item *);
329 static int get_terminal_width (void);
331 static bool update_page_size (struct ascii_driver *, bool issue_error);
332 static int parse_page_size (struct driver_option *);
334 static void ascii_draw_line (void *, int bb[TABLE_N_AXES][2],
335 enum render_line_style styles[TABLE_N_AXES][2],
336 struct cell_color colors[TABLE_N_AXES][2]);
337 static void ascii_measure_cell_width (void *, const struct table_cell *,
339 static int ascii_measure_cell_height (void *, const struct table_cell *,
341 static void ascii_draw_cell (void *, const struct table_cell *, int color_idx,
342 int bb[TABLE_N_AXES][2], int valign_offset,
343 int spill[TABLE_N_AXES][2],
344 int clip[TABLE_N_AXES][2]);
346 static struct ascii_driver *
347 ascii_driver_cast (struct output_driver *driver)
349 assert (driver->class == &ascii_driver_class);
350 return UP_CAST (driver, struct ascii_driver, driver);
353 static struct driver_option *
354 opt (struct output_driver *d, struct string_map *options, const char *key,
355 const char *default_value)
357 return driver_option_get (d, options, key, default_value);
360 /* Return true iff the terminal appears to be an xterm with
361 UTF-8 capabilities */
363 term_is_utf8_xterm (void)
365 const char *term = getenv ("TERM");
366 const char *xterm_locale = getenv ("XTERM_LOCAL");
367 return (term && xterm_locale
368 && !strcmp (term, "xterm")
369 && (strcasestr (xterm_locale, "utf8")
370 || strcasestr (xterm_locale, "utf-8")));
373 static struct output_driver *
374 ascii_create (struct file_handle *fh, enum settings_output_devices device_type,
375 struct string_map *o)
377 enum { BOX_ASCII, BOX_UNICODE } box;
378 struct output_driver *d;
379 struct ascii_driver *a;
381 a = xzalloc (sizeof *a);
383 output_driver_init (&a->driver, &ascii_driver_class, fh_get_file_name (fh), device_type);
384 a->append = parse_boolean (opt (d, o, "append", "false"));
385 a->emphasis = parse_boolean (opt (d, o, "emphasis", "false"));
387 a->chart_file_name = parse_chart_file_name (opt (d, o, "charts", fh_get_file_name (fh)));
391 bool terminal = !strcmp (fh_get_file_name (fh), "-") && isatty (1);
392 a->width = parse_page_size (opt (d, o, "width", "-1"));
393 a->width_mode = (a->width > 0 ? FIXED_WIDTH
394 : terminal ? TERMINAL_WIDTH
396 a->min_hbreak = parse_int (opt (d, o, "min-hbreak", "-1"), -1, INT_MAX);
399 a->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
400 a->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
403 const char *default_box = (terminal && (!strcmp (locale_charset (), "UTF-8")
404 || term_is_utf8_xterm ())
405 ? "unicode" : "ascii");
406 box = parse_enum (opt (d, o, "box", default_box),
408 "unicode", BOX_UNICODE,
410 a->box = box == BOX_ASCII ? get_ascii_box () : get_unicode_box ();
415 a->allocated_lines = 0;
419 static const struct render_ops ascii_render_ops = {
420 .draw_line = ascii_draw_line,
421 .measure_cell_width = ascii_measure_cell_width,
422 .measure_cell_height = ascii_measure_cell_height,
423 .adjust_break = NULL,
424 .draw_cell = ascii_draw_cell,
426 a->params.ops = &ascii_render_ops;
428 a->params.size[H] = a->width;
429 a->params.size[V] = INT_MAX;
430 a->params.font_size[H] = 1;
431 a->params.font_size[V] = 1;
433 static const int ascii_line_widths[RENDER_N_LINES] = {
434 [RENDER_LINE_NONE] = 0,
435 [RENDER_LINE_SINGLE] = 1,
436 [RENDER_LINE_DASHED] = 1,
437 [RENDER_LINE_THICK] = 1,
438 [RENDER_LINE_THIN] = 1,
439 [RENDER_LINE_DOUBLE] = 1,
441 a->params.line_widths = ascii_line_widths;
442 a->params.supports_margins = false;
443 a->params.rtl = render_direction_rtl ();
445 if (!update_page_size (a, true))
448 a->file = fn_open (a->handle, a->append ? "a" : "w");
451 msg_error (errno, _("ascii: opening output file `%s'"),
452 fh_get_file_name (a->handle));
459 output_driver_destroy (d);
464 parse_page_size (struct driver_option *option)
466 int dim = atol (option->default_value);
468 if (option->value != NULL)
470 if (!strcmp (option->value, "auto"))
478 value = strtol (option->value, &tail, 0);
479 if (value >= 1 && errno != ERANGE && *tail == '\0')
482 msg (MW, _("%s: %s must be positive integer or `auto'"),
483 option->driver_name, option->name);
487 driver_option_destroy (option);
492 /* Re-calculates the page width based on settings, margins, and, if "auto" is
493 set, the size of the user's terminal window or GUI output window. */
495 update_page_size (struct ascii_driver *a, bool issue_error)
497 enum { MIN_WIDTH = 6 };
499 int want_width = (a->width_mode == VIEW_WIDTH ? settings_get_viewwidth ()
500 : a->width_mode == TERMINAL_WIDTH ? get_terminal_width ()
502 bool ok = want_width >= MIN_WIDTH;
503 if (!ok && issue_error)
504 msg (ME, _("ascii: page must be at least %d characters wide, but "
505 "as configured is only %d characters"),
506 MIN_WIDTH, want_width);
508 a->width = ok ? want_width : MIN_WIDTH;
509 a->params.size[H] = a->width;
510 a->params.min_break[H] = a->min_hbreak >= 0 ? a->min_hbreak : a->width / 2;
516 ascii_destroy (struct output_driver *driver)
518 struct ascii_driver *a = ascii_driver_cast (driver);
522 fn_close (a->handle, a->file);
523 fh_unref (a->handle);
524 free (a->chart_file_name);
525 for (i = 0; i < a->allocated_lines; i++)
526 u8_line_destroy (&a->lines[i]);
532 ascii_flush (struct output_driver *driver)
534 struct ascii_driver *a = ascii_driver_cast (driver);
540 ascii_output_lines (struct ascii_driver *a, size_t n_lines)
542 for (size_t y = 0; y < n_lines; y++)
544 if (y < a->allocated_lines)
546 struct u8_line *line = &a->lines[y];
548 while (ds_chomp_byte (&line->s, ' '))
550 fwrite (ds_data (&line->s), 1, ds_length (&line->s), a->file);
551 u8_line_clear (&a->lines[y]);
553 putc ('\n', a->file);
558 ascii_output_table_item (struct ascii_driver *a,
559 const struct table_item *table_item)
561 struct render_pager *p;
563 update_page_size (a, false);
566 putc ('\n', a->file);
568 p = render_pager_create (&a->params, table_item);
569 for (int i = 0; render_pager_has_next (p); i++)
572 putc ('\n', a->file);
573 ascii_output_lines (a, render_pager_draw_next (p, INT_MAX));
575 render_pager_destroy (p);
579 ascii_output_table_item_unref (struct ascii_driver *a,
580 struct table_item *table_item)
582 ascii_output_table_item (a, table_item);
583 table_item_unref (table_item);
587 ascii_submit (struct output_driver *driver,
588 const struct output_item *output_item)
590 struct ascii_driver *a = ascii_driver_cast (driver);
595 if (is_table_item (output_item))
596 ascii_output_table_item (a, to_table_item (output_item));
598 else if (is_chart_item (output_item) && a->chart_file_name != NULL)
600 struct chart_item *chart_item = to_chart_item (output_item);
603 file_name = xr_draw_png_chart (chart_item, a->chart_file_name,
607 if (file_name != NULL)
609 struct text_item *text_item;
611 text_item = text_item_create_format (
612 TEXT_ITEM_LOG, _("See %s for a chart."), file_name);
614 ascii_submit (driver, &text_item->output_item);
615 text_item_unref (text_item);
619 #endif /* HAVE_CAIRO */
620 else if (is_text_item (output_item))
622 const struct text_item *text_item = to_text_item (output_item);
623 enum text_item_type type = text_item_get_type (text_item);
625 if (type != TEXT_ITEM_PAGE_TITLE)
626 ascii_output_table_item_unref (
627 a, text_item_to_table_item (text_item_ref (text_item)));
629 else if (is_message_item (output_item))
630 ascii_output_table_item_unref (
631 a, text_item_to_table_item (
632 message_item_to_text_item (
634 output_item_ref (output_item)))));
637 const struct output_driver_factory txt_driver_factory =
638 { "txt", "-", ascii_create };
639 const struct output_driver_factory list_driver_factory =
640 { "list", "-", ascii_create };
642 static const struct output_driver_class ascii_driver_class =
650 static char *ascii_reserve (struct ascii_driver *, int y, int x0, int x1,
652 static void ascii_layout_cell (struct ascii_driver *,
653 const struct table_cell *,
654 int bb[TABLE_N_AXES][2],
655 int clip[TABLE_N_AXES][2],
656 int *width, int *height);
659 ascii_draw_line (void *a_, int bb[TABLE_N_AXES][2],
660 enum render_line_style styles[TABLE_N_AXES][2],
661 struct cell_color colors[TABLE_N_AXES][2] UNUSED)
663 struct ascii_driver *a = a_;
670 /* Clip to the page. */
671 x0 = MAX (bb[H][0], 0);
672 y0 = MAX (bb[V][0], 0);
673 x1 = MIN (bb[H][1], a->width);
675 if (x1 <= 0 || y1 <= 0 || x0 >= a->width)
679 uc = box_get (a->box, styles[V][0], styles[V][1], styles[H][0], styles[H][1]);
680 mblen = u8_uctomb (CHAR_CAST (uint8_t *, mbchar), uc, 6);
681 for (y = y0; y < y1; y++)
683 char *p = ascii_reserve (a, y, x0, x1, mblen * (x1 - x0));
684 for (x = x0; x < x1; x++)
686 memcpy (p, mbchar, mblen);
693 ascii_measure_cell_width (void *a_, const struct table_cell *cell,
694 int *min_width, int *max_width)
696 struct ascii_driver *a = a_;
697 int bb[TABLE_N_AXES][2];
698 int clip[TABLE_N_AXES][2];
705 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
706 ascii_layout_cell (a, cell, bb, clip, max_width, &h);
708 if (cell->n_footnotes || strchr (cell->text, ' ')
709 || cell->n_subscripts)
712 ascii_layout_cell (a, cell, bb, clip, min_width, &h);
715 *min_width = *max_width;
719 ascii_measure_cell_height (void *a_, const struct table_cell *cell, int width)
721 struct ascii_driver *a = a_;
722 int bb[TABLE_N_AXES][2];
723 int clip[TABLE_N_AXES][2];
730 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
731 ascii_layout_cell (a, cell, bb, clip, &w, &h);
736 ascii_draw_cell (void *a_, const struct table_cell *cell, int color_idx UNUSED,
737 int bb[TABLE_N_AXES][2], int valign_offset,
738 int spill[TABLE_N_AXES][2] UNUSED,
739 int clip[TABLE_N_AXES][2])
741 struct ascii_driver *a = a_;
744 bb[V][0] += valign_offset;
745 ascii_layout_cell (a, cell, bb, clip, &w, &h);
749 ascii_reserve (struct ascii_driver *a, int y, int x0, int x1, int n)
751 if (y >= a->allocated_lines)
753 size_t new_alloc = MAX (25, a->allocated_lines);
754 while (new_alloc <= y)
755 new_alloc = xtimes (new_alloc, 2);
756 a->lines = xnrealloc (a->lines, new_alloc, sizeof *a->lines);
757 for (size_t i = a->allocated_lines; i < new_alloc; i++)
758 u8_line_init (&a->lines[i]);
759 a->allocated_lines = new_alloc;
761 return u8_line_reserve (&a->lines[y], x0, x1, n);
765 text_draw (struct ascii_driver *a, enum table_halign halign, int options,
766 bool bold, bool underline,
767 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
768 int y, const uint8_t *string, int n, size_t width)
770 int x0 = MAX (0, clip[H][0]);
771 int y0 = MAX (0, clip[V][0]);
772 int x1 = MIN (a->width, clip[H][1]);
776 if (y < y0 || y >= y1)
779 switch (table_halign_interpret (halign, options & TAB_NUMERIC))
781 case TABLE_HALIGN_LEFT:
784 case TABLE_HALIGN_CENTER:
785 x = (bb[H][0] + bb[H][1] - width + 1) / 2;
787 case TABLE_HALIGN_RIGHT:
788 case TABLE_HALIGN_DECIMAL:
789 x = bb[H][1] - width;
805 mblen = u8_mbtouc (&uc, string, n);
810 w = uc_width (uc, "UTF-8");
825 for (ofs = 0; ofs < n;)
831 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
833 w = uc_width (uc, "UTF-8");
836 if (width + w > x1 - x)
847 if (!a->emphasis || (!bold && !underline))
848 memcpy (ascii_reserve (a, y, x, x + width, n), string, n);
856 /* First figure out how many bytes need to be inserted. */
858 for (ofs = 0; ofs < n; ofs += mblen)
863 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
864 w = uc_width (uc, "UTF-8");
875 /* Then insert them. */
876 out = ascii_reserve (a, y, x, x + width, n_out);
877 for (ofs = 0; ofs < n; ofs += mblen)
882 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
883 w = uc_width (uc, "UTF-8");
889 out = mempcpy (out, string + ofs, mblen);
898 out = mempcpy (out, string + ofs, mblen);
904 add_markers (const char *text, const struct table_cell *cell)
906 struct string s = DS_EMPTY_INITIALIZER;
907 ds_put_cstr (&s, text);
908 for (size_t i = 0; i < cell->n_subscripts; i++)
909 ds_put_format (&s, "%c%s", i ? ',' : '_', cell->subscripts[i]);
910 for (size_t i = 0; i < cell->n_footnotes; i++)
911 ds_put_format (&s, "[%s]", cell->footnotes[i]->marker);
912 return ds_steal_cstr (&s);
916 ascii_layout_cell (struct ascii_driver *a, const struct table_cell *cell,
917 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
918 int *widthp, int *heightp)
923 /* Get the basic textual contents. */
924 const char *plain_text = (cell->options & TAB_MARKUP
925 ? output_get_text_from_markup (cell->text)
928 /* Append footnotes, subscripts if any. */
930 if (cell->n_footnotes || cell->n_subscripts)
932 text = add_markers (plain_text, cell);
933 if (plain_text != cell->text)
934 free (CONST_CAST (char *, plain_text));
939 /* Calculate length; if it's zero, then there's nothing to do. */
940 size_t length = strlen (text);
943 if (text != cell->text)
944 free (CONST_CAST (char *, text));
948 char *breaks = xmalloc (length + 1);
949 u8_possible_linebreaks (CHAR_CAST (const uint8_t *, text), length,
951 breaks[length] = (breaks[length - 1] == UC_BREAK_MANDATORY
952 ? UC_BREAK_PROHIBITED : UC_BREAK_POSSIBLE);
955 int bb_width = bb[H][1] - bb[H][0];
956 for (int y = bb[V][0]; y < bb[V][1] && pos < length; y++)
958 const uint8_t *line = CHAR_CAST (const uint8_t *, text + pos);
959 const char *b = breaks + pos;
960 size_t n = length - pos;
962 size_t last_break_ofs = 0;
963 int last_break_width = 0;
968 for (ofs = 0; ofs < n;)
974 mblen = u8_mbtouc (&uc, line + ofs, n - ofs);
975 if (b[ofs] == UC_BREAK_MANDATORY)
977 else if (b[ofs] == UC_BREAK_POSSIBLE)
979 last_break_ofs = ofs;
980 last_break_width = width;
983 w = uc_width (uc, "UTF-8");
986 if (width + w > bb_width)
988 if (isspace (line[ofs]))
990 else if (last_break_ofs != 0)
992 ofs = last_break_ofs;
993 width = last_break_width;
1002 /* Trim any trailing spaces off the end of the text to be drawn. */
1003 for (graph_ofs = ofs; graph_ofs > 0; graph_ofs--)
1004 if (!isspace (line[graph_ofs - 1]))
1006 width -= ofs - graph_ofs;
1009 text_draw (a, cell->style->cell_style.halign, cell->options,
1010 cell->style->font_style.bold,
1011 cell->style->font_style.underline,
1012 bb, clip, y, line, graph_ofs, width);
1014 /* If a new-line ended the line, just skip the new-line. Otherwise, skip
1015 past any spaces past the end of the line (but not past a new-line). */
1016 if (b[ofs] == UC_BREAK_MANDATORY)
1019 while (ofs < n && isspace (line[ofs]) && b[ofs] != UC_BREAK_MANDATORY)
1022 if (width > *widthp)
1029 if (text != cell->text)
1030 free (CONST_CAST (char *, text));
1034 ascii_test_write (struct output_driver *driver,
1035 const char *s, int x, int y, bool bold, bool underline)
1037 struct ascii_driver *a = ascii_driver_cast (driver);
1038 int bb[TABLE_N_AXES][2];
1044 struct table_area_style style = {
1045 .cell_style.halign = TABLE_HALIGN_LEFT,
1046 .font_style.bold = bold,
1047 .font_style.underline = underline,
1049 struct table_cell cell = {
1050 .text = CONST_CAST (char *, s),
1054 bb[TABLE_HORZ][0] = x;
1055 bb[TABLE_HORZ][1] = a->width;
1056 bb[TABLE_VERT][0] = y;
1057 bb[TABLE_VERT][1] = INT_MAX;
1059 ascii_layout_cell (a, &cell, bb, bb, &width, &height);
1063 ascii_test_set_length (struct output_driver *driver, int y, int length)
1065 struct ascii_driver *a = ascii_driver_cast (driver);
1072 u8_line_set_length (&a->lines[y], length);
1076 ascii_test_flush (struct output_driver *driver)
1078 struct ascii_driver *a = ascii_driver_cast (driver);
1080 for (size_t i = a->allocated_lines; i-- > 0;)
1081 if (a->lines[i].width)
1083 ascii_output_lines (a, i + 1);
1088 static sig_atomic_t terminal_changed = true;
1089 static int terminal_width;
1091 #if HAVE_DECL_SIGWINCH
1093 winch_handler (int signum UNUSED)
1095 terminal_changed = true;
1100 get_terminal_width (void)
1102 #if HAVE_DECL_SIGWINCH
1103 static bool setup_signal;
1106 setup_signal = true;
1108 struct sigaction action = { .sa_handler = winch_handler };
1109 sigemptyset (&action.sa_mask);
1110 sigaction (SIGWINCH, &action, NULL);
1114 if (terminal_changed)
1116 terminal_changed = false;
1118 #ifdef HAVE_TERMIOS_H
1120 if (!ioctl (0, TIOCGWINSZ, &ws))
1121 terminal_width = ws.ws_col;
1125 if (getenv ("COLUMNS"))
1126 terminal_width = atoi (getenv ("COLUMNS"));
1129 if (terminal_width <= 0 || terminal_width > 1024)
1130 terminal_width = 79;
1133 return terminal_width;