Skip to content

Commit

Permalink
Use Lua string length instead of relying on strlen (lite-xl#1262)
Browse files Browse the repository at this point in the history
This allows us to render `NULL` byte sequences and not truncate strings 
that contain them.
  • Loading branch information
Guldoman authored Dec 26, 2022
1 parent 18e3542 commit c29b1c2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/api/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ static int f_font_gc(lua_State *L) {

static int f_font_get_width(lua_State *L) {
RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1);
lua_pushnumber(L, ren_font_group_get_width(fonts, luaL_checkstring(L, 2)));
size_t len;
const char *text = luaL_checklstring(L, 2, &len);

lua_pushnumber(L, ren_font_group_get_width(fonts, text, len));
return 1;
}

Expand Down Expand Up @@ -338,11 +341,12 @@ static int f_draw_text(lua_State *L) {
}
lua_pop(L, 1);

const char *text = luaL_checkstring(L, 2);
size_t len;
const char *text = luaL_checklstring(L, 2, &len);
float x = luaL_checknumber(L, 3);
int y = luaL_checknumber(L, 4);
RenColor color = checkcolor(L, 5, 255);
x = rencache_draw_text(fonts, text, x, y, color);
x = rencache_draw_text(fonts, text, len, x, y, color);
lua_pushnumber(L, x);
return 1;
}
Expand Down
10 changes: 6 additions & 4 deletions src/rencache.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct {
RenColor color;
RenFont *fonts[FONT_FALLBACK_MAX];
float text_x;
size_t len;
char text[];
} Command;

Expand Down Expand Up @@ -145,19 +146,20 @@ void rencache_draw_rect(RenRect rect, RenColor color) {
}
}

float rencache_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor color)
float rencache_draw_text(RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color)
{
float width = ren_font_group_get_width(fonts, text);
float width = ren_font_group_get_width(fonts, text, len);
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
if (rects_overlap(screen_rect, rect)) {
int sz = strlen(text) + 1;
int sz = len + 1;
Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz);
if (cmd) {
memcpy(cmd->text, text, sz);
cmd->color = color;
memcpy(cmd->fonts, fonts, sizeof(RenFont*)*FONT_FALLBACK_MAX);
cmd->rect = rect;
cmd->text_x = x;
cmd->len = len;
cmd->tab_size = ren_font_group_get_tab_size(fonts);
}
}
Expand Down Expand Up @@ -266,7 +268,7 @@ void rencache_end_frame() {
break;
case DRAW_TEXT:
ren_font_group_set_tab_size(cmd->fonts, cmd->tab_size);
ren_draw_text(cmd->fonts, cmd->text, cmd->text_x, cmd->rect.y, cmd->color);
ren_draw_text(cmd->fonts, cmd->text, cmd->len, cmd->text_x, cmd->rect.y, cmd->color);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rencache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
void rencache_show_debug(bool enable);
void rencache_set_clip_rect(RenRect rect);
void rencache_draw_rect(RenRect rect, RenColor color);
float rencache_draw_text(RenFont **font, const char *text, float x, int y, RenColor color);
float rencache_draw_text(RenFont **font, const char *text, size_t len, float x, int y, RenColor color);
void rencache_invalidate(void);
void rencache_begin_frame();
void rencache_end_frame();
Expand Down
8 changes: 4 additions & 4 deletions src/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ int ren_font_group_get_height(RenFont **fonts) {
return fonts[0]->height;
}

float ren_font_group_get_width(RenFont **fonts, const char *text) {
float ren_font_group_get_width(RenFont **fonts, const char *text, size_t len) {
float width = 0;
const char* end = text + strlen(text);
const char* end = text + len;
GlyphMetric* metric = NULL; GlyphSet* set = NULL;
while (text < end) {
unsigned int codepoint;
Expand All @@ -374,15 +374,15 @@ float ren_font_group_get_width(RenFont **fonts, const char *text) {
return width / surface_scale;
}

float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor color) {
float ren_draw_text(RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) {
SDL_Surface *surface = renwin_get_surface(&window_renderer);
const RenRect clip = window_renderer.clip;

const int surface_scale = renwin_surface_scale(&window_renderer);
float pen_x = x * surface_scale;
y *= surface_scale;
int bytes_per_pixel = surface->format->BytesPerPixel;
const char* end = text + strlen(text);
const char* end = text + len;
uint8_t* destination_pixels = surface->pixels;
int clip_end_x = clip.x + clip.width, clip_end_y = clip.y + clip.height;

Expand Down
4 changes: 2 additions & 2 deletions src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ int ren_font_group_get_height(RenFont **font);
float ren_font_group_get_size(RenFont **font);
void ren_font_group_set_size(RenFont **font, float size);
void ren_font_group_set_tab_size(RenFont **font, int n);
float ren_font_group_get_width(RenFont **font, const char *text);
float ren_draw_text(RenFont **font, const char *text, float x, int y, RenColor color);
float ren_font_group_get_width(RenFont **font, const char *text, size_t len);
float ren_draw_text(RenFont **font, const char *text, size_t len, float x, int y, RenColor color);

void ren_draw_rect(RenRect rect, RenColor color);

Expand Down

0 comments on commit c29b1c2

Please sign in to comment.