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