From 00e331b0b5ffbcd4692d14ee81443357ccc4fe9d Mon Sep 17 00:00:00 2001 From: LC-SOFT Date: Thu, 21 Jan 2016 14:12:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=B8=BA=E4=B8=80=E4=BA=9B=E6=95=B0=E6=8D=AE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=9C=89=E6=95=88=E6=80=A7=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现在, 图形输出功能在初始化失败后不会终止程序运行, 在其它地方做了数据有效性判断,以应对初始化失败的情况。 --- src/font/fontlibrary.c | 28 ++++++++++++--------- src/gui/css_parser.c | 26 ++++++++++++-------- src/output/graph_display.c | 47 ++++++++++++++++++++++++------------ src/output/linuxfb_surface.c | 6 ++--- 4 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/font/fontlibrary.c b/src/font/fontlibrary.c index f3394bdb..3eb0bec2 100644 --- a/src/font/fontlibrary.c +++ b/src/font/fontlibrary.c @@ -128,8 +128,8 @@ static void DestroyTreeNode( void *arg ) RBTree_Destroy( (LCUI_RBTree*)arg ); } -/** - * 添加字体信息记录,并返回该字体的ID +/** + * 添加字体信息记录,并返回该字体的ID * @warning 该函数仅仅是将 font 参数的引用添加至记录中,为保证记录的有效性, * 传入的 font 参数必须是动态分配的。 */ @@ -143,7 +143,7 @@ int LCUIFont_Add( LCUI_Font *font, const char *filepath ) if( font->id >= fontlib.font_cache_num * FONT_CACHE_SIZE ) { LCUI_Font ***caches, **cache; fontlib.font_cache_num += 1; - caches = (LCUI_Font***)realloc( fontlib.font_cache, + caches = (LCUI_Font***)realloc( fontlib.font_cache, fontlib.font_cache_num * sizeof(LCUI_Font**) ); if( caches == NULL ) { fontlib.font_cache_num -= 1; @@ -194,7 +194,7 @@ static LCUI_Font* LCUIFont_GetById( int id ) if( id < 0 || id >= fontlib.font_cache_num * FONT_CACHE_SIZE ) { return NULL; } - + return SelectFontCache(id); } @@ -212,8 +212,8 @@ int LCUIFont_GetIdByPath( const char *filepath ) return font->id; } -/** - * 获取字体的ID +/** + * 获取字体的ID * @param[in] family_name 字族名称 * @param[in] style_name 样式名称,若设为 NULL,则默认获取 regular 样式或 * 最后一个样式的字体 @@ -356,6 +356,9 @@ int LCUIFont_GetBitmap( wchar_t ch, int font_id, int size, break; } if( font_id <= 0 ) { + if( !fontlib.default_font ) { + return -3; + } font_id = fontlib.default_font->id; } ctx = SelectFont( ctx, font_id ); @@ -398,7 +401,7 @@ int LCUIFont_LoadFile( const char *filepath ) } font->engine = fontlib.engine; id = LCUIFont_Add( font, filepath ); - printf("[font] add family: %s, style name: %s, id: %d\n", + printf("[font] add family: %s, style name: %s, id: %d\n", font->family_name, font->family_name, id); return id; } @@ -476,7 +479,7 @@ int FontBitmap_Print( LCUI_FontBitmap *fontbmp ) } static void FontBitmap_MixARGB( LCUI_Graph *graph, LCUI_Rect *write_rect, - const LCUI_FontBitmap *bmp, LCUI_Color color, + const LCUI_FontBitmap *bmp, LCUI_Color color, LCUI_Rect *read_rect ) { int x, y; @@ -513,7 +516,7 @@ static void FontBitmap_MixARGB( LCUI_Graph *graph, LCUI_Rect *write_rect, } static void FontBitmap_MixRGB( LCUI_Graph *graph, LCUI_Rect *write_rect, - const LCUI_FontBitmap *bmp, LCUI_Color color, + const LCUI_FontBitmap *bmp, LCUI_Color color, LCUI_Rect *read_rect ) { int x, y; @@ -564,7 +567,7 @@ int FontBitmap_Mix( LCUI_Graph *graph, LCUI_Pos pos, graph = Graph_GetQuote( graph ); if( graph->color_type == COLOR_TYPE_ARGB ) { FontBitmap_MixARGB( graph, &w_rect, bmp, color, &r_rect ); - } else { + } else { FontBitmap_MixRGB( graph, &w_rect, bmp, color, &r_rect ); } return 0; @@ -589,11 +592,12 @@ int FontBitmap_Load( LCUI_FontBitmap *buff, wchar_t ch, info = fontlib.incore_font; } } + if( !info ) { + return -1; + } return info->engine->render( buff, ch, pixel_size, info ); } - - #ifdef LCUI_BUILD_IN_WIN32 #define MAX_FONTFILE_NUM 4 #else diff --git a/src/gui/css_parser.c b/src/gui/css_parser.c index 6c244986..e10aa414 100644 --- a/src/gui/css_parser.c +++ b/src/gui/css_parser.c @@ -79,7 +79,7 @@ static struct { #define SPLIT_STYLE (1<<2) int ParseStyleOption( const char *str ) -{ +{ LCUI_RBTreeNode *node; node = RBTree_CustomSearch( &self.option_tree, str ); if( !node ) { @@ -93,7 +93,7 @@ const char *GetStyleName( int key ) return RBTree_GetData( &self.name_tree, key ); } -static int SplitValues( const char *str, LCUI_Style *slist, +static int SplitValues( const char *str, LCUI_Style *slist, int max_len, int mode ) { int val, vi = 0, vj = 0; @@ -378,7 +378,7 @@ static int OnParsePadding( LCUI_StyleSheet ss, int key, const char *str ) { int value_count; LCUI_Style s[4]; - + value_count = SplitValues( str, s, 4, SPLIT_NUMBER ); switch( value_count ) { case 1: @@ -413,7 +413,7 @@ static int OnParseMargin( LCUI_StyleSheet ss, int key, const char *str ) { int value_count; LCUI_Style s[4]; - + value_count = SplitValues( str, s, 4, SPLIT_NUMBER ); switch( value_count ) { case 1: @@ -492,6 +492,7 @@ static KeyNameGroup style_name_map[] = { { key_width, "width" }, { key_height, "height" }, { key_display, "display" }, + { key_z_index, "z-index" }, { key_top, "top" }, { key_right, "right" }, { key_left, "left" }, @@ -648,7 +649,7 @@ static int LCUICSS_LoadBlock( CSSParserContext ctx, const char *str ) size_t size = 0; LCUI_Selector s; LinkedListNode *node; - + ctx->ptr = str; for( ; *ctx->ptr && size < ctx->buffer_size; ++ctx->ptr, ++size ) { switch( ctx->target ) { @@ -681,7 +682,7 @@ static int LCUICSS_LoadBlock( CSSParserContext ctx, const char *str ) case '\n': case '\r': case '\t': - case ';': + case ';': ctx->pos = 0; continue; case ':': break; @@ -747,7 +748,7 @@ proc_comment: switch( *(ctx->ptr + 1) ) { case '/': ctx->is_line_comment = TRUE; break; case '*': ctx->is_line_comment = FALSE; break; - default: + default: ctx->buffer[ctx->pos++] = *ctx->ptr; continue; } @@ -765,7 +766,7 @@ put_css: LinkedList_Clear( &ctx->selectors, OnDeleteSelector ); DeleteStyleSheet( &ctx->css ); continue; -select_parser: +select_parser: ctx->target = is_value; ctx->buffer[ctx->pos] = 0; ctx->pos = 0; @@ -800,7 +801,7 @@ int LCUICSS_LoadFile( const char *filepath ) FILE *fp; char buff[512]; CSSParserContext ctx; - + fp = fopen( filepath, "r" ); if( !fp ) { return -1; @@ -886,7 +887,12 @@ void LCUICSS_Init(void) new_sp->key = sp->key; new_sp->parse = sp->parse; if( !sp->name && sp->key >= 0 ) { - new_sp->name = strdup( GetStyleName(sp->key) ); + const char *name = GetStyleName(sp->key); + if( !name ) { + free( new_sp ); + continue; + } + new_sp->name = strdup( name ); } else { new_sp->name = strdup( sp->name ); } diff --git a/src/output/graph_display.c b/src/output/graph_display.c index 0a8f24e5..48bce5a6 100755 --- a/src/output/graph_display.c +++ b/src/output/graph_display.c @@ -171,7 +171,7 @@ static void LCUIDisplay_BindSurface( LCUI_Widget widget ) Surface_SetCaptionW( p_sr->surface, widget->title ); p_rect = &widget->box.graph; - if( widget->style->sheet[key_top].is_valid && + if( widget->style->sheet[key_top].is_valid && widget->style->sheet[key_left].is_valid ) { Surface_Move( p_sr->surface, p_rect->x, p_rect->y ); } @@ -225,8 +225,7 @@ static int LCUIDisplay_Windowed( void ) break; } Widget_Show( root ); - Widget_Resize( root, display.info.getWidth(), - display.info.getHeight() ); + Widget_Resize( root, LCUIDisplay_GetWidth(), LCUIDisplay_GetHeight() ); display.mode = LDM_WINDOWED; return 0; } @@ -244,8 +243,7 @@ static int LCUIDisplay_FullScreen( void ) return 0; } display.mode = LDM_FULLSCREEN; - LCUIDisplay_SetSize( display.info.getWidth(), - display.info.getHeight() ); + LCUIDisplay_SetSize( LCUIDisplay_GetWidth(), LCUIDisplay_GetHeight() ); return 0; } @@ -327,13 +325,13 @@ void LCUIDisplay_SetSize( int width, int height ) /** 获取屏幕宽度 */ int LCUIDisplay_GetWidth( void ) { - return display.info.getWidth(); + return display.info.getWidth ? display.info.getWidth() : 0; } /** 获取屏幕高度 */ int LCUIDisplay_GetHeight( void ) { - return display.info.getHeight(); + return display.info.getHeight ? display.info.getHeight() : 0; } /** LCUI的图形显示处理线程 */ @@ -442,17 +440,22 @@ static void Surface_OnInvalidRect( LCUI_Surface surface, LCUI_Rect *rect ) void Surface_Delete( LCUI_Surface surface ) { - display.methods->delete( surface ); + if( display.methods && display.methods->delete ) { + display.methods->delete( surface ); + } } LCUI_Surface Surface_New( void ) { - return display.methods->new(); + if( display.methods ) { + return display.methods->new(); + } + return NULL; } LCUI_BOOL Surface_IsReady( LCUI_Surface surface ) { - if( display.methods->isReady ) { + if( display.methods && display.methods->isReady ) { return display.methods->isReady( surface ); } return TRUE; @@ -460,7 +463,7 @@ LCUI_BOOL Surface_IsReady( LCUI_Surface surface ) void Surface_Move( LCUI_Surface surface, int x, int y ) { - if( display.methods->move ) { + if( display.methods && display.methods->move ) { display.methods->move( surface, x, y ); } } @@ -474,21 +477,21 @@ void Surface_Resize( LCUI_Surface surface, int w, int h ) void Surface_SetCaptionW( LCUI_Surface surface, const wchar_t *str ) { - if( display.methods->setCaptionW ) { + if( display.methods && display.methods->setCaptionW ) { display.methods->setCaptionW( surface, str ); } } void Surface_Show( LCUI_Surface surface ) { - if( display.methods->show ) { + if( display.methods && display.methods->show ) { display.methods->show( surface ); } } void Surface_Hide( LCUI_Surface surface ) { - if( display.methods->hide ) { + if( display.methods && display.methods->hide ) { display.methods->hide( surface ); } } @@ -496,7 +499,7 @@ void Surface_Hide( LCUI_Surface surface ) /** 设置 Surface 的渲染模式 */ void Surface_SetRenderMode( LCUI_Surface surface, int mode ) { - if( display.methods->setRenderMode ) { + if( display.methods && display.methods->setRenderMode ) { display.methods->setRenderMode( surface, mode ); } } @@ -504,7 +507,7 @@ void Surface_SetRenderMode( LCUI_Surface surface, int mode ) /** 更新 surface,应用缓存的变更 */ void Surface_Update( LCUI_Surface surface ) { - if( display.methods->update ) { + if( display.methods && display.methods->update ) { display.methods->update( surface ); } } @@ -517,6 +520,9 @@ void Surface_Update( LCUI_Surface surface ) */ LCUI_PaintContext Surface_BeginPaint( LCUI_Surface surface, LCUI_Rect *rect ) { + if( !display.methods ) { + return NULL; + } return display.methods->beginPaint( surface, rect ); } @@ -527,12 +533,18 @@ LCUI_PaintContext Surface_BeginPaint( LCUI_Surface surface, LCUI_Rect *rect ) */ void Surface_EndPaint( LCUI_Surface surface, LCUI_PaintContext paint_ctx ) { + if( !display.methods ) { + return; + } display.methods->endPaint( surface, paint_ctx ); } /** 将帧缓存中的数据呈现至Surface的窗口内 */ void Surface_Present( LCUI_Surface surface ) { + if( !display.methods ) { + return; + } display.methods->present( surface ); } @@ -553,6 +565,9 @@ int LCUI_InitDisplay( void ) #elif defined(LCUI_VIDEO_DRIVER_FRAMEBUFFER) display.methods = LCUIDisplay_InitLinuxFB( &display.info ); #endif + if( !display.methods ) { + return -2; + } display.methods->onInvalidRect = Surface_OnInvalidRect; display.methods->onEvent = Surface_OnEvent; display.fc_ctx = FrameControl_Create(); diff --git a/src/output/linuxfb_surface.c b/src/output/linuxfb_surface.c index 5f44fd6d..1e89b6b2 100644 --- a/src/output/linuxfb_surface.c +++ b/src/output/linuxfb_surface.c @@ -200,7 +200,7 @@ LCUI_PaintContext LinuxFB_BeginPaint( LCUI_Surface surface, LCUI_Rect *rect ) Graph_Init( &paint->canvas ); Graph_Create( &paint->canvas, rect->w, rect->h ); paint->rect = *rect; - _DEBUG_MSG("canvas: %p, rect: %d,%d,%d,%d\n", + _DEBUG_MSG("canvas: %p, rect: %d,%d,%d,%d\n", &paint->canvas, rect->x, rect->y, rect->w, rect->h); return paint; } @@ -350,7 +350,7 @@ void LinuxFB_EndPaint( LCUI_Surface surface, LCUI_PaintContext paint_ctx ) LCUI_Rect cut_rect; LCUI_Graph canvas; - LCUIRect_GetCutArea( linuxfb.width, linuxfb.height, + LCUIRect_GetCutArea( linuxfb.width, linuxfb.height, paint_ctx->rect, &cut_rect ); if( cut_rect.w <= 0 || cut_rect.h <= 0 ) { free( paint_ctx ); @@ -438,7 +438,7 @@ LCUI_SurfaceMethods *LCUIDisplay_InitLinuxFB( LCUI_DisplayInfo *info ) if( (void *)-1 == linuxfb.mem ) { printf("fail\n"); perror(strerror(errno)); - exit(-1); + return NULL; } else { printf("success\n"); }