text-item: Use struct font_style instead of individual fields.
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 29 Dec 2020 17:54:50 +0000 (09:54 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 31 Dec 2020 03:58:11 +0000 (19:58 -0800)
The foreground and background colors can't be represented in SPV files,
but it seems like an improvement nonetheless.

src/output/csv.c
src/output/spv/spv-output.c
src/output/table.c
src/output/table.h
src/output/text-item.c
src/output/text-item.h

index dd1b953b5a34242f43b02c7d05e0b28f1b17a813..bde8711314f7384c44906c7cc4f05cb0fa4ea218 100644 (file)
@@ -288,7 +288,7 @@ csv_submit (struct output_driver *driver,
 
       csv_put_separator (csv);
 
-      if (text_item->markup)
+      if (text_item->style.markup)
         {
           char *plain_text = output_get_text_from_markup (text);
           csv_output_lines (csv, plain_text);
index cb4964a7f1d7bdf9d4c33b6fd661e366769684dd..00b3dead4729416faa1ca19cb28a63314a850ced 100644 (file)
@@ -37,16 +37,11 @@ spv_text_submit (const struct spv_item *in)
                                       SETTINGS_VALUE_SHOW_DEFAULT);
   char *label = in->label ? xstrdup (in->label) : NULL;
   struct text_item *item = text_item_create_nocopy (type, text, label);
-  const struct font_style *font = value->font_style;
-  if (font)
+
+  if (value->font_style)
     {
-      item->bold = font->bold;
-      item->italic = font->italic;
-      item->underline = font->underline;
-      item->markup = font->markup;
-      if (font->typeface)
-        item->typeface = xstrdup (font->typeface);
-      item->size = font->size;
+      font_style_uninit (&item->style);
+      font_style_copy (NULL, &item->style, value->font_style);
     }
   text_item_submit (item);
 }
index 90e9491a39ed71e572e2020130e1b95e1843f39a..bd017f6b4f16ac518ad7ba826234c72b2ea2668c 100644 (file)
@@ -381,6 +381,22 @@ font_style_dump (const struct font_style *f)
     fputs (" underline", stdout);
 }
 
+bool
+font_style_equal (const struct font_style *a, const struct font_style *b)
+{
+  return (a->bold == b->bold
+          && a->italic == b->italic
+          && a->underline == b->underline
+          && a->markup == b->markup
+          && cell_color_equal (&a->fg[0], &b->fg[0])
+          && cell_color_equal (&a->fg[1], &b->fg[1])
+          && cell_color_equal (&a->bg[0], &b->bg[0])
+          && cell_color_equal (&a->bg[1], &b->bg[1])
+          && !strcmp (a->typeface ? a->typeface : "",
+                      b->typeface ? b->typeface : "")
+          && a->size == b->size);
+}
+
 void
 cell_style_dump (const struct cell_style *c)
 {
index ec4335a5d4f0f478e71ff7b135ba02726921593b..f43bd81fa217a1641cc1f23752ae1ac73439aeef 100644 (file)
@@ -156,6 +156,7 @@ void font_style_copy (struct pool *,
                       struct font_style *, const struct font_style *);
 void font_style_uninit (struct font_style *);
 void font_style_dump (const struct font_style *);
+bool font_style_equal (const struct font_style *, const struct font_style *);
 
 struct table_area_style
   {
index 2f0cef5ee7f6cbfb7cc8989a50b66ccf4b9bcfe4..9dad57dee8ca6a45c9b3d2b8ecaf71670e843e3f 100644 (file)
@@ -68,6 +68,7 @@ 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,
   };
   return item;
 }
@@ -118,13 +119,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 +139,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
     {
@@ -167,20 +157,15 @@ 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;
+  *style = (struct table_area_style) {
+    .cell_style = CELL_STYLE_INITIALIZER,
+    .cell_style.halign = TABLE_HALIGN_LEFT,
+  };
+  font_style_copy (tab->container, &style->font_style, &text_item->style);
   tab->styles[0] = style;
 
   int opts = 0;
-  if (text_item->markup)
+  if (text_item->style.markup)
     opts |= TAB_MARKUP;
   if (text_item->type == TEXT_ITEM_SYNTAX || text_item->type == TEXT_ITEM_LOG)
     opts |= TAB_FIX;
@@ -202,7 +187,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);
 }
 
index 28e2457a2c9f8c76491b5243ee259108aeb745c2..8b4114e0061dc1b4960c04dcab43735d121cb552 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdbool.h>
 #include "libpspp/compiler.h"
 #include "output/output-item.h"
+#include "output/table.h"
 
 enum text_item_type
   {
@@ -44,10 +45,7 @@ struct text_item
     struct output_item output_item;
     char *text;                 /* The content. */
     enum text_item_type type;   /* Type. */
-
-    bool bold, italic, underline, markup;
-    char *typeface;
-    int size;
+    struct font_style style;
   };
 
 struct text_item *text_item_create (enum text_item_type,