output: Refactor implementation of charts.
[pspp-builds.git] / src / output / chart.c
index 49819e9fe03cc4073242ae97d05b77365c7e78a2..b096a7d2195fc632c863a9e01a7319ea3dceeb80 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 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
 #include <config.h>
 
 #include <output/chart.h>
+#include <output/chart-provider.h>
 
 #include <assert.h>
 #include <errno.h>
 #include <float.h>
 #include <math.h>
+#include <plot.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <plot.h>
-
 #include <libpspp/str.h>
 #include <output/manager.h>
 #include <output/output.h>
 
 extern struct som_table_class tab_table_class;
 
-struct chart *
-chart_create(void)
+void
+chart_init (struct chart *chart, const struct chart_class *class)
 {
-  struct chart *chart;
-  struct outp_driver *d;
-
-  d = outp_drivers (NULL);
-  if (d == NULL)
-    return NULL;
-
-  chart = xmalloc (sizeof *chart);
-  chart->lp = NULL;
-  d->class->initialise_chart(d, chart);
-  if (!chart->lp)
-    {
-      free (chart);
-      return NULL;
-    }
-
-  if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
-    return NULL;
-
-  pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
-  pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
-  pl_pencolorname_r (chart->lp, "black");
-
-  pl_erase_r (chart->lp);               /* erase graphics display */
-  pl_filltype_r(chart->lp,0);
-
-  pl_savestate_r(chart->lp);
+  chart->class = class;
+  chart->ref_cnt = 1;
+}
 
-  /* Set default chartetry */
-  chart->data_top =   900;
-  chart->data_right = 800;
-  chart->data_bottom = 120;
-  chart->data_left = 150;
-  chart->abscissa_top = 70;
-  chart->ordinate_right = 120;
-  chart->title_bottom = 920;
-  chart->legend_left = 810;
-  chart->legend_right = 1000;
-  chart->font_size = 0;
-  strcpy(chart->fill_colour,"red");
+void
+chart_geometry_init (plPlotter *lp, struct chart_geometry *geom)
+{
+  /* Start output page. */
+  pl_openpl_r (lp);
+
+  /* Set coordinate system. */
+  pl_fspace_r (lp, 0.0, 0.0, 1000.0, 1000.0);
+
+  /* Set line thickness. */
+  pl_flinewidth_r (lp, 0.25);
+  pl_pencolorname_r (lp, "black");
+
+  /* Erase graphics display. */
+  pl_erase_r (lp);
+
+  pl_filltype_r (lp, 0);
+  pl_savestate_r(lp);
+
+  /* Set default chartetry. */
+  geom->data_top = 900;
+  geom->data_right = 800;
+  geom->data_bottom = 120;
+  geom->data_left = 150;
+  geom->abscissa_top = 70;
+  geom->ordinate_right = 120;
+  geom->title_bottom = 920;
+  geom->legend_left = 810;
+  geom->legend_right = 1000;
+  geom->font_size = 0;
+  strcpy (geom->fill_colour, "red");
 
   /* Get default font size */
-  if ( !chart->font_size)
-    chart->font_size = pl_fontsize_r(chart->lp, -1);
+  if (!geom->font_size)
+    geom->font_size = pl_fontsize_r (lp, -1);
 
   /* Draw the data area */
-  pl_box_r(chart->lp,
-          chart->data_left, chart->data_bottom,
-          chart->data_right, chart->data_top);
-
-  return chart;
+  pl_box_r (lp,
+           geom->data_left, geom->data_bottom,
+           geom->data_right, geom->data_top);
 }
 
 void
-chart_submit(struct chart *chart)
+chart_geometry_free (plPlotter *lp)
 {
-  struct som_entity s;
-  struct outp_driver *d;
-
-  if ( ! chart )
-     return ;
+  if (pl_closepl_r (lp) < 0)
+    fprintf (stderr, "Couldn't close Plotter\n");
+}
 
-  pl_restorestate_r(chart->lp);
+void
+chart_draw (const struct chart *chart, plPlotter *lp)
+{
+  chart->class->draw (chart, lp);
+}
 
-  s.class = &tab_table_class;
-  s.ext = chart;
-  s.type = SOM_CHART;
-  som_submit (&s);
+struct chart *
+chart_ref (const struct chart *chart_)
+{
+  struct chart *chart = (struct chart *) chart_;
+  chart->ref_cnt++;
+  return chart;
+}
 
-  if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
-    {
-      fprintf (stderr, "Couldn't close Plotter\n");
-    }
+void
+chart_unref (struct chart *chart)
+{
+  assert (chart->ref_cnt > 0);
+  if (--chart->ref_cnt == 0)
+    chart->class->destroy (chart);
+}
 
-  pl_deletepl_r(chart->lp);
+void
+chart_submit (struct chart *chart)
+{
+  struct outp_driver *d;
 
-  pl_deleteplparams(chart->pl_params);
+  for (d = outp_drivers (NULL); d; d = outp_drivers (d))
+    if (d->class->output_chart != NULL)
+      d->class->output_chart (d, chart);
 
-  d = outp_drivers (NULL);
-  d->class->finalise_chart(d, chart);
-  free(chart);
+  chart_unref (chart);
 }
 
-void
-chart_init_separate (struct chart *ch, const char *type,
-                     const char *file_name_tmpl, int number)
+bool
+chart_create_file (const char *type, const char *file_name_tmpl, int number,
+                   plPlotterParams *params, char **file_namep, plPlotter **lpp)
 {
-  FILE *fp;
+  char *file_name = NULL;
+  FILE *fp = NULL;
   int number_pos;
+  plPlotter *lp;
 
   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
-  ch->file_name = xasprintf ("%.*s%d%s",
-                             number_pos, file_name_tmpl,
-                             number,
-                             file_name_tmpl + number_pos + 1);
-  fp = fopen (ch->file_name, "wb");
+  file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl,
+                         number, file_name_tmpl + number_pos + 1);
+
+  fp = fopen (file_name, "wb");
   if (fp == NULL)
     {
-      error (0, errno, _("creating \"%s\""), ch->file_name);
-      free (ch->file_name);
-      ch->file_name = NULL;
-      return;
+      error (0, errno, _("creating \"%s\""), file_name);
+      goto error;
     }
 
-  ch->pl_params = pl_newplparams ();
-  ch->lp = pl_newpl_r (type, 0, fp, stderr, ch->pl_params);
-}
+  if (params != NULL)
+    lp = pl_newpl_r (type, 0, fp, stderr, params);
+  else
+    {
+      params = pl_newplparams ();
+      lp = pl_newpl_r (type, 0, fp, stderr, params);
+      pl_deleteplparams (params);
+    }
+  if (lp == NULL)
+    goto error;
 
-void
-chart_finalise_separate (struct chart *ch)
-{
-  free (ch->file_name);
+  *file_namep = file_name;
+  *lpp = lp;
+  return true;
+
+error:
+  if (fp != NULL)
+    {
+      fclose (fp);
+      if (file_name != NULL)
+        unlink (file_name);
+    }
+  free (file_name);
+  *file_namep = NULL;
+  *lpp = NULL;
+  return false;
 }