output: Make building without libplot possible again.
[pspp] / src / output / chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <output/chart.h>
20 #include <output/chart-provider.h>
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <libpspp/str.h>
32 #include <output/manager.h>
33 #include <output/output.h>
34
35 #include "error.h"
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 extern struct som_table_class tab_table_class;
42
43 void
44 chart_init (struct chart *chart, const struct chart_class *class)
45 {
46   chart->class = class;
47   chart->ref_cnt = 1;
48 }
49
50 void
51 chart_geometry_init (plPlotter *lp, struct chart_geometry *geom)
52 {
53   /* Start output page. */
54   pl_openpl_r (lp);
55
56   /* Set coordinate system. */
57   pl_fspace_r (lp, 0.0, 0.0, 1000.0, 1000.0);
58
59   /* Set line thickness. */
60   pl_flinewidth_r (lp, 0.25);
61   pl_pencolorname_r (lp, "black");
62
63   /* Erase graphics display. */
64   pl_erase_r (lp);
65
66   pl_filltype_r (lp, 0);
67   pl_savestate_r(lp);
68
69   /* Set default chartetry. */
70   geom->data_top = 900;
71   geom->data_right = 800;
72   geom->data_bottom = 120;
73   geom->data_left = 150;
74   geom->abscissa_top = 70;
75   geom->ordinate_right = 120;
76   geom->title_bottom = 920;
77   geom->legend_left = 810;
78   geom->legend_right = 1000;
79   geom->font_size = 0;
80   strcpy (geom->fill_colour, "red");
81
82   /* Get default font size */
83   if (!geom->font_size)
84     geom->font_size = pl_fontsize_r (lp, -1);
85
86   /* Draw the data area */
87   pl_box_r (lp,
88             geom->data_left, geom->data_bottom,
89             geom->data_right, geom->data_top);
90 }
91
92 void
93 chart_geometry_free (plPlotter *lp)
94 {
95   if (pl_closepl_r (lp) < 0)
96     fprintf (stderr, "Couldn't close Plotter\n");
97 }
98
99 void
100 chart_draw (const struct chart *chart, plPlotter *lp)
101 {
102   chart->class->draw (chart, lp);
103 }
104
105 struct chart *
106 chart_ref (const struct chart *chart_)
107 {
108   struct chart *chart = (struct chart *) chart_;
109   chart->ref_cnt++;
110   return chart;
111 }
112
113 void
114 chart_unref (struct chart *chart)
115 {
116   if (chart != NULL)
117     {
118       assert (chart->ref_cnt > 0);
119       if (--chart->ref_cnt == 0)
120         chart->class->destroy (chart);
121     }
122 }
123
124 void
125 chart_submit (struct chart *chart)
126 {
127 #ifdef HAVE_LIBPLOT
128   struct outp_driver *d;
129
130   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
131     if (d->class->output_chart != NULL)
132       d->class->output_chart (d, chart);
133 #endif
134
135   chart_unref (chart);
136 }
137
138 bool
139 chart_create_file (const char *type, const char *file_name_tmpl, int number,
140                    plPlotterParams *params, char **file_namep, plPlotter **lpp)
141 {
142   char *file_name = NULL;
143   FILE *fp = NULL;
144   int number_pos;
145   plPlotter *lp;
146
147   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
148   file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl,
149                          number, file_name_tmpl + number_pos + 1);
150
151   fp = fopen (file_name, "wb");
152   if (fp == NULL)
153     {
154       error (0, errno, _("creating \"%s\""), file_name);
155       goto error;
156     }
157
158   if (params != NULL)
159     lp = pl_newpl_r (type, 0, fp, stderr, params);
160   else
161     {
162       params = pl_newplparams ();
163       lp = pl_newpl_r (type, 0, fp, stderr, params);
164       pl_deleteplparams (params);
165     }
166   if (lp == NULL)
167     goto error;
168
169   *file_namep = file_name;
170   *lpp = lp;
171   return true;
172
173 error:
174   if (fp != NULL)
175     {
176       fclose (fp);
177       if (file_name != NULL)
178         unlink (file_name);
179     }
180   free (file_name);
181   *file_namep = NULL;
182   *lpp = NULL;
183   return false;
184 }