#include #include #include #include #include #include #include #include #include #include #include #include #include FT_FREETYPE_H #include FT_GLYPH_H int fd_fb; struct fb_var_screeninfo var; /* Current var */ int screen_size; unsigned char *fbmem; unsigned int line_width; unsigned int pixel_width; /********************************************************************** * 函数名称: lcd_put_pixel * 功能描述: 在LCD指定位置上输出指定颜色(描点) * 输入参数: x坐标,y坐标,颜色 * 输出参数: 无 * 返 回 值: 会 * 修改日期 版本号 修改人 修改内容 * ----------------------------------------------- * 2020/05/12 V1.0 zh(angenao) 创建 ***********************************************************************/ void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width; unsigned short *pen_16; unsigned int *pen_32; unsigned int red, green, blue; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned int *)pen_8; switch (var.bits_per_pixel) { case 8: { *pen_8 = color; break; } case 16: { /* 565 */ red = (color >> 16) & 0xff; green = (color >> 8) & 0xff; blue = (color >> 0) & 0xff; color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3); *pen_16 = color; break; } case 32: { *pen_32 = color; break; } default: { printf("can't surport %dbpp\n", var.bits_per_pixel); break; } } } void draw_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y, unsigned int color) { FT_Int i, j, p, q; FT_Int x_max = x + bitmap->width; FT_Int y_max = y + bitmap->rows; for (j = y, q = 0; j < y_max; j++, q++) { for (i = x, p = 0; i < x_max; i++, p++) { if (i < 0 || j < 0 || i >= var.xres || j >= var.yres) continue; if (bitmap->buffer[q * bitmap->width + p] != 0) { lcd_put_pixel(i, j, color); } } } } void draw_bitmap_with_border(FT_Bitmap* bitmap, FT_Int char_x, FT_Int char_y, int border_size, unsigned int text_color, unsigned int border_color) { // 绘制边框 for (int dx = -border_size; dx <= border_size; ++dx) { for (int dy = -border_size; dy <= border_size; ++dy) { if (dx == 0 && dy == 0) continue; // 跳过中心点 for (int j = 0; j < bitmap->rows; ++j) { for (int i = 0; i < bitmap->width; ++i) { if (bitmap->buffer[j * bitmap->width + i] != 0) { int px = char_x + i + dx; int py = char_y + j + dy; if (px >= 0 && px < var.xres && py >= 0 && py < var.yres) { lcd_put_pixel(px, py, border_color); } } } } } } // 绘制文字 for (int j = 0; j < bitmap->rows; ++j) { for (int i = 0; i < bitmap->width; ++i) { if (bitmap->buffer[j * bitmap->width + i] != 0) { int px = char_x + i; int py = char_y + j; if (px >= 0 && px < var.xres && py >= 0 && py < var.yres) { lcd_put_pixel(px, py, text_color); } } } } } void draw_text_with_border(FT_Face face, const wchar_t* text, int x, int y, int border_size, unsigned int text_color, unsigned int border_color) { FT_GlyphSlot slot = face->glyph; int pen_x = x; // 初始笔位置 int pen_y = y; for (const wchar_t* p = text; *p; ++p) { if (FT_Load_Char(face, *p, FT_LOAD_RENDER)) { continue; // 如果加载失败,跳过这个字符 } // 计算当前字符的位置 int char_x = pen_x + slot->bitmap_left; int char_y = pen_y - slot->bitmap_top; // 绘制带有边框的字符 draw_bitmap_with_border(&slot->bitmap, char_x, char_y, border_size, text_color, border_color); // 更新笔位置到下一个字符 pen_x += slot->advance.x >> 6; // 字符间距通常是 advance 的值除以 64 } } int main(int argc, char **argv) { wchar_t *chinese_str = L"Hello World!"; FT_Library library; FT_Face face; int error; FT_Vector pen; FT_GlyphSlot slot; int font_size = 24; const char *filepathname = NULL; int lcd_x = 0, lcd_y = 0; if (argc < 5) { printf("Usage : %s [font_size] \n", argv[0]); return -1; } if (argc == 5){ filepathname = argv[1]; font_size = strtoul(argv[2], NULL, 0); lcd_x = strtoul(argv[3], NULL, 0); lcd_y = strtoul(argv[4], NULL, 0); } fd_fb = open("/dev/fb0", O_RDWR); if (fd_fb < 0) { printf("can't open /dev/fb0\n"); return -1; } if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) { printf("can't get var\n"); return -1; } line_width = var.xres * var.bits_per_pixel / 8; pixel_width = var.bits_per_pixel / 8; screen_size = var.xres * var.yres * var.bits_per_pixel / 8; fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if (fbmem == (unsigned char *)-1) { printf("can't mmap\n"); return -1; } /* 清屏: 全部设为白色 */ memset(fbmem, 0xcc, screen_size); /* 显示矢量字体 */ error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, filepathname, 0, &face ); /* create face object */ /* error handling omitted */ slot = face->glyph; FT_Set_Pixel_Sizes(face, font_size, 0); /* 确定座标: */ //pen.x = 0; //pen.y = 0; /* set transformation */ //FT_Set_Transform( face, 0, &pen); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER ); if (error) { printf("FT_Load_Char error\n"); return -1; } // draw_bitmap( &slot->bitmap, // var.xres/2, // var.yres/2, 0x00000000); // 使用白色字体和黑色边框 unsigned int white = 0x00FFFFFF; // 假设32位颜色模式下白色是0xFFFFFFFF unsigned int black = 0x00000000; // 黑色是0x00000000 int border_size = 1; // 边框宽度 // 绘制带边框的文字 draw_text_with_border(face, chinese_str, lcd_x, lcd_y, border_size, white, black); return 0; }