file-name: Generalize fn_interp_vars().
[pspp-builds.git] / src / output / postscript.c
index 68f4ddacbe8a9467c30088b8a736c7e724894db1..394ebbb27dc5fb3c7d0e251908905db97bd54ddc 100644 (file)
@@ -1,32 +1,30 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 1997-9, 2000, 2006, 2007, 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 <ctype.h>
 #include <errno.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <time.h>
 #include <unistd.h>
 
-#include <libpspp/alloc.h>
+#include <data/file-name.h>
 #include <libpspp/assertion.h>
 #include <libpspp/bit-vector.h>
 #include <libpspp/compiler.h>
 #include <libpspp/misc.h>
 #include <libpspp/start-date.h>
 #include <libpspp/version.h>
+#include <output/afm.h>
+#include <output/chart-provider.h>
+#include <output/chart.h>
+#include <output/manager.h>
+#include <output/output.h>
 
-#include <data/file-name.h>
-
-#include "afm.h"
-#include "chart.h"
 #include "error.h"
-#include "getline.h"
 #include "intprops.h"
-#include "manager.h"
 #include "minmax.h"
-#include "output.h"
-#include "size_max.h"
-#include "strsep.h"
+#include "xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -109,12 +104,14 @@ struct ps_driver_ext
 
     struct font *fonts[OUTP_FONT_CNT];
     int last_font;              /* Index of last font set with setfont. */
+
+    int doc_num;                /* %%DocumentNumber counter. */
   };
 
 /* Transform logical y-ordinate Y into a page ordinate. */
 #define YT(Y) (this->length - (Y))
 
-static bool handle_option (struct outp_driver *this, const char *key,
+static bool handle_option (void *this, const char *key,
                            const struct string *val);
 static void draw_headers (struct outp_driver *this);
 
@@ -129,11 +126,13 @@ static void setup_font (struct outp_driver *this, struct font *, int index);
 /* Driver initialization. */
 
 static bool
-ps_open_driver (struct outp_driver *this, struct substring options)
+ps_open_driver (const char *name, int types, struct substring options)
 {
+  struct outp_driver *this;
   struct ps_driver_ext *x;
   size_t i;
 
+  this = outp_allocate_driver (&postscript_class, name, types);
   this->width = this->length = 0;
   this->font_height = PSUS * 10 / 72;
 
@@ -143,8 +142,7 @@ ps_open_driver (struct outp_driver *this, struct substring options)
   x->draw_headers = true;
   x->page_number = 0;
   x->portrait = true;
-  x->paper_width = PSUS * 17 / 2;
-  x->paper_length = PSUS * 11;
+  outp_get_paper_size ("", &x->paper_width, &x->paper_length);
   x->left_margin = PSUS / 2;
   x->right_margin = PSUS / 2;
   x->top_margin = PSUS / 2;
@@ -154,8 +152,9 @@ ps_open_driver (struct outp_driver *this, struct substring options)
   x->line_width = PSUS / 144;
   for (i = 0; i < OUTP_FONT_CNT; i++)
     x->fonts[i] = NULL;
+  x->doc_num = 0;
 
-  outp_parse_options (options, handle_option, this);
+  outp_parse_options (this->name, options, handle_option, this);
 
   x->file = fn_open (x->file_name, "w");
   if (x->file == NULL)
@@ -165,7 +164,7 @@ ps_open_driver (struct outp_driver *this, struct substring options)
       goto error;
     }
 
-  if (x->portrait) 
+  if (x->portrait)
     {
       this->width = x->paper_width;
       this->length = x->paper_length;
@@ -174,7 +173,7 @@ ps_open_driver (struct outp_driver *this, struct substring options)
     {
       this->width = x->paper_length;
       this->length = x->paper_width;
-    }    
+    }
   this->width -= x->left_margin + x->right_margin;
   this->length -= x->top_margin + x->bottom_margin;
   if (x->draw_headers)
@@ -224,10 +223,12 @@ ps_open_driver (struct outp_driver *this, struct substring options)
 
   write_ps_prologue (this);
 
+  outp_register_driver (this);
   return true;
 
  error:
   this->class->close_driver (this);
+  outp_free_driver (this);
   return false;
 }
 
@@ -235,18 +236,23 @@ static bool
 ps_close_driver (struct outp_driver *this)
 {
   struct ps_driver_ext *x = this->ext;
-  bool ok;
+  bool ok = true;
   size_t i;
 
-  fprintf (x->file,
-          "%%%%Trailer\n"
-           "%%%%Pages: %d\n"
-           "%%%%EOF\n",
-           x->page_number);
-
-  ok = fn_close (x->file_name, x->file) == 0;
-  if (!ok)
-    error (0, errno, _("closing PostScript output file \"%s\""), x->file_name);
+  if (x->file != NULL)
+    {
+      fprintf (x->file,
+               "%%%%Trailer\n"
+               "%%%%Pages: %d\n"
+               "%%%%EOF\n",
+               x->page_number);
+
+      ok = fn_close (x->file_name, x->file) == 0;
+      if (!ok)
+        error (0, errno, _("closing PostScript output file \"%s\""),
+               x->file_name);
+    }
+
   free (x->file_name);
   for (i = 0; i < OUTP_FONT_CNT; i++)
     free_font (x->fonts[i]);
@@ -295,9 +301,10 @@ static const struct outp_option option_tab[] =
 };
 
 static bool
