diff --git a/kitty/cursor.c b/kitty/cursor.c index 39b578c0f..74cce114e 100644 --- a/kitty/cursor.c +++ b/kitty/cursor.c @@ -142,7 +142,7 @@ cursor_from_sgr(Cursor *self, int *params, unsigned int count, bool is_group) { } static void -apply_sgr_to_style(Style *st, int *params, unsigned int count, bool is_group) { +apply_sgr_to_style(CellStyle *st, int *params, unsigned int count, bool is_group) { // Apply SGR params to a style as DECCARA does #define SET_COLOR(which) \ { \ @@ -208,7 +208,7 @@ apply_sgr_to_style(Style *st, int *params, unsigned int count, bool is_group) { } void -apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group) { +apply_sgr_to_cells(CellStyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group) { if (!count) { params[0] = 0; count = 1; @@ -218,7 +218,7 @@ apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int * for (unsigned c = 0; c < cell_count; c++) { const uint32_t old = cells[c].style_idx; if (old != last_old) { - Style st = *style_table_get(t, old); + CellStyle st = *style_table_get(t, old); apply_sgr_to_style(&st, params, count, is_group); last_old = old; last_new = style_table_intern(t, &st); @@ -229,7 +229,7 @@ apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int * const char * cursor_as_sgr(const Cursor *self) { - static const Style blank = {0}; + static const CellStyle blank = {0}; return style_as_sgr(&self->style, &blank); } diff --git a/kitty/data-types.h b/kitty/data-types.h index 065bb60ae..cc8439527 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -110,12 +110,12 @@ typedef union CellAttrs { static_assert(sizeof(CellAttrs) == sizeof(uint32_t), "Fix the ordering of CellAttrs"); // The SGR formatting of a cell. All zero is the default style. Styles are -// interned per screen in a StyleTable, see style-table.h -typedef struct Style { +// interned per screen in a CellStyleTable, see style-table.h +typedef struct CellStyle { color_type fg, bg, decoration_fg; CellAttrs attrs; // the mark bits are always zero -} Style; -static_assert(sizeof(Style) == 4 * sizeof(uint32_t), "Style must have no padding as it is compared and hashed bytewise"); +} CellStyle; +static_assert(sizeof(CellStyle) == 4 * sizeof(uint32_t), "CellStyle must have no padding as it is compared and hashed bytewise"); typedef enum CursorShapes { NO_CURSOR_SHAPE, CURSOR_BLOCK, CURSOR_BEAM, CURSOR_UNDERLINE, CURSOR_HOLLOW, NUM_OF_CURSOR_SHAPES } CursorShape; typedef enum { DISABLE_LIGATURES_NEVER, DISABLE_LIGATURES_CURSOR, DISABLE_LIGATURES_ALWAYS } DisableLigature; @@ -283,8 +283,8 @@ typedef struct { bool non_blinking; CursorShape shape; - Style style; - // A cache of the index of style in the StyleTable of the screen, see screen_cursor_style_idx() + CellStyle style; + // A cache of the index of style in the CellStyleTable of the screen, see screen_cursor_style_idx() uint32_t style_idx; } Cursor; diff --git a/kitty/fonts.c b/kitty/fonts.c index 76c098e14..520597be5 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -2087,7 +2087,7 @@ expanded_line_for_render(const Line *line) { render_scratch.capacity = line->xnum; } // Only the attributes (for font selection) and sprites are used when rendering - const StyleTable *t = line_style_table(line); + const CellStyleTable *t = line_style_table(line); for (index_type i = 0; i < line->xnum; i++) { render_scratch.cells[i] = (GPUCell){ .attrs = style_table_get(t, line->cpu_cells[i].style_idx)->attrs, .sprite_idx = line->render_cells ? line->render_cells[i].sprite_idx : 0}; @@ -2284,7 +2284,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, if (!line->render_cells) return; // lines without render data cannot be rendered FontGroup *fg = (FontGroup *)fg_; GPUCell *gpu_cells = expanded_line_for_render(line); - StyleTable *style_table = line_style_table(line); + CellStyleTable *style_table = line_style_table(line); RunFont basic_font = {.scale = 1, .font_idx = NO_FONT}, run_font = basic_font, cell_font = basic_font; bool center_glyph = false; bool disable_ligature_at_cursor = cursor != NULL && disable_ligature_strategy == DISABLE_LIGATURES_CURSOR; @@ -2334,8 +2334,8 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, // for the space and the PUA. See for example: https://github.com/kovidgoyal/kitty/issues/467 // The colors are changed in the style of the cell, as the GPU gets colors from it CPUCell *space_cpu_cell = line->cpu_cells + i + num_spaces; - const Style *pua_style = style_table_get(style_table, cpu_cell->style_idx); - Style st = *style_table_get(style_table, space_cpu_cell->style_idx); + const CellStyle *pua_style = style_table_get(style_table, cpu_cell->style_idx); + CellStyle st = *style_table_get(style_table, space_cpu_cell->style_idx); st.fg = pua_style->fg; st.decoration_fg = pua_style->decoration_fg; space_cpu_cell->style_idx = style_table_intern(style_table, &st); diff --git a/kitty/line.c b/kitty/line.c index 4b184b76f..887735cec 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -637,7 +637,7 @@ line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_ return s->escape_code_written; } - const StyleTable *style_table = line_style_table(self); + const CellStyleTable *style_table = line_style_table(self); uint32_t prev_style_idx = UINT32_MAX; for (s->pos = start_at; s->pos < s->limit; s->pos++) { @@ -649,7 +649,7 @@ line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_ if (style_idx != prev_style_idx) { // consecutive cells usually have the same style prev_style_idx = style_idx; - const Style *cs = style_table_get(style_table, style_idx); + const CellStyle *cs = style_table_get(style_table, style_idx); if (!style_eq(cs, &s->prev_style)) { const char *sgr = style_as_sgr(cs, &s->prev_style); if (*sgr) write_sgr_to_ansi_buf(s, sgr); @@ -864,7 +864,7 @@ bool colors_for_cell(Line *self, const ColorProfile *cp, index_type *x, color_type *fg, color_type *bg, bool *reversed) { if (*x >= self->xnum) return false; while (self->cpu_cells[*x].is_multicell && self->cpu_cells[*x].x && *x) (*x)--; - const Style *st = line_cell_style(self, *x); + const CellStyle *st = line_cell_style(self, *x); *fg = resolve_color(cp, st->fg, *fg); *bg = resolve_color(cp, st->bg, *bg); if (st->attrs.reverse) { @@ -889,9 +889,9 @@ line_get_char(Line *self, index_type at) { static void line_set_char(Line *self, unsigned int at, uint32_t ch, Cursor *cursor, hyperlink_id_type hyperlink_id) { - StyleTable *t = line_style_table(self); + CellStyleTable *t = line_style_table(self); CPUCell *c = self->cpu_cells + at; - Style st = cursor ? cursor->style : *style_table_get(t, c->style_idx); + CellStyle st = cursor ? cursor->style : *style_table_get(t, c->style_idx); *c = (CPUCell){0}; cell_set_char(c, ch); c->hyperlink_id = hyperlink_id; @@ -966,7 +966,7 @@ decoration_as_sgr(uint8_t decoration) { const char * -style_as_sgr(const Style *cell, const Style *prev) { +style_as_sgr(const CellStyle *cell, const CellStyle *prev) { static char buf[128]; #define SZ sizeof(buf) - (p - buf) - 2 #define P(s) \ @@ -1019,7 +1019,7 @@ __eq__(Line *a, Line *b) { if (a->xnum != b->xnum) return false; if (a->text_cache == b->text_cache) return memcmp(a->cpu_cells, b->cpu_cells, sizeof(CPUCell) * a->xnum) == 0; // Lines from different screens, compare styles by value - const StyleTable *ta = line_style_table(a), *tb = line_style_table(b); + const CellStyleTable *ta = line_style_table(a), *tb = line_style_table(b); for (index_type x = 0; x < a->xnum; x++) { CPUCell ca = a->cpu_cells[x], cb = b->cpu_cells[x]; if (!style_eq(style_table_get(ta, ca.style_idx), style_table_get(tb, cb.style_idx))) return false; diff --git a/kitty/line.h b/kitty/line.h index 06003dbca..2705d73ca 100644 --- a/kitty/line.h +++ b/kitty/line.h @@ -29,7 +29,7 @@ static_assert(sizeof(GPUCell) == 20, "Fix the ordering of GPUCell"); // GPUCell is used when rendering lines, see render_line() // The data for a cell uploaded to the GPU. The shaders get the style of the -// cell from a texture holding the StyleTable of the screen. +// cell from a texture holding the CellStyleTable of the screen. typedef struct CellGPUData { sprite_index sprite_idx; uint32_t style_and_mark; // style_idx << CELL_GPU_STYLE_SHIFT | mark @@ -76,7 +76,7 @@ typedef union CPUCell { char_type x : WIDTH_BITS + SCALE_BITS; char_type y : SCALE_BITS; char_type width : WIDTH_BITS; - // Index of the formatting of this cell in the StyleTable of the screen + // Index of the formatting of this cell in the CellStyleTable of the screen char_type style_idx : STYLE_IDX_BITS; }; struct { @@ -122,7 +122,7 @@ typedef struct MultiCellCommand { } MultiCellCommand; typedef struct ANSILineOutput { - Style prev_style; // style of the previous cell output, zero (the default style) at the start + CellStyle prev_style; // style of the previous cell output, zero (the default style) at the start const CPUCell *current_multicell_state; index_type pos, limit; ANSIBuf *output_buf; @@ -137,8 +137,8 @@ cleanup_ansibuf(ANSIBuf *b) { #define RAII_ANSIBuf(name) __attribute__((cleanup(cleanup_ansibuf))) ANSIBuf name = {0} Line *alloc_line(TextCache *text_cache); -void apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group); -const char *style_as_sgr(const Style *, const Style *); +void apply_sgr_to_cells(CellStyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group); +const char *style_as_sgr(const CellStyle *, const CellStyle *); static inline bool cell_has_text(const CPUCell *c) { return c->ch_and_idx != 0; @@ -235,7 +235,7 @@ cell_first_char(const CPUCell *c, const TextCache *tc) { return c->ch_or_idx; } -static inline StyleTable * +static inline CellStyleTable * line_style_table(const Line *l) { return tc_style_table(l->text_cache); } @@ -250,13 +250,13 @@ line_styles_are_valid(const Line *l) { return true; } -static inline const Style * +static inline const CellStyle * line_cell_style(const Line *l, index_type x) { return style_table_get(line_style_table(l), l->cpu_cells[x].style_idx); } static inline uint32_t -cursor_style_idx_in(StyleTable *t, const Cursor *cursor) { +cursor_style_idx_in(CellStyleTable *t, const Cursor *cursor) { // Uses the cache in the cursor without updating it uint32_t idx = cursor->style_idx; return style_table_intern_cached(t, &cursor->style, &idx); @@ -271,7 +271,7 @@ fill_blank_cpu_cells(CPUCell *c, index_type num, uint32_t style_idx) { } static inline void -remap_style_idx_range(StyleTable *dest, const StyleTable *src, CPUCell *c, index_type num) { +remap_style_idx_range(CellStyleTable *dest, const CellStyleTable *src, CPUCell *c, index_type num) { // Change the style indices of cells copied from a buffer with a different style table uint32_t cache = 0; for (index_type i = 0; i < num; i++) c[i].style_idx = style_table_intern_cached(dest, style_table_get(src, c[i].style_idx), &cache); diff --git a/kitty/lineops.h b/kitty/lineops.h index b0dc4b3c6..496e20b60 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -10,7 +10,7 @@ #include "line-buf.h" static inline bool -set_named_attribute_on_line(StyleTable *t, CPUCell *cells, RenderCell *render_cells, const char *which, uint16_t val, index_type xnum) { +set_named_attribute_on_line(CellStyleTable *t, CPUCell *cells, RenderCell *render_cells, const char *which, uint16_t val, index_type xnum) { // Set a single attribute on all cells in the line if (strcmp("mark", which) == 0) { if (render_cells) @@ -21,7 +21,7 @@ set_named_attribute_on_line(StyleTable *t, CPUCell *cells, RenderCell *render_ce #define s(q) \ if (strcmp(#q, which) == 0) { \ for (index_type i = 0; i < xnum; i++) { \ - Style st = *style_table_get(t, cells[i].style_idx); \ + CellStyle st = *style_table_get(t, cells[i].style_idx); \ st.attrs.q = val; \ cells[i].style_idx = style_table_intern_cached(t, &st, &cache); \ } \ diff --git a/kitty/resize.c b/kitty/resize.c index 77f885563..8e587eb2c 100644 --- a/kitty/resize.c +++ b/kitty/resize.c @@ -518,7 +518,7 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co uint32_t fill_bg = 0; bg_freq_map freq; vt_init(&freq); - const StyleTable *t = tc_style_table(lb->text_cache); + const CellStyleTable *t = tc_style_table(lb->text_cache); for (index_type y = 0; y < ans.num_content_lines_after; y++) { CPUCell *cp; linebuf_init_cells(lb, y, &cp, NULL); @@ -536,12 +536,12 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co } vt_cleanup(&freq); if (fill_bg) { - StyleTable *t = tc_style_table(ans.lb->text_cache); + CellStyleTable *t = tc_style_table(ans.lb->text_cache); uint32_t style_cache = 0; for (index_type y = ans.num_content_lines_after; y < ans.lb->ynum; y++) { linebuf_init_line(ans.lb, y); for (index_type x = 0; x < ans.lb->xnum; x++) { - Style st = *style_table_get(t, ans.lb->line->cpu_cells[x].style_idx); + CellStyle st = *style_table_get(t, ans.lb->line->cpu_cells[x].style_idx); st.bg = fill_bg; ans.lb->line->cpu_cells[x].style_idx = style_table_intern_cached(t, &st, &style_cache); } diff --git a/kitty/screen.c b/kitty/screen.c index 0f87111a8..449b14ca4 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1299,12 +1299,12 @@ screen_garbage_collect_text_cache(Screen *self) { } static void -style_gc_process_cells(StyleTable *t, StyleTableGC *gc, CPUCell *cells, size_t count, bool quantize) { +style_gc_process_cells(CellStyleTable *t, StyleTableGC *gc, CPUCell *cells, size_t count, bool quantize) { for (size_t i = 0; i < count; i++) cells[i].style_idx = style_table_gc_map(t, gc, cells[i].style_idx, quantize); } static void -style_gc_process_linebuf(StyleTable *t, StyleTableGC *gc, LineBuf *lb) { +style_gc_process_linebuf(CellStyleTable *t, StyleTableGC *gc, LineBuf *lb) { if (lb) style_gc_process_cells(t, gc, lb->cpu_cell_buf, (size_t)lb->ynum * lb->xnum, false); } @@ -1314,7 +1314,7 @@ garbage_collect_style_table(Screen *self, unsigned quantize_bits, bool degraded_ // that are no longer used are freed. With quantize_bits, styles of cells // in the scrollback get colors of reduced precision. Must only be called // when no style indices are held outside of cells. - StyleTable *t = self->style_table; + CellStyleTable *t = self->style_table; StyleTableGC *gc = style_table_gc_begin(t, quantize_bits); if (!gc) return false; // allocation failure, table left unchanged // Screen cells first, so they get exact styles even if the table overflows @@ -1361,7 +1361,7 @@ screen_garbage_collect_style_table(Screen *self) { // precision first and if that does not free at least an eighth of the // table, reduces the color precision of cells in the scrollback. Cells on // screen always keep their exact styles. - StyleTable *t = self->style_table; + CellStyleTable *t = self->style_table; const uint32_t threshold = style_table_max_count(t) - style_table_max_count(t) / 8; // If the last collection had to reduce precision, start there, as the // scrollback is likely still full of distinct styles @@ -1983,7 +1983,7 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ if (tc_should_gc(self->text_cache)) screen_garbage_collect_text_cache(self); \ if (style_table_needs_gc(self->style_table)) screen_garbage_collect_style_table(self); \ const bool force_underline = OPT(underline_hyperlinks) == UNDERLINE_ALWAYS && self->active_hyperlink_id != 0; \ - Style draw_style = self->cursor->style; \ + CellStyle draw_style = self->cursor->style; \ uint32_t draw_style_idx; \ if (force_underline) { \ draw_style.attrs.decoration = OPT(url_style); \ @@ -4600,7 +4600,7 @@ screen_render_line_graphics(Screen *self, Line *line, int32_t row) { line->attrs.has_image_placeholders = true; // The lower 24 bits of the image id are encoded in the foreground // color, and the placement id is (optionally) in the underline color. - const Style *st = style_table_get(self->style_table, cpu_cell->style_idx); + const CellStyle *st = style_table_get(self->style_table, cpu_cell->style_idx); cur_img_id_lower24bits = color_to_id(st->fg); cur_placement_id = color_to_id(st->decoration_fg); text_in_cell(cpu_cell, self->text_cache, self->lc); @@ -5744,7 +5744,7 @@ screen_draw_overlay_line(Screen *self) { // dashed avoids colliding with the styles applications already use: curly // for spell checking and straight for hyperlinks. Saved and restored around // the draw, like the modes above. - const Style orig_style = self->cursor->style; + const CellStyle orig_style = self->cursor->style; self->cursor->style.attrs.italic = true; self->cursor->style.attrs.decoration = 5; // dashed self->cursor->style.decoration_fg = ((colorprofile_to_color_with_fallback( @@ -7910,7 +7910,7 @@ test_commit_write_buffer(Screen *screen, PyObject *args) { } static bool -verify_styles_in_line(const StyleTable *t, const char *which, index_type y, const CPUCell *c, index_type xnum, PyObject *ans) { +verify_styles_in_line(const CellStyleTable *t, const char *which, index_type y, const CPUCell *c, index_type xnum, PyObject *ans) { for (index_type x = 0; x < xnum; x++) { if (c[x].style_idx >= t->count) { RAII_PyObject(e, Py_BuildValue("sIII", which, y, x, (unsigned)c[x].style_idx)); @@ -7926,7 +7926,7 @@ test_verify_styles(Screen *self, PyObject *a UNUSED) { // Check that the style index of every cell is valid. Returns a list of invalid cells. RAII_PyObject(ans, PyList_New(0)); if (!ans) return NULL; - const StyleTable *t = self->style_table; + const CellStyleTable *t = self->style_table; LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, self->paused_rendering.linebuf}; const char *names[3] = {"main", "alt", "paused"}; for (unsigned b = 0; b < arraysz(bufs); b++) { diff --git a/kitty/screen.h b/kitty/screen.h index 71ad4015f..9a8c98ca4 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -151,7 +151,7 @@ typedef struct { Savepoint main_savepoint, alt_savepoint; PyObject *callbacks, *test_child; TextCache *text_cache; - StyleTable *style_table; // owned by text_cache + CellStyleTable *style_table; // owned by text_cache uint32_t hyperlink_style_idx_cache; // The texture holding the style table for the shaders, see sync_style_texture() in shaders.c struct { diff --git a/kitty/shaders.c b/kitty/shaders.c index ff2fb536f..0476cdec5 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -1222,14 +1222,14 @@ cell_update_uniform_block( return default_bg; } -// Style table texture {{{ +// CellStyle table texture {{{ // The cell shaders get the style of each cell from a texture holding the style -// table of its screen. Each Style is four R32UI texels, fg, bg, decoration_fg +// table of its screen. Each CellStyle is four R32UI texels, fg, bg, decoration_fg // and attrs, in the same layout as the C struct, so styles are uploaded // directly from the table. The texture width is a multiple of four, so the // texels of a style are always in a single row. -#define TEXELS_PER_STYLE (sizeof(Style) / sizeof(uint32_t)) -static_assert(sizeof(Style) == 4 * sizeof(uint32_t), "Fix TEXELS_PER_STYLE and the shaders"); +#define TEXELS_PER_STYLE (sizeof(CellStyle) / sizeof(uint32_t)) +static_assert(sizeof(CellStyle) == 4 * sizeof(uint32_t), "Fix TEXELS_PER_STYLE and the shaders"); static void upload_style_texels(gpu_texture_t texture, uint32_t width, const uint32_t *texels, uint32_t start, uint32_t limit) { @@ -1254,7 +1254,7 @@ static void sync_style_texture(Screen *screen) { // Upload styles added to the table since the last upload, or all of them // if the table was garbage collected since then - const StyleTable *t = screen->style_table; + const CellStyleTable *t = screen->style_table; __typeof__(screen->style_texture) *st = &screen->style_texture; bool upload_all = st->generation != t->generation || !st->texture; const uint32_t needed = t->count * TEXELS_PER_STYLE; diff --git a/kitty/style-table.c b/kitty/style-table.c index d376a4619..5e9b57f5a 100644 --- a/kitty/style-table.c +++ b/kitty/style-table.c @@ -9,18 +9,18 @@ #include static uint64_t -hash_style(Style k) { +hash_style(CellStyle k) { return XXH3_64bits(&k, sizeof(k)); } static bool -cmpr_style(Style a, Style b) { +cmpr_style(CellStyle a, CellStyle b) { return style_eq(&a, &b); } // Maps styles to indices, used only to find approximate matches when the table is full #define NAME style_map -#define KEY_TY Style +#define KEY_TY CellStyle #define VAL_TY uint32_t #define HASH_FN hash_style #define CMPR_FN cmpr_style @@ -35,17 +35,17 @@ cmpr_style(Style a, Style b) { // before every operation on the set. Tables are only used on the main thread. // A style not in the table is looked up using the PROBE_KEY index which refers to probe_style. #define PROBE_KEY UINT32_MAX -static const Style *ctx_styles = NULL; -static Style probe_style = {0}; +static const CellStyle *ctx_styles = NULL; +static CellStyle probe_style = {0}; -static inline const Style * +static inline const CellStyle * resolve_key(uint32_t k) { return k == PROBE_KEY ? &probe_style : ctx_styles + k; } static uint64_t hash_style_idx(uint32_t k) { - return XXH3_64bits(resolve_key(k), sizeof(Style)); + return XXH3_64bits(resolve_key(k), sizeof(CellStyle)); } static bool @@ -63,7 +63,7 @@ cmpr_style_idx(uint32_t a, uint32_t b) { #define RECENT_CACHE_BITS 6u typedef struct StyleTableImpl { - StyleTable pub; // must be first + CellStyleTable pub; // must be first uint32_t capacity, max_count; style_set map; // Indices of recently interned styles, indexed by a cheap hash of the @@ -85,12 +85,12 @@ typedef struct StyleTableImpl { #define FINE_BITS 4u #define COARSE_BITS 2u -StyleTable * +CellStyleTable * style_table_alloc(void) { StyleTableImpl *self = calloc(1, sizeof(StyleTableImpl)); if (!self) return NULL; self->capacity = INITIAL_CAPACITY; - self->pub.styles = calloc(self->capacity, sizeof(Style)); + self->pub.styles = calloc(self->capacity, sizeof(CellStyle)); if (!self->pub.styles) { free(self); return NULL; @@ -105,7 +105,7 @@ style_table_alloc(void) { } void -style_table_free(StyleTable *pub) { +style_table_free(CellStyleTable *pub) { if (!pub) return; StyleTableImpl *self = (StyleTableImpl *)pub; vt_cleanup(&self->map); @@ -130,11 +130,11 @@ bump_generation(StyleTableImpl *self) { } void -style_table_reset(StyleTable *pub) { +style_table_reset(CellStyleTable *pub) { // Remove all styles except the default style, freeing memory StyleTableImpl *self = (StyleTableImpl *)pub; if (self->capacity > INITIAL_CAPACITY) { - Style *n = realloc(self->pub.styles, INITIAL_CAPACITY * sizeof(Style)); + CellStyle *n = realloc(self->pub.styles, INITIAL_CAPACITY * sizeof(CellStyle)); if (n) { self->pub.styles = n; self->capacity = INITIAL_CAPACITY; @@ -155,13 +155,13 @@ style_table_reset(StyleTable *pub) { } void -style_table_set_max_count(StyleTable *pub, uint32_t max_count) { +style_table_set_max_count(CellStyleTable *pub, uint32_t max_count) { StyleTableImpl *self = (StyleTableImpl *)pub; self->max_count = MAX(2u, MIN(max_count, MAX_NUM_OF_STYLES)); } uint32_t -style_table_max_count(const StyleTable *pub) { +style_table_max_count(const CellStyleTable *pub) { return ((const StyleTableImpl *)pub)->max_count; } @@ -176,10 +176,10 @@ quantize_color(color_type c, unsigned bits) { return ans; } -Style -style_quantize(const Style *s, unsigned bits) { +CellStyle +style_quantize(const CellStyle *s, unsigned bits) { if (!bits || bits >= 8) return *s; - Style ans = *s; + CellStyle ans = *s; ans.fg = quantize_color(s->fg, bits); ans.bg = quantize_color(s->bg, bits); ans.decoration_fg = quantize_color(s->decoration_fg, bits); @@ -189,8 +189,8 @@ style_quantize(const Style *s, unsigned bits) { static void build_approx(StyleTableImpl *self) { for (uint32_t i = 0; i < self->pub.count; i++) { - const Style *s = self->pub.styles + i; - Style q = style_quantize(s, FINE_BITS); + const CellStyle *s = self->pub.styles + i; + CellStyle q = style_quantize(s, FINE_BITS); if (vt_is_end(vt_get_or_insert(&self->approx.fine, q, i))) fatal("Out of memory building approximate style map"); q = style_quantize(s, COARSE_BITS); if (vt_is_end(vt_get_or_insert(&self->approx.coarse, q, i))) fatal("Out of memory building approximate style map"); @@ -199,7 +199,7 @@ build_approx(StyleTableImpl *self) { } static uint32_t -approximate_match(StyleTableImpl *self, const Style *style) { +approximate_match(StyleTableImpl *self, const CellStyle *style) { // The table is full, use an existing style with the same attributes and close colors if (!self->approx.valid) build_approx(self); style_map_itr itr = vt_get(&self->approx.fine, style_quantize(style, FINE_BITS)); @@ -210,14 +210,14 @@ approximate_match(StyleTableImpl *self, const Style *style) { } static inline uint32_t -recent_slot(const Style *s) { +recent_slot(const CellStyle *s) { const uint32_t h = (s->fg * 0x9E3779B1u) ^ (s->bg * 0x85EBCA77u) ^ (s->decoration_fg * 0xC2B2AE3Du) ^ (s->attrs.val * 0x27D4EB2Fu); return h >> (32 - RECENT_CACHE_BITS); } uint32_t -style_table_intern(StyleTable *pub, const Style *style) { - static const Style default_style = {0}; +style_table_intern(CellStyleTable *pub, const CellStyle *style) { + static const CellStyle default_style = {0}; if (style_eq(style, &default_style)) return 0; StyleTableImpl *self = (StyleTableImpl *)pub; const uint32_t slot = recent_slot(style), recent = self->recent[slot]; @@ -233,7 +233,7 @@ style_table_intern(StyleTable *pub, const Style *style) { } if (self->pub.count >= self->capacity) { const uint32_t new_capacity = MIN(self->capacity * 2, MAX_NUM_OF_STYLES); - Style *n = realloc(self->pub.styles, new_capacity * sizeof(Style)); + CellStyle *n = realloc(self->pub.styles, new_capacity * sizeof(CellStyle)); if (!n) fatal("Out of memory growing the style table"); self->pub.styles = n; self->capacity = new_capacity; @@ -248,7 +248,7 @@ style_table_intern(StyleTable *pub, const Style *style) { // Garbage collection {{{ bool -style_table_needs_gc(const StyleTable *pub) { +style_table_needs_gc(const CellStyleTable *pub) { const StyleTableImpl *self = (const StyleTableImpl *)pub; if (self->pub.count < self->max_count) return false; // When even the styles on screen do not fit, collecting cannot free much, @@ -258,19 +258,19 @@ style_table_needs_gc(const StyleTable *pub) { } struct StyleTableGC { - Style *old_styles; + CellStyle *old_styles; uint32_t old_count, *remap, *quantized_remap; // old index -> new index + 1, 0 means not yet remapped unsigned quantize_bits; }; StyleTableGC * -style_table_gc_begin(StyleTable *pub, unsigned quantize_bits) { +style_table_gc_begin(CellStyleTable *pub, unsigned quantize_bits) { StyleTableImpl *self = (StyleTableImpl *)pub; StyleTableGC *gc = calloc(1, sizeof(StyleTableGC)); if (!gc) return NULL; gc->old_count = self->pub.count; gc->quantize_bits = quantize_bits; - gc->old_styles = malloc(gc->old_count * sizeof(Style)); + gc->old_styles = malloc(gc->old_count * sizeof(CellStyle)); gc->remap = calloc(gc->old_count, sizeof(uint32_t)); if (quantize_bits) gc->quantized_remap = calloc(gc->old_count, sizeof(uint32_t)); if (!gc->old_styles || !gc->remap || (quantize_bits && !gc->quantized_remap)) { @@ -280,7 +280,7 @@ style_table_gc_begin(StyleTable *pub, unsigned quantize_bits) { free(gc); return NULL; } - memcpy(gc->old_styles, self->pub.styles, gc->old_count * sizeof(Style)); + memcpy(gc->old_styles, self->pub.styles, gc->old_count * sizeof(CellStyle)); vt_clear(&self->map); self->pub.count = 1; invalidate_approx(self); @@ -288,7 +288,7 @@ style_table_gc_begin(StyleTable *pub, unsigned quantize_bits) { } uint32_t -style_table_gc_map(StyleTable *pub, StyleTableGC *gc, uint32_t old_idx, bool quantize) { +style_table_gc_map(CellStyleTable *pub, StyleTableGC *gc, uint32_t old_idx, bool quantize) { // Called for every live cell, returns its new style index. quantize is ignored unless // the GC was started with quantize_bits. Cannot overflow the table as the number of // styles re-interned is at most the number there were before. @@ -296,14 +296,14 @@ style_table_gc_map(StyleTable *pub, StyleTableGC *gc, uint32_t old_idx, bool qua uint32_t *map = gc->remap; if (quantize && gc->quantized_remap) map = gc->quantized_remap; if (!map[old_idx]) { - const Style s = map == gc->remap ? gc->old_styles[old_idx] : style_quantize(gc->old_styles + old_idx, gc->quantize_bits); + const CellStyle s = map == gc->remap ? gc->old_styles[old_idx] : style_quantize(gc->old_styles + old_idx, gc->quantize_bits); map[old_idx] = style_table_intern(pub, &s) + 1; } return map[old_idx] - 1; } void -style_table_gc_end(StyleTable *pub, StyleTableGC *gc, bool degraded) { +style_table_gc_end(CellStyleTable *pub, StyleTableGC *gc, bool degraded) { StyleTableImpl *self = (StyleTableImpl *)pub; bump_generation(self); self->misses_since_gc = 0; @@ -321,17 +321,17 @@ style_table_gc_end(StyleTable *pub, StyleTableGC *gc, bool degraded) { } unsigned -style_table_last_gc_quantize_bits(const StyleTable *pub) { +style_table_last_gc_quantize_bits(const CellStyleTable *pub) { return ((const StyleTableImpl *)pub)->last_gc_quantize_bits; } uint64_t -style_table_gc_count(const StyleTable *pub) { +style_table_gc_count(const CellStyleTable *pub) { return ((const StyleTableImpl *)pub)->gc_count; } bool -style_table_is_degraded(const StyleTable *pub) { +style_table_is_degraded(const CellStyleTable *pub) { return ((const StyleTableImpl *)pub)->degraded; } // }}} diff --git a/kitty/style-table.h b/kitty/style-table.h index b75d0cd77..9d585ea1f 100644 --- a/kitty/style-table.h +++ b/kitty/style-table.h @@ -9,33 +9,33 @@ #include "data-types.h" -// The formatting (Style) of cells is interned per screen in a StyleTable and +// The formatting (CellStyle) of cells is interned per screen in a CellStyleTable and // cells store only the index of their style. Index zero is always the default // (all zero) style, so zeroed cells have the default style. #define STYLE_IDX_BITS 18u #define MAX_NUM_OF_STYLES (1u << STYLE_IDX_BITS) -typedef struct StyleTable { - Style *styles; +typedef struct CellStyleTable { + CellStyle *styles; uint32_t count; // Incremented whenever existing indices are invalidated, never zero uint32_t generation; -} StyleTable; +} CellStyleTable; -StyleTable *style_table_alloc(void); -void style_table_free(StyleTable *self); -uint32_t style_table_intern(StyleTable *self, const Style *style); +CellStyleTable *style_table_alloc(void); +void style_table_free(CellStyleTable *self); +uint32_t style_table_intern(CellStyleTable *self, const CellStyle *style); // Remove all styles except the default style, invalidating all indices and freeing memory. // Must only be called when no cells use a style other than the default style. -void style_table_reset(StyleTable *self); +void style_table_reset(CellStyleTable *self); // For testing -void style_table_set_max_count(StyleTable *self, uint32_t max_count); -uint32_t style_table_max_count(const StyleTable *self); -uint64_t style_table_gc_count(const StyleTable *self); -bool style_table_is_degraded(const StyleTable *self); +void style_table_set_max_count(CellStyleTable *self, uint32_t max_count); +uint32_t style_table_max_count(const CellStyleTable *self); +uint64_t style_table_gc_count(const CellStyleTable *self); +bool style_table_is_degraded(const CellStyleTable *self); // Reduce the precision of RGB colors in style to bits per channel -Style style_quantize(const Style *style, unsigned bits); +CellStyle style_quantize(const CellStyle *style, unsigned bits); // Garbage collection. When the table is full, style_table_intern() returns // the index of an existing style close to the requested one. The owner of the @@ -44,20 +44,20 @@ Style style_quantize(const Style *style, unsigned bits); // every live cell, replacing it with the returned index, and // style_table_gc_end(). With quantize_bits, cells for which quantize is true // get styles with colors of reduced precision, to free up space. -bool style_table_needs_gc(const StyleTable *self); -unsigned style_table_last_gc_quantize_bits(const StyleTable *self); +bool style_table_needs_gc(const CellStyleTable *self); +unsigned style_table_last_gc_quantize_bits(const CellStyleTable *self); typedef struct StyleTableGC StyleTableGC; -StyleTableGC *style_table_gc_begin(StyleTable *self, unsigned quantize_bits); -uint32_t style_table_gc_map(StyleTable *self, StyleTableGC *gc, uint32_t old_idx, bool quantize); -void style_table_gc_end(StyleTable *self, StyleTableGC *gc, bool degraded); +StyleTableGC *style_table_gc_begin(CellStyleTable *self, unsigned quantize_bits); +uint32_t style_table_gc_map(CellStyleTable *self, StyleTableGC *gc, uint32_t old_idx, bool quantize); +void style_table_gc_end(CellStyleTable *self, StyleTableGC *gc, bool degraded); -static inline const Style * -style_table_get(const StyleTable *self, uint32_t idx) { +static inline const CellStyle * +style_table_get(const CellStyleTable *self, uint32_t idx) { return self->styles + (idx < self->count ? idx : 0); } static inline bool -style_eq(const Style *a, const Style *b) { +style_eq(const CellStyle *a, const CellStyle *b) { return a->fg == b->fg && a->bg == b->bg && a->decoration_fg == b->decoration_fg && a->attrs.val == b->attrs.val; } @@ -66,7 +66,7 @@ style_eq(const Style *a, const Style *b) { // no invalidation: if the style at *idx equals style, *idx is a valid index // for style, even after the table is compacted. static inline uint32_t -style_table_intern_cached(StyleTable *self, const Style *style, uint32_t *idx) { +style_table_intern_cached(CellStyleTable *self, const CellStyle *style, uint32_t *idx) { if (*idx >= self->count || !style_eq(style, self->styles + *idx)) *idx = style_table_intern(self, style); return *idx; } diff --git a/kitty/text-cache.c b/kitty/text-cache.c index fd7a245c3..5278d5f88 100644 --- a/kitty/text-cache.c +++ b/kitty/text-cache.c @@ -37,7 +37,7 @@ typedef struct TextCache { unsigned refcnt; CharsMonotonicArena arena; unsigned adds_since_last_gc, adds_between_gcs; - StyleTable *style_table; + CellStyleTable *style_table; } TextCache; static uint64_t hash_chars(Chars k) { @@ -82,7 +82,7 @@ free_text_cache(TextCache *self) { free(self); } -StyleTable * +CellStyleTable * tc_style_table(const TextCache *self) { return self->style_table; } diff --git a/kitty/text-cache.h b/kitty/text-cache.h index 83c382424..208a74ca8 100644 --- a/kitty/text-cache.h +++ b/kitty/text-cache.h @@ -58,7 +58,7 @@ typedef struct { TextCache *tc_alloc(void); // The style table of the screen the text cache belongs to -struct StyleTable *tc_style_table(const TextCache *self); +struct CellStyleTable *tc_style_table(const TextCache *self); TextCache *tc_incref(TextCache *self); TextCache *tc_decref(TextCache *self); void tc_chars_at_index(const TextCache *self, char_type idx, ListOfChars *ans);