1
0
Fork 0

[C] Changed cell padding setting

This commit is contained in:
seleznevae 2018-02-27 21:41:58 +03:00
parent 46758ee032
commit 441442fa9e
6 changed files with 134 additions and 209 deletions

View File

@ -267,8 +267,8 @@ struct border_chars
FORT_EXTERN int ft_set_default_borders(struct border_chars *border_chs, struct border_chars *header_border_chs);
FORT_EXTERN int ft_set_table_borders(FTABLE * FORT_RESTRICT table, struct border_chars *border_chs, struct border_chars *header_border_chs);
FORT_EXTERN int ft_set_default_option(uint32_t option, int value);
FORT_EXTERN int ft_set_table_option(FTABLE * FORT_RESTRICT table, uint32_t option, int value);
//FORT_EXTERN int ft_set_default_option(uint32_t option, int value);
//FORT_EXTERN int ft_set_table_option(FTABLE * FORT_RESTRICT table, uint32_t option, int value);
FORT_EXTERN int ft_set_cell_option(FTABLE * FORT_RESTRICT table, unsigned row, unsigned col, uint32_t option, int value);
FORT_EXTERN int ft_set_default_cell_option(uint32_t option, int value);

View File

@ -46,7 +46,9 @@ int hint_width_cell(const fort_cell_t *cell, const context_t *context)
assert(cell);
assert(context);
int result = context->table_options->cell_padding_left + context->table_options->cell_padding_right;
int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_LEFT_PADDING);
int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_RIGHT_PADDING);
int result = cell_padding_left + cell_padding_right;
if (cell->str_buffer && cell->str_buffer->str) {
result += buffer_text_width(cell->str_buffer);
}
@ -58,10 +60,13 @@ int hint_height_cell(const fort_cell_t *cell, const context_t *context)
{
assert(cell);
assert(context);
int result = context->table_options->cell_padding_top + context->table_options->cell_padding_bottom;
int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_TOP_PADDING);
int cell_padding_bottom = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_BOTTOM_PADDING);
int cell_empty_string_height = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_EMPTY_STR_HEIGHT);
int result = cell_padding_top + cell_padding_bottom;
if (cell->str_buffer && cell->str_buffer->str) {
size_t text_height = buffer_text_height(cell->str_buffer);
result += text_height == 0 ? context->table_options->cell_empty_string_height : text_height;
result += text_height == 0 ? cell_empty_string_height : text_height;
}
return result;
}
@ -94,20 +99,24 @@ int cell_printf(fort_cell_t *cell, size_t row, size_t column, char *buf, size_t
return -1;
}
int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_TOP_PADDING);
int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_LEFT_PADDING);
int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_OPT_RIGHT_PADDING);
if (row >= hint_height_cell(cell, context)
|| row < context->table_options->cell_padding_top
|| row >= (context->table_options->cell_padding_top + buffer_text_height(cell->str_buffer))) {
|| row < cell_padding_top
|| row >= (cell_padding_top + buffer_text_height(cell->str_buffer))) {
int k = snprint_n_chars(buf, buf_len, buf_len - 1, ' ');
return k;
} else {
int written = 0;
int left = context->table_options->cell_padding_left;
int right = context->table_options->cell_padding_right;
int left = cell_padding_left;
int right = cell_padding_right;
written += snprint_n_chars(buf + written, buf_len - written, left, ' ');
if (cell->str_buffer)
written += buffer_printf(cell->str_buffer, row - context->table_options->cell_padding_top, column, buf + written, buf_len - written - right, context);
written += buffer_printf(cell->str_buffer, row - cell_padding_top, column, buf + written, buf_len - written - right, context);
else
written += snprint_n_chars(buf + written, buf_len - written, buf_len - written - right, ' ');
written += snprint_n_chars(buf + written, buf_len - written, right, ' ');

View File

@ -575,30 +575,30 @@ int ft_add_separator(FTABLE *table)
return F_SUCCESS;
}
int ft_set_default_option(uint32_t option, int value)
{
switch (option) {
case FT_OPT_TOP_PADDING:
g_table_options.cell_padding_top = value;
break;
case FT_OPT_BOTTOM_PADDING:
g_table_options.cell_padding_bottom = value;
break;
case FT_OPT_LEFT_PADDING:
g_table_options.cell_padding_left = value;
break;
case FT_OPT_RIGHT_PADDING:
g_table_options.cell_padding_right = value;
break;
case FT_OPT_EMPTY_STR_HEIGHT:
g_table_options.cell_empty_string_height = value;
break;
default:
// todo
exit(22);
}
return F_SUCCESS;
}
//int ft_set_default_option(uint32_t option, int value)
//{
// switch (option) {
// case FT_OPT_TOP_PADDING:
// g_table_options.cell_padding_top = value;
// break;
// case FT_OPT_BOTTOM_PADDING:
// g_table_options.cell_padding_bottom = value;
// break;
// case FT_OPT_LEFT_PADDING:
// g_table_options.cell_padding_left = value;
// break;
// case FT_OPT_RIGHT_PADDING:
// g_table_options.cell_padding_right = value;
// break;
// case FT_OPT_EMPTY_STR_HEIGHT:
// g_table_options.cell_empty_string_height = value;
// break;
// default:
// // todo
// exit(22);
// }
// return F_SUCCESS;
//}
static void set_border_options_for_options(fort_table_options_t *options, struct border_chars *border_chs, struct border_chars *header_border_chs)
@ -667,37 +667,37 @@ int ft_set_table_borders(FTABLE *table, struct border_chars *border_chs, struct
return F_SUCCESS;
}
int ft_set_table_option(FTABLE *table, uint32_t option, int value)
{
assert(table);
if (table->options == NULL) {
table->options = create_table_options();
if (table->options == NULL)
return F_MEMORY_ERROR;
}
switch (option) {
case FT_OPT_TOP_PADDING:
table->options->cell_padding_top = value;
break;
case FT_OPT_BOTTOM_PADDING:
table->options->cell_padding_bottom = value;
break;
case FT_OPT_LEFT_PADDING:
table->options->cell_padding_left = value;
break;
case FT_OPT_RIGHT_PADDING:
table->options->cell_padding_right = value;
break;
case FT_OPT_EMPTY_STR_HEIGHT:
table->options->cell_empty_string_height = value;
break;
default:
// todo
exit(22);
}
return F_SUCCESS;
//int ft_set_table_option(FTABLE *table, uint32_t option, int value)
//{
// assert(table);
// if (table->options == NULL) {
// table->options = create_table_options();
// if (table->options == NULL)
// return F_MEMORY_ERROR;
// }
// switch (option) {
// case FT_OPT_TOP_PADDING:
// table->options->cell_padding_top = value;
// break;
// case FT_OPT_BOTTOM_PADDING:
// table->options->cell_padding_bottom = value;
// break;
// case FT_OPT_LEFT_PADDING:
// table->options->cell_padding_left = value;
// break;
// case FT_OPT_RIGHT_PADDING:
// table->options->cell_padding_right = value;
// break;
// case FT_OPT_EMPTY_STR_HEIGHT:
// table->options->cell_empty_string_height = value;
// break;
// default:
// // todo
// exit(22);
// }
// return F_SUCCESS;
}
//}

