From 189a3dcfd5ff9ff341c24ba2342100aaf1072cd2 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Sun, 10 Feb 2013 16:47:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=97=E4=BD=93=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97=E7=9A=84=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主要修改字体数据库的数据结构,font_database用于记录已经打开的字体文件信息,fontbitmap_database用于记录各种字体位图。 修改了部分函数的参数列表,新增相关处理函数。 还存在冗余代码,等调试完后再删除。 --- include/LCUI/LCUI_Font.h | 13 + include/LCUI/font/bitmapfont.h | 2 +- include/LCUI/font/fontlibrary.h | 28 ++- include/LCUI/font/textstyle.h | 2 +- src/font/bitmapfont.c | 110 +++------ src/font/fontlibrary.c | 409 ++++++++++++++++++-------------- src/font/textlayer.c | 9 +- 7 files changed, 303 insertions(+), 270 deletions(-) diff --git a/include/LCUI/LCUI_Font.h b/include/LCUI/LCUI_Font.h index 5ef90bf0..16851791 100755 --- a/include/LCUI/LCUI_Font.h +++ b/include/LCUI/LCUI_Font.h @@ -42,6 +42,19 @@ #define __LCUI_FONT_H__ #include + +#ifdef USE_FREETYPE + #include + #include FT_FREETYPE_H + #include FT_GLYPH_H + #include FT_OUTLINE_H + #define LCUI_FONT_RENDER_MODE FT_RENDER_MODE_MONO + #define LCUI_FONT_LOAD_FALGS (FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT) +#else + typedef void* FT_Library; + typedef void* FT_Face; +#endif + LCUI_BEGIN_HEADER #include LC_CHARSET_H diff --git a/include/LCUI/font/bitmapfont.h b/include/LCUI/font/bitmapfont.h index 549f70f3..c18fcdf2 100644 --- a/include/LCUI/font/bitmapfont.h +++ b/include/LCUI/font/bitmapfont.h @@ -58,4 +58,4 @@ int Open_Fontfile(LCUI_Font *font_data, char *fontfile); /* 获取现有的字体位图数据 */ LCUI_FontBMP * -Get_ExistFontBMP( LCUI_Font *font_data, wchar_t ch, int pixel_size ); +Get_ExistFontBMP( int font_id, wchar_t ch, int pixel_size ); diff --git a/include/LCUI/font/fontlibrary.h b/include/LCUI/font/fontlibrary.h index 78013f28..acb4ad06 100644 --- a/include/LCUI/font/fontlibrary.h +++ b/include/LCUI/font/fontlibrary.h @@ -8,11 +8,20 @@ void FontLIB_DestroyAll( void ); /* 初始化字体数据库 */ void FontLIB_Init( void ); -/* 获取字体的风格ID */ -int FontLIB_GetStyleID( const char *style_name ); +/* 通过字体文件路径来查找字体信息,并获取字体ID */ +int FontLIB_FindInfoByFilePath( const char *filepath ); -/* 获取字体的字族ID */ -int FontLIB_GetFamliyID( const char *family_name ); +/* 获取指定字族名的字体ID */ +int FontLIB_GetFontIDByFamilyName( const char *family_name ); + +/* 获取指定字体ID的字体face对象句柄 */ +FT_Face FontLIB_GetFontFace( int font_id ); + +/* 获取默认的字体ID */ +int FontLIB_GetDefaultFontID( void ); + +/* 设定默认的字体 */ +void FontLIB_SetDefaultFont( int id ); /* * 添加一个字体位图数据至数据库中 @@ -20,14 +29,15 @@ int FontLIB_GetFamliyID( const char *family_name ); * 的此数据会无效 * */ LCUI_FontBMP * -FontLIB_Add( wchar_t char_code, int family_id, - int style_id, int pixel_size, - LCUI_FontBMP *fontbmp_buff ); +FontLIB_AddFontBMP( wchar_t char_code, int font_id, + int pixel_size, LCUI_FontBMP *fontbmp_buff ); /* 获取字体位图数据 */ LCUI_FontBMP * -FontLIB_GetFontBMP( wchar_t char_code, int family_id, - int style_id, int pixel_size ); +FontLIB_GetFontBMP( wchar_t char_code, int font_id, int pixel_size ); + +/* 载入字体值数据库中 */ +int FontLIB_LoadFontFile( const char *filepath ); LCUI_END_HEADER diff --git a/include/LCUI/font/textstyle.h b/include/LCUI/font/textstyle.h index 5311967f..bfaf24a2 100644 --- a/include/LCUI/font/textstyle.h +++ b/include/LCUI/font/textstyle.h @@ -35,7 +35,7 @@ typedef struct _LCUI_TextStyle BOOL _fore_color:1; BOOL _pixel_size:1; - char family[256]; + int font_id; enum_font_style style :3; enum_font_weight weight :3; enum_font_decoration decoration :4; diff --git a/src/font/bitmapfont.c b/src/font/bitmapfont.c index 0c86eb1f..f5926fd1 100644 --- a/src/font/bitmapfont.c +++ b/src/font/bitmapfont.c @@ -39,8 +39,9 @@ * 没有,请查看:. * ****************************************************************************/ +#define USE_FREETYPE //#define DEBUG -#include "config.h" +//#include "config.h" #include #include LC_LCUI_H #include LC_MISC_H @@ -51,13 +52,6 @@ #include -#ifdef USE_FREETYPE -#include -#include FT_FREETYPE_H -#include FT_GLYPH_H -#include FT_OUTLINE_H -#endif - BOOL FontBMP_Valid(LCUI_FontBMP *bitmap) /* * 功能:检测位图数据是否有效 @@ -175,26 +169,11 @@ void Get_Default_FontBMP(unsigned short code, LCUI_FontBMP *out_bitmap) out_bitmap->advance.y = 8; } -/* 默认的字体文件路径 */ -static char default_font[1024] = LCUI_DEFAULT_FONTFILE; - -void Set_Default_Font(char *fontfile) -/* - * 功能:设定默认的字体文件路径 - * 说明:需要在LCUI初始化前使用,因为LCUI初始化时会打开默认的字体文件 - * */ -{ - if( !fontfile ) { - strcpy(default_font, ""); - } else { - strcpy(default_font, fontfile); - } -} - /* 初始化字体处理模块 */ void LCUIModule_Font_Init( void ) { char *p; + int font_id; LCUI_Font *font; font = &LCUI_Sys.default_font; @@ -217,12 +196,12 @@ void LCUIModule_Font_Init( void ) font->ft_face = NULL; /* 如果在环境变量中定义了字体文件路径,那就使用它 */ p = getenv("LCUI_FONTFILE"); - if( p ) { - strcpy(default_font, p); + if( !p ) { + p = LCUI_DEFAULT_FONTFILE; } - /* 打开默认字体文件 */ - Open_Fontfile(&LCUI_Sys.default_font, default_font); - FontLIB_Init(); + FontLIB_Init(); /* 初始化字体数据库 */ + font_id = FontLIB_LoadFontFile( p ); /* 载入默认的字体至库中 */ + FontLIB_SetDefaultFont( font_id ); /* 设定该字体为默认字体 */ } /* 停用字体处理模块 */ @@ -389,7 +368,7 @@ int Open_Fontfile(LCUI_Font *font_data, char *fontfile) #ifdef USE_FREETYPE int type; FT_Library library; - FT_Face face; + FT_Face face; FT_Error face_error = 0, lib_error = 0; type = font_data->type; @@ -517,45 +496,41 @@ Convert_FTGlyph( LCUI_FontBMP *des, FT_GlyphSlot slot, int render_mode ) #endif - -static int -Get_NewFontBMP( LCUI_Font *font_data, wchar_t ch, - int pixel_size, LCUI_FontBMP *out_bitmap) -/* - * 功能:获取单个wchar_t型字符的字体位图数据 - * 说明:LCUI_Font结构体中储存着已被打开的字体文件句柄和face对象的句柄,如果字体文件 - * 已经被成功打开一次,此函数不会再次打开字体文件。 - */ +/* 获取新的字体位图,并记录至字体位图库中 */ +int Get_NewFontBMP( int font_id, wchar_t ch, int pixel_size, + LCUI_FontBMP *out_bitmap ) { #ifdef USE_FREETYPE size_t size; BOOL have_space = FALSE; - FT_Face p_FT_Face = NULL; /* face对象的句柄 */ - FT_Error error; + FT_Face face; + int error; - if( font_data && font_data->ft_face ) { - p_FT_Face = font_data->ft_face; + if( font_id > 0 ) { + face = FontLIB_GetFontFace( font_id ); + if( !face ) { + Get_Default_FontBMP( ch, out_bitmap ); + return -1; + } } else { Get_Default_FontBMP( ch, out_bitmap ); return -1; } - /* 设定为UNICODE,默认的也是 */ - FT_Select_Charmap( p_FT_Face, FT_ENCODING_UNICODE ); /* 设定字体尺寸 */ - FT_Set_Pixel_Sizes( p_FT_Face, 0, pixel_size ); + FT_Set_Pixel_Sizes( face, 0, pixel_size ); /* 如果是空格 */ if( ch == ' ' ) { ch = 'a'; have_space = TRUE; } /* 这个函数只是简单地调用FT_Get_Char_Index和FT_Load_Glyph */ - error = FT_Load_Char( p_FT_Face, ch, font_data->load_flags); + error = FT_Load_Char( face, ch, LCUI_FONT_LOAD_FALGS ); if(error) { return error; } - size = Convert_FTGlyph( out_bitmap, p_FT_Face->glyph, font_data->render_mode ); + size = Convert_FTGlyph( out_bitmap, face->glyph, LCUI_FONT_RENDER_MODE ); /* 如果是空格则将位图内容清空 */ if( have_space ) { memset( out_bitmap->buffer, 0, size ); @@ -569,42 +544,15 @@ Get_NewFontBMP( LCUI_Font *font_data, wchar_t ch, /* 获取现有的字体位图数据 */ LCUI_FontBMP * -Get_ExistFontBMP( LCUI_Font *font_data, wchar_t ch, int pixel_size ) +Get_ExistFontBMP( int font_id, wchar_t ch, int pixel_size ) { LCUI_FontBMP *font_bmp, bmp_buff; - int error, family_id, style_id; - -#ifdef USE_FREETYPE - if( !font_data ) { - goto get_default; - } else { - if( !font_data->ft_face || !font_data->ft_lib ) { - error = Open_Fontfile( font_data, - font_data->font_file.string ); - - if( error ) { - goto get_default; - } - } - } - family_id = FontLIB_GetFamliyID( font_data->family_name.string ); - style_id = FontLIB_GetStyleID( font_data->style_name.string ); - goto skip_get_default; -get_default:; - family_id = FontLIB_GetFamliyID( "default" ); - style_id = FontLIB_GetStyleID( "default" ); - -skip_get_default:; -#else - family_id = FontLIB_GetFamliyID( "defalut" ); - style_id = FontLIB_GetStyleID( "defalut" ); -#endif - font_bmp = FontLIB_GetFontBMP( ch, family_id, style_id, pixel_size ); - if( !font_bmp ) { - Get_NewFontBMP( font_data, ch, pixel_size, &bmp_buff ); - font_bmp = FontLIB_Add( ch, family_id, style_id, - pixel_size, &bmp_buff ); + font_bmp = FontLIB_GetFontBMP( ch, font_id, pixel_size ); + if( font_bmp ) { + return font_bmp; } + Get_NewFontBMP( font_id, ch, pixel_size, &bmp_buff ); + font_bmp = FontLIB_AddFontBMP( ch, font_id, pixel_size, &bmp_buff ); return font_bmp; } diff --git a/src/font/fontlibrary.c b/src/font/fontlibrary.c index 4fcc7b52..d2599570 100644 --- a/src/font/fontlibrary.c +++ b/src/font/fontlibrary.c @@ -1,54 +1,42 @@ + +#define USE_FREETYPE + #include #include LC_LCUI_H +#include LC_ERROR_H #include LC_FONT_H +#define NAME_MAX_LEN 256 +#define PATH_MAX_LEN 1024 + typedef struct { int pixel_size; /* 位图像素大小 */ LCUI_FontBMP *bitmap; /* 位图数据 */ } LCUI_FontBMPItem; typedef struct { - int style_id; /* 风格ID */ + int font_id; /* 字体信息ID */ LCUI_Queue font_bmp; /* 位图库 */ -} LCUI_FontStyleItem; - -typedef struct { - int family_id; /* 字族ID */ - LCUI_Queue font_style; /* 风格库 */ -} LCUI_FontFamliyItem; +} LCUI_FontDataItem; typedef struct { wchar_t char_code; /* 字符码 */ - LCUI_Queue font_family; /* 字族库 */ -} LCUI_FontDataItem; - -#define MAX_LEN 256 + LCUI_Queue data; /* 与该字符关联的数据 */ +} LCUI_FontCharItem; typedef struct { - int style_id; /* 风格ID */ - char style_name[MAX_LEN]; /* 风格名 */ -} LCUI_FontStyle; - -typedef struct { - int family_id; /* 字族ID */ - char family_name[MAX_LEN]; /* 字族名 */ -} LCUI_FontFamliy; + int id; + char style_name[NAME_MAX_LEN]; /* 风格名 */ + char family_name[NAME_MAX_LEN]; /* 字族名 */ + char filepath[PATH_MAX_LEN]; /* 字体文件路径 */ + FT_Face face; +} LCUI_FontInfo; +static int font_count = 0; +static FT_Library library = NULL; static BOOL database_init = FALSE; -static LCUI_Queue family_table, style_table; -static LCUI_Queue font_database; - -/* 初始化字体的字族信息表 */ -static void FontLIB_FamliyTableInit( void ) -{ - Queue_Init( &family_table, sizeof(LCUI_FontFamliy), NULL ); -} - -/* 初始化字体的样式信息表 */ -static void FontLIB_StyleTableInit( void ) -{ - Queue_Init( &style_table, sizeof(LCUI_FontStyle), NULL ); -} +static LCUI_Queue font_database, fontbitmap_database; +static LCUI_FontInfo *default_font = NULL; static void FontLIB_DestroyBMP( LCUI_FontBMPItem *bmp ) { @@ -57,19 +45,19 @@ static void FontLIB_DestroyBMP( LCUI_FontBMPItem *bmp ) } } -static void FontLIB_DestroyStyle( LCUI_FontStyleItem *style ) +static void FontLIB_DestroyData( LCUI_FontDataItem *data ) { - Destroy_Queue( &style->font_bmp ); + Destroy_Queue( &data->font_bmp ); } -static void FontLIB_DestroyFamliy( LCUI_FontFamliyItem *family ) +static void FontLIB_Destroy( LCUI_FontCharItem *font ) { - Destroy_Queue( &family->font_style ); + Destroy_Queue( &font->data ); } -static void FontLIB_Destroy( LCUI_FontDataItem *font ) +static void FontLIB_DestroyFontInfo( LCUI_FontInfo *info ) { - Destroy_Queue( &font->font_family ); + } void FontLIB_DestroyAll( void ) @@ -77,28 +65,27 @@ void FontLIB_DestroyAll( void ) if( !database_init ) { return; } + database_init = FALSE; Destroy_Queue( &font_database ); + Destroy_Queue( &fontbitmap_database ); +#ifdef USE_FREETYPE + FT_Done_FreeType( library ); +#endif } -static void FontLIB_StyleInit( LCUI_FontStyleItem *item ) + +static void FontLIB_DataInit( LCUI_FontDataItem *item ) { Queue_Init( &item->font_bmp, sizeof(LCUI_FontBMPItem), FontLIB_DestroyBMP ); } -static void FontLIB_FamliyInit( LCUI_FontFamliyItem *item ) +static void FontLIB_CharInit( LCUI_FontCharItem *item ) { - Queue_Init( &item->font_style, - sizeof(LCUI_FontStyleItem), - FontLIB_DestroyStyle ); -} - -static void FontLIB_DataInit( LCUI_FontDataItem *item ) -{ - Queue_Init( &item->font_family, - sizeof(LCUI_FontFamliyItem), - FontLIB_DestroyFamliy ); + Queue_Init( &item->data, + sizeof(LCUI_FontDataItem), + FontLIB_DestroyData ); } /* 初始化字体数据库 */ @@ -107,84 +94,146 @@ void FontLIB_Init( void ) if( database_init ) { return; } - Queue_Init( &font_database, - sizeof(LCUI_FontDataItem), + Queue_Init( &fontbitmap_database, + sizeof(LCUI_FontCharItem), FontLIB_Destroy ); - - FontLIB_FamliyTableInit(); - FontLIB_StyleTableInit(); + Queue_Init( &font_database, + sizeof(LCUI_FontInfo), + FontLIB_DestroyFontInfo ); +#ifdef USE_FREETYPE + /* 当初始化库时发生了一个错误 */ + if ( FT_Init_FreeType( &library ) ) { + printf("%s: %s", __FUNCTION__, FT_INIT_ERROR); + return; + } +#else + library = NULL; +#endif database_init = TRUE; } - -/* 获取字体的风格ID */ -int FontLIB_GetStyleID( const char *style_name ) +/* 通过字体文件路径来查找字体信息,并获取字体ID */ +int FontLIB_FindInfoByFilePath( const char *filepath ) { int i, n; - LCUI_FontStyle *item; + LCUI_FontInfo *item; if( !database_init ) { return -1; } - n = Queue_Get_Total( &style_table ); + n = Queue_Get_Total( &font_database ); for(i=0; istyle_name ) == 0 ) { - return item->style_id; + if( strcmp( filepath, item->filepath ) == 0 ) { + return item->id; } } - item = malloc( sizeof(LCUI_FontStyle) ); - if( !item ) { - return -2; - } - item->style_id = rand(); - strncpy( item->style_name, style_name, MAX_LEN ); - Queue_Add_Pointer( &style_table, item ); - return item->style_id; + return -2; } -/* 获取字体的字族ID */ -int FontLIB_GetFamliyID( const char *family_name ) +/* 获取指定字体ID的字体face对象句柄 */ +LCUI_FontInfo *FontLIB_GetFont( int font_id ) { int i, n; - LCUI_FontFamliy *item; + LCUI_FontInfo *item; + + if( !database_init ) { + return NULL; + } + n = Queue_Get_Total( &font_database ); + for(i=0; iid == font_id ) { + return item; + } + } + return NULL; +} + +/* 获取指定字族名的字体ID */ +int FontLIB_GetFontIDByFamilyName( const char *family_name ) +{ + int i, n; + LCUI_FontInfo *item; if( !database_init ) { return -1; } - n = Queue_Get_Total( &family_table ); + n = Queue_Get_Total( &font_database ); for(i=0; ifamily_name ) == 0 ) { - return item->family_id; + /* 不区分大小写,进行对比 */ + if( strcasecmp( item->family_name, family_name) == 0 ) { + return item->id; } } - item = malloc( sizeof(LCUI_FontFamliy) ); - if( !item ) { - return -2; - } - item->family_id = rand(); /* 用随机数作为字族ID */ - strncpy( item->family_name, family_name, MAX_LEN ); /* 保存字族名 */ - Queue_Add_Pointer( &family_table, item ); - return item->family_id; + return -1; } +/* 获取指定字体ID的字体face对象句柄 */ +FT_Face FontLIB_GetFontFace( int font_id ) +{ + LCUI_FontInfo *info; + info = FontLIB_GetFont( font_id ); + if( !info ) { + return NULL; + } + return info->face; +} + +/* 获取默认的字体ID */ +int FontLIB_GetDefaultFontID( void ) +{ + if( !default_font ) { + return -1; + } + return default_font->id; +} + +/* 设定默认的字体 */ +void FontLIB_SetDefaultFont( int id ) +{ + LCUI_FontInfo *p; + p = FontLIB_GetFont( id ); + if( p ) { + default_font = p; + } +} + +/* 添加字体族,并返回该字族的ID */ +int +FontLIB_AddFontInfo( const char *family_name, const char *style_name, + const char *filepath, FT_Face face ) +{ + LCUI_FontInfo *info; + info = malloc( sizeof(LCUI_FontInfo) ); + info->id = ++font_count; + strncpy( info->family_name, family_name, NAME_MAX_LEN ); + strncpy( info->style_name, style_name, NAME_MAX_LEN ); + strncpy( info->filepath, style_name,PATH_MAX_LEN ); + info->face = face; + Queue_Add_Pointer( &font_database, info ); + return info->id; +} static LCUI_FontBMPItem * -FontLIB_GetBMPItem( LCUI_FontStyleItem *style_item, int pixel_size ) +FontLIB_GetBMPItem( LCUI_FontDataItem *data_item, int pixel_size ) { int i, n; LCUI_FontBMPItem *item; - n = Queue_Get_Total( &style_item->font_bmp ); + n = Queue_Get_Total( &data_item->font_bmp ); for( i=0; ifont_bmp, i ); + item = Queue_Get( &data_item->font_bmp, i ); if( !item ) { continue; } @@ -195,53 +244,34 @@ FontLIB_GetBMPItem( LCUI_FontStyleItem *style_item, int pixel_size ) return NULL; } -static LCUI_FontStyleItem * -FontLIB_GetStyleItemByID( LCUI_FontFamliyItem *family_item, int style_id ) -{ - int i, n; - LCUI_FontStyleItem *item; - - n = Queue_Get_Total( &family_item->font_style); - for( i=0; ifont_style, i ); - if( !item ) { - continue; - } - if( item->style_id == style_id ) { - return item; - } - } - return NULL; -} - -static LCUI_FontFamliyItem * -FontLIB_GetFamliyItemByID( LCUI_FontDataItem *data_item, int family_id ) -{ - int i, n; - LCUI_FontFamliyItem *item; - - n = Queue_Get_Total( &data_item->font_family ); - for( i=0; ifont_family, i ); - if( !item ) { - continue; - } - if( item->family_id == family_id ) { - return item; - } - } - return NULL; -} - static LCUI_FontDataItem * -FontLIB_GetDataItem( wchar_t char_code ) +FontLIB_GetDataItem( LCUI_FontCharItem *char_item, int id ) { int i, n; LCUI_FontDataItem *item; - n = Queue_Get_Total( &font_database ); + n = Queue_Get_Total( &char_item->data ); for( i=0; idata, i ); + if( !item ) { + continue; + } + if( item->font_id == id ) { + return item; + } + } + return NULL; +} + +static LCUI_FontCharItem * +FontLIB_GetCharItem( wchar_t char_code ) +{ + int i, n; + LCUI_FontCharItem *item; + + n = Queue_Get_Total( &fontbitmap_database ); + for( i=0; ifont_family, family ); - } - - /* 获取字体风格句柄,如果获取失败,则新增 */ - style = FontLIB_GetStyleItemByID( family, style_id ); - if( !style ) { - style = malloc( sizeof(LCUI_FontStyleItem) ); - if( !style ) { - return NULL; - } - FontLIB_StyleInit( style ); - Queue_Add_Pointer( &family->font_style, style ); + FontLIB_DataInit( data ); + data->font_id = font_id; + Queue_Add_Pointer( &font->data, data ); } /* 获取字体位图句柄,如果获取失败,则新增 */ - bmp = FontLIB_GetBMPItem( style, pixel_size ); + bmp = FontLIB_GetBMPItem( data, pixel_size ); if( !bmp ) { bmp = malloc( sizeof(LCUI_FontBMPItem) ); if( !bmp ) { @@ -312,7 +330,7 @@ FontLIB_Add( wchar_t char_code, int family_id, } bmp->pixel_size = pixel_size; bmp->bitmap = NULL; - Queue_Add_Pointer( &style->font_bmp, bmp ); + Queue_Add_Pointer( &data->font_bmp, bmp ); } /* 如果该指针为NULL,那么就申请一块空间 */ @@ -329,33 +347,78 @@ FontLIB_Add( wchar_t char_code, int family_id, /* 获取字体位图数据 */ LCUI_FontBMP * -FontLIB_GetFontBMP( wchar_t char_code, int family_id, - int style_id, int pixel_size ) +FontLIB_GetFontBMP( wchar_t char_code, int font_id, int pixel_size ) { - LCUI_FontDataItem *font; - LCUI_FontFamliyItem *family; - LCUI_FontStyleItem *style; + LCUI_FontCharItem *font; + LCUI_FontDataItem *data; LCUI_FontBMPItem *bmp; if( !database_init ) { return NULL; } - font = FontLIB_GetDataItem( char_code ); + font = FontLIB_GetCharItem( char_code ); if( !font ) { return NULL; } - family = FontLIB_GetFamliyItemByID( font, family_id ); - if( !family ) { + data = FontLIB_GetDataItem( font, font_id ); + if( !data ) { return NULL; } - style = FontLIB_GetStyleItemByID( family, style_id ); - if( !style ) { - return NULL; - } - bmp = FontLIB_GetBMPItem( style, pixel_size ); + bmp = FontLIB_GetBMPItem( data, pixel_size ); if( !bmp ) { return NULL; } return bmp->bitmap; } + +/* 载入字体值数据库中 */ +int FontLIB_LoadFontFile( const char *filepath ) +{ + int id, error_code; + + if( !filepath ) { + return -1; + } + /* 如果有同一文件路径的字族信息 */ + id = FontLIB_FindInfoByFilePath( filepath ); + if( id >= 0 ) { + return -1; + } + +#ifdef USE_FREETYPE + FT_Face face; + error_code = FT_New_Face( library, filepath , 0 , &face ); + if( error_code ) { + FT_Done_FreeType( library ); + printf("%s: ", __FUNCTION__); + if ( error_code == FT_Err_Unknown_File_Format ) { + printf( "%s", FT_UNKNOWN_FILE_FORMAT ); + } else { + printf("%s", FT_OPEN_FILE_ERROR); + } + perror( filepath ); + return -1; + } + /* 打印字体信息 */ + printf( "=============== font info ==============\n" + "family name: %s\n" + "style name : %s\n" + "========================================\n" , + face->family_name, face->style_name ); + + /* 设定为UNICODE,默认的也是 */ + FT_Select_Charmap( face, FT_ENCODING_UNICODE ); + /* 记录字体信息至数据库中 */ + id = FontLIB_AddFontInfo( face->family_name, face->style_name, filepath, face ); +#else + printf("%s: %s\n", __FUNCTION__, "warning: not font engine support!"); +#endif + return id; +} + +/* 移除指定ID的字体数据 */ +int FontLIB_DeleteFontInfo( int id ) +{ + return 0; +} diff --git a/src/font/textlayer.c b/src/font/textlayer.c index d976c1ba..e4e03a01 100644 --- a/src/font/textlayer.c +++ b/src/font/textlayer.c @@ -94,8 +94,7 @@ TextStyle_Init ( LCUI_TextStyle *data ) data->_family = FALSE; data->_back_color = FALSE; data->_fore_color = FALSE; - - memset( data->family, 0, sizeof(data->family) ); + data->font_id = FontLIB_GetDefaultFontID(); data->style = FONT_STYLE_NORMAL; data->weight = FONT_WEIGHT_NORMAL; data->decoration = FONT_DECORATION_NONE; @@ -108,7 +107,7 @@ void TextStyle_FontFamily( LCUI_TextStyle *style, const char *fontfamily ) /* 设置字体族 */ { - strncpy( style->family, fontfamily, sizeof(style->family) ); + style->font_id = FontLIB_GetFontIDByFamilyName( fontfamily ); } void @@ -528,7 +527,7 @@ TextLayer_Get_Char_BMP ( LCUI_TextStyle *default_style, LCUI_CharData *data ) pixel_size = default_style->pixel_size; } } - data->bitmap = Get_ExistFontBMP( &font, data->char_code, pixel_size ); + data->bitmap = Get_ExistFontBMP( default_style->font_id, data->char_code, pixel_size ); } static int @@ -1078,7 +1077,7 @@ TextLayer_Text_Set_Default_Style( LCUI_TextLayer *layer, LCUI_TextStyle style ) char_ptr->need_update = TRUE; } if(!old_style->_family) { - strcpy(old_style->family, style.family); + old_style->font_id = style.font_id; char_ptr->need_update = TRUE; } if(!old_style->_weight) {