返回资讯中心
外部精选
软件工程
#开发者实时流

博客文章讨论编程字体难题:点值字号与位图字体难以匹配

来源标题:Coding Font Problems

这篇博客认为,编程字体要达到最佳显示效果应使用位图字体,并让终端等软件严格按字体作者设计的原始位图尺寸显示字形。但多数程序只支持按点值调整字号,遇到非原生尺寸时只能靠额外留白或缩放位图应付,显示效果会变差。文中还讨论了真实 DPI、Wayland DPI 和字号配置等相关问题。

Hacker News 最新Brajeshwar2026/11/27 · 今天25 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

What Should Happen

What Happens

Information on the DisplayReal DPI

Wayland DPI

Font Sizes

Font Bitmaps

Configuring Font Sizes

Misc

What Should Happen

Firstly here is how I would like fonts to work for coding.

Coding requires monospaced fonts of a reasonably small size. To achieve that you need bitmap fonts. So a bunch of font creators design fonts that match their ideas of whatever is clearest to read. I select a font that is the product of a font designer who has similar ideas to mine about what is readable. Then my terminal emulator and other software fits the font glyphs of a size I select into an arrangement that exactly matches the bitmap size.

That makes things easy for me and gives a result that’s as good as can be achieved. Font sizes varying by 0.1 point wouldn’t be an option because the font author didn’t write that many but that’s OK. I don’t want every possible size just the ones that can be displayed clearly.

When you change the font size in points which is the only way most programs support changing the size the program will apparently fudge things with extra space or try to scale bitmap fonts and look ugly.

Font size is in points which is expressed in a way where 1 point is 1/72 of a virtual inch in height of a character [1].

The command “kscreen-doctor -o” shows information on outputs which isn’t very useful for DPI but does show the scale factor (EG 1.5 for 150% scaling in display configuration).

The command wayland-info gives information on the real size and scaling via the fields width, height, logical_width, logical_height, physical_width, and physical_height fields. The scaling is width/logical_width. The actual DPI would either be “width/(physical_width to inches)” or “logical_width/(physical_width to inches)” which is the following on my laptop calculated with qalc:

$ qalc

3840/(309mm to inches)

warning: Conversion of only part of an expression is not supported ("3840/(309mm" is converted to "inches)"). 3840 / (309 millimeters) ? 315.6504854 in^?1

2560/(309mm to inches)

warning: Conversion of only part of an expression is not supported ("2560/(309mm" is converted to "inches)"). 2560 / (309 millimeters) ? 210.4336570 in^?1

This is not the DPI used for screen calculations. Firstly whatever the DPI is it will almost certainly be an integer to avoid using floating point maths as much as possible. Secondly DPI is mostly just made up anyway.

According to the results of google searches (this Gnome Discourse discussion is one example [2]) increasing the “scale” to a non integer multiple (EG deciding to use 150% instead of 100% or 200%) causes the “scale” to be sent to the application as the smallest integer that is greater than the multiple used and the buffer that the application gives is then scaled down to the required size. So if you specify 150% (as I am doing on my laptop) then the “scale” number will be 2 and the application will give a double size buffer which is then scaled to 3/4 of it’s size. So 38402160 at 150% scaling means that a 51202880 buffer where things are rendered at twice the standard size which is then scaled back to 3/4 of it’s size.

The way Wayland works for non-integer scaling (which is the common case for high-DPI laptop screens) significantly increases the memory use for application frame buffers (78% increase for 150%) and then requires another layer of scaling to convert those buffers to the resolution for display output. I thought that this was part of why my latest laptop with a i7-10610U CPU and 38402160 screen and 150% scaling feels slower than the previous one with i7-8550U CPU and 25601440 screen with 100% scaling.

However recent updates to Wayland add a protocol wp_fractional_scale_manager_v1 for fractional scaling. It allows scaling in multiples of 1/120 of the screen size, presumably this number was chosen to allow 2/3 and 4/3 sizes to be handled cleanly. That protocol is served by the KWin Wayland system and used by Chrome and Kitty, it’s presumably used in most cases by KDE applications too. So the apps that I spend the most time using should all look great with fractional scaling and not take excessive RAM.

There are many references to Wayland having 96 DPI but it’s hard to find an authoritative reference, the best I could find is the Wezterm documentation which says 96 DPI and “high density” is 192 DPI [3], that documentation seems to be written before fractional scaling but at least it’s a somewhat official source.

As the “points” of font sizes are multiples of 1/72 of an inch and Wayland is deemed to have 96DPI that means the pixel height of a font on a 100% scaling system will be it’s “point” size multiplied by 96/72 which equals 4/3. In a test a system with 10.5 point Terminus font on Wayland with 100% scaling gave a distance of 16 pixels between the top of one letter and the top of the same letter on the line below. 10.5*4/3 == 14, so presumably there’s 2 pixels between lines in that case.

