⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xktv_ft.h

📁 freetype的C/C++ 的应用源代码,经过实际使用,稳定可靠 对于在嵌入式linux系统中使用TTF字库的情况尤其适合
💻 H
字号:
#ifndef XKTV_FT_C#define XKTV_FT_C/*****************************************************************************/
#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <ft2build.h>#include FT_FREETYPE_H/*****************************************************************************/
struct BitPlat{	unsigned char *	m_bf;	unsigned long	m_hy;	unsigned long	m_wx;	unsigned long	m_hu;	unsigned long	m_wu;};/*****************************************************************************/void BitPlat_freeplat(struct BitPlat *);void BitPlat_fillplat(struct BitPlat *,unsigned char);long BitPlat_initplat(struct BitPlat *,unsigned long , unsigned long);/*****************************************************************************/
void BitPlat_freeplat(struct BitPlat * vplat){	if(vplat->m_bf != NULL)	{		free( vplat->m_bf);		vplat->m_bf = NULL;	}	vplat->m_hy = 0;	vplat->m_wx = 0;	vplat->m_hu = 0;	vplat->m_wu = 0;}long BitPlat_initplat(struct BitPlat * vplat,	unsigned long w, unsigned long h){	if((vplat->m_bf = (unsigned char *)		malloc(w * h)) == NULL)	{		vplat->m_hy = 0;		vplat->m_wx = 0;		vplat->m_hu = 0;		vplat->m_wu = 0;		return(0);	}	else	{		vplat->m_hy = h;		vplat->m_wx = w;		vplat->m_hu = 0;		vplat->m_wu = 0;		return(1);	}}void BitPlat_fillplat(struct BitPlat * vplat,	unsigned char color){	unsigned long i,sz;	sz = vplat->m_hy * vplat->m_wx;	for(i = 0; i < sz; i ++)	{		vplat->m_bf[i] = color;	}}/*****************************************************************************//*****************************************************************************/static FT_Library	g_ttf_lib;static FT_Error		g_ttf_err;static FT_Face		g_ttf_face[8];
/*****************************************************************************/void ttfFont_init(void);void ttfFont_quit(void);void ttfFont_freefont(FT_Face *);long ttfFont_loadfont(FT_Face *,char *);long ttfFont_fontsize(FT_Face *,long *);long ttfFont_drawtext(FT_Face *,char *,struct BitPlat *,	unsigned char);long ttfFont_bandtext(FT_Face *,char *,struct BitPlat *,	unsigned char,unsigned char);void ttfFont_ptext(FT_Bitmap *,int,int,struct BitPlat *,	unsigned char);void ttfFont_pband(FT_Bitmap *,int,int,struct BitPlat *,	unsigned char,unsigned char);
/*****************************************************************************/void ttfFont_init(void){	g_ttf_err =	FT_Init_FreeType(&g_ttf_lib);}void ttfFont_quit(void){	FT_Done_FreeType( g_ttf_lib);}
/*****************************************************************************/void ttfFont_freefont(FT_Face *p_face){	FT_Done_Face(*p_face);}long ttfFont_loadfont(FT_Face *p_face,char *s_file){	g_ttf_err =	FT_New_Face(g_ttf_lib, s_file, 0, p_face);	return(g_ttf_err == 0);}long ttfFont_fontsize(FT_Face *p_face,long *p_size){	g_ttf_err =	FT_Set_Pixel_Sizes(*p_face, p_size[0], p_size[1]);	return(g_ttf_err == 0);}long ttfFont_drawtext(FT_Face *p_face, char *s_utf8,struct BitPlat *p_plat,	unsigned char color){	static FT_Matrix matrix;	static FT_Vector vector;	wchar_t *pstr,wstr[256] = {0,0,0,0,};	str_utf8_to_wchar(wstr, s_utf8, 255);	matrix.xx = 0x10000L;	matrix.xy = 0;	matrix.yx = 0;	matrix.yy = 0x10000L;	vector.x = 0;	vector.y = p_plat->m_hy << 5;	for(pstr = wstr; *pstr ; pstr ++)	{		FT_Set_Transform(*p_face, &matrix, &vector);		g_ttf_err =		FT_Load_Char(*p_face,*pstr, FT_LOAD_RENDER);		if(g_ttf_err != 0)			continue;		ttfFont_ptext(&((*p_face)->glyph->bitmap),			(*p_face)->glyph->bitmap_left,			p_plat->m_hy -			(*p_face)->glyph->bitmap_top,			p_plat,color);		vector.x += (*p_face)->glyph->advance.x;		vector.y += (*p_face)->glyph->advance.y;	}	p_plat->m_wu =  (*p_face)->glyph->bitmap_left +			(*p_face)->glyph->bitmap.width;	return(pstr - wstr);}long ttfFont_bandtext(FT_Face *p_face,char *s_utf8,struct BitPlat *p_plat,	unsigned char text_color,unsigned char band_color){	static FT_Matrix matrix;	static FT_Vector vector;	wchar_t *pstr,wstr[256] = {0,0,0,0,};	str_utf8_to_wchar(wstr, s_utf8, 255);	matrix.xx = 0x10000L;	matrix.xy = 0;	matrix.yx = 0;	matrix.yy = 0x10000L;	vector.x = 0;	vector.y = p_plat->m_hy << 5;	for(pstr = wstr; *pstr ; pstr ++)	{		FT_Set_Transform(*p_face, &matrix, &vector);		g_ttf_err =		FT_Load_Char(*p_face,*pstr, FT_LOAD_RENDER);		if(g_ttf_err != 0)			continue;		ttfFont_pband(&((*p_face)->glyph->bitmap),			(*p_face)->glyph->bitmap_left,			p_plat->m_hy -			(*p_face)->glyph->bitmap_top,			p_plat,text_color,band_color);		vector.x += (*p_face)->glyph->advance.x;		vector.y += (*p_face)->glyph->advance.y;	}	p_plat->m_wu =  (*p_face)->glyph->bitmap_left +			(*p_face)->glyph->bitmap.width;	return(pstr - wstr);}
/*****************************************************************************/void ttfFont_ptext(FT_Bitmap *p_bmp, int x, int y,struct BitPlat *p_plat,	unsigned char color){	unsigned char * p_src;	unsigned char * p_dst;	unsigned char * p_tmp;	int w = p_bmp->width;	int h = p_bmp->rows;	if(x < 0 || (unsigned)(x + w) >= p_plat->m_wx)		return;	if(y < 0 || (unsigned)(y + h) >= p_plat->m_hy)		return;	if(p_plat->m_hu < (unsigned)(y + h))	   p_plat->m_hu = (unsigned)(y + h);	p_src  = p_bmp->buffer;	p_dst  = p_plat->m_bf + x;	p_dst += p_plat->m_wx * y;	for(y = 0; y < h; y ++)	{		p_tmp = p_dst + y * p_plat->m_wx;		for(x = 0; x < w; x ++)		{			if(*p_src > 32)				*p_tmp = color;			p_src ++;			p_tmp ++;		}	}}void ttfFont_pband(FT_Bitmap *p_bmp, int x, int y,struct BitPlat *p_plat,	unsigned char text_color,unsigned char band_color){	unsigned char * p_src;	unsigned char * p_dst;	unsigned char * p_top;	unsigned char * p_lef;	unsigned char * p_bot;	unsigned char * p_rig;	int w = p_bmp->width;	int h = p_bmp->rows;	if(x < 0 || (unsigned)(x + w + 2) >= p_plat->m_wx)		return;	if(y < 0 || (unsigned)(y + h + 2) >= p_plat->m_hy)		return;	if(p_plat->m_hu < (unsigned)(y + h))	   p_plat->m_hu = (unsigned)(y + h);	p_src  = p_bmp->buffer;	p_dst  = p_plat->m_bf + x;	p_dst += p_plat->m_wx * y;	for(y = 0; y < h; y ++)	{		p_lef = p_dst + p_plat->m_wx * y;		p_top = p_lef + 1;		p_lef = p_lef + p_plat->m_wx;		p_rig = p_lef + 2;		p_bot = p_lef + p_plat->m_wx + 1;		for(x = 0; x < w; x ++)		{			if(*p_src > 32)			{				*p_top = band_color;				*p_lef = band_color;				*p_rig = band_color;				*p_bot = band_color;			}			p_src ++;			p_top ++;			p_lef ++;			p_rig ++;			p_bot ++;		}	}	p_src = p_bmp->buffer;	for(y = 0; y < h; y ++)	{		p_lef = p_dst + p_plat->m_wx * y;		p_top = p_lef + p_plat->m_wx + 1;		for(x = 0; x < w; x ++)		{			if(*p_src > 32)			{				*p_top = text_color;			}			p_src ++;			p_top ++;		}	}}
/*****************************************************************************/
#endif/*XKTV_FT_C*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -