diff --git a/src/font/bitmapfont.c b/src/font/bitmapfont.c index 172c74ae..42462374 100644 --- a/src/font/bitmapfont.c +++ b/src/font/bitmapfont.c @@ -227,7 +227,8 @@ void LCUI_Font_Init(LCUI_Font *font) strcpy(default_font, p); } /* 打开默认字体文件 */ - Open_Fontfile(&LCUI_Sys.default_font, default_font); + Open_Fontfile(&LCUI_Sys.default_font, default_font); + FontLIB_Init(); } void Font_Init(LCUI_Font *in) @@ -522,8 +523,9 @@ Convert_FTGlyph( LCUI_FontBMP *des, FT_GlyphSlot slot, int render_mode ) #endif -int -Get_FontBMP( LCUI_Font *font_data, wchar_t ch, + +static int +Get_NewFontBMP( LCUI_Font *font_data, wchar_t ch, int pixel_size, LCUI_FontBMP *out_bitmap) /* * 功能:获取单个wchar_t型字符的字体位图数据 @@ -538,27 +540,13 @@ Get_FontBMP( LCUI_Font *font_data, wchar_t ch, FT_Face p_FT_Face = NULL; /* face对象的句柄 */ FT_Error error; - if( font_data ) { - /* 如果font_data有效,则打开font_data中的指定的字体文件,并将字体文件 - * 和face对象的句柄保存至结构体中。 - * 当然,如果LCUI_Font结构体有有效的字体文件和face对象的句柄,就不会再重新 - * 打开字体文件。 - */ - if( !font_data->ft_face || !font_data->ft_lib ) { - error = Open_Fontfile( font_data, - font_data->font_file.string); - if( error ) { - Get_Default_FontBMP( ch, out_bitmap ); - return 1; - } - } - /* 引用face对象句柄 */ + if( font_data && font_data->ft_face ) { p_FT_Face = font_data->ft_face; } else { - /* 使用内置的字体位图 */ Get_Default_FontBMP( ch, out_bitmap ); return -1; } + /* 设定为UNICODE,默认的也是 */ FT_Select_Charmap( p_FT_Face, FT_ENCODING_UNICODE ); /* 设定字体尺寸 */ @@ -585,4 +573,44 @@ Get_FontBMP( LCUI_Font *font_data, wchar_t ch, #endif } +/* 获取现有的字体位图数据 */ +LCUI_FontBMP * +Get_ExistFontBMP( LCUI_Font *font_data, 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 ); + } + return font_bmp; +} diff --git a/src/font/fontlibrary.c b/src/font/fontlibrary.c index 73a54d44..4fcc7b52 100644 --- a/src/font/fontlibrary.c +++ b/src/font/fontlibrary.c @@ -13,13 +13,13 @@ typedef struct { } LCUI_FontStyleItem; typedef struct { - int famliy_id; /* 字族ID */ + int family_id; /* 字族ID */ LCUI_Queue font_style; /* 风格库 */ } LCUI_FontFamliyItem; typedef struct { wchar_t char_code; /* 字符码 */ - LCUI_Queue font_famliy; /* 字族库 */ + LCUI_Queue font_family; /* 字族库 */ } LCUI_FontDataItem; #define MAX_LEN 256 @@ -30,18 +30,18 @@ typedef struct { } LCUI_FontStyle; typedef struct { - int famliy_id; /* 字族ID */ - char famliy_name[MAX_LEN]; /* 字族名 */ + int family_id; /* 字族ID */ + char family_name[MAX_LEN]; /* 字族名 */ } LCUI_FontFamliy; static BOOL database_init = FALSE; -static LCUI_Queue famliy_table, style_table; +static LCUI_Queue family_table, style_table; static LCUI_Queue font_database; /* 初始化字体的字族信息表 */ static void FontLIB_FamliyTableInit( void ) { - Queue_Init( &famliy_table, sizeof(LCUI_FontFamliy), NULL ); + Queue_Init( &family_table, sizeof(LCUI_FontFamliy), NULL ); } /* 初始化字体的样式信息表 */ @@ -62,14 +62,14 @@ static void FontLIB_DestroyStyle( LCUI_FontStyleItem *style ) Destroy_Queue( &style->font_bmp ); } -static void FontLIB_DestroyFamliy( LCUI_FontFamliyItem *famliy ) +static void FontLIB_DestroyFamliy( LCUI_FontFamliyItem *family ) { - Destroy_Queue( &famliy->font_style ); + Destroy_Queue( &family->font_style ); } static void FontLIB_Destroy( LCUI_FontDataItem *font ) { - Destroy_Queue( &font->font_famliy ); + Destroy_Queue( &font->font_family ); } void FontLIB_DestroyAll( void ) @@ -96,7 +96,7 @@ static void FontLIB_FamliyInit( LCUI_FontFamliyItem *item ) static void FontLIB_DataInit( LCUI_FontDataItem *item ) { - Queue_Init( &item->font_famliy, + Queue_Init( &item->font_family, sizeof(LCUI_FontFamliyItem), FontLIB_DestroyFamliy ); } @@ -113,6 +113,7 @@ void FontLIB_Init( void ) FontLIB_FamliyTableInit(); FontLIB_StyleTableInit(); + database_init = TRUE; } @@ -146,7 +147,7 @@ int FontLIB_GetStyleID( const char *style_name ) } /* 获取字体的字族ID */ -int FontLIB_GetFamliyID( const char *famliy_name ) +int FontLIB_GetFamliyID( const char *family_name ) { int i, n; LCUI_FontFamliy *item; @@ -154,24 +155,24 @@ int FontLIB_GetFamliyID( const char *famliy_name ) if( !database_init ) { return -1; } - n = Queue_Get_Total( &famliy_table ); + n = Queue_Get_Total( &family_table ); for(i=0; ifamliy_name ) == 0 ) { - return item->famliy_id; + if( strcmp( family_name, item->family_name ) == 0 ) { + return item->family_id; } } item = malloc( sizeof(LCUI_FontFamliy) ); if( !item ) { return -2; } - item->famliy_id = rand(); /* 用随机数作为字族ID */ - strncpy( item->famliy_name, famliy_name, MAX_LEN ); /* 保存字族名 */ - Queue_Add_Pointer( &famliy_table, item ); - return item->famliy_id; + item->family_id = rand(); /* 用随机数作为字族ID */ + strncpy( item->family_name, family_name, MAX_LEN ); /* 保存字族名 */ + Queue_Add_Pointer( &family_table, item ); + return item->family_id; } @@ -195,14 +196,14 @@ FontLIB_GetBMPItem( LCUI_FontStyleItem *style_item, int pixel_size ) } static LCUI_FontStyleItem * -FontLIB_GetStyleItemByID( LCUI_FontFamliyItem *famliy_item, int style_id ) +FontLIB_GetStyleItemByID( LCUI_FontFamliyItem *family_item, int style_id ) { int i, n; LCUI_FontStyleItem *item; - n = Queue_Get_Total( &famliy_item->font_style); + n = Queue_Get_Total( &family_item->font_style); for( i=0; ifont_style, i ); + item = Queue_Get( &family_item->font_style, i ); if( !item ) { continue; } @@ -214,18 +215,18 @@ FontLIB_GetStyleItemByID( LCUI_FontFamliyItem *famliy_item, int style_id ) } static LCUI_FontFamliyItem * -FontLIB_GetFamliyItemByID( LCUI_FontDataItem *data_item, int famliy_id ) +FontLIB_GetFamliyItemByID( LCUI_FontDataItem *data_item, int family_id ) { int i, n; LCUI_FontFamliyItem *item; - n = Queue_Get_Total( &data_item->font_famliy ); + n = Queue_Get_Total( &data_item->font_family ); for( i=0; ifont_famliy, i ); + item = Queue_Get( &data_item->font_family, i ); if( !item ) { continue; } - if( item->famliy_id == famliy_id ) { + if( item->family_id == family_id ) { return item; } } @@ -257,12 +258,12 @@ FontLIB_GetDataItem( wchar_t char_code ) * 的此数据会无效 * */ LCUI_FontBMP * -FontLIB_Add( wchar_t char_code, int famliy_id, +FontLIB_Add( wchar_t char_code, int family_id, int style_id, int pixel_size, LCUI_FontBMP *fontbmp_buff ) { LCUI_FontDataItem *font; - LCUI_FontFamliyItem *famliy; + LCUI_FontFamliyItem *family; LCUI_FontStyleItem *style; LCUI_FontBMPItem *bmp; @@ -281,25 +282,25 @@ FontLIB_Add( wchar_t char_code, int famliy_id, } /* 获取字体字族句柄,如果获取失败,则新增 */ - famliy = FontLIB_GetFamliyItemByID( font, famliy_id ); - if( !famliy ) { - famliy = malloc( sizeof(LCUI_FontFamliyItem) ); - if( !famliy ) { + family = FontLIB_GetFamliyItemByID( font, family_id ); + if( !family ) { + family = malloc( sizeof(LCUI_FontFamliyItem) ); + if( !family ) { return NULL; } - FontLIB_FamliyInit( famliy ); - Queue_Add_Pointer( &font->font_famliy, famliy ); + FontLIB_FamliyInit( family ); + Queue_Add_Pointer( &font->font_family, family ); } /* 获取字体风格句柄,如果获取失败,则新增 */ - style = FontLIB_GetStyleItemByID( famliy, style_id ); + style = FontLIB_GetStyleItemByID( family, style_id ); if( !style ) { style = malloc( sizeof(LCUI_FontStyleItem) ); if( !style ) { return NULL; } FontLIB_StyleInit( style ); - Queue_Add_Pointer( &famliy->font_style, style ); + Queue_Add_Pointer( &family->font_style, style ); } /* 获取字体位图句柄,如果获取失败,则新增 */ @@ -328,11 +329,11 @@ FontLIB_Add( wchar_t char_code, int famliy_id, /* 获取字体位图数据 */ LCUI_FontBMP * -FontLIB_GetFontBMP( wchar_t char_code, int famliy_id, +FontLIB_GetFontBMP( wchar_t char_code, int family_id, int style_id, int pixel_size ) { LCUI_FontDataItem *font; - LCUI_FontFamliyItem *famliy; + LCUI_FontFamliyItem *family; LCUI_FontStyleItem *style; LCUI_FontBMPItem *bmp; @@ -344,11 +345,11 @@ FontLIB_GetFontBMP( wchar_t char_code, int famliy_id, if( !font ) { return NULL; } - famliy = FontLIB_GetFamliyItemByID( font, famliy_id ); - if( !famliy ) { + family = FontLIB_GetFamliyItemByID( font, family_id ); + if( !family ) { return NULL; } - style = FontLIB_GetStyleItemByID( famliy, style_id ); + style = FontLIB_GetStyleItemByID( family, style_id ); if( !style ) { return NULL; } diff --git a/src/font/textlayer.c b/src/font/textlayer.c index ebdcb3ab..d976c1ba 100644 --- a/src/font/textlayer.c +++ b/src/font/textlayer.c @@ -175,8 +175,8 @@ Destroy_Special_KeyWord(Special_KeyWord *key) static void Destroy_CharData(LCUI_CharData *data) { - FontBMP_Free( &data->bitmap ); - free( data->data ); + //FontBMP_Free( &data->bitmap ); + //free( data->data ); //if( data->using_quote == IS_FALSE ) { //free( data->data ); //} @@ -498,14 +498,13 @@ TextLayer_Clear( LCUI_CharData *char_ptr ) { static LCUI_Rect area; - //printf("pos: %d,%d, max_h: %d\n", pos.x, pos.y, max_h); /* 计算区域范围 */ - area.x = pos.x + char_ptr->bitmap.left; + area.x = pos.x + char_ptr->bitmap->left; area.y = pos.y + max_h-1; - area.y -= char_ptr->bitmap.top; - area.width = char_ptr->bitmap.width; - area.height = char_ptr->bitmap.rows; + area.y -= char_ptr->bitmap->top; + area.width = char_ptr->bitmap->width; + area.height = char_ptr->bitmap->rows; /* 记录需刷新的区域 */ RectQueue_Add( &layer->clear_area, area ); //printf("record area: %d,%d,%d,%d\n", area.x, area.y, area.width, area.height); @@ -529,7 +528,7 @@ TextLayer_Get_Char_BMP ( LCUI_TextStyle *default_style, LCUI_CharData *data ) pixel_size = default_style->pixel_size; } } - Get_FontBMP( &font, data->char_code, pixel_size, &data->bitmap ); + data->bitmap = Get_ExistFontBMP( &font, data->char_code, pixel_size ); } static int @@ -626,7 +625,7 @@ TextLayer_Update_RowSize (LCUI_TextLayer *layer, int row ) continue; } } - size.w += char_data->bitmap.advance.x; + size.w += char_data->bitmap->advance.x; if( char_data->data ) { if( char_data->data->_pixel_size ) { if( size.h < char_data->data->pixel_size + 2) { @@ -676,7 +675,7 @@ TextLayer_Init( LCUI_TextLayer *layer ) layer->password_char.need_update = FALSE; layer->password_char.data = NULL; layer->password_char.char_code = 0; - FontBMP_Init( &layer->password_char.bitmap ); + layer->password_char.bitmap = NULL; layer->default_data.pixel_size = 12; layer->current_src_pos = 0; @@ -749,10 +748,10 @@ __TextLayer_OldArea_Erase( LCUI_Widget *widget, LCUI_TextLayer *layer ) if( !char_ptr ) { continue; } - if( x+char_ptr->bitmap.advance.x >= 0 ) { + if( x+char_ptr->bitmap->advance.x >= 0 ) { break; } - x += char_ptr->bitmap.advance.x; + x += char_ptr->bitmap->advance.x; } area.height = layer->default_data.pixel_size+2; @@ -762,11 +761,11 @@ __TextLayer_OldArea_Erase( LCUI_Widget *widget, LCUI_TextLayer *layer ) continue; } area.y = row_ptr->max_size.h-1; - area.y -= char_ptr->bitmap.top; - if( area.y + char_ptr->bitmap.rows > area.height ) { - area.height = area.y + char_ptr->bitmap.rows; + area.y -= char_ptr->bitmap->top; + if( area.y + char_ptr->bitmap->rows > area.height ) { + area.height = area.y + char_ptr->bitmap->rows; } - x += char_ptr->bitmap.advance.x; + x += char_ptr->bitmap->advance.x; if( x > widget->size.w ) { break; } @@ -864,8 +863,8 @@ TextLayer_Draw( LCUI_Widget *widget, LCUI_TextLayer *layer, int mode ) p_data = &layer->password_char; } /* 如果当前字的位图的X轴跨距不在有效绘制区域内 */ - if( pos.x + p_data->bitmap.advance.x <= 0) { - pos.x += p_data->bitmap.advance.x; + if( pos.x + p_data->bitmap->advance.x <= 0) { + pos.x += p_data->bitmap->advance.x; continue; } /* 获取该字体位图的大致尺寸 */ @@ -883,18 +882,18 @@ TextLayer_Draw( LCUI_Widget *widget, LCUI_TextLayer *layer, int mode ) // i, p_data, p_data->char_code ); p_data->need_update = FALSE; /* 计算区域范围 */ - area.x = pos.x + p_data->bitmap.left; + area.x = pos.x + p_data->bitmap->left; area.y = pos.y + p_row->max_size.h-1; - area.y -= p_data->bitmap.top; - area.height = p_data->bitmap.rows; - area.width = p_data->bitmap.width; + area.y -= p_data->bitmap->top; + area.height = p_data->bitmap->rows; + area.width = p_data->bitmap->width; /* 贴上字体位图 */ FontBMP_Mix( graph, Pos(area.x, area.y), - &p_data->bitmap, color, mode ); + p_data->bitmap, color, mode ); /* 记录该区域,以刷新显示到屏幕上 */ Widget_InvalidArea( widget, area ); } - pos.x += p_data->bitmap.advance.x; + pos.x += p_data->bitmap->advance.x; if( pos.x > widget->size.w ) { break; } @@ -1028,7 +1027,7 @@ TextLayer_CharLater_Refresh( LCUI_TextLayer *layer, LCUI_Pos char_pos ) if( !char_ptr ) { continue; } - pos.x += char_ptr->bitmap.advance.x; + pos.x += char_ptr->bitmap->advance.x; } for( i=char_pos.x; imax_size.h, char_ptr ); /* 标记该字的位图需要重绘 */ char_ptr->need_update = TRUE; - pos.x += char_ptr->bitmap.advance.x; + pos.x += char_ptr->bitmap->advance.x; } } @@ -1101,12 +1100,10 @@ TextLayer_Text_Set_Default_Style( LCUI_TextLayer *layer, LCUI_TextStyle style ) skip_style_cmp:; if(char_ptr->need_update) { TextLayer_Clear( layer, pos, row_ptr->max_size.h, char_ptr ); - pos.x += char_ptr->bitmap.advance.x; - /* 重新获取字体位图 */ - FontBMP_Free(&char_ptr->bitmap); + pos.x += char_ptr->bitmap->advance.x; TextLayer_Get_Char_BMP ( &layer->default_data, char_ptr ); } else { - pos.x += char_ptr->bitmap.advance.x; + pos.x += char_ptr->bitmap->advance.x; } } pos.y += row_ptr->max_size.h; @@ -1238,7 +1235,7 @@ TextLayer_Text_Process( LCUI_TextLayer *layer, int pos_type, char *new_text ) DEBUG_MSG1( "total char: %d\n", total ); DEBUG_MSG1( "total row: %d\n", total_row ); - FontBMP_Init( &char_data.bitmap ); + char_data.bitmap = NULL; /* 先记录这一行需要刷新的区域,起点为光标所在位置 */ TextLayer_CharLater_Refresh( layer, cur_pos ); for(p=buff, finish=buff+total; p 0) { DEBUG_MSG2( "ignore = %d\n", n_ignore ); @@ -1365,10 +1362,10 @@ TextLayer_Text_GenerateBMP( LCUI_TextLayer *layer ) DEBUG_MSG1("no display\n"); continue; } - if( FontBMP_Valid( &char_ptr->bitmap ) ) { + if( FontBMP_Valid( char_ptr->bitmap ) ) { DEBUG_MSG1("have FontBMP\n"); if( !refresh ) { - pos.x += char_ptr->bitmap.advance.x; + pos.x += char_ptr->bitmap->advance.x; continue; } } else { @@ -1376,10 +1373,10 @@ TextLayer_Text_GenerateBMP( LCUI_TextLayer *layer ) DEBUG_MSG1( "generate FontBMP, char code: %d\n", char_ptr->char_code ); TextLayer_Get_Char_BMP ( &layer->default_data, char_ptr ); } - DEBUG_MSG1( "char_data->bitmap.advance.x: %d\n", char_ptr->bitmap.advance.x ); + DEBUG_MSG1( "char_data->bitmap->advance.x: %d\n", char_ptr->bitmap->advance.x ); TextLayer_Clear( layer, pos, row_ptr->max_size.h, char_ptr ); char_ptr->need_update = TRUE; - pos.x += char_ptr->bitmap.advance.x; + pos.x += char_ptr->bitmap->advance.x; } refresh = FALSE; /* 更新当前行的尺寸 */ @@ -1408,7 +1405,7 @@ TextLayer_Print_Info( LCUI_TextLayer *layer ) char_ptr = Queue_Get( &row_ptr->string, i ); printf( "char code: %d, display: %d\n", char_ptr->char_code, char_ptr->display ); - Print_FontBMP_Info( &char_ptr->bitmap ); + Print_FontBMP_Info( char_ptr->bitmap ); } } printf("\n\n"); @@ -1629,7 +1626,7 @@ TextLayer_Set_Cursor_PixelPos( LCUI_TextLayer *layer, LCUI_Pos pixel_pos ) if( !char_ptr ) { continue; } - tmp = char_ptr->bitmap.advance.x; + tmp = char_ptr->bitmap->advance.x; if( pixel_pos.x >= tmp/2 ) { pixel_pos.x -= tmp; new_pos.x += tmp; @@ -1704,7 +1701,7 @@ TextLayer_Set_Cursor_Pos( LCUI_TextLayer *layer, LCUI_Pos pos ) if( !char_ptr ) { continue; } - pixel_pos.x += char_ptr->bitmap.advance.x; + pixel_pos.x += char_ptr->bitmap->advance.x; //printf("TextLayer_Set_Cursor_Pos(): pixel pos x: %d, total: %d, cols: %d, pos.x: %d\n", //pixel_pos.x, total, cols, pos.x); } @@ -1845,7 +1842,7 @@ _TextLayer_Text_Delete ( LCUI_TextLayer *layer, LCUI_Pos start_pos, int len ) continue; } TextLayer_Clear( layer, pixel_pos, row_ptr->max_size.h, char_ptr ); - pixel_pos.x += char_ptr->bitmap.advance.x; + pixel_pos.x += char_ptr->bitmap->advance.x; /* 将该字从源文本中移除 */ TextLayer_Text_DeleteChar( layer, char_ptr, left_or_right ); /* 该字在这行的字体位图也需要删除 */ @@ -1952,9 +1949,9 @@ TextLayer_Get_Char_PixelPos( LCUI_TextLayer *layer, LCUI_Pos char_pos ) } /* 如果设定了屏蔽字符 */ if( layer->password_char.char_code > 0 ) { - pixel_pos.x += layer->password_char.bitmap.advance.x; + pixel_pos.x += layer->password_char.bitmap->advance.x; } else { - pixel_pos.x += char_ptr->bitmap.advance.x; + pixel_pos.x += char_ptr->bitmap->advance.x; } } /* 微调位置 */