-handle_option (struct outp_driver *this, const char *key,
+handle_option (void *this_, const char *key,
                const struct string *val)
 {
+  struct outp_driver *this = this_;
   struct ps_driver_ext *x = this->ext;
   int subcat;
   char *value = ds_cstr (val);
@@ -380,14 +387,10 @@ handle_option (struct outp_driver *this, const char *key,
       break;
     case dimension_arg:
       {
-       int dimension = outp_evaluate_dimension (value, NULL);
+       int dimension = outp_evaluate_dimension (value);
 
        if (dimension <= 0)
-         {
-           error (0, 0, _("value for `%s' must be a dimension of positive "
-                           "length (i.e., `1in')"), key);
-           break;
-         }
+          break;
        switch (subcat)
          {
          case 0:
@@ -605,8 +608,6 @@ ps_submit (struct outp_driver *this UNUSED, struct som_entity *s)
 {
   switch (s->type)
     {
-    case SOM_CHART:
-      break;
     default:
       NOT_REACHED ();
     }
@@ -798,7 +799,7 @@ draw_text (struct outp_driver *this,
 static void
 draw_header_line (struct outp_driver *this,
                   const char *left, const char *right,
-                  int x0, int x1, int y) 
+                  int x0, int x1, int y)
 {
   int right_width = 0;
   if (right != NULL)
@@ -829,12 +830,12 @@ draw_headers (struct outp_driver *this)
 
   r1 = xasprintf (_("%s - Page %d"), get_start_date (), ext->page_number);
   r2 = xasprintf ("%s - %s", version, host_system);
+
   draw_header_line (this, outp_title, r1, x0, x1, y);
   y += this->font_height;
-  
+
   draw_header_line (this, outp_subtitle, r2, x0, x1, y);
+
   free (r1);
   free (r2);
 }
@@ -849,7 +850,7 @@ draw_headers (struct outp_driver *this)
 static void
 write_text (struct outp_driver *this,
             int x0, int y0,
-            enum outp_font font, 
+            enum outp_font font,
             enum outp_justification justification,
             const struct afm_character **chars, int *kerns, size_t char_cnt,
             int width_left)
@@ -1016,7 +1017,7 @@ text (struct outp_driver *this, const struct outp_text *text, bool draw,
       char_width = cur->width * this->font_height / 1000;
 
       /* Get kern adjustment. */
-      if (s.glyph_cnt > 0) 
+      if (s.glyph_cnt > 0)
         kern_adjust = (afm_get_kern_adjustment (s.glyphs[s.glyph_cnt - 1], cur)
                        * this->font_height / 1000);
       else
@@ -1086,72 +1087,6 @@ ps_text_draw (struct outp_driver *this, const struct outp_text *t)
   text (this, t, true, NULL, NULL);
 }
 \f
-static void
-ps_chart_initialise (struct outp_driver *this UNUSED, struct chart *ch)
-{
-#ifdef NO_CHARTS
-  ch->lp = NULL;
-#else
-  struct ps_driver_ext *x = this->ext;
-  char page_size[128];
-  int size;
-  int x_origin, y_origin;
-
-  ch->file = tmpfile ();
-  if (ch->file == NULL)
-    {
-      ch->lp = NULL;
-      return;
-    }
-
-  size = this->width < this->length ? this->width : this->length;
-  x_origin = x->left_margin + (size - this->width) / 2;
-  y_origin = x->bottom_margin + (size - this->length) / 2;
-
-  snprintf (page_size, sizeof page_size,
-            "a,xsize=%.3f,ysize=%.3f,xorigin=%.3f,yorigin=%.3f",
-            (double) size / PSUS, (double) size / PSUS,
-            (double) x_origin / PSUS, (double) y_origin / PSUS);
-
-  ch->pl_params = pl_newplparams ();
-  pl_setplparam (ch->pl_params, "PAGESIZE", page_size);
-  ch->lp = pl_newpl_r ("ps", NULL, ch->file, stderr, ch->pl_params);
-#endif
-}
-
-static void
-ps_chart_finalise (struct outp_driver *this UNUSED, struct chart *ch UNUSED)
-{
-#ifndef NO_CHARTS
-  struct ps_driver_ext *x = this->ext;
-  char buf[BUFSIZ];
-  static int doc_num = 0;
-
-  outp_eject_page (this);
-  fprintf (x->file,
-           "/sp save def\n"
-           "%d %d translate 1000 dup scale\n"
-           "userdict begin\n"
-           "/showpage { } def\n"
-           "0 setgray 0 setlinecap 1 setlinewidth\n"
-           "0 setlinejoin 10 setmiterlimit [ ] 0 setdash newpath clear\n"
-           "%%%%BeginDocument: %d\n",
-           -x->left_margin, -x->bottom_margin,
-           doc_num++);
-
-  rewind (ch->file);
-  while (fwrite (buf, 1, fread (buf, 1, sizeof buf, ch->file), x->file))
-    continue;
-  fclose (ch->file);
-
-  fputs ("%%EndDocument\n"
-         "end\n"
-         "sp restore\n",
-         x->file);
-  outp_close_page (this);
-#endif
-}
-\f
 static void embed_font (struct outp_driver *this, struct font *font);
 static void reencode_font (struct outp_driver *this, struct font *font);
 
@@ -1422,7 +1357,8 @@ reencode_font (struct outp_driver *this, struct font *font)
   fputs ("] RF\n", x->file);
 
   if (freaderror (file) != 0)
-    error (errno, 0, "closing Postscript encoding \"%s\"", font->encoding_fn);
+    error (errno, 0, _("closing Postscript encoding \"%s\""),
+           font->encoding_fn);
 }
 
 /* PostScript driver class. */
@@ -1436,13 +1372,13 @@ const struct outp_class postscript_class =
 
   ps_open_page,
   ps_close_page,
+  NULL,
+
+  NULL,                         /* output_chart */
 
   ps_submit,
 
   ps_line,
   ps_text_metrics,
   ps_text_draw,
-
-  ps_chart_initialise,
-  ps_chart_finalise
 };