render: Fix memory leaks in rendering.
[pspp] / src / output / render.c
index f41f504df0a90cc4e73a5941d3c238fb8ecbe17e..88b4db16a6f5a20691e8a5a0b4cce548ed63bea2 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
+   Copyright (C) 2009, 2010, 2011, 2013, 2014, 2016 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -33,6 +33,9 @@
 #include "gl/minmax.h"
 #include "gl/xalloc.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
 #define H TABLE_HORZ
 #define V TABLE_VERT
@@ -137,7 +140,7 @@ struct render_page
   };
 
 static struct render_page *render_page_create (const struct render_params *,
-                                               const struct table *);
+                                               struct table *);
 
 struct render_page *render_page_ref (const struct render_page *page_);
 static void render_page_unref (struct render_page *);
@@ -681,11 +684,9 @@ set_join_crossings (struct render_page *page, enum table_axis axis,
    size is PARAMS->size, but the caller is responsible for actually breaking it
    up to fit on such a device, using the render_break abstraction.  */
 static struct render_page *
-render_page_create (const struct render_params *params,
-                    const struct table *table_)
+render_page_create (const struct render_params *params, struct table *table)
 {
   struct render_page *page;
-  struct table *table;
   enum { MIN, MAX };
   struct render_row *columns[2];
   struct render_row *rows;
@@ -698,7 +699,6 @@ render_page_create (const struct render_params *params,
   int i;
   enum table_axis axis;
 
-  table = table_ref (table_);
   nc = table_nc (table);
   nr = table_nr (table);
 
@@ -927,13 +927,13 @@ render_page_unref (struct render_page *page)
 /* Returns the size of PAGE along AXIS.  (This might be larger than the page
    size specified in the parameters passed to render_page_create().  Use a
    render_break to break up a render_page into page-sized chunks.) */
-int
+static int
 render_page_get_size (const struct render_page *page, enum table_axis axis)
 {
   return page->cp[axis][page->n[axis] * 2 + 1];
 }
 
-int
+static int
 render_page_get_best_breakpoint (const struct render_page *page, int height)
 {
   int y;
@@ -966,6 +966,23 @@ is_rule (int z)
   return !(z & 1);
 }
 
+bool
+render_direction_rtl (void)
+{
+  /* TRANSLATORS: Do not translate this string.  If the script of your language 
+     reads from right to left (eg Persian, Arabic, Hebrew etc), then replace 
+     this string with "output-direction-rtl".  Otherwise either leave it 
+     untranslated or copy it verbatim. */
+  const char *dir = _("output-direction-ltr");
+  if ( 0 == strcmp ("output-direction-rtl", dir))
+    return true;
+
+  if ( 0 != strcmp ("output-direction-ltr", dir))
+    fprintf (stderr, "This localisation has been incorrectly translated.  Complain to the translator.\n");
+
+  return false;
+}
+
 static void
 render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
              const int d[TABLE_N_AXES])
@@ -1009,6 +1026,12 @@ render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
 
       bb[H][0] = ofs[H] + page->cp[H][d[H]];
       bb[H][1] = ofs[H] + page->cp[H][d[H] + 1];
+      if (render_direction_rtl ())
+       {
+         int temp = bb[H][0];
+         bb[H][0] = render_page_get_size (page, H) - bb[H][1];
+         bb[H][1] = render_page_get_size (page, H) - temp;
+       }
       bb[V][0] = ofs[V] + page->cp[V][d[V]];
       bb[V][1] = ofs[V] + page->cp[V][d[V] + 1];
       page->params->draw_line (page->params->aux, bb, styles);
@@ -1025,6 +1048,12 @@ render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
 
   bb[H][0] = clip[H][0] = ofs[H] + page->cp[H][cell->d[H][0] * 2 + 1];
   bb[H][1] = clip[H][1] = ofs[H] + page->cp[H][cell->d[H][1] * 2];
+  if (render_direction_rtl ())
+    {
+      int temp = bb[H][0];
+      bb[H][0] = clip[H][0] = render_page_get_size (page, H) - bb[H][1];
+      bb[H][1] = clip[H][1] = render_page_get_size (page, H) - temp;
+    }
   bb[V][0] = clip[V][0] = ofs[V] + page->cp[V][cell->d[V][0] * 2 + 1];
   bb[V][1] = clip[V][1] = ofs[V] + page->cp[V][cell->d[V][1] * 2];
 
@@ -1085,7 +1114,7 @@ render_page_draw_cells (const struct render_page *page,
 
 /* Renders PAGE, by calling the 'draw_line' and 'draw_cell' functions from the
    render_params provided to render_page_create(). */
-void
+static void
 render_page_draw (const struct render_page *page, int ofs[TABLE_N_AXES])
 {
   int bb[TABLE_N_AXES][2];
@@ -1151,7 +1180,7 @@ get_clip_max_extent (int x1, const int cp[], int n)
 /* Renders the cells of PAGE that intersect (X,Y)-(X+W,Y+H), by calling the
    'draw_line' and 'draw_cell' functions from the render_params provided to
    render_page_create(). */
-void
+static void
 render_page_draw_region (const struct render_page *page,
                          int ofs[TABLE_N_AXES], int clip[TABLE_N_AXES][2])
 {
@@ -1164,7 +1193,7 @@ render_page_draw_region (const struct render_page *page,
 
   render_page_draw_cells (page, ofs, bb);
 }
-\f
+
 /* Breaking up tables to fit on a page. */
 
 /* An iterator for breaking render_pages into smaller chunks. */
@@ -1184,12 +1213,13 @@ static struct render_page *render_page_select (const struct render_page *,
                                                int z0, int p0,
                                                int z1, int p1);
 
-/* Initializes render_break B for breaking PAGE along AXIS. */
+/* Initializes render_break B for breaking PAGE along AXIS.
+   Takes ownership of PAGE. */
 static void
-render_break_init (struct render_break *b, const struct render_page *page,
+render_break_init (struct render_break *b, struct render_page *page,
                    enum table_axis axis)
 {
-  b->page = render_page_ref (page);
+  b->page = page;
   b->axis = axis;
   b->z = page->h[axis][0];
   b->pixel = 0;
@@ -1416,18 +1446,22 @@ struct render_pager
     struct render_break y_break;
   };
 
-static void
+static const struct render_page *
 render_pager_add_table (struct render_pager *p, struct table *table)
 {
+  struct render_page *page;
+
   if (p->n_pages >= p->allocated_pages)
     p->pages = x2nrealloc (p->pages, &p->allocated_pages, sizeof *p->pages);
-  p->pages[p->n_pages++] = render_page_create (p->params, table);
+  page = p->pages[p->n_pages++] = render_page_create (p->params, table);
+  return page;
 }
 
 static void
 render_pager_start_page (struct render_pager *p)
 {
-  render_break_init (&p->x_break, p->pages[p->cur_page++], H);
+  render_break_init (&p->x_break, render_page_ref (p->pages[p->cur_page++]),
+                     H);
   render_break_init_empty (&p->y_break);
 }
 
@@ -1483,17 +1517,28 @@ struct render_pager *
 render_pager_create (const struct render_params *params,
                      const struct table_item *table_item)
 {
+  const char *caption = table_item_get_caption (table_item);
+  const char *title = table_item_get_title (table_item);
+  const struct render_page *body_page;
   struct render_pager *p;
-  const char *caption;
 
   p = xzalloc (sizeof *p);
   p->params = params;
 
-  caption = table_item_get_caption (table_item);
+  /* Title. */
+  if (title)
+    render_pager_add_table (p, table_from_string (TAB_LEFT, title));
+
+  /* Body. */
+  body_page = render_pager_add_table (p, table_ref (table_item_get_table (
+                                                      table_item)));
+
+  /* Caption. */
   if (caption)
     render_pager_add_table (p, table_from_string (TAB_LEFT, caption));
-  render_pager_add_table (p, table_ref (table_item_get_table (table_item)));
-  add_footnote_page (p, p->pages[p->n_pages - 1]);
+
+  /* Footnotes. */
+  add_footnote_page (p, body_page);
 
   render_pager_start_page (p);
 
@@ -1599,11 +1644,14 @@ render_pager_draw_region (const struct render_pager *p,
   for (i = 0; i < p->n_pages; i++)
     {
       const struct render_page *page = p->pages[i];
+      int size = render_page_get_size (page, V);
 
       clip[V][0] = MAX (y, ofs[V]) - ofs[V];
-      clip[V][1] = MIN (y + h, ofs[V] + render_page_get_size (page, V)) - ofs[V];
+      clip[V][1] = MIN (y + h, ofs[V] + size) - ofs[V];
       if (clip[V][1] > clip[V][0])
         render_page_draw_region (page, ofs, clip);
+
+      ofs[V] += size;
     }
 }