3

How do I render a string in an ancient X11 bitmap font? I mean something like

-*-lucida-medium-r-normal-*-34-*-*-*-*-*-*-*

which I can find with xfontsel. I just need a command line tool etc. that displays a given string in that font. A similar TrueType font won't work, since it has to be pixel-perfect (I'm trying to get images of each letter to OCR an image in that font).

4

4 回答 4

3

我不确定是否有命令行工具。但是你总是可以用 Xlib 和 C 来做你自己的。

似乎有人编写了一个使用 Xlib 显示字符串的简单 X11 C 程序。更改几行并编译:

gcc -lX11 -o xrenderstring xrenderstring.c

..应该为你做。

我已经在这个要点中复制了整个程序(以防它从原始源中消失),修改为采用两个参数:1.字体名称,2.要呈现的字符串。

所以你会这样称呼它(当然是在编译之后):

./xrenderstring "-*-lucida-medium-r-normal-*-34-*-*-*-*-*-*-*" "Quick brown fox"
于 2012-07-27T10:51:12.033 回答
2

仅以该字体呈现字符:

  • xfd -fn '-*-lucida-medium-r-normal-*-34-*-*-*-*-*-*-*'
  • xfontsel -pattern '-*-lucida-medium-r-normal-*-34-*-*-*-*-*-*-*'

渲染一个特定的字符串:

  • xfontsel -pattern '-*-lucida-medium-r-normal-*-34-*-*-*-*-*-*-*' -sample 'The quick brown fox jumps the shark'
于 2012-07-31T05:40:19.860 回答
1

找到了一种骇人听闻的方式。将以下内容添加到 ~/.Xdefaults:

xedit*editWindow*font: -*-lucida-medium-r-normal-*-36-*-*-*-*-*-*-*

然后运行

$ xrdb -merge ~/.Xdefaults
$ xedit

然后我输入字符串,截取屏幕截图,然后在 gimp 中进行比较。

(事实证明,X11 字体毕竟不是正确的字体。我的来源使用 Lucida Sans 36 pt (ttf),但没有抗锯齿,可能还有奇怪的提示。)

于 2012-07-27T10:47:30.487 回答
0

您可以使用 ImageMagick。

额外的好处:

  • 可以渲染老式(固定)或TTF或其他字体
  • 可以渲染到屏幕或图像文件(PNG,无论如何)

简单的

对于短文本

echo "The quick brown fox jumps over the lazy dog." \
| convert \
-font '-*-fixed-*-*-*-*-18-*-*-*-*-*-iso8859-*' \
TEXT:- -trim fox.png

渲染到屏幕:

echo "The quick brown fox jumps over the lazy dog." \
| convert \
-font '-*-fixed-*-*-*-*-18-*-*-*-*-*-iso8859-*' \
TEXT:- -trim MIFF:- | display MIFF:-

移除-trim后可以看到 ImageMagick 使用画布进行绘制。

更多控制

  • 画布的尺寸。为避免两步计算,请创建一个比所需更大的画布,然后对其进行修剪。
  • 控制背景和前景色。
  • 控制 PNG 参数(每像素一位)
convert -size 2000x20 xc:black \
-font '-*-fixed-*-*-*-*-18-*-*-*-*-*-iso8859-*' \
-fill white \
-draw "text 0,15 'The quick brown fox jumps over the lazy dog.'" \
-trim +dither -depth 1 fox.png

下面生成的图像重 592 字节:

用 X11 字体“固定”渲染的句子。

获得独立构图的角色(用于以后的构图)

您可以只在上面一行中放置一个字符,但单独应用于多个字符会导致生成的图像不再具有相同的大小或相同的公共基线。

for CHAR in H e l o ' ' W r d
do
convert -size 9x13 xc:black \
-font '-*-fixed-*-*-*-*-18-*-*-*-*-*-iso8859-*' \
-fill white -stroke none \
-draw "text 0,10 '${CHAR}'" \
+dither -depth 2 fixed_char_${CHAR}.png
done

下面的命令会生成一个 PNG,显示单独加框的图像保持它们的共同对齐:

montage -mode concatenate -tile x1 fixed_char_H.png fixed_char_e.png \
fixed_char_l.png fixed_char_l.png fixed_char_o.png fixed_char_\ .png \
fixed_char_W.png fixed_char_o.png fixed_char_r.png fixed_char_l.png \
fixed_char_d.png helloworld.png
于 2020-04-03T09:57:40.260 回答