output: Make spv-writer code understand output_item.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Jan 2021 05:28:37 +0000 (21:28 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Jan 2021 05:28:37 +0000 (21:28 -0800)
src/output/spv-driver.c
src/output/spv/spv-writer.c
src/output/spv/spv-writer.h

index a0cb023f3431a997551409a07647703cc95f40ec..75c8e82f04e842cddc8e6934ac5dbaae16e6801a 100644 (file)
@@ -98,42 +98,7 @@ spv_submit (struct output_driver *driver,
 {
   struct spv_driver *spv = spv_driver_cast (driver);
 
-  if (is_group_open_item (output_item))
-    spv_writer_open_heading (spv->writer,
-                             to_group_open_item (output_item)->command_name,
-                             to_group_open_item (output_item)->command_name);
-  else if (is_group_close_item (output_item))
-    spv_writer_close_heading (spv->writer);
-  else if (is_table_item (output_item))
-    {
-      const struct table_item *table_item = to_table_item (output_item);
-      if (table_item->pt)
-        spv_writer_put_table (spv->writer, table_item->pt);
-    }
-  else if (is_chart_item (output_item))
-    {
-      cairo_surface_t *surface = xr_draw_image_chart (
-        to_chart_item (output_item),
-        &(struct cell_color) CELL_COLOR_BLACK,
-        &(struct cell_color) CELL_COLOR_WHITE);
-      if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
-        spv_writer_put_image (spv->writer, surface);
-      cairo_surface_destroy (surface);
-    }
-  else if (is_image_item (output_item))
-    spv_writer_put_image (spv->writer, to_image_item (output_item)->image);
-  else if (is_text_item (output_item))
-    {
-      char *command_id = output_get_command_name ();
-      spv_writer_put_text (spv->writer, to_text_item (output_item),
-                           command_id);
-      free (command_id);
-    }
-  else if (is_page_eject_item (output_item))
-    spv_writer_eject_page (spv->writer);
-  else if (is_page_setup_item (output_item))
-    spv_writer_set_page_setup (spv->writer,
-                               to_page_setup_item (output_item)->page_setup);
+  spv_writer_write (spv->writer, output_item);
 }
 
 struct output_driver_factory spv_driver_factory =
index 09ca01b412f10f77844e8b96dc81ffb529d3e199..e43dccf2d25f7b02a383b33b84b52d0206e87876 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "output/spv/spv-writer.h"
 
+#include <cairo.h>
 #include <inttypes.h>
 #include <libxml/xmlwriter.h>
 #include <math.h>
 #include "libpspp/temp-file.h"
 #include "libpspp/version.h"
 #include "libpspp/zip-writer.h"
+#include "output/cairo-chart.h"
+#include "output/chart-item.h"
+#include "output/driver.h"
+#include "output/group-item.h"
+#include "output/image-item.h"
+#include "output/page-eject-item.h"
 #include "output/page-setup-item.h"
 #include "output/pivot-table.h"
+#include "output/table-item.h"
 #include "output/text-item.h"
 
 #include "gl/xalloc.h"
@@ -58,6 +66,8 @@ struct spv_writer
     bool need_page_break;
   };
 
+static void spv_writer_close_heading (struct spv_writer *);
+
 char * WARN_UNUSED_RESULT
 spv_writer_open (const char *filename, struct spv_writer **writerp)
 {
@@ -93,14 +103,6 @@ spv_writer_close (struct spv_writer *w)
   return error;
 }
 
-void
-spv_writer_set_page_setup (struct spv_writer *w,
-                           const struct page_setup *page_setup)
-{
-  page_setup_destroy (w->page_setup);
-  w->page_setup = page_setup_clone (page_setup);
-}
-
 static void
 write_attr (struct spv_writer *w, const char *name, const char *value)
 {
@@ -224,7 +226,7 @@ spv_writer_open_file (struct spv_writer *w)
   return true;
 }
 
-void
+static void
 spv_writer_open_heading (struct spv_writer *w, const char *command_id,
                          const char *label)
 {
@@ -263,7 +265,7 @@ spv_writer_close_file (struct spv_writer *w, const char *infix)
   w->heading = NULL;
 }
 
-void
+static void
 spv_writer_close_heading (struct spv_writer *w)
 {
   const char *infix = "";
@@ -290,7 +292,7 @@ start_container (struct spv_writer *w)
     }
 }
 
