pivot-table: Incorporate format settings.
[pspp] / src / output / text-item.c
index 2f0cef5ee7f6cbfb7cc8989a50b66ccf4b9bcfe4..49c9f225c06a21a6b95557ddf7ce7bc2a0caff96 100644 (file)
@@ -25,6 +25,7 @@
 #include "libpspp/pool.h"
 #include "output/driver.h"
 #include "output/output-item-provider.h"
+#include "output/pivot-table.h"
 #include "output/table.h"
 #include "output/table-item.h"
 #include "output/table-provider.h"
@@ -68,7 +69,15 @@ text_item_create_nocopy (enum text_item_type type, char *text, char *label)
     .output_item.label = label,
     .text = text,
     .type = type,
+    .style = FONT_STYLE_INITIALIZER,
   };
+
+  if (type == TEXT_ITEM_SYNTAX || type == TEXT_ITEM_LOG)
+    {
+      free (item->style.typeface);
+      item->style.typeface = xstrdup ("Monospaced");
+    }
+
   return item;
 }
 
@@ -118,13 +127,8 @@ text_item_unshare (struct text_item *old)
     .output_item = OUTPUT_ITEM_CLONE_INITIALIZER (&old->output_item),
     .text = xstrdup (old->text),
     .type = old->type,
-    .bold = old->bold,
-    .italic = old->italic,
-    .underline = old->underline,
-    .markup = old->markup,
-    .typeface = old->typeface ? xstrdup (old->typeface) : NULL,
-    .size = old->size
   };
+  font_style_copy (NULL, &new->style, &old->style);
   return new;
 }
 
@@ -143,14 +147,8 @@ text_item_append (struct text_item *dst, const struct text_item *src)
       || (dst->type != TEXT_ITEM_SYNTAX && dst->type != TEXT_ITEM_LOG)
       || strcmp (output_item_get_label (&dst->output_item),
                  output_item_get_label (&src->output_item))
-      || dst->bold != src->bold
-      || dst->italic != src->italic
-      || dst->underline != src->underline
-      || dst->markup
-      || src->markup
-      || strcmp (dst->typeface ? dst->typeface : "",
-                 src->typeface ? src->typeface : "")
-      || dst->size != src->size)
+      || !font_style_equal (&dst->style, &src->style)
+      || dst->style.markup)
     return false;
   else
     {
@@ -161,33 +159,40 @@ text_item_append (struct text_item *dst, const struct text_item *src)
     }
 }
 
+static const struct pivot_table_look *
+text_item_table_look (void)
+{
+  static struct pivot_table_look *look;
+  if (!look)
+    {
+      look = pivot_table_look_new_builtin_default ();
+
+      for (int a = 0; a < PIVOT_N_AREAS; a++)
+        memset (look->areas[a].cell_style.margin, 0,
+                sizeof look->areas[a].cell_style.margin);
+      for (int b = 0; b < PIVOT_N_BORDERS; b++)
+        look->borders[b].stroke = TABLE_STROKE_NONE;
+    }
+  return look;
+}
+
 struct table_item *
 text_item_to_table_item (struct text_item *text_item)
 {
-  struct table *tab = table_create (1, 1, 0, 0, 0, 0);
-
-  struct table_area_style *style = pool_alloc (tab->container, sizeof *style);
-  *style = (struct table_area_style) { TABLE_AREA_STYLE_INITIALIZER__,
-                                       .cell_style.halign = TABLE_HALIGN_LEFT };
-  struct font_style *font_style = &style->font_style;
-  if (text_item->typeface)
-    font_style->typeface = pool_strdup (tab->container, text_item->typeface);
-  font_style->size = text_item->size;
-  font_style->bold = text_item->bold;
-  font_style->italic = text_item->italic;
-  font_style->underline = text_item->underline;
-  font_style->markup = text_item->markup;
-  tab->styles[0] = style;
-
-  int opts = 0;
-  if (text_item->markup)
-    opts |= TAB_MARKUP;
-  if (text_item->type == TEXT_ITEM_SYNTAX || text_item->type == TEXT_ITEM_LOG)
-    opts |= TAB_FIX;
-  table_text (tab, 0, 0, opts, text_item_get_text (text_item));
-  struct table_item *table_item = table_item_create (tab);
-  text_item_unref (text_item);
-  return table_item;
+  struct pivot_table *table = pivot_table_create__ (NULL, "Text");
+  pivot_table_set_look (table, text_item_table_look ());
+
+  struct pivot_dimension *d = pivot_dimension_create (
+    table, PIVOT_AXIS_ROW, N_("Text"));
+  d->hide_all_labels = true;
+  pivot_category_create_leaf (d->root, pivot_value_new_text ("null"));
+
+  struct pivot_value *content = pivot_value_new_user_text (
+    text_item->text, SIZE_MAX);
+  pivot_value_set_font_style (content, &text_item->style);
+  pivot_table_put1 (table, 0, content);
+
+  return table_item_create (table);
 }
 \f
 static const char *
@@ -202,7 +207,7 @@ text_item_destroy (struct output_item *output_item)
 {
   struct text_item *item = to_text_item (output_item);
   free (item->text);
-  free (item->typeface);
+  font_style_uninit (&item->style);
   free (item);
 }