From 0394b56ed72885ec7a9dec776c1c676768e7c01a Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 29 Dec 2020 09:54:50 -0800 Subject: [PATCH] text-item: Use struct font_style instead of individual fields. The foreground and background colors can't be represented in SPV files, but it seems like an improvement nonetheless. This also eliminates the last use of TAB_FIX in the tree. An upcoming commit will remove TAB_FIX entirely. --- src/output/csv.c | 2 +- src/output/spv/spv-output.c | 13 ++++------- src/output/table.c | 16 ++++++++++++++ src/output/table.h | 1 + src/output/text-item.c | 44 +++++++++++++++---------------------- src/output/text-item.h | 6 ++--- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/output/csv.c b/src/output/csv.c index dd1b953b5a..bde8711314 100644 --- a/src/output/csv.c +++ b/src/output/csv.c @@ -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); diff --git a/src/output/spv/spv-output.c b/src/output/spv/spv-output.c index cb4964a7f1..00b3dead47 100644 --- a/src/output/spv/spv-output.c +++ b/src/output/spv/spv-output.c @@ -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); } diff --git a/src/output/table.c b/src/output/table.c index a8b7704fd2..44c33b9d10 100644 --- a/src/output/table.c +++ b/src/output/table.c @@ -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) { diff --git a/src/output/table.h b/src/output/table.h index a212aa4be2..5c47ffe88c 100644 --- a/src/output/table.h +++ b/src/output/table.h @@ -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 { diff --git a/src/output/text-item.c b/src/output/text-item.c index 2f0cef5ee7..34f1886d85 100644 --- a/src/output/text-item.c +++ b/src/output/text-item.c @@ -68,7 +68,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 +126,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 +146,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 +164,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 +194,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); } diff --git a/src/output/text-item.h b/src/output/text-item.h index 28e2457a2c..8b4114e006 100644 --- a/src/output/text-item.h +++ b/src/output/text-item.h @@ -27,6 +27,7 @@ #include #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, -- 2.30.2