data-out: Make binary output buffer big enough for a null terminator.
[pspp] / src / output / cairo.c
index a08771bb449ae68cb2d0d126c3c439a0e2907395..a47d57fe1f2dd4f56373eda5b07c4f7279e4d332 100644 (file)
@@ -119,6 +119,8 @@ struct xr_driver
 
 static const struct output_driver_class cairo_driver_class;
 
+static void xr_update_page_setup (struct output_driver *,
+                                  const struct page_setup *);
 \f
 /* Output driver basics. */
 
@@ -129,11 +131,10 @@ xr_driver_cast (struct output_driver *driver)
   return UP_CAST (driver, struct xr_driver, driver);
 }
 
-static struct driver_option *
-opt (struct output_driver *d, struct string_map *options, const char *key,
-     const char *default_value)
+static struct driver_option
+opt (struct driver_options *options, const char *key, const char *default_value)
 {
-  return driver_option_get (d, options, key, default_value);
+  return driver_option_get (options, key, default_value);
 }
 
 static PangoFontDescription *
@@ -163,11 +164,11 @@ parse_font (const char *font, int default_size, bool bold, bool italic)
 }
 
 static PangoFontDescription *
-parse_font_option (struct output_driver *d, struct string_map *options,
+parse_font_option (struct driver_options *options,
                    const char *key, const char *default_value,
                    int default_size, bool bold, bool italic)
 {
-  char *string = parse_string (opt (d, options, key, default_value));
+  char *string = parse_string (opt (options, key, default_value));
   PangoFontDescription *desc = parse_font (string, default_size, bold, italic);
   if (!desc)
     {
@@ -183,59 +184,30 @@ parse_font_option (struct output_driver *d, struct string_map *options,
   return desc;
 }
 
+/* Scale INCHES into inch/(72 * XR_POINT). */
+static int
+scale (double inches)
+{
+  return inches * 72 * XR_POINT + 0.5;
+}
+
 static struct xr_driver *
 xr_allocate (const char *name, int device_type,
-             enum xr_output_type output_type, struct string_map *o)
+             enum xr_output_type output_type, struct driver_options *o)
 {
-  struct xr_driver *xr = XZALLOC (struct xr_driver);
-  struct output_driver *d = &xr->driver;
-
-  output_driver_init (d, &cairo_driver_class, name, device_type);
-  xr->output_type = output_type;
-
-  /* Scale factor from inch/72000 to inch/(72 * XR_POINT). */
-  const double scale = XR_POINT / 1000.;
-
-  int paper[TABLE_N_AXES];
-  parse_paper_size (opt (d, o, "paper-size", ""), &paper[H], &paper[V]);
-  for (int a = 0; a < TABLE_N_AXES; a++)
-    paper[a] *= scale;
-
-  int margins[TABLE_N_AXES][2];
-  margins[H][0] = parse_dimension (opt (d, o, "left-margin", ".5in")) * scale;
-  margins[H][1] = parse_dimension (opt (d, o, "right-margin", ".5in")) * scale;
-  margins[V][0] = parse_dimension (opt (d, o, "top-margin", ".5in")) * scale;
-  margins[V][1] = parse_dimension (opt (d, o, "bottom-margin", ".5in")) * scale;
-
-  int size[TABLE_N_AXES];
-  for (int a = 0; a < TABLE_N_AXES; a++)
-    size[a] = paper[a] - margins[a][0] - margins[a][1];
-
-  int min_break[TABLE_N_AXES];
-  min_break[H] = parse_dimension (opt (d, o, "min-hbreak", NULL)) * scale;
-  min_break[V] = parse_dimension (opt (d, o, "min-vbreak", NULL)) * scale;
-  for (int a = 0; a < TABLE_N_AXES; a++)
-    if (min_break[a] <= 0)
-      min_break[a] = size[a] / 2;
-
-  int font_size = parse_int (opt (d, o, "font-size", "10000"), 1000, 1000000);
+  int font_size = parse_int (opt (o, "font-size", "10000"), 1000, 1000000);
   PangoFontDescription *font = parse_font_option (
-    d, o, "prop-font", "Sans Serif", font_size, false, false);
-
-  struct cell_color fg = parse_color (opt (d, o, "foreground-color", "black"));
+    o, "prop-font", "Sans Serif", font_size, false, false);
 
-  bool systemcolors = parse_boolean (opt (d, o, "systemcolors", "false"));
+  struct cell_color fg = parse_color (opt (o, "foreground-color", "black"));
 
-  int object_spacing
-    = parse_dimension (opt (d, o, "object-spacing", NULL)) * scale;
-  if (object_spacing <= 0)
-    object_spacing = XR_POINT * 12;
+  bool systemcolors = parse_boolean (opt (o, "systemcolors", "false"));
 
   const char *default_resolution = (output_type == XR_PNG ? "96" : "72");
-  int font_resolution = parse_int (opt (d, o, "font-resolution",
+  int font_resolution = parse_int (opt (o, "font-resolution",
                                         default_resolution), 10, 1000);
 
-  xr->trim = parse_boolean (opt (d, o, "trim", "false"));
+  bool trim = parse_boolean (opt (o, "trim", "false"));
 
   /* Cairo 1.16.0 has a bug that causes crashes if outlines are enabled at the
      same time as trimming:
@@ -243,47 +215,54 @@ xr_allocate (const char *name, int device_type,
      For now, just disable the outline if trimming is enabled. */
   bool include_outline
     = (output_type == XR_PDF
-       && parse_boolean (opt (d, o, "outline", xr->trim ? "false" : "true")));
+       && parse_boolean (opt (o, "outline", trim ? "false" : "true")));
 
-  xr->page_style = xmalloc (sizeof *xr->page_style);
-  *xr->page_style = (struct xr_page_style) {
+  struct xr_page_style *page_style = xmalloc (sizeof *page_style);
+  *page_style = (struct xr_page_style) {
     .ref_cnt = 1,
-
-    .margins = {
-      [H] = { margins[H][0], margins[H][1], },
-      [V] = { margins[V][0], margins[V][1], },
-    },
-
     .initial_page_number = 1,
     .include_outline = include_outline,
   };
 
-  xr->fsm_style = xmalloc (sizeof *xr->fsm_style);
-  *xr->fsm_style = (struct xr_fsm_style) {
+  struct xr_fsm_style *fsm_style = xmalloc (sizeof *fsm_style);
+  *fsm_style = (struct xr_fsm_style) {
     .ref_cnt = 1,
-    .size = { [H] = size[H], [V] = size[V] },
-    .min_break = { [H] = min_break[H], [V] = min_break[V] },
     .font = font,
     .fg = fg,
     .use_system_colors = systemcolors,
-    .object_spacing = object_spacing,
     .font_resolution = font_resolution,
   };
 
+  struct xr_driver *xr = xmalloc (sizeof *xr);
+  *xr = (struct xr_driver) {
+    .driver = {
+      .class = &cairo_driver_class,
+      .name = xstrdup (name),
+      .device_type = device_type,
+    },
+    .output_type = output_type,
+    .fsm_style = fsm_style,
+    .page_style = page_style,
+    .trim = trim,
+  };
+
   return xr;
 }
 
 static struct output_driver *
 xr_create (struct file_handle *fh, enum settings_output_devices device_type,
-           struct string_map *o, enum xr_output_type output_type)
+           struct driver_options *o, enum xr_output_type output_type)
 {
   const char *file_name = fh_get_file_name (fh);
   struct xr_driver *xr = xr_allocate (file_name, device_type, output_type, o);
 
+  struct page_setup *ps = page_setup_parse (o);
+  xr_update_page_setup (&xr->driver, ps);
+
   double paper[TABLE_N_AXES];
   for (int a = 0; a < TABLE_N_AXES; a++)
-    paper[a] = xr_to_pt (xr_page_style_paper_size (xr->page_style,
-                                                   xr->fsm_style, a));
+    paper[a] = ps->paper[a] * 72.0;
+  page_setup_destroy (ps);
 
   xr->dest_surface
     = (output_type == XR_PDF
@@ -322,29 +301,29 @@ xr_create (struct file_handle *fh, enum settings_output_devices device_type,
 }
 
 static struct output_driver *
-xr_pdf_create (struct  file_handle *fh, enum settings_output_devices device_type,
-               struct string_map *o)
+xr_pdf_create (struct file_handle *fh, enum settings_output_devices device_type,
+               struct driver_options *o)
 {
   return xr_create (fh, device_type, o, XR_PDF);
 }
 
 static struct output_driver *
-xr_ps_create (struct  file_handle *fh, enum settings_output_devices device_type,
-               struct string_map *o)
+xr_ps_create (struct file_handle *fh, enum settings_output_devices device_type,
+              struct driver_options *o)
 {
   return xr_create (fh, device_type, o, XR_PS);
 }
 
 static struct output_driver *
 xr_svg_create (struct file_handle *fh, enum settings_output_devices device_type,
-               struct string_map *o)
+               struct driver_options *o)
 {
   return xr_create (fh, device_type, o, XR_SVG);
 }
 
 static struct output_driver *
 xr_png_create (struct file_handle *fh, enum settings_output_devices device_type,
-               struct string_map *o)
+               struct driver_options *o)
 {
   return xr_create (fh, device_type, o, XR_PNG);
 }
@@ -554,52 +533,56 @@ xr_destroy (struct output_driver *driver)
 
 static void
 xr_update_page_setup (struct output_driver *driver,
-                      const struct page_setup *setup)
+                      const struct page_setup *ps)
 {
   struct xr_driver *xr = xr_driver_cast (driver);
 
-  const double scale = 72 * XR_POINT;
-
-  int swap = setup->orientation == PAGE_LANDSCAPE;
+  int swap = ps->orientation == PAGE_LANDSCAPE;
   enum table_axis h = H ^ swap;
   enum table_axis v = V ^ swap;
 
+  int size[TABLE_N_AXES];
+  for (int a = 0; a < TABLE_N_AXES; a++)
+    size[a] = scale (ps->paper[a] - ps->margins[a][0] - ps->margins[a][1]);
+
   struct xr_page_style *old_ps = xr->page_style;
   xr->page_style = xmalloc (sizeof *xr->page_style);
   *xr->page_style = (struct xr_page_style) {
     .ref_cnt = 1,
 
     .margins = {
-      [H] = { setup->margins[h][0] * scale, setup->margins[h][1] * scale },
-      [V] = { setup->margins[v][0] * scale, setup->margins[v][1] * scale },
+      [H] = { scale (ps->margins[h][0]), scale (ps->margins[h][1]) },
+      [V] = { scale (ps->margins[v][0]), scale (ps->margins[v][1]) },
     },
 
-    .initial_page_number = setup->initial_page_number,
+    .initial_page_number = ps->initial_page_number,
     .include_outline = old_ps->include_outline,
   };
   for (size_t i = 0; i < 2; i++)
-    page_heading_copy (&xr->page_style->headings[i], &setup->headings[i]);
+    page_heading_copy (&xr->page_style->headings[i], &ps->headings[i]);
   xr_page_style_unref (old_ps);
 
   struct xr_fsm_style *old_fs = xr->fsm_style;
   xr->fsm_style = xmalloc (sizeof *xr->fsm_style);
   *xr->fsm_style = (struct xr_fsm_style) {
     .ref_cnt = 1,
-    .size = { [H] = setup->paper[H] * scale, [V] = setup->paper[V] * scale },
+    .size = { [H] = size[H], [V] = size[V] },
     .min_break = {
-      [H] = setup->paper[H] * scale / 2,
-      [V] = setup->paper[V] * scale / 2,
+      [H] = size[H] / 2,
+      [V] = size[V] / 2,
     },
     .font = pango_font_description_copy (old_fs->font),
     .fg = old_fs->fg,
     .use_system_colors = old_fs->use_system_colors,
-    .object_spacing = setup->object_spacing * 72 * XR_POINT,
+    .object_spacing = scale (ps->object_spacing),
     .font_resolution = old_fs->font_resolution,
   };
   xr_fsm_style_unref (old_fs);
 
-  xr_set_surface_size (xr->dest_surface, xr->output_type,
-                       setup->paper[H] * 72.0, setup->paper[V] * 72.0);
+  if (xr->dest_surface
+      && (xr->output_type == XR_PDF || xr->output_type == XR_PS))
+    xr_set_surface_size (xr->dest_surface, xr->output_type,
+                         ps->paper[H] * 72.0, ps->paper[V] * 72.0);
 }
 
 static void