commit a17abf7

Michael Forney  ·  2013-07-25 07:46:59 +0000 UTC
parent b4bcdad
draw_text_utf8: Optionally return extents
6 files changed,  +43, -19
M wld.h
+3, -2
 1@@ -92,12 +92,13 @@ void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
 2 void wld_draw_text_utf8_n(struct wld_drawable * drawable,
 3                           struct wld_font * font_base, uint32_t color,
 4                           int32_t x, int32_t y,
 5-                          const char * text, int32_t length)
 6+                          const char * text, int32_t length,
 7+                          struct wld_extents * extents)
 8 {
 9     struct font * font = (void *) font_base;
10 
11     drawable->interface->draw_text_utf8(drawable, font, color, x, y,
12-                                        text, length);
13+                                        text, length, extents);
14 }
15 
16 void wld_destroy_drawable(struct wld_drawable * drawable)
+11, -5
 1@@ -60,7 +60,8 @@ static void intel_copy_rectangle(struct wld_drawable * src,
 2 static void intel_draw_text_utf8(struct wld_drawable * drawable,
 3                                  struct font * font, uint32_t color,
 4                                  int32_t x, int32_t y,
 5-                                 const char * text, int32_t length);
 6+                                 const char * text, int32_t length,
 7+                                 struct wld_extents * extents);
 8 static void intel_flush(struct wld_drawable * drawable);
 9 static void intel_destroy(struct wld_drawable * drawable);
10 
11@@ -158,7 +159,8 @@ static void intel_copy_rectangle(struct wld_drawable * src_drawable,
12 static void intel_draw_text_utf8(struct wld_drawable * drawable,
13                                  struct font * font, uint32_t color,
14                                  int32_t x, int32_t y,
15-                                 const char * text, int32_t length)
16+                                 const char * text, int32_t length,
17+                                 struct wld_extents * extents)
18 {
19     struct intel_drawable * intel = (void *) drawable;
20     int ret;
21@@ -169,6 +171,7 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
22     uint8_t immediate[512];
23     uint8_t * byte;
24     uint8_t byte_index;
25+    int32_t origin_x = x;
26 
27     xy_setup_blt(&intel->context->batch, true, INTEL_BLT_RASTER_OPERATION_SRC,
28                  0, color, intel->bo, intel->drm.pitch);
29@@ -199,8 +202,8 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
30 
31       retry:
32         ret = xy_text_immediate_blt(&intel->context->batch, intel->bo,
33-                                    x + glyph->x, y + glyph->y,
34-                                    x + glyph->x + glyph->bitmap.width,
35+                                    origin_x + glyph->x, y + glyph->y,
36+                                    origin_x + glyph->x + glyph->bitmap.width,
37                                     y + glyph->y + glyph->bitmap.rows,
38                                     (byte - immediate + 3) / 4,
39                                     (uint32_t *) immediate);
40@@ -215,8 +218,11 @@ static void intel_draw_text_utf8(struct wld_drawable * drawable,
41         }
42 
43       advance:
44-        x += glyph->advance;
45+        origin_x += glyph->advance;
46     }
47+
48+    if (extents)
49+        extents->advance = origin_x - x;
50 }
51 
52 static void intel_flush(struct wld_drawable * drawable)
+10, -5
 1@@ -63,7 +63,8 @@ static void pixman_copy_region(struct wld_drawable * src,
 2 static void pixman_draw_text_utf8(struct wld_drawable * drawable,
 3                                   struct font * font, uint32_t color,
 4                                   int32_t x, int32_t y,
 5-                                  const char * text, int32_t length);
 6+                                  const char * text, int32_t length,
 7+                                  struct wld_extents * extents);
 8 static void pixman_flush(struct wld_drawable * drawable);
 9 static void pixman_destroy(struct wld_drawable * drawable);
