treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / output / tex.c
index 2d29c2aa85629affce079eb58cf146e347af703b..de111ec13f47b74177b0a48aed47046a59053e39 100644 (file)
 #include "libpspp/temp-file.h"
 #include "libpspp/version.h"
 #include "output/cairo-chart.h"
-#include "output/chart-item.h"
 #include "output/driver-provider.h"
-#include "output/image-item.h"
-#include "output/message-item.h"
 #include "output/options.h"
-#include "output/output-item-provider.h"
+#include "output/output-item.h"
 #include "output/pivot-output.h"
 #include "output/pivot-table.h"
 #include "output/table-provider.h"
-#include "output/table-item.h"
-#include "output/text-item.h"
 #include "output/tex-rendering.h"
 #include "output/tex-parsing.h"
 
@@ -80,7 +75,7 @@ struct tex_driver
     char *chart_file_name;
 
     FILE *file;
-    size_t chart_cnt;
+    size_t n_charts;
 
     struct ll_list preamble_list;
     struct ll_list token_list;
@@ -104,7 +99,7 @@ shipout (struct ll_list *list, const char *str, ...)
 
 static const struct output_driver_class tex_driver_class;
 
-static void tex_output_table (struct tex_driver *, const struct table_item *);
+static void tex_output_table (struct tex_driver *, const struct pivot_table *);
 
 static struct tex_driver *
 tex_driver_cast (struct output_driver *driver)
@@ -136,7 +131,7 @@ tex_create (struct file_handle *fh, enum settings_output_devices device_type,
   tex->handle = fh;
   tex->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
                                                       fh_get_file_name (fh)));
-  tex->chart_cnt = 1;
+  tex->n_charts = 1;
   tex->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
   tex->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
 
@@ -180,7 +175,11 @@ post_process_tokens (FILE *file, struct ll_list *list)
               if (nt->cat == CAT_SPACE || nt->cat == CAT_EOL)
                 break;
               if (prev_x && (prev_x->cat == CAT_COMMENT) && (nt->cat != CAT_COMMENT))
-                break;
+                {
+                  ds_destroy (&prev_x->str);
+                  free (prev_x);
+                  break;
+                }
               word_len += ds_length (&nt->str);
               prev_x = nt;
             }
@@ -189,6 +188,11 @@ post_process_tokens (FILE *file, struct ll_list *list)
             {
               fputs ("\n", file);
               line_len = 0;
+              if (tt)
+                {
+                  ds_destroy (&tt->str);
+                  free (tt);
+                }
               continue;
             }
         }
@@ -308,97 +312,113 @@ tex_escape_string (struct tex_driver *tex, const char *text,
 }
 
 static void
