output: Always use helper functions to access struct tab_table members.
[pspp-builds.git] / src / output / odt.c
index f557278de8d41b8ac9adbb1ffd2ca07d20f79e25..8471b6b3fb7eb46bb16a536b748161eac7337902 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <config.h>
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
 
 /* A driver for creating OpenDocument Format text files from PSPP's output */
 
 
 #include "xalloc.h"
 
+#include "error.h"
+
 #define _xml(X) (const xmlChar *)(X)
 
+
+struct odf_driver_options
+{
+  struct outp_driver *driver;
+  
+  char *file_name;            /* Output file name. */
+  bool debug;
+};
+
+
 struct odt_driver_ext 
 {
   /* The name of the temporary directory used to construct the ODF */
@@ -49,6 +63,8 @@ struct odt_driver_ext
 
   /* Writer fot the manifest.xml file */
   xmlTextWriterPtr manifest_wtr;
+
+  struct odf_driver_options opts;
 };
 
 
@@ -105,6 +121,98 @@ register_file (struct odt_driver_ext *x, const char *filename)
   xmlTextWriterEndElement (x->manifest_wtr);
 }
 
+static void
+write_style_data (struct odt_driver_ext *x)
+{
+  xmlTextWriterPtr w = create_writer (x, "styles.xml");
+  register_file (x, "styles.xml");
+
+  xmlTextWriterStartElement (w, _xml ("office:document-styles"));
+  xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
+                              _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
+
+  xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
+                              _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
+
+  xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
+                              _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
+
+  xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
+                              
+
+
+  xmlTextWriterStartElement (w, _xml ("office:styles"));
+
+
+  {
+    xmlTextWriterStartElement (w, _xml ("style:style"));
+    xmlTextWriterWriteAttribute (w, _xml ("style:name"),
+                                _xml ("Standard"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:family"),
+                                _xml ("paragraph"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:class"),
+                                _xml ("text"));
+
+    xmlTextWriterEndElement (w); /* style:style */
+  }
+
+  {
+    xmlTextWriterStartElement (w, _xml ("style:style"));
+    xmlTextWriterWriteAttribute (w, _xml ("style:name"),
+                                _xml ("Table_20_Contents"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
+                                _xml ("Table Contents"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:family"),
+                                _xml ("paragraph"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
+                                _xml ("Standard"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:class"),
+                                _xml ("extra"));
+
+    xmlTextWriterEndElement (w); /* style:style */
+  }
+
+  {
+    xmlTextWriterStartElement (w, _xml ("style:style"));
+    xmlTextWriterWriteAttribute (w, _xml ("style:name"),
+                                _xml ("Table_20_Heading"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
+                                _xml ("Table Heading"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:family"),
+                                _xml ("paragraph"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
+                                _xml ("Table_20_Contents"));
+
+    xmlTextWriterWriteAttribute (w, _xml ("style:class"),
+                                _xml ("extra"));
+
+
+    xmlTextWriterStartElement (w, _xml ("style:text-properties"));
+    xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
+    xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
+    xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
+    xmlTextWriterEndElement (w); /* style:text-properties */
+
+    xmlTextWriterEndElement (w); /* style:style */
+  }
+
+
+  xmlTextWriterEndElement (w); /* office:styles */
+  xmlTextWriterEndElement (w); /* office:document-styles */
+
+  xmlTextWriterEndDocument (w);
+  xmlFreeTextWriter (w);
+}
+
 static void
 write_meta_data (struct odt_driver_ext *x)
 {
@@ -158,6 +266,62 @@ write_meta_data (struct odt_driver_ext *x)
   xmlFreeTextWriter (w);
 }
 
+enum
+{
+  output_file_arg,
+  boolean_arg,
+};
+
+static const struct outp_option option_tab[] =
+{
+  {"output-file",              output_file_arg,0},
+
+  {"debug",                    boolean_arg,    1},
+
+  {NULL, 0, 0},
+};
+
+static bool
+handle_option (void *options_, const char *key, const struct string *val)
+{
+  struct odf_driver_options *options = options_;
+  struct outp_driver *this = options->driver;
+  int subcat;
+  char *value = ds_cstr (val);
+
+  switch (outp_match_keyword (key, option_tab, &subcat))
+    {
+    case -1:
+      error (0, 0,
+             _("unknown configuration parameter `%s' for %s device "
+               "driver"), key, this->class->name);
+      break;
+    case output_file_arg:
+      free (options->file_name);
+      options->file_name = xstrdup (value);
+      break;
+    case boolean_arg:
+      if (!strcmp (value, "on") || !strcmp (value, "true")
+          || !strcmp (value, "yes") || atoi (value))
+        options->debug = true;
+      else if (!strcmp (value, "off") || !strcmp (value, "false")
+               || !strcmp (value, "no") || !strcmp (value, "0"))
+        options->debug = false;
+      else
+        {
+          error (0, 0, _("boolean value expected for %s"), key);
+          return false;
+        }
+      break;
+
+    default:
+      NOT_REACHED ();
+    }
+
+  return true;
+}
+
+
 static bool
 odt_open_driver (const char *name, int types, struct substring option_string)
 {
@@ -166,6 +330,12 @@ odt_open_driver (const char *name, int types, struct substring option_string)
 
   this->ext = x = xmalloc (sizeof *x);
 
+  x->opts.driver = this;
+  x->opts.file_name = xstrdup ("pspp.pdt");
+  x->opts.debug = false;
+
+  outp_parse_options (this->name, option_string, handle_option, &x->opts);
+
   outp_register_driver (this);
 
   x->dirname = xstrdup ("odt-XXXXXX");
@@ -189,6 +359,7 @@ odt_open_driver (const char *name, int types, struct substring option_string)
 
 
   write_meta_data (x);
+  write_style_data (x);
 
   x->content_wtr = create_writer (x, "content.xml");
   register_file (x, "content.xml");
@@ -236,15 +407,23 @@ odt_close_driver (struct outp_driver *this)
 
   /* Zip up the directory */
   ds_init_empty (&zip_cmd);
-  ds_put_format (&zip_cmd, "cd %s ; zip -r ../pspp.odt . > /dev/null", x->dirname);
+  ds_put_format (&zip_cmd,
+                "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../pspp.odt .",
+                x->dirname, x->opts.file_name, x->opts.file_name);
   system (ds_cstr (&zip_cmd));
   ds_destroy (&zip_cmd);
 
-  /* Remove the temp dir */
-  ds_init_empty (&rm_cmd);
-  ds_put_format (&rm_cmd, "rm -r %s", x->dirname);
-  system (ds_cstr (&rm_cmd));
-  ds_destroy (&rm_cmd);
+
+  if ( !x->opts.debug )
+    {
+      /* Remove the temp dir */
+      ds_init_empty (&rm_cmd);
+      ds_put_format (&rm_cmd, "rm -r %s", x->dirname);
+      system (ds_cstr (&rm_cmd));
+      ds_destroy (&rm_cmd);
+    }
+  else
+    fprintf (stderr, "Not removing directory %s\n", x->dirname);
 
   free (x->dirname);
   free (x);
@@ -253,17 +432,17 @@ odt_close_driver (struct outp_driver *this)
 }
 
 static void
-odt_open_page (struct outp_driver *this)
+odt_open_page (struct outp_driver *this UNUSED)
 {
 }
 
 static void
-odt_close_page (struct outp_driver *this)
+odt_close_page (struct outp_driver *this UNUSED)
 {
 }
 
 static void
-odt_output_chart (struct outp_driver *this, const struct chart *chart)
+odt_output_chart (struct outp_driver *this UNUSED, const struct chart *chart UNUSED)
 {
  printf ("%s\n", __FUNCTION__);
 }
@@ -293,48 +472,88 @@ odt_submit (struct outp_driver *this, struct som_entity *e)
 
   /* Start column definitions */
   xmlTextWriterStartElement (x->content_wtr, _xml("table:table-column"));
-  xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab->nc);
+  xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab_nc (tab));
   xmlTextWriterEndElement (x->content_wtr);
 
 
   /* Deal with row headers */
-  if ( tab->t > 0)
+  if ( tab_t (tab) > 0)
     xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
     
 
   /* Write all the rows */
-  for (r = 0 ; r < tab->nr; ++r)
+  for (r = 0 ; r < tab_nr (tab); ++r)
     {
+      int spanned_columns = 0;
       /* Start row definition */
       xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
 
       /* Write all the columns */
-      for (c = 0 ; c < tab->nc ; ++c)
+      for (c = 0 ; c < tab_nc (tab) ; ++c)
        {
-         int opts = tab->ct[tab->nc * r + c];
-         xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
-         xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
+         char *s = NULL;
+         unsigned int opts = tab->ct[tab_nc (tab) * r + c];
+         struct substring ss = tab->cc[tab_nc (tab) * r + c];
+
+         if (opts & TAB_EMPTY)
+           {
+             xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
+             xmlTextWriterEndElement (x->content_wtr);
+             continue;
+           }
 
-         if (! (opts & TAB_EMPTY) ) 
+         if ( opts & TAB_JOIN)
            {
-             char *s = ss_xstrdup (tab->cc[tab->nc * r + c]);
+             if ( spanned_columns == 0)
+               {
+                 struct tab_joined_cell *j = (struct tab_joined_cell*) ss_data (ss);
+                 s = ss_xstrdup (j->contents);
+               }
+           }
+         else
+           s = ss_xstrdup (ss);
+
+         if ( spanned_columns == 0 )
+           {
+             xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
+             xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
+
+             if ( opts & TAB_JOIN )
+               {
+                 struct tab_joined_cell *j = (struct tab_joined_cell*) ss_data (ss);
+                 spanned_columns = j->x2 - j->x1;
+
+                 xmlTextWriterWriteFormatAttribute (x->content_wtr,
+                                                    _xml("table:number-columns-spanned"),
+                                                    "%d", spanned_columns);
+               }
+
              xmlTextWriterStartElement (x->content_wtr, _xml("text:p"));
-             if ( r < tab->t || c < tab->l )
+
+             if ( r < tab_t (tab) || c < tab_l (tab) )
                xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
              else
                xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
 
              xmlTextWriterWriteString (x->content_wtr, _xml (s));
          
+             xmlTextWriterEndElement (x->content_wtr); /* text:p */
+             xmlTextWriterEndElement (x->content_wtr); /* table:table-cell */
+           }
+         else
+           {
+             xmlTextWriterStartElement (x->content_wtr, _xml("table:covered-table-cell"));
              xmlTextWriterEndElement (x->content_wtr);
-             free (s);
            }
-         xmlTextWriterEndElement (x->content_wtr);
+         if ( opts & TAB_JOIN )
+           spanned_columns --;
+
+         free (s);
        }
   
       xmlTextWriterEndElement (x->content_wtr); /* row */
 
-      if ( tab->t > 0 && r == tab->t - 1)
+      if ( tab_t (tab) > 0 && r == tab_t (tab) - 1)
        xmlTextWriterEndElement (x->content_wtr); /* table-header-rows */
     }