~hp/d2tk

84d245394e369babef3013f980582f56054bce99 — Hanspeter Portner a month ago 08b84db
core: serialize font_face|text|image string length

This saves us a `strlen` in the backends.
6 files changed, 14 insertions(+), 6 deletions(-)

M meson_options.txt
M src/backend_cairo.c
M src/backend_nanovg.c
M src/core.c
M src/core_internal.h
M test/core.c
M meson_options.txt => meson_options.txt +1 -1
@@ 51,4 51,4 @@ option('use-vterm',
	value : 'disabled',
	yield : true)

option('version', type : 'string', value : '0.9.91')
option('version', type : 'string', value : '0.9.93')

M src/backend_cairo.c => src/backend_cairo.c +2 -2
@@ 632,7 632,7 @@ d2tk_cairo_process(void *data, d2tk_core_t *core, const d2tk_com_t *com,
		{
			const d2tk_body_font_face_t *body = &com->body->font_face;

			const uint64_t hash = d2tk_hash(body->face, strlen(body->face));
			const uint64_t hash = d2tk_hash(body->face, body->len);
			uintptr_t *sprite = d2tk_core_get_sprite(core, hash, SPRITE_TYPE_FONT);
			assert(sprite);



@@ 720,7 720,7 @@ d2tk_cairo_process(void *data, d2tk_core_t *core, const d2tk_com_t *com,
		{
			const d2tk_body_image_t *body = &com->body->image;

			const uint64_t hash = d2tk_hash(body->path, strlen(body->path));
			const uint64_t hash = d2tk_hash(body->path, body->len);
			uintptr_t *sprite = d2tk_core_get_sprite(core, hash, SPRITE_TYPE_SURF);
			assert(sprite);


M src/backend_nanovg.c => src/backend_nanovg.c +2 -3
@@ 8,7 8,6 @@
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <string.h>

#include "d2tk_config.h"



@@ 658,7 657,7 @@ d2tk_nanovg_process(void *data, d2tk_core_t *core, const d2tk_com_t *com,
		{
			const d2tk_body_font_face_t *body = &com->body->font_face;

			const uint64_t hash = d2tk_hash(body->face, strlen(body->face));
			const uint64_t hash = d2tk_hash(body->face, body->len);
			uintptr_t *sprite = d2tk_core_get_sprite(core, hash, SPRITE_TYPE_FONT);
			assert(sprite);



@@ 733,7 732,7 @@ d2tk_nanovg_process(void *data, d2tk_core_t *core, const d2tk_com_t *com,
		{
			const d2tk_body_image_t *body = &com->body->image;

			const uint64_t hash = d2tk_hash(body->path, strlen(body->path));
			const uint64_t hash = d2tk_hash(body->path, body->len);
			uintptr_t *sprite = d2tk_core_get_sprite(core, hash, SPRITE_TYPE_IMG);
			assert(sprite);


M src/core.c => src/core.c +3 -0
@@ 1024,6 1024,7 @@ d2tk_core_font_face(d2tk_core_t *core, size_t sz, const char *face)
	if(body)
	{
		memcpy(body->font_face.face, face, sz);
		body->font_face.len = sz;
		body->font_face.face[sz] = '\0';

		_d2tk_append_advance(core, len);


@@ 1044,6 1045,7 @@ d2tk_core_text(d2tk_core_t *core, const d2tk_rect_t *rect, size_t sz,
		body->text.w = rect->w;
		body->text.h = rect->h;
		body->text.align = align;
		body->text.len = sz;
		memcpy(body->text.text, text, sz);
		body->text.text[sz] = '\0';



@@ 1068,6 1070,7 @@ d2tk_core_image(d2tk_core_t *core, const d2tk_rect_t *rect, size_t sz,
		body->image.w = rect->w;
		body->image.h = rect->h;
		body->image.align = align;
		body->image.len = sz;
		memcpy(body->image.path, path, sz);
		body->image.path[sz] = '\0';


M src/core_internal.h => src/core_internal.h +3 -0
@@ 137,6 137,7 @@ struct _d2tk_body_rotate_t {
};

struct _d2tk_body_font_face_t {
	size_t len;
	char face [1]; // at least zero-terminator
};



@@ 150,6 151,7 @@ struct _d2tk_body_text_t {
	d2tk_coord_t w;
	d2tk_coord_t h;
	d2tk_align_t align;
	size_t len;
	char text [1]; // at least zero-terminator
};



@@ 159,6 161,7 @@ struct _d2tk_body_image_t {
	d2tk_coord_t w;
	d2tk_coord_t h;
	d2tk_align_t align;
	size_t len;
	char path [1]; // at least zero-terminator
};


M test/core.c => test/core.c +3 -0
@@ 989,6 989,7 @@ _check_font_face(const d2tk_com_t *com, const d2tk_clip_t *clip)
	assert(com->size == sizeof(d2tk_body_font_face_t) + strlen(FONT_FACE));
	assert(com->instr == D2TK_INSTR_FONT_FACE);
	assert(strcmp(com->body->font_face.face, FONT_FACE) == 0);
	assert(com->body->font_face.len == strlen(FONT_FACE));
}

static void


@@ 1042,6 1043,7 @@ _check_text(const d2tk_com_t *com, const d2tk_clip_t *clip)
	assert(com->body->text.h == TEXT_H);
	assert(strcmp(com->body->text.text, TEXT_TEXT) == 0);
	assert(com->body->text.align == TEXT_ALIGN);
	assert(com->body->text.len == strlen(TEXT_TEXT));
}

static void


@@ 1101,6 1103,7 @@ _check_image(const d2tk_com_t *com, const d2tk_clip_t *clip)
	assert(com->body->image.h == IMAGE_H);
	assert(strcmp(com->body->image.path , IMAGE_PATH) == 0);
	assert(com->body->image.align == IMAGE_ALIGN);
	assert(com->body->image.len == strlen(IMAGE_PATH));
}

static void