嵌入式Linux之freetype ascii 显示实例代码解析

本文介绍了如何在C语言中通过FreeType库加载字体文件simsun.ttc,设置字符大小和旋转角度,然后逐个渲染字符,包括处理中文字符和获取字体轮廓点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

main函数

int main( int     argc,
      char**  argv )
{
  FT_Library    library;
  FT_Face       face;

  FT_GlyphSlot  slot;
  FT_Matrix     matrix;                 /* transformation matrix */
  FT_Vector     pen;                    /* untransformed origin  */
  FT_Error      error;

  char*         filename;
  char*         text;

  double        angle;
  int           target_height;
  int           n, num_chars;


  if ( argc != 3 )
  {
    fprintf ( stderr, "usage: %s font sample-text\n", argv[0] );
    exit( 1 );
  }

  //1、字体文件simsun.ttc
  filename      = argv[1];                           /* first argument     */
  //2、显示字符
  text          = argv[2];                           /* second argument    */

  num_chars     = strlen( text );
  
  //3、旋转角度,FT_Set_Transform,matrix 
  angle         = ( 25.0 / 360 ) * 3.14159 * 2;      /* use 25 degrees     */

  target_height = HEIGHT;
 
  //4、初始化freetype 库
  error = FT_Init_FreeType( &library );              /* initialize library */
  /* error handling omitted */
  
  //5、打开字体文件simsun.ttc
  error = FT_New_Face( library, filename, 0, &face );/* create face object */
  /* error handling omitted */

  /* use 50pt at 100dpi */
  /*6、设置字体大小,一行有50 * 64 /64 / 72英寸,一英寸有 100 像素;
注意,使用FT_Set_Pixel_Sizes(face, 24, 0) 方便直观*/
  error = FT_Set_Char_Size( face, 50 * 64, 0,
                            100, 0 );                /* set character size */
  /* error handling omitted */

  /* cmap selection omitted;                                        */
  /* for simplicity we assume that the font contains a Unicode cmap */
  //7、使用slot接收 获取到的glyph
  slot = face->glyph;

  /* set up matrix */
  //8、使用angle构成旋转矩阵
  matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
  matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
  matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
  matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );

  /* the pen position in 26.6 cartesian space coordinates; */
  /* start at (300,200) relative to the upper left corner  */
  //9、使用pen设置字符初始打印位置,FT_Set_Transform
  pen.x = 300 * 64;
  pen.y = ( target_height - 200 ) * 64;
  //10、把num_chars个字符都打印出来
  for ( n = 0; n < num_chars; n++ )
  {
    /* set transformation */
    //11、设置字符变换(旋转、偏移)
    FT_Set_Transform( face, &matrix, &pen );

    /* load glyph image into the slot (erase previous one) */
    //12、此函数代替FT_Get_Char_Index、FT_Load_Glyph、FT_Render_Glyph
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
    if ( error )
      continue;                 /* ignore errors */

    /* now, draw to our target surface (convert position) */
    //13、把获取到的glyph的数据放在images数组中
    draw_bitmap( &slot->bitmap,
                 slot->bitmap_left,
                 target_height - slot->bitmap_top );

    /* increment pen position */
    //14、把字符显示位置加上当前显示位置,方便显示下一个字符,不重叠
    pen.x += slot->advance.x;
    pen.y += slot->advance.y;
  }
  //15、把draw_bitmap里的images全部打印出来
  show_image();

  FT_Done_Face    ( face );
  FT_Done_FreeType( library );

  return 0;
}

/* EOF */

draw_bitmap函数

void draw_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] |= bitmap->buffer[q * bitmap->width + p];
    }
  }
}

show_bitmap函数

void show_image( void )
{
  int  i, j;


  for ( i = 0; i < HEIGHT; i++ )
  {
    for ( j = 0; j < WIDTH; j++ )
      putchar( image[i][j] == 0 ? ' '
                                : image[i][j] < 128 ? '+'
                                                    : '*' );
    putchar( '\n' );
  }
}

扩展

1、显示中文:定义int数组,存放ASCII;然后FT_load_char直接输出chinese_str;这是因为char只有一个字节,而中文字符的ASCII需要两个字节;

2、显示中文:直接使用wchar存放中文字符;然后FT_load_char直接输出chinese_str;wchar是 wide char;

3、获取自型轮廓点:FT_Get_Glyph(face->glyph, &glyph);FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE,&bbox);获取到的bbox.xmin\bbox.xmax\bbox.ymin\bbox.ymax是左上角坐标和右下角坐标;

### 关于 LVGL 支持多字库的实现方法 LVGL 是一款专为嵌入式设备设计的轻量级图形界面库,其支持多种字体加载方式以适应不同的应用场景。以下是关于如何在 LVGL 中实现多字库的支持及相关资源说明。 #### 1. 使用内置字体 LVGL 提供了一些默认的 ASCII 和扩展字符集字体,这些字体可以直接通过配置文件 `lv_conf.h` 启用并设置大小[^1]。然而,默认字体并不包含完整的中文字体支持,因此需要引入外部字库来补充这一功能。 #### 2. 集成中文点阵字库 为了支持中文字体,可以利用已有的中文点阵字库资源。例如,在引用[2]中提到的一个仓库提供了专门针对 LVGL 的中文点阵字库,覆盖了多达 20902 个标准汉字,并提供三种不同尺寸(12x12、14x14、16x16)。开发者可以根据实际需求选择合适的点阵尺寸并将该字库集成到项目中[^2]。 具体操作如下: - 下载所需字库文件。 - 将字库文件放置到项目的指定路径下(如 `lvgl/src/font`),并通过 IDE 添加现有项完成导入[^5]。 - 修改 `lv_conf.h` 文件,注册新字体以便后续调用。假设字库名为 `myfont_16.c`,则需添加类似以下代码: ```c #include "myfont_16.c" ... static lv_font_t *chinese_font = &myfont_16; ``` 最后重新编译整个工程即可生效。 #### 3. 动态加载 TrueType 字体 (TTF) 除了静态链接固定字库外,还可以借助 FreeType 库动态解析 TTF 文件生成对应位图数据用于渲染文本。这种方法灵活性更高,尤其适合处理复杂场景下的多样化字体需求。根据引用[3]描述的内容可知,有现成方案可供参考学习如何基于 ESP32 平台结合 LVGL 实现此目标[^3]。 需要注意的是,由于涉及额外计算开销以及内存占用等问题,在选用前应充分评估硬件性能是否能够胜任此类任务。 #### 4. Arduino 环境下的应用实例 对于采用 Arduino 架构进行开发的朋友来说,也可以尝试使用由官方维护的适配版本库 `lv_arduino` 来简化部署流程[^4]。它不仅包含了基础组件还附带若干示范程序帮助快速入门实践上述概念。 --- ### 示例代码片段展示如何切换不同字体 下面给出一段简单的 C++ 示例演示怎样更改对象使用的字体属性: ```cpp // 假设已经初始化好显示屏驱动等相关环境变量 void setCustomFont(lv_obj_t* obj, const lv_font_t* font){ lv_style_t style; lv_style_init(&style); lv_style_set_text_font(&style, font); // 设置新的字体样式 lv_obj_add_style(obj,&style,LV_PART_MAIN | LV_STATE_DEFAULT ); } int main(){ ... /* 创建标签控件 */ lv_obj_t *label = lv_label_create(lv_scr_act()); /* 默认情况下会继承全局设定好的字体 */ setCustomFont(label,chinese_font); while(1){...} } ``` 以上就是有关 LVGL 支持多字库的一些常见做法及其关联资料汇总。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值