I'm using graphical GNU Emacs 26.3 on Windows.
At least on Windows, Emacs determines column width by looking at the AveCharWidth
field in the font's OS/2
table. As the name suggests, this field contains the average width of all non-zero-width characters in the font. Emacs then uses this to calculate pixel widths for things like an 80-column window.
However, the font I'm using, BIZ UDGothic, happens to be a CJK (Chinese, Japanese, Korean) monospace font. In fonts like this, there's a notion of "half-width" and "full-width" characters. For example, a character like a
is "half-width" and therefore one cell wide, but CJK characters like あ
are generally "full-width", or two cells wide.
In this font, half-width characters are 1024 FUnits wide, and full-width characters are 2048 FUnits wide. Ideally, the column width would be 1024 FUnits, since that aligns with the CJK notion of a half-width character being one cell wide. Unfortunately, because of the large number of full-width characters in this font, the average character width is 1991 FUnits, meaning the column width is nearly twice what it should be.
Now, this doesn't negatively impact the actual rendering of the font, since Emacs treats it as it would treat any proportional font, looking at the width of each individual character. It does, however, mean that windows are created much wider than they should be, and code that attempts to center text based on number of columns is significantly off.
I have fixed the problem by actually editing this field in the font file, but doing so was a bit of an ordeal, and I'd much prefer to configure this in Emacs itself. So, my question is, is there any way to do so, short of patching Emacs?
EDIT: To clarify a little, while editing the font itself is a solution, I'm not sure it's the right solution. Having the AveCharWidth
field be 1991 FUnits isn't wrong per se, it's an accurate description of the font. The problem is that the heuristic of column width = average character width
doesn't work in this case.
EDIT 2: Adding some screenshots to illustrate the problem:
With AveCharWidth
set to 1991 FUnits, the default size of the window is way too wide, and nothing is centered properly relative to the window size or the Spacemacs logo:
With AveCharWidth
set to 1024 FUnits, things are as they should be: