libfort/src/cell.c

190 lines
6.8 KiB
C
Raw Normal View History

2018-01-17 19:22:57 +01:00
#include "cell.h"
#include "options.h"
#include "string_buffer.h"
#include "assert.h"
/*****************************************************************************
* CELL
* ***************************************************************************/
2018-01-17 19:22:57 +01:00
struct fort_cell
{
string_buffer_t *str_buffer;
fort_table_options_t *options;
};
fort_cell_t * create_cell()
{
fort_cell_t *cell = (fort_cell_t *)F_CALLOC(sizeof(fort_cell_t), 1);
2018-01-17 19:22:57 +01:00
if (cell == NULL)
return NULL;
2018-03-05 19:08:14 +01:00
cell->str_buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, CharBuf);
2018-01-17 19:22:57 +01:00
if (cell->str_buffer == NULL) {
F_FREE(cell);
return NULL;
}
cell->options = NULL;
2018-03-09 10:44:16 +01:00
/* init_cell_options(&(cell->options)); */
2018-01-17 19:22:57 +01:00
return cell;
}
void destroy_cell(fort_cell_t *cell)
{
if (cell == NULL)
return;
destroy_string_buffer(cell->str_buffer);
F_FREE(cell->options);
F_FREE(cell);
}
int hint_width_cell(const fort_cell_t *cell, const context_t *context)
{
/* todo:
* At the moment min width includes paddings. Maybe it is better that min width weren't include
* paddings but be min width of the cell content without padding
*/
2018-01-17 19:22:57 +01:00
assert(cell);
assert(context);
2018-03-25 10:11:08 +02:00
int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING);
int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING);
2018-02-27 19:41:58 +01:00
int result = cell_padding_left + cell_padding_right;
if (cell->str_buffer && cell->str_buffer->str.data) {
2018-01-17 19:22:57 +01:00
result += buffer_text_width(cell->str_buffer);
}
2018-03-25 10:11:08 +02:00
result = MAX(result, get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_MIN_WIDTH));
2018-01-17 19:22:57 +01:00
return result;
}
int hint_height_cell(const fort_cell_t *cell, const context_t *context)
{
assert(cell);
assert(context);
2018-03-25 10:11:08 +02:00
int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING);
int cell_padding_bottom = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_BOTTOM_PADDING);
int cell_empty_string_height = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_EMPTY_STR_HEIGHT);
2018-02-27 19:41:58 +01:00
int result = cell_padding_top + cell_padding_bottom;
if (cell->str_buffer && cell->str_buffer->str.data) {
2018-01-17 19:22:57 +01:00
size_t text_height = buffer_text_height(cell->str_buffer);
2018-02-27 19:41:58 +01:00
result += text_height == 0 ? cell_empty_string_height : text_height;
2018-01-17 19:22:57 +01:00
}
return result;
}
/*
* Returns number of lines in cell. If cell is empty or
* contains empty string, then 0 is returned.
*/
2018-03-09 10:44:16 +01:00
/*
static int lines_number_cell(fort_cell_t *cell)
{
assert(cell);
if (cell->str_buffer == NULL || cell->str_buffer->str == NULL || cell->str_buffer->str[0] == '\0') {
return 0;
}
2018-01-17 19:22:57 +01:00
2018-03-09 10:44:16 +01:00
int result = 0;
char *pos = cell->str_buffer->str;
while ((pos = strchr(pos, '\n')) != NULL) {
result++;
pos++;
}
return result + 1;
}
*/
2018-01-17 19:22:57 +01:00
int cell_printf(fort_cell_t *cell, size_t row, size_t column, char *buf, size_t buf_len, const context_t *context)
{
2018-03-05 19:08:14 +01:00
char space_char = ' ';
int (*buffer_printf_)(string_buffer_t *, size_t , size_t , char *, size_t , const context_t *) = buffer_printf;
int (*snprint_n_chars_)(char *, size_t , size_t , char) = snprint_n_chars;
2018-01-21 09:19:18 +01:00
if (cell == NULL || buf_len == 0
2018-01-17 19:22:57 +01:00
|| (buf_len <= hint_width_cell(cell, context))) {
return -1;
}
2018-03-25 10:11:08 +02:00
int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING);
int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING);
int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING);
2018-02-27 19:41:58 +01:00
2018-01-21 09:19:18 +01:00
if (row >= hint_height_cell(cell, context)
2018-02-27 19:41:58 +01:00
|| row < cell_padding_top
|| row >= (cell_padding_top + buffer_text_height(cell->str_buffer))) {
2018-03-05 19:08:14 +01:00
int k = snprint_n_chars_(buf, buf_len, buf_len - 1, space_char);
2018-01-17 19:22:57 +01:00
return k;
} else {
int written = 0;
2018-02-27 19:41:58 +01:00
int left = cell_padding_left;
int right = cell_padding_right;
2018-01-17 19:22:57 +01:00
2018-03-05 19:08:14 +01:00
written += snprint_n_chars_(buf + written, buf_len - written, left, space_char);
2018-01-17 19:22:57 +01:00
if (cell->str_buffer)
2018-03-05 19:08:14 +01:00
written += buffer_printf_(cell->str_buffer, row - cell_padding_top, column, buf + written, buf_len - written - right, context);
2018-01-17 19:22:57 +01:00
else
2018-03-05 19:08:14 +01:00
written += snprint_n_chars_(buf + written, buf_len - written, buf_len - written - right, space_char);
written += snprint_n_chars_(buf + written, buf_len - written, right, space_char);
2018-01-17 19:22:57 +01:00
return written;
}
}
2018-03-05 19:08:14 +01:00
int cell_wprintf(fort_cell_t *cell, size_t row, size_t column, wchar_t *buf, size_t buf_len, const context_t *context)
{
wchar_t space_char = L' ';
int (*buffer_printf_)(string_buffer_t *, size_t , size_t , wchar_t *, size_t , const context_t *) = buffer_wprintf;
int (*snprint_n_chars_)(wchar_t *, size_t , size_t , wchar_t) = wsnprint_n_chars;
if (cell == NULL || buf_len == 0
|| (buf_len <= hint_width_cell(cell, context))) {
return -1;
}
2018-03-25 10:11:08 +02:00
int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING);
int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING);
int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING);
2018-03-05 19:08:14 +01:00
if (row >= hint_height_cell(cell, context)
|| 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, space_char);
return k;
} else {
int written = 0;
int left = cell_padding_left;
int right = cell_padding_right;
written += snprint_n_chars_(buf + written, buf_len - written, left, space_char);
if (cell->str_buffer)
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, space_char);
written += snprint_n_chars_(buf + written, buf_len - written, right, space_char);
return written;
}
}
2018-01-17 19:22:57 +01:00
fort_status_t fill_cell_from_string(fort_cell_t *cell, const char *str)
{
assert(str);
assert(cell);
return fill_buffer_from_string(cell->str_buffer, str);
}
string_buffer_t *cell_get_string_buffer(fort_cell_t *cell)
{
assert(cell);
assert(cell->str_buffer);
return cell->str_buffer;
}