cairo: Factor out code for basic rendering.
[pspp] / src / output / cairo.c
index b02e8974f8ac23f4c3aca63a3224b2350e4b5832..2556ab6fc16e621e366784ca9538f351e789193f 100644 (file)
@@ -29,6 +29,7 @@
 #include "libpspp/version.h"
 #include "data/file-handle-def.h"
 #include "output/cairo-chart.h"
+#include "output/cairo-fsm.h"
 #include "output/chart-item-provider.h"
 #include "output/charts/boxplot.h"
 #include "output/charts/np-plot.h"
@@ -53,6 +54,7 @@
 #include <cairo/cairo-pdf.h>
 #include <cairo/cairo-ps.h>
 #include <cairo/cairo-svg.h>
+
 #include <cairo/cairo.h>
 #include <inttypes.h>
 #include <math.h>
@@ -105,14 +107,6 @@ enum xr_output_type
     XR_SVG
   };
 
-/* Cairo fonts. */
-enum xr_font_type
-  {
-    XR_FONT_PROPORTIONAL,
-    XR_FONT_FIXED,
-    XR_N_FONTS
-  };
-
 /* A font for use with Cairo. */
 struct xr_font
   {
@@ -1565,22 +1559,6 @@ static const struct output_driver_class cairo_driver_class =
   xr_flush,
 };
 \f
-/* GUI rendering helpers. */
-
-struct xr_rendering
-  {
-    struct output_item *item;
-
-    /* Table items. */
-    struct render_pager *p;
-    struct xr_driver *xr;
-  };
-
-#define CHART_WIDTH 500
-#define CHART_HEIGHT 375
-
-
-
 struct xr_driver *
 xr_driver_create (cairo_t *cairo, struct string_map *options)
 {
@@ -1600,111 +1578,6 @@ xr_driver_destroy (struct xr_driver *xr)
       output_driver_destroy (&xr->driver);
     }
 }
-
-static struct xr_rendering *
-xr_rendering_create_text (struct xr_driver *xr, const char *text, cairo_t *cr)
-{
-  struct table_item *table_item;
-  struct xr_rendering *r;
-
-  table_item = table_item_create (table_from_string (text), NULL, NULL);
-  r = xr_rendering_create (xr, &table_item->output_item, cr);
-  table_item_unref (table_item);
-
-  return r;
-}
-
-struct xr_rendering *
-xr_rendering_create (struct xr_driver *xr, const struct output_item *item,
-                     cairo_t *cr)
-{
-  struct xr_rendering *r = NULL;
-
-  if (is_text_item (item))
-    r = xr_rendering_create_text (xr, text_item_get_text (to_text_item (item)),
-                                  cr);
-  else if (is_message_item (item))
-    {
-      const struct message_item *message_item = to_message_item (item);
-      char *s = msg_to_string (message_item_get_msg (message_item));
-      r = xr_rendering_create_text (xr, s, cr);
-      free (s);
-    }
-  else if (is_table_item (item))
-    {
-      r = xzalloc (sizeof *r);
-      r->item = output_item_ref (item);
-      r->xr = xr;
-      xr_set_cairo (xr, cr);
-      r->p = render_pager_create (xr->params, to_table_item (item));
-    }
-  else if (is_chart_item (item))
-    {
-      r = xzalloc (sizeof *r);
-      r->item = output_item_ref (item);
-    }
-  else if (is_group_open_item (item))
-    r = xr_rendering_create_text (xr, to_group_open_item (item)->command_name,
-                                  cr);
-
-  return r;
-}
-
-void
-xr_rendering_destroy (struct xr_rendering *r)
-{
-  if (r)
-    {
-      output_item_unref (r->item);
-      render_pager_destroy (r->p);
-      free (r);
-    }
-}
-
-void
-xr_rendering_measure (const struct xr_rendering *r, int *wp, int *hp)
-{
-  int w, h;
-
-  if (is_table_item (r->item))
-    {
-      w = render_pager_get_size (r->p, H) / XR_POINT;
-      h = render_pager_get_size (r->p, V) / XR_POINT;
-    }
-  else
-    {
-      w = CHART_WIDTH;
-      h = CHART_HEIGHT;
-    }
-
-  if (wp)
-    *wp = w;
-  if (hp)
-    *hp = h;
-}
-
-static void xr_draw_chart (const struct chart_item *, cairo_t *,
-                    double x, double y, double width, double height);
-
-/* Draws onto CR */
-void
-xr_rendering_draw (struct xr_rendering *r, cairo_t *cr,
-                   int x0, int y0, int x1, int y1)
-{
-  if (is_table_item (r->item))
-    {
-      struct xr_driver *xr = r->xr;
-
-      xr_set_cairo (xr, cr);
-
-      render_pager_draw_region (r->p, x0 * XR_POINT, y0 * XR_POINT,
-                                (x1 - x0) * XR_POINT, (y1 - y0) * XR_POINT);
-    }
-  else
-    xr_draw_chart (to_chart_item (r->item), cr,
-                   0, 0, CHART_WIDTH, CHART_HEIGHT);
-}
-
 static void
 xr_draw_chart (const struct chart_item *chart_item, cairo_t *cr,
                double x, double y, double width, double height)
@@ -1998,23 +1871,3 @@ xr_render_output_item (struct xr_driver *xr,
   else
     return NULL;
 }
-
-bool
-xr_draw_svg_file (struct xr_rendering *r,
-                 const char *filename)
-{
-  int width, height;
-  g_assert (r);
-  xr_rendering_measure (r, &width, &height);
-  cairo_surface_t *surface = cairo_svg_surface_create (filename, width, height);
-  if (!surface)
-    {
-      g_error ("Could not create cairo svg surface with file %s", filename);
-      return FALSE;
-    }
-  cairo_t *cr = cairo_create (surface);
-  xr_rendering_draw (r, cr, 0, 0, width, height);
-  cairo_destroy (cr);
-  cairo_surface_destroy (surface);
-  return TRUE;
-}