commit 6dbcf1c163f86a933fb95cec363dfde2b44f355e
parent 72be3d251c931b1fb1be704859efee3b36ceb6c8
Author: cowmonk <cowmonk@based.pt>
Date: Wed, 2 Dec 2026 12:53:38 -0700
wld.patch: newest MICE patch
fonts open by file, fontconfig optional
Diffstat:
9 files changed, 419 insertions(+), 81 deletions(-)
diff --git a/font.c b/font.c
@@ -23,7 +23,9 @@
#include "wld-private.h"
+#ifdef WLD_FONTCONFIG
#include <fontconfig/fcfreetype.h>
+#endif
#include <string.h>
EXPORT
@@ -59,6 +61,62 @@ wld_font_destroy_context(struct wld_font_context *context)
free(context);
}
+/* A FreeType face put at a size: the font every way of opening one ends in. */
+static struct wld_font *
+open_face(struct wld_font_context *context, FT_Face face, double pixel_size,
+ double aspect)
+{
+ struct font *font;
+
+ font = malloc(sizeof *font);
+
+ if (!font) {
+ FT_Done_Face(face);
+ return NULL;
+ }
+
+ font->context = context;
+ font->face = face;
+ memset(font->chars, 0, sizeof font->chars);
+
+ if (font->face->face_flags & FT_FACE_FLAG_SCALABLE) {
+ FT_F26Dot6 width, height;
+
+ width = ((unsigned int)pixel_size) << 6;
+ height = ((unsigned int)(pixel_size * aspect)) << 6;
+
+ FT_Set_Char_Size(font->face, width, height, 0, 0);
+ } else {
+ FT_Set_Pixel_Sizes(font->face, (unsigned int)pixel_size,
+ (unsigned int)(pixel_size * aspect));
+ }
+
+ font->base.ascent = font->face->size->metrics.ascender >> 6;
+ font->base.descent = -font->face->size->metrics.descender >> 6;
+ font->base.height = font->base.ascent + font->base.descent;
+ font->base.max_advance = font->face->size->metrics.max_advance >> 6;
+
+ font->glyphs = calloc(font->face->num_glyphs, sizeof(struct glyph *));
+
+ return &font->base;
+}
+
+EXPORT
+struct wld_font *
+wld_font_open_file(struct wld_font_context *context, const char *path,
+ int index, double pixel_size)
+{
+ FT_Face face;
+
+ DEBUG("Loading font file: %s\n", path);
+
+ if (FT_New_Face(context->library, path, index, &face) != 0)
+ return NULL;
+
+ return open_face(context, face, pixel_size, 1.0);
+}
+
+#ifdef WLD_FONTCONFIG
EXPORT
struct wld_font *
wld_font_open_name(struct wld_font_context *context, const char *name)
@@ -85,72 +143,35 @@ struct wld_font *
wld_font_open_pattern(struct wld_font_context *context, FcPattern *match)
{
char *filename;
- struct font *font;
+ FT_Face face;
FcResult result;
- double pixel_size, aspect;
-
- font = malloc(sizeof *font);
-
- if (!font)
- goto error0;
-
- font->context = context;
- memset(font->chars, 0, sizeof font->chars);
+ double pixel_size = 12, aspect;
+ int index = 0;
result = FcPatternGetString(match, FC_FILE, 0, (FcChar8 **)&filename);
+ FcPatternGetInteger(match, FC_INDEX, 0, &index);
if (result == FcResultMatch) {
- FT_Error error;
-
DEBUG("Loading font file: %s\n", filename);
- error = FT_New_Face(context->library, filename, 0, &font->face);
-
- if (error == 0)
+ if (FT_New_Face(context->library, filename, index, &face) == 0)
goto load_face;
}
- result = FcPatternGetFTFace(match, FC_FT_FACE, 0, &font->face);
-
- if (result != FcResultMatch) {
+ if (FcPatternGetFTFace(match, FC_FT_FACE, 0, &face) != FcResultMatch) {
DEBUG("Couldn't determine font filename or FreeType face\n");
- goto error1;
+ return NULL;
}
load_face:
- result = FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixel_size);
-
- result = FcPatternGetDouble(match, FC_ASPECT, 0, &aspect);
+ FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixel_size);
- if (result == FcResultNoMatch)
+ if (FcPatternGetDouble(match, FC_ASPECT, 0, &aspect) != FcResultMatch)
aspect = 1.0;
- if (font->face->face_flags & FT_FACE_FLAG_SCALABLE) {
- FT_F26Dot6 width, height;
-
- width = ((unsigned int)pixel_size) << 6;
- height = ((unsigned int)(pixel_size * aspect)) << 6;
-
- FT_Set_Char_Size(font->face, width, height, 0, 0);
- } else {
- FT_Set_Pixel_Sizes(font->face, (unsigned int)pixel_size,
- (unsigned int)(pixel_size * aspect));
- }
-
- font->base.ascent = font->face->size->metrics.ascender >> 6;
- font->base.descent = -font->face->size->metrics.descender >> 6;
- font->base.height = font->base.ascent + font->base.descent;
- font->base.max_advance = font->face->size->metrics.max_advance >> 6;
-
- font->glyphs = calloc(font->face->num_glyphs, sizeof(struct glyph *));
-
- return &font->base;
-
-error1:
- free(font);
-error0:
- return NULL;
+ return open_face(context, face, pixel_size, aspect);
}
+#endif
EXPORT
void
@@ -257,7 +278,7 @@ wld_font_text_extents_n(struct wld_font *font_base,
extents->advance = 0;
- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
+ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
length -= ret;
text += ret;
glyph_index = font_char_index(font, c);
diff --git a/intel.c b/intel.c
@@ -313,7 +313,7 @@ renderer_draw_text(struct wld_renderer *base,
if (length == -1)
length = strlen(text);
- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
+ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
glyph_index = font_char_index(font, c);
diff --git a/meson.build b/meson.build
@@ -40,9 +40,17 @@ c_args = cc.get_supported_arguments([
]
requires = [
- dependency('fontconfig'),
dependency('pixman-1'),
]
+# fontconfig, where it is, finds fonts by name; without it a font is opened
+# from its file (wld_font_open_file), as a system without fontconfig has to
+fontconfig = dependency('fontconfig', required: get_option('fontconfig'))
+extra_cflags = []
+if fontconfig.found()
+ requires += fontconfig
+ c_args += '-DWLD_FONTCONFIG'
+ extra_cflags += '-DWLD_FONTCONFIG'
+endif
requires_private = [
dependency('freetype2'),
]
@@ -128,6 +136,7 @@ pkg.generate(
description: 'A primitive drawing library library targeted at Wayland',
requires: requires,
requires_private: requires_private,
+ extra_cflags: extra_cflags,
)
doxygen = find_program('doxygen', required: get_option('doxygen'))
diff --git a/meson.options b/meson.options
@@ -1,4 +1,5 @@
option('wayland', type: 'feature', value: 'auto', description: 'Wayland frontend')
option('drm', type: 'feature', value: 'auto', description: 'Enable libdrm backend')
option('drivers', type: 'array', choices: ['auto', 'intel', 'nouveau'], value: ['auto'], description: 'libdrm drivers')
+option('fontconfig', type: 'feature', value: 'auto', description: 'Find fonts by name through fontconfig')
option('doxygen', type: 'feature', value: 'auto', description: 'Build documentation')
diff --git a/nouveau.c b/nouveau.c
@@ -596,7 +596,7 @@ renderer_draw_text(struct wld_renderer *base,
if (length == -1)
length = strlen(text);
- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
+ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
glyph_index = font_char_index(font, c);
diff --git a/patches/wld.patch b/patches/wld.patch
@@ -1,26 +1,167 @@
-Subject: clips, fonts, the target for the CPU
+Subject: clips, fonts, the target for the CPU, fonts opened by file and fontconfig optional
diff --git a/font.c b/font.c
-index cbb187c..c492dff 100644
+index cbb187c..7c6b52f 100644
--- a/font.c
+++ b/font.c
-@@ -24,6 +24,7 @@
+@@ -23,7 +23,10 @@
+
#include "wld-private.h"
++#ifdef WLD_FONTCONFIG
#include <fontconfig/fcfreetype.h>
++#endif
+#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;
+@@ -58,6 +61,62 @@ wld_font_destroy_context(struct wld_font_context *context)
+ free(context);
+ }
- font->context = context;
++/* A FreeType face put at a size: the font every way of opening one ends in. */
++static struct wld_font *
++open_face(struct wld_font_context *context, FT_Face face, double pixel_size,
++ double aspect)
++{
++ struct font *font;
++
++ font = malloc(sizeof *font);
++
++ if (!font) {
++ FT_Done_Face(face);
++ return NULL;
++ }
++
++ font->context = context;
++ font->face = face;
+ memset(font->chars, 0, sizeof font->chars);
++
++ if (font->face->face_flags & FT_FACE_FLAG_SCALABLE) {
++ FT_F26Dot6 width, height;
++
++ width = ((unsigned int)pixel_size) << 6;
++ height = ((unsigned int)(pixel_size * aspect)) << 6;
++
++ FT_Set_Char_Size(font->face, width, height, 0, 0);
++ } else {
++ FT_Set_Pixel_Sizes(font->face, (unsigned int)pixel_size,
++ (unsigned int)(pixel_size * aspect));
++ }
++
++ font->base.ascent = font->face->size->metrics.ascender >> 6;
++ font->base.descent = -font->face->size->metrics.descender >> 6;
++ font->base.height = font->base.ascent + font->base.descent;
++ font->base.max_advance = font->face->size->metrics.max_advance >> 6;
++
++ font->glyphs = calloc(font->face->num_glyphs, sizeof(struct glyph *));
++
++ return &font->base;
++}
++
++EXPORT
++struct wld_font *
++wld_font_open_file(struct wld_font_context *context, const char *path,
++ int index, double pixel_size)
++{
++ FT_Face face;
++
++ DEBUG("Loading font file: %s\n", path);
++
++ if (FT_New_Face(context->library, path, index, &face) != 0)
++ return NULL;
++
++ return open_face(context, face, pixel_size, 1.0);
++}
++
++#ifdef WLD_FONTCONFIG
+ EXPORT
+ struct wld_font *
+ wld_font_open_name(struct wld_font_context *context, const char *name)
+@@ -84,71 +143,35 @@ struct wld_font *
+ wld_font_open_pattern(struct wld_font_context *context, FcPattern *match)
+ {
+ char *filename;
+- struct font *font;
++ FT_Face face;
+ FcResult result;
+- double pixel_size, aspect;
+-
+- font = malloc(sizeof *font);
+-
+- if (!font)
+- goto error0;
+-
+- font->context = context;
++ double pixel_size = 12, aspect;
++ int index = 0;
result = FcPatternGetString(match, FC_FILE, 0, (FcChar8 **)&filename);
++ FcPatternGetInteger(match, FC_INDEX, 0, &index);
+
+ if (result == FcResultMatch) {
+- FT_Error error;
+-
+ DEBUG("Loading font file: %s\n", filename);
+
+- error = FT_New_Face(context->library, filename, 0, &font->face);
+-
+- if (error == 0)
++ if (FT_New_Face(context->library, filename, index, &face) == 0)
+ goto load_face;
+ }
+
+- result = FcPatternGetFTFace(match, FC_FT_FACE, 0, &font->face);
+-
+- if (result != FcResultMatch) {
++ if (FcPatternGetFTFace(match, FC_FT_FACE, 0, &face) != FcResultMatch) {
+ DEBUG("Couldn't determine font filename or FreeType face\n");
+- goto error1;
++ return NULL;
+ }
+
+ load_face:
+- result = FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixel_size);
++ FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixel_size);
+
+- result = FcPatternGetDouble(match, FC_ASPECT, 0, &aspect);
+-
+- if (result == FcResultNoMatch)
++ if (FcPatternGetDouble(match, FC_ASPECT, 0, &aspect) != FcResultMatch)
+ aspect = 1.0;
+
+- if (font->face->face_flags & FT_FACE_FLAG_SCALABLE) {
+- FT_F26Dot6 width, height;
+-
+- width = ((unsigned int)pixel_size) << 6;
+- height = ((unsigned int)(pixel_size * aspect)) << 6;
+-
+- FT_Set_Char_Size(font->face, width, height, 0, 0);
+- } else {
+- FT_Set_Pixel_Sizes(font->face, (unsigned int)pixel_size,
+- (unsigned int)(pixel_size * aspect));
+- }
+-
+- font->base.ascent = font->face->size->metrics.ascender >> 6;
+- font->base.descent = -font->face->size->metrics.descender >> 6;
+- font->base.height = font->base.ascent + font->base.descent;
+- font->base.max_advance = font->face->size->metrics.max_advance >> 6;
+-
+- font->glyphs = calloc(font->face->num_glyphs, sizeof(struct glyph *));
+-
+- return &font->base;
+-
+-error1:
+- free(font);
+-error0:
+- return NULL;
++ return open_face(context, face, pixel_size, aspect);
+ }
++#endif
-@@ -160,6 +162,20 @@ wld_font_close(struct wld_font *font_base)
+ EXPORT
+ void
+@@ -160,6 +183,20 @@ wld_font_close(struct wld_font *font_base)
free(font);
}
@@ -41,7 +182,7 @@ index cbb187c..c492dff 100644
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)
+@@ -200,11 +237,31 @@ wld_font_ensure_char(struct wld_font *font_base, uint32_t character)
struct font *font = (void *)font_base;
FT_UInt glyph_index;
@@ -74,7 +215,7 @@ index cbb187c..c492dff 100644
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,
+@@ -216,16 +273,42 @@ wld_font_text_extents_n(struct wld_font *font_base,
uint32_t c;
FT_UInt glyph_index;
@@ -83,7 +224,8 @@ index cbb187c..c492dff 100644
+
extents->advance = 0;
- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
+- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
++ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
length -= ret;
text += ret;
- glyph_index = FT_Get_Char_Index(font->face, c);
@@ -120,11 +262,15 @@ index cbb187c..c492dff 100644
+ extents->ink_height = y2 - y1;
}
diff --git a/intel.c b/intel.c
-index 8cf0a21..0cb0973 100644
+index 8cf0a21..b684270 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') {
+@@ -313,10 +313,10 @@ renderer_draw_text(struct wld_renderer *base,
+ if (length == -1)
+ length = strlen(text);
+
+- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
++ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
- glyph_index = FT_Get_Char_Index(font->face, c);
@@ -157,12 +303,57 @@ index ed34b3d..1c5e693 100644
.flush = &renderer_flush,
.destroy = &renderer_destroy
};
+diff --git a/meson.build b/meson.build
+index 4de1248..ca762e9 100644
+--- a/meson.build
++++ b/meson.build
+@@ -40,9 +40,17 @@ c_args = cc.get_supported_arguments([
+ ]
+
+ requires = [
+- dependency('fontconfig'),
+ dependency('pixman-1'),
+ ]
++# fontconfig, where it is, finds fonts by name; without it a font is opened
++# from its file (wld_font_open_file), as a system without fontconfig has to
++fontconfig = dependency('fontconfig', required: get_option('fontconfig'))
++extra_cflags = []
++if fontconfig.found()
++ requires += fontconfig
++ c_args += '-DWLD_FONTCONFIG'
++ extra_cflags += '-DWLD_FONTCONFIG'
++endif
+ requires_private = [
+ dependency('freetype2'),
+ ]
+@@ -128,6 +136,7 @@ pkg.generate(
+ description: 'A primitive drawing library library targeted at Wayland',
+ requires: requires,
+ requires_private: requires_private,
++ extra_cflags: extra_cflags,
+ )
+
+ doxygen = find_program('doxygen', required: get_option('doxygen'))
+diff --git a/meson.options b/meson.options
+index 0dc80cf..cb19e40 100644
+--- a/meson.options
++++ b/meson.options
+@@ -1,4 +1,5 @@
+ option('wayland', type: 'feature', value: 'auto', description: 'Wayland frontend')
+ option('drm', type: 'feature', value: 'auto', description: 'Enable libdrm backend')
+ option('drivers', type: 'array', choices: ['auto', 'intel', 'nouveau'], value: ['auto'], description: 'libdrm drivers')
++option('fontconfig', type: 'feature', value: 'auto', description: 'Find fonts by name through fontconfig')
+ option('doxygen', type: 'feature', value: 'auto', description: 'Build documentation')
diff --git a/nouveau.c b/nouveau.c
-index 6bd7ab1..77f69db 100644
+index 6bd7ab1..c22bb21 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') {
+@@ -596,10 +596,10 @@ renderer_draw_text(struct wld_renderer *base,
+ if (length == -1)
+ length = strlen(text);
+
+- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
++ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
- glyph_index = FT_Get_Char_Index(font->face, c);
@@ -171,7 +362,7 @@ index 6bd7ab1..77f69db 100644
if (!font_ensure_glyph(font, glyph_index))
continue;
diff --git a/pixman.c b/pixman.c
-index ed7788f..824ba15 100644
+index ed7788f..0c2ea1f 100644
--- a/pixman.c
+++ b/pixman.c
@@ -37,6 +37,7 @@
@@ -232,8 +423,12 @@ index ed7788f..824ba15 100644
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') {
+@@ -423,10 +451,10 @@ renderer_draw_text(struct wld_renderer *base,
+ return;
+ solid = pixman_image_create_solid_fill(&pixman_color);
+
+- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
++ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
- glyph_index = FT_Get_Char_Index(font->face, c);
@@ -293,10 +488,10 @@ index 2f49bc8..c1c17ff 100644
void
wld_flush(struct wld_renderer *renderer)
diff --git a/wld-private.h b/wld-private.h
-index 5a02630..87189aa 100644
+index 5a02630..c3d1a70 100644
--- a/wld-private.h
+++ b/wld-private.h
-@@ -69,12 +69,20 @@ struct glyph {
+@@ -69,12 +69,52 @@ struct glyph {
uint16_t advance;
};
@@ -304,6 +499,38 @@ index 5a02630..87189aa 100644
+ * low bits: FT_Get_Char_Index searches the face's cmap on every call */
+#define FONT_CHAR_CACHE 256
+
++/* One character of UTF-8 text of at most len bytes into *c: its length,
++ * 0 at the end of the text or on a byte that starts no character. */
++static inline int
++wld_utf8_decode(const char *text, int len, uint32_t *c)
++{
++ const unsigned char *s = (const unsigned char *)text;
++ int n, i;
++
++ if (len <= 0 || !s[0])
++ return 0;
++ if (s[0] < 0x80) {
++ *c = s[0];
++ return 1;
++ }
++ if ((s[0] & 0xe0) == 0xc0)
++ n = 2, *c = s[0] & 0x1f;
++ else if ((s[0] & 0xf0) == 0xe0)
++ n = 3, *c = s[0] & 0x0f;
++ else if ((s[0] & 0xf8) == 0xf0)
++ n = 4, *c = s[0] & 0x07;
++ else
++ return 0;
++ if (n > len)
++ return 0;
++ for (i = 1; i < n; i++) {
++ if ((s[i] & 0xc0) != 0x80)
++ return 0;
++ *c = *c << 6 | (s[i] & 0x3f);
++ }
++ return n;
++}
++
struct font {
struct wld_font base;
@@ -317,7 +544,7 @@ index 5a02630..87189aa 100644
};
struct wld_context_impl {
-@@ -116,6 +124,8 @@ struct wld_renderer_impl {
+@@ -116,6 +156,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);
@@ -326,7 +553,7 @@ index 5a02630..87189aa 100644
void (*flush)(struct wld_renderer *renderer);
void (*destroy)(struct wld_renderer *renderer);
};
-@@ -156,6 +166,7 @@ struct buffer_socket_impl {
+@@ -156,6 +198,7 @@ struct buffer_socket_impl {
};
bool font_ensure_glyph(struct font *font, FT_UInt glyph_index);
@@ -335,10 +562,20 @@ index 5a02630..87189aa 100644
/**
* Returns the number of bytes per pixel for the given format.
diff --git a/wld.h b/wld.h
-index 8191ae3..b0ecf85 100644
+index 8191ae3..0c90afd 100644
--- a/wld.h
+++ b/wld.h
-@@ -90,6 +90,13 @@ void wld_destroy_context(struct wld_context *context);
+@@ -24,7 +24,9 @@
+ #ifndef WLD_H
+ #define WLD_H
+
++#ifdef WLD_FONTCONFIG
+ #include <fontconfig/fontconfig.h>
++#endif
+ #include <pixman.h>
+ #include <stdbool.h>
+ #include <stdint.h>
+@@ -90,6 +92,13 @@ void wld_destroy_context(struct wld_context *context);
struct wld_extents {
uint32_t advance;
@@ -352,7 +589,32 @@ index 8191ae3..b0ecf85 100644
};
struct wld_font {
-@@ -133,6 +140,11 @@ void wld_font_close(struct wld_font *font);
+@@ -110,6 +119,16 @@ struct wld_font_context *wld_font_create_context();
+ */
+ void wld_font_destroy_context(struct wld_font_context *context);
+
++/**
++ * Open a font file, face index of it (0 unless the file is a collection),
++ * pixel_size pixels tall. It needs no font matcher: this is how a font is
++ * opened where there is no fontconfig.
++ */
++struct wld_font *wld_font_open_file(struct wld_font_context *context,
++ const char *path, int index,
++ double pixel_size);
++
++#ifdef WLD_FONTCONFIG
+ /**
+ * Open a new font from the given fontconfig match.
+ */
+@@ -121,6 +140,7 @@ struct wld_font *wld_font_open_pattern(struct wld_font_context *context,
+ */
+ struct wld_font *wld_font_open_name(struct wld_font_context *context,
+ const char *name);
++#endif
+
+ /**
+ * Close a font.
+@@ -133,6 +153,11 @@ void wld_font_close(struct wld_font *font);
*/
bool wld_font_ensure_char(struct wld_font *font, uint32_t character);
@@ -364,7 +626,7 @@ index 8191ae3..b0ecf85 100644
/**
* Calculate the text extents of the given UTF-8 string.
*
-@@ -258,6 +270,15 @@ void wld_blend_region(struct wld_renderer *renderer,
+@@ -258,6 +283,15 @@ void wld_blend_region(struct wld_renderer *renderer,
int32_t dst_x, int32_t dst_y,
pixman_region32_t *region);
@@ -380,7 +642,7 @@ index 8191ae3..b0ecf85 100644
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,
+@@ -272,6 +306,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
*/
diff --git a/pixman.c b/pixman.c
@@ -451,7 +451,7 @@ renderer_draw_text(struct wld_renderer *base,
return;
solid = pixman_image_create_solid_fill(&pixman_color);
- while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
+ while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
text += ret;
length -= ret;
glyph_index = font_char_index(font, c);
diff --git a/wld-private.h b/wld-private.h
@@ -73,6 +73,38 @@ struct glyph {
* low bits: FT_Get_Char_Index searches the face's cmap on every call */
#define FONT_CHAR_CACHE 256
+/* One character of UTF-8 text of at most len bytes into *c: its length,
+ * 0 at the end of the text or on a byte that starts no character. */
+static inline int
+wld_utf8_decode(const char *text, int len, uint32_t *c)
+{
+ const unsigned char *s = (const unsigned char *)text;
+ int n, i;
+
+ if (len <= 0 || !s[0])
+ return 0;
+ if (s[0] < 0x80) {
+ *c = s[0];
+ return 1;
+ }
+ if ((s[0] & 0xe0) == 0xc0)
+ n = 2, *c = s[0] & 0x1f;
+ else if ((s[0] & 0xf0) == 0xe0)
+ n = 3, *c = s[0] & 0x0f;
+ else if ((s[0] & 0xf8) == 0xf0)
+ n = 4, *c = s[0] & 0x07;
+ else
+ return 0;
+ if (n > len)
+ return 0;
+ for (i = 1; i < n; i++) {
+ if ((s[i] & 0xc0) != 0x80)
+ return 0;
+ *c = *c << 6 | (s[i] & 0x3f);
+ }
+ return n;
+}
+
struct font {
struct wld_font base;
diff --git a/wld.h b/wld.h
@@ -24,7 +24,9 @@
#ifndef WLD_H
#define WLD_H
+#ifdef WLD_FONTCONFIG
#include <fontconfig/fontconfig.h>
+#endif
#include <pixman.h>
#include <stdbool.h>
#include <stdint.h>
@@ -118,6 +120,16 @@ struct wld_font_context *wld_font_create_context();
void wld_font_destroy_context(struct wld_font_context *context);
/**
+ * Open a font file, face index of it (0 unless the file is a collection),
+ * pixel_size pixels tall. It needs no font matcher: this is how a font is
+ * opened where there is no fontconfig.
+ */
+struct wld_font *wld_font_open_file(struct wld_font_context *context,
+ const char *path, int index,
+ double pixel_size);
+
+#ifdef WLD_FONTCONFIG
+/**
* Open a new font from the given fontconfig match.
*/
struct wld_font *wld_font_open_pattern(struct wld_font_context *context,
@@ -128,6 +140,7 @@ struct wld_font *wld_font_open_pattern(struct wld_font_context *context,
*/
struct wld_font *wld_font_open_name(struct wld_font_context *context,
const char *name);
+#endif
/**
* Close a font.