From 5e8eab0fb47cff52247ef61713003537dd986118 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 10 Jan 2021 18:20:11 -0800 Subject: [PATCH] output-item: Make command name part of every output_item. Until now, group_items and text_items have had a command name, but SPV files make the command name part of every output_item. This change make output_item better aligned. --- src/language/command.c | 5 ++- src/output/cairo-pager.c | 4 +- src/output/driver.c | 2 +- src/output/group-item.c | 7 ++-- src/output/group-item.h | 5 --- src/output/output-item-provider.h | 1 + src/output/output-item.c | 2 + src/output/output-item.h | 5 +++ src/output/spv/spv-writer.c | 65 +++++++++++++++---------------- src/output/table-item.c | 1 + src/output/text-item.c | 1 + 11 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/language/command.c b/src/language/command.c index de20cb3caa..639bf4a3c0 100644 --- a/src/language/command.c +++ b/src/language/command.c @@ -198,8 +198,9 @@ do_parse_command (struct lexer *lexer, result = CMD_FAILURE; goto finish; } - group_open_item_submit (group_open_item_create_nocopy ( - utf8_to_title (command->name), NULL)); + group_open_item_submit ( + group_open_item_create_nocopy (utf8_to_title (command->name), + utf8_to_title (command->name))); opened = true; if (command->function == NULL) diff --git a/src/output/cairo-pager.c b/src/output/cairo-pager.c index 057ad41c6c..e1cae64819 100644 --- a/src/output/cairo-pager.c +++ b/src/output/cairo-pager.c @@ -436,8 +436,8 @@ xr_pager_run (struct xr_pager *p) { parent_group_id = add_outline ( p->cr, parent_group_id, - p->group_opens[i]->command_name, attrs, - CAIRO_PDF_OUTLINE_FLAG_OPEN); + output_item_get_label (&p->group_opens[i]->output_item), + attrs, CAIRO_PDF_OUTLINE_FLAG_OPEN); group_open_item_unref (p->group_opens[i]); if (p->n_group_ids >= p->allocated_group_ids) diff --git a/src/output/driver.c b/src/output/driver.c index 95dcbfd94a..6c4fd414cb 100644 --- a/src/output/driver.c +++ b/src/output/driver.c @@ -228,7 +228,7 @@ output_submit (struct output_item *item) e->groups = x2nrealloc (e->groups, &e->allocated_groups, sizeof *e->groups); e->groups[e->n_groups] = xstrdup_if_nonnull ( - group_open_item->command_name); + group_open_item->output_item.command_name); e->n_groups++; } else if (is_group_close_item (item)) diff --git a/src/output/group-item.c b/src/output/group-item.c index 1c8fce5e8b..c0aad17d9f 100644 --- a/src/output/group-item.c +++ b/src/output/group-item.c @@ -45,7 +45,7 @@ group_open_item_create_nocopy (char *command_name, char *label) *item = (struct group_open_item) { .output_item = OUTPUT_ITEM_INITIALIZER (&group_open_item_class), .output_item.label = label, - .command_name = command_name, + .output_item.command_name = command_name, }; return item; } @@ -63,7 +63,9 @@ group_open_item_get_label (const struct output_item *output_item) { struct group_open_item *item = to_group_open_item (output_item); - return item->command_name ? item->command_name : _("Group"); + return (item->output_item.command_name + ? item->output_item.command_name + : _("Group")); } static void @@ -71,7 +73,6 @@ group_open_item_destroy (struct output_item *output_item) { struct group_open_item *item = to_group_open_item (output_item); - free (item->command_name); free (item); } diff --git a/src/output/group-item.h b/src/output/group-item.h index 61593632cd..b8ac464c3d 100644 --- a/src/output/group-item.h +++ b/src/output/group-item.h @@ -29,11 +29,6 @@ struct group_open_item { struct output_item output_item; - - /* Locale-invariant name of the command that produced the enclosed output. - May be NULL if the group doesn't enclose a particular command's - output. */ - char *command_name; }; struct group_open_item *group_open_item_create (const char *command_name, diff --git a/src/output/output-item-provider.h b/src/output/output-item-provider.h index 552549cee9..62e7561559 100644 --- a/src/output/output-item-provider.h +++ b/src/output/output-item-provider.h @@ -42,6 +42,7 @@ struct output_item_class .class = (SRC)->class, \ .ref_cnt = 1, \ .label = xstrdup_if_nonnull ((SRC)->label), \ + .command_name = xstrdup_if_nonnull ((SRC)->command_name), \ } #endif /* output/output-item-provider.h */ diff --git a/src/output/output-item.c b/src/output/output-item.c index cd37031b65..f80ea19c46 100644 --- a/src/output/output-item.c +++ b/src/output/output-item.c @@ -51,8 +51,10 @@ output_item_unref (struct output_item *item) if (--item->ref_cnt == 0) { char *label = item->label; + char *command_name = item->command_name; item->class->destroy (item); free (label); + free (command_name); } } } diff --git a/src/output/output-item.h b/src/output/output-item.h index b522df4c90..1580d1e1eb 100644 --- a/src/output/output-item.h +++ b/src/output/output-item.h @@ -42,6 +42,11 @@ struct output_item Use output_item_get_label() to read an item's label. */ char *label; + + /* A locale-invariant identifier for the command that produced the output, + which may be NULL if unknown or if a command did not produce this + output. */ + char *command_name; }; struct output_item *output_item_ref (const struct output_item *); diff --git a/src/output/spv/spv-writer.c b/src/output/spv/spv-writer.c index 4bdac19d7b..e3f3b28ffa 100644 --- a/src/output/spv/spv-writer.c +++ b/src/output/spv/spv-writer.c @@ -227,8 +227,7 @@ spv_writer_open_file (struct spv_writer *w) } static void -spv_writer_open_heading (struct spv_writer *w, const char *command_id, - const char *label) +spv_writer_open_heading (struct spv_writer *w, const struct output_item *item) { if (!w->heading) { @@ -238,12 +237,13 @@ spv_writer_open_heading (struct spv_writer *w, const char *command_id, w->heading_depth++; start_elem (w, "heading"); - write_attr (w, "commandName", command_id); + if (item->command_name) + write_attr (w, "commandName", item->command_name); /* XXX locale */ /* XXX olang */ start_elem (w, "label"); - write_text (w, label); + write_text (w, output_item_get_label (item)); end_elem (w); } @@ -281,7 +281,8 @@ spv_writer_close_heading (struct spv_writer *w) } static void -start_container (struct spv_writer *w, const struct output_item *item) +open_container (struct spv_writer *w, const struct output_item *item, + const char *inner_elem) { start_elem (w, "container"); write_attr (w, "visibility", "visible"); @@ -294,32 +295,38 @@ start_container (struct spv_writer *w, const struct output_item *item) start_elem (w, "label"); write_text (w, output_item_get_label (item)); end_elem (w); + + start_elem (w, inner_elem); + if (item->command_name) + write_attr (w, "commandName", item->command_name); +} + +static void +close_container (struct spv_writer *w) +{ + end_elem (w); + end_elem (w); } static void -spv_writer_put_text (struct spv_writer *w, const struct text_item *text, - const char *command_id) +spv_writer_put_text (struct spv_writer *w, const struct text_item *text) { bool initial_depth = w->heading_depth; if (!initial_depth) spv_writer_open_file (w); - start_container (w, &text->output_item); - - start_elem (w, "vtx:text"); + open_container (w, &text->output_item, "vtx:text"); write_attr (w, "type", (text->type == TEXT_ITEM_TITLE ? "title" : text->type == TEXT_ITEM_PAGE_TITLE ? "page-title" : "log")); - if (command_id) - write_attr (w, "commandName", command_id); start_elem (w, "html"); char *s = text_item_get_plain_text (text); write_text (w, s); free (s); - end_elem (w); /* html */ - end_elem (w); /* vtx:text */ - end_elem (w); /* container */ + end_elem (w); + + close_container (w); if (!initial_depth) spv_writer_close_file (w, ""); @@ -344,13 +351,10 @@ spv_writer_put_image (struct spv_writer *w, const struct output_item *item, char *uri = xasprintf ("%010d_Imagegeneric.png", ++w->n_tables); - start_container (w, item); - - start_elem (w, "object"); + open_container (w, item, "object"); write_attr (w, "type", "unknown"); write_attr (w, "uri", uri); - end_elem (w); /* object */ - end_elem (w); /* container */ + close_container (w); if (!initial_depth) spv_writer_close_file (w, ""); @@ -1055,18 +1059,14 @@ spv_writer_put_table (struct spv_writer *w, const struct table_item *item) if (!initial_depth) spv_writer_open_file (w); - start_container (w, &item->output_item); + open_container (w, &item->output_item, "vtb:table"); + write_attr (w, "type", "table"); /* XXX */ + write_attr_format (w, "tableId", "%d", table_id); char *subtype = (item->pt->subtype ? pivot_value_to_string (item->pt->subtype, item->pt) : xstrdup ("unknown")); - - start_elem (w, "vtb:table"); - write_attr (w, "commandName", item->pt->command_c); - write_attr (w, "type", "table"); /* XXX */ write_attr (w, "subType", subtype); - write_attr_format (w, "tableId", "%d", table_id); - free (subtype); start_elem (w, "vtb:tableStructure"); @@ -1075,8 +1075,8 @@ spv_writer_put_table (struct spv_writer *w, const struct table_item *item) write_text (w, data_path); end_elem (w); /* vtb:dataPath */ end_elem (w); /* vtb:tableStructure */ - end_elem (w); /* vtb:table */ - end_elem (w); /* container */ + + close_container (w); if (!initial_depth) spv_writer_close_file (w, ""); @@ -1093,9 +1093,7 @@ void spv_writer_write (struct spv_writer *w, const struct output_item *item) { if (is_group_open_item (item)) - spv_writer_open_heading (w, - to_group_open_item (item)->command_name, - to_group_open_item (item)->command_name); + spv_writer_open_heading (w, item); else if (is_group_close_item (item)) spv_writer_close_heading (w); else if (is_table_item (item)) @@ -1113,8 +1111,7 @@ spv_writer_write (struct spv_writer *w, const struct output_item *item) else if (is_image_item (item)) spv_writer_put_image (w, item, to_image_item (item)->image); else if (is_text_item (item)) - spv_writer_put_text (w, to_text_item (item), - output_get_command_name ()); + spv_writer_put_text (w, to_text_item (item)); else if (is_page_break_item (item)) w->need_page_break = true; else if (is_page_setup_item (item)) diff --git a/src/output/table-item.c b/src/output/table-item.c index 4860c3450c..5d63be972a 100644 --- a/src/output/table-item.c +++ b/src/output/table-item.c @@ -43,6 +43,7 @@ table_item_create (struct pivot_table *pt) struct table_item *item = xmalloc (sizeof *item); *item = (struct table_item) { .output_item = OUTPUT_ITEM_INITIALIZER (&table_item_class), + .output_item.command_name = xstrdup_if_nonnull (pt->command_c), .pt = pt, }; return item; diff --git a/src/output/text-item.c b/src/output/text-item.c index a0428acc82..6b0d75f374 100644 --- a/src/output/text-item.c +++ b/src/output/text-item.c @@ -100,6 +100,7 @@ text_item_create_value (enum text_item_type type, struct pivot_value *value, struct text_item *item = xzalloc (sizeof *item); *item = (struct text_item) { .output_item = OUTPUT_ITEM_INITIALIZER (&text_item_class), + .output_item.command_name = xstrdup_if_nonnull (output_get_command_name ()), .output_item.label = label, .type = type, .text = value, -- 2.30.2