Browse Source

Take text length as parameter

Léo Gaspard 5 years ago
parent
commit
5cfd7099af
3 changed files with 10 additions and 14 deletions
  1. 3 8
      dtext.c
  2. 2 2
      dtext.h
  3. 5 4
      examples/basic.c

+ 3 - 8
dtext.c

@@ -2,7 +2,6 @@
 
 #include <errno.h>
 #include <stdint.h>
-#include <wchar.h>
 
 #include <ft2build.h>
 #include FT_FREETYPE_H
@@ -137,16 +136,15 @@ dt_free(dt_context *ctx, dt_font *fnt)
 }
 
 dt_error
-dt_box(dt_context *ctx, dt_font *fnt, dt_bbox *bbox, wchar_t const *txt)
+dt_box(dt_context *ctx, dt_font *fnt, dt_bbox *bbox,
+       wchar_t const *txt, size_t len)
 {
 	dt_error err;
-	size_t len;
 	size_t i;
 	dt_pair const *p;
 
 	memset(bbox, 0, sizeof(*bbox));
 
-	len = wcslen(txt);
 	for (i = 0; i < len; ++i) {
 		if ((err = load_char(ctx, fnt, txt[i])))
 			return err;
@@ -161,11 +159,10 @@ dt_box(dt_context *ctx, dt_font *fnt, dt_bbox *bbox, wchar_t const *txt)
 
 dt_error
 dt_draw(dt_context *ctx, dt_font *fnt, dt_color const *color,
-        uint32_t x, uint32_t y, wchar_t const *txt)
+        uint32_t x, uint32_t y, wchar_t const *txt, size_t len)
 {
 	dt_error err;
 	XRenderColor col;
-	size_t len;
 	size_t i;
 
 	col.red   = (color->red   << 8) + color->red;
@@ -174,8 +171,6 @@ dt_draw(dt_context *ctx, dt_font *fnt, dt_color const *color,
 	col.alpha = (color->alpha << 8) + color->alpha;
 	XRenderFillRectangle(ctx->dpy, PictOpSrc, ctx->fill, &col, 0, 0, 1, 1);
 
-	len = wcslen(txt);
-
 	for (i = 0; i < len; ++i)
 		if ((err = load_char(ctx, fnt, txt[i])))
 			return err;

+ 2 - 2
dtext.h

@@ -60,6 +60,6 @@ dt_error dt_load(dt_context *ctx, dt_font **fnt, char const *name);
 void dt_free(dt_context *ctx, dt_font *fnt);
 
 dt_error dt_box(dt_context *ctx, dt_font *fnt, dt_bbox *bbox,
-                wchar_t const *txt);
+                wchar_t const *txt, size_t len);
 dt_error dt_draw(dt_context *ctx, dt_font *fnt, dt_color const *color,
-                 uint32_t x, uint32_t y, wchar_t const *txt);
+                 uint32_t x, uint32_t y, wchar_t const *txt, size_t len);

+ 5 - 4
examples/basic.c

@@ -2,6 +2,7 @@
 
 #include <assert.h>
 #include <stdint.h>
+#include <wchar.h>
 
 #include <X11/Xlib.h>
 #include <X11/keysym.h>
@@ -96,14 +97,14 @@ static void draw()
 {
 	dt_bbox bbox;
 
-	assert(!dt_draw(ctx, fnt, &color, 10, 50, TEXT));
+	assert(!dt_draw(ctx, fnt, &color, 10, 50, TEXT, wcslen(TEXT)));
 
-	assert(!dt_box(ctx, fnt, &bbox, TEXT));
+	assert(!dt_box(ctx, fnt, &bbox, TEXT, wcslen(TEXT)));
 	XFillRectangle(dpy, win, gc, 10 + bbox.x, 100 + bbox.y, bbox.w, bbox.h);
-	assert(!dt_draw(ctx, fnt, &color_inv, 10, 100, TEXT));
+	assert(!dt_draw(ctx, fnt, &color_inv, 10, 100, TEXT, wcslen(TEXT)));
 
 	XFillRectangle(dpy, win, gc, 10 + bbox.x, 150 - fnt->ascent, bbox.w, fnt->height);
-	assert(!dt_draw(ctx, fnt, &color_inv, 10, 150, TEXT));
+	assert(!dt_draw(ctx, fnt, &color_inv, 10, 150, TEXT, wcslen(TEXT)));
 
 	XFlush(dpy);
 }