commit a17abf756b718057ca004a0d0d1b0d98145340de
parent b4bcdad02c23c129cfbb077c5b4b6d80a1096834
Author: Michael Forney <mforney@mforney.org>
Date: Thu, 25 Jul 2013 00:46:59 -0700
draw_text_utf8: Optionally return extents
Diffstat:
6 files changed, 43 insertions(+), 19 deletions(-)
diff --git a/drawable.c b/drawable.c
@@ -92,12 +92,13 @@ void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
void wld_draw_text_utf8_n(struct wld_drawable * drawable,
struct wld_font * font_base, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length)
+ const char * text, int32_t length,
+ struct wld_extents * extents)
{
struct font * font = (void *) font_base;
drawable->interface->draw_text_utf8(drawable, font, color, x, y,
- text, length);
+ text, length, extents);
}
void wld_destroy_drawable(struct wld_drawable * drawable)
diff --git a/intel.c b/intel.c
@@ -60,7 +60,8 @@ static void intel_copy_rectangle(struct wld_drawable * src,
static void intel_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length);
+ const char * text, int32_t length,
+ struct wld_extents * extents);
static void intel_flush(struct wld_drawable * drawable);
static void intel_destroy(struct wld_drawable * drawable);
@@ -158,7 +159,8 @@ static void intel_copy_rectangle(struct wld_drawable * src_drawable,
static void intel_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length)
+ const char * text, int32_t length,
+ struct wld_extents * extents)
{
struct intel_drawable * intel = (void *) drawable;
int ret;
@@ -169,6 +171,7 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
uint8_t immediate[512];
uint8_t * byte;
uint8_t byte_index;
+ int32_t origin_x = x;
xy_setup_blt(&intel->context->batch, true, INTEL_BLT_RASTER_OPERATION_SRC,
0, color, intel->bo, intel->drm.pitch);
@@ -199,8 +202,8 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
retry:
ret = xy_text_immediate_blt(&intel->context->batch, intel->bo,
- x + glyph->x, y + glyph->y,
- x + glyph->x + glyph->bitmap.width,
+ origin_x + glyph->x, y + glyph->y,
+ origin_x + glyph->x + glyph->bitmap.width,
y + glyph->y + glyph->bitmap.rows,
(byte - immediate + 3) / 4,
(uint32_t *) immediate);
@@ -215,8 +218,11 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
}
advance:
- x += glyph->advance;
+ origin_x += glyph->advance;
}
+
+ if (extents)
+ extents->advance = origin_x - x;
}
static void intel_flush(struct wld_drawable * drawable)
diff --git a/pixman.c b/pixman.c
@@ -63,7 +63,8 @@ static void pixman_copy_region(struct wld_drawable * src,
static void pixman_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length);
+ const char * text, int32_t length,
+ struct wld_extents * extents);
static void pixman_flush(struct wld_drawable * drawable);
static void pixman_destroy(struct wld_drawable * drawable);
@@ -209,7 +210,8 @@ static inline uint8_t reverse(uint8_t byte)
static void pixman_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length)
+ const char * text, int32_t length,
+ struct wld_extents * extents)
{
struct pixman_drawable * pixman = (void *) drawable;
int ret;
@@ -217,7 +219,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
struct glyph * glyph;
FT_UInt glyph_index;
pixman_glyph_t glyphs[strlen(text)];
- uint32_t index = 0, glyph_x = 0;
+ uint32_t index = 0, origin_x = 0;
pixman_color_t pixman_color = PIXMAN_COLOR(color);
pixman_image_t * solid;
@@ -234,7 +236,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
glyph = font->glyphs[glyph_index];
- glyphs[index].x = glyph_x;
+ glyphs[index].x = origin_x;
glyphs[index].y = 0;
glyphs[index].glyph = pixman_glyph_cache_lookup
(pixman->context->glyph_cache, font, glyph);
@@ -285,7 +287,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
++index;
advance:
- glyph_x += glyph->advance;
+ origin_x += glyph->advance;
}
pixman_composite_glyphs_no_mask(PIXMAN_OP_OVER, solid, pixman->image, 0, 0,
@@ -293,6 +295,9 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
index, glyphs);
pixman_image_unref(solid);
+
+ if (extents)
+ extents->advance = origin_x;
}
static void pixman_flush(struct wld_drawable * drawable)
diff --git a/wayland.c b/wayland.c
@@ -79,7 +79,8 @@ static void wayland_copy_region(struct wld_drawable * src,
static void wayland_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length);
+ const char * text, int32_t length,
+ struct wld_extents * extents);
static void wayland_flush(struct wld_drawable * drawable);
static void wayland_destroy(struct wld_drawable * drawable);
@@ -333,13 +334,14 @@ static void wayland_copy_region(struct wld_drawable * src_drawable,
static void wayland_draw_text_utf8(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length)
+ const char * text, int32_t length,
+ struct wld_extents * extents)
{
struct wayland_drawable * wayland = (void *) drawable;
begin(wayland);
wld_draw_text_utf8_n(BACKBUF(wayland).drawable, &font->base, color,
- x, y, text, length);
+ x, y, text, length, extents);
}
static void wayland_flush(struct wld_drawable * drawable)
diff --git a/wld-private.h b/wld-private.h
@@ -94,7 +94,8 @@ struct wld_draw_interface
void (* draw_text_utf8)(struct wld_drawable * drawable,
struct font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length);
+ const char * text, int32_t length,
+ struct wld_extents * extents);
void (* flush)(struct wld_drawable * drawable);
void (* destroy)(struct wld_drawable * drawable);
};
diff --git a/wld.h b/wld.h
@@ -137,17 +137,26 @@ void wld_copy_rectangle(struct wld_drawable * src, struct wld_drawable * dst,
void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
pixman_region32_t * region, int32_t dst_x, int32_t dst_y);
+/**
+ * Draw a UTF-8 text string to the given drawable.
+ *
+ * @param length The maximum number of bytes in the string to process
+ * @param extents If not NULL, will be initialized to the extents of the
+ * drawn text
+ */
void wld_draw_text_utf8_n(struct wld_drawable * drawable,
struct wld_font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text, int32_t length);
+ const char * text, int32_t length,
+ struct wld_extents * extents);
static inline void wld_draw_text_utf8(struct wld_drawable * drawable,
struct wld_font * font, uint32_t color,
int32_t x, int32_t y,
- const char * text)
+ const char * text,
+ struct wld_extents * extents)
{
- wld_draw_text_utf8_n(drawable, font, color, x, y, text, INT32_MAX);
+ wld_draw_text_utf8_n(drawable, font, color, x, y, text, INT32_MAX, extents);
}
void wld_flush(struct wld_drawable * drawable);