-void
+static void
 spv_writer_put_text (struct spv_writer *w, const struct text_item *text,
                      const char *command_id)
 {
@@ -332,7 +334,7 @@ write_to_zip (void *zw_, const unsigned char *data, unsigned int length)
   return CAIRO_STATUS_SUCCESS;
 }
 
-void
+static void
 spv_writer_put_image (struct spv_writer *w, cairo_surface_t *image)
 {
   bool initial_depth = w->heading_depth;
@@ -362,12 +364,6 @@ spv_writer_put_image (struct spv_writer *w, cairo_surface_t *image)
 
   free (uri);
 }
-
-void
-spv_writer_eject_page (struct spv_writer *w)
-{
-  w->need_page_break = true;
-}
 \f
 #define H TABLE_HORZ
 #define V TABLE_VERT
@@ -1053,7 +1049,7 @@ put_light_table (struct buf *buf, uint64_t table_id,
     }
 }
 
-void
+static void
 spv_writer_put_table (struct spv_writer *w, const struct pivot_table *table)
 {
   struct pivot_table *table_rw = CONST_CAST (struct pivot_table *, table);
@@ -1103,3 +1099,46 @@ spv_writer_put_table (struct spv_writer *w, const struct pivot_table *table)
 
   free (data_path);
 }
+\f
+void
+spv_writer_write (struct spv_writer *w, const struct output_item *item)
+{
+  if (is_group_open_item (item))
+    spv_writer_open_heading (w,
+                             to_group_open_item (item)->command_name,
+                             to_group_open_item (item)->command_name);
+  else if (is_group_close_item (item))
+    spv_writer_close_heading (w);
+  else if (is_table_item (item))
+    {
+      const struct table_item *table_item = to_table_item (item);
+      if (table_item->pt)
+        spv_writer_put_table (w, table_item->pt);
+    }
+  else if (is_chart_item (item))
+    {
+      cairo_surface_t *surface = xr_draw_image_chart (
+        to_chart_item (item),
+        &(struct cell_color) CELL_COLOR_BLACK,
+        &(struct cell_color) CELL_COLOR_WHITE);
+      if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
+        spv_writer_put_image (w, surface);
+      cairo_surface_destroy (surface);
+    }
+  else if (is_image_item (item))
+    spv_writer_put_image (w, to_image_item (item)->image);
+  else if (is_text_item (item))
+    {
+      char *command_id = output_get_command_name ();
+      spv_writer_put_text (w, to_text_item (item),
+                           command_id);
+      free (command_id);
+    }
+  else if (is_page_eject_item (item))
+    w->need_page_break = true;
+  else if (is_page_setup_item (item))
+    {
+      page_setup_destroy (w->page_setup);
+      w->page_setup = page_setup_clone (to_page_setup_item (item)->page_setup);
+    }
+}
index cc937216cb0189c47b15893a1255e8a9906c8ccb..7641f1c34cb80ec7880cd7c860686f2a0f99f9b6 100644 (file)
 #ifndef OUTPUT_SPV_WRITER_H
 #define OUTPUT_SPV_WRITER_H 1
 
-struct page_setup;
-struct pivot_table;
+struct output_item;
 struct spv_writer;
-struct text_item;
-
-#include <cairo.h>
 
 #include "libpspp/compiler.h"
 
@@ -30,19 +26,6 @@ char *spv_writer_open (const char *filename, struct spv_writer **)
   WARN_UNUSED_RESULT;
 char *spv_writer_close (struct spv_writer *) WARN_UNUSED_RESULT;
 
-void spv_writer_set_page_setup (struct spv_writer *,
-                                const struct page_setup *);
-
-void spv_writer_open_heading (struct spv_writer *, const char *command_id,
-                              const char *label);
-void spv_writer_close_heading (struct spv_writer *);
-
-void spv_writer_put_text (struct spv_writer *, const struct text_item *,
-                          const char *command_id);
-void spv_writer_put_table (struct spv_writer *, const struct pivot_table *);
-
-void spv_writer_put_image (struct spv_writer *, cairo_surface_t *);
-
-void spv_writer_eject_page (struct spv_writer *);
+void spv_writer_write (struct spv_writer *, const struct output_item *);
 
 #endif /* output/spv/spv-writer.h */