cell: pack more efficiently and store glyph as a wchar

The 'attributes' struct is now 8 bytes and naturally packed (used to
be 9 bytes, artificially packed).

'cell' struct is now 12 bytes, naturally packed (used to be 13 bytes,
artificially packed).

Furthermore, the glyph is stored as a wchar instead of a char*. This
makes it easier (faster) to do glyph lookup when rendering.
This commit is contained in:
Daniel Eklöf 2019-08-02 18:19:07 +02:00
parent ab92abbd21
commit 4d7993b36f
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
9 changed files with 146 additions and 129 deletions

49
vt.c
View file

@ -728,33 +728,29 @@ action_print_utf8(struct terminal *term)
print_insert(term);
//LOG_DBG("print: UTF8: %.*s", (int)term->vt.utf8.idx, term->vt.utf8.data);
memcpy(cell->c, term->vt.utf8.data, term->vt.utf8.idx);
cell->c[term->vt.utf8.idx] = '\0';
mbstate_t ps = {0};
if (mbrtowc(&cell->wc, (const char *)term->vt.utf8.data, term->vt.utf8.idx, &ps) < 0)
cell->wc = 0;
term->vt.utf8.idx = 0;
cell->attrs = term->vt.attrs;
/* Hack: zero- and double-width characters */
mbstate_t ps = {0};
wchar_t wc;
if (mbrtowc(&wc, cell->c, 4, &ps) >= 0) {
int width = wcwidth(wc);
if (width <= 0) {
/* Skip post_print() below - i.e. don't advance cursor */
return;
}
int width = wcwidth(cell->wc);
if (width <= 0) {
/* Skip post_print() below - i.e. don't advance cursor */
return;
}
/* Advance cursor the 'additional' columns (last step is done
* by post_print()) */
for (int i = 1; i < width && term->cursor.col < term->cols - 1; i++) {
term_cursor_right(term, 1);
/* Advance cursor the 'additional' columns (last step is done
* by post_print()) */
for (int i = 1; i < width && term->cursor.col < term->cols - 1; i++) {
term_cursor_right(term, 1);
assert(term->cursor.col < term->cols);
struct cell *cell = &row->cells[term->cursor.col];
cell->c[0] = '\0';
cell->attrs.clean = 0;
}
assert(term->cursor.col < term->cols);
struct cell *cell = &row->cells[term->cursor.col];
cell->wc = 0;
cell->attrs.clean = 0;
}
post_print(term);
@ -788,16 +784,17 @@ action_print(struct terminal *term, uint8_t c)
c >= 0x41 && c <= 0x7e)
{
const char *glyph = vt100_0[c - 0x41];
if (glyph != NULL)
strncpy(cell->c, glyph, sizeof(cell->c));
if (glyph != NULL) {
mbstate_t ps = {0};
if (mbrtowc(&cell->wc, glyph, strlen(glyph), &ps) < 0)
cell->wc = 0;
}
} else {
//LOG_DBG("print: ASCII: %c", c);
cell->c[0] = c;
cell->c[1] = '\0';
cell->wc = c;
}
cell->attrs = term->vt.attrs;
post_print(term);
}