对于 128x64 OLED 显示器,我需要将一些 X11 位图字体转换为 C 中的数组。
我看到了一些字体,比如 /usr/share/fonts/X11/misc/9x15-ISO8859-1.pcf.gz
结果应该是这样的
/* Standard ASCII 6x8 font */
const char PROGMEM font6x8[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
Run Code Online (Sandbox Code Playgroud)
是否有将 pcf 字体转换为 C 中字符数组的工具?位图字体的好资源在哪里?
还有什么我错过的吗?
您可以使用convert
如下所示:C header file with bitmapped fonts
可能有很多命令可以将字体转换为位图,但是freetype API很容易编写您自己的代码,无需太多代码。
使用freetype 示例,我编写了一些您可以使用的内容作为开始,它输出一个.xbm
文件(是一个c
文件,但您可以使用图形程序gimp
来查看/编辑它)。
Notchegimp
还支持导出到.xbm
每个像素不同的字节。
的.xbm
由示例性导出的是每像素格式(1个黑色,0白)1个比特,则需要编辑的代码,以处理更多的颜色(freetype的通常使用8位灰度级位图缓冲区来呈现,如果渲染模式是FT_RENDER_MODE_NORMAL
,您可能还需要删除用于将 FT 缓冲区对齐到 1 个字节的临时位图的步骤)。
该to_bitmap
函数是完成从 FT 缓冲区到目标位图的转换的地方,
要构建示例,freetype
需要开发包,在Debian
(也可能Ubuntu
)上使用:
sudo apt-get install libfreetype6-dev
Run Code Online (Sandbox Code Playgroud)
简单make
地构建它。
使用带有字体路径作为参数的命令,输出打开 stdout
./font2c /usr/share/fonts/X11/misc/9x15-ISO8859-1.pcf.gz >c-bmp-font.xbm
Makefile
:
CFLAGS += -O2 -Wall
CFLAGS += $(shell freetype-config --cflags)
LIBS += $(shell freetype-config --libs)
font2c: font2c.c
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
clean:
-rm -f font2c
Run Code Online (Sandbox Code Playgroud)
font2c.c
:
#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <ftbitmap.h>
#define WIDTH 640
#define BYTEWIDTH (WIDTH)/8
#define HEIGHT 480
static unsigned char image[HEIGHT][BYTEWIDTH];
static FT_Library library;
static FT_Face face;
static FT_Error err;
static FT_Bitmap tempbitmap;
static void to_bitmap( FT_Bitmap* bitmap, FT_Int x, FT_Int y) {
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for ( i = x, p = 0; i < x_max; i++, p++ ) {
for ( j = y, q = 0; j < y_max; j++, q++ ) {
if ( (i < 0) || (j < 0) || (i >= WIDTH || j >= HEIGHT) )
continue;
image[j][i >> 3] |= (bitmap->buffer[q * bitmap->width + p]) << (i & 7);
}
}
}
static void draw_glyph(unsigned char glyph, int *x, int *y) {
FT_UInt glyph_index;
FT_GlyphSlot slot = face->glyph;
glyph_index = FT_Get_Char_Index( face, glyph );
if ((err = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT ))) {
fprintf( stderr, "warning: failed FT_Load_Glyph 0x%x %d\n", glyph, err);
return;
}
if ((err = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_MONO ))) {
fprintf( stderr, "warning: failed FT_Render_Glyph 0x%x %d\n", glyph, err);
return;
}
FT_Bitmap_New(&tempbitmap);
FT_Bitmap_Convert( library, &slot->bitmap, &tempbitmap, 1);
to_bitmap( &tempbitmap, *x, *y );
FT_Bitmap_Done( library, &tempbitmap );
*x += slot->advance.x >> 6;
}
static void out_xbm(int w, int h) {
int x, y;
printf("#define BMP_width %d\n", WIDTH);
printf("#define BMP_height %d\n", h);
printf("static char BMP_bits[] = {\n");
for (y=0; y < h; y++) {
printf("\t");
for (x=0; x < w; x++) {
printf("0x%x, ", image[y][x]);
}
printf("\n");
}
printf("\n}\n");
}
int main(int argc, char **argv) {
char *filename;
int x = 0, y = 0;
int g;
memset (image, 0, BYTEWIDTH*HEIGHT);
if (argc < 2) {
fprintf( stderr, "usage: font2c [font]\n");
exit(1);
}
filename = argv[1];
if ((err = FT_Init_FreeType( &library ))) {
fprintf( stderr, "error: Init_Freetype failed %d\n", err);
exit(1);
}
if ((err = FT_New_Face( library, filename, 0, &face ))) {
fprintf( stderr, "error: FT_New_Face failed %d\n", err);
exit(1);
}
for (g = 0; g < 256; g++) {
if (x+8 >= WIDTH) {
x = 0;
y += 15; // FIXME get ascender
}
draw_glyph(g, &x, &y);
}
out_xbm(BYTEWIDTH, HEIGHT);
FT_Done_Face( face );
FT_Done_FreeType( library );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14239 次 |
最近记录: |