output: Reimplement table_from_string in terms of tab.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 9 Feb 2019 22:58:37 +0000 (14:58 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 9 Feb 2019 23:16:09 +0000 (15:16 -0800)
With this change, there is only a single table_class in the tree.

src/output/ascii.c
src/output/cairo.c
src/output/table.c
src/output/table.h

index 2193f35224ee19ec0f4fca870797019b29a7a6f8..6f8644ba3f299b117bbf844783bc49d757d85f88 100644 (file)
@@ -461,8 +461,7 @@ static void
 ascii_output_text (struct ascii_driver *a, const char *text)
 {
   ascii_output_table_item_unref (
-    a, table_item_create (table_from_string (TABLE_HALIGN_LEFT, text),
-                          NULL, NULL));
+    a, table_item_create (table_from_string (text), NULL, NULL));
 }
 
 static void
index 56919b8d77970a4b2550c4d8b9a084edf7a92728..4794fe25e1ec8b0a8264e3ad795b9bb2b6d4c7fb 100644 (file)
@@ -1585,8 +1585,7 @@ 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 (TABLE_HALIGN_LEFT, text),
-                                  NULL, NULL);
+  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);
 
index 3690df3bd6c48593ee6832fabdf6a3723cb17a26..8f717ead5679359b4f5c8529811e7b731d9c307d 100644 (file)
@@ -30,6 +30,7 @@
 #include "libpspp/pool.h"
 #include "libpspp/str.h"
 #include "output/table-item.h"
+#include "output/tab.h"
 
 #include "gl/xalloc.h"
 
@@ -297,82 +298,15 @@ table_collect_footnotes (const struct table_item *item,
   return n;
 }
 \f
-struct table_string
-  {
-    struct table table;
-    char *string;
-    enum table_halign halign;
-  };
-
-static const struct table_class table_string_class;
-
-/* Returns a table that contains a single cell, whose contents are S with
-   options OPTIONS (a combination of TAB_* values).  */
+/* Returns a table that contains a single cell, whose contents are the
+   left-aligned TEXT.  */
 struct table *
-table_from_string (enum table_halign halign, const char *s)
-{
-  struct table_string *ts = xmalloc (sizeof *ts);
-  table_init (&ts->table, &table_string_class);
-  ts->table.n[TABLE_HORZ] = ts->table.n[TABLE_VERT] = 1;
-  ts->string = xstrdup (s);
-  ts->halign = halign;
-  return &ts->table;
-}
-
-static struct table_string *
-table_string_cast (const struct table *table)
-{
-  assert (table->klass == &table_string_class);
-  return UP_CAST (table, struct table_string, table);
-}
-
-static void
-table_string_destroy (struct table *ts_)
+table_from_string (const char *text)
 {
-  struct table_string *ts = table_string_cast (ts_);
-  free (ts->string);
-  free (ts);
+  struct tab_table *t = tab_create (1, 1);
+  tab_text (t, 0, 0, TAB_LEFT, text);
+  return &t->table;
 }
-
-static void
-table_string_get_cell (const struct table *ts_, int x UNUSED, int y UNUSED,
-                       struct table_cell *cell)
-{
-  static const struct area_style styles[] = {
-#define S(H) [H] = { AREA_STYLE_INITIALIZER__, .cell_style.halign = H }
-    S(TABLE_HALIGN_LEFT),
-    S(TABLE_HALIGN_CENTER),
-    S(TABLE_HALIGN_RIGHT),
-    S(TABLE_HALIGN_MIXED),
-    S(TABLE_HALIGN_DECIMAL),
-  };
-  struct table_string *ts = table_string_cast (ts_);
-  cell->d[TABLE_HORZ][0] = 0;
-  cell->d[TABLE_HORZ][1] = 1;
-  cell->d[TABLE_VERT][0] = 0;
-  cell->d[TABLE_VERT][1] = 1;
-  cell->options = 0;
-  cell->style = &styles[table_halign_interpret (ts->halign, false)];
-  cell->text = ts->string;
-  cell->n_footnotes = 0;
-  cell->destructor = NULL;
-}
-
-
-static int
-table_string_get_rule (const struct table *ts UNUSED,
-                       enum table_axis axis UNUSED, int x UNUSED, int y UNUSED,
-                       struct cell_color *color UNUSED)
-{
-  return TAL_0;
-}
-
-static const struct table_class table_string_class =
-  {
-    table_string_destroy,
-    table_string_get_cell,
-    table_string_get_rule,
-  };
 \f
 const char *
 table_halign_to_string (enum table_halign halign)
index 040e89dcc71904b07f17e8a03b6f00f1f8dc61be..5331aa34388d97f0d534ddf6bf8fc3a462db30d5 100644 (file)
@@ -266,6 +266,6 @@ void table_set_hb (struct table *, int hb);
 /* Table classes. */
 
 /* Simple kinds of tables. */
-struct table *table_from_string (enum table_halign, const char *);
+struct table *table_from_string (const char *);
 
 #endif /* output/table.h */