1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "libpspp/assertion.h"
20 #include "libpspp/cast.h"
21 #include "libpspp/message.h"
22 #include "libpspp/str.h"
23 #include "libpspp/string-map.h"
24 #include "data/file-handle-def.h"
25 #include "output/cairo-fsm.h"
26 #include "output/cairo-pager.h"
27 #include "output/driver-provider.h"
28 #include "output/options.h"
29 #include "output/table.h"
31 #include <cairo/cairo-pdf.h>
32 #include <cairo/cairo-ps.h>
33 #include <cairo/cairo-svg.h>
35 #include <cairo/cairo.h>
38 #include <pango/pango-font.h>
41 #include "gl/c-strcase.h"
42 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
47 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
51 /* The unit used for internal measurements is inch/(72 * XR_POINT).
52 (Thus, XR_POINT units represent one point.) */
53 #define XR_POINT PANGO_SCALE
55 /* Conversions to and from points. */
59 return x / (double) XR_POINT;
71 /* Cairo output driver. */
74 struct output_driver driver;
76 enum xr_output_type output_type;
77 struct xr_fsm_style *fsm_style;
78 struct xr_page_style *page_style;
79 struct xr_pager *pager;
82 /* This is the surface where we're currently drawing. It is always
85 If 'trim' is true, this is a special Cairo "recording surface" that we
86 are using to save output temporarily just to find out the bounding box,
87 then later replay it into the destination surface.
91 - For output to a PDF or PostScript file, it is the same pointer as
94 - For output to a PNG file, it is an image surface.
96 - For output to an SVG file, it is a recording surface.
98 cairo_surface_t *drawing_surface;
100 /* - For output to a PDF or PostScript file, this is the surface for the
101 PDF or PostScript file where the output is ultimately going.
103 - For output to a PNG file, this is NULL, because Cairo has very
104 limited support for PNG. Cairo can't open a PNG file for writing as
105 a surface, it can only save an existing surface to a PNG file.
107 - For output to a SVG file, this is NULL, because Cairo does not
108 permit resizing the SVG page size after creating the file, whereas
109 this driver needs to do that sometimes. Also, SVG is not multi-page
110 (according to https://wiki.inkscape.org/wiki/index.php/Multipage).
112 cairo_surface_t *dest_surface;
114 /* Used only in file names, for PNG and SVG output where we can only write
115 one page per file. */
119 static const struct output_driver_class cairo_driver_class;
122 /* Output driver basics. */
124 static struct xr_driver *
125 xr_driver_cast (struct output_driver *driver)
127 assert (driver->class == &cairo_driver_class);
128 return UP_CAST (driver, struct xr_driver, driver);
131 static struct driver_option *
132 opt (struct output_driver *d, struct string_map *options, const char *key,
133 const char *default_value)
135 return driver_option_get (d, options, key, default_value);
138 static PangoFontDescription *
139 parse_font (const char *font, int default_size, bool bold, bool italic)
141 if (!c_strcasecmp (font, "Monospaced"))
144 PangoFontDescription *desc = pango_font_description_from_string (font);
148 /* If the font description didn't include an explicit font size, then set it
149 to DEFAULT_SIZE, which is in inch/72000 units. */
150 if (!(pango_font_description_get_set_fields (desc) & PANGO_FONT_MASK_SIZE))
151 pango_font_description_set_size (desc,
152 (default_size / 1000.0) * PANGO_SCALE);
154 pango_font_description_set_weight (desc, (bold
156 : PANGO_WEIGHT_NORMAL));
157 pango_font_description_set_style (desc, (italic
159 : PANGO_STYLE_NORMAL));
164 static PangoFontDescription *
165 parse_font_option (struct output_driver *d, struct string_map *options,
166 const char *key, const char *default_value,
167 int default_size, bool bold, bool italic)
169 char *string = parse_string (opt (d, options, key, default_value));
170 PangoFontDescription *desc = parse_font (string, default_size, bold, italic);
173 msg (MW, _("`%s': bad font specification"), string);
175 /* Fall back to DEFAULT_VALUE, which had better be a valid font
177 desc = parse_font (default_value, default_size, bold, italic);
178 assert (desc != NULL);
185 static struct xr_driver *
186 xr_allocate (const char *name, int device_type,
187 enum xr_output_type output_type, struct string_map *o)
189 struct xr_driver *xr = xzalloc (sizeof *xr);
190 struct output_driver *d = &xr->driver;
192 output_driver_init (d, &cairo_driver_class, name, device_type);
193 xr->output_type = output_type;
195 /* Scale factor from inch/72000 to inch/(72 * XR_POINT). */
196 const double scale = XR_POINT / 1000.;
198 int paper[TABLE_N_AXES];
199 parse_paper_size (opt (d, o, "paper-size", ""), &paper[H], &paper[V]);
200 for (int a = 0; a < TABLE_N_AXES; a++)
203 int margins[TABLE_N_AXES][2];
204 margins[H][0] = parse_dimension (opt (d, o, "left-margin", ".5in")) * scale;
205 margins[H][1] = parse_dimension (opt (d, o, "right-margin", ".5in")) * scale;
206 margins[V][0] = parse_dimension (opt (d, o, "top-margin", ".5in")) * scale;
207 margins[V][1] = parse_dimension (opt (d, o, "bottom-margin", ".5in")) * scale;
209 int size[TABLE_N_AXES];
210 for (int a = 0; a < TABLE_N_AXES; a++)
211 size[a] = paper[a] - margins[a][0] - margins[a][1];
213 int min_break[TABLE_N_AXES];
214 min_break[H] = parse_dimension (opt (d, o, "min-hbreak", NULL)) * scale;
215 min_break[V] = parse_dimension (opt (d, o, "min-vbreak", NULL)) * scale;
216 for (int a = 0; a < TABLE_N_AXES; a++)
217 if (min_break[a] <= 0)
218 min_break[a] = size[a] / 2;
220 int font_size = parse_int (opt (d, o, "font-size", "10000"), 1000, 1000000);
221 PangoFontDescription *fixed_font = parse_font_option
222 (d, o, "fixed-font", "monospace", font_size, false, false);
223 PangoFontDescription *proportional_font = parse_font_option (
224 d, o, "prop-font", "sans serif", font_size, false, false);
226 struct cell_color fg = parse_color (opt (d, o, "foreground-color", "black"));
228 bool systemcolors = parse_boolean (opt (d, o, "systemcolors", "false"));
231 = parse_dimension (opt (d, o, "object-spacing", NULL)) * scale;
232 if (object_spacing <= 0)
233 object_spacing = XR_POINT * 12;
235 const char *default_resolution = (output_type == XR_PNG ? "96" : "72");
236 int font_resolution = parse_int (opt (d, o, "font-resolution",
237 default_resolution), 10, 1000);
239 xr->trim = parse_boolean (opt (d, o, "trim", "false"));
241 xr->page_style = xmalloc (sizeof *xr->page_style);
242 *xr->page_style = (struct xr_page_style) {
246 [H] = { margins[H][0], margins[H][1], },
247 [V] = { margins[V][0], margins[V][1], },
250 .initial_page_number = 1,
251 .object_spacing = object_spacing,
254 xr->fsm_style = xmalloc (sizeof *xr->fsm_style);
255 *xr->fsm_style = (struct xr_fsm_style) {
257 .size = { [H] = size[H], [V] = size[V] },
258 .min_break = { [H] = min_break[H], [V] = min_break[V] },
260 [XR_FONT_PROPORTIONAL] = proportional_font,
261 [XR_FONT_FIXED] = fixed_font,
264 .use_system_colors = systemcolors,
265 .font_resolution = font_resolution,
271 static struct output_driver *
272 xr_create (struct file_handle *fh, enum settings_output_devices device_type,
273 struct string_map *o, enum xr_output_type output_type)
275 const char *file_name = fh_get_file_name (fh);
276 struct xr_driver *xr = xr_allocate (file_name, device_type, output_type, o);
278 double paper[TABLE_N_AXES];
279 for (int a = 0; a < TABLE_N_AXES; a++)
280 paper[a] = xr_to_pt (xr_page_style_paper_size (xr->page_style,
284 = (output_type == XR_PDF
285 ? cairo_pdf_surface_create (file_name, paper[H], paper[V])
286 : output_type == XR_PS
287 ? cairo_ps_surface_create (file_name, paper[H], paper[V])
289 if (xr->dest_surface)
291 cairo_status_t status = cairo_surface_status (xr->dest_surface);
292 if (status != CAIRO_STATUS_SUCCESS)
294 msg (ME, _("error opening output file `%s': %s"),
295 file_name, cairo_status_to_string (status));
301 = (xr->trim || output_type == XR_SVG
302 ? cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA,
303 &(cairo_rectangle_t) {
305 .height = paper[V] })
306 : output_type == XR_PNG
307 ? cairo_image_surface_create (CAIRO_FORMAT_ARGB32, paper[H], paper[V])
315 output_driver_destroy (&xr->driver);
319 static struct output_driver *
320 xr_pdf_create (struct file_handle *fh, enum settings_output_devices device_type,
321 struct string_map *o)
323 return xr_create (fh, device_type, o, XR_PDF);
326 static struct output_driver *
327 xr_ps_create (struct file_handle *fh, enum settings_output_devices device_type,
328 struct string_map *o)
330 return xr_create (fh, device_type, o, XR_PS);
333 static struct output_driver *
334 xr_svg_create (struct file_handle *fh, enum settings_output_devices device_type,
335 struct string_map *o)
337 return xr_create (fh, device_type, o, XR_SVG);
340 static struct output_driver *
341 xr_png_create (struct file_handle *fh, enum settings_output_devices device_type,
342 struct string_map *o)
344 return xr_create (fh, device_type, o, XR_PNG);
348 xr_set_surface_size (cairo_surface_t *surface, enum xr_output_type output_type,
349 double width, double height)
354 cairo_pdf_surface_set_size (surface, width, height);
358 cairo_ps_surface_set_size (surface, width, height);
368 xr_copy_surface (cairo_surface_t *dst, cairo_surface_t *src,
371 cairo_t *cr = cairo_create (dst);
372 cairo_set_source_surface (cr, src, x, y);
378 clear_rectangle (cairo_surface_t *surface,
379 double x0, double y0, double x1, double y1)
381 cairo_t *cr = cairo_create (surface);
382 cairo_set_source_rgb (cr, 1, 1, 1);
384 cairo_rectangle (cr, x0, y0, x1 - x0, y1 - y0);
390 xr_report_error (cairo_status_t status, const char *file_name)
392 if (status != CAIRO_STATUS_SUCCESS)
393 fprintf (stderr, "%s: %s\n", file_name, cairo_status_to_string (status));
397 xr_finish_page (struct xr_driver *xr)
399 xr_pager_finish_page (xr->pager);
403 - If the destination is PDF or PostScript, set the dest surface size, copy
404 ink extent, show_page.
406 - If the destination is PNG, create image surface, copy ink extent,
407 cairo_surface_write_to_png(), destroy image surface.
409 - If the destination is SVG, create svg surface, copy ink extent, close.
411 then destroy drawing_surface and make a new one.
415 - If the destination is PDF or PostScript, show_page.
417 - If the destination is PNG, cairo_surface_write_to_png(), destroy image
418 surface, create new image surface.
420 - If the destination is SVG, create svg surface, copy whole thing, close.
423 double paper[TABLE_N_AXES];
424 for (int a = 0; a < TABLE_N_AXES; a++)
425 paper[a] = xr_to_pt (xr_page_style_paper_size (
426 xr->page_style, xr->fsm_style, a));
429 char *file_name = (xr->page_number > 1
430 ? xasprintf ("%s-%d", xr->driver.name, xr->page_number)
435 /* Get the bounding box for the drawing surface. */
436 double ofs[TABLE_N_AXES], size[TABLE_N_AXES];
437 cairo_recording_surface_ink_extents (xr->drawing_surface,
440 const int (*margins)[2] = xr->page_style->margins;
441 for (int a = 0; a < TABLE_N_AXES; a++)
443 double scale = XR_POINT;
444 size[a] += (margins[a][0] + margins[a][1]) / scale;
445 ofs[a] = -ofs[a] + margins[a][0] / scale;
448 switch (xr->output_type)
452 xr_set_surface_size (xr->dest_surface, xr->output_type,
454 xr_copy_surface (xr->dest_surface, xr->drawing_surface,
456 cairo_surface_show_page (xr->dest_surface);
461 cairo_surface_t *svg = cairo_svg_surface_create (
462 file_name, size[H], size[V]);
463 xr_copy_surface (svg, xr->drawing_surface, ofs[H], ofs[V]);
464 xr_report_error (cairo_surface_status (svg), file_name);
465 cairo_surface_destroy (svg);
471 cairo_surface_t *png = cairo_image_surface_create (
472 CAIRO_FORMAT_ARGB32, size[H], size[V]);
473 clear_rectangle (png, 0, 0, size[H], size[V]);
474 xr_copy_surface (png, xr->drawing_surface, ofs[H], ofs[V]);
475 xr_report_error (cairo_surface_write_to_png (png, file_name),
477 cairo_surface_destroy (png);
482 /* Destroy the recording surface and create a fresh one of the same
484 cairo_surface_destroy (xr->drawing_surface);
485 xr->drawing_surface = cairo_recording_surface_create (
486 CAIRO_CONTENT_COLOR_ALPHA,
487 &(cairo_rectangle_t) { .width = paper[H], .height = paper[V] });
491 switch (xr->output_type)
495 cairo_surface_show_page (xr->dest_surface);
500 cairo_surface_t *svg = cairo_svg_surface_create (
501 file_name, paper[H], paper[V]);
502 xr_copy_surface (svg, xr->drawing_surface, 0.0, 0.0);
503 xr_report_error (cairo_surface_status (svg), file_name);
504 cairo_surface_destroy (svg);
509 xr_report_error (cairo_surface_write_to_png (xr->drawing_surface,
510 file_name), file_name);
511 cairo_surface_destroy (xr->drawing_surface);
512 xr->drawing_surface = cairo_image_surface_create (
513 CAIRO_FORMAT_ARGB32, paper[H], paper[V]);
518 if (file_name != xr->driver.name)
523 xr_destroy (struct output_driver *driver)
525 struct xr_driver *xr = xr_driver_cast (driver);
530 xr_pager_destroy (xr->pager);
532 if (xr->drawing_surface && xr->drawing_surface != xr->dest_surface)
533 cairo_surface_destroy (xr->drawing_surface);
534 if (xr->dest_surface)
536 cairo_surface_finish (xr->dest_surface);
537 cairo_status_t status = cairo_surface_status (xr->dest_surface);
538 if (status != CAIRO_STATUS_SUCCESS)
539 fprintf (stderr, _("error drawing output for %s driver: %s\n"),
540 output_driver_get_name (driver),
541 cairo_status_to_string (status));
542 cairo_surface_destroy (xr->dest_surface);
545 xr_page_style_unref (xr->page_style);
546 xr_fsm_style_unref (xr->fsm_style);
551 xr_update_page_setup (struct output_driver *driver,
552 const struct page_setup *ps)
554 struct xr_driver *xr = xr_driver_cast (driver);
556 const double scale = 72 * XR_POINT;
558 int swap = ps->orientation == PAGE_LANDSCAPE;
559 enum table_axis h = H ^ swap;
560 enum table_axis v = V ^ swap;
562 xr_page_style_unref (xr->page_style);
563 xr->page_style = xmalloc (sizeof *xr->page_style);
564 *xr->page_style = (struct xr_page_style) {
568 [H] = { ps->margins[h][0] * scale, ps->margins[h][1] * scale },
569 [V] = { ps->margins[v][0] * scale, ps->margins[v][1] * scale },
572 .initial_page_number = ps->initial_page_number,
573 .object_spacing = ps->object_spacing * 72 * XR_POINT,
575 for (size_t i = 0; i < 2; i++)
576 page_heading_copy (&xr->page_style->headings[i], &ps->headings[i]);
578 struct xr_fsm_style *old_fs = xr->fsm_style;
579 xr->fsm_style = xmalloc (sizeof *xr->fsm_style);
580 *xr->fsm_style = (struct xr_fsm_style) {
582 .size = { [H] = ps->paper[H] * scale, [V] = ps->paper[V] * scale },
584 [H] = ps->paper[H] * scale / 2,
585 [V] = ps->paper[V] * scale / 2,
588 .use_system_colors = old_fs->use_system_colors,
589 .font_resolution = old_fs->font_resolution,
591 for (size_t i = 0; i < XR_N_FONTS; i++)
592 xr->fsm_style->fonts[i] = pango_font_description_copy (old_fs->fonts[i]);
593 xr_fsm_style_unref (old_fs);
595 xr_set_surface_size (xr->dest_surface, xr->output_type, ps->paper[H] * 72.0,
596 ps->paper[V] * 72.0);
600 xr_submit (struct output_driver *driver, const struct output_item *output_item)
602 struct xr_driver *xr = xr_driver_cast (driver);
604 if (is_page_setup_item (output_item))
607 xr_update_page_setup (driver,
608 to_page_setup_item (output_item)->page_setup);
614 xr->pager = xr_pager_create (xr->page_style, xr->fsm_style);
615 xr_pager_add_page (xr->pager, cairo_create (xr->drawing_surface));
618 xr_pager_add_item (xr->pager, output_item);
619 while (xr_pager_needs_new_page (xr->pager))
622 xr_pager_add_page (xr->pager, cairo_create (xr->drawing_surface));
626 struct output_driver_factory pdf_driver_factory =
627 { "pdf", "pspp.pdf", xr_pdf_create };
628 struct output_driver_factory ps_driver_factory =
629 { "ps", "pspp.ps", xr_ps_create };
630 struct output_driver_factory svg_driver_factory =
631 { "svg", "pspp.svg", xr_svg_create };
632 struct output_driver_factory png_driver_factory =
633 { "png", "pspp.png", xr_png_create };
635 static const struct output_driver_class cairo_driver_class =