neuwld

patched neuwld library (tracked)
git clone https://git.daat.foo/neuwld.git
Log | Files | Refs | README | LICENSE

commit 72be3d251c931b1fb1be704859efee3b36ceb6c8
parent 6873478b8367768430216c40b21c85fb27e30408
Author: cowmonk <cowmonk@based.pt>
Date:   Tue, 24 Nov 2026 14:04:20 -0700

apply refactored MICE wld patch

Diffstat:
Mfont.c | 20++++++++++++++++++--
Mintel.c | 2+-
Mnouveau.c | 2+-
Dpatches/glyphByName.patch | 60------------------------------------------------------------
Dpatches/inkBoxInTextExtents.patch | 76----------------------------------------------------------------------------
Dpatches/pixmanClipFillsToTarget.patch | 39---------------------------------------
Dpatches/setClipRectangle.patch | 160-------------------------------------------------------------------------------
Apatches/wld.patch | 396+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpixman.c | 2+-
Mrenderer.c | 22++++++++++++++++++++++
Mwld-private.h | 9+++++++++
Mwld.h | 9+++++++++
12 files changed, 457 insertions(+), 340 deletions(-)

diff --git a/font.c b/font.c @@ -24,6 +24,7 @@ #include "wld-private.h" #include <fontconfig/fcfreetype.h> +#include <string.h> EXPORT struct wld_font_context * @@ -94,6 +95,7 @@ wld_font_open_pattern(struct wld_font_context *context, FcPattern *match) goto error0; font->context = context; + memset(font->chars, 0, sizeof font->chars); result = FcPatternGetString(match, FC_FILE, 0, (FcChar8 **)&filename); @@ -160,6 +162,20 @@ wld_font_close(struct wld_font *font_base) free(font); } +FT_UInt +font_char_index(struct font *font, uint32_t character) +{ + unsigned slot = character % FONT_CHAR_CACHE; + + /* the key is the character plus one, so an empty slot (0) matches + * nothing and a character the face lacks is remembered too */ + if (font->chars[slot].key != character + 1) { + font->chars[slot].key = character + 1; + font->chars[slot].index = FT_Get_Char_Index(font->face, character); + } + return font->chars[slot].index; +} + bool font_ensure_glyph(struct font *font, FT_UInt glyph_index) { @@ -200,7 +216,7 @@ wld_font_ensure_char(struct wld_font *font_base, uint32_t character) struct font *font = (void *)font_base; FT_UInt glyph_index; - glyph_index = FT_Get_Char_Index(font->face, character); + glyph_index = font_char_index(font, character); return font_ensure_glyph(font, glyph_index); } @@ -244,7 +260,7 @@ wld_font_text_extents_n(struct wld_font *font_base, while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { length -= ret; text += ret; - glyph_index = FT_Get_Char_Index(font->face, c); + glyph_index = font_char_index(font, c); if (!font_ensure_glyph(font, glyph_index)) continue; diff --git a/intel.c b/intel.c @@ -316,7 +316,7 @@ renderer_draw_text(struct wld_renderer *base, while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { text += ret; length -= ret; - glyph_index = FT_Get_Char_Index(font->face, c); + glyph_index = font_char_index(font, c); if (!font_ensure_glyph(font, glyph_index)) continue; diff --git a/nouveau.c b/nouveau.c @@ -599,7 +599,7 @@ renderer_draw_text(struct wld_renderer *base, while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { text += ret; length -= ret; - glyph_index = FT_Get_Char_Index(font->face, c); + glyph_index = font_char_index(font, c); if (!font_ensure_glyph(font, glyph_index)) continue; diff --git a/patches/glyphByName.patch b/patches/glyphByName.patch @@ -1,60 +0,0 @@ -From 330cd44b2c2b785a41ec5022a47c911ed0b91491 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Mon, 16 Nov 2026 13:26:14 -0300 -Subject: [PATCH 3/4] font: find a glyph by name - ---- - font.c | 20 ++++++++++++++++++++ - wld.h | 5 +++++ - 2 files changed, 25 insertions(+) - -diff --git a/font.c b/font.c -index 5ff011a..0cf6ae3 100644 ---- a/font.c -+++ b/font.c -@@ -205,6 +205,26 @@ wld_font_ensure_char(struct wld_font *font_base, uint32_t character) - return font_ensure_glyph(font, glyph_index); - } - -+EXPORT -+uint32_t -+wld_font_char_by_name(struct wld_font *font_base, const char *name) -+{ -+ struct font *font = (void *)font_base; -+ FT_UInt glyph_index, index; -+ FT_ULong c; -+ -+ if (!FT_HAS_GLYPH_NAMES(font->face)) -+ return 0; -+ glyph_index = FT_Get_Name_Index(font->face, (FT_String *)name); -+ if (!glyph_index) -+ return 0; -+ for (c = FT_Get_First_Char(font->face, &index); index; c = FT_Get_Next_Char(font->face, c, &index)) { -+ if (index == glyph_index) -+ return c; -+ } -+ return 0; -+} -+ - EXPORT - void - wld_font_text_extents_n(struct wld_font *font_base, -diff --git a/wld.h b/wld.h -index 47ef77c..887e9b4 100644 ---- a/wld.h -+++ b/wld.h -@@ -140,6 +140,11 @@ void wld_font_close(struct wld_font *font); - */ - bool wld_font_ensure_char(struct wld_font *font, uint32_t character); - -+/** -+ * The character that draws the glyph called name, or 0. -+ */ -+uint32_t wld_font_char_by_name(struct wld_font *font, const char *name); -+ - /** - * Calculate the text extents of the given UTF-8 string. - * --- -2.56.1 - diff --git a/patches/inkBoxInTextExtents.patch b/patches/inkBoxInTextExtents.patch @@ -1,76 +0,0 @@ -From abc6ef868423b46260a5c749ed34c51e679ab828 Mon Sep 17 00:00:00 2001 -From: xplshn <noreply@localhost> -Date: Mon, 16 Nov 2026 10:39:25 -0300 -Subject: [PATCH 2/4] font: ink box in text extents - ---- - font.c | 28 +++++++++++++++++++++++++++- - wld.h | 7 +++++++ - 2 files changed, 34 insertions(+), 1 deletion(-) - -diff --git a/font.c b/font.c -index cbb187c..5ff011a 100644 ---- a/font.c -+++ b/font.c -@@ -216,6 +216,9 @@ wld_font_text_extents_n(struct wld_font *font_base, - uint32_t c; - FT_UInt glyph_index; - -+ struct glyph *glyph; -+ int32_t x1 = INT32_MAX, y1 = INT32_MAX, x2 = INT32_MIN, y2 = INT32_MIN; -+ - extents->advance = 0; - - while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { -@@ -226,6 +229,29 @@ wld_font_text_extents_n(struct wld_font *font_base, - if (!font_ensure_glyph(font, glyph_index)) - continue; - -- extents->advance += font->glyphs[glyph_index]->advance; -+ glyph = font->glyphs[glyph_index]; -+ if (glyph->bitmap.width && glyph->bitmap.rows) { -+ int32_t gx = (int32_t)extents->advance + glyph->x; -+ -+ if (gx < x1) -+ x1 = gx; -+ if (glyph->y < y1) -+ y1 = glyph->y; -+ if (gx + (int32_t)glyph->bitmap.width > x2) -+ x2 = gx + glyph->bitmap.width; -+ if (glyph->y + (int32_t)glyph->bitmap.rows > y2) -+ y2 = glyph->y + glyph->bitmap.rows; -+ } -+ extents->advance += glyph->advance; -+ } -+ -+ if (x1 > x2) { -+ extents->ink_x = extents->ink_y = 0; -+ extents->ink_width = extents->ink_height = 0; -+ return; - } -+ extents->ink_x = x1; -+ extents->ink_y = y1; -+ extents->ink_width = x2 - x1; -+ extents->ink_height = y2 - y1; - } -diff --git a/wld.h b/wld.h -index 8191ae3..47ef77c 100644 ---- a/wld.h -+++ b/wld.h -@@ -90,6 +90,13 @@ void wld_destroy_context(struct wld_context *context); - - struct wld_extents { - uint32_t advance; -+ -+ /** -+ * The box the glyphs cover, from the pen origin on the baseline. -+ * Only wld_font_text_extents sets it. -+ */ -+ int32_t ink_x, ink_y; -+ uint32_t ink_width, ink_height; - }; - - struct wld_font { --- -2.56.1 - diff --git a/patches/pixmanClipFillsToTarget.patch b/patches/pixmanClipFillsToTarget.patch @@ -1,39 +0,0 @@ -From 67dc537b00c583a71b64b86f3ca2fdbf2c233a0c Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:49:18 -0300 -Subject: [PATCH 1/4] pixman: clip fills to the target - -A fill past the buffer edge wrote out of bounds. ---- - pixman.c | 15 +++++++++++++-- - 1 file changed, 13 insertions(+), 2 deletions(-) - -diff --git a/pixman.c b/pixman.c -index ed7788f..afd8ae6 100644 ---- a/pixman.c -+++ b/pixman.c -@@ -258,8 +258,19 @@ renderer_set_target(struct wld_renderer *base, struct buffer *buffer) - if (renderer->target) - pixman_image_unref(renderer->target); - -- if (buffer) -- return (renderer->target = pixman_image(buffer)); -+ if (buffer) { -+ pixman_region32_t clip; -+ -+ if (!(renderer->target = pixman_image(buffer))) -+ return false; -+ -+ /* solid fills only clip against an explicit region */ -+ pixman_region32_init_rect(&clip, 0, 0, buffer->base.width, -+ buffer->base.height); -+ pixman_image_set_clip_region32(renderer->target, &clip); -+ pixman_region32_fini(&clip); -+ return true; -+ } - - renderer->target = NULL; - return true; --- -2.56.1 - diff --git a/patches/setClipRectangle.patch b/patches/setClipRectangle.patch @@ -1,160 +0,0 @@ -From dc00445a35f3369faaea6dbaef67b1b4357665a5 Mon Sep 17 00:00:00 2001 -From: xplshn <noreply@localhost> -Date: Mon, 16 Nov 2026 12:46:16 -0300 -Subject: [PATCH 4/4] renderer: add wld_set_clip - -Returns false when the renderer cant clip. ---- - interface/renderer.h | 7 +++++++ - pixman.c | 29 +++++++++++++++++++++++------ - renderer.c | 11 +++++++++++ - wld-private.h | 2 ++ - wld.h | 7 +++++++ - 5 files changed, 50 insertions(+), 6 deletions(-) - -diff --git a/interface/renderer.h b/interface/renderer.h -index ed34b3d..1c5e693 100644 ---- a/interface/renderer.h -+++ b/interface/renderer.h -@@ -47,6 +47,10 @@ static void renderer_draw_text(struct wld_renderer *renderer, - int32_t x, int32_t y, - const char *text, uint32_t length, - struct wld_extents *extents); -+#ifdef RENDERER_IMPLEMENTS_CLIP -+static void renderer_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, -+ uint32_t width, uint32_t height); -+#endif - static void renderer_flush(struct wld_renderer *renderer); - static void renderer_destroy(struct wld_renderer *renderer); - -@@ -65,6 +69,9 @@ static const struct wld_renderer_impl wld_renderer_impl = { - .draw_circle = &wld_draw_circle, - .draw_line = &wld_draw_line, - .draw_text = &renderer_draw_text, -+#ifdef RENDERER_IMPLEMENTS_CLIP -+ .set_clip = &renderer_set_clip, -+#endif - .flush = &renderer_flush, - .destroy = &renderer_destroy - }; -diff --git a/pixman.c b/pixman.c -index afd8ae6..2ebe613 100644 ---- a/pixman.c -+++ b/pixman.c -@@ -37,6 +37,7 @@ - struct pixman_renderer { - struct wld_renderer base; - pixman_image_t *target; -+ uint32_t target_width, target_height; - pixman_glyph_cache_t *glyph_cache; - }; - -@@ -53,6 +54,7 @@ struct pixman_map { - - #include "interface/context.h" - #define RENDERER_IMPLEMENTS_REGION -+#define RENDERER_IMPLEMENTS_CLIP - #include "interface/buffer.h" - #include "interface/renderer.h" - IMPL(pixman_renderer, wld_renderer) -@@ -259,16 +261,13 @@ renderer_set_target(struct wld_renderer *base, struct buffer *buffer) - pixman_image_unref(renderer->target); - - if (buffer) { -- pixman_region32_t clip; -- - if (!(renderer->target = pixman_image(buffer))) - return false; - - /* solid fills only clip against an explicit region */ -- pixman_region32_init_rect(&clip, 0, 0, buffer->base.width, -- buffer->base.height); -- pixman_image_set_clip_region32(renderer->target, &clip); -- pixman_region32_fini(&clip); -+ renderer->target_width = buffer->base.width; -+ renderer->target_height = buffer->base.height; -+ renderer_set_clip(base, 0, 0, buffer->base.width, buffer->base.height); - return true; - } - -@@ -276,6 +275,24 @@ renderer_set_target(struct wld_renderer *base, struct buffer *buffer) - return true; - } - -+void -+renderer_set_clip(struct wld_renderer *base, int32_t x, int32_t y, -+ uint32_t width, uint32_t height) -+{ -+ struct pixman_renderer *renderer = pixman_renderer(base); -+ pixman_region32_t clip, bounds; -+ -+ if (!renderer->target) -+ return; -+ pixman_region32_init_rect(&bounds, 0, 0, renderer->target_width, -+ renderer->target_height); -+ pixman_region32_init_rect(&clip, x, y, width, height); -+ pixman_region32_intersect(&clip, &clip, &bounds); -+ pixman_image_set_clip_region32(renderer->target, &clip); -+ pixman_region32_fini(&clip); -+ pixman_region32_fini(&bounds); -+} -+ - void - renderer_fill_rectangle(struct wld_renderer *base, uint32_t color, - int32_t x, int32_t y, -diff --git a/renderer.c b/renderer.c -index 2f49bc8..7315a82 100644 ---- a/renderer.c -+++ b/renderer.c -@@ -286,6 +286,17 @@ wld_draw_text(struct wld_renderer *renderer, - extents); - } - -+EXPORT -+bool -+wld_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, -+ uint32_t width, uint32_t height) -+{ -+ if (!renderer->impl->set_clip) -+ return false; -+ renderer->impl->set_clip(renderer, x, y, width, height); -+ return true; -+} -+ - EXPORT - void - wld_flush(struct wld_renderer *renderer) -diff --git a/wld-private.h b/wld-private.h -index 5a02630..9ab1115 100644 ---- a/wld-private.h -+++ b/wld-private.h -@@ -116,6 +116,8 @@ struct wld_renderer_impl { - struct font *font, uint32_t color, - int32_t x, int32_t y, const char *text, uint32_t length, - struct wld_extents *extents); -+ void (*set_clip)(struct wld_renderer *renderer, int32_t x, int32_t y, -+ uint32_t width, uint32_t height); - void (*flush)(struct wld_renderer *renderer); - void (*destroy)(struct wld_renderer *renderer); - }; -diff --git a/wld.h b/wld.h -index 887e9b4..0e4d29a 100644 ---- a/wld.h -+++ b/wld.h -@@ -284,6 +284,13 @@ void wld_draw_line(struct wld_renderer *renderer, uint32_t color, - * @param extents If not NULL, will be initialized to the extents of the - * drawn text - */ -+/** -+ * Limit drawing to a rectangle of the target. Setting a target resets it. -+ * Returns false when the renderer cannot clip. -+ */ -+bool wld_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, -+ uint32_t width, uint32_t height); -+ - void wld_draw_text(struct wld_renderer *renderer, - struct wld_font *font, uint32_t color, - int32_t x, int32_t y, const char *text, uint32_t length, --- -2.56.1 - diff --git a/patches/wld.patch b/patches/wld.patch @@ -0,0 +1,396 @@ +Subject: clips, fonts, the target for the CPU + +diff --git a/font.c b/font.c +index cbb187c..c492dff 100644 +--- a/font.c ++++ b/font.c +@@ -24,6 +24,7 @@ + #include "wld-private.h" + + #include <fontconfig/fcfreetype.h> ++#include <string.h> + + EXPORT + struct wld_font_context * +@@ -94,6 +95,7 @@ wld_font_open_pattern(struct wld_font_context *context, FcPattern *match) + goto error0; + + font->context = context; ++ memset(font->chars, 0, sizeof font->chars); + + result = FcPatternGetString(match, FC_FILE, 0, (FcChar8 **)&filename); + +@@ -160,6 +162,20 @@ wld_font_close(struct wld_font *font_base) + free(font); + } + ++FT_UInt ++font_char_index(struct font *font, uint32_t character) ++{ ++ unsigned slot = character % FONT_CHAR_CACHE; ++ ++ /* the key is the character plus one, so an empty slot (0) matches ++ * nothing and a character the face lacks is remembered too */ ++ if (font->chars[slot].key != character + 1) { ++ font->chars[slot].key = character + 1; ++ font->chars[slot].index = FT_Get_Char_Index(font->face, character); ++ } ++ return font->chars[slot].index; ++} ++ + bool + font_ensure_glyph(struct font *font, FT_UInt glyph_index) + { +@@ -200,11 +216,31 @@ wld_font_ensure_char(struct wld_font *font_base, uint32_t character) + struct font *font = (void *)font_base; + FT_UInt glyph_index; + +- glyph_index = FT_Get_Char_Index(font->face, character); ++ glyph_index = font_char_index(font, character); + + return font_ensure_glyph(font, glyph_index); + } + ++EXPORT ++uint32_t ++wld_font_char_by_name(struct wld_font *font_base, const char *name) ++{ ++ struct font *font = (void *)font_base; ++ FT_UInt glyph_index, index; ++ FT_ULong c; ++ ++ if (!FT_HAS_GLYPH_NAMES(font->face)) ++ return 0; ++ glyph_index = FT_Get_Name_Index(font->face, (FT_String *)name); ++ if (!glyph_index) ++ return 0; ++ for (c = FT_Get_First_Char(font->face, &index); index; c = FT_Get_Next_Char(font->face, c, &index)) { ++ if (index == glyph_index) ++ return c; ++ } ++ return 0; ++} ++ + EXPORT + void + wld_font_text_extents_n(struct wld_font *font_base, +@@ -216,16 +252,42 @@ wld_font_text_extents_n(struct wld_font *font_base, + uint32_t c; + FT_UInt glyph_index; + ++ struct glyph *glyph; ++ int32_t x1 = INT32_MAX, y1 = INT32_MAX, x2 = INT32_MIN, y2 = INT32_MIN; ++ + extents->advance = 0; + + while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { + length -= ret; + text += ret; +- glyph_index = FT_Get_Char_Index(font->face, c); ++ glyph_index = font_char_index(font, c); + + if (!font_ensure_glyph(font, glyph_index)) + continue; + +- extents->advance += font->glyphs[glyph_index]->advance; ++ glyph = font->glyphs[glyph_index]; ++ if (glyph->bitmap.width && glyph->bitmap.rows) { ++ int32_t gx = (int32_t)extents->advance + glyph->x; ++ ++ if (gx < x1) ++ x1 = gx; ++ if (glyph->y < y1) ++ y1 = glyph->y; ++ if (gx + (int32_t)glyph->bitmap.width > x2) ++ x2 = gx + glyph->bitmap.width; ++ if (glyph->y + (int32_t)glyph->bitmap.rows > y2) ++ y2 = glyph->y + glyph->bitmap.rows; ++ } ++ extents->advance += glyph->advance; ++ } ++ ++ if (x1 > x2) { ++ extents->ink_x = extents->ink_y = 0; ++ extents->ink_width = extents->ink_height = 0; ++ return; + } ++ extents->ink_x = x1; ++ extents->ink_y = y1; ++ extents->ink_width = x2 - x1; ++ extents->ink_height = y2 - y1; + } +diff --git a/intel.c b/intel.c +index 8cf0a21..0cb0973 100644 +--- a/intel.c ++++ b/intel.c +@@ -316,7 +316,7 @@ renderer_draw_text(struct wld_renderer *base, + while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { + text += ret; + length -= ret; +- glyph_index = FT_Get_Char_Index(font->face, c); ++ glyph_index = font_char_index(font, c); + + if (!font_ensure_glyph(font, glyph_index)) + continue; +diff --git a/interface/renderer.h b/interface/renderer.h +index ed34b3d..1c5e693 100644 +--- a/interface/renderer.h ++++ b/interface/renderer.h +@@ -47,6 +47,10 @@ static void renderer_draw_text(struct wld_renderer *renderer, + int32_t x, int32_t y, + const char *text, uint32_t length, + struct wld_extents *extents); ++#ifdef RENDERER_IMPLEMENTS_CLIP ++static void renderer_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, ++ uint32_t width, uint32_t height); ++#endif + static void renderer_flush(struct wld_renderer *renderer); + static void renderer_destroy(struct wld_renderer *renderer); + +@@ -65,6 +69,9 @@ static const struct wld_renderer_impl wld_renderer_impl = { + .draw_circle = &wld_draw_circle, + .draw_line = &wld_draw_line, + .draw_text = &renderer_draw_text, ++#ifdef RENDERER_IMPLEMENTS_CLIP ++ .set_clip = &renderer_set_clip, ++#endif + .flush = &renderer_flush, + .destroy = &renderer_destroy + }; +diff --git a/nouveau.c b/nouveau.c +index 6bd7ab1..77f69db 100644 +--- a/nouveau.c ++++ b/nouveau.c +@@ -599,7 +599,7 @@ renderer_draw_text(struct wld_renderer *base, + while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { + text += ret; + length -= ret; +- glyph_index = FT_Get_Char_Index(font->face, c); ++ glyph_index = font_char_index(font, c); + + if (!font_ensure_glyph(font, glyph_index)) + continue; +diff --git a/pixman.c b/pixman.c +index ed7788f..824ba15 100644 +--- a/pixman.c ++++ b/pixman.c +@@ -37,6 +37,7 @@ + struct pixman_renderer { + struct wld_renderer base; + pixman_image_t *target; ++ uint32_t target_width, target_height; + pixman_glyph_cache_t *glyph_cache; + }; + +@@ -53,6 +54,7 @@ struct pixman_map { + + #include "interface/context.h" + #define RENDERER_IMPLEMENTS_REGION ++#define RENDERER_IMPLEMENTS_CLIP + #include "interface/buffer.h" + #include "interface/renderer.h" + IMPL(pixman_renderer, wld_renderer) +@@ -258,13 +260,39 @@ renderer_set_target(struct wld_renderer *base, struct buffer *buffer) + if (renderer->target) + pixman_image_unref(renderer->target); + +- if (buffer) +- return (renderer->target = pixman_image(buffer)); ++ if (buffer) { ++ if (!(renderer->target = pixman_image(buffer))) ++ return false; ++ ++ /* solid fills only clip against an explicit region */ ++ renderer->target_width = buffer->base.width; ++ renderer->target_height = buffer->base.height; ++ renderer_set_clip(base, 0, 0, buffer->base.width, buffer->base.height); ++ return true; ++ } + + renderer->target = NULL; + return true; + } + ++void ++renderer_set_clip(struct wld_renderer *base, int32_t x, int32_t y, ++ uint32_t width, uint32_t height) ++{ ++ struct pixman_renderer *renderer = pixman_renderer(base); ++ pixman_region32_t clip, bounds; ++ ++ if (!renderer->target) ++ return; ++ pixman_region32_init_rect(&bounds, 0, 0, renderer->target_width, ++ renderer->target_height); ++ pixman_region32_init_rect(&clip, x, y, width, height); ++ pixman_region32_intersect(&clip, &clip, &bounds); ++ pixman_image_set_clip_region32(renderer->target, &clip); ++ pixman_region32_fini(&clip); ++ pixman_region32_fini(&bounds); ++} ++ + void + renderer_fill_rectangle(struct wld_renderer *base, uint32_t color, + int32_t x, int32_t y, +@@ -426,7 +454,7 @@ renderer_draw_text(struct wld_renderer *base, + while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { + text += ret; + length -= ret; +- glyph_index = FT_Get_Char_Index(font->face, c); ++ glyph_index = font_char_index(font, c); + + if (!font_ensure_glyph(font, glyph_index)) + continue; +diff --git a/renderer.c b/renderer.c +index 2f49bc8..c1c17ff 100644 +--- a/renderer.c ++++ b/renderer.c +@@ -195,6 +195,28 @@ unmap_src: + wld_unmap(buffer); + } + ++EXPORT ++void * ++wld_target_map(struct wld_renderer *renderer, uint32_t *pitch, uint32_t *format) ++{ ++ if (!renderer->target) ++ return NULL; ++ /* accelerator writes land before the CPU looks */ ++ renderer->impl->flush(renderer); ++ if (!wld_map(renderer->target)) ++ return NULL; ++ *pitch = renderer->target->pitch; ++ *format = renderer->target->format; ++ return renderer->target->map; ++} ++ ++EXPORT ++void ++wld_target_unmap(struct wld_renderer *renderer) ++{ ++ if (renderer->target) ++ wld_unmap(renderer->target); ++} + + /* https://en.wikipedia.org/wiki/Midpoint_circle_algorithm */ + static void +@@ -286,6 +308,17 @@ wld_draw_text(struct wld_renderer *renderer, + extents); + } + ++EXPORT ++bool ++wld_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, ++ uint32_t width, uint32_t height) ++{ ++ if (!renderer->impl->set_clip) ++ return false; ++ renderer->impl->set_clip(renderer, x, y, width, height); ++ return true; ++} ++ + EXPORT + void + wld_flush(struct wld_renderer *renderer) +diff --git a/wld-private.h b/wld-private.h +index 5a02630..87189aa 100644 +--- a/wld-private.h ++++ b/wld-private.h +@@ -69,12 +69,20 @@ struct glyph { + uint16_t advance; + }; + ++/* glyph indices of recently used characters, indexed by the character's ++ * low bits: FT_Get_Char_Index searches the face's cmap on every call */ ++#define FONT_CHAR_CACHE 256 ++ + struct font { + struct wld_font base; + + struct wld_font_context *context; + FT_Face face; + struct glyph **glyphs; ++ struct { ++ uint32_t key; ++ FT_UInt index; ++ } chars[FONT_CHAR_CACHE]; + }; + + struct wld_context_impl { +@@ -116,6 +124,8 @@ struct wld_renderer_impl { + struct font *font, uint32_t color, + int32_t x, int32_t y, const char *text, uint32_t length, + struct wld_extents *extents); ++ void (*set_clip)(struct wld_renderer *renderer, int32_t x, int32_t y, ++ uint32_t width, uint32_t height); + void (*flush)(struct wld_renderer *renderer); + void (*destroy)(struct wld_renderer *renderer); + }; +@@ -156,6 +166,7 @@ struct buffer_socket_impl { + }; + + bool font_ensure_glyph(struct font *font, FT_UInt glyph_index); ++FT_UInt font_char_index(struct font *font, uint32_t character); + + /** + * Returns the number of bytes per pixel for the given format. +diff --git a/wld.h b/wld.h +index 8191ae3..b0ecf85 100644 +--- a/wld.h ++++ b/wld.h +@@ -90,6 +90,13 @@ void wld_destroy_context(struct wld_context *context); + + struct wld_extents { + uint32_t advance; ++ ++ /** ++ * The box the glyphs cover, from the pen origin on the baseline. ++ * Only wld_font_text_extents sets it. ++ */ ++ int32_t ink_x, ink_y; ++ uint32_t ink_width, ink_height; + }; + + struct wld_font { +@@ -133,6 +140,11 @@ void wld_font_close(struct wld_font *font); + */ + bool wld_font_ensure_char(struct wld_font *font, uint32_t character); + ++/** ++ * The character that draws the glyph called name, or 0. ++ */ ++uint32_t wld_font_char_by_name(struct wld_font *font, const char *name); ++ + /** + * Calculate the text extents of the given UTF-8 string. + * +@@ -258,6 +270,15 @@ void wld_blend_region(struct wld_renderer *renderer, + int32_t dst_x, int32_t dst_y, + pixman_region32_t *region); + ++/** ++ * The pixels of the renderer's target, for the CPU to read and write: what ++ * has been drawn so far, after every pending operation has finished. NULL ++ * when the target cannot be mapped. Unmap it before drawing again. ++ */ ++void *wld_target_map(struct wld_renderer *renderer, uint32_t *pitch, ++ uint32_t *format); ++void wld_target_unmap(struct wld_renderer *renderer); ++ + void wld_draw_circle(struct wld_renderer *renderer, uint32_t color, + int32_t x, int32_t y, uint32_t r, bool fill); + +@@ -272,6 +293,13 @@ void wld_draw_line(struct wld_renderer *renderer, uint32_t color, + * @param extents If not NULL, will be initialized to the extents of the + * drawn text + */ ++/** ++ * Limit drawing to a rectangle of the target. Setting a target resets it. ++ * Returns false when the renderer cannot clip. ++ */ ++bool wld_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, ++ uint32_t width, uint32_t height); ++ + void wld_draw_text(struct wld_renderer *renderer, + struct wld_font *font, uint32_t color, + int32_t x, int32_t y, const char *text, uint32_t length, diff --git a/pixman.c b/pixman.c @@ -454,7 +454,7 @@ renderer_draw_text(struct wld_renderer *base, while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') { text += ret; length -= ret; - glyph_index = FT_Get_Char_Index(font->face, c); + glyph_index = font_char_index(font, c); if (!font_ensure_glyph(font, glyph_index)) continue; diff --git a/renderer.c b/renderer.c @@ -195,6 +195,28 @@ unmap_src: wld_unmap(buffer); } +EXPORT +void * +wld_target_map(struct wld_renderer *renderer, uint32_t *pitch, uint32_t *format) +{ + if (!renderer->target) + return NULL; + /* accelerator writes land before the CPU looks */ + renderer->impl->flush(renderer); + if (!wld_map(renderer->target)) + return NULL; + *pitch = renderer->target->pitch; + *format = renderer->target->format; + return renderer->target->map; +} + +EXPORT +void +wld_target_unmap(struct wld_renderer *renderer) +{ + if (renderer->target) + wld_unmap(renderer->target); +} /* https://en.wikipedia.org/wiki/Midpoint_circle_algorithm */ static void diff --git a/wld-private.h b/wld-private.h @@ -69,12 +69,20 @@ struct glyph { uint16_t advance; }; +/* glyph indices of recently used characters, indexed by the character's + * low bits: FT_Get_Char_Index searches the face's cmap on every call */ +#define FONT_CHAR_CACHE 256 + struct font { struct wld_font base; struct wld_font_context *context; FT_Face face; struct glyph **glyphs; + struct { + uint32_t key; + FT_UInt index; + } chars[FONT_CHAR_CACHE]; }; struct wld_context_impl { @@ -158,6 +166,7 @@ struct buffer_socket_impl { }; bool font_ensure_glyph(struct font *font, FT_UInt glyph_index); +FT_UInt font_char_index(struct font *font, uint32_t character); /** * Returns the number of bytes per pixel for the given format. diff --git a/wld.h b/wld.h @@ -270,6 +270,15 @@ void wld_blend_region(struct wld_renderer *renderer, int32_t dst_x, int32_t dst_y, pixman_region32_t *region); +/** + * The pixels of the renderer's target, for the CPU to read and write: what + * has been drawn so far, after every pending operation has finished. NULL + * when the target cannot be mapped. Unmap it before drawing again. + */ +void *wld_target_map(struct wld_renderer *renderer, uint32_t *pitch, + uint32_t *format); +void wld_target_unmap(struct wld_renderer *renderer); + void wld_draw_circle(struct wld_renderer *renderer, uint32_t color, int32_t x, int32_t y, uint32_t r, bool fill);