END_TEST START_TEST (test_text_line) { text *text; int i; char buf[128]; const char *p; int ret; int len; text = text_create (); fail_unless (text != NULL); for (i = 0; i < 120; i++) { sprintf (buf, "Line nr %d", i); ret = text_add_line (text, buf); fail_unless (ret == i + 1); fail_unless (text_nr_of_lines (text) == i + 1); } for (i = 0; i < 120; i++) { sprintf (buf, "Line nr %d", i); p = text_get_line (text, i, &len); fail_unless (p != NULL); fail_unless (strlen (buf) == len); fail_unless (strcmp (p, buf) == 0); } p = text_get_line (text, 500, &len); fail_unless (p == NULL); p = text_get_line (text, -50, &len); fail_unless (p == NULL); text_clear (text); fail_unless (text_nr_of_lines (text) == 0); ret = text_add_line (text, "\001"); fail_unless (ret < 0); fail_unless (text_nr_of_lines (text) == 0); ret = text_add_line (text, "\t"); fail_unless (ret == 1); fail_unless (text_nr_of_lines (text) == 1); p = text_get_line (text, 0, &len); fail_unless (p != NULL); fail_unless (len == 2); fail_unless (strcmp (p, " ") == 0); text_free (text); }
/** * convert a str into text ( for listbox ) * width is the available width of a line in pixels */ Text* create_text( struct _Font *fnt, const char *orig_str, int width ) { struct TextData td; const char *line_start = orig_str; const char *head = orig_str; const char *committed = orig_str; int cumulated_width = 0; /* width of text in this line */ int break_line = 0; if (width < 0) width = 0; memset(&td, 0, sizeof(td)); td.text = calloc ( 1, sizeof( Text ) ); while (*committed) { int ch_width = char_width(fnt, *head); if (committed != head && text_is_linebreak(head[-1])) break_line = 1; else if (cumulated_width > width) { /* if the word is too long to fit into one line, * force a line break at the current position. */ if (committed == line_start) /* take away last char (unless only one) and break there */ committed = head - (head - 1 != line_start); head = committed; break_line = 1; } else if (text_is_breakable(committed, head)) committed = head; if (!break_line) { cumulated_width += ch_width; head++; } if (!*head) break_line = 1; if (break_line) { text_add_line(&td, line_start, head); line_start = committed = head; cumulated_width = 0; break_line = 0; } } if (!td.text->lines) text_add_line(&td, "", "" + 1); return td.text; }
/** * @brief ggs_parse_text * * Parse a text (ie a set of lines) from a buffer. * * @param buffer Buffer. * @param text Text. * @return buffer's remaining. */ static char* ggs_parse_text(const char *buffer, Text *text) { char *line; assert(buffer); while (buffer[0] == '|' || text->n_lines == 0) { buffer = ggs_parse_line(buffer, &line); if (!line) break; text_add_line(text, line); }; return (char*) buffer; }