For a system with fractional scaling and the Roboto Mono font I tested a system with 150% scaling and 9 point font which appeared to have 17 pixels from the top of a capital letter to the bottom of a lowercase p/q/g. 94/31.5==18 so in that case the font seems to have one pixel of space at the top or bottom. That case had 24 pixels from the top of one character to the top of the one on the next line so the vertical spacing seemed to be 6 pixels. On the same system with the Terminus font I found that it was 20 pixels from the top of one character to the top of the next line and 5 pixels space between the top of a capital and the bottom of a p/q implying a 2 pixel line spacing and 3 empty pixels at the top and bottom of characters.

A scalable font gives the best clarity at arbitrary sizes. But for best clarity with the caveat of matching the pixel sizes a bitmap font is best.

The following command shows the bitmap table in a TTF font file, no bitmap table means it’s a scalable font:

ttx -l /usr/share/fonts/truetype/terminus/TerminusTTF-4.46.0.ttf|grep EBDT

The ttx command is in the Debian package fontutils.

The first command dumps the EBLC table (which has the bitmap sizes) to a XML file, it creates a file in the same directory so copy that file to /tmp or something first, the second file greps for bitmap sizes in the XML:

ttx -t EBLC TerminusTTF-4.46.0.ttf grep ppem TerminusTTF-4.46.0.ttx

For Terminus this shows that all the bitmaps are square with sizes 12 14 16 18 20 22 24 28 32.

The following command gives the point sizes that match the bitmap sizes for 150% scaling (the /1.5 in the equation):

for n in $(grep ppemX TerminusTTF-4.46.0.ttx |cut -f2 -d") ; do echo "scale=1;$n/4*3/1.5"|bc ; done

Configuring Font Sizes

The following kitty config disables the standard keys for increasing and decreasing font size and maps

map ctrl+shift+equal kitten ~/bin/bitmap_size.py up map ctrl+shift+minus kitten ~/bin/bitmap_size.py down

The following is the ~/bin/bitmap_size.py script which was generated by ChatGPT and used the numbers generated by the shell commands from the previous section:

from kittens.tui.handler import result_handler from kitty.fast_data_types import os_window_font_size

font sizes in points = pixel_size * 72 / DPI (edit for your font and DPI)

SIZES = [6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 14.0, 16.0]

def main(args): pass

@result_handler(no_ui=True) def handle_result(args, answer, target_window_id, boss): w = boss.window_id_map.get(target_window_id) if w is None: return cur = os_window_font_size(w.os_window_id) if args[1] == 'up': bigger = [s for s in SIZES if s > cur + 0.01] new = bigger[0] if bigger else SIZES[-1] else: smaller = [s for s in SIZES if s The following shell script takes a TTF font file name as a parameter and gives a list of the point sizes needed to display at the natural sizes on the current Wayland display:

#!/bin/bash set -e

LOG_WIDTH=$(wayland-info|grep logical_width:|cut -f1 -d,|sed -e "s/^.*: //") WIDTH=$(wayland-info|grep width|grep -v _width|cut -f2 -d:|cut -f2 -d\ ) MULT=$(echo "$WIDTH/$LOG_WIDTH"|bc -l)

if [ ! -f "$1" ]; then echo "usage $0 FONTFILE" exit 1 fi EBDT=$(ttx -l $1|grep EBDT 2> /dev/null || true) if [ "$EBDT" == "" ]; then echo "$1 is not a bitmap font" exit 1 fi TMP="/tmp/$(basename $1)" cp "$1" "$TMP" ttx -t EBLC "$TMP" 2> /dev/null TTX=$(echo "$TMP"|sed -e s/ttf/ttx/) A="" for n in $(grep ppemX "$TTX" |cut -f2 -d") ; do A="$A, $(echo "scale=1;$n/4*3/1.5"|bc)" done OUT=$(echo "$A" | sed -e s/^,.//) echo $OUT

The following Kitty settings reduce the cell size to remove surrounding space:

modify_font cell_height -2px modify_font cell_width -1px

Misc

A command like the following will display the style and postscript name of font files which can be useful when configuring a terminal emulator like Kitty which has text mode configuration for fonts.

fc-scan --format "%{file} : %{style} : %{postscriptname} \n" /usr/share/fonts/truetype/robotomono

The following commands are for selecting a font in the Kitty terminal emulator and then starting a new session displaying font choices to make sure it uses what you think it's using.

kitten choose-fonts kitty --debug-font-fallback

One problem I encountered while writing this post is the google "AI" that it gives as part of the default search page returning information that was correct but which was not easy to find in regular search results. This is bad performance by google and the result of using it is bad for society. Getting a LLM overview of a topic can be useful but when writing a document that's worth reading you can't include "trust me bro a LLM told me" you need an actual human source. The source can be documentation for a program that you don't use and while not great a forum discussion is adequate at this time. But eventually forums will start filling up with LLM generated answers and to continue this practice will end up with citing sites that have LLM output.

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →