output: Break struct pivot_table_look out of struct pivot_table.
[pspp] / src / output / pivot-output.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "output/pivot-table.h"
22
23 #include "data/settings.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/pool.h"
26 #include "output/table.h"
27 #include "output/table-item.h"
28 #include "output/text-item.h"
29 #include "output/table-provider.h"
30
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
33
34 #define H TABLE_HORZ
35 #define V TABLE_VERT
36
37 static const struct pivot_category *
38 find_category (const struct pivot_dimension *d, int dim_index,
39                const size_t *indexes, int row_ofs)
40 {
41   size_t index = indexes[dim_index];
42   assert (index < d->n_leaves);
43   for (const struct pivot_category *c = d->presentation_leaves[index];
44        c; c = c->parent)
45     {
46       if (!row_ofs)
47         return c;
48
49       row_ofs -= 1 + c->extra_depth;
50       if (row_ofs < 0)
51         return NULL;
52     }
53   return NULL;
54 }
55
56 static struct table_area_style *
57 table_area_style_override (struct pool *pool,
58                            const struct table_area_style *in,
59                            const struct cell_style *cell_,
60                            const struct font_style *font_)
61 {
62   const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
63   const struct font_style *font = font_ ? font_ : &in->font_style;
64
65   struct table_area_style *out = (pool
66                             ? pool_alloc (pool, sizeof *out)
67                             : xmalloc (sizeof *out));
68   *out = (struct table_area_style) {
69     .cell_style.halign = cell->halign,
70     .cell_style.valign = cell->valign,
71     .cell_style.decimal_offset = cell->decimal_offset,
72     .cell_style.margin[H][0] = cell->margin[H][0],
73     .cell_style.margin[H][1] = cell->margin[H][1],
74     .cell_style.margin[V][0] = cell->margin[V][0],
75     .cell_style.margin[V][1] = cell->margin[V][1],
76     .font_style.fg[0] = font->fg[0],
77     .font_style.fg[1] = font->fg[1],
78     .font_style.bg[0] = font->bg[0],
79     .font_style.bg[1] = font->bg[1],
80     .font_style.typeface = (font->typeface
81                             ? pool_strdup (pool, font->typeface)
82                             : NULL),
83     .font_style.size = font->size,
84     .font_style.bold = font->bold,
85     .font_style.italic = font->italic,
86     .font_style.underline = font->underline,
87     .font_style.markup = font->markup,
88   };
89   return out;
90 }
91
92 static void
93 fill_cell (struct table *t, int x1, int y1, int x2, int y2,
94            const struct table_area_style *style, int style_idx,
95            const struct pivot_value *value, struct footnote **footnotes,
96            enum settings_value_show show_values,
97            enum settings_value_show show_variables,
98            bool rotate_label)
99 {
100
101   struct string s = DS_EMPTY_INITIALIZER;
102   int opts = style_idx << TAB_STYLE_SHIFT;
103   if (value)
104     {
105       bool numeric = pivot_value_format_body (value, show_values,
106                                               show_variables, &s);
107       if (numeric)
108         opts |= TAB_NUMERIC;
109       if (value->font_style && value->font_style->markup)
110         opts |= TAB_MARKUP;
111       if (rotate_label)
112         opts |= TAB_ROTATE;
113     }
114   table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
115   ds_destroy (&s);
116
117   if (value)
118     {
119       if (value->cell_style || value->font_style)
120         table_add_style (t, x1, y1,
121                          table_area_style_override (t->container, style,
122                                                     value->cell_style,
123                                                     value->font_style));
124
125       for (size_t i = 0; i < value->n_footnotes; i++)
126         {
127           struct footnote *f = footnotes[value->footnotes[i]->idx];
128           if (f)
129             table_add_footnote (t, x1, y1, f);
130         }
131
132       if (value->n_subscripts)
133         table_add_subscripts (t, x1, y1,
134                               value->subscripts, value->n_subscripts);
135     }
136 }
137
138 static struct table_item_text *
139 pivot_value_to_table_item_text (const struct pivot_value *value,
140                                 const struct table_area_style *area,
141                                 struct footnote **footnotes,
142                                 enum settings_value_show show_values,
143                                 enum settings_value_show show_variables)
144 {
145   if (!value)
146     return NULL;
147
148   struct string s = DS_EMPTY_INITIALIZER;
149   pivot_value_format_body (value, show_values, show_variables, &s);
150
151   struct table_item_text *text = xmalloc (sizeof *text);
152   *text = (struct table_item_text) {
153     .content = ds_steal_cstr (&s),
154     .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
155     .style = table_area_style_override (
156       NULL, area, value->cell_style, value->font_style),
157   };
158
159   for (size_t i = 0; i < value->n_footnotes; i++)
160     {
161       struct footnote *f = footnotes[value->footnotes[i]->idx];
162       if (f)
163         text->footnotes[text->n_footnotes++] = f;
164     }
165
166   return text;
167 }
168
169 static int
170 get_table_rule (const struct table_border_style *styles,
171                 enum pivot_border style_idx)
172 {
173   return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
174 }
175
176 static void
177 draw_line (struct table *t, const struct table_border_style *styles,
178            enum pivot_border style_idx,
179            enum table_axis axis, int a, int b0, int b1)
180 {
181   int rule = get_table_rule (styles, style_idx);
182   if (axis == H)
183     table_hline (t, rule, b0, b1, a);
184   else
185     table_vline (t, rule, a, b0, b1);
186 }
187
188 static void
189 compose_headings (struct table *t,
190                   const struct pivot_axis *a_axis, enum table_axis a,
191                   const struct pivot_axis *b_axis,
192                   const struct table_border_style *borders,
193                   enum pivot_border dim_col_horz,
194                   enum pivot_border dim_col_vert,
195                   enum pivot_border cat_col_horz,
196                   enum pivot_border cat_col_vert,
197                   const size_t *column_enumeration, size_t n_columns,
198                   const struct table_area_style *label_style,
199                   int label_style_idx,
200                   const struct table_area_style *corner_style,
201                   struct footnote **footnotes,
202                   enum settings_value_show show_values,
203                   enum settings_value_show show_variables,
204                   bool rotate_inner_labels, bool rotate_outer_labels)
205 {
206   enum table_axis b = !a;
207   int b_size = a_axis->label_depth;
208   int a_ofs = b_axis->label_depth;
209
210   if (!a_axis->n_dimensions || !n_columns || !b_size)
211     return;
212
213   int bottom_row = b_size - 1;
214   const int stride = MAX (1, a_axis->n_dimensions);
215   for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
216     {
217       const struct pivot_dimension *d = a_axis->dimensions[dim_index];
218       if (d->hide_all_labels)
219         continue;
220
221       for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
222         {
223           for (size_t x1 = 0; x1 < n_columns;)
224             {
225               const struct pivot_category *c = find_category (
226                 d, dim_index, column_enumeration + x1 * stride, row_ofs);
227               if (!c)
228                 {
229                   x1++;
230                   continue;
231                 }
232
233               size_t x2;
234               for (x2 = x1 + 1; x2 < n_columns; x2++)
235                 {
236                   const struct pivot_category *c2 = find_category (
237                     d, dim_index, column_enumeration + x2 * stride, row_ofs);
238                   if (c != c2)
239                     break;
240                 }
241
242               int y1 = bottom_row - row_ofs - c->extra_depth;
243               int y2 = bottom_row - row_ofs + 1;
244               bool is_outer_row = y1 == 0;
245               bool is_inner_row = y2 == b_size;
246               if (pivot_category_is_leaf (c) || c->show_label)
247                 {
248                   int bb[TABLE_N_AXES][2];
249                   bb[a][0] = x1 + a_ofs;
250                   bb[a][1] = x2 + a_ofs - 1;
251                   bb[b][0] = y1;
252                   bb[b][1] = y2 - 1;
253                   bool rotate = ((rotate_inner_labels && is_inner_row)
254                                  || (rotate_outer_labels && is_outer_row));
255                   fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
256                              label_style, label_style_idx, c->name, footnotes,
257                              show_values, show_variables, rotate);
258
259                   if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
260                     {
261                       enum pivot_border style
262                         = (y1 == 0 && a_axis->label_depth > d->label_depth
263                            ? dim_col_vert
264                            : cat_col_vert);
265                       draw_line (t, borders, style, b, x2 + a_ofs, y1,
266                                  t->n[b] - 1);
267                     }
268                   if (pivot_category_is_leaf (c) && x1 > 0)
269                     {
270                       enum pivot_border style
271                         = (y1 == 0 && a_axis->label_depth > d->label_depth
272                            ? dim_col_vert
273                            : cat_col_vert);
274                       draw_line (t, borders, style, b, x1 + a_ofs, y1,
275                                  t->n[b] - 1);
276                     }
277                 }
278               if (c->parent && c->parent->show_label)
279                 draw_line (t, borders, cat_col_horz, a, y1,
280                            x1 + a_ofs, x2 + a_ofs - 1);
281
282               x1 = x2;
283             }
284         }
285
286       if (d->root->show_label_in_corner && a_ofs > 0)
287         {
288           int bb[TABLE_N_AXES][2];
289           bb[a][0] = a_ofs - 1;
290           bb[a][1] = a_ofs - 1;
291           bb[b][0] = bottom_row - d->label_depth + 1;
292           bb[b][1] = bottom_row;
293           fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
294                      corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
295                      show_values, show_variables, false);
296         }
297
298       if (dim_index > 1)
299         draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
300                    t->n[a] - 1);
301
302       bottom_row -= d->label_depth;
303     }
304 }
305
306 static void
307 pivot_table_submit_layer (const struct pivot_table *pt,
308                           const size_t *layer_indexes)
309 {
310   const size_t *pindexes[PIVOT_N_AXES]
311     = { [PIVOT_AXIS_LAYER] = layer_indexes };
312
313   size_t body[TABLE_N_AXES];
314   size_t *column_enumeration = pivot_table_enumerate_axis (
315     pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look.omit_empty, &body[H]);
316   size_t *row_enumeration = pivot_table_enumerate_axis (
317     pt, PIVOT_AXIS_ROW, layer_indexes, pt->look.omit_empty, &body[V]);
318
319   int stub[TABLE_N_AXES] = {
320     [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
321     [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
322   };
323   struct table *table = table_create (body[H] + stub[H],
324                                       body[V] + stub[V],
325                                       stub[H], 0, stub[V], 0);
326
327   for (size_t i = 0; i < PIVOT_N_AREAS; i++)
328     table->styles[i] = table_area_style_override (
329       table->container, &pt->look.areas[i], NULL, NULL);
330
331   for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
332     {
333       const struct table_border_style *in = &pt->look.borders[i];
334       table->rule_colors[i] = pool_alloc (table->container,
335                                           sizeof *table->rule_colors[i]);
336       struct cell_color *out = table->rule_colors[i];
337       out->alpha = in->color.alpha;
338       out->r = in->color.r;
339       out->g = in->color.g;
340       out->b = in->color.b;
341     }
342
343   struct footnote **footnotes = XCALLOC (pt->n_footnotes,  struct footnote *);
344   for (size_t i = 0; i < pt->n_footnotes; i++)
345     {
346       const struct pivot_footnote *pf = pt->footnotes[i];
347
348       if (!pf->show)
349         continue;
350
351       char *content = pivot_value_to_string (pf->content, pt->show_values,
352                                              pt->show_variables);
353       char *marker = pivot_value_to_string (pf->marker, pt->show_values,
354                                             pt->show_variables);
355       footnotes[i] = table_create_footnote (
356         table, i, content, marker,
357         table_area_style_override (table->container,
358                                    &pt->look.areas[PIVOT_AREA_FOOTER],
359                                    pf->content->cell_style,
360                                    pf->content->font_style));
361       free (marker);
362       free (content);
363     }
364
365   compose_headings (table,
366                     &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
367                     pt->look.borders,
368                     PIVOT_BORDER_DIM_COL_HORZ,
369                     PIVOT_BORDER_DIM_COL_VERT,
370                     PIVOT_BORDER_CAT_COL_HORZ,
371                     PIVOT_BORDER_CAT_COL_VERT,
372                     column_enumeration, body[H],
373                     &pt->look.areas[PIVOT_AREA_COLUMN_LABELS],
374                     PIVOT_AREA_COLUMN_LABELS,
375                     &pt->look.areas[PIVOT_AREA_CORNER], footnotes,
376                     pt->show_values, pt->show_variables,
377                     pt->rotate_inner_column_labels, false);
378
379   compose_headings (table,
380                     &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
381                     pt->look.borders,
382                     PIVOT_BORDER_DIM_ROW_VERT,
383                     PIVOT_BORDER_DIM_ROW_HORZ,
384                     PIVOT_BORDER_CAT_ROW_VERT,
385                     PIVOT_BORDER_CAT_ROW_HORZ,
386                     row_enumeration, body[V],
387                     &pt->look.areas[PIVOT_AREA_ROW_LABELS],
388                     PIVOT_AREA_ROW_LABELS,
389                     &pt->look.areas[PIVOT_AREA_CORNER], footnotes,
390                     pt->show_values, pt->show_variables,
391                     false, pt->rotate_outer_row_labels);
392
393   size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
394   size_t y = 0;
395   PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
396                               &pt->axes[PIVOT_AXIS_ROW])
397     {
398       size_t x = 0;
399       PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
400                                   column_enumeration,
401                                   &pt->axes[PIVOT_AXIS_COLUMN])
402         {
403           pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
404           const struct pivot_value *value = pivot_table_get (pt, dindexes);
405           fill_cell (table,
406                      x + stub[H], y + stub[V],
407                      x + stub[H], y + stub[V],
408                      &pt->look.areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
409                      value, footnotes,
410                      pt->show_values, pt->show_variables, false);
411
412           x++;
413         }
414
415       y++;
416     }
417   free (dindexes);
418
419   if (pt->corner_text && stub[H] && stub[V])
420     fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
421                &pt->look.areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
422                pt->corner_text, footnotes,
423                pt->show_values, pt->show_variables, false);
424
425   if (table_nc (table) && table_nr (table))
426     {
427       table_hline (
428         table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_TOP),
429         0, table_nc (table) - 1, 0);
430       table_hline (
431         table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_BOTTOM),
432         0, table_nc (table) - 1, table_nr (table));
433       table_vline (
434         table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_LEFT),
435         0, 0, table_nr (table) - 1);
436       table_vline (
437         table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_RIGHT),
438         table_nc (table), 0, table_nr (table) - 1);
439
440       if (stub[V])
441         table_hline (
442           table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_TOP),
443           0, table_nc (table) - 1, stub[V]);
444       if (stub[H])
445         table_vline (
446           table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_LEFT),
447           stub[H], 0, table_nr (table) - 1);
448
449     }
450   free (column_enumeration);
451   free (row_enumeration);
452
453   struct table_item *ti = table_item_create (table, NULL, NULL);
454
455   if (pt->title)
456     {
457       struct table_item_text *title = pivot_value_to_table_item_text (
458         pt->title, &pt->look.areas[PIVOT_AREA_TITLE], footnotes,
459         pt->show_values, pt->show_variables);
460       table_item_set_title (ti, title);
461       table_item_text_destroy (title);
462     }
463
464   const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
465   struct table_item_layers *layers = NULL;
466   for (size_t i = 0; i < layer_axis->n_dimensions; i++)
467     {
468       const struct pivot_dimension *d = layer_axis->dimensions[i];
469       if (d->n_leaves)
470         {
471           if (!layers)
472             {
473               layers = xzalloc (sizeof *layers);
474               layers->style = table_area_style_override (
475                 NULL, &pt->look.areas[PIVOT_AREA_LAYERS], NULL, NULL);
476               layers->layers = xnmalloc (layer_axis->n_dimensions,
477                                          sizeof *layers->layers);
478             }
479
480           const struct pivot_value *name
481             = d->data_leaves[layer_indexes[i]]->name;
482           struct table_item_layer *layer = &layers->layers[layers->n_layers++];
483           struct string s = DS_EMPTY_INITIALIZER;
484           pivot_value_format_body (name, pt->show_values, pt->show_variables,
485                                    &s);
486           layer->content = ds_steal_cstr (&s);
487           layer->n_footnotes = 0;
488           layer->footnotes = xnmalloc (name->n_footnotes,
489                                        sizeof *layer->footnotes);
490           for (size_t i = 0; i < name->n_footnotes; i++)
491             {
492               struct footnote *f = footnotes[name->footnotes[i]->idx];
493               if (f)
494                 layer->footnotes[layer->n_footnotes++] = f;
495             }
496         }
497     }
498   if (layers)
499     {
500       table_item_set_layers (ti, layers);
501       table_item_layers_destroy (layers);
502     }
503
504   if (pt->caption && pt->show_caption)
505     {
506       struct table_item_text *caption = pivot_value_to_table_item_text (
507         pt->caption, &pt->look.areas[PIVOT_AREA_CAPTION], footnotes,
508         pt->show_values, pt->show_variables);
509       table_item_set_caption (ti, caption);
510       table_item_text_destroy (caption);
511     }
512
513   free (footnotes);
514   ti->pt = pivot_table_ref (pt);
515
516   table_item_submit (ti);
517 }
518
519 void
520 pivot_table_submit (struct pivot_table *pt)
521 {
522   pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
523
524   int old_decimal = settings_get_decimal_char (FMT_COMMA);
525   if (pt->decimal == '.' || pt->decimal == ',')
526     settings_set_decimal_char (pt->decimal);
527
528   if (pt->look.print_all_layers)
529     {
530       size_t *layer_indexes;
531
532       PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
533         {
534           if (pt->look.paginate_layers)
535             text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, ""));
536           pivot_table_submit_layer (pt, layer_indexes);
537         }
538     }
539   else
540     pivot_table_submit_layer (pt, pt->current_layer);
541
542   settings_set_decimal_char (old_decimal);
543
544   pivot_table_unref (pt);
545 }