-\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)
-{
- struct xr_driver *xr = xr_allocate ("cairo", 0, options);
- if (!xr_set_cairo (xr, cairo))
- {
- output_driver_destroy (&xr->driver);
- return NULL;
- }
- return xr;
-}
-
-/* Destroy XR, which should have been created with xr_driver_create(). Any
- cairo_t added to XR is not destroyed, because it is owned by the client. */
-void
-xr_driver_destroy (struct xr_driver *xr)
-{
- if (xr != NULL)
- {
- xr->cairo = NULL;
- 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 (TAB_LEFT, text),
- NULL, NULL);
- r = xr_rendering_create (xr, &table_item->output_item, cr);
- table_item_unref (table_item);
-
- return r;
-}
-
-void
-xr_rendering_apply_options (struct xr_rendering *xr, struct string_map *o)
-{
- if (is_table_item (xr->item))
- apply_options (xr->xr, o);
-}
-
-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);
- const struct msg *msg = message_item_get_msg (message_item);
- char *s = msg_to_string (msg, NULL);
- 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);
- }
-
- 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 (struct xr_rendering *r, int *w, int *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;
- }
-}
-
-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_all (struct xr_rendering *r, cairo_t *cr)
-{
- if (is_table_item (r->item))
- {
- struct xr_driver *xr = r->xr;
-
- xr_set_cairo (xr, cr);
-
- render_pager_draw (r->p);
-
- }
- 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)
-{
- struct xrchart_geometry geom;
-
- cairo_save (cr);
- cairo_translate (cr, x, y + height);
- cairo_scale (cr, 1.0, -1.0);
- xrchart_geometry_init (cr, &geom, width, height);
- if (is_boxplot (chart_item))
- xrchart_draw_boxplot (chart_item, cr, &geom);
- else if (is_histogram_chart (chart_item))
- xrchart_draw_histogram (chart_item, cr, &geom);
- else if (is_np_plot_chart (chart_item))
- xrchart_draw_np_plot (chart_item, cr, &geom);
- else if (is_piechart (chart_item))
- xrchart_draw_piechart (chart_item, cr, &geom);
- else if (is_barchart (chart_item))
- xrchart_draw_barchart (chart_item, cr, &geom);
- else if (is_roc_chart (chart_item))
- xrchart_draw_roc (chart_item, cr, &geom);
- else if (is_scree (chart_item))
- xrchart_draw_scree (chart_item, cr, &geom);
- else if (is_spreadlevel_plot_chart (chart_item))
- xrchart_draw_spreadlevel (chart_item, cr, &geom);
- else if (is_scatterplot_chart (chart_item))
- xrchart_draw_scatterplot (chart_item, cr, &geom);
- else
- NOT_REACHED ();
- xrchart_geometry_free (cr, &geom);
-
- cairo_restore (cr);
-}
-
-char *
-xr_draw_png_chart (const struct chart_item *item,
- const char *file_name_template, int number,
- const struct xr_color *fg,
- const struct xr_color *bg
- )
-{
- const int width = 640;
- const int length = 480;
-
- cairo_surface_t *surface;
- cairo_status_t status;
- const char *number_pos;
- char *file_name;
- cairo_t *cr;
-
- number_pos = strchr (file_name_template, '#');
- if (number_pos != NULL)
- file_name = xasprintf ("%.*s%d%s", (int) (number_pos - file_name_template),
- file_name_template, number, number_pos + 1);
- else
- file_name = xstrdup (file_name_template);
-
- surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
- cr = cairo_create (surface);
-
- cairo_set_source_rgb (cr, bg->red, bg->green, bg->blue);
- cairo_paint (cr);
-
- cairo_set_source_rgb (cr, fg->red, fg->green, fg->blue);
-
- xr_draw_chart (item, cr, 0.0, 0.0, width, length);
-
- status = cairo_surface_write_to_png (surface, file_name);
- if (status != CAIRO_STATUS_SUCCESS)
- msg (ME, _("error writing output file `%s': %s"),
- file_name, cairo_status_to_string (status));
-
- cairo_destroy (cr);
- cairo_surface_destroy (surface);
-
- return file_name;
-}
-\f
-struct xr_table_state
- {
- struct xr_render_fsm fsm;
- struct table_item *table_item;
- struct render_pager *p;
- };
-
-static bool
-xr_table_render (struct xr_render_fsm *fsm, struct xr_driver *xr)
-{
- struct xr_table_state *ts = UP_CAST (fsm, struct xr_table_state, fsm);
-
- while (render_pager_has_next (ts->p))
- {
- int used;
-
- used = render_pager_draw_next (ts->p, xr->length - xr->y);
- if (!used)
- {
- assert (xr->y > 0);
- return true;
- }
- else
- xr->y += used;
- }
- return false;
-}
-
-static void
-xr_table_destroy (struct xr_render_fsm *fsm)
-{
- struct xr_table_state *ts = UP_CAST (fsm, struct xr_table_state, fsm);
-
- table_item_unref (ts->table_item);
- render_pager_destroy (ts->p);
- free (ts);
-}
-
-static struct xr_render_fsm *
-xr_render_table (struct xr_driver *xr, const struct table_item *table_item)
-{
- struct xr_table_state *ts;
-
- ts = xmalloc (sizeof *ts);
- ts->fsm.render = xr_table_render;
- ts->fsm.destroy = xr_table_destroy;
- ts->table_item = table_item_ref (table_item);
-
- if (xr->y > 0)
- xr->y += xr->char_height;
-
- ts->p = render_pager_create (xr->params, table_item);
-
- return &ts->fsm;
-}
-\f
-struct xr_chart_state
- {
- struct xr_render_fsm fsm;
- struct chart_item *chart_item;
- };
-
-static bool
-xr_chart_render (struct xr_render_fsm *fsm, struct xr_driver *xr)
-{
- struct xr_chart_state *cs = UP_CAST (fsm, struct xr_chart_state, fsm);
-
- if (xr->y > 0)
- return true;
-
- if (xr->cairo != NULL)
- xr_draw_chart (cs->chart_item, xr->cairo, 0.0, 0.0,
- xr_to_pt (xr->width), xr_to_pt (xr->length));
- xr->y = xr->length;
-
- return false;
-}
-
-static void
-xr_chart_destroy (struct xr_render_fsm *fsm)
-{
- struct xr_chart_state *cs = UP_CAST (fsm, struct xr_chart_state, fsm);
-
- chart_item_unref (cs->chart_item);
- free (cs);
-}
-
-static struct xr_render_fsm *
-xr_render_chart (const struct chart_item *chart_item)
-{
- struct xr_chart_state *cs;
-
- cs = xmalloc (sizeof *cs);
- cs->fsm.render = xr_chart_render;
- cs->fsm.destroy = xr_chart_destroy;
- cs->chart_item = chart_item_ref (chart_item);
-
- return &cs->fsm;
-}
-\f
-static bool
-xr_eject_render (struct xr_render_fsm *fsm UNUSED, struct xr_driver *xr)
-{
- return xr->y > 0;
-}
-
-static void
-xr_eject_destroy (struct xr_render_fsm *fsm UNUSED)
-{
- /* Nothing to do. */
-}
-
-static struct xr_render_fsm *
-xr_render_eject (void)
-{
- static struct xr_render_fsm eject_renderer =
- {
- xr_eject_render,
- xr_eject_destroy
- };
-
- return &eject_renderer;
-}
-\f
-static struct xr_render_fsm *
-xr_create_text_renderer (struct xr_driver *xr, const char *text)
-{
- struct table_item *table_item;
- struct xr_render_fsm *fsm;
-
- table_item = table_item_create (table_from_string (TAB_LEFT, text),
- NULL, NULL);
- fsm = xr_render_table (xr, table_item);
- table_item_unref (table_item);
-
- return fsm;
-}
-
-static struct xr_render_fsm *
-xr_render_text (struct xr_driver *xr, const struct text_item *text_item)
-{
- enum text_item_type type = text_item_get_type (text_item);
- const char *text = text_item_get_text (text_item);
-
- switch (type)
- {
- case TEXT_ITEM_TITLE:
- free (xr->title);
- xr->title = xstrdup (text);
- break;
-
- case TEXT_ITEM_SUBTITLE:
- free (xr->subtitle);
- xr->subtitle = xstrdup (text);
- break;
-
- case TEXT_ITEM_COMMAND_CLOSE:
- break;
-
- case TEXT_ITEM_BLANK_LINE:
- if (xr->y > 0)
- xr->y += xr->char_height;
- break;
-
- case TEXT_ITEM_EJECT_PAGE:
- if (xr->y > 0)
- return xr_render_eject ();
- break;
-
- default:
- return xr_create_text_renderer (xr, text);
- }
-
- return NULL;
-}
-
-static struct xr_render_fsm *
-xr_render_message (struct xr_driver *xr,
- const struct message_item *message_item)
-{
- const struct msg *msg = message_item_get_msg (message_item);
- struct xr_render_fsm *fsm;
- char *s;
-
- s = msg_to_string (msg, xr->command_name);
- fsm = xr_create_text_renderer (xr, s);
- free (s);
-
- return fsm;
-}
-
-static struct xr_render_fsm *
-xr_render_output_item (struct xr_driver *xr,
- const struct output_item *output_item)
-{
- if (is_table_item (output_item))
- return xr_render_table (xr, to_table_item (output_item));
- else if (is_chart_item (output_item))
- return xr_render_chart (to_chart_item (output_item));
- else if (is_text_item (output_item))
- return xr_render_text (xr, to_text_item (output_item));
- else if (is_message_item (output_item))
- return xr_render_message (xr, to_message_item (output_item));
- else
- return NULL;
-}