-/* Writes the headers. Some of them might be vertical; most are
- probably horizontal. */
-static void
-write_header (struct outp_driver *d)
-{
- struct list_ext *prc = d->prc;
-
- if (!prc->header_rows)
- return;
-
- if (n_lines_remaining (d) < prc->header_rows + 1)
- {
- outp_eject_page (d);
- assert (n_lines_remaining (d) >= prc->header_rows + 1);
- }
-
- /* Design the header. */
- if (!prc->header)
- {
- size_t i;
- size_t x;
-
- /* Allocate, initialize header. */
- prc->header = xnmalloc (prc->header_rows, sizeof *prc->header);
- {
- int w = n_chars_width (d);
- for (i = 0; i < prc->header_rows; i++)
- {
- prc->header[i] = xmalloc (w + 1);
- memset (prc->header[i], ' ', w);
- }
- }
-
- /* Put in vertical names. */
- for (i = x = 0; i < prc->n_vertical; i++)
- {
- struct variable *v = cmd.v_variables[i];
- size_t j;
-
- memset (&prc->header[prc->header_rows - 1][x], '-', v->print.w);
- x += v->print.w - 1;
- for (j = 0; j < strlen (v->name); j++)
- prc->header[strlen (v->name) - j - 1][x] = v->name[j];
- x += 2;
- }
-
- /* Put in horizontal names. */
- for (; i < cmd.n_variables; i++)
- {
- struct variable *v = cmd.v_variables[i];
-
- memset (&prc->header[prc->header_rows - 1][x], '-',
- max (v->print.w, (int) strlen (v->name)));
- if ((int) strlen (v->name) < v->print.w)
- x += v->print.w - strlen (v->name);
- memcpy (&prc->header[0][x], v->name, strlen (v->name));
- x += strlen (v->name) + 1;
- }
-
- /* Add null bytes. */
- for (i = 0; i < prc->header_rows; i++)
- {
- for (x = n_chars_width (d); x >= 1; x--)
- if (prc->header[i][x - 1] != ' ')
- {
- prc->header[i][x] = 0;
- break;
- }
- assert (x);
- }
- }
-
- /* Write out the header, in back-to-front order except for the last line. */
- if (prc->header_rows >= 2)
- {
- size_t i;
-
- for (i = prc->header_rows - 1; i-- != 0; )
- write_line (d, prc->header[i]);
- }
- write_line (d, prc->header[prc->header_rows - 1]);
-}
-
-
-/* Frees up all the memory we've allocated. */
-static void
-clean_up (void)
-{
- struct outp_driver *d;
-
- for (d = outp_drivers (NULL); d; d = outp_drivers (d))
- if (d->class->special == 0)
- {
- struct list_ext *prc = d->prc;
- size_t i;
-
- if (prc->header)
- {
- for (i = 0; i < prc->header_rows; i++)
- free (prc->header[i]);
- free (prc->header);
- }
- free (prc);
-
- d->class->text_set_font_by_name (d, "PROP");
- }
- else if (d->class == &html_class)
- {
- if (d->driver_open && d->page_open)
- {
- struct html_driver_ext *x = d->ext;
-
- fputs ("</TABLE>\n", x->file.file);
- }
- }
- else
- assert (0);
-
- free (cmd.v_variables);
-}
-
-/* Writes string STRING at the current position. If the text would
- fall off the side of the page, then advance to the next line,
- indenting by amount INDENT. */
-static void
-write_varname (struct outp_driver *d, char *string, int indent)
-{
- struct outp_text text;
-
- text.options = OUTP_T_JUST_LEFT;
- ls_init (&text.s, string, strlen (string));
- d->class->text_metrics (d, &text);
-
- if (d->cp_x + text.h > d->width)
- {
- d->cp_y += d->font_height;
- if (d->cp_y + d->font_height > d->length)
- outp_eject_page (d);
- d->cp_x = indent;
- }
-
- text.x = d->cp_x;
- text.y = d->cp_y;
- d->class->text_draw (d, &text);
- d->cp_x += text.h;
-}
-
-/* When we can't fit all the values across the page, we write out all
- the variable names just once. This is where we do it. */
-static void
-write_fallback_headers (struct outp_driver *d)
-{
- const int max_width = n_chars_width(d) - 10;
-
- int index = 0;
- int width = 0;
- int line_number = 0;
-
- const char *Line = _("Line");
- char *leader = local_alloc (strlen (Line) + INT_DIGITS + 1 + 1);
-
- while (index < cmd.n_variables)
- {
- struct outp_text text;
-
- /* Ensure that there is enough room for a line of text. */
- if (d->cp_y + d->font_height > d->length)
- outp_eject_page (d);
-
- /* The leader is a string like `Line 1: '. Write the leader. */
- sprintf(leader, "%s %d:", Line, ++line_number);
- text.options = OUTP_T_JUST_LEFT;
- ls_init (&text.s, leader, strlen (leader));
- text.x = 0;
- text.y = d->cp_y;
- d->class->text_draw (d, &text);
- d->cp_x = text.h;
-
- goto entry;
- do
- {
- width++;
-
- entry:
- {
- int var_width = cmd.v_variables[index]->print.w;
- if (width + var_width > max_width && width != 0)
- {
- width = 0;
- d->cp_x = 0;
- d->cp_y += d->font_height;
- break;
- }
- width += var_width;
- }
-
- {
- char varname[10];
- sprintf (varname, " %s", cmd.v_variables[index]->name);
- write_varname (d, varname, text.h);
- }
- }
- while (++index < cmd.n_variables);
-
- }
- d->cp_x = 0;
- d->cp_y += d->font_height;
-
- local_free (leader);
-}
-
-/* There are three possible layouts for the LIST procedure:
-
- 1. If the values and their variables' name fit across the page,
- then they are listed across the page in that way.
-
- 2. If the values can fit across the page, but not the variable
- names, then as many variable names as necessary are printed
- vertically to compensate.
-
- 3. If not even the values can fit across the page, the variable
- names are listed just once, at the beginning, in a compact format,
- and the values are listed with a variable name label at the
- beginning of each line for easier reference.
-
- This is complicated by the fact that we have to do all this for
- every output driver, not just once. */
-static void
-determine_layout (void)
-{
- struct outp_driver *d;
-
- /* This is the largest page width of any driver, so we can tell what
- size buffer to allocate. */
- int largest_page_width = 0;
-
- for (d = outp_drivers (NULL); d; d = outp_drivers (d))
- {
- size_t column; /* Current column. */
- int width; /* Accumulated width. */
- int height; /* Height of vertical names. */
- int max_width; /* Page width. */
-
- struct list_ext *prc;
-
- if (d->class == &html_class)
- continue;
-
- assert (d->class->special == 0);
-
- if (!d->page_open)
- d->class->open_page (d);
-
- max_width = n_chars_width (d);
- largest_page_width = max (largest_page_width, max_width);
-
- prc = d->prc = xmalloc (sizeof *prc);
- prc->type = 0;
- prc->n_vertical = 0;
- prc->header = NULL;
-
- /* Try layout #1. */
- for (width = cmd.n_variables - 1, column = 0; column < cmd.n_variables; column++)
- {
- struct variable *v = cmd.v_variables[column];
- width += max (v->print.w, (int) strlen (v->name));
- }
- if (width <= max_width)
- {
- prc->header_rows = 2;
- d->class->text_set_font_by_name (d, "FIXED");
- continue;
- }
-
- /* Try layout #2. */
- for (width = cmd.n_variables - 1, height = 0, column = 0;
- column < cmd.n_variables && width <= max_width;
- column++)