Fix problem with tab size and subpixel positioning

Solve github issue https://github.com/franko/lite-xl/issues/109
This commit is contained in:
Francesco Abbate 2021-03-18 13:54:33 +01:00
parent 27f3cb23d4
commit 88bec172a1
5 changed files with 18 additions and 14 deletions

View File

@ -370,7 +370,7 @@ function DocView:draw()
self:draw_background(style.background) self:draw_background(style.background)
local font = self:get_font() local font = self:get_font()
font:set_tab_width(font:get_width(" ") * config.indent_size) font:set_tab_size(config.indent_size)
local minline, maxline = self:get_visible_line_range() local minline, maxline = self:get_visible_line_range()
local lh = self:get_line_height() local lh = self:get_line_height()

View File

@ -48,10 +48,10 @@ static int f_load(lua_State *L) {
} }
static int f_set_tab_width(lua_State *L) { static int f_set_tab_size(lua_State *L) {
RenFont **self = luaL_checkudata(L, 1, API_TYPE_FONT); RenFont **self = luaL_checkudata(L, 1, API_TYPE_FONT);
int n = luaL_checknumber(L, 2); int n = luaL_checknumber(L, 2);
ren_set_font_tab_width(*self, n); ren_set_font_tab_size(*self, n);
return 0; return 0;
} }
@ -98,7 +98,7 @@ static int f_get_height(lua_State *L) {
static const luaL_Reg lib[] = { static const luaL_Reg lib[] = {
{ "__gc", f_gc }, { "__gc", f_gc },
{ "load", f_load }, { "load", f_load },
{ "set_tab_width", f_set_tab_width }, { "set_tab_size", f_set_tab_size },
{ "get_width", f_get_width }, { "get_width", f_get_width },
{ "get_width_subpixel", f_get_width_subpixel }, { "get_width_subpixel", f_get_width_subpixel },
{ "get_height", f_get_height }, { "get_height", f_get_height },

View File

@ -20,7 +20,7 @@ typedef struct {
RenFont *font; RenFont *font;
short int subpixel_scale; short int subpixel_scale;
short int x_subpixel_offset; short int x_subpixel_offset;
int tab_width; int tab_size;
char text[0]; char text[0];
} Command; } Command;
@ -149,7 +149,7 @@ int rencache_draw_text(RenFont *font, const char *text, int x, int y, RenColor c
cmd->rect = rect; cmd->rect = rect;
cmd->subpixel_scale = (draw_subpixel ? subpixel_scale : 1); cmd->subpixel_scale = (draw_subpixel ? subpixel_scale : 1);
cmd->x_subpixel_offset = x - subpixel_scale * rect.x; cmd->x_subpixel_offset = x - subpixel_scale * rect.x;
cmd->tab_width = ren_get_font_tab_width(font); cmd->tab_size = ren_get_font_tab_size(font);
} }
} }
@ -261,11 +261,11 @@ void rencache_end_frame(void) {
ren_draw_rect(cmd->rect, cmd->color); ren_draw_rect(cmd->rect, cmd->color);
break; break;
case DRAW_TEXT: case DRAW_TEXT:
ren_set_font_tab_width(cmd->font, cmd->tab_width); ren_set_font_tab_size(cmd->font, cmd->tab_size);
ren_draw_text(cmd->font, cmd->text, cmd->rect.x, cmd->rect.y, cmd->color); ren_draw_text(cmd->font, cmd->text, cmd->rect.x, cmd->rect.y, cmd->color);
break; break;
case DRAW_TEXT_SUBPIXEL: case DRAW_TEXT_SUBPIXEL:
ren_set_font_tab_width(cmd->font, cmd->tab_width); ren_set_font_tab_size(cmd->font, cmd->tab_size);
ren_draw_text_subpixel(cmd->font, cmd->text, cmd->subpixel_scale * cmd->rect.x + cmd->x_subpixel_offset, cmd->rect.y, cmd->color); ren_draw_text_subpixel(cmd->font, cmd->text, cmd->subpixel_scale * cmd->rect.x + cmd->x_subpixel_offset, cmd->rect.y, cmd->color);
break; break;
} }

View File

@ -23,6 +23,7 @@ struct RenFont {
GlyphSet *sets[MAX_GLYPHSET]; GlyphSet *sets[MAX_GLYPHSET];
float size; float size;
int height; int height;
int space_advance;
FR_Renderer *renderer; FR_Renderer *renderer;
}; };
@ -145,6 +146,9 @@ RenFont* ren_load_font(const char *filename, float size, unsigned int renderer_f
} }
font->height = FR_Get_Font_Height(font->renderer, size); font->height = FR_Get_Font_Height(font->renderer, size);
FR_Bitmap_Glyph_Metrics *gs = get_glyphset(font, ' ')->glyphs;
font->space_advance = gs[' '].xadvance;
/* make tab and newline glyphs invisible */ /* make tab and newline glyphs invisible */
FR_Bitmap_Glyph_Metrics *g = get_glyphset(font, '\n')->glyphs; FR_Bitmap_Glyph_Metrics *g = get_glyphset(font, '\n')->glyphs;
g['\t'].x1 = g['\t'].x0; g['\t'].x1 = g['\t'].x0;
@ -167,15 +171,15 @@ void ren_free_font(RenFont *font) {
} }
void ren_set_font_tab_width(RenFont *font, int n) { void ren_set_font_tab_size(RenFont *font, int n) {
GlyphSet *set = get_glyphset(font, '\t'); GlyphSet *set = get_glyphset(font, '\t');
set->glyphs['\t'].xadvance = n; set->glyphs['\t'].xadvance = font->space_advance * n;
} }
int ren_get_font_tab_width(RenFont *font) { int ren_get_font_tab_size(RenFont *font) {
GlyphSet *set = get_glyphset(font, '\t'); GlyphSet *set = get_glyphset(font, '\t');
return set->glyphs['\t'].xadvance; return set->glyphs['\t'].xadvance / font->space_advance;
} }

View File

@ -32,8 +32,8 @@ void ren_free_image(RenImage *image);
RenFont* ren_load_font(const char *filename, float size, unsigned int renderer_flags); RenFont* ren_load_font(const char *filename, float size, unsigned int renderer_flags);
void ren_free_font(RenFont *font); void ren_free_font(RenFont *font);
void ren_set_font_tab_width(RenFont *font, int n); void ren_set_font_tab_size(RenFont *font, int n);
int ren_get_font_tab_width(RenFont *font); int ren_get_font_tab_size(RenFont *font);
int ren_get_font_width(RenFont *font, const char *text, int *subpixel_scale); int ren_get_font_width(RenFont *font, const char *text, int *subpixel_scale);
int ren_get_font_height(RenFont *font); int ren_get_font_height(RenFont *font);
int ren_get_font_subpixel_scale(RenFont *font); int ren_get_font_subpixel_scale(RenFont *font);