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