Subject: Re: bug#65803: 29.1; Noto Sans Mono CJK JP has doubled-width on Windows

From: Po Lu <[email-masked]>
Date: Sun, 10 Sep 2023 15:31:36 +0800

Eli Zaretskii <[email-masked]> writes:

>> From: Po Lu <[email-masked]>
>> Cc: [email-masked],  [email-masked]
>> Date: Sun, 10 Sep 2023 13:55:47 +0800
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> > No, the purpose of FRAME_COLUMN_WIDTH is much more than just "the
>> > width of ASCII characters".  It is used as the canonical character
>> > width of the frame, for gazillion purposes.  One example which
>> > triggered this bug is :align-to display spec, something utterly
>> > unrelated to ASCII characters.
>> 
>> However, the column width has hitherto been defined to the average width
>> of the frame font's ASCII characters.  At least outside W32, that is.
>
> No!  Once again, for fixed-pitch fonts the average width is taken from
> the font.  Here, from ftfont.c:
>
>     if (spacing != FC_PROPORTIONAL
>   #ifdef FC_DUAL
> 	&& spacing != FC_DUAL
>   #endif	/* FC_DUAL */
> 	)
>       font->min_width = font->average_width = font->space_width
> 	= (scalable ? ft_face->max_advance_width * size / upEM + 0.5
> 	   : ft_face->size->metrics.max_advance >> 6);
>     else
>       {
> 	int n;
>
> 	font->min_width = font->average_width = font->space_width = 0;
> 	for (i = 32, n = 0; i < 127; i++)
> 	  if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) == 0)
> 	    {
> 	      int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
>
> 	      if (this_width > 0
> 		  && (! font->min_width || font->min_width > this_width))
> 		font->min_width = this_width;
> 	      if (i == 32)
> 		font->space_width = this_width;
> 	      font->average_width += this_width;
> 	      n++;
> 	    }
> 	if (n > 0)
> 	  font->average_width /= n;
>       }
>
> This clearly only calculates the average width for proportional fonts,
> and otherwise takes the average width from the font's max_advance
> width without calculating anything.  Or what am I missing?
>
>> > But other font back-ends don't compute average_width for fixed-pitch
>> > fonts, so are you only talking about proportional fonts here?
>> 
>> I'm talking about fonts in general: since fixed pitch fonts are meant to
>> incorporate uniformly sized glyphs, the width of the space glyph should
>> represent the average width of any subset of the font's glyphs.  In this
>> particular case, Fontconfig doesn't deem the font in question a fixed
>> pitch font, and thus Emacs measures the average width of each ASCII
>> character itself.
>
> See above: other backends only calculate the average width for
> proportional fonts.  So what you say doesn't fit my reading of the
> code.

Because if spacing is not FC_PROPORTIONAL or FC_DUAL, we know in advance
that max_advance_width or max_advance are identical to the average of
all ASCII glyphs.  Such special treatment is an optimization, nothing
more.  max_advance_width is the advance width (in em space) of the
widest glyph when the font is scalable, and max_advance is that in pixel
space if not.