commit 36158a8eed8df59a32c11554a2c6fda2510adb54
parent 712fa71be579f3796b3cd500de87c736a8617dd7
Author: cowmonk <cowmonk@based.pt>
Date: Sun, 22 Nov 2026 21:13:16 -0700
neuswc: apply MICE patches
QOL patches from mice that aren't upstreamed yet
Diffstat:
6 files changed, 116 insertions(+), 3 deletions(-)
diff --git a/font.c b/font.c
@@ -206,6 +206,26 @@ wld_font_ensure_char(struct wld_font *font_base, uint32_t character)
}
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,
const char *text, int32_t length,
@@ -216,6 +236,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 +249,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/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
@@ -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,14 +260,40 @@ 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,
uint32_t width, uint32_t height)
diff --git a/renderer.c b/renderer.c
@@ -287,6 +287,17 @@ wld_draw_text(struct wld_renderer *renderer,
}
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
@@ -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
@@ -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 {
@@ -134,6 +141,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.
*
* @param length The maximum number of bytes in the string to process
@@ -272,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,