public inbox for git-commits@fedoraproject.org
help / color / mirror / Atom feed
* [rpms/kmscon] f45: Filter out non monospace fonts with freetype
@ 2026-10-08 14:05 Jocelyn Falempe
  0 siblings, 0 replies; only message in thread
From: Jocelyn Falempe @ 2026-10-08 14:05 UTC (permalink / raw)
  To: git-commits

            A new commit has been pushed.

            Repo   : rpms/kmscon
            Branch : f45
            Commit : 190eaed1c6dfd6c5a37fc43166f2db8177f6835b
            Author : Jocelyn Falempe <jfalempe@redhat.com>
            Date   : 2026-10-08T14:57:49+02:00
            Stats  : +369/-0 in 3 file(s)
            URL    : https://src.fedoraproject.org/rpms/kmscon/c/190eaed1c6dfd6c5a37fc43166f2db8177f6835b?branch=f45

            Log:
            Filter out non monospace fonts with freetype

This is necessary, when Fedora is installed with arabic language, the
default monospace font is not monospace, and looks ugly in kmscon.

Signed-off-by: Jocelyn Falempe <jfalempe@redhat.com>

---
diff --git a/freetype-Fix-memory-leak-when-loading-fallback-font.patch b/freetype-Fix-memory-leak-when-loading-fallback-font.patch
new file mode 100644
index 0000000..e806301
--- /dev/null
+++ b/freetype-Fix-memory-leak-when-loading-fallback-font.patch
@@ -0,0 +1,39 @@
+From c4c7c3ffd3056698a33c9b18b4a66510a57c1a6a Mon Sep 17 00:00:00 2001
+From: Jocelyn Falempe <jfalempe@redhat.com>
+Date: Thu, 8 Oct 2026 13:59:01 +0200
+Subject: [PATCH] freetype: Fix memory leak when loading fallback font.
+
+font->name is the main font name, so when loading a fallback font,
+the name was replaced by the fallback, leaking the main font name.
+---
+ src/font/font_freetype.c | 7 ++-----
+ 1 file changed, 2 insertions(+), 5 deletions(-)
+
+diff --git a/src/font/font_freetype.c b/src/font/font_freetype.c
+index 2ecdbee..86ff897 100644
+--- a/src/font/font_freetype.c
++++ b/src/font/font_freetype.c
+@@ -462,10 +462,7 @@ static FT_Face prepare_tmp_face(FT_Library ft, struct ft_font *font, int fallbac
+ 	if (!pattern)
+ 		return NULL;
+ 
+-	if (FcPatternGetString(pattern, FC_FULLNAME, 0, &full_name) == FcResultMatch)
+-		font->name = strdup((char *)full_name);
+-	else
+-		font->name = strdup("Unknown");
++	FcPatternGetString(pattern, FC_FULLNAME, 0, &full_name);
+ 
+ 	if (FcPatternGetString(pattern, FC_FILE, 0, &path) != FcResultMatch)
+ 		goto err_pattern;
+@@ -473,7 +470,7 @@ static FT_Face prepare_tmp_face(FT_Library ft, struct ft_font *font, int fallbac
+ 	if (FcPatternGetInteger(pattern, FC_INDEX, 0, &index) != FcResultMatch)
+ 		log_warn("%s: failed to get face index", path);
+ 
+-	log_debug("Loading fallback font %s %s", font->name, (char *)path);
++	log_debug("Loading fallback font %s %s", (char *)full_name, (char *)path);
+ 
+ 	err = FT_New_Face(ft, (char *)path, index, &face);
+ 	if (err)
+-- 
+2.55.0
+

diff --git a/freetype-filter-out-non-monospace-fonts-completely.patch b/freetype-filter-out-non-monospace-fonts-completely.patch
new file mode 100644
index 0000000..c90b112
--- /dev/null
+++ b/freetype-filter-out-non-monospace-fonts-completely.patch
@@ -0,0 +1,325 @@
+From 5bbd44757f0a2a724060277f53d216d143eae8ab Mon Sep 17 00:00:00 2001
+From: Akira TAGOH <akira@tagoh.org>
+Date: Thu, 8 Oct 2026 19:21:30 +0900
+Subject: [PATCH] freetype: filter out non-monospace fonts completely
+
+kmscon renders into fixed cells, so it must only ever use monospace
+fonts. Requesting monospace spacing on the query pattern merely re-ranks
+the results: proportional fonts remain in the sorted font set and are
+still chosen during glyph fallback for a character the monospace fonts
+don't cover (e.g. Arabic falls back to the proportional Vazirmatn when
+no monospace Arabic font is installed).
+
+Pre-filter the fontconfig font set with FcConfigSetFontSetFilter() so
+non-monospace fonts are dropped before the font list is built and can
+never be selected. As a consequence, a script with no monospace font
+available is left unrenderable rather than shown with a misaligned
+proportional glyph.
+
+This change requires fontconfig >= 2.16.0.
+---
+ meson.build                |   1 +
+ src/font/font_freetype.c   |  14 +++
+ tests/meson.build          |  16 +++
+ tests/test_font_freetype.c | 221 +++++++++++++++++++++++++++++++++++++
+ 4 files changed, 252 insertions(+)
+ create mode 100644 tests/test_font_freetype.c
+
+diff --git a/meson.build b/meson.build
+index f34baa6..3563084 100644
+--- a/meson.build
++++ b/meson.build
+@@ -94,6 +94,7 @@ freetype_deps = dependency(
+ )
+ fontconfig_deps = dependency(
+     'fontconfig',
++    version: '>=2.16.0',  # FcConfigSetFontSetFilter()
+     disabler: true,
+     required: get_option('font_freetype'),
+ )
+diff --git a/src/font/font_freetype.c b/src/font/font_freetype.c
+index e1dd9ec..2ecdbee 100644
+--- a/src/font/font_freetype.c
++++ b/src/font/font_freetype.c
+@@ -204,6 +204,19 @@ err:
+ 	return -EINVAL;
+ }
+ 
++static FcBool _fc_monospace_font_filter_func(const FcPattern *pat, void *user_data)
++{
++	int spacing;
++
++	if (FcPatternGetInteger(pat, FC_SPACING, 0, &spacing) == FcResultMatch) {
++		if (spacing == FC_SPACING_CHARCELL || spacing == FC_SPACING_MONO ||
++		    spacing == FC_SPACING_DUAL)
++			return FcTrue;
++	}
++	/* non-monospace font doesn't have FC_SPACING property */
++	return FcFalse;
++}
++
+ static int kmscon_font_freetype_init(struct kmscon_font *out, const char *name, unsigned int height)
+ {
+ 	struct ft_data *ftf;
+@@ -220,6 +233,7 @@ static int kmscon_font_freetype_init(struct kmscon_font *out, const char *name,
+ 		log_err("Failed to initialize FreeType\n");
+ 		goto err_free;
+ 	}
++	FcConfigSetFontSetFilter(NULL, _fc_monospace_font_filter_func, NULL, NULL);
+ 	if (prepare_font(ftf->ft, &ftf->regular, name, height, false, out))
+ 		goto err_done;
+ 
+diff --git a/tests/meson.build b/tests/meson.build
+index 9edcb70..4955edb 100644
+--- a/tests/meson.build
++++ b/tests/meson.build
+@@ -76,3 +76,19 @@ test_issue_parameter = executable(
+     dependencies: [libtsm_deps, shl_deps],
+ )
+ test('test_issue_parameter', test_issue_parameter)
++
++if enable_font_freetype
++    test_font_freetype = executable(
++        'test_font_freetype',
++        'test_font_freetype.c',
++        include_directories: src_inc,
++        dependencies: [
++            libtsm_deps,
++            freetype_deps,
++            fontconfig_deps,
++            threads_deps,
++            shl_deps,
++        ],
++    )
++    test('test_font_freetype', test_font_freetype)
++endif
+diff --git a/tests/test_font_freetype.c b/tests/test_font_freetype.c
+new file mode 100644
+index 0000000..b6b51d3
+--- /dev/null
++++ b/tests/test_font_freetype.c
+@@ -0,0 +1,221 @@
++/*
++ * Tests for the freetype font backend's monospace fontconfig filter.
++ *
++ * Background: there is no monospace Vazirmatn font, yet on an Arabic locale
++ * fontconfig happily offers the proportional Vazirmatn for a "monospace"
++ * request (reproduce with `fc-match monospace:lang=ar`). A proportional font
++ * used as a glyph fallback breaks column alignment in a cell-based terminal.
++ *
++ * The backend requests monospace spacing on the pattern AND installs a
++ * fontconfig filter (_fc_monospace_font_filter_func). The per-pattern request
++ * only re-ranks fonts (proportional ones still sit in the sorted fallback set
++ * and can be picked for a glyph the top monospace font lacks); the filter is
++ * what removes proportional fonts from the set entirely. That set-wide property
++ * is the real guarantee and the one worth testing.
++ *
++ * Two levels:
++ *   1. test_filter_unit          - the filter predicate in isolation.
++ *   2. test_monospace_fallback_set - exercise kmscon the way it really runs:
++ *      register the backend and kmscon_font_find() it through the
++ *      kmscon_font_ops callback table, then check that every font in the
++ *      fallback set it built is monospace.
++ *
++ * The public font API does not expose the fallback set (callers only get
++ * metrics and a glyph bitmap), so to inspect it we reach into the backend's
++ * private ft_data (font->data). Both src files are #included directly (like
++ * test_text.c) to get at that layout and the static filter.
++ */
++
++#include <assert.h>
++#include <fontconfig/fontconfig.h>
++#include <locale.h>
++#include <stdio.h>
++#include <string.h>
++
++#include "../src/font/font.c"	       /* real dispatch: register / find / render */
++#undef LOG_SUBSYSTEM		       /* font.c and font_freetype.c both define it */
++#include "../src/font/font_freetype.c" /* backend ops + ft_data layout */
++
++#define ARABIC_ALEF 0x0627u
++
++/* font.c pulls in shl/module.h; the backend registers with a NULL owner, so
++ * module ref-counting is a no-op here. */
++void shl_module_ref(struct shl_module *module) {}
++void shl_module_unref(struct shl_module *module) {}
++
++static bool spacing_is_monospace(int spacing)
++{
++	return spacing == FC_SPACING_CHARCELL || spacing == FC_SPACING_MONO ||
++	       spacing == FC_SPACING_DUAL;
++}
++
++/* --- 1. unit test of the filter predicate --- */
++
++static FcPattern *pattern_with_spacing(int spacing)
++{
++	FcPattern *pat = FcPatternCreate();
++	assert(pat);
++	assert(FcPatternAddInteger(pat, FC_SPACING, spacing));
++	return pat;
++}
++
++static void test_filter_unit(void)
++{
++	FcPattern *pat;
++
++	/* Monospace spacings must be accepted. */
++	pat = pattern_with_spacing(FC_SPACING_CHARCELL);
++	assert(_fc_monospace_font_filter_func(pat, NULL) == FcTrue);
++	FcPatternDestroy(pat);
++
++	pat = pattern_with_spacing(FC_SPACING_MONO);
++	assert(_fc_monospace_font_filter_func(pat, NULL) == FcTrue);
++	FcPatternDestroy(pat);
++
++	pat = pattern_with_spacing(FC_SPACING_DUAL);
++	assert(_fc_monospace_font_filter_func(pat, NULL) == FcTrue);
++	FcPatternDestroy(pat);
++
++	/* A proportional font (e.g. Vazirmatn) must be rejected. */
++	pat = pattern_with_spacing(FC_SPACING_PROPORTIONAL);
++	assert(_fc_monospace_font_filter_func(pat, NULL) == FcFalse);
++	FcPatternDestroy(pat);
++
++	/* Proportional fonts commonly omit FC_SPACING entirely; reject those too
++	 * (this is the real-world Vazirmatn shape). */
++	pat = FcPatternCreate();
++	assert(pat);
++	assert(_fc_monospace_font_filter_func(pat, NULL) == FcFalse);
++	FcPatternDestroy(pat);
++}
++
++/* --- 2. behavioral test through the public kmscon font API --- */
++
++/* Build the pattern kmscon's prepare_font() uses for a "monospace" request. */
++static FcPattern *kmscon_monospace_pattern(void)
++{
++	FcPattern *pat = FcNameParse((const FcChar8 *)KMSCON_FONT_DEFAULT_NAME);
++	assert(pat);
++	FcPatternAddInteger(pat, FC_WEIGHT, FC_WEIGHT_NORMAL);
++	FcPatternAddDouble(pat, FC_PIXEL_SIZE, 20.0);
++	FcPatternAddInteger(pat, FC_SPACING, FC_CHARCELL);
++	FcPatternAddInteger(pat, FC_SPACING, FC_MONO);
++	FcPatternAddInteger(pat, FC_SPACING, FC_DUAL);
++	FcConfigSubstitute(NULL, pat, FcMatchPattern);
++	FcDefaultSubstitute(pat);
++	return pat;
++}
++
++/*
++ * With no kmscon filter active, count the proportional fonts in the "monospace"
++ * sorted set for the current locale. If @ch is non-zero, only count fonts that
++ * cover @ch. This establishes that the unfiltered set really does contain
++ * proportional fonts for the filter to remove -- otherwise the monospace
++ * assertions below would hold trivially and guard nothing.
++ *
++ * Must be called before kmscon_font_find(), i.e. before the filter is installed.
++ */
++static int unfiltered_proportional_count(uint32_t ch)
++{
++	FcPattern *pat = kmscon_monospace_pattern();
++	FcResult res;
++	FcFontSet *fs = FcFontSort(NULL, pat, FcTrue, NULL, &res);
++	int count = 0;
++
++	for (int i = 0; fs && i < fs->nfont; i++) {
++		int spacing = FC_SPACING_PROPORTIONAL;
++
++		if (ch) {
++			FcCharSet *cs;
++			if (FcPatternGetCharSet(fs->fonts[i], FC_CHARSET, 0, &cs) != FcResultMatch)
++				continue;
++			if (!FcCharSetHasChar(cs, ch))
++				continue;
++		}
++		FcPatternGetInteger(fs->fonts[i], FC_SPACING, 0, &spacing);
++		if (!spacing_is_monospace(spacing))
++			count++;
++	}
++	if (fs)
++		FcFontSetDestroy(fs);
++	FcPatternDestroy(pat);
++	return count;
++}
++
++static void test_monospace_fallback_set(void)
++{
++	struct kmscon_font *font = NULL;
++	struct ft_data *ftd;
++	FcFontSet *set;
++	int arabic_fonts = 0;
++	int prop_total, prop_arabic;
++	int ret;
++
++	/* Use an Arabic locale so the ordering matches the real bug scenario. */
++	setlocale(LC_ALL, "ar_AE.UTF-8");
++
++	/* Measure the unfiltered set *before* the filter is installed by find(). */
++	prop_total = unfiltered_proportional_count(0);
++	prop_arabic = unfiltered_proportional_count(ARABIC_ALEF);
++
++	/* Register and look up the backend exactly as kmscon does at runtime;
++	 * init() installs the monospace filter as a side effect. */
++	ret = kmscon_font_register(&kmscon_font_freetype_ops);
++	assert(ret == 0);
++	ret = kmscon_font_find(&font, KMSCON_FONT_DEFAULT_NAME, 20, "freetype");
++	assert(ret == 0);
++	assert(font);
++	assert(font->ops == &kmscon_font_freetype_ops); /* freetype really chosen */
++
++	ftd = font->data;
++	assert(ftd);
++	set = ftd->regular.fc;
++	assert(set);
++
++	/*
++	 * Core, language-agnostic guarantee: every font kmscon put in the fallback
++	 * set carries a proper monospace FC_SPACING, no matter which script it
++	 * covers. The proportional Vazirmatn (and every other proportional font) is
++	 * therefore absent. Without the filter this set contains proportional fonts
++	 * (prop_total of them) and these assertions fail.
++	 */
++	if (prop_total == 0)
++		fprintf(stderr, "note: no proportional fonts installed; the monospace "
++				"assertions hold trivially and guard nothing\n");
++	for (int i = 0; i < set->nfont; i++) {
++		FcChar8 *family = NULL;
++		int spacing = -1;
++
++		/* proper, present FC_SPACING, and it must be a monospace value */
++		assert(FcPatternGetInteger(set->fonts[i], FC_SPACING, 0, &spacing) ==
++		       FcResultMatch);
++		assert(spacing_is_monospace(spacing));
++
++		FcPatternGetString(set->fonts[i], FC_FAMILY, 0, &family);
++		assert(family);
++		assert(strcasestr((const char *)family, "Vazirmatn") == NULL);
++
++		FcCharSet *cs;
++		if (FcPatternGetCharSet(set->fonts[i], FC_CHARSET, 0, &cs) == FcResultMatch &&
++		    FcCharSetHasChar(cs, ARABIC_ALEF))
++			arabic_fonts++;
++	}
++	fprintf(stderr,
++		"kmscon monospace fallback set: %d fonts, %d cover Arabic "
++		"(unfiltered had %d proportional, %d covering Arabic)\n",
++		set->nfont, arabic_fonts, prop_total, prop_arabic);
++
++	kmscon_font_unref(font);
++	kmscon_font_unregister(kmscon_font_freetype_ops.name);
++}
++
++int main(void)
++{
++	assert(FcInit());
++
++	test_filter_unit();
++	test_monospace_fallback_set();
++
++	FcFini();
++	return 0;
++}
+-- 
+2.55.0
+

diff --git a/kmscon.spec b/kmscon.spec
index 2aa7359..db97f49 100644
--- a/kmscon.spec
+++ b/kmscon.spec
@@ -36,6 +36,11 @@ Patch1: systemd-prevent-kmscon.service-to-run-if-dev-tty0-is.patch
 # Upstream patch, prevent crash on kernel 7.3+
 Patch2: drm_shared-Fix-crash-if-a-plane-has-no-properties.patch
 
+# Upstream patch, filter out non monospace fonts in freetype
+Patch10: freetype-filter-out-non-monospace-fonts-completely.patch
+# Upstream patch, fix a memory leak when using fallback font with freetype
+Patch11: freetype-Fix-memory-leak-when-loading-fallback-font.patch
+
 %description
 Kmscon is a simple terminal emulator based on linux kernel mode setting (KMS).
 It is an attempt to replace the in-kernel VT implementation with a userspace

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-08 14:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-08 14:05 [rpms/kmscon] f45: Filter out non monospace fonts with freetype Jocelyn Falempe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox