im6ull学习总结(三-五)freetype显示正行字

知识补充

笛卡尔坐标系

在这里插入图片描述
这里笛卡尔坐标系就是初高中学的直角坐标系的第一象限
lcd坐标系则不同
这两个坐标系如何转换
观察两个坐标系 点(x,y)的x坐标在两个坐标系中相同,纵坐标(y)存在着y+V-y=V V是整个屏幕的行数的像素点

如何将做到一行文字胖瘦不一

在这里插入图片描述

在显示一行文字时,这些文字会基于同一个基线来绘制位图:baseline(这个baseline不是最低点)。
在 baseline 上,每一个字符都有它的原点(origin),比如上图中 baseline
左边的黑色圆点就是字母“g”的原点。当前 origin 加上 advance 就可以得到
下一个字符的 origin,比如上图中 baseline 右边的黑色圆点。
在显示一行中多个文件字时,后一个文字的原点依赖于前一个文字的原点及 advance。
字符的位图是有可能越过 baseline 的,比如上图中字母“g”在 baseline
下方还有图像。
上图中红色方框内就是字母“g”所点据的位图,它的四个角落不一定与原点
重合。
上图中那些 xMin 、 xMax 、 yMin 、 yMax 如 何 获 得 ? 可 以 使 用
FT_Glyph_Get_CBox 函数获得一个字体的这些参数,将会保存在一个 FT_BBox结构体中,
在这里插入图片描述
要显示一行文字时,每一个字符都有自己外框:xMin、xMax、yMin、yMax。
把这些字符的 xMin、yMin 中的最小值取出来,把这些字符的 xMax、yMax 中的
最大值取出来,就可以确定这行文字的外框了。
在这里插入图片描述
第1步 先指定第 1 个字符的原点 pen 坐标为(0, 0),计算出它的外框
第2步 再计算右边字符的原点,也计算出它的外框
把所有字符都处理完后就可以得到一行文字的整体外框:假设外框左上角坐
标为(x’, y’)。
第3步 想在(x, y)处显示这行文字,调整一下 pen 坐标即可。怎么调整?
pen 为(0, 0)时对应左上角(x’, y’);那么左上角为(x, y)时就可以算出pen 为(x-x’, y-y’)。

freetype 的几个重要数据结构

FT_Library
对应 freetype 库,使用 freetype 之前要先调用以下代码:
FT_Library library; /* 对应 freetype 库 /
error = FT_Init_FreeType( &library ); /
初始化 freetype 库 /
FT_Face
它对应一个矢量字体文件,在源码中使用 FT_New_Face 函数打开字体文件
后,就可以得到一个 face。
FT_GlyphSlot
在这里插入图片描述
注意两点
glyph是face的一个成员
face->glyph会覆盖
一个 face 中有很多字符,生成一个字符的点阵位图时,位图保存在哪里?
保存在插槽中:face->glyph。
生成第 1 个字符位图时,它保存在 face->glyph 中;生成第 2 个字符位图
时,也会保存在 face->glyph 中,会覆盖第 1 个字符的位图。
FT_GlyphSlot slot = face->glyph; /
插槽: 字体的处理结果保存在这里 */
FT_Glyph
字体文件中保存有字符的原始关键点信息,使用 freetype 的函数可以放大、
缩小、旋转,这些新的关键点保存在插槽中(注意:位图也是保存在插槽中)。
新的关键点使用 FT_Glyph 来表示,可以使用这样的代码从 slot 中获得glyph:
error = FT_Get_Glyph(slot , &glyph);

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <sys/ioctl.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

int fd_fb;
struct fb_var_screeninfo var;   /* Current var */
struct fb_fix_screeninfo fix;   /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;

/* color : 0x00RRGGBB */
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;
        }
    }
}

/**********************************************************************
 * 函数名称: draw_bitmap
 * 功能描述: 根据bitmap位图,在LCD指定位置显示汉字
 * 输入参数: x坐标,y坐标,位图指针
 * 输出参数: 无
 * 返 回 值: 无
 * 修改日期        版本号     修改人        修改内容
 * -----------------------------------------------
 * 2020/05/12        V1.0     zh(angenao)         创建
 ***********************************************************************/ 
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;

    //printf("x = %d, y = %d\n", x, y);

    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;

            //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
            lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);
        }
    }
}

