Add support for PNG images in .spv files.
[pspp] / src / output / spv-driver.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2019 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/driver-provider.h"
20
21 #include <stdlib.h>
22
23 #include "data/file-handle-def.h"
24 #include "libpspp/cast.h"
25 #ifdef HAVE_CAIRO
26 #include "output/cairo-chart.h"
27 #endif
28 #include "output/chart-item.h"
29 #include "output/group-item.h"
30 #include "output/page-eject-item.h"
31 #include "output/page-setup-item.h"
32 #include "output/table-item.h"
33 #include "output/text-item.h"
34 #include "output/spv/spv-writer.h"
35
36 #include "gl/xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 struct spv_driver
42   {
43     struct output_driver driver;
44     struct spv_writer *writer;
45     struct file_handle *handle;
46   };
47
48 static const struct output_driver_class spv_driver_class;
49
50 static struct spv_driver *
51 spv_driver_cast (struct output_driver *driver)
52 {
53   assert (driver->class == &spv_driver_class);
54   return UP_CAST (driver, struct spv_driver, driver);
55 }
56
57 static struct output_driver *
58 spv_create (struct file_handle *fh, enum settings_output_devices device_type,
59              struct string_map *o UNUSED)
60 {
61   struct output_driver *d;
62   struct spv_driver *spv;
63
64   spv = xzalloc (sizeof *spv);
65   d = &spv->driver;
66   spv->handle = fh;
67   output_driver_init (&spv->driver, &spv_driver_class, fh_get_file_name (fh),
68                       device_type);
69
70   char *error = spv_writer_open (fh_get_file_name (fh), &spv->writer);
71   if (spv->writer == NULL)
72     {
73       msg (ME, "%s", error);
74       goto error;
75     }
76
77   return d;
78
79  error:
80   output_driver_destroy (d);
81   return NULL;
82 }
83
84 static void
85 spv_destroy (struct output_driver *driver)
86 {
87   struct spv_driver *spv = spv_driver_cast (driver);
88
89   char *error = spv_writer_close (spv->writer);
90   if (error)
91     msg (ME, "%s", error);
92   fh_unref (spv->handle);
93   free (spv);
94 }
95
96 static void
97 spv_submit (struct output_driver *driver,
98              const struct output_item *output_item)
99 {
100   struct spv_driver *spv = spv_driver_cast (driver);
101
102   if (is_group_open_item (output_item))
103     spv_writer_open_heading (spv->writer,
104                              to_group_open_item (output_item)->command_name,
105                              to_group_open_item (output_item)->command_name);
106   else if (is_group_close_item (output_item))
107     spv_writer_close_heading (spv->writer);
108   else if (is_table_item (output_item))
109     {
110       const struct table_item *table_item = to_table_item (output_item);
111       if (table_item->pt)
112         spv_writer_put_table (spv->writer, table_item->pt);
113     }
114 #ifdef HAVE_CAIRO
115   else if (is_chart_item (output_item))
116     {
117       cairo_surface_t *surface = xr_draw_image_chart (
118         to_chart_item (output_item),
119         &(struct cell_color) CELL_COLOR_BLACK,
120         &(struct cell_color) CELL_COLOR_WHITE);
121       if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
122         spv_writer_put_image (spv->writer, surface);
123       cairo_surface_destroy (surface);
124     }
125 #endif
126   else if (is_text_item (output_item))
127     {
128       char *command_id = output_get_command_name ();
129       spv_writer_put_text (spv->writer, to_text_item (output_item),
130                            command_id);
131       free (command_id);
132     }
133   else if (is_page_eject_item (output_item))
134     spv_writer_eject_page (spv->writer);
135   else if (is_page_setup_item (output_item))
136     spv_writer_set_page_setup (spv->writer,
137                                to_page_setup_item (output_item)->page_setup);
138 }
139
140 struct output_driver_factory spv_driver_factory =
141   { "spv", "pspp.spv", spv_create };
142
143 static const struct output_driver_class spv_driver_class =
144   {
145     "spv",
146     spv_destroy,
147     spv_submit,
148     NULL,
149   };