-tex_submit (struct output_driver *driver,
-             const struct output_item *output_item)
+tex_submit (struct output_driver *driver, const struct output_item *item)
 {
   struct tex_driver *tex = tex_driver_cast (driver);
 
-  if (is_table_item (output_item))
-    {
-      struct table_item *table_item = to_table_item (output_item);
-      tex_output_table (tex, table_item);
-    }
-  else if (is_image_item (output_item) && tex->chart_file_name != NULL)
+  switch (item->type)
     {
-      struct image_item *image_item = to_image_item (output_item);
-      char *file_name = xr_write_png_image (
-        image_item->image, tex->chart_file_name, tex->chart_cnt++);
-      if (file_name != NULL)
+    case OUTPUT_ITEM_CHART:
+      if (tex->chart_file_name != NULL)
         {
-          shipout (&tex->token_list, "\\includegraphics{%s}\n", file_name);
-          tex->require_graphics = true;
-          free (file_name);
-        }
-    }
-  else if (is_chart_item (output_item) && tex->chart_file_name != NULL)
-    {
-      struct chart_item *chart_item = to_chart_item (output_item);
-      char *file_name = xr_draw_png_chart (chart_item, tex->chart_file_name,
-                                           tex->chart_cnt++,
-                                           &tex->fg,
-                                           &tex->bg);
-      if (file_name != NULL)
-        {
-         //const char *title = chart_item_get_title (chart_item);
-          //          printf ("The chart title is %s\n", title);
-
-          shipout (&tex->token_list, "\\includegraphics{%s}\n", file_name);
-          tex->require_graphics = true;
-          free (file_name);
-        }
-    }
-  else if (is_text_item (output_item))
-    {
-      struct text_item *text_item = to_text_item (output_item);
-      const char *s = text_item_get_text (text_item);
+          char *file_name = xr_draw_png_chart (item->chart,
+                                               tex->chart_file_name,
+                                               tex->n_charts++,
+                                               &tex->fg, &tex->bg);
+          if (file_name != NULL)
+            {
+              //const char *title = chart_item_get_title (chart_item);
+              //          printf ("The chart title is %s\n", title);
 
-      switch (text_item_get_type (text_item))
-        {
-        case TEXT_ITEM_PAGE_TITLE:
-          shipout (&tex->token_list, "\\headline={\\bf ");
-          tex_escape_string (tex, s, false);
-          shipout (&tex->token_list, "\\hfil}\n");
-          break;
-
-        case TEXT_ITEM_LOG:
-          shipout (&tex->token_list, "{\\tt ");
-          tex_escape_string (tex, s, false);
-          shipout (&tex->token_list, "}\\par\n\n");
-          break;
-
-        case TEXT_ITEM_SYNTAX:
-          /* So far as I'm aware, this can never happen.  */
-        default:
-          printf ("Unhandled type %d\n", text_item_get_type (text_item));
-          break;
+              shipout (&tex->token_list, "\\includegraphics{%s}\n", file_name);
+              tex->require_graphics = true;
+              free (file_name);
+            }
         }
-    }
-  else if (is_message_item (output_item))
-    {
-      const struct message_item *message_item = to_message_item (output_item);
-      char *s = msg_to_string (message_item_get_msg (message_item));
-      tex_escape_string (tex, s, false);
-      shipout (&tex->token_list, "\\par\n");
-      free (s);
+      break;
+
+    case OUTPUT_ITEM_GROUP:
+      break;
+
+    case OUTPUT_ITEM_IMAGE:
+      {
+        char *file_name = xr_write_png_image (
+          item->image, tex->chart_file_name, tex->n_charts++);
+        if (file_name != NULL)
+          {
+            shipout (&tex->token_list, "\\includegraphics{%s}\n", file_name);
+            tex->require_graphics = true;
+            free (file_name);
+          }
+      }
+      break;
+
+    case OUTPUT_ITEM_MESSAGE:
+      {
+        char *s = msg_to_string (item->message);
+        tex_escape_string (tex, s, false);
+        shipout (&tex->token_list, "\\par\n");
+        free (s);
+      }
+      break;
+
+    case OUTPUT_ITEM_PAGE_BREAK:
+      break;
+
+    case OUTPUT_ITEM_TABLE:
+      tex_output_table (tex, item->table);
+      break;
+
+    case OUTPUT_ITEM_TEXT:
+      {
+        char *s = text_item_get_plain_text (item);
+
+        switch (item->text.subtype)
+          {
+          case TEXT_ITEM_PAGE_TITLE:
+            shipout (&tex->token_list, "\\headline={\\bf ");
+            tex_escape_string (tex, s, false);
+            shipout (&tex->token_list, "\\hfil}\n");
+            break;
+
+          case TEXT_ITEM_LOG:
+            shipout (&tex->token_list, "{\\tt ");
+            tex_escape_string (tex, s, false);
+            shipout (&tex->token_list, "}\\par\n\n");
+            break;
+
+          case TEXT_ITEM_SYNTAX:
+            /* So far as I'm aware, this can never happen.  */
+          default:
+            printf ("Unhandled type %d\n", item->text.subtype);
+            break;
+          }
+        free (s);
+      }
+      break;
     }
 }
 
 static void
 tex_put_footnote_markers (struct tex_driver *tex,
                           const struct pivot_table *pt,
-                          const size_t *footnote_indexes,
-                          size_t n_footnotes)
+                          const struct pivot_value_ex *ex)
 {
-  if (n_footnotes > 0)
-    shipout (&tex->token_list, "$^{");
-  for (size_t i = 0; i < n_footnotes; i++)
+  size_t n_visible = 0;
+  for (size_t i = 0; i < ex->n_footnotes; i++)
     {
-      const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
-      char *marker = pivot_value_to_string (f->marker, pt);
-      tex_escape_string (tex, marker, true);
-      free (marker);
+      const struct pivot_footnote *f = pt->footnotes[ex->footnote_indexes[i]];
+      if (f->show)
+        {
+          if (!n_visible++)
+            shipout (&tex->token_list, "$^{");
+
+          char *marker = pivot_footnote_marker_string (f, pt);
+          tex_escape_string (tex, marker, true);
+          free (marker);
+        }
     }
-  if (n_footnotes > 0)
+  if (n_visible)
     shipout (&tex->token_list, "}$");
 }
 
@@ -411,9 +431,7 @@ tex_put_table_cell (struct tex_driver *tex, const struct pivot_table *pt,
   tex_escape_string (tex, ds_cstr (&s), false);
   ds_destroy (&s);
 
-  tex_put_footnote_markers (tex, pt,
-                            cell->value->footnote_indexes,
-                            cell->value->n_footnotes);
+  tex_put_footnote_markers (tex, pt, pivot_value_ex (cell->value));
 }
 
 static void
@@ -537,8 +555,7 @@ tex_output_table_layer (struct tex_driver *tex, const struct pivot_table *pt,
           tex_escape_string (tex, ds_cstr (&s), true);
           ds_destroy (&s);
 
-          tex_put_footnote_markers (tex, pt, cell.value->footnote_indexes,
-                                    cell.value->n_footnotes);
+          tex_put_footnote_markers (tex, pt, pivot_value_ex (cell.value));
           if (halign == TABLE_HALIGN_CENTER || halign == TABLE_HALIGN_RIGHT)
             {
               shipout (&tex->token_list, "}");
@@ -562,7 +579,7 @@ tex_output_table_layer (struct tex_driver *tex, const struct pivot_table *pt,
 
   for (int i = 0; i < n_footnotes; ++i)
     {
-      char *marker = pivot_value_to_string (footnotes[i]->marker, pt);
+      char *marker = pivot_footnote_marker_string (footnotes[i], pt);
       char *content = pivot_value_to_string (footnotes[i]->content, pt);
 
       shipout (&tex->token_list, "$^{");
@@ -584,11 +601,11 @@ tex_output_table_layer (struct tex_driver *tex, const struct pivot_table *pt,
 }
 
 static void
-tex_output_table (struct tex_driver *tex, const struct table_item *item)
+tex_output_table (struct tex_driver *tex, const struct pivot_table *pt)
 {
   size_t *layer_indexes;
-  PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true)
-    tex_output_table_layer (tex, item->pt, layer_indexes);
+  PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
+    tex_output_table_layer (tex, pt, layer_indexes);
 }
 
 struct output_driver_factory tex_driver_factory =
@@ -596,8 +613,7 @@ struct output_driver_factory tex_driver_factory =
 
 static const struct output_driver_class tex_driver_class =
   {
-    "tex",
-    tex_destroy,
-    tex_submit,
-    NULL,
+    .name = "tex",
+    .destroy = tex_destroy,
+    .submit = tex_submit,
   };