Add support for reading and writing SPV files.
[pspp] / src / output / odt.c
index 065ef379a6d176455dde3d0a523b1c7ea28872b8..104e57a9332c373502794a9550ddda5bc1b09f4d 100644 (file)
@@ -40,7 +40,6 @@
 #include "output/driver-provider.h"
 #include "output/message-item.h"
 #include "output/options.h"
-#include "output/tab.h"
 #include "output/table-item.h"
 #include "output/table-provider.h"
 #include "output/text-item.h"
@@ -57,6 +56,7 @@ struct odt_driver
   struct output_driver driver;
 
   struct zip_writer *zip;     /* ZIP file writer. */
+  struct file_handle *handle; /* Handle for 'file_name'. */
   char *file_name;            /* Output file name. */
 
   /* content.xml */
@@ -69,12 +69,6 @@ struct odt_driver
 
   /* Number of tables so far. */
   int table_num;
-
-  /* Name of current command. */
-  char *command_name;
-
-  /* Number of footnotes so far. */
-  int n_footnotes;
 };
 
 static const struct output_driver_class odt_driver_class;
@@ -86,26 +80,6 @@ odt_driver_cast (struct output_driver *driver)
   return UP_CAST (driver, struct odt_driver, driver);
 }
 
-/* Create the "mimetype" file needed by ODF */
-static bool
-create_mimetype (struct zip_writer *zip)
-{
-  FILE *fp;
-
-  fp = create_temp_file ();
-  if (fp == NULL)
-    {
-      msg_error (errno, _("error creating temporary file"));
-      return false;
-    }
-
-  fprintf (fp, "application/vnd.oasis.opendocument.text");
-  zip_writer_add (zip, fp, "mimetype");
-  close_temp_file (fp);
-
-  return true;
-}
-
 /* Creates a new temporary file and stores it in *FILE, then creates an XML
    writer for it and stores it in *W. */
 static void
@@ -309,13 +283,11 @@ odt_create (struct file_handle *fh, enum settings_output_devices device_type,
   output_driver_init (d, &odt_driver_class, file_name, device_type);
 
   odt->zip = zip;
+  odt->handle = fh;
   odt->file_name = xstrdup (file_name);
 
-  if (!create_mimetype (zip))
-    {
-      output_driver_destroy (d);
-      return NULL;
-    }
+  zip_writer_add_string (zip, "mimetype",
+                         "application/vnd.oasis.opendocument.text");
 
   /* Create the manifest */
   create_writer (&odt->manifest_file, &odt->manifest_wtr);
@@ -386,8 +358,8 @@ odt_destroy (struct output_driver *driver)
       zip_writer_close (odt->zip);
     }
 
+  fh_unref (odt->handle);
   free (odt->file_name);
-  free (odt->command_name);
   free (odt);
 }
 
@@ -426,27 +398,24 @@ write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
 }
 
 static void
-write_footnote (struct odt_driver *odt, const char *footnote)
+write_footnote (struct odt_driver *odt, const struct footnote *f)
 {
-  char marker[16];
-
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
                                _xml("footnote"));
 
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
-  str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker);
-  if (strlen (marker) > 1)
+  if (strlen (f->marker) > 1)
     xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
-                                       "(%s)", marker);
+                                       "(%s)", f->marker);
   else
     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
-                                 _xml(marker));
+                                 _xml(f->marker));
   xmlTextWriterEndElement (odt->content_wtr);
 
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
-  write_xml_with_line_breaks (odt, footnote);
+  write_xml_with_line_breaks (odt, f->content);
   xmlTextWriterEndElement (odt->content_wtr);
   xmlTextWriterEndElement (odt->content_wtr);
 
@@ -454,23 +423,50 @@ write_footnote (struct odt_driver *odt, const char *footnote)
 }
 
 static void
-write_table (struct odt_driver *odt, const struct table_item *item)
+write_table_item_text (struct odt_driver *odt,
+                       const struct table_item_text *text)
 {
-  const struct table *tab = table_item_get_table (item);
-  const char *caption = table_item_get_caption (item);
-  const char *title = table_item_get_title (item);
-  int r, c;
+  if (!text)
+    return;
+
+  xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
+  xmlTextWriterWriteFormatAttribute (odt->content_wtr,
+                                     _xml("text:outline-level"), "%d", 2);
+  xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
+  for (size_t i = 0; i < text->n_footnotes; i++)
+    write_footnote (odt, text->footnotes[i]);
+  xmlTextWriterEndElement (odt->content_wtr);
+}
 
