Subject: Re: bug#2667: Still seeing inconsistent fonts

From: Kenichi Handa <[email-masked]>
Date: Mon, 11 May 2009 21:21:45 +0900

Sorry for the late response on this matter.

In article <[email-masked]>, Stefan Monnier <[email-masked]> writes:

> > I can't reproduce this.  Could you provide a self-contained testcase?
> Sure:

> xrdb -remove
> rm ~/.Xdefaults
> src/emacs -Q --eval '(set-face-font (quote default) \
>        "-misc-fixed-medium-r-semicondensed--13-*-*-*-*-*-*-*")'
>    C-u C-\ TeX RET
>    a
>    \ ' e
>    \ ' r
>    \ f o r a l l

> "aéŕ" use misc-fixed-semicondensed (the first two with iso8859-1 and
> the last with iso8859-2), but "∀" is displayed with
> xft:-unknown-DejaVu Sans-normal-normal-semi-condensed-*-13-*-*-*-*-0-iso10646-1

∀ belongs to `symbol' script, but the xfont backend didn't
support :script font property.  That is because I was afraid
that it made the font listing extremely slow (we must open
all iso10646-1 fonts to check if it supports the requested
characters).  Actually my trial implementation of :script
property supports took more than 1 minute to display HELLO
file.

But, if we can use this heuristic:

   X fonts that have the same property values except for
   size related properties supports the same set of
   characters on all display.  For example, all these fonts 

-adobe-courier-medium-r-normal--8-80-75-75-m-50-iso10646-1
-adobe-courier-medium-r-normal--10-100-75-75-m-60-iso10646-1
-adobe-courier-medium-r-normal--11-80-100-100-m-60-iso10646-1
-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso10646-1
-adobe-courier-medium-r-normal--14-100-100-100-m-90-iso10646-1
-adobe-courier-medium-r-normal--14-140-75-75-m-90-iso10646-1
-adobe-courier-medium-r-normal--17-120-100-100-m-100-iso10646-1
-adobe-courier-medium-r-normal--18-180-75-75-m-110-iso10646-1
-adobe-courier-medium-r-normal--20-140-100-100-m-110-iso10646-1
-adobe-courier-medium-r-normal--24-240-75-75-m-150-iso10646-1
-adobe-courier-medium-r-normal--25-180-100-100-m-150-iso10646-1
-adobe-courier-medium-r-normal--34-240-100-100-m-200-iso10646-1

   suports the same set of characters.

we can list fonts in a realistic time.  Attached is the
patch to try it.  Could you please test it?  As the change
is not simple, I have not yet installed it.

---
Kenichi Handa
[email-masked]

Index: xfont.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/xfont.c,v
retrieving revision 1.28
diff -u -r1.28 xfont.c
--- xfont.c	8 May 2009 06:22:40 -0000	1.28
+++ xfont.c	11 May 2009 12:03:16 -0000
@@ -256,20 +256,172 @@
   return len;
 }
 
-static Lisp_Object xfont_list_pattern P_ ((Lisp_Object, Display *, char *));
+static int xfont_chars_supported P_ ((Lisp_Object, XFontStruct *,
+				      struct charset *, struct charset *));
+
+/* Check if CHARS (cons or vector) is supported by XFONT whose
+   encoding charset is ENCODING (XFONT is NULL) or by a font whose
+   registry corresponds to ENCODING and REPERTORY.
+   Return 1 if supported, return 0 otherwise.  */
+
+static int
+xfont_chars_supported (chars, xfont, encoding, repertory)
+     Lisp_Object chars;
+     XFontStruct *xfont;
+     struct charset *encoding, *repertory;
+{
+  struct charset *charset = repertory ? repertory : encoding;
+
+  if (CONSP (chars))
+    {
+      for (; CONSP (chars); chars = XCDR (chars))
+	{
+	  int c = XINT (XCAR (chars));
+	  unsigned code = ENCODE_CHAR (charset, c);
+	  XChar2b char2b;
+
+	  if (code == CHARSET_INVALID_CODE (charset))
+	    break;
+	  if (! xfont)
+	    continue;
+	  if (code >= 0x10000)
+	    break;
+	  char2b.byte1 = code >> 8;
+	  char2b.byte2 = code & 0xFF;
+	  if (! xfont_get_pcm (xfont, &char2b))
+	    break;
+	}
+      return (NILP (chars));
+    }
+  else if (VECTORP (chars))
+    {
+      int i;
+
+      for (i = ASIZE (chars) - 1; i >= 0; i--)
+	{
+	  int c = XINT (AREF (chars, i));
+	  unsigned code = ENCODE_CHAR (charset, c);
+	  XChar2b char2b;
+
+	  if (code == CHARSET_INVALID_CODE (charset))
+	    continue;
+	  if (! xfont)
+	    break;
+	  if (code >= 0x10000)
+	    continue;
+	  char2b.byte1 = code >> 8;
+	  char2b.byte2 = code & 0xFF;
+	  if (xfont_get_pcm (xfont, &char2b))
+	    break;
+	}
+      return (i >= 0);
+    }
+  return 0;
+}
+
+/* A hash table recoding which font supports which scritps.  Each key
+   is a vector of characteristic font propertis FOUNDRY to WIDTH and
+   ADDSTYLE, and each value is a list of script symbols.
+
+   We assume that fonts that have the same value in the above
+   properties supports the same set of characters on all displays.  */
+
+static Lisp_Object xfont_scripts_cache;
+
+/* Return a list of scripts supported by the font of FONTNAME whose
+   characteristic properties are in PROPS and whose encoding charset
+   is ENCODING.  A caller must call BLOCK_INPUT in advance.  */
 
 static Lisp_Object
-xfont_list_pattern (frame, display, pattern)
-     Lisp_Object frame;
+xfont_supported_scripts (display, fontname, props, encoding)
+     Display *display;
+     char *fontname;
+     Lisp_Object props;
+     struct charset *encoding;
+{
+  Lisp_Object scripts;
+
+  /* Two special cases to avoid opening rather big fonts.  */
+  if (AREF (props, 2), Qja)
+    return Fcons (intern ("kana"), Fcons (intern ("han"), Qnil));
+  if (AREF (props, 2), Qko)
+    return Fcons (intern ("hangul"), Qnil);
+  scripts = Fgethash (props, xfont_scripts_cache, Qt);
+  if (EQ (scripts, Qt))
+    {
+      XFontStruct *xfont;
+      Lisp_Object val;
+
+      scripts = Qnil;
+      xfont = XLoadQueryFont (display, fontname);
+      if (xfont)
+	{
+	  if (xfont->per_char)
+	    {
+	      for (val = Vscript_representative_chars; CONSP (val);
+		   val = XCDR (val))
+		if (CONSP (XCAR (val)) && SYMBOLP (XCAR (XCAR (val))))
+		  {
+		    Lisp_Object script = XCAR (XCAR (val));
+		    Lisp_Object chars = XCDR (XCAR (val));
+
+		    if (xfont_chars_supported (chars, xfont, encoding, NULL))
+		      scripts = Fcons (script, scripts);
+		  }
+	    }
+	  XFreeFont (display, xfont);
+	}
+      Fputhash (Fcopy_sequence (props), scripts, xfont_scripts_cache);
+    }
+  return scripts;
+}
+
+extern Lisp_Object Vscalable_fonts_allowed;
+
+static Lisp_Object xfont_list_pattern P_ ((Display *, char *, 
+					   Lisp_Object, Lisp_Object));
+
+static Lisp_Object
+xfont_list_pattern (display, pattern, registry, script)
      Display *display;
      char *pattern;
+     Lisp_Object registry, script;
 {
   Lisp_Object list = Qnil;
+  Lisp_Object chars = Qnil;
+  struct charset *encoding, *repertory = NULL;
   int i, limit, num_fonts;
   char **names;
   /* Large enough to decode the longest XLFD (255 bytes). */
   char buf[512];
 
+  if (! NILP (registry)
+      && font_registry_charsets (registry, &encoding, &repertory) < 0)
+    /* Unknown REGISTRY, not supported.  */
+    return Qnil;
+  if (! NILP (script))
+    {
+      chars = assq_no_quit (script, Vscript_representative_chars);
+      if (NILP (chars))
+	/* We can't tell whether or not a font supports SCRIPT.  */
+	return Qnil;
+      chars = XCDR (chars);
+      if (repertory)
+	{
+	  if (! xfont_chars_supported (chars, NULL, encoding, repertory))
+	    return Qnil;
+	  script = Qnil;
+	}
+    }
+  if (! repertory && NILP (xfont_scripts_cache))
+    {
+      Lisp_Object args[2];
+
+      args[0] = QCtest;
+      args[1] = Qequal;
+      xfont_scripts_cache = Fmake_hash_table (2, args);
+    }
+      
   BLOCK_INPUT;
   x_catch_errors (display);
 
@@ -292,6 +444,8 @@
   if (num_fonts > 0)
     {
       char **indices = alloca (sizeof (char *) * num_fonts);
+      Lisp_Object props = Fmake_vector (make_number (9), Qnil);
+      Lisp_Object scripts = Qnil;
 
       for (i = 0; i < num_fonts; i++)
 	indices[i] = names[i];
@@ -300,47 +454,68 @@
       for (i = 0; i < num_fonts; i++)
 	{
 	  Lisp_Object entity;
-	  int result;
-	  char *p;
 
 	  if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0)
 	    continue;
-
 	  entity = font_make_entity ();
-	  ASET (entity, FONT_TYPE_INDEX, Qx);
 	  xfont_decode_coding_xlfd (indices[i], -1, buf);
-	  result = font_parse_xlfd (buf, entity);
-	  if (result < 0)
+	  font_parse_xlfd (buf, entity);
+	  ASET (entity, FONT_TYPE_INDEX, Qx);
+	  /* Avoid auto-scaled fonts.  */
+	  if (XINT (AREF (entity, FONT_DPI_INDEX)) != 0
+	      && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0)
+	    continue;
+	  /* Avoid not-allowed scalable fonts.  */
+	  if (NILP (Vscalable_fonts_allowed))
 	    {
-	      /* This may be an alias name.  Try to get the full XLFD name
-		 from XA_FONT property of the font.  */
-	      XFontStruct *font = XLoadQueryFont (display, indices[i]);
-	      unsigned long value;
-
-	      if (! font)
+	      if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
 		continue;
-	      if (XGetFontProperty (font, XA_FONT, &value))
-		{
-		  char *name = (char *) XGetAtomName (display, (Atom) value);
-		  int len = strlen (name);
+	    }
+	  else if (CONSP (Vscalable_fonts_allowed))
+	    {
+	      Lisp_Object tail, elt;
 
-		  /* If DXPC (a Differential X Protocol Compressor)
-		     Ver.3.7 is running, XGetAtomName will return null
-		     string.  We must avoid such a name.  */
-		  if (len > 0)
-		    {
-		      xfont_decode_coding_xlfd (indices[i], -1, buf);
-		      result = font_parse_xlfd (buf, entity);
-		    }
-		  XFree (name);
+	      for (tail = Vscalable_fonts_allowed; CONSP (tail);
+		   tail = XCDR (tail))
+		{
+		  elt = XCAR (tail);
+		  if (STRINGP (elt)
+		      && fast_c_string_match_ignore_case (elt, indices[i]) >= 0)
+		    break;
 		}
-	      XFreeFont (display, font);
+	      if (! CONSP (tail))
+		continue;
 	    }
 
-	  if (result == 0
-	      /* Avoid auto-scaled fonts.  */
-	      && (XINT (AREF (entity, FONT_DPI_INDEX)) == 0
-		  || XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) > 0))
+	  /* Update encoding and repertory if necessary.  */
+	  if (! EQ (registry, AREF (entity, FONT_REGISTRY_INDEX)))
+	    {
+	      registry = AREF (entity, FONT_REGISTRY_INDEX);
+	      if (font_registry_charsets (registry, &encoding, &repertory) < 0)
+		encoding = NULL;
+	    }
+	  if (! encoding)
+	    /* Unknown REGISTRY, not supported.  */
+	    continue;
+	  if (repertory)
+	    {
+	      if (NILP (script)
+		  || xfont_chars_supported (chars, NULL, encoding, repertory))
+		list = Fcons (entity, list);
+	      continue;
+	    }
+	  if (memcmp (&(AREF (props, 0)), &(AREF (entity, FONT_FOUNDRY_INDEX)),
+		      sizeof (Lisp_Object) * 7)
+	      || ! EQ (AREF (entity, FONT_SPACING_INDEX), AREF (props, 8)))
+	    {
+	      memcpy (&(AREF (props, 0)), &(AREF (entity, FONT_FOUNDRY_INDEX)),
+		      sizeof (Lisp_Object) * 7);
+	      ASET (props, 8, AREF (entity, FONT_SPACING_INDEX));
+	      scripts = xfont_supported_scripts (display, indices[i],
+						 props, encoding);
+	    }
+	  if (NILP (script)
+	      || ! NILP (Fmemq (script, scripts)))
 	    list = Fcons (entity, list);
 	}
       XFreeFontNames (names);
@@ -359,7 +534,7 @@
 {
   FRAME_PTR f = XFRAME (frame);
   Display *display = FRAME_X_DISPLAY_INFO (f)->display;
-  Lisp_Object registry, list, val, extra;
+  Lisp_Object registry, list, val, extra, script;
   int len;
   /* Large enough to contain the longest XLFD (255 bytes) in UTF-8.  */
   char name[512];
@@ -370,9 +545,6 @@
       val = assq_no_quit (QCotf, extra);
       if (! NILP (val))
 	return Qnil;
-      val = assq_no_quit (QCscript, extra);
-      if (! NILP (val))
-	return Qnil;
       val = assq_no_quit (QClang, extra);
       if (! NILP (val))
 	return Qnil;
@@ -382,8 +554,13 @@
   len = font_unparse_xlfd (spec, 0, name, 512);
   if (len < 0 || (len = xfont_encode_coding_xlfd (name)) < 0)
     return Qnil;
-  ASET (spec, FONT_REGISTRY_INDEX, registry);
-  list = xfont_list_pattern (frame, display, name);
+
+  val = assq_no_quit (QCscript, extra);
+  if (NILP (val))
+    script = Qnil;
+  else
+    script = XCDR (val);
+  list = xfont_list_pattern (display, name, registry, script);
   if (NILP (list) && NILP (registry))
     {
       /* Try iso10646-1 */
@@ -392,7 +569,7 @@
       if (r - name + 10 < 256)	/* 10 == strlen (iso10646-1) */
 	{
 	  strcpy (r, "iso10646-1");
-	  list = xfont_list_pattern (frame, display, name);
+	  list = xfont_list_pattern (display, name, Qiso10646_1, script);
 	}
     }
   if (NILP (list) && ! NILP (registry))
@@ -412,7 +589,7 @@
 		&& ((r - name) + SBYTES (XCAR (alter))) < 256)
 	      {
 		strcpy (r, (char *) SDATA (XCAR (alter)));
-		list = xfont_list_pattern (frame, display, name);
+		list = xfont_list_pattern (display, name, registry, script);
 		if (! NILP (list))
 		  break;
 	      }
@@ -427,7 +604,7 @@
 	  bcopy (SDATA (XCDR (val)), name, SBYTES (XCDR (val)) + 1);
 	  if (xfont_encode_coding_xlfd (name) < 0)
 	    return Qnil;
-	  list = xfont_list_pattern (frame, display, name);
+	  list = xfont_list_pattern (display, name, registry, script);
 	}
     }
 
@@ -996,6 +1173,8 @@
 void
 syms_of_xfont ()
 {
+  staticpro (&xfont_scripts_cache);
+  xfont_scripts_cache = Qnil;
   xfont_driver.type = Qx;
   register_font_driver (&xfont_driver, NULL);
 }