栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

freetype显示一行文字

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

freetype显示一行文字

直接上代码:

#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;   
struct fb_fix_screeninfo fix;   
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;


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:
        {
            
            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 %dbppn", var.bits_per_pixel);
            break;
        }
    }
}

 
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 = %dn", 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;

    
    pen.x = 0;
    pen.y = 0;

    
    
    for (i = 0; i < wcslen(wstr); i++)
    {
        
        FT_Set_Transform(face, 0, &pen);

        
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char errorn");
            return -1;
        }

        
        error = FT_Get_Glyph(face->glyph, &glyph);
        if (error)
        {
            printf("FT_Get_Glyph error!n");
            return -1;
        }
        
        
        FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox);

        
        if ( glyph_bbox.xMin < bbox.xMin ) //更新最左边的字符外框,之后平移原点,更新下一位字符外框
            bbox.xMin = glyph_bbox.xMin;   //这样一直做下去,就可以把整个字符的所有外框全部更新到bbox中

        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;
        
        
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    
    *abbox = bbox;
}

//
int display_string(FT_Face face, wchar_t *wstr, int lcd_x, int lcd_y, double angle, int* y_limit_p)
{
    int i;
    int error;
    FT_Vector pen;
    FT_Glyph  glyph;
	FT_BBox bbox;
    FT_GlyphSlot slot = face->glyph;
	FT_Matrix	  matrix;
	//static int y_limit = var.yres;
	if(var.yres - lcd_y < *y_limit_p)//这些操作应该是在笛卡尔坐标系中操作的
		;
	else
	{
		lcd_y = var.yres - (*y_limit_p)  + 1;
	}
	
    
    int x = lcd_x;
    int y = var.yres - lcd_y;
	
	
	
    
    compute_string_bbox(face, wstr, &bbox);

    
    pen.x = (x - bbox.xMin) * 64;  //由外框反推原点
    pen.y = (y - bbox.yMax) * 64; 
	
	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 );
	
    
    for (i = 0; i < wcslen(wstr); i++)
    {
        
        FT_Set_Transform(face, &matrix, &pen);

        
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char errorn");
            return -1;
        }

        
        draw_bitmap( &slot->bitmap,
                        slot->bitmap_left,
                        var.yres - slot->bitmap_top);

        
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }
	*y_limit_p = var.yres - (bbox.yMax - bbox.yMin) - lcd_y;
	
    return 0;
}
int display_nums_string(FT_Face face, wchar_t **wstr, int lcd_x, int lcd_y, double angle, int strnum)
{
    int i;
    int error;
    FT_Vector pen;
    FT_Glyph  glyph;
	FT_BBox bbox;
    FT_GlyphSlot slot = face->glyph;
	FT_Matrix	  matrix;
	
	for(int j = 0; j < strnum; j++)
	{
		
		lcd_y = lcd_y + (bbox.yMax - bbox.yMin) + 1;
		
		
		
		int x = lcd_x;
		int y = var.yres - lcd_y;
		
		
		
		
		compute_string_bbox(face, wstr[j], &bbox);

		
		pen.x = (x - bbox.xMin) * 64;  //由外框反推原点
		pen.y = (y - bbox.yMax) * 64; 
		
		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 );
		
		
		for (i = 0; i < wcslen(wstr[j]); i++)
		{
			
			FT_Set_Transform(face, &matrix, &pen);

			
			error = FT_Load_Char(face, wstr[j][i], FT_LOAD_RENDER);
			if (error)
			{
				printf("FT_Load_Char errorn");
				return -1;
			}

			
			draw_bitmap( &slot->bitmap,
							slot->bitmap_left,
							var.yres - slot->bitmap_top);

			
			pen.x += slot->advance.x;
			pen.y += slot->advance.y;
		}
	}
    return 0;
}
// 修改程序,支持倾斜角度显示一行文字。

// 修改程序,支持显示多行文字,需要考虑这次输入的字符整体框架,是不是和上一次字符框架重合
//也就是判断这一次框架的左上角的坐标是否在y轴上小于上一次字符框架的右下角的y轴的值
//只要转化到一个坐标系下考虑,就比较好想
int main(int argc, char **argv)
{
    wchar_t *wstr = L"第一行显示大卫天龙";
	wchar_t *wstr2 = L"第二行显示";
	wchar_t *wstr3 = L"第三行显示123";
	wchar_t *wstr4[] = {L"第一行显示大卫天龙", L"第二行显示", L"第三行显示123"};
    FT_Library    library;
    FT_Face       face;
    int error;
    
    int font_size = 24;
    int lcd_x, lcd_y;
	double angle;
	int y_limit;
	
	
    if (argc < 5)
    {
        printf("Usage : %s     [font_size]n", argv[0]);
        return -1;
    }

    lcd_x = strtoul(argv[2], NULL, 0);      
    lcd_y = strtoul(argv[3], NULL, 0);      
    angle = ( 1.0* strtoul(argv[4], NULL, 0) / 360 ) * 3.14159 * 2;
    if (argc == 6)
        font_size = strtoul(argv[5], NULL, 0);      

    fd_fb = open("/dev/fb0", O_RDWR);
    if (fd_fb < 0)
    {
        printf("can't open /dev/fb0n");
        return -1;
    }

    if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
    {
        printf("can't get varn");
        return -1;
    }

    if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
    {
        printf("can't get fixn");
        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);
	y_limit = var.yres;
    if (fbmem == (unsigned char *)-1)
    {
        printf("can't mmapn");
        return -1;
    }

    
    memset(fbmem, 0, screen_size);

    error = FT_Init_FreeType( &library );              
    
    error = FT_New_Face( library, argv[1], 0, &face ); 

    FT_Set_Pixel_Sizes(face, font_size, 0);
	

    // display_string(face, wstr, lcd_x, lcd_y, angle, &y_limit);
    // display_string(face, wstr2, lcd_x, lcd_y, angle, &y_limit);//设定在相同的位置,必然会出发判断,将293行代码显示的字符下移一个字符框架的高度*****明天就测试这行
	// display_string(face, wstr3, lcd_x, lcd_y, angle, &y_limit);
	display_nums_string(face, wstr4, lcd_x, lcd_y, angle, 3);
    return 0;   
}

现在是我写的第一种方法,其实也不能说是第一种方法,我总共写了两种方式不同的多行显示。

现在这种是直接调用display_nums_string()函数,是在不同的行上显示一维数组中的多个字符串,原理很简单。就是利用每次调用的compute_string_bbox()函数计算出来的整个字符串外框的高度,然后在lcd坐标系下你想显示矢量字体的左上角的y轴坐标加上整个外框的高度,即

lcd_y = lcd_y + (bbox.yMax - bbox.yMin) + 1;

这样每次显示完一行字符串之后,都会下移一定高度。

第二种方法,其实我也不确定算不算多行显示。这种方法是调用display_string()函数(方法2和1只能同时显示一种,不然应该是会乱码),其原理就是我每次调用这个函数的时候都会判断你想要放置的位置上是不是已经存在矢量字体了,如果存在就沿y轴平移。同时提个建议吧,就是如果你想对屏幕进行其他的操作,一定要使用一种坐标系用到底,不要中间用笛卡尔后面就换成lcd坐标系会容易乱的。

有啥其他的问题,再补充

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309164.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号