From f392a07a4eb72a2ac99166e61decd91be665dddd Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Mon, 4 Dec 2017 22:55:21 +0800 Subject: [PATCH] Update textlayer style cache processing --- include/LCUI/font/textstyle.h | 4 +++ src/font/textlayer.c | 25 ++++++++--------- src/font/textstyle.c | 51 +++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/include/LCUI/font/textstyle.h b/include/LCUI/font/textstyle.h index 3d95ef43..7fc47c68 100755 --- a/include/LCUI/font/textstyle.h +++ b/include/LCUI/font/textstyle.h @@ -63,10 +63,14 @@ typedef struct LCUI_TextStyleRec_ { /** 初始化字体样式数据 */ LCUI_API void TextStyle_Init( LCUI_TextStyle data ); +LCUI_API int TextStyle_CopyFamily( LCUI_TextStyle dst, LCUI_TextStyle src ); + LCUI_API int TextStyle_Copy( LCUI_TextStyle dst, LCUI_TextStyle src ); LCUI_API void TextStyle_Destroy( LCUI_TextStyle data ); +LCUI_API void TextStyle_Merge( LCUI_TextStyle base, LCUI_TextStyle target ); + /* 设置字体粗细程度 */ LCUI_API int TextStyle_SetWeight( LCUI_TextStyle ts, LCUI_FontWeight weight ); diff --git a/src/font/textlayer.c b/src/font/textlayer.c index fd1fc288..95b24da5 100644 --- a/src/font/textlayer.c +++ b/src/font/textlayer.c @@ -1168,23 +1168,20 @@ void TextLayer_SetUsingStyleTags( LCUI_TextLayer layer, LCUI_BOOL is_true ) layer->is_using_style_tags = is_true; } +static void TextLayer_UpdateTextStyleCache( LCUI_TextLayer layer ) +{ + LinkedListNode *node; + /* 替换缺省字体,确保能够正确应用字体设置 */ + for( LinkedList_Each( node, &layer->style_cache ) ) { + TextStyle_Merge( node->data, &layer->text_style ); + } +} + /** 重新载入各个文字的字体位图 */ void TextLayer_ReloadCharBitmap( LCUI_TextLayer layer ) { - LinkedListNode *node; - int row, col, *font_ids; - /* 替换缺省字体,确保能够正确应用字体粗细程度设置 */ - for( LinkedList_Each( node, &layer->style_cache ) ) { - LCUI_TextStyle style = node->data; - if( style->has_family || !style->has_weight ) { - continue; - } - if( LCUIFont_UpdateWeight( layer->text_style.font_ids, - style->weight, &font_ids ) > 0 ) { - style->has_family = TRUE; - style->font_ids = font_ids; - } - } + int row, col; + TextLayer_UpdateTextStyleCache( layer ); for( row = 0; row < layer->text_rows.length; ++row ) { TextRow txtrow = layer->text_rows.rows[row]; for( col = 0; col < txtrow->length; ++col ) { diff --git a/src/font/textstyle.c b/src/font/textstyle.c index 9bc70093..7abc7444 100755 --- a/src/font/textstyle.c +++ b/src/font/textstyle.c @@ -75,23 +75,33 @@ void TextStyle_Init( LCUI_TextStyle data ) data->pixel_size = 13; } -int TextStyle_Copy( LCUI_TextStyle dst, LCUI_TextStyle src ) +int TextStyle_CopyFamily( LCUI_TextStyle dst, LCUI_TextStyle src ) { - int len; - *dst = *src; - if( !dst->has_family ) { + size_t len; + if( !src->has_family ) { return 0; } - for( len = 0; dst->font_ids[len] != -1; ++len ); + for( len = 0; src->font_ids[len] != -1; ++len ); len += 1; + if( dst->font_ids ) { + free( dst->font_ids ); + } dst->font_ids = malloc( len * sizeof( int ) ); if( !dst->font_ids ) { return -ENOMEM; } + dst->has_family = TRUE; memcpy( dst->font_ids, src->font_ids, len * sizeof( int ) ); return 0; } +int TextStyle_Copy( LCUI_TextStyle dst, LCUI_TextStyle src ) +{ + *dst = *src; + dst->font_ids = NULL; + return TextStyle_CopyFamily( dst, src ); +} + void TextStyle_Destroy( LCUI_TextStyle data ) { if( data->font_ids ) { @@ -100,6 +110,37 @@ void TextStyle_Destroy( LCUI_TextStyle data ) data->font_ids = NULL; } +void TextStyle_Merge( LCUI_TextStyle base, LCUI_TextStyle target ) +{ + int *font_ids = NULL; + if( !base->has_family && target->has_family ) { + base->has_family = TRUE; + TextStyle_CopyFamily( base, target ); + } + if( target->has_style && !base->has_style && + target->style != FONT_STYLE_NORMAL ) { + base->has_style = TRUE; + base->style = target->style; + if( LCUIFont_UpdateStyle( base->font_ids, + base->style, + &font_ids ) > 0 ) { + free( base->font_ids ); + base->font_ids = font_ids; + } + } + if( target->has_weight && !base->has_weight && + target->weight != FONT_WEIGHT_NORMAL ) { + base->has_weight = TRUE; + base->weight = target->weight; + if( LCUIFont_UpdateWeight( base->font_ids, + base->weight, + &font_ids ) > 0 ) { + free( base->font_ids ); + base->font_ids = font_ids; + } + } +} + int TextStyle_SetWeight( LCUI_TextStyle ts, LCUI_FontWeight weight ) { int *font_ids;