View File

@ -226,11 +226,11 @@ fort_status_t set_default_cell_option(uint32_t option, int value)
fort_table_options_t g_table_options = {
0, /* cell_padding_top */
0, /* cell_padding_bottom */
1, /* cell_padding_left */
1, /* cell_padding_right */
1, /* cell_empty_string_height */
// 0, /* cell_padding_top */
// 0, /* cell_padding_bottom */
// 1, /* cell_padding_left */
// 1, /* cell_padding_right */
// 1, /* cell_empty_string_height */
/* border_chars */
{

View File

@ -133,11 +133,11 @@ enum SeparatorItemPos
struct fort_table_options
{
int cell_padding_top;
int cell_padding_bottom;
int cell_padding_left;
int cell_padding_right;
int cell_empty_string_height;
// int cell_padding_top;
// int cell_padding_bottom;
// int cell_padding_left;
// int cell_padding_right;
// int cell_empty_string_height;
char border_chars[BorderItemPosSize];
char header_border_chars[BorderItemPosSize];

View File

@ -45,11 +45,12 @@ int set_test_options_for_table(FTABLE *table)
{
assert(table);
int status = F_SUCCESS;
status |= ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_TOP_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_LEFT_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_EMPTY_STR_HEIGHT, 1);
status |= ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_TOP_PADDING, 1);
status |= ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_LEFT_PADDING, 1);
status |= ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_EMPTY_STR_HEIGHT, 1);
assert_true( status == F_SUCCESS );
@ -78,11 +79,12 @@ int set_test_options_as_default()
status |= ft_set_default_cell_option(FT_OPT_MIN_WIDTH, 0);
status |= ft_set_default_cell_option(FT_OPT_TEXT_ALIGN, RightAligned);
status |= ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_default_option(FT_OPT_TOP_PADDING, 1);
status |= ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
status |= ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 1);
status |= ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_default_cell_option(FT_OPT_TOP_PADDING, 1);
status |= ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 1);
status |= ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_default_cell_option(FT_OPT_EMPTY_STR_HEIGHT, 1);
assert_true( status == F_SUCCESS );
@ -391,19 +393,12 @@ void test_table_options(void **state)
WHEN("All paddings = 1") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// ft_set_default_options(&table_options);
set_test_options_as_default();
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 1);
table = create_test_int_table(0);
@ -465,17 +460,11 @@ void test_table_options(void **state)
}
WHEN("Top and bottom padding = 0") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 1);
table = create_test_int_table(0);
@ -497,17 +486,11 @@ void test_table_options(void **state)
}
WHEN("Left and right padding = 0") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 0);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 0);
table = create_test_int_table(0);
@ -535,17 +518,11 @@ void test_table_options(void **state)
}
WHEN("All paddings = 0") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 0);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 0);
table = create_test_int_table(0);
@ -567,19 +544,12 @@ void test_table_options(void **state)
}
WHEN("Empty string has 0 heigt") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// table_options.cell_empty_string_height = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
table = create_test_int_table(0);
int n = ft_printf_ln(table, "|||");
@ -612,27 +582,7 @@ void test_table_options(void **state)
}
WHEN("Changing cell separators") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
//#define BOR_CHARS table_options.border_chars
//#define H_BOR_CHARS table_options.header_border_chars
// BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
// BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '|';
// BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
// BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
// H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
// H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
// H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
// H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
//#undef BOR_CHARS
//#undef H_BOR_CHARS
// ft_set_default_options(&table_options);
struct border_chars border_chs;
border_chs.top_border_ch = '|';
border_chs.separator_ch = '|';
@ -677,29 +627,7 @@ void test_table_options(void **state)
//#define BOR_CHARS table_options.border_chars
//#define H_BOR_CHARS table_options.header_border_chars
// BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
// BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '\0';
// BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
// BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
// H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
// H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
// H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
// H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
//#undef BOR_CHARS
//#undef H_BOR_CHARS
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// table_options.cell_empty_string_height = 0;
// ft_set_default_options(&table_options);
border_chs.top_border_ch = '|';
border_chs.separator_ch = '\0';
@ -717,11 +645,11 @@ void test_table_options(void **state)
ft_set_default_borders(&border_chs, &header_border_chs);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_cell_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_cell_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_cell_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
table = create_test_int_table(0);
@ -742,21 +670,14 @@ void test_table_options(void **state)
}
WHEN("Setting options for a particular table") {
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
table = create_test_int_table(0);
set_test_options_for_table(table);
ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 0);
ft_set_table_option(table, FT_OPT_TOP_PADDING, 0);
ft_set_table_option(table, FT_OPT_LEFT_PADDING, 0);
ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_BOTTOM_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_TOP_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_LEFT_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_RIGHT_PADDING, 0);
const char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
@ -772,17 +693,12 @@ void test_table_options(void **state)
assert_true( strcmp(table_str, table_str_etalon) == 0);
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_table_options(table, &table_options);
ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 1);
ft_set_table_option(table, FT_OPT_TOP_PADDING, 1);
ft_set_table_option(table, FT_OPT_LEFT_PADDING, 0);
ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 0);
ft_set_table_option(table, FT_OPT_EMPTY_STR_HEIGHT, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_BOTTOM_PADDING, 1);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_TOP_PADDING, 1);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_LEFT_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_RIGHT_PADDING, 0);
ft_set_cell_option(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_OPT_EMPTY_STR_HEIGHT, 0);
table_str = ft_to_string(table);
assert_true( table_str != NULL );