字体图形例程
自己的字体是怎么做的? 我不需要重量级算法(freetype、truetype、adobe 等),并且使用预渲染位图字体就可以了。
我确实想要抗锯齿,并且如果可能的话想要比例字体。
我听说我可以使用 Gimp 进行渲染(进行一些后期处理?)
我正在为带有 LCD 的嵌入式设备进行开发。 它有一个 32 位处理器,但我不想运行 Linux(过度杀伤力 - 太多的代码/数据空间,而我要使用的功能太少)
C。C++(如果需要),但 C 是首选。 算法和想法/概念在任何语言中都很好......-
Adam
How do you do your own fonts? I don't want a heavyweight algorithm (freetype, truetype, adobe, etc) and would be fine with pre-rendered bitmap fonts.
I do want anti-aliasing, and would like proportional fonts if possible.
I've heard I can use Gimp to do the rendering (with some post processing?)
I'm developing for an embedded device with an LCD. It's got a 32 bit processor, but I don't want to run Linux (overkill - too much code/data space for too little functionality that I would use)
C. C++ if necessary, but C is preferred. Algorithms and ideas/concepts are fine in any language...
-Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我以前的演示场景中,我经常在一张大位图图像中绘制字体中的所有字符。 在代码中,我存储了字体中每个字符的(X,Y)坐标,以及每个字符的宽度。 整个字体的高度通常是恒定的。 如果空间不是问题,您可以将所有字符放在一个网格中,即每个字符的左上角之间的距离保持恒定。
然后,渲染文本就变成一次将一个字母复制到目标位置的问题。 当时,我通常保留一种颜色作为“透明”颜色,但今天您绝对可以使用 alpha 通道。
一种可用于小型黑白字体的更简单的方法是直接在代码中定义字符:
XPM 文件格式实际上是一种具有 C 语法的文件格式,可用作存储字符的混合解决方案。
In my old demo-scene days I often drew all characters in the font in one big bitmap image. In the code, I stored the (X,Y) coordinates of each character in the font, as well as the width of each character. The height was usually constant throughout the font. If space isn't an issue, you can put all characters in a grid, that is - have a constant distance between the top-left corner of each character.
Rendering the text then becomes a matter of copying one letter at a time to the destination position. At that time, I usually reserved one color as being the "transparent" color, but you could definitely use an alpha-channel for this today.
A simpler approach, that can be used for small b/w fonts, is to define the characters directly in code:
The XPM file format is actually a file format with C syntax that can be used as a hybrid solution for storing the characters.
预渲染位图字体可能是最佳选择。 使用任何渲染字体,将字符排列在网格中,并将图像保存为简单的未压缩格式,如 PPM、BMP 或 TGA。 如果您想要抗锯齿,请确保使用支持透明度的格式(BMP 和 TGA 支持;PPM 不支持)。
为了支持比例宽度,您需要从网格中提取每个字符的宽度。 没有简单的方法可以做到这一点,这取决于您如何生成网格。 您可能可以编写一些简短的小程序来分析每个字符并找到最小边界框。 获得宽度数据后,将其放入辅助文件中,其中包含每个字符的坐标和大小。
最后,为了渲染字符串,您查找每个字符并将其矩形从字体位图位块传输到帧缓冲区上,将光栅位置前进字符的宽度。
Pre-rendered bitmap fonts are probably the way to go. Render your font using whatever, arrange the characters in a grid, and save the image in a simple uncompressed format like PPM, BMP or TGA. If you want antialiasing, make sure to use a format that supports transparency (BMP and TGA do; PPM does not).
In order to support proportional widths, you'll need to extract the widths of each character from the grid. There's no simple way to do this, it depends on how you generate the grid. You could probably write some short little program to analyze each character and find the minimal bounding box. Once you have the width data, you put it in an auxiliary file which contains the coordinates and sizes of each character.
Finally, to render a string, you look up each character and bitblit its rectangle from the font bitmap onto your frame buffer, advancing the raster position by the width of the character.
我们已成功使用 SRGP 字体包。 我们确实使用了固定间距字体,所以我不确定它是否可以比例字体。
We have successfully used the SRGP package for fonts. We did use fixed-pitch fonts, so I'm not sure if it can proportional fonts.
我们使用由anglecode#s位图字体生成器生成的位图字体:
http://www.angelcode。 com/products/bmfont/
这非常有用,因为它具有 XML 输出,可以轻松转换为您需要的任何数据格式。
Angel Code 的 bmfont 还为旧的 MudFont 替代方案添加了字距调整和更好的包装。
We're using bitmap fonts generated by anglecode#s bitmap font generator :
http://www.angelcode.com/products/bmfont/
This is very usable as it has XML output which will be easy to convert to any data format you need.
Angel Code's bmfont also adds kerning and better packing to the old alternative that was MudFont.