output: Use Cairo and Pango to draw charts, instead of libplot.
[pspp-builds.git] / src / output / html.c
index d2c5312ff67306881ecdd6a08d71a0fae7862c87..3a8be8eb0b0e2cbf8db200b73bf5099244f5c2c3 100644 (file)
@@ -1,66 +1,72 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 #include "chart.h"
 #include "htmlP.h"
 #include <errno.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <time.h>
 #include <unistd.h>
 
-#include <libpspp/alloc.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
 #include <data/file-name.h>
-#include "error.h"
-#include "getline.h"
-#include "output.h"
-#include "manager.h"
-#include "table.h"
+#include <output/chart-provider.h>
+#include <output/output.h>
+#include <output/manager.h>
+#include <output/table.h>
 #include <libpspp/version.h>
 
-#include "size_max.h"
+#include "error.h"
+#include "xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
+/* HTML driver options: (defaults listed first)
+
+   output-file="pspp.html"
+   chart-files="pspp-#.png"
+*/
+
 static void escape_string (FILE *file,
                            const char *text, size_t length,
                            const char *space);
-static bool handle_option (struct outp_driver *this,
+static bool handle_option (void *this,
                            const char *key, const struct string *val);
 static void print_title_tag (FILE *file, const char *name,
                              const char *content);
 
 static bool
-html_open_driver (struct outp_driver *this, struct substring options)
+html_open_driver (const char *name, int types, struct substring options)
 {
+  struct outp_driver *this;
   struct html_driver_ext *x;
 
+  this = outp_allocate_driver (&html_class, name, types);
   this->ext = x = xmalloc (sizeof *x);
   x->file_name = xstrdup ("pspp.html");
   x->chart_file_name = xstrdup ("pspp-#.png");
   x->file = NULL;
-  x->chart_cnt = 0;
+  x->chart_cnt = 1;
 
-  outp_parse_options (options, handle_option, this);
+  outp_parse_options (name, options, handle_option, this);
 
   x->file = fn_open (x->file_name, "w");
   if (x->file == NULL)
@@ -68,7 +74,7 @@ html_open_driver (struct outp_driver *this, struct substring options)
       error (0, errno, _("opening HTML output file: %s"), x->file_name);
       goto error;
     }
+
   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
   fputs ("<HTML>\n", x->file);
@@ -85,22 +91,22 @@ html_open_driver (struct outp_driver *this, struct substring options)
   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
   print_title_tag (x->file, "H1", outp_title);
   print_title_tag (x->file, "H2", outp_subtitle);
-  free (x->chart_file_name); 
 
+  outp_register_driver (this);
   return true;
 
  error:
-  free (x->chart_file_name); 
   this->class->close_driver (this);
+  outp_free_driver (this);
   return false;
 }
 
 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
    necessary for HTML. */
 static void
