Subject: Re: bug#52888: 29.0.50; font_{delete_unmatched,score} do not handle nil FONT_WEIGHT_INDEX

From: Sean Whitton <[email-masked]>
Date: Tue, 04 Jan 2022 19:10:46 -0700

Hello,

On Sat 01 Jan 2022 at 09:15AM +02, Eli Zaretskii wrote:

> However, the question that I think we should ask ourselves at this
> point is whether there's a reasonable way to process these fonts into
> a font spec that Emacs expects.  So if FcPatternGetInteger and
> FcPatternGetDouble don't do the job, perhaps there's another
> fontconfig function that does, or maybe these fonts need to be
> processed in some slightly different ways (like in a loop, for
> example) to produce the weight matches from them?

I received a helpful reply from Akira TAGOH on the fontconfig list.  The
problematic entries are not in fact additional font weights available to
Emacs, but meta or virtual entries from which applications can extract
information about the range of weights provided by the variable weight
.ttf file.

As we do not need to know the range of weights explicitly, the virtual
entries are no use to us.  We can just skip over them and process the
non-virtual FcPattern entries we get for each weight.  I'm attaching a
patch which detects these virtual entries and skips over them.  Maybe we
could revert and replace the temporary fix with my patch?

As a test, I tried calling FcPatternGetRange on the virtual entry for
Inconsolata-VariableFont_wdth,wght.ttf and I got a range of 40 to 210.
I confirmed that the weights of all the non-virtual entries fall within
this range, inclusive.

-- 
Sean Whitton
>From 4747fe3072c6fca0385203cef9b6e2661badaaba Mon Sep 17 00:00:00 2001
From: Sean Whitton <[email-masked]>
Date: Tue, 4 Jan 2022 19:07:29 -0700
Subject: [PATCH] Skip virtual FcPattern entries for variable weight fonts

* src/ftfont.c (ftfont_list): Pass FC_VARIABLE to FcObjectSetBuild.
* src/ftfont.c (ftfont_pattern_entity): Skip meta/virtual FcPattern
entries for variable weight fonts (Bug#52888).
---
 src/ftfont.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/ftfont.c b/src/ftfont.c
index cf592759ab..73276fdc86 100644
--- a/src/ftfont.c
+++ b/src/ftfont.c
@@ -184,11 +184,22 @@ ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
   int numeric;
   double dbl;
   FcBool b;
+  FcRange *range;
 
   if (FcPatternGetString (p, FC_FILE, 0, &str) != FcResultMatch)
     return Qnil;
   if (FcPatternGetInteger (p, FC_INDEX, 0, &idx) != FcResultMatch)
     return Qnil;
+  if (FcPatternGetRange (p, FC_WEIGHT, 0, &range) == FcResultMatch
+      && FcPatternGetBool (p, FC_VARIABLE, 0, &b) == FcResultMatch
+      && b == FcTrue)
+    /* This is a virtual/meta FcPattern for a variable weight font,
+       from which it is possible to extract an FcRange value
+       specifying the minimum and maximum weights available in this
+       file.  We don't need to know that information explicitly, so
+       skip it.  We will be called with an FcPattern for each actually
+       available, non-virtual weight.  */
+    return Qnil;
 
   file = (char *) str;
   key = Fcons (build_unibyte_string (file), make_fixnum (idx));
@@ -853,7 +864,7 @@ ftfont_list (struct frame *f, Lisp_Object spec)
     adstyle = Qnil;
   objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
 			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
-			     FC_STYLE, FC_FILE, FC_INDEX,
+			     FC_STYLE, FC_FILE, FC_INDEX, FC_VARIABLE,
 #ifdef FC_CAPABILITY
 			     FC_CAPABILITY,
 #endif	/* FC_CAPABILITY */
-- 
2.30.2