From d13675e8480a403ea2fe40283ec49cfba13ad938 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Tue, 16 Oct 2012 21:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=98=E5=88=B6=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E4=BD=8D=E5=9B=BE=E6=97=B6=E7=9A=84=E5=9D=90=E6=A0=87?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/font/bitmapfont.c | 153 ++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 74 deletions(-) diff --git a/src/font/bitmapfont.c b/src/font/bitmapfont.c index 6f7933b5..9d56bd89 100644 --- a/src/font/bitmapfont.c +++ b/src/font/bitmapfont.c @@ -21,22 +21,22 @@ * ****************************************************************************/ /* **************************************************************************** - * bitmapfont.c -- 位图字体的操作集 + * bitmapfont.c -- 浣嶅浘瀛椾綋鐨勬搷浣滈泦 * - * 版权所有 (C) 2012 归属于 - * 刘超 + * 鐗堟潈鎵�鏈� (C) 2012 褰掑睘浜� + * 鍒樿秴 * - * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 + * 杩欎釜鏂囦欢鏄疞CUI椤圭洰鐨勪竴閮ㄥ垎锛屽苟涓斿彧鍙互鏍规嵁GPLv2璁稿彲鍗忚鏉ヤ娇鐢ㄣ�佹洿鏀瑰拰鍙戝竷銆� * - * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写) + * (GPLv2 鏄� GNU閫氱敤鍏叡璁稿彲璇佺浜岀増 鐨勮嫳鏂囩缉鍐�) * - * 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。 + * 缁х画浣跨敤銆佷慨鏀规垨鍙戝竷鏈枃浠讹紝琛ㄦ槑鎮ㄥ凡缁忛槄璇诲苟瀹屽叏鐞嗚В鍜屾帴鍙楄繖涓鍙崗璁�� * - * LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特 - * 定用途的隐含担保,详情请参照GPLv2许可协议。 + * LCUI 椤圭洰鏄熀浜庝娇鐢ㄧ洰鐨勮�屽姞浠ユ暎甯冪殑锛屼絾涓嶈礋浠讳綍鎷呬繚璐d换锛岀敋鑷虫病鏈夐�傞攢鎬ф垨鐗� + * 瀹氱敤閫旂殑闅愬惈鎷呬繚锛岃鎯呰鍙傜収GPLv2璁稿彲鍗忚銆� * - * 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果 - * 没有,请查看:. + * 鎮ㄥ簲宸叉敹鍒伴檮闅忎簬鏈枃浠剁殑GPLv2璁稿彲鍗忚鐨勫壇鏈紝瀹冮�氬父鍦↙ICENSE.TXT鏂囦欢涓紝濡傛灉 + * 娌℃湁锛岃鏌ョ湅锛�. * ****************************************************************************/ //#define DEBUG @@ -49,9 +49,6 @@ #include LC_ERROR_H #include LC_GRAPH_H -#include -#include -#include #include #ifdef USE_FREETYPE @@ -65,8 +62,8 @@ extern uchar_t const *in_core_font_8x8(); int FontBMP_Valid(LCUI_FontBMP *bitmap) /* - * 功能:检测位图数据是否有效 - * 返回值:有效返回1,无效返回0 + * 鍔熻兘锛氭娴嬩綅鍥炬暟鎹槸鍚︽湁鏁� + * 杩斿洖鍊硷細鏈夋晥杩斿洖1锛屾棤鏁堣繑鍥�0 */ { if(NULL != bitmap && bitmap->width > 0 && bitmap->rows > 0) { @@ -76,7 +73,7 @@ int FontBMP_Valid(LCUI_FontBMP *bitmap) } void Print_FontBMP_Info(LCUI_FontBMP *bitmap) -/* 功能:打印位图的信息 */ +/* 鍔熻兘锛氭墦鍗颁綅鍥剧殑淇℃伅 */ { printf("address:%p\n",bitmap); if(bitmap == NULL) { @@ -86,7 +83,7 @@ void Print_FontBMP_Info(LCUI_FontBMP *bitmap) } void FontBMP_Init(LCUI_FontBMP *bitmap) -/* 初始化字体位图 */ +/* 鍒濆鍖栧瓧浣撲綅鍥� */ { bitmap->rows = 0; bitmap->width = 0; @@ -98,7 +95,7 @@ void FontBMP_Init(LCUI_FontBMP *bitmap) } void FontBMP_Free(LCUI_FontBMP *bitmap) -/* 释放字体位图占用的资源 */ +/* 閲婃斁瀛椾綋浣嶅浘鍗犵敤鐨勮祫婧� */ { if(FontBMP_Valid(bitmap)) { free(bitmap->buffer); @@ -107,7 +104,7 @@ void FontBMP_Free(LCUI_FontBMP *bitmap) } int FontBMP_Create(LCUI_FontBMP *bitmap, int width, int rows) -/* 功能:创建字体位图 */ +/* 鍔熻兘锛氬垱寤哄瓧浣撲綅鍥� */ { size_t size; if(width < 0 || rows < 0) { @@ -128,13 +125,13 @@ int FontBMP_Create(LCUI_FontBMP *bitmap, int width, int rows) } void Get_Default_FontBMP(unsigned short code, LCUI_FontBMP *out_bitmap) -/* 功能:根据字符编码,获取系统自带的字体位图 */ +/* 鍔熻兘锛氭牴鎹瓧绗︾紪鐮侊紝鑾峰彇绯荤粺鑷甫鐨勫瓧浣撲綅鍥� */ { int i,j, start, m; uchar_t const *p; p = in_core_font_8x8(); - FontBMP_Create(out_bitmap, 8, 8);/* 为位图分配内存,8x8的尺寸 */ + FontBMP_Create(out_bitmap, 8, 8);/* 涓轰綅鍥惧垎閰嶅唴瀛橈紝8x8鐨勫昂瀵� */ out_bitmap->left = 0; out_bitmap->top = 0; if(code < 256) { @@ -144,7 +141,7 @@ void Get_Default_FontBMP(unsigned short code, LCUI_FontBMP *out_bitmap) start = code * 8; for (i=start;ibuffer[m] = (p[i]&(1<type = DEFAULT; font->size = 12; font->fore_color.red = 0; @@ -217,22 +214,22 @@ void LCUI_Font_Init(LCUI_Font *font) #endif font->ft_lib = NULL; font->ft_face = NULL; - /* 如果在环境变量中定义了字体文件路径,那就使用它 */ + /* 濡傛灉鍦ㄧ幆澧冨彉閲忎腑瀹氫箟浜嗗瓧浣撴枃浠惰矾寰勶紝閭e氨浣跨敤瀹� */ p = getenv("LCUI_FONTFILE"); if(p != NULL) { strcpy(default_font, p); } - /* 打开默认字体文件 */ + /* 鎵撳紑榛樿瀛椾綋鏂囦欢 */ Open_Fontfile(&LCUI_Sys.default_font, default_font); } void Font_Init(LCUI_Font *in) /* - * 功能:初始化Font结构体数据 - * 说明:默认是继承系统的字体数据 + * 鍔熻兘锛氬垵濮嬪寲Font缁撴瀯浣撴暟鎹� + * 璇存槑锛氶粯璁ゆ槸缁ф壙绯荤粺鐨勫瓧浣撴暟鎹� * */ { - in->type = DEFAULT; /* 字体类型为LCUI默认的 */ + in->type = DEFAULT; /* 瀛椾綋绫诲瀷涓篖CUI榛樿鐨� */ in->size = 12; in->fore_color.red = 0; in->fore_color.green = 0; @@ -259,7 +256,7 @@ void Font_Init(LCUI_Font *in) } void Font_Free(LCUI_Font *in) -/* 功能:释放Font结构体数据占用的内存资源 */ +/* 鍔熻兘锛氶噴鏀綟ont缁撴瀯浣撴暟鎹崰鐢ㄧ殑鍐呭瓨璧勬簮 */ { String_Free(&in->font_file); String_Free(&in->family_name); @@ -278,7 +275,7 @@ void Font_Free(LCUI_Font *in) } void LCUI_Font_Free() -/* 功能:释放LCUI默认的Font结构体数据占用的内存资源 */ +/* 鍔熻兘锛氶噴鏀綥CUI榛樿鐨凢ont缁撴瀯浣撴暟鎹崰鐢ㄧ殑鍐呭瓨璧勬簮 */ { String_Free(&LCUI_Sys.default_font.font_file); String_Free(&LCUI_Sys.default_font.family_name); @@ -296,16 +293,18 @@ void LCUI_Font_Free() int Show_FontBMP(LCUI_FontBMP *fontbmp) -/* 功能:在屏幕打印以0和1表示字体位图 */ +/* 鍔熻兘锛氬湪灞忓箷鎵撳嵃浠�0鍜�1琛ㄧず瀛椾綋浣嶅浘 */ { int x,y,m; for(y = 0;y < fontbmp->rows; ++y){ m = y*fontbmp->width; - for(x = 0; x < fontbmp->width; ++x,++m){ - if(fontbmp->buffer[m] > 0) { - printf("1"); + for(x = 0; x < fontbmp->width; ++x,++m){ + if(fontbmp->buffer[m] > 128) { + printf("#"); + } else if(fontbmp->buffer[m] > 64) { + printf("-"); } else { - printf("0"); + printf(" "); } } printf("\n"); @@ -317,11 +316,12 @@ int Show_FontBMP(LCUI_FontBMP *fontbmp) int FontBMP_Mix( LCUI_Graph *graph, LCUI_Pos des_pos, LCUI_FontBMP *bitmap, LCUI_RGB color, int flag ) -/* 功能:将字体位图数据与背景图形混合 */ +/* 鍔熻兘锛氬皢瀛椾綋浣嶅浘鏁版嵁涓庤儗鏅浘褰㈡贩鍚� */ { LCUI_Graph *des; LCUI_Rect des_rect, cut; uint_t total, m, n, y; + uint_t src_start_pos, des_start_pos; des_rect = Get_Graph_Valid_Rect( graph ); des = Get_Quote_Graph( graph ); @@ -340,41 +340,46 @@ int FontBMP_Mix( LCUI_Graph *graph, LCUI_Pos des_pos, des_pos.x += cut.x; des_pos.y += cut.y; } - Graph_Lock( des, 1 ); - /* 开始读取图片中的图形数组并写入窗口 */ + /* 寮�濮嬭鍙栧浘鐗囦腑鐨勫浘褰㈡暟缁勫苟鍐欏叆绐楀彛 */ if( flag == GRAPH_MIX_FLAG_OVERLAY ) { + src_start_pos = cut.y * bitmap->width + cut.x; + des_start_pos = (des_pos.y + des_rect.y) * des->width + des_pos.x + des_rect.x; for (y = 0; y < cut.height; ++y) { - m = (cut.y + y) * bitmap->width + cut.x; - n = (des_pos.y + y + des_rect.y) * des->width + des_pos.x + des_rect.x; + m = src_start_pos; + n = des_start_pos; total = n + cut.width; for (; n < total; ++n,++m) { des->rgba[0][n] = ALPHA_BLENDING(color.red, des->rgba[0][n], bitmap->buffer[m]); des->rgba[1][n] = ALPHA_BLENDING(color.green, des->rgba[1][n], bitmap->buffer[m]); des->rgba[2][n] = ALPHA_BLENDING(color.blue, des->rgba[2][n], bitmap->buffer[m]); } + des_start_pos += des->width; + src_start_pos += bitmap->width; } } else { + src_start_pos = cut.y * bitmap->width + cut.x; + des_start_pos = (des_pos.y + des_rect.y) * des->width + des_pos.x + des_rect.x; for (y = 0; y < cut.height; ++y) { - m = (cut.y + y) * bitmap->width + cut.x; - n = (des_pos.y + y + des_rect.y) * des->width + des_pos.x + des_rect.x; + m = src_start_pos; + n = des_start_pos; total = n + cut.width; - for (; n < total; ++n,++m) { - //if(bitmap->buffer[m] != 0) { - des->rgba[0][n] = color.red; - des->rgba[1][n] = color.green; - des->rgba[2][n] = color.blue; - des->rgba[3][n] = bitmap->buffer[m]; - //} - } - } + for (; n < total; ++n,++m) { + des->rgba[0][n] = color.red; + des->rgba[1][n] = color.green; + des->rgba[2][n] = color.blue; + des->rgba[3][n] = bitmap->buffer[m]; + } + des_start_pos += des->width; + src_start_pos += bitmap->width; + } } Graph_Unlock( des ); return 0; } int Open_Fontfile(LCUI_Font *font_data, char *fontfile) -/* 功能:打开字体文件,并保存数据至LCUI_Font结构体中 */ +/* 鍔熻兘锛氭墦寮�瀛椾綋鏂囦欢锛屽苟淇濆瓨鏁版嵁鑷矻CUI_Font缁撴瀯浣撲腑 */ { #ifdef USE_FREETYPE int type; @@ -396,8 +401,8 @@ int Open_Fontfile(LCUI_Font *font_data, char *fontfile) else if(fontfile == NULL) { return -1; } - lib_error = FT_Init_FreeType( & library); /* 初始化FreeType库 */ - if (lib_error) { /* 当初始化库时发生了一个错误 */ + lib_error = FT_Init_FreeType( & library); /* 鍒濆鍖朏reeType搴� */ + if (lib_error) { /* 褰撳垵濮嬪寲搴撴椂鍙戠敓浜嗕竴涓敊璇� */ printf("open fontfile: "FT_INIT_ERROR); return - 1 ; } @@ -406,9 +411,9 @@ int Open_Fontfile(LCUI_Font *font_data, char *fontfile) if(face_error) { FT_Done_FreeType(library); if ( face_error == FT_Err_Unknown_File_Format ) - printf("open fontfile: "FT_UNKNOWN_FILE_FORMAT); /* 未知文件格式 */ + printf("open fontfile: "FT_UNKNOWN_FILE_FORMAT); /* 鏈煡鏂囦欢鏍煎紡 */ else - printf("open fontfile: "FT_OPEN_FILE_ERROR);/* 打开错误 */ + printf("open fontfile: "FT_OPEN_FILE_ERROR);/* 鎵撳紑閿欒 */ perror(fontfile); return -1; } @@ -421,7 +426,7 @@ int Open_Fontfile(LCUI_Font *font_data, char *fontfile) face->style_name ); Font_Free(font_data); - /* 保存字体信息 */ + /* 淇濆瓨瀛椾綋淇℃伅 */ Strcpy(&font_data->family_name, face->family_name); Strcpy(&font_data->style_name, face->style_name); Strcpy(&font_data->font_file, fontfile); @@ -462,24 +467,24 @@ static int Convert_FT_BitmapGlyph(LCUI_FontBMP *des, const FT_BitmapGlyph src) int Get_FontBMP(LCUI_Font *font_data, wchar_t ch, LCUI_FontBMP *out_bitmap) /* - * 功能:获取单个wchar_t型字符的字体位图数据 - * 说明:LCUI_Font结构体中储存着已被打开的字体文件句柄和face对象的句柄,如果字体文件已经被 - * 成功打开一次,此函数不会再次打开字体文件。 + * 鍔熻兘锛氳幏鍙栧崟涓獁char_t鍨嬪瓧绗︾殑瀛椾綋浣嶅浘鏁版嵁 + * 璇存槑锛歀CUI_Font缁撴瀯浣撲腑鍌ㄥ瓨鐫�宸茶鎵撳紑鐨勫瓧浣撴枃浠跺彞鏌勫拰face瀵硅薄鐨勫彞鏌勶紝濡傛灉瀛椾綋鏂囦欢宸茬粡琚� + * 鎴愬姛鎵撳紑涓�娆★紝姝ゅ嚱鏁颁笉浼氬啀娆℃墦寮�瀛椾綋鏂囦欢銆� */ { #ifdef USE_FREETYPE size_t size; BOOL have_space = IS_FALSE; - FT_Face p_FT_Face = NULL; /* face对象的句柄 */ + FT_Face p_FT_Face = NULL; /* face瀵硅薄鐨勫彞鏌� */ FT_BitmapGlyph bitmap_glyph; FT_Glyph glyph; FT_Error error; if(font_data != NULL) { - /* 如果LCUI_Font结构体中的字体信息有效,就打开结构体中的指定的字体文件,并 - * 将字体文件和face对象的句柄保存至结构体中。 - * 当然,如果LCUI_Font结构体有有效的字体文件和face对象的句柄,就直接返回0。 + /* 濡傛灉LCUI_Font缁撴瀯浣撲腑鐨勫瓧浣撲俊鎭湁鏁堬紝灏辨墦寮�缁撴瀯浣撲腑鐨勬寚瀹氱殑瀛椾綋鏂囦欢锛屽苟 + * 灏嗗瓧浣撴枃浠跺拰face瀵硅薄鐨勫彞鏌勪繚瀛樿嚦缁撴瀯浣撲腑銆� + * 褰撶劧锛屽鏋淟CUI_Font缁撴瀯浣撴湁鏈夋晥鐨勫瓧浣撴枃浠跺拰face瀵硅薄鐨勫彞鏌勶紝灏辩洿鎺ヨ繑鍥�0銆� */ if(font_data->ft_face == NULL || font_data->ft_lib == NULL ) { @@ -496,21 +501,21 @@ int Get_FontBMP(LCUI_Font *font_data, wchar_t ch, LCUI_FontBMP *out_bitmap) return -1; } - FT_Select_Charmap( p_FT_Face, FT_ENCODING_UNICODE ); /* 设定为UNICODE,默认的也是 */ - FT_Set_Pixel_Sizes( p_FT_Face, 0, font_data->size ); /* 设定字体尺寸 */ + FT_Select_Charmap( p_FT_Face, FT_ENCODING_UNICODE ); /* 璁惧畾涓篣NICODE锛岄粯璁ょ殑涔熸槸 */ + FT_Set_Pixel_Sizes( p_FT_Face, 0, font_data->size ); /* 璁惧畾瀛椾綋灏哄 */ if( ch == ' ' ) { ch = 'a'; have_space = IS_TRUE; } - /* 这个函数只是简单地调用FT_Get_Char_Index和FT_Load_Glyph */ + /* 杩欎釜鍑芥暟鍙槸绠�鍗曞湴璋冪敤FT_Get_Char_Index鍜孎T_Load_Glyph */ error = FT_Load_Char( p_FT_Face, ch, font_data->load_flags); if(error) { return error; } - /* 从插槽中提取一个字形图像 - * 请注意,创建的FT_Glyph对象必须与FT_Done_Glyph成对使用 */ + /* 浠庢彃妲戒腑鎻愬彇涓�涓瓧褰㈠浘鍍� + * 璇锋敞鎰忥紝鍒涘缓鐨凢T_Glyph瀵硅薄蹇呴』涓嶧T_Done_Glyph鎴愬浣跨敤 */ error = FT_Get_Glyph( p_FT_Face->glyph, &glyph ); if(error) { return error;