From fe6dbfce4a0fa8e11c84c96672508788997feb13 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Fri, 16 Nov 2018 23:20:57 +0300 Subject: [PATCH] [A] Added multiple text styles --- lib/fort.c | 55 +++++++++++++---- lib/fort.h | 22 +++---- src/properties.c | 46 +++++++++++---- src/properties.h | 4 +- tests/bb_tests/test_table_properties.c | 81 ++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 35 deletions(-) diff --git a/lib/fort.c b/lib/fort.c index 89f3932..c5f66e4 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -418,8 +418,8 @@ struct fort_cell_props { unsigned int content_fg_color_number; unsigned int content_bg_color_number; unsigned int cell_bg_color_number; - unsigned int cell_text_style; - unsigned int content_text_style; + enum ft_text_style cell_text_style; + enum ft_text_style content_text_style; }; typedef struct fort_cell_props fort_cell_props_t; @@ -920,8 +920,12 @@ void get_style_tag_for_cell(const fort_table_properties_t *props, style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(style_tag, text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(style_tag, text_styles[i]); + } + } } else { goto error; } @@ -951,8 +955,12 @@ void get_reset_style_tag_for_cell(const fort_table_properties_t *props, reset_style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(reset_style_tag, reset_text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(reset_style_tag, reset_text_styles[i]); + } + } } else { goto error; } @@ -984,8 +992,12 @@ void get_style_tag_for_content(const fort_table_properties_t *props, style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(style_tag, text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(style_tag, text_styles[i]); + } + } } else { goto error; } @@ -1022,8 +1034,12 @@ void get_reset_style_tag_for_content(const fort_table_properties_t *props, reset_style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(reset_style_tag, reset_text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(reset_style_tag, reset_text_styles[i]); + } + } } else { goto error; } @@ -1256,9 +1272,24 @@ static fort_status_t set_cell_property_impl(fort_cell_props_t *opt, uint32_t pro } else if (PROP_IS_SET(property, FT_CPROP_CELL_BG_COLOR)) { opt->cell_bg_color_number = value; } else if (PROP_IS_SET(property, FT_CPROP_CELL_TEXT_STYLE)) { - opt->cell_text_style = value; +// opt->cell_text_style = value; + enum ft_text_style v = (enum ft_text_style)value; + if (v == FT_TSTYLE_DEFAULT) { + opt->cell_text_style = FT_TSTYLE_DEFAULT; + } else { +// opt->cell_text_style &= ~((unsigned)FT_TSTYLE_DEFAULT); + opt->cell_text_style |= v; + } } else if (PROP_IS_SET(property, FT_CPROP_CONT_TEXT_STYLE)) { - opt->content_text_style = value; +// opt->content_text_style = value; + + enum ft_text_style v = (enum ft_text_style)value; + if (v == FT_TSTYLE_DEFAULT) { + opt->content_text_style = v; + } else { + // opt->cell_text_style &= ~((unsigned)FT_TSTYLE_DEFAULT); + opt->content_text_style |= v; + } } return FT_SUCCESS; diff --git a/lib/fort.h b/lib/fort.h index f70ff9e..28bb74a 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -716,18 +716,18 @@ int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style); /** @} */ /** - * @name Text styles. - * @{ + * Text styles. */ -#define FT_TSTYLE_DEFAULT 0 -#define FT_TSTYLE_BOLD 1 -#define FT_TSTYLE_DIM 2 -#define FT_TSTYLE_ITALIC 3 -#define FT_TSTYLE_UNDERLINED 4 -#define FT_TSTYLE_BLINK 5 -#define FT_TSTYLE_INVERTED 6 -#define FT_TSTYLE_HIDDEN 7 -/** @} */ +enum ft_text_style { + FT_TSTYLE_DEFAULT = (1U << 0), + FT_TSTYLE_BOLD = (1U << 1), + FT_TSTYLE_DIM = (1U << 2), + FT_TSTYLE_ITALIC = (1U << 3), + FT_TSTYLE_UNDERLINED = (1U << 4), + FT_TSTYLE_BLINK = (1U << 5), + FT_TSTYLE_INVERTED = (1U << 6), + FT_TSTYLE_HIDDEN = (1U << 7) +}; /** diff --git a/src/properties.c b/src/properties.c index 26d4e7c..934b899 100644 --- a/src/properties.c +++ b/src/properties.c @@ -123,8 +123,12 @@ void get_style_tag_for_cell(const fort_table_properties_t *props, style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(style_tag, text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(style_tag, text_styles[i]); + } + } } else { goto error; } @@ -154,8 +158,12 @@ void get_reset_style_tag_for_cell(const fort_table_properties_t *props, reset_style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(reset_style_tag, reset_text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(reset_style_tag, reset_text_styles[i]); + } + } } else { goto error; } @@ -187,8 +195,12 @@ void get_style_tag_for_content(const fort_table_properties_t *props, style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(style_tag, text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(style_tag, text_styles[i]); + } + } } else { goto error; } @@ -225,8 +237,12 @@ void get_reset_style_tag_for_content(const fort_table_properties_t *props, reset_style_tag[0] = '\0'; - if (text_style < n_styles) { - strcat(reset_style_tag, reset_text_styles[text_style]); + if (text_style < (1U << n_styles)) { + for (size_t i = 0; i < n_styles; ++i) { + if (text_style & (1 << i)) { + strcat(reset_style_tag, reset_text_styles[i]); + } + } } else { goto error; } @@ -459,9 +475,19 @@ static fort_status_t set_cell_property_impl(fort_cell_props_t *opt, uint32_t pro } else if (PROP_IS_SET(property, FT_CPROP_CELL_BG_COLOR)) { opt->cell_bg_color_number = value; } else if (PROP_IS_SET(property, FT_CPROP_CELL_TEXT_STYLE)) { - opt->cell_text_style = value; + enum ft_text_style v = (enum ft_text_style)value; + if (v == FT_TSTYLE_DEFAULT) { + opt->cell_text_style = FT_TSTYLE_DEFAULT; + } else { + opt->cell_text_style |= v; + } } else if (PROP_IS_SET(property, FT_CPROP_CONT_TEXT_STYLE)) { - opt->content_text_style = value; + enum ft_text_style v = (enum ft_text_style)value; + if (v == FT_TSTYLE_DEFAULT) { + opt->content_text_style = v; + } else { + opt->content_text_style |= v; + } } return FT_SUCCESS; diff --git a/src/properties.h b/src/properties.h index dba27d7..a20bf10 100644 --- a/src/properties.h +++ b/src/properties.h @@ -43,8 +43,8 @@ struct fort_cell_props { unsigned int content_fg_color_number; unsigned int content_bg_color_number; unsigned int cell_bg_color_number; - unsigned int cell_text_style; - unsigned int content_text_style; + enum ft_text_style cell_text_style; + enum ft_text_style content_text_style; }; typedef struct fort_cell_props fort_cell_props_t; diff --git a/tests/bb_tests/test_table_properties.c b/tests/bb_tests/test_table_properties.c index 41c4bcf..19a5dd2 100644 --- a/tests/bb_tests/test_table_properties.c +++ b/tests/bb_tests/test_table_properties.c @@ -825,6 +825,46 @@ void test_table_text_styles(void) } #endif + WHEN("Simple table with one cell and multiple content style") { + table = ft_create_table(); + assert(table); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD) == FT_SUCCESS); + assert(ft_write(table, "42") == FT_SUCCESS); + + const char *table_str = ft_to_string(table); + assert_true(table_str != NULL); + const char *table_str_etalon = + "+----+\n" + "|\033[1m\033[4m\033[21m\033[24m |\n" + "| \033[1m\033[4m42\033[21m\033[24m |\n" + "|\033[1m\033[4m\033[21m\033[24m |\n" + "+----+\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } + +#ifdef FT_HAVE_WCHAR + WHEN("Simple table with one cell and multiple content style(wide strings case)") { + table = ft_create_table(); + assert(table); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD) == FT_SUCCESS); + assert(ft_wwrite(table, L"42") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true(table_str != NULL); + const wchar_t *table_str_etalon = + L"+----+\n" + L"|\033[1m\033[4m\033[21m\033[24m |\n" + L"| \033[1m\033[4m42\033[21m\033[24m |\n" + L"|\033[1m\033[4m\033[21m\033[24m |\n" + L"+----+\n"; + assert_wcs_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } +#endif + WHEN("Simple table with one cell and cell style") { table = ft_create_table(); assert(table); @@ -863,6 +903,47 @@ void test_table_text_styles(void) } #endif + WHEN("Simple table with one cell and multiple cell style") { + table = ft_create_table(); + assert(table); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_BOLD) == FT_SUCCESS); + assert(ft_write(table, "42") == FT_SUCCESS); + + const char *table_str = ft_to_string(table); + assert_true(table_str != NULL); + const char *table_str_etalon = + "+----+\n" + "|\033[1m\033[4m \033[21m\033[24m|\n" + "|\033[1m\033[4m 42 \033[21m\033[24m|\n" + "|\033[1m\033[4m \033[21m\033[24m|\n" + "+----+\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } + +#ifdef FT_HAVE_WCHAR + WHEN("Simple table with one cell and multiple cell style(wide strings case)") { + table = ft_create_table(); + assert(table); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED) == FT_SUCCESS); + assert(ft_set_cell_prop(table, 0, 0, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_BOLD) == FT_SUCCESS); + assert(ft_wwrite(table, L"42") == FT_SUCCESS); + + const wchar_t *table_str = ft_to_wstring(table); + assert_true(table_str != NULL); + const wchar_t *table_str_etalon = + L"+----+\n" + L"|\033[1m\033[4m \033[21m\033[24m|\n" + L"|\033[1m\033[4m 42 \033[21m\033[24m|\n" + L"|\033[1m\033[4m \033[21m\033[24m|\n" + L"+----+\n"; + assert_wcs_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } +#endif + + WHEN("Simple table with one cell background color, content foreground color and style.") { set_test_properties_as_default();