-  /* Write a heading for the table */
-  if (title != NULL)
+static void
+write_table_item_layers (struct odt_driver *odt,
+                         const struct table_item_layers *layers)
+{
+  if (!layers)
+    return;
+
+  for (size_t i = 0; i < layers->n_layers; i++)
     {
+      const struct table_item_layer *layer = &layers->layers[i];
       xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
       xmlTextWriterWriteFormatAttribute (odt->content_wtr,
                                          _xml("text:outline-level"), "%d", 2);
-      xmlTextWriterWriteString (odt->content_wtr,
-                                _xml (table_item_get_title (item)) );
+      xmlTextWriterWriteString (odt->content_wtr, _xml (layer->content) );
+      for (size_t i = 0; i < layer->n_footnotes; i++)
+        write_footnote (odt, layer->footnotes[i]);
       xmlTextWriterEndElement (odt->content_wtr);
     }
+}
+
+static void
+write_table (struct odt_driver *odt, const struct table_item *item)
+{
+  const struct table *tab = table_item_get_table (item);
+  int r, c;
+
+  /* Write a heading for the table */
+  write_table_item_text (odt, table_item_get_title (item));
+  write_table_item_layers (odt, table_item_get_layers (item));
 
   /* Start table */
   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
@@ -499,7 +495,6 @@ write_table (struct odt_driver *odt, const struct table_item *item)
       for (c = 0 ; c < table_nc (tab) ; ++c)
        {
           struct table_cell cell;
-          size_t i;
 
           table_get_cell (tab, c, r, &cell);
 
@@ -521,31 +516,27 @@ write_table (struct odt_driver *odt, const struct table_item *item)
                   odt->content_wtr, _xml("table:number-rows-spanned"),
                   "%d", rowspan);
 
-              for (i = 0; i < cell.n_contents; i++)
-                {
-                  const struct cell_contents *contents = &cell.contents[i];
-                  int j;
-
-                  if (contents->text)
-                    {
-                      xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
+              xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
 
-                      if ( r < table_ht (tab) || c < table_hl (tab) )
-                        xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
-                      else
-                        xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
+              if ( r < table_ht (tab) || c < table_hl (tab) )
+                xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
+              else
+                xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
 
-                      write_xml_with_line_breaks (odt, contents->text);
-
-                      for (j = 0; j < contents->n_footnotes; j++)
-                        write_footnote (odt, contents->footnotes[j]);
+              if (cell.options & TAB_MARKUP)
+                {
+                  /* XXX */
+                  char *s = output_get_text_from_markup (cell.text);
+                  write_xml_with_line_breaks (odt, s);
+                  free (s);
+                }
+              else
+                write_xml_with_line_breaks (odt, cell.text);
 
-                      xmlTextWriterEndElement (odt->content_wtr); /* text:p */
-                    }
-                  else if (contents->table)
-                    write_table (odt, contents->table);
+              for (int i = 0; i < cell.n_footnotes; i++)
+                write_footnote (odt, cell.footnotes[i]);
 
-                }
+              xmlTextWriterEndElement (odt->content_wtr); /* text:p */
               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
            }
          else
@@ -566,16 +557,7 @@ write_table (struct odt_driver *odt, const struct table_item *item)
   xmlTextWriterEndElement (odt->content_wtr); /* table */
 
   /* Write a caption for the table */
-  if (caption != NULL)
-    {
-      xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
-      xmlTextWriterWriteFormatAttribute (odt->content_wtr,
-                                         _xml("text:outline-level"), "%d", 2);
-      xmlTextWriterWriteString (odt->content_wtr,
-                                _xml (table_item_get_caption (item)) );
-      xmlTextWriterEndElement (odt->content_wtr);
-    }
-
+  write_table_item_text (odt, table_item_get_caption (item));
 }
 
 static void
@@ -593,22 +575,14 @@ odt_submit (struct output_driver *driver,
 {
   struct odt_driver *odt = odt_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &odt->command_name);
-
   if (is_table_item (output_item))
     write_table (odt, to_table_item (output_item));
   else if (is_text_item (output_item))
-    {
-      struct text_item *text_item = to_text_item (output_item);
-
-      if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE)
-        odt_output_text (odt, text_item_get_text (text_item));
-    }
+    odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
   else if (is_message_item (output_item))
     {
       const struct message_item *message_item = to_message_item (output_item);
-      const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, odt->command_name);
+      char *s = msg_to_string (message_item_get_msg (message_item));
       odt_output_text (odt, s);
       free (s);
     }