1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2018 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/>. */
21 #include "output/pivot-table.h"
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"
32 #include "gl/minmax.h"
33 #include "gl/xalloc.h"
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)
42 size_t index = indexes[dim_index];
43 assert (index < d->n_leaves);
44 for (const struct pivot_category *c = d->presentation_leaves[index];
50 row_ofs -= 1 + c->extra_depth;
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_,
64 const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
65 const struct font_style *font = font_ ? font_ : &in->font_style;
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)
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,
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)
100 int options = style_idx << TAB_STYLE_SHIFT;
103 bool numeric = pivot_value_format_body (value, show_values,
106 options |= TAB_NUMERIC;
107 if (value->font_style && value->font_style->markup)
108 options |= TAB_MARKUP;
110 options |= TAB_ROTATE;
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,
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));
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,
138 for (size_t i = 0; i < value->n_footnotes; i++)
140 struct footnote *f = footnotes[value->footnotes[i]->idx];
142 table_add_footnote (t, x1, y1, f);
145 if (value->n_subscripts)
146 table_add_subscripts (t, x1, y1,
147 value->subscripts, value->n_subscripts);
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)
161 struct string s = DS_EMPTY_INITIALIZER;
162 int options = format_cell (value, style_idx,
163 show_values, show_variables, false, &s);
165 struct table_cell *cell = xmalloc (sizeof *cell);
166 *cell = (struct table_cell) {
168 .text = ds_steal_cstr (&s),
169 .style = table_area_style_override (
170 NULL, style, value->cell_style, value->font_style, false),
173 if (value->n_subscripts)
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]);
182 if (value->n_footnotes)
184 cell->footnotes = xnmalloc (value->n_footnotes, sizeof *cell->footnotes);
185 for (size_t i = 0; i < value->n_footnotes; i++)
187 struct footnote *f = footnotes[value->footnotes[i]->idx];
189 cell->footnotes[cell->n_footnotes++] = f;
196 static struct table_item_text *
197 pivot_value_to_table_item_text (const struct pivot_value *value,
198 const struct table_area_style *area,
199 struct footnote **footnotes,
200 enum settings_value_show show_values,
201 enum settings_value_show show_variables)
206 struct string s = DS_EMPTY_INITIALIZER;
207 pivot_value_format_body (value, show_values, show_variables, &s);
209 struct table_item_text *text = xmalloc (sizeof *text);
210 *text = (struct table_item_text) {
211 .content = ds_steal_cstr (&s),
212 .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
213 .style = table_area_style_override (
214 NULL, area, value->cell_style, value->font_style, false),
217 for (size_t i = 0; i < value->n_footnotes; i++)
219 struct footnote *f = footnotes[value->footnotes[i]->idx];
221 text->footnotes[text->n_footnotes++] = f;
228 get_table_rule (const struct table_border_style *styles,
229 enum pivot_border style_idx)
231 return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
235 draw_line (struct table *t, const struct table_border_style *styles,
236 enum pivot_border style_idx,
237 enum table_axis axis, int a, int b0, int b1)
239 int rule = get_table_rule (styles, style_idx);
241 table_hline (t, rule, b0, b1, a);
243 table_vline (t, rule, a, b0, b1);
247 compose_headings (struct table *t,
248 const struct pivot_axis *a_axis, enum table_axis a,
249 const struct pivot_axis *b_axis,
250 const struct table_border_style *borders,
251 enum pivot_border dim_col_horz,
252 enum pivot_border dim_col_vert,
253 enum pivot_border cat_col_horz,
254 enum pivot_border cat_col_vert,
255 const size_t *column_enumeration, size_t n_columns,
256 const struct table_area_style *label_style,
258 const struct table_area_style *corner_style,
259 struct footnote **footnotes,
260 enum settings_value_show show_values,
261 enum settings_value_show show_variables,
262 bool rotate_inner_labels, bool rotate_outer_labels)
264 enum table_axis b = !a;
265 int b_size = a_axis->label_depth;
266 int a_ofs = b_axis->label_depth;
268 if (!a_axis->n_dimensions || !n_columns || !b_size)
271 int bottom_row = b_size - 1;
272 const int stride = MAX (1, a_axis->n_dimensions);
273 for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
275 const struct pivot_dimension *d = a_axis->dimensions[dim_index];
276 if (d->hide_all_labels)
279 for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
281 for (size_t x1 = 0; x1 < n_columns;)
283 const struct pivot_category *c = find_category (
284 d, dim_index, column_enumeration + x1 * stride, row_ofs);
292 for (x2 = x1 + 1; x2 < n_columns; x2++)
294 const struct pivot_category *c2 = find_category (
295 d, dim_index, column_enumeration + x2 * stride, row_ofs);
300 int y1 = bottom_row - row_ofs - c->extra_depth;
301 int y2 = bottom_row - row_ofs + 1;
302 bool is_outer_row = y1 == 0;
303 bool is_inner_row = y2 == b_size;
304 if (pivot_category_is_leaf (c) || c->show_label)
306 int bb[TABLE_N_AXES][2];
307 bb[a][0] = x1 + a_ofs;
308 bb[a][1] = x2 + a_ofs - 1;
311 bool rotate = ((rotate_inner_labels && is_inner_row)
312 || (rotate_outer_labels && is_outer_row));
313 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
314 label_style, label_style_idx, c->name, footnotes,
315 show_values, show_variables, rotate);
317 if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
319 enum pivot_border style
320 = (y1 == 0 && a_axis->label_depth > d->label_depth
323 draw_line (t, borders, style, b, x2 + a_ofs, y1,
326 if (pivot_category_is_leaf (c) && x1 > 0)
328 enum pivot_border style
329 = (y1 == 0 && a_axis->label_depth > d->label_depth
332 draw_line (t, borders, style, b, x1 + a_ofs, y1,
336 if (c->parent && c->parent->show_label)
337 draw_line (t, borders, cat_col_horz, a, y1,
338 x1 + a_ofs, x2 + a_ofs - 1);
344 if (d->root->show_label_in_corner && a_ofs > 0)
346 int bb[TABLE_N_AXES][2];
348 bb[a][1] = a_ofs - 1;
349 bb[b][0] = bottom_row - d->label_depth + 1;
350 bb[b][1] = bottom_row;
351 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
352 corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
353 show_values, show_variables, false);
357 draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
360 bottom_row -= d->label_depth;
365 pivot_table_submit_layer (const struct pivot_table *pt,
366 const size_t *layer_indexes)
368 const size_t *pindexes[PIVOT_N_AXES]
369 = { [PIVOT_AXIS_LAYER] = layer_indexes };
371 size_t body[TABLE_N_AXES];
372 size_t *column_enumeration = pivot_table_enumerate_axis (
373 pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look->omit_empty, &body[H]);
374 size_t *row_enumeration = pivot_table_enumerate_axis (
375 pt, PIVOT_AXIS_ROW, layer_indexes, pt->look->omit_empty, &body[V]);
377 int stub[TABLE_N_AXES] = {
378 [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
379 [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
381 struct table *table = table_create (body[H] + stub[H],
383 stub[H], 0, stub[V], 0);
385 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
386 table->styles[i] = table_area_style_override (
387 table->container, &pt->look->areas[i], NULL, NULL, false);
389 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
391 const struct table_border_style *in = &pt->look->borders[i];
392 table->rule_colors[i] = pool_alloc (table->container,
393 sizeof *table->rule_colors[i]);
394 struct cell_color *out = table->rule_colors[i];
395 out->alpha = in->color.alpha;
396 out->r = in->color.r;
397 out->g = in->color.g;
398 out->b = in->color.b;
401 struct footnote **footnotes = XCALLOC (pt->n_footnotes, struct footnote *);
402 for (size_t i = 0; i < pt->n_footnotes; i++)
404 const struct pivot_footnote *pf = pt->footnotes[i];
409 char *content = pivot_value_to_string (pf->content, pt->show_values,
411 char *marker = pivot_value_to_string (pf->marker, pt->show_values,
413 footnotes[i] = table_create_footnote (
414 table, i, content, marker,
415 table_area_style_override (table->container,
416 &pt->look->areas[PIVOT_AREA_FOOTER],
417 pf->content->cell_style,
418 pf->content->font_style,
424 compose_headings (table,
425 &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
427 PIVOT_BORDER_DIM_COL_HORZ,
428 PIVOT_BORDER_DIM_COL_VERT,
429 PIVOT_BORDER_CAT_COL_HORZ,
430 PIVOT_BORDER_CAT_COL_VERT,
431 column_enumeration, body[H],
432 &pt->look->areas[PIVOT_AREA_COLUMN_LABELS],
433 PIVOT_AREA_COLUMN_LABELS,
434 &pt->look->areas[PIVOT_AREA_CORNER], footnotes,
435 pt->show_values, pt->show_variables,
436 pt->rotate_outer_row_labels, false);
438 compose_headings (table,
439 &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
441 PIVOT_BORDER_DIM_ROW_VERT,
442 PIVOT_BORDER_DIM_ROW_HORZ,
443 PIVOT_BORDER_CAT_ROW_VERT,
444 PIVOT_BORDER_CAT_ROW_HORZ,
445 row_enumeration, body[V],
446 &pt->look->areas[PIVOT_AREA_ROW_LABELS],
447 PIVOT_AREA_ROW_LABELS,
448 &pt->look->areas[PIVOT_AREA_CORNER], footnotes,
449 pt->show_values, pt->show_variables,
450 false, pt->rotate_inner_column_labels);
452 size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
454 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
455 &pt->axes[PIVOT_AXIS_ROW])
458 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
460 &pt->axes[PIVOT_AXIS_COLUMN])
462 pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
463 const struct pivot_value *value = pivot_table_get (pt, dindexes);
465 x + stub[H], y + stub[V],
466 x + stub[H], y + stub[V],
467 &pt->look->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
469 pt->show_values, pt->show_variables, false);
478 if ((pt->corner_text || !pt->look->row_labels_in_corner)
479 && stub[H] && stub[V])
480 fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
481 &pt->look->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
482 pt->corner_text, footnotes,
483 pt->show_values, pt->show_variables, false);
485 if (table->n[H] && table->n[V])
488 table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_TOP),
489 0, table->n[H] - 1, 0);
491 table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_BOTTOM),
492 0, table->n[H] - 1, table->n[V]);
494 table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_LEFT),
495 0, 0, table->n[V] - 1);
497 table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_RIGHT),
498 table->n[H], 0, table->n[V] - 1);
502 table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_TOP),
503 0, table->n[H] - 1, stub[V]);
506 table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_LEFT),
507 stub[H], 0, table->n[V] - 1);
510 free (column_enumeration);
511 free (row_enumeration);
513 struct table_item *ti = table_item_create (table);
516 table_item_set_notes (ti, pt->notes);
520 struct table_cell *title = pivot_value_to_table_cell (
521 pt->title, &pt->look->areas[PIVOT_AREA_TITLE], PIVOT_AREA_TITLE,
522 footnotes, pt->show_values, pt->show_variables);
523 table_item_set_title (ti, title);
524 table_cell_destroy (title);
527 const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
528 struct table_item_layers *layers = NULL;
529 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
531 const struct pivot_dimension *d = layer_axis->dimensions[i];
536 layers = xzalloc (sizeof *layers);
537 layers->style = table_area_style_override (
538 NULL, &pt->look->areas[PIVOT_AREA_LAYERS], NULL, NULL, false);
539 layers->layers = xnmalloc (layer_axis->n_dimensions,
540 sizeof *layers->layers);
543 const struct pivot_value *name
544 = d->data_leaves[layer_indexes[i]]->name;
545 struct table_item_layer *layer = &layers->layers[layers->n_layers++];
546 struct string s = DS_EMPTY_INITIALIZER;
547 pivot_value_format_body (name, pt->show_values, pt->show_variables,
549 layer->content = ds_steal_cstr (&s);
550 layer->n_footnotes = 0;
551 layer->footnotes = xnmalloc (name->n_footnotes,
552 sizeof *layer->footnotes);
553 for (size_t i = 0; i < name->n_footnotes; i++)
555 struct footnote *f = footnotes[name->footnotes[i]->idx];
557 layer->footnotes[layer->n_footnotes++] = f;
563 table_item_set_layers (ti, layers);
564 table_item_layers_destroy (layers);
567 if (pt->caption && pt->show_caption)
569 struct table_item_text *caption = pivot_value_to_table_item_text (
570 pt->caption, &pt->look->areas[PIVOT_AREA_CAPTION], footnotes,
571 pt->show_values, pt->show_variables);
572 table_item_set_caption (ti, caption);
573 table_item_text_destroy (caption);
577 ti->pt = pivot_table_ref (pt);
579 table_item_submit (ti);
583 pivot_table_submit (struct pivot_table *pt)
585 pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
587 int old_decimal = settings_get_decimal_char (FMT_COMMA);
588 if (pt->decimal == '.' || pt->decimal == ',')
589 settings_set_decimal_char (pt->decimal);
591 if (pt->look->print_all_layers)
593 size_t *layer_indexes;
595 PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
597 if (pt->look->paginate_layers)
598 page_eject_item_submit (page_eject_item_create ());
599 pivot_table_submit_layer (pt, layer_indexes);
603 pivot_table_submit_layer (pt, pt->current_layer);
605 settings_set_decimal_char (old_decimal);
607 pivot_table_unref (pt);