output-item: Make command name part of every output_item.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 11 Jan 2021 02:20:11 +0000 (18:20 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 11 Jan 2021 02:20:11 +0000 (18:20 -0800)
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
src/output/cairo-pager.c
src/output/driver.c
src/output/group-item.c
src/output/group-item.h
src/output/output-item-provider.h
src/output/output-item.c
src/output/output-item.h
src/output/spv/spv-writer.c
src/output/table-item.c
src/output/text-item.c

index de20cb3caac25833be6ffd60654c3b5ac48e8436..639bf4a3c0054e0ff59edd94e48575c2a22cb190 100644 (file)
@@ -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)
index 057ad41c6c4c67e047b659285b265c3ba76f5a2f..e1cae64819098140d939dfc40db4044fa51dcd1a 100644 (file)
@@ -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)
index 95dcbfd94a791943f15cc4999574f38ce995bded..6c4fd414cbbefaa47442a857f99f1a501fab7e01 100644 (file)
@@ -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))
index 1c8fce5e8b444412358061837d4d8a0a6dd87014..c0aad17d9f9bc2e0d85432e41cc4a18989f47d83 100644 (file)
@@ -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);
 }
 
index 61593632cd513990a7b10f302f750db0f950cffc..b8ac464c3d7e96c1e2ad3dfcf489b8f877f42cec 100644 (file)
 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,
index 552549cee9eaa54fee0288fd2b5dc9877f49c637..62e756155951d817f8b63ed4b1cc11d82434ce0b 100644 (file)
@@ -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 */
index cd37031b654b628711fc63dafc4faca3cf795e8b..f80ea19c46019815f520c656b125392408e03639 100644 (file)
@@ -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);
         }
     }
 }
index b522df4c90d042f46fcb1c1c95025e0bcaaaa515..1580d1e1eb6c3797b1e391bd77dc453ecf5b1147 100644 (file)
@@ -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 *);
index 4bdac19d7b0df300cbf8d3cca72ded7f03177612..e3f3b28ffae9fbc41aa445fa30ae60de6a657bf2 100644 (file)
@@ -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))
index 4860c3450c11f69a1719d0738370db9556a146a0..5d63be972a3bad004925cdbcefc93736a844cd46 100644 (file)
@@ -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;
index a0428acc82c44209e3c91d16e98ebb2a6a34a8c2..6b0d75f374c07bbf6701e6c45e81f2a3499ca960 100644 (file)
@@ -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,