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