-print_title_tag (FILE *file, const char *name, const char *content) 
+print_title_tag (FILE *file, const char *name, const char *content)
 {
-  if (content != NULL) 
+  if (content != NULL)
     {
       fprintf (file, "<%s>", name);
       escape_string (file, content, strlen (content), " ");
@@ -113,32 +119,25 @@ html_close_driver (struct outp_driver *this)
 {
   struct html_driver_ext *x = this->ext;
   bool ok;
-  if (x->file != NULL) 
+
+  if (x->file != NULL)
     {
       fprintf (x->file,
                "</BODY>\n"
                "</HTML>\n"
                "<!-- end of file -->\n");
       ok = fn_close (x->file_name, x->file) == 0;
-      x->file = NULL; 
+      x->file = NULL;
     }
   else
     ok = true;
+  free (x->chart_file_name);
   free (x->file_name);
   free (x);
-   
+
   return ok;
 }
 
-/* Link the image contained in FILE_NAME to the 
-   HTML stream in FILE. */
-static void
-link_image (FILE *file, char *file_name)
-{
-  fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
- }
-
 /* Generic option types. */
 enum
   {
@@ -155,9 +154,9 @@ static const struct outp_option option_tab[] =
   };
 
 static bool
-handle_option (struct outp_driver *this,
-               const char *key, const struct string *val)
+handle_option (void *this_, const char *key, const struct string *val)
 {
+  struct outp_driver *this = this_;
   struct html_driver_ext *x = this->ext;
   int subcat;
 
@@ -169,19 +168,20 @@ handle_option (struct outp_driver *this,
              key);
       break;
     case string_arg:
-      switch (subcat) 
+      switch (subcat)
         {
         case 0:
           free (x->file_name);
           x->file_name = ds_xstrdup (val);
           break;
         case 1:
-          if (ds_find_char (val, '#') != SIZE_MAX) 
+          if (ds_find_char (val, '#') != SIZE_MAX)
             {
               free (x->chart_file_name);
               x->chart_file_name = ds_xstrdup (val);
             }
-          error (0, 0, _("`chart-files' value must contain `#'"));
+          else
+            error (0, 0, _("`chart-files' value must contain `#'"));
           break;
         default:
           NOT_REACHED ();
@@ -190,28 +190,35 @@ handle_option (struct outp_driver *this,
     default:
       NOT_REACHED ();
     }
-  
+
   return true;
 }
 
 static void output_tab_table (struct outp_driver *, struct tab_table *);
 
+static void
+html_output_chart (struct outp_driver *this, const struct chart *chart)
+{
+  struct html_driver_ext *x = this->ext;
+  char *file_name;
+
+  file_name = chart_draw_png (chart, x->chart_file_name, x->chart_cnt++);
+  fprintf (x->file, "<IMG SRC=\"%s\"/>", file_name);
+  free (file_name);
+}
+
 static void
 html_submit (struct outp_driver *this, struct som_entity *s)
 {
   extern struct som_table_class tab_table_class;
-  struct html_driver_ext *x = this->ext;
-  
+
   assert (s->class == &tab_table_class ) ;
 
-  switch (s->type) 
+  switch (s->type)
     {
     case SOM_TABLE:
       output_tab_table ( this, (struct tab_table *) s->ext);
       break;
-    case SOM_CHART:
-      link_image (x->file, ((struct chart *)s->ext)->file_name);
-      break;
     default:
       NOT_REACHED ();
     }
@@ -248,7 +255,7 @@ escape_string (FILE *file,
         }
     }
 }
-  
+
 /* Outputs content for a cell with options OPTS and contents
    TEXT. */
 void
@@ -257,23 +264,23 @@ html_put_cell_contents (struct outp_driver *this,
 {
   struct html_driver_ext *x = this->ext;
 
-  if (!(opts & TAB_EMPTY)) 
+  if (!(opts & TAB_EMPTY))
     {
       if (opts & TAB_EMPH)
         fputs ("<EM>", x->file);
-      if (opts & TAB_FIX) 
+      if (opts & TAB_FIX)
         {
           fputs ("<TT>", x->file);
           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
           fputs ("</TT>", x->file);
         }
-      else 
+      else
         {
           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
           escape_string (x->file,
                          ss_data (text) + initial_spaces,
                          ss_length (text) - initial_spaces,
-                         " "); 
+                         " ");
         }
       if (opts & TAB_EMPH)
         fputs ("</EM>", x->file);
@@ -285,25 +292,25 @@ static void
 output_tab_table (struct outp_driver *this, struct tab_table *t)
 {
   struct html_driver_ext *x = this->ext;
-  
+
   if (t->nr == 1 && t->nc == 1)
     {
       fputs ("<P>", x->file);
       html_put_cell_contents (this, t->ct[0], *t->cc);
       fputs ("</P>\n", x->file);
-      
+
       return;
     }
 
   fputs ("<TABLE BORDER=1>\n", x->file);
-  
+
   if (t->title != NULL)
     {
       fprintf (x->file, "  <CAPTION>");
       escape_string (x->file, t->title, strlen (t->title), " ");
       fputs ("</CAPTION>\n", x->file);
     }
-  
+
   {
     int r;
     unsigned char *ct = t->ct;
@@ -311,7 +318,7 @@ output_tab_table (struct outp_driver *this, struct tab_table *t)
     for (r = 0; r < t->nr; r++)
       {
        int c;
-       
+
        fputs ("  <TR>\n", x->file);
        for (c = 0; c < t->nc; c++, ct++)
          {
@@ -325,7 +332,7 @@ output_tab_table (struct outp_driver *this, struct tab_table *t)
                 j = (struct tab_joined_cell *) ss_data (*cc);
                 cc = &j->contents;
                 if (j->x1 != c || j->y1 != r)
-                  continue; 
+                  continue;
               }
 
             /* Output <TD> or <TH> tag. */
@@ -354,46 +361,8 @@ output_tab_table (struct outp_driver *this, struct tab_table *t)
        fputs ("  </TR>\n", x->file);
       }
   }
-             
-  fputs ("</TABLE>\n\n", x->file);
-}
-
-static void
-html_initialise_chart (struct outp_driver *this UNUSED, struct chart *ch)
-{
-#ifdef NO_CHARTS
-  ch->lp = NULL;
-#else
-  struct html_driver_ext *x = this->ext;
-  
-  FILE *fp;
-  int number_pos;
-
-  x->chart_cnt++;
-  
-  number_pos = strchr (x->chart_file_name, '#') - x->chart_file_name;
-  ch->file_name = xasprintf ("%.*s%d%s",
-                             number_pos, x->chart_file_name,
-                             (int) x->chart_cnt,
-                             x->chart_file_name + number_pos + 1);
-  fp = fopen (ch->file_name, "wb");
-  if (fp == NULL) 
-    {
-      error (0, errno, _("creating \"%s\""), ch->file_name);
-      free (ch->file_name);
-      ch->file_name = NULL;
-      return;
-    }
-
-  ch->pl_params = pl_newplparams ();
-  ch->lp = pl_newpl_r ("png", 0, fp, stderr, ch->pl_params);
-#endif
-}
 
-static void 
-html_finalise_chart(struct outp_driver *d UNUSED, struct chart *ch)
-{
-  free(ch->file_name);
+  fputs ("</TABLE>\n\n", x->file);
 }
 
 
@@ -409,12 +378,13 @@ const struct outp_class html_class =
 
     NULL,
     NULL,
+    NULL,
+
+    html_output_chart,
 
     html_submit,
 
     NULL,
     NULL,
     NULL,
-    html_initialise_chart,
-    html_finalise_chart
   };