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_)
63 const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
64 const struct font_style *font = font_ ? font_ : &in->font_style;
66 struct table_area_style *out = (pool
67 ? pool_alloc (pool, sizeof *out)
68 : xmalloc (sizeof *out));
69 *out = (struct table_area_style) {
70 .cell_style.halign = cell->halign,
71 .cell_style.valign = cell->valign,
72 .cell_style.decimal_offset = cell->decimal_offset,
73 .cell_style.margin[H][0] = cell->margin[H][0],
74 .cell_style.margin[H][1] = cell->margin[H][1],
75 .cell_style.margin[V][0] = cell->margin[V][0],
76 .cell_style.margin[V][1] = cell->margin[V][1],
77 .font_style.fg[0] = font->fg[0],
78 .font_style.fg[1] = font->fg[1],
79 .font_style.bg[0] = font->bg[0],
80 .font_style.bg[1] = font->bg[1],
81 .font_style.typeface = (font->typeface
82 ? pool_strdup (pool, font->typeface)
84 .font_style.size = font->size,
85 .font_style.bold = font->bold,
86 .font_style.italic = font->italic,
87 .font_style.underline = font->underline,
88 .font_style.markup = font->markup,
94 fill_cell (struct table *t, int x1, int y1, int x2, int y2,
95 const struct table_area_style *style, int style_idx,
96 const struct pivot_value *value, struct footnote **footnotes,
97 enum settings_value_show show_values,
98 enum settings_value_show show_variables,
102 struct string s = DS_EMPTY_INITIALIZER;
103 int opts = style_idx << TAB_STYLE_SHIFT;
106 bool numeric = pivot_value_format_body (value, show_values,
110 if (value->font_style && value->font_style->markup)
115 table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
120 if (value->cell_style || value->font_style)
121 table_add_style (t, x1, y1,
122 table_area_style_override (t->container, style,
126 for (size_t i = 0; i < value->n_footnotes; i++)
128 struct footnote *f = footnotes[value->footnotes[i]->idx];
130 table_add_footnote (t, x1, y1, f);
133 if (value->n_subscripts)
134 table_add_subscripts (t, x1, y1,
135 value->subscripts, value->n_subscripts);
139 static struct table_item_text *
140 pivot_value_to_table_item_text (const struct pivot_value *value,
141 const struct table_area_style *area,
142 struct footnote **footnotes,
143 enum settings_value_show show_values,
144 enum settings_value_show show_variables)
149 struct string s = DS_EMPTY_INITIALIZER;
150 pivot_value_format_body (value, show_values, show_variables, &s);
152 struct table_item_text *text = xmalloc (sizeof *text);
153 *text = (struct table_item_text) {
154 .content = ds_steal_cstr (&s),
155 .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
156 .style = table_area_style_override (
157 NULL, area, value->cell_style, value->font_style),
160 for (size_t i = 0; i < value->n_footnotes; i++)
162 struct footnote *f = footnotes[value->footnotes[i]->idx];
164 text->footnotes[text->n_footnotes++] = f;
171 get_table_rule (const struct table_border_style *styles,
172 enum pivot_border style_idx)
174 return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
178 draw_line (struct table *t, const struct table_border_style *styles,
179 enum pivot_border style_idx,
180 enum table_axis axis, int a, int b0, int b1)
182 int rule = get_table_rule (styles, style_idx);
184 table_hline (t, rule, b0, b1, a);
186 table_vline (t, rule, a, b0, b1);
190 compose_headings (struct table *t,
191 const struct pivot_axis *a_axis, enum table_axis a,
192 const struct pivot_axis *b_axis,
193 const struct table_border_style *borders,
194 enum pivot_border dim_col_horz,
195 enum pivot_border dim_col_vert,
196 enum pivot_border cat_col_horz,
197 enum pivot_border cat_col_vert,
198 const size_t *column_enumeration, size_t n_columns,
199 const struct table_area_style *label_style,
201 const struct table_area_style *corner_style,
202 struct footnote **footnotes,
203 enum settings_value_show show_values,
204 enum settings_value_show show_variables,
205 bool rotate_inner_labels, bool rotate_outer_labels)
207 enum table_axis b = !a;
208 int b_size = a_axis->label_depth;
209 int a_ofs = b_axis->label_depth;
211 if (!a_axis->n_dimensions || !n_columns || !b_size)
214 int bottom_row = b_size - 1;
215 const int stride = MAX (1, a_axis->n_dimensions);
216 for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
218 const struct pivot_dimension *d = a_axis->dimensions[dim_index];
219 if (d->hide_all_labels)
222 for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
224 for (size_t x1 = 0; x1 < n_columns;)
226 const struct pivot_category *c = find_category (
227 d, dim_index, column_enumeration + x1 * stride, row_ofs);
235 for (x2 = x1 + 1; x2 < n_columns; x2++)
237 const struct pivot_category *c2 = find_category (
238 d, dim_index, column_enumeration + x2 * stride, row_ofs);
243 int y1 = bottom_row - row_ofs - c->extra_depth;
244 int y2 = bottom_row - row_ofs + 1;
245 bool is_outer_row = y1 == 0;
246 bool is_inner_row = y2 == b_size;
247 if (pivot_category_is_leaf (c) || c->show_label)
249 int bb[TABLE_N_AXES][2];
250 bb[a][0] = x1 + a_ofs;
251 bb[a][1] = x2 + a_ofs - 1;
254 bool rotate = ((rotate_inner_labels && is_inner_row)
255 || (rotate_outer_labels && is_outer_row));
256 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
257 label_style, label_style_idx, c->name, footnotes,
258 show_values, show_variables, rotate);
260 if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
262 enum pivot_border style
263 = (y1 == 0 && a_axis->label_depth > d->label_depth
266 draw_line (t, borders, style, b, x2 + a_ofs, y1,
269 if (pivot_category_is_leaf (c) && x1 > 0)
271 enum pivot_border style
272 = (y1 == 0 && a_axis->label_depth > d->label_depth
275 draw_line (t, borders, style, b, x1 + a_ofs, y1,
279 if (c->parent && c->parent->show_label)
280 draw_line (t, borders, cat_col_horz, a, y1,
281 x1 + a_ofs, x2 + a_ofs - 1);
287 if (d->root->show_label_in_corner && a_ofs > 0)
289 int bb[TABLE_N_AXES][2];
290 bb[a][0] = a_ofs - 1;
291 bb[a][1] = a_ofs - 1;
292 bb[b][0] = bottom_row - d->label_depth + 1;
293 bb[b][1] = bottom_row;
294 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
295 corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
296 show_values, show_variables, false);
300 draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
303 bottom_row -= d->label_depth;
308 pivot_table_submit_layer (const struct pivot_table *pt,
309 const size_t *layer_indexes)
311 const size_t *pindexes[PIVOT_N_AXES]
312 = { [PIVOT_AXIS_LAYER] = layer_indexes };
314 size_t body[TABLE_N_AXES];
315 size_t *column_enumeration = pivot_table_enumerate_axis (
316 pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look.omit_empty, &body[H]);
317 size_t *row_enumeration = pivot_table_enumerate_axis (
318 pt, PIVOT_AXIS_ROW, layer_indexes, pt->look.omit_empty, &body[V]);
320 int stub[TABLE_N_AXES] = {
321 [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
322 [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
324 struct table *table = table_create (body[H] + stub[H],
326 stub[H], 0, stub[V], 0);
328 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
329 table->styles[i] = table_area_style_override (
330 table->container, &pt->look.areas[i], NULL, NULL);
332 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
334 const struct table_border_style *in = &pt->look.borders[i];
335 table->rule_colors[i] = pool_alloc (table->container,
336 sizeof *table->rule_colors[i]);
337 struct cell_color *out = table->rule_colors[i];
338 out->alpha = in->color.alpha;
339 out->r = in->color.r;
340 out->g = in->color.g;
341 out->b = in->color.b;
344 struct footnote **footnotes = XCALLOC (pt->n_footnotes, struct footnote *);
345 for (size_t i = 0; i < pt->n_footnotes; i++)
347 const struct pivot_footnote *pf = pt->footnotes[i];
352 char *content = pivot_value_to_string (pf->content, pt->show_values,
354 char *marker = pivot_value_to_string (pf->marker, pt->show_values,
356 footnotes[i] = table_create_footnote (
357 table, i, content, marker,
358 table_area_style_override (table->container,
359 &pt->look.areas[PIVOT_AREA_FOOTER],
360 pf->content->cell_style,
361 pf->content->font_style));
366 compose_headings (table,
367 &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
369 PIVOT_BORDER_DIM_COL_HORZ,
370 PIVOT_BORDER_DIM_COL_VERT,
371 PIVOT_BORDER_CAT_COL_HORZ,
372 PIVOT_BORDER_CAT_COL_VERT,
373 column_enumeration, body[H],
374 &pt->look.areas[PIVOT_AREA_COLUMN_LABELS],
375 PIVOT_AREA_COLUMN_LABELS,
376 &pt->look.areas[PIVOT_AREA_CORNER], footnotes,
377 pt->show_values, pt->show_variables,
378 pt->rotate_inner_column_labels, false);
380 compose_headings (table,
381 &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
383 PIVOT_BORDER_DIM_ROW_VERT,
384 PIVOT_BORDER_DIM_ROW_HORZ,
385 PIVOT_BORDER_CAT_ROW_VERT,
386 PIVOT_BORDER_CAT_ROW_HORZ,
387 row_enumeration, body[V],
388 &pt->look.areas[PIVOT_AREA_ROW_LABELS],
389 PIVOT_AREA_ROW_LABELS,
390 &pt->look.areas[PIVOT_AREA_CORNER], footnotes,
391 pt->show_values, pt->show_variables,
392 false, pt->rotate_outer_row_labels);
394 size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
396 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
397 &pt->axes[PIVOT_AXIS_ROW])
400 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
402 &pt->axes[PIVOT_AXIS_COLUMN])
404 pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
405 const struct pivot_value *value = pivot_table_get (pt, dindexes);
407 x + stub[H], y + stub[V],
408 x + stub[H], y + stub[V],
409 &pt->look.areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
411 pt->show_values, pt->show_variables, false);
420 if (pt->corner_text && stub[H] && stub[V])
421 fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
422 &pt->look.areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
423 pt->corner_text, footnotes,
424 pt->show_values, pt->show_variables, false);
426 if (table_nc (table) && table_nr (table))
429 table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_TOP),
430 0, table_nc (table) - 1, 0);
432 table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_BOTTOM),
433 0, table_nc (table) - 1, table_nr (table));
435 table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_LEFT),
436 0, 0, table_nr (table) - 1);
438 table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_RIGHT),
439 table_nc (table), 0, table_nr (table) - 1);
443 table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_TOP),
444 0, table_nc (table) - 1, stub[V]);
447 table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_LEFT),
448 stub[H], 0, table_nr (table) - 1);
451 free (column_enumeration);
452 free (row_enumeration);
454 struct table_item *ti = table_item_create (table, NULL, NULL);
458 struct table_item_text *title = pivot_value_to_table_item_text (
459 pt->title, &pt->look.areas[PIVOT_AREA_TITLE], footnotes,
460 pt->show_values, pt->show_variables);
461 table_item_set_title (ti, title);
462 table_item_text_destroy (title);
465 const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
466 struct table_item_layers *layers = NULL;
467 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
469 const struct pivot_dimension *d = layer_axis->dimensions[i];
474 layers = xzalloc (sizeof *layers);
475 layers->style = table_area_style_override (
476 NULL, &pt->look.areas[PIVOT_AREA_LAYERS], NULL, NULL);
477 layers->layers = xnmalloc (layer_axis->n_dimensions,
478 sizeof *layers->layers);
481 const struct pivot_value *name
482 = d->data_leaves[layer_indexes[i]]->name;
483 struct table_item_layer *layer = &layers->layers[layers->n_layers++];
484 struct string s = DS_EMPTY_INITIALIZER;
485 pivot_value_format_body (name, pt->show_values, pt->show_variables,
487 layer->content = ds_steal_cstr (&s);
488 layer->n_footnotes = 0;
489 layer->footnotes = xnmalloc (name->n_footnotes,
490 sizeof *layer->footnotes);
491 for (size_t i = 0; i < name->n_footnotes; i++)
493 struct footnote *f = footnotes[name->footnotes[i]->idx];
495 layer->footnotes[layer->n_footnotes++] = f;
501 table_item_set_layers (ti, layers);
502 table_item_layers_destroy (layers);
505 if (pt->caption && pt->show_caption)
507 struct table_item_text *caption = pivot_value_to_table_item_text (
508 pt->caption, &pt->look.areas[PIVOT_AREA_CAPTION], footnotes,
509 pt->show_values, pt->show_variables);
510 table_item_set_caption (ti, caption);
511 table_item_text_destroy (caption);
515 ti->pt = pivot_table_ref (pt);
517 table_item_submit (ti);
521 pivot_table_submit (struct pivot_table *pt)
523 pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
525 int old_decimal = settings_get_decimal_char (FMT_COMMA);
526 if (pt->decimal == '.' || pt->decimal == ',')
527 settings_set_decimal_char (pt->decimal);
529 if (pt->look.print_all_layers)
531 size_t *layer_indexes;
533 PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
535 if (pt->look.paginate_layers)
536 page_eject_item_submit (page_eject_item_create ());
537 pivot_table_submit_layer (pt, layer_indexes);
541 pivot_table_submit_layer (pt, pt->current_layer);
543 settings_set_decimal_char (old_decimal);
545 pivot_table_unref (pt);