10 
11@@ -209,7 +210,8 @@ static inline uint8_t reverse(uint8_t byte)
12 static void pixman_draw_text_utf8(struct wld_drawable * drawable,
13                                   struct font * font, uint32_t color,
14                                   int32_t x, int32_t y,
15-                                  const char * text, int32_t length)
16+                                  const char * text, int32_t length,
17+                                  struct wld_extents * extents)
18 {
19     struct pixman_drawable * pixman = (void *) drawable;
20     int ret;
21@@ -217,7 +219,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
22     struct glyph * glyph;
23     FT_UInt glyph_index;
24     pixman_glyph_t glyphs[strlen(text)];
25-    uint32_t index = 0, glyph_x = 0;
26+    uint32_t index = 0, origin_x = 0;
27     pixman_color_t pixman_color = PIXMAN_COLOR(color);
28     pixman_image_t * solid;
29 
30@@ -234,7 +236,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
31 
32         glyph = font->glyphs[glyph_index];
33 
34-        glyphs[index].x = glyph_x;
35+        glyphs[index].x = origin_x;
36         glyphs[index].y = 0;
37         glyphs[index].glyph = pixman_glyph_cache_lookup
38             (pixman->context->glyph_cache, font, glyph);
39@@ -285,7 +287,7 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
40         ++index;
41 
42       advance:
43-        glyph_x += glyph->advance;
44+        origin_x += glyph->advance;
45     }
46 
47     pixman_composite_glyphs_no_mask(PIXMAN_OP_OVER, solid, pixman->image, 0, 0,
48@@ -293,6 +295,9 @@ static void pixman_draw_text_utf8(struct wld_drawable * drawable,
49                                     index, glyphs);
50 
51     pixman_image_unref(solid);
52+
53+    if (extents)
54+        extents->advance = origin_x;
55 }
56 
57 static void pixman_flush(struct wld_drawable * drawable)
+5, -3
 1@@ -79,7 +79,8 @@ static void wayland_copy_region(struct wld_drawable * src,
 2 static void wayland_draw_text_utf8(struct wld_drawable * drawable,
 3                                    struct font * font, uint32_t color,
 4                                    int32_t x, int32_t y,
 5-                                   const char * text, int32_t length);
 6+                                   const char * text, int32_t length,
 7+                                   struct wld_extents * extents);
 8 static void wayland_flush(struct wld_drawable * drawable);
 9 static void wayland_destroy(struct wld_drawable * drawable);
10 
11@@ -333,13 +334,14 @@ static void wayland_copy_region(struct wld_drawable * src_drawable,
12 static void wayland_draw_text_utf8(struct wld_drawable * drawable,
13                                    struct font * font, uint32_t color,
14                                    int32_t x, int32_t y,
15-                                   const char * text, int32_t length)
16+                                   const char * text, int32_t length,
17+                                   struct wld_extents * extents)
18 {
19     struct wayland_drawable * wayland = (void *) drawable;
20 
21     begin(wayland);
22     wld_draw_text_utf8_n(BACKBUF(wayland).drawable, &font->base, color,
23-                         x, y, text, length);
24+                         x, y, text, length, extents);
25 }
26 
27 static void wayland_flush(struct wld_drawable * drawable)
+2, -1
 1@@ -94,7 +94,8 @@ struct wld_draw_interface
 2     void (* draw_text_utf8)(struct wld_drawable * drawable,
 3                             struct font * font, uint32_t color,
 4                             int32_t x, int32_t y,
 5-                            const char * text, int32_t length);
 6+                            const char * text, int32_t length,
 7+                            struct wld_extents * extents);
 8     void (* flush)(struct wld_drawable * drawable);
 9     void (* destroy)(struct wld_drawable * drawable);
10 };
M wld.h
+12, -3
 1@@ -137,17 +137,26 @@ void wld_copy_rectangle(struct wld_drawable * src, struct wld_drawable * dst,
 2 void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
 3                      pixman_region32_t * region, int32_t dst_x, int32_t dst_y);
 4 
 5+/**
 6+ * Draw a UTF-8 text string to the given drawable.
 7+ *
 8+ * @param length    The maximum number of bytes in the string to process
 9+ * @param extents   If not NULL, will be initialized to the extents of the
10+ *                  drawn text
11+ */
12 void wld_draw_text_utf8_n(struct wld_drawable * drawable,
13                           struct wld_font * font, uint32_t color,
14                           int32_t x, int32_t y,
15-                          const char * text, int32_t length);
16+                          const char * text, int32_t length,
17+                          struct wld_extents * extents);
18 
19 static inline void wld_draw_text_utf8(struct wld_drawable * drawable,
20                         struct wld_font * font, uint32_t color,
21                         int32_t x, int32_t y,
22-                        const char * text)
23+                        const char * text,
24+                        struct wld_extents * extents)
25 {
26-    wld_draw_text_utf8_n(drawable, font, color, x, y, text, INT32_MAX);
27+    wld_draw_text_utf8_n(drawable, font, color, x, y, text, INT32_MAX, extents);
28 }
29 
30 void wld_flush(struct wld_drawable * drawable);