X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Foutput%2Fpivot-output.c;h=8cde929c500b6f16e03be9f934768b80157784fb;hb=052cf5f9ffd4fbbda175fcb0417352b332fabc37;hp=8ca3601cfcbaf0882dbd903ad2fa47929712bcce;hpb=5164eb1bfa0527dcb32248fb1fc7db55c2bcc94c;p=pspp diff --git a/src/output/pivot-output.c b/src/output/pivot-output.c index 8ca3601cfc..8cde929c50 100644 --- a/src/output/pivot-output.c +++ b/src/output/pivot-output.c @@ -24,6 +24,7 @@ #include "libpspp/assertion.h" #include "libpspp/pool.h" #include "output/table.h" +#include "output/page-eject-item.h" #include "output/table-item.h" #include "output/text-item.h" #include "output/table-provider.h" @@ -43,7 +44,9 @@ find_category (const struct pivot_dimension *d, int dim_index, for (const struct pivot_category *c = d->presentation_leaves[index]; c; c = c->parent) { - if (!row_ofs) + /* A category can covert multiple rows. Only return the category for its + top row. */ + if (row_ofs == c->extra_depth) return c; row_ofs -= 1 + c->extra_depth; @@ -57,7 +60,8 @@ static struct table_area_style * table_area_style_override (struct pool *pool, const struct table_area_style *in, const struct cell_style *cell_, - const struct font_style *font_) + const struct font_style *font_, + bool rotate_label) { const struct cell_style *cell = cell_ ? cell_ : &in->cell_style; const struct font_style *font = font_ ? font_ : &in->font_style; @@ -66,8 +70,8 @@ table_area_style_override (struct pool *pool, ? pool_alloc (pool, sizeof *out) : xmalloc (sizeof *out)); *out = (struct table_area_style) { - .cell_style.halign = cell->halign, - .cell_style.valign = cell->valign, + .cell_style.halign = rotate_label ? TABLE_HALIGN_CENTER : cell->halign, + .cell_style.valign = rotate_label ? TABLE_VALIGN_CENTER : cell->valign, .cell_style.decimal_offset = cell->decimal_offset, .cell_style.margin[H][0] = cell->margin[H][0], .cell_style.margin[H][1] = cell->margin[H][1], @@ -89,6 +93,27 @@ table_area_style_override (struct pool *pool, return out; } +static int +format_cell (const struct pivot_value *value, int style_idx, + enum settings_value_show show_values, + enum settings_value_show show_variables, + bool rotate_label, struct string *s) +{ + int options = style_idx << TAB_STYLE_SHIFT; + if (value) + { + bool numeric = pivot_value_format_body (value, show_values, + show_variables, s); + if (numeric) + options |= TAB_NUMERIC; + if (value->font_style && value->font_style->markup) + options |= TAB_MARKUP; + if (rotate_label) + options |= TAB_ROTATE; + } + return options; +} + static void fill_cell (struct table *t, int x1, int y1, int x2, int y2, const struct table_area_style *style, int style_idx, @@ -97,30 +122,20 @@ fill_cell (struct table *t, int x1, int y1, int x2, int y2, enum settings_value_show show_variables, bool rotate_label) { - struct string s = DS_EMPTY_INITIALIZER; - int opts = style_idx << TAB_STYLE_SHIFT; - if (value) - { - bool numeric = pivot_value_format_body (value, show_values, - show_variables, &s); - if (numeric) - opts |= TAB_NUMERIC; - if (value->font_style && value->font_style->markup) - opts |= TAB_MARKUP; - if (rotate_label) - opts |= TAB_ROTATE; - } - table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s)); + int options = format_cell (value, style_idx, + show_values, show_variables, rotate_label, &s); + table_joint_text (t, x1, y1, x2, y2, options, ds_cstr (&s)); ds_destroy (&s); if (value) { - if (value->cell_style || value->font_style) + if (value->cell_style || value->font_style || rotate_label) table_add_style (t, x1, y1, table_area_style_override (t->container, style, value->cell_style, - value->font_style)); + value->font_style, + rotate_label)); for (size_t i = 0; i < value->n_footnotes; i++) { @@ -135,35 +150,49 @@ fill_cell (struct table *t, int x1, int y1, int x2, int y2, } } -static struct table_item_text * -pivot_value_to_table_item_text (const struct pivot_value *value, - const struct table_area_style *area, - struct footnote **footnotes, - enum settings_value_show show_values, - enum settings_value_show show_variables) +static struct table_cell * +pivot_value_to_table_cell (const struct pivot_value *value, + const struct table_area_style *style, int style_idx, + struct footnote **footnotes, + enum settings_value_show show_values, + enum settings_value_show show_variables) { if (!value) return NULL; struct string s = DS_EMPTY_INITIALIZER; - pivot_value_format_body (value, show_values, show_variables, &s); + int options = format_cell (value, style_idx, + show_values, show_variables, false, &s); - struct table_item_text *text = xmalloc (sizeof *text); - *text = (struct table_item_text) { - .content = ds_steal_cstr (&s), - .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes), + struct table_cell *cell = xmalloc (sizeof *cell); + *cell = (struct table_cell) { + .options = options, + .text = ds_steal_cstr (&s), .style = table_area_style_override ( - NULL, area, value->cell_style, value->font_style), + NULL, style, value->cell_style, value->font_style, false), }; - for (size_t i = 0; i < value->n_footnotes; i++) + if (value->n_subscripts) { - struct footnote *f = footnotes[value->footnotes[i]->idx]; - if (f) - text->footnotes[text->n_footnotes++] = f; + cell->subscripts = xnmalloc (value->n_subscripts, + sizeof *cell->subscripts); + cell->n_subscripts = value->n_subscripts; + for (size_t i = 0; i < value->n_subscripts; i++) + cell->subscripts[i] = xstrdup (value->subscripts[i]); } - return text; + if (value->n_footnotes) + { + cell->footnotes = xnmalloc (value->n_footnotes, sizeof *cell->footnotes); + for (size_t i = 0; i < value->n_footnotes; i++) + { + struct footnote *f = footnotes[value->footnotes[i]->idx]; + if (f) + cell->footnotes[cell->n_footnotes++] = f; + } + } + + return cell; } static int @@ -185,6 +214,13 @@ draw_line (struct table *t, const struct table_border_style *styles, table_vline (t, rule, a, b0, b1); } +/* Fills row or column headings into T. + + This function uses terminology and variable names for column headings, but + it also applies to row headings because it uses variables for the + differences, e.g. when for column headings it would use the H axis, it + instead uses 'h', which is set to H for column headings and V for row + headings. */ static void compose_headings (struct table *t, const struct pivot_axis *a_axis, enum table_axis a, @@ -210,9 +246,61 @@ compose_headings (struct table *t, if (!a_axis->n_dimensions || !n_columns || !b_size) return; - int bottom_row = b_size - 1; const int stride = MAX (1, a_axis->n_dimensions); - for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++) + + /* Below, we're going to iterate through the dimensions. Each dimension + occupies one or more rows in the heading. 'top_row' is the top row of + these (and 'top_row + d->label_depth - 1' is the bottom row). */ + int top_row = 0; + + /* We're going to iterate through dimensions and the rows that label them + from top to bottom (from outer to inner dimensions). As we move downward, + we start drawing vertical rules to separate categories and groups. After + we start drawing a vertical rule in a particular horizontal position, it + continues until the bottom of the heading. vrules[pos] indicates whether, + in our current row, we have already started drawing a vertical rule in + horizontal position 'pos'. (There are n_columns + 1 horizontal positions. + We allocate all of them for convenience below but only the inner n_columns + - 1 of them really matter.) + + Here's an example that shows how vertical rules continue all the way + downward: + + +-----------------------------------------------------+ __ + | bbbb | | + +-----------------+-----------------+-----------------+ |dimension "bbbb" + | bbbb1 | bbbb2 | bbbb3 | _| + +-----------------+-----------------+-----------------+ __ + | aaaa | aaaa | aaaa | | + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ |dimension "aaaa" + |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3| _| + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + + ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ + | | | | | | | | | | + 0 1 2 3 4 5 6 7 8 9 + |___________________vrules[] indexes__________________| + + Our data structures are more naturally iterated from bottom to top (inner + to outer dimensions). A previous version of this code actually worked + like that, but it didn't draw all of the vertical lines correctly as shown + above. It ended up rendering the above heading much like shown below, + which isn't what users expect. The "aaaa" label really needs to be shown + three times for clarity: + + +-----------------------------------------------------+ + | bbbb | + +-----------------+-----------------+-----------------+ + | bbbb1 | bbbb2 | bbbb3 | + +-----------------+-----------------+-----------------+ + | | aaaa | | + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3| + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + */ + bool *vrules = xzalloc (n_columns + 1); + vrules[0] = vrules[n_columns] = true; + for (int dim_index = a_axis->n_dimensions; --dim_index >= 0; ) { const struct pivot_dimension *d = a_axis->dimensions[dim_index]; if (d->hide_all_labels) @@ -223,7 +311,8 @@ compose_headings (struct table *t, for (size_t x1 = 0; x1 < n_columns;) { const struct pivot_category *c = find_category ( - d, dim_index, column_enumeration + x1 * stride, row_ofs); + d, dim_index, column_enumeration + x1 * stride, + d->label_depth - row_ofs - 1); if (!c) { x1++; @@ -233,14 +322,17 @@ compose_headings (struct table *t, size_t x2; for (x2 = x1 + 1; x2 < n_columns; x2++) { + if (vrules[x2]) + break; const struct pivot_category *c2 = find_category ( - d, dim_index, column_enumeration + x2 * stride, row_ofs); + d, dim_index, column_enumeration + x2 * stride, + d->label_depth - row_ofs - 1); if (c != c2) break; } - int y1 = bottom_row - row_ofs - c->extra_depth; - int y2 = bottom_row - row_ofs + 1; + int y1 = top_row + row_ofs; + int y2 = top_row + row_ofs + c->extra_depth + 1; bool is_outer_row = y1 == 0; bool is_inner_row = y2 == b_size; if (pivot_category_is_leaf (c) || c->show_label) @@ -256,29 +348,57 @@ compose_headings (struct table *t, label_style, label_style_idx, c->name, footnotes, show_values, show_variables, rotate); - if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns) + /* Draw all the vertical lines in our running example, other + than the far left and far right ones. Only the ones that + start in the last row of the heading are drawn with the + "category" style, the rest with the "dimension" style, + e.g. only the # below are category style: + + +-----------------------------------------------------+ + | bbbb | + +-----------------+-----------------+-----------------+ + | bbbb1 | bbbb2 | bbbb3 | + +-----------------+-----------------+-----------------+ + | aaaa | aaaa | aaaa | + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + |aaaa1#aaaa2#aaaa3|aaaa1#aaaa2#aaaa3|aaaa1#aaaa2#aaaa3| + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + */ + enum pivot_border style + = (y1 == b_size - 1 ? cat_col_vert : dim_col_vert); + if (!vrules[x2]) { - enum pivot_border style - = (y1 == 0 && a_axis->label_depth > d->label_depth - ? dim_col_vert - : cat_col_vert); draw_line (t, borders, style, b, x2 + a_ofs, y1, t->n[b] - 1); + vrules[x2] = true; } - if (pivot_category_is_leaf (c) && x1 > 0) + if (!vrules[x1]) { - enum pivot_border style - = (y1 == 0 && a_axis->label_depth > d->label_depth - ? dim_col_vert - : cat_col_vert); draw_line (t, borders, style, b, x1 + a_ofs, y1, t->n[b] - 1); + vrules[x1] = true; } } + + /* Draws the horizontal lines within a dimension, that is, those + that separate a separating a category (or group) from its + parent group or dimension's label. Our running example + doesn't have groups but the ==== lines below show the + separators between categories and their dimension label: + + +-----------------------------------------------------+ + | bbbb | + +=================+=================+=================+ + | bbbb1 | bbbb2 | bbbb3 | + +-----------------+-----------------+-----------------+ + | aaaa | aaaa | aaaa | + +=====+=====+=====+=====+=====+=====+=====+=====+=====+ + |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3| + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + */ if (c->parent && c->parent->show_label) draw_line (t, borders, cat_col_horz, a, y1, x1 + a_ofs, x2 + a_ofs - 1); - x1 = x2; } } @@ -286,21 +406,33 @@ compose_headings (struct table *t, if (d->root->show_label_in_corner && a_ofs > 0) { int bb[TABLE_N_AXES][2]; - bb[a][0] = a_ofs - 1; + bb[a][0] = 0; bb[a][1] = a_ofs - 1; - bb[b][0] = bottom_row - d->label_depth + 1; - bb[b][1] = bottom_row; + bb[b][0] = top_row; + bb[b][1] = top_row + d->label_depth - 1; fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1], corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes, show_values, show_variables, false); } - if (dim_index > 1) - draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs, + /* Draw the horizontal line between dimensions, e.g. the ===== line here: + + +-----------------------------------------------------+ __ + | bbbb | | + +-----------------+-----------------+-----------------+ |dim "bbbb" + | bbbb1 | bbbb2 | bbbb3 | _| + +=================+=================+=================+ __ + | aaaa | aaaa | aaaa | | + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ |dim "aaaa" + |aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3|aaaa1|aaaa2|aaaa3| _| + +-----+-----+-----+-----+-----+-----+-----+-----+-----+ + */ + if (dim_index != a_axis->n_dimensions - 1) + draw_line (t, borders, dim_col_horz, a, top_row, a_ofs, t->n[a] - 1); - - bottom_row -= d->label_depth; + top_row += d->label_depth; } + free (vrules); } static void @@ -312,9 +444,9 @@ pivot_table_submit_layer (const struct pivot_table *pt, size_t body[TABLE_N_AXES]; size_t *column_enumeration = pivot_table_enumerate_axis ( - pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look.omit_empty, &body[H]); + pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look->omit_empty, &body[H]); size_t *row_enumeration = pivot_table_enumerate_axis ( - pt, PIVOT_AXIS_ROW, layer_indexes, pt->look.omit_empty, &body[V]); + pt, PIVOT_AXIS_ROW, layer_indexes, pt->look->omit_empty, &body[V]); int stub[TABLE_N_AXES] = { [H] = pt->axes[PIVOT_AXIS_ROW].label_depth, @@ -326,11 +458,11 @@ pivot_table_submit_layer (const struct pivot_table *pt, for (size_t i = 0; i < PIVOT_N_AREAS; i++) table->styles[i] = table_area_style_override ( - table->container, &pt->look.areas[i], NULL, NULL); + table->container, &pt->look->areas[i], NULL, NULL, false); for (size_t i = 0; i < PIVOT_N_BORDERS; i++) { - const struct table_border_style *in = &pt->look.borders[i]; + const struct table_border_style *in = &pt->look->borders[i]; table->rule_colors[i] = pool_alloc (table->container, sizeof *table->rule_colors[i]); struct cell_color *out = table->rule_colors[i]; @@ -355,40 +487,41 @@ pivot_table_submit_layer (const struct pivot_table *pt, footnotes[i] = table_create_footnote ( table, i, content, marker, table_area_style_override (table->container, - &pt->look.areas[PIVOT_AREA_FOOTER], + &pt->look->areas[PIVOT_AREA_FOOTER], pf->content->cell_style, - pf->content->font_style)); + pf->content->font_style, + false)); free (marker); free (content); } compose_headings (table, &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW], - pt->look.borders, + pt->look->borders, PIVOT_BORDER_DIM_COL_HORZ, PIVOT_BORDER_DIM_COL_VERT, PIVOT_BORDER_CAT_COL_HORZ, PIVOT_BORDER_CAT_COL_VERT, column_enumeration, body[H], - &pt->look.areas[PIVOT_AREA_COLUMN_LABELS], + &pt->look->areas[PIVOT_AREA_COLUMN_LABELS], PIVOT_AREA_COLUMN_LABELS, - &pt->look.areas[PIVOT_AREA_CORNER], footnotes, + &pt->look->areas[PIVOT_AREA_CORNER], footnotes, pt->show_values, pt->show_variables, - pt->rotate_inner_column_labels, false); + pt->rotate_outer_row_labels, false); compose_headings (table, &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN], - pt->look.borders, + pt->look->borders, PIVOT_BORDER_DIM_ROW_VERT, PIVOT_BORDER_DIM_ROW_HORZ, PIVOT_BORDER_CAT_ROW_VERT, PIVOT_BORDER_CAT_ROW_HORZ, row_enumeration, body[V], - &pt->look.areas[PIVOT_AREA_ROW_LABELS], + &pt->look->areas[PIVOT_AREA_ROW_LABELS], PIVOT_AREA_ROW_LABELS, - &pt->look.areas[PIVOT_AREA_CORNER], footnotes, + &pt->look->areas[PIVOT_AREA_CORNER], footnotes, pt->show_values, pt->show_variables, - false, pt->rotate_outer_row_labels); + false, pt->rotate_inner_column_labels); size_t *dindexes = XCALLOC (pt->n_dimensions, size_t); size_t y = 0; @@ -405,7 +538,7 @@ pivot_table_submit_layer (const struct pivot_table *pt, fill_cell (table, x + stub[H], y + stub[V], x + stub[H], y + stub[V], - &pt->look.areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA, + &pt->look->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA, value, footnotes, pt->show_values, pt->show_variables, false); @@ -416,49 +549,53 @@ pivot_table_submit_layer (const struct pivot_table *pt, } free (dindexes); - if (pt->corner_text && stub[H] && stub[V]) + if ((pt->corner_text || !pt->look->row_labels_in_corner) + && stub[H] && stub[V]) fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1, - &pt->look.areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER, + &pt->look->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER, pt->corner_text, footnotes, pt->show_values, pt->show_variables, false); - if (table_nc (table) && table_nr (table)) + if (table->n[H] && table->n[V]) { table_hline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_TOP), - 0, table_nc (table) - 1, 0); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_TOP), + 0, table->n[H] - 1, 0); table_hline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_BOTTOM), - 0, table_nc (table) - 1, table_nr (table)); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_BOTTOM), + 0, table->n[H] - 1, table->n[V]); table_vline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_LEFT), - 0, 0, table_nr (table) - 1); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_LEFT), + 0, 0, table->n[V] - 1); table_vline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_INNER_RIGHT), - table_nc (table), 0, table_nr (table) - 1); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_RIGHT), + table->n[H], 0, table->n[V] - 1); if (stub[V]) table_hline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_TOP), - 0, table_nc (table) - 1, stub[V]); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_TOP), + 0, table->n[H] - 1, stub[V]); if (stub[H]) table_vline ( - table, get_table_rule (pt->look.borders, PIVOT_BORDER_DATA_LEFT), - stub[H], 0, table_nr (table) - 1); + table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_LEFT), + stub[H], 0, table->n[V] - 1); } free (column_enumeration); free (row_enumeration); - struct table_item *ti = table_item_create (table, NULL, NULL); + struct table_item *ti = table_item_create (table); + + if (pt->notes) + table_item_set_notes (ti, pt->notes); - if (pt->title) + if (pt->title && pt->show_title) { - struct table_item_text *title = pivot_value_to_table_item_text ( - pt->title, &pt->look.areas[PIVOT_AREA_TITLE], footnotes, - pt->show_values, pt->show_variables); + struct table_cell *title = pivot_value_to_table_cell ( + pt->title, &pt->look->areas[PIVOT_AREA_TITLE], PIVOT_AREA_TITLE, + footnotes, pt->show_values, pt->show_variables); table_item_set_title (ti, title); - table_item_text_destroy (title); + table_cell_destroy (title); } const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER]; @@ -472,7 +609,7 @@ pivot_table_submit_layer (const struct pivot_table *pt, { layers = xzalloc (sizeof *layers); layers->style = table_area_style_override ( - NULL, &pt->look.areas[PIVOT_AREA_LAYERS], NULL, NULL); + NULL, &pt->look->areas[PIVOT_AREA_LAYERS], NULL, NULL, false); layers->layers = xnmalloc (layer_axis->n_dimensions, sizeof *layers->layers); } @@ -503,11 +640,11 @@ pivot_table_submit_layer (const struct pivot_table *pt, if (pt->caption && pt->show_caption) { - struct table_item_text *caption = pivot_value_to_table_item_text ( - pt->caption, &pt->look.areas[PIVOT_AREA_CAPTION], footnotes, - pt->show_values, pt->show_variables); + struct table_cell *caption = pivot_value_to_table_cell ( + pt->caption, &pt->look->areas[PIVOT_AREA_CAPTION], PIVOT_AREA_CAPTION, + footnotes, pt->show_values, pt->show_variables); table_item_set_caption (ti, caption); - table_item_text_destroy (caption); + table_cell_destroy (caption); } free (footnotes); @@ -525,14 +662,14 @@ pivot_table_submit (struct pivot_table *pt) if (pt->decimal == '.' || pt->decimal == ',') settings_set_decimal_char (pt->decimal); - if (pt->look.print_all_layers) + if (pt->look->print_all_layers) { size_t *layer_indexes; PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER]) { - if (pt->look.paginate_layers) - text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, "")); + if (pt->look->paginate_layers) + page_eject_item_submit (page_eject_item_create ()); pivot_table_submit_layer (pt, layer_indexes); } }