int compute_string_bbox(FT_Face       face, wchar_t *wstr, FT_BBox  *abbox)
{
    int i;
    int error;
    FT_BBox bbox;
    FT_BBox glyph_bbox;
    FT_Vector pen;
    FT_Glyph  glyph;
    FT_GlyphSlot slot = face->glyph;

    /* 初始化 */
    bbox.xMin = bbox.yMin = 32000;
    bbox.xMax = bbox.yMax = -32000;

    /* 指定原点为(0, 0) */
    pen.x = 0;
    pen.y = 0;

    /* 计算每个字符的bounding box */
    /* 先translate, 再load char, 就可以得到它的外框了 */
    /* wcslen是一个C库函数,用于计算宽字符字符串的长度(以宽字符的数量为单位),即获取宽字符字符串中的字符数(不包括终止空字符L'\0')。 */
    for (i = 0; i < wcslen(wstr); i++)
    {
        /* 转换:transformation */
        /* FT_Set_Transform是FreeType库中用于设置字形变换矩阵的函数。获得这个字符的原点*/
        FT_Set_Transform(face, 0, &pen);
        

        /* 加载位图: load glyph image into the slot (erase previous one) */
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        /* 取出glyph */
        error = FT_Get_Glyph(face->glyph, &glyph);
        if (error)
        {
            printf("FT_Get_Glyph error!\n");
            return -1;
        }
        
        /* 从glyph得到外框: bbox */
        FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox);

        /* 更新外框 */
        if ( glyph_bbox.xMin < bbox.xMin )
            bbox.xMin = glyph_bbox.xMin;

        if ( glyph_bbox.yMin < bbox.yMin )
            bbox.yMin = glyph_bbox.yMin;

        if ( glyph_bbox.xMax > bbox.xMax )
            bbox.xMax = glyph_bbox.xMax;

        if ( glyph_bbox.yMax > bbox.yMax )
            bbox.yMax = glyph_bbox.yMax;
        
        /* 计算下一个字符的原点: increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    /* return string bbox */
    *abbox = bbox;
}

/* face:生成的矢量字体文件
    wstr 要显示的文字字符串地址
    x 文字显示位置横坐标
    y 文字显示位置纵坐标
 */
int display_string(FT_Face     face, wchar_t *wstr, int lcd_x, int lcd_y)
{
    int i;
    int error;
    FT_BBox bbox;
    FT_Vector pen;
    FT_Glyph  glyph;
    FT_GlyphSlot slot = face->glyph;

    /* 把LCD坐标转换为笛卡尔坐标 */
    int x = lcd_x;
    int y = var.yres - lcd_y;

    /* 计算外框 */
    compute_string_bbox(face, wstr, &bbox);

    /* 左上角反推原点 */
    pen.x = (x - bbox.xMin) * 64; /* 单位: 1/64像素 */
    pen.y = (y - bbox.yMax) * 64; /* 单位: 1/64像素 */

    /* 处理每个字符 */
    for (i = 0; i < wcslen(wstr); i++)
    {
        /* 转换:transformation */
        FT_Set_Transform(face, 0, &pen);

        /* 加载位图: load glyph image into the slot (erase previous one) */
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        /* 在LCD上绘制: 使用LCD坐标 */
        draw_bitmap( &slot->bitmap,
                        slot->bitmap_left,
                        var.yres - slot->bitmap_top);

        /* 计算下一个字符的原点: increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    return 0;
}


int main(int argc, char **argv)
{ 
    //1、字符处理以Unicode类型
    wchar_t *wstr = L"坚持学习Linux";

    FT_Library    library;
    FT_Face       face;
    int error;
    FT_BBox bbox;
    int font_size = 24;
    int lcd_x, lcd_y;

    if (argc < 4)
    {
        printf("Usage : %s <font_file> <lcd_x> <lcd_y> [font_size]\n", argv[0]);
        return -1;
    }
    //2、获取显示位置 将str to ul
    lcd_x = strtoul(argv[2], NULL, 0);      
    lcd_y = strtoul(argv[3], NULL, 0);      
    
    if (argc == 5)
        //3、获取字体大小
        font_size = strtoul(argv[4], NULL, 0);      
    //4、以O_RDWR打开/dev/fb0驱动文件
    fd_fb = open("/dev/fb0", O_RDWR);
    if (fd_fb < 0)
    {
        printf("can't open /dev/fb0\n");
        return -1;
    }
    /* 5、A 获取lcd信息  这获得的是可变参数*/
    if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
    {
        printf("can't get var\n");
        return -1;
    }
    /* 5、B 获取lcd信息  这获得的是固定参数*/
    if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
    {
        printf("can't get fix\n");
        return -1;
    }
    /* 6、计算屏幕宽度,屏幕尺寸,像素的bpp以字节为单位 */
    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;
    /* 7、映射framebuffer */
    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, 0, screen_size);
    /* 8、初始化 FreeType库*/
    error = FT_Init_FreeType( &library );              /* initialize library */
    /* 9、根据字体库得到face对应一个矢量字体文件 */
    error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
    /* 10、设置字体大小 */
    FT_Set_Pixel_Sizes(face, font_size, 0);
    /*  11、打印到lcd*/
    display_string(face, wstr, lcd_x, lcd_y);
    
    return 0;   
}


  • 31
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值