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. */
82 ucs4_t c[ASCII_N_LINES][ASCII_N_LINES][ASCII_N_LINES][ASCII_N_LINES];
85 static const struct box_chars *
90 S = ASCII_LINE_SINGLE,
91 D = ASCII_LINE_DOUBLE,
94 static const struct box_chars ascii_box =
97 .c[_][_][_] = { ' ', '|', '#', },
98 .c[_][_][S] = { '-', '+', '#', },
99 .c[_][_][D] = { '=', '#', '#', },
100 .c[_][S][_] = { '|', '|', '#', },
101 .c[_][S][S] = { '+', '+', '#', },
102 .c[_][S][D] = { '#', '#', '#', },
103 .c[_][D][_] = { '#', '#', '#', },
104 .c[_][D][S] = { '#', '#', '#', },
105 .c[_][D][D] = { '#', '#', '#', },
106 .c[S][_][_] = { '-', '+', '#', },
107 .c[S][_][S] = { '-', '+', '#', },
108 .c[S][_][D] = { '#', '#', '#', },
109 .c[S][S][_] = { '+', '+', '#', },
110 .c[S][S][S] = { '+', '+', '#', },
111 .c[S][S][D] = { '#', '#', '#', },
112 .c[S][D][_] = { '#', '#', '#', },
113 .c[S][D][S] = { '#', '#', '#', },
114 .c[S][D][D] = { '#', '#', '#', },
115 .c[D][_][_] = { '=', '#', '#', },
116 .c[D][_][S] = { '#', '#', '#', },
117 .c[D][_][D] = { '=', '#', '#', },
118 .c[D][S][_] = { '#', '#', '#', },
119 .c[D][S][S] = { '#', '#', '#', },
120 .c[D][S][D] = { '#', '#', '#', },
121 .c[D][D][_] = { '#', '#', '#', },
122 .c[D][D][S] = { '#', '#', '#', },
123 .c[D][D][D] = { '#', '#', '#', },
128 static const struct box_chars *
129 get_unicode_box (void)
133 S = ASCII_LINE_SINGLE,
134 D = ASCII_LINE_DOUBLE,
137 static const struct box_chars unicode_box =
140 .c[_][_][_] = { 0x0020, 0x2575, 0x2551, }, /* ╵║ */
141 .c[_][_][S] = { 0x2574, 0x256f, 0x255c, }, /* ╴╯╜ */
142 .c[_][_][D] = { 0x2550, 0x255b, 0x255d, }, /* ═╛╝ */
143 .c[_][S][_] = { 0x2577, 0x2502, 0x2551, }, /* ╷│║ */
144 .c[_][S][S] = { 0x256e, 0x2524, 0x2562, }, /* ╮┤╢ */
145 .c[_][S][D] = { 0x2555, 0x2561, 0x2563, }, /* ╕╡╣ */
146 .c[_][D][_] = { 0x2551, 0x2551, 0x2551, }, /* ║║║ */
147 .c[_][D][S] = { 0x2556, 0x2562, 0x2562, }, /* ╖╢╢ */
148 .c[_][D][D] = { 0x2557, 0x2563, 0x2563, }, /* ╗╣╣ */
149 .c[S][_][_] = { 0x2576, 0x2570, 0x2559, }, /* ╶╰╙ */
150 .c[S][_][S] = { 0x2500, 0x2534, 0x2568, }, /* ─┴╨ */
151 .c[S][_][D] = { 0x2550, 0x2567, 0x2569, }, /* ═╧╩ */
152 .c[S][S][_] = { 0x256d, 0x251c, 0x255f, }, /* ╭├╟ */
153 .c[S][S][S] = { 0x252c, 0x253c, 0x256a, }, /* ┬┼╪ */
154 .c[S][S][D] = { 0x2564, 0x256a, 0x256c, }, /* ╤╪╬ */
155 .c[S][D][_] = { 0x2553, 0x255f, 0x255f, }, /* ╓╟╟ */
156 .c[S][D][S] = { 0x2565, 0x256b, 0x256b, }, /* ╥╫╫ */
157 .c[S][D][D] = { 0x2566, 0x256c, 0x256c, }, /* ╦╬╬ */
158 .c[D][_][_] = { 0x2550, 0x2558, 0x255a, }, /* ═╘╚ */
159 .c[D][_][S] = { 0x2550, 0x2567, 0x2569, }, /* ═╧╩ */
160 .c[D][_][D] = { 0x2550, 0x2567, 0x2569, }, /* ═╧╩ */
161 .c[D][S][_] = { 0x2552, 0x255e, 0x2560, }, /* ╒╞╠ */
162 .c[D][S][S] = { 0x2564, 0x256a, 0x256c, }, /* ╤╪╬ */
163 .c[D][S][D] = { 0x2564, 0x256a, 0x256c, }, /* ╤╪╬ */
164 .c[D][D][_] = { 0x2554, 0x2560, 0x2560, }, /* ╔╠╠ */
165 .c[D][D][S] = { 0x2560, 0x256c, 0x256c, }, /* ╠╬╬ */
166 .c[D][D][D] = { 0x2566, 0x256c, 0x256c, }, /* ╦╬╬ */
172 ascii_line_from_render_line (int render_line)
176 case RENDER_LINE_NONE:
177 return ASCII_LINE_NONE;
179 case RENDER_LINE_SINGLE:
180 case RENDER_LINE_DASHED:
181 case RENDER_LINE_THICK:
182 case RENDER_LINE_THIN:
183 return ASCII_LINE_SINGLE;
185 case RENDER_LINE_DOUBLE:
186 return ASCII_LINE_DOUBLE;
189 return ASCII_LINE_NONE;
195 box_get (const struct box_chars *box,
196 int left_, int right_, int top_, int bottom_)
198 bool rtl = render_direction_rtl ();
199 int left = ascii_line_from_render_line (rtl ? right_ : left_);
200 int right = ascii_line_from_render_line (rtl ? left_ : right_);
201 int top = ascii_line_from_render_line (top_);
202 int bottom = ascii_line_from_render_line (bottom_);
204 return box->c[right][bottom][left][top];
207 /* ASCII output driver. */
210 struct output_driver driver;
212 /* User parameters. */
213 bool append; /* Append if output file already exists? */
214 bool emphasis; /* Enable bold and underline in output? */
215 char *chart_file_name; /* Name of files used for charts. */
218 /* Colours for charts */
219 struct cell_color fg;
220 struct cell_color bg;
223 /* How the page width is determined: */
225 FIXED_WIDTH, /* Specified by configuration. */
226 VIEW_WIDTH, /* From SET WIDTH. */
227 TERMINAL_WIDTH /* From the terminal's width. */
229 int width; /* Page width. */
231 int min_hbreak; /* Min cell size to break across pages. */
233 const struct box_chars *box; /* Line & box drawing characters. */
235 /* Internal state. */
236 struct file_handle *handle;
237 FILE *file; /* Output file. */
238 bool error; /* Output error? */
239 struct u8_line *lines; /* Page content. */
240 int allocated_lines; /* Number of lines allocated. */
241 int chart_cnt; /* Number of charts so far. */
242 int object_cnt; /* Number of objects so far. */
243 struct render_params params;
246 static const struct output_driver_class ascii_driver_class;
248 static void ascii_submit (struct output_driver *, const struct output_item *);
250 static int get_terminal_width (void);
252 static bool update_page_size (struct ascii_driver *, bool issue_error);
253 static int parse_page_size (struct driver_option *);
255 static void ascii_draw_line (void *, int bb[TABLE_N_AXES][2],
256 enum render_line_style styles[TABLE_N_AXES][2],
257 struct cell_color colors[TABLE_N_AXES][2]);
258 static void ascii_measure_cell_width (void *, const struct table_cell *,
260 static int ascii_measure_cell_height (void *, const struct table_cell *,
262 static void ascii_draw_cell (void *, const struct table_cell *, int color_idx,
263 int bb[TABLE_N_AXES][2], int valign_offset,
264 int spill[TABLE_N_AXES][2],
265 int clip[TABLE_N_AXES][2]);
267 static struct ascii_driver *
268 ascii_driver_cast (struct output_driver *driver)
270 assert (driver->class == &ascii_driver_class);
271 return UP_CAST (driver, struct ascii_driver, driver);
274 static struct driver_option *
275 opt (struct output_driver *d, struct string_map *options, const char *key,
276 const char *default_value)
278 return driver_option_get (d, options, key, default_value);
281 /* Return true iff the terminal appears to be an xterm with
282 UTF-8 capabilities */
284 term_is_utf8_xterm (void)
286 const char *term = getenv ("TERM");
287 const char *xterm_locale = getenv ("XTERM_LOCAL");
288 return (term && xterm_locale
289 && !strcmp (term, "xterm")
290 && (strcasestr (xterm_locale, "utf8")
291 || strcasestr (xterm_locale, "utf-8")));
294 static struct output_driver *
295 ascii_create (struct file_handle *fh, enum settings_output_devices device_type,
296 struct string_map *o)
298 enum { BOX_ASCII, BOX_UNICODE } box;
299 struct output_driver *d;
300 struct ascii_driver *a;
302 a = xzalloc (sizeof *a);
304 output_driver_init (&a->driver, &ascii_driver_class, fh_get_file_name (fh), device_type);
305 a->append = parse_boolean (opt (d, o, "append", "false"));
306 a->emphasis = parse_boolean (opt (d, o, "emphasis", "false"));
308 a->chart_file_name = parse_chart_file_name (opt (d, o, "charts", fh_get_file_name (fh)));
312 bool terminal = !strcmp (fh_get_file_name (fh), "-") && isatty (1);
313 a->width = parse_page_size (opt (d, o, "width", "-1"));
314 a->width_mode = (a->width > 0 ? FIXED_WIDTH
315 : terminal ? TERMINAL_WIDTH
317 a->min_hbreak = parse_int (opt (d, o, "min-hbreak", "-1"), -1, INT_MAX);
320 a->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
321 a->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
324 const char *default_box = (terminal && (!strcmp (locale_charset (), "UTF-8")
325 || term_is_utf8_xterm ())
326 ? "unicode" : "ascii");
327 box = parse_enum (opt (d, o, "box", default_box),
329 "unicode", BOX_UNICODE,
331 a->box = box == BOX_ASCII ? get_ascii_box () : get_unicode_box ();
336 a->allocated_lines = 0;
340 static const struct render_ops ascii_render_ops = {
341 .draw_line = ascii_draw_line,
342 .measure_cell_width = ascii_measure_cell_width,
343 .measure_cell_height = ascii_measure_cell_height,
344 .adjust_break = NULL,
345 .draw_cell = ascii_draw_cell,
347 a->params.ops = &ascii_render_ops;
349 a->params.size[H] = a->width;
350 a->params.size[V] = INT_MAX;
351 a->params.font_size[H] = 1;
352 a->params.font_size[V] = 1;
354 static const int ascii_line_widths[RENDER_N_LINES] = {
355 [RENDER_LINE_NONE] = 0,
356 [RENDER_LINE_SINGLE] = 1,
357 [RENDER_LINE_DASHED] = 1,
358 [RENDER_LINE_THICK] = 1,
359 [RENDER_LINE_THIN] = 1,
360 [RENDER_LINE_DOUBLE] = 1,
362 a->params.line_widths = ascii_line_widths;
363 a->params.supports_margins = false;
364 a->params.rtl = render_direction_rtl ();
366 if (!update_page_size (a, true))
369 a->file = fn_open (a->handle, a->append ? "a" : "w");
372 msg_error (errno, _("ascii: opening output file `%s'"),
373 fh_get_file_name (a->handle));
380 output_driver_destroy (d);
385 parse_page_size (struct driver_option *option)
387 int dim = atol (option->default_value);
389 if (option->value != NULL)
391 if (!strcmp (option->value, "auto"))
399 value = strtol (option->value, &tail, 0);
400 if (value >= 1 && errno != ERANGE && *tail == '\0')
403 msg (MW, _("%s: %s must be positive integer or `auto'"),
404 option->driver_name, option->name);
408 driver_option_destroy (option);
413 /* Re-calculates the page width based on settings, margins, and, if "auto" is
414 set, the size of the user's terminal window or GUI output window. */
416 update_page_size (struct ascii_driver *a, bool issue_error)
418 enum { MIN_WIDTH = 6 };
420 int want_width = (a->width_mode == VIEW_WIDTH ? settings_get_viewwidth ()
421 : a->width_mode == TERMINAL_WIDTH ? get_terminal_width ()
423 bool ok = want_width >= MIN_WIDTH;
424 if (!ok && issue_error)
425 msg (ME, _("ascii: page must be at least %d characters wide, but "
426 "as configured is only %d characters"),
427 MIN_WIDTH, want_width);
429 a->width = ok ? want_width : MIN_WIDTH;
430 a->params.size[H] = a->width;
431 a->params.min_break[H] = a->min_hbreak >= 0 ? a->min_hbreak : a->width / 2;
437 ascii_destroy (struct output_driver *driver)
439 struct ascii_driver *a = ascii_driver_cast (driver);
443 fn_close (a->handle, a->file);
444 fh_unref (a->handle);
445 free (a->chart_file_name);
446 for (i = 0; i < a->allocated_lines; i++)
447 u8_line_destroy (&a->lines[i]);
453 ascii_flush (struct output_driver *driver)
455 struct ascii_driver *a = ascii_driver_cast (driver);
461 ascii_output_lines (struct ascii_driver *a, size_t n_lines)
463 for (size_t y = 0; y < n_lines; y++)
465 if (y < a->allocated_lines)
467 struct u8_line *line = &a->lines[y];
469 while (ds_chomp_byte (&line->s, ' '))
471 fwrite (ds_data (&line->s), 1, ds_length (&line->s), a->file);
472 u8_line_clear (&a->lines[y]);
474 putc ('\n', a->file);
479 ascii_output_table_item (struct ascii_driver *a,
480 const struct table_item *table_item)
482 struct render_pager *p;
484 update_page_size (a, false);
487 putc ('\n', a->file);
489 p = render_pager_create (&a->params, table_item);
490 for (int i = 0; render_pager_has_next (p); i++)
493 putc ('\n', a->file);
494 ascii_output_lines (a, render_pager_draw_next (p, INT_MAX));
496 render_pager_destroy (p);
500 ascii_output_table_item_unref (struct ascii_driver *a,
501 struct table_item *table_item)
503 ascii_output_table_item (a, table_item);
504 table_item_unref (table_item);
508 ascii_submit (struct output_driver *driver,
509 const struct output_item *output_item)
511 struct ascii_driver *a = ascii_driver_cast (driver);
516 if (is_table_item (output_item))
517 ascii_output_table_item (a, to_table_item (output_item));
519 else if (is_chart_item (output_item) && a->chart_file_name != NULL)
521 struct chart_item *chart_item = to_chart_item (output_item);
524 file_name = xr_draw_png_chart (chart_item, a->chart_file_name,
528 if (file_name != NULL)
530 struct text_item *text_item;
532 text_item = text_item_create_format (
533 TEXT_ITEM_LOG, _("See %s for a chart."), file_name);
535 ascii_submit (driver, &text_item->output_item);
536 text_item_unref (text_item);
540 #endif /* HAVE_CAIRO */
541 else if (is_text_item (output_item))
543 const struct text_item *text_item = to_text_item (output_item);
544 enum text_item_type type = text_item_get_type (text_item);
546 if (type != TEXT_ITEM_PAGE_TITLE)
547 ascii_output_table_item_unref (
548 a, text_item_to_table_item (text_item_ref (text_item)));
550 else if (is_message_item (output_item))
551 ascii_output_table_item_unref (
552 a, text_item_to_table_item (
553 message_item_to_text_item (
555 output_item_ref (output_item)))));
558 const struct output_driver_factory txt_driver_factory =
559 { "txt", "-", ascii_create };
560 const struct output_driver_factory list_driver_factory =
561 { "list", "-", ascii_create };
563 static const struct output_driver_class ascii_driver_class =
571 static char *ascii_reserve (struct ascii_driver *, int y, int x0, int x1,
573 static void ascii_layout_cell (struct ascii_driver *,
574 const struct table_cell *,
575 int bb[TABLE_N_AXES][2],
576 int clip[TABLE_N_AXES][2],
577 int *width, int *height);
580 ascii_draw_line (void *a_, int bb[TABLE_N_AXES][2],
581 enum render_line_style styles[TABLE_N_AXES][2],
582 struct cell_color colors[TABLE_N_AXES][2] UNUSED)
584 struct ascii_driver *a = a_;
591 /* Clip to the page. */
592 x0 = MAX (bb[H][0], 0);
593 y0 = MAX (bb[V][0], 0);
594 x1 = MIN (bb[H][1], a->width);
596 if (x1 <= 0 || y1 <= 0 || x0 >= a->width)
600 uc = box_get (a->box, styles[V][0], styles[V][1], styles[H][0], styles[H][1]);
601 mblen = u8_uctomb (CHAR_CAST (uint8_t *, mbchar), uc, 6);
602 for (y = y0; y < y1; y++)
604 char *p = ascii_reserve (a, y, x0, x1, mblen * (x1 - x0));
605 for (x = x0; x < x1; x++)
607 memcpy (p, mbchar, mblen);
614 ascii_measure_cell_width (void *a_, const struct table_cell *cell,
615 int *min_width, int *max_width)
617 struct ascii_driver *a = a_;
618 int bb[TABLE_N_AXES][2];
619 int clip[TABLE_N_AXES][2];
626 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
627 ascii_layout_cell (a, cell, bb, clip, max_width, &h);
629 if (cell->n_footnotes || strchr (cell->text, ' ')
630 || cell->n_subscripts || cell->superscript)
633 ascii_layout_cell (a, cell, bb, clip, min_width, &h);
636 *min_width = *max_width;
640 ascii_measure_cell_height (void *a_, const struct table_cell *cell, int width)
642 struct ascii_driver *a = a_;
643 int bb[TABLE_N_AXES][2];
644 int clip[TABLE_N_AXES][2];
651 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
652 ascii_layout_cell (a, cell, bb, clip, &w, &h);
657 ascii_draw_cell (void *a_, const struct table_cell *cell, int color_idx UNUSED,
658 int bb[TABLE_N_AXES][2], int valign_offset,
659 int spill[TABLE_N_AXES][2] UNUSED,
660 int clip[TABLE_N_AXES][2])
662 struct ascii_driver *a = a_;
665 bb[V][0] += valign_offset;
666 ascii_layout_cell (a, cell, bb, clip, &w, &h);
670 ascii_reserve (struct ascii_driver *a, int y, int x0, int x1, int n)
672 if (y >= a->allocated_lines)
674 size_t new_alloc = MAX (25, a->allocated_lines);
675 while (new_alloc <= y)
676 new_alloc = xtimes (new_alloc, 2);
677 a->lines = xnrealloc (a->lines, new_alloc, sizeof *a->lines);
678 for (size_t i = a->allocated_lines; i < new_alloc; i++)
679 u8_line_init (&a->lines[i]);
680 a->allocated_lines = new_alloc;
682 return u8_line_reserve (&a->lines[y], x0, x1, n);
686 text_draw (struct ascii_driver *a, enum table_halign halign, int options,
687 bool bold, bool underline,
688 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
689 int y, const uint8_t *string, int n, size_t width)
691 int x0 = MAX (0, clip[H][0]);
692 int y0 = MAX (0, clip[V][0]);
693 int x1 = MIN (a->width, clip[H][1]);
697 if (y < y0 || y >= y1)
700 switch (table_halign_interpret (halign, options & TAB_NUMERIC))
702 case TABLE_HALIGN_LEFT:
705 case TABLE_HALIGN_CENTER:
706 x = (bb[H][0] + bb[H][1] - width + 1) / 2;
708 case TABLE_HALIGN_RIGHT:
709 case TABLE_HALIGN_DECIMAL:
710 x = bb[H][1] - width;
726 mblen = u8_mbtouc (&uc, string, n);
731 w = uc_width (uc, "UTF-8");
746 for (ofs = 0; ofs < n;)
752 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
754 w = uc_width (uc, "UTF-8");
757 if (width + w > x1 - x)
768 if (!a->emphasis || (!bold && !underline))
769 memcpy (ascii_reserve (a, y, x, x + width, n), string, n);
777 /* First figure out how many bytes need to be inserted. */
779 for (ofs = 0; ofs < n; ofs += mblen)
784 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
785 w = uc_width (uc, "UTF-8");
796 /* Then insert them. */
797 out = ascii_reserve (a, y, x, x + width, n_out);
798 for (ofs = 0; ofs < n; ofs += mblen)
803 mblen = u8_mbtouc (&uc, string + ofs, n - ofs);
804 w = uc_width (uc, "UTF-8");
810 out = mempcpy (out, string + ofs, mblen);
819 out = mempcpy (out, string + ofs, mblen);
825 add_markers (const char *text, const struct table_cell *cell)
827 struct string s = DS_EMPTY_INITIALIZER;
828 ds_put_cstr (&s, text);
829 for (size_t i = 0; i < cell->n_subscripts; i++)
830 ds_put_format (&s, "%c%s", i ? ',' : '_', cell->subscripts[i]);
831 if (cell->superscript)
832 ds_put_format (&s, "^%s", cell->superscript);
833 for (size_t i = 0; i < cell->n_footnotes; i++)
834 ds_put_format (&s, "[%s]", cell->footnotes[i]->marker);
835 return ds_steal_cstr (&s);
839 ascii_layout_cell (struct ascii_driver *a, const struct table_cell *cell,
840 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
841 int *widthp, int *heightp)
846 /* Get the basic textual contents. */
847 const char *plain_text = (cell->options & TAB_MARKUP
848 ? output_get_text_from_markup (cell->text)
851 /* Append footnotes, subscripts, superscript if any. */
853 if (cell->n_footnotes || cell->n_subscripts || cell->superscript)
855 text = add_markers (plain_text, cell);
856 if (plain_text != cell->text)
857 free (CONST_CAST (char *, plain_text));
862 /* Calculate length; if it's zero, then there's nothing to do. */
863 size_t length = strlen (text);
866 if (text != cell->text)
867 free (CONST_CAST (char *, text));
871 char *breaks = xmalloc (length + 1);
872 u8_possible_linebreaks (CHAR_CAST (const uint8_t *, text), length,
874 breaks[length] = (breaks[length - 1] == UC_BREAK_MANDATORY
875 ? UC_BREAK_PROHIBITED : UC_BREAK_POSSIBLE);
878 int bb_width = bb[H][1] - bb[H][0];
879 for (int y = bb[V][0]; y < bb[V][1] && pos < length; y++)
881 const uint8_t *line = CHAR_CAST (const uint8_t *, text + pos);
882 const char *b = breaks + pos;
883 size_t n = length - pos;
885 size_t last_break_ofs = 0;
886 int last_break_width = 0;
891 for (ofs = 0; ofs < n;)
897 mblen = u8_mbtouc (&uc, line + ofs, n - ofs);
898 if (b[ofs] == UC_BREAK_MANDATORY)
900 else if (b[ofs] == UC_BREAK_POSSIBLE)
902 last_break_ofs = ofs;
903 last_break_width = width;
906 w = uc_width (uc, "UTF-8");
909 if (width + w > bb_width)
911 if (isspace (line[ofs]))
913 else if (last_break_ofs != 0)
915 ofs = last_break_ofs;
916 width = last_break_width;
925 /* Trim any trailing spaces off the end of the text to be drawn. */
926 for (graph_ofs = ofs; graph_ofs > 0; graph_ofs--)
927 if (!isspace (line[graph_ofs - 1]))
929 width -= ofs - graph_ofs;
932 text_draw (a, cell->style->cell_style.halign, cell->options,
933 cell->style->font_style.bold,
934 cell->style->font_style.underline,
935 bb, clip, y, line, graph_ofs, width);
937 /* If a new-line ended the line, just skip the new-line. Otherwise, skip
938 past any spaces past the end of the line (but not past a new-line). */
939 if (b[ofs] == UC_BREAK_MANDATORY)
942 while (ofs < n && isspace (line[ofs]) && b[ofs] != UC_BREAK_MANDATORY)
952 if (text != cell->text)
953 free (CONST_CAST (char *, text));
957 ascii_test_write (struct output_driver *driver,
958 const char *s, int x, int y, bool bold, bool underline)
960 struct ascii_driver *a = ascii_driver_cast (driver);
961 int bb[TABLE_N_AXES][2];
967 struct table_area_style style = {
968 .cell_style.halign = TABLE_HALIGN_LEFT,
969 .font_style.bold = bold,
970 .font_style.underline = underline,
972 struct table_cell cell = {
973 .text = CONST_CAST (char *, s),
977 bb[TABLE_HORZ][0] = x;
978 bb[TABLE_HORZ][1] = a->width;
979 bb[TABLE_VERT][0] = y;
980 bb[TABLE_VERT][1] = INT_MAX;
982 ascii_layout_cell (a, &cell, bb, bb, &width, &height);
986 ascii_test_set_length (struct output_driver *driver, int y, int length)
988 struct ascii_driver *a = ascii_driver_cast (driver);
995 u8_line_set_length (&a->lines[y], length);
999 ascii_test_flush (struct output_driver *driver)
1001 struct ascii_driver *a = ascii_driver_cast (driver);
1003 for (size_t i = a->allocated_lines; i-- > 0;)
1004 if (a->lines[i].width)
1006 ascii_output_lines (a, i + 1);
1011 static sig_atomic_t terminal_changed = true;
1012 static int terminal_width;
1014 #if HAVE_DECL_SIGWINCH
1016 winch_handler (int signum UNUSED)
1018 terminal_changed = true;
1023 get_terminal_width (void)
1025 #if HAVE_DECL_SIGWINCH
1026 static bool setup_signal;
1029 setup_signal = true;
1031 struct sigaction action = { .sa_handler = winch_handler };
1032 sigemptyset (&action.sa_mask);
1033 sigaction (SIGWINCH, &action, NULL);
1037 if (terminal_changed)
1039 terminal_changed = false;
1041 #ifdef HAVE_TERMIOS_H
1043 if (!ioctl (0, TIOCGWINSZ, &ws))
1044 terminal_width = ws.ws_col;
1048 if (getenv ("COLUMNS"))
1049 terminal_width = atoi (getenv ("COLUMNS"));
1052 if (terminal_width <= 0 || terminal_width > 1024)
1053 terminal_width = 79;
1056 return terminal_width;