diff --git a/components/rtgui/common/font_hz_bmp.c b/components/rtgui/common/font_hz_bmp.c index c35f70bfc95f0b8280663830198e52ca53996c0b..d4b9637e756171d31e74ff674b54c4f2a049fca8 100644 --- a/components/rtgui/common/font_hz_bmp.c +++ b/components/rtgui/common/font_hz_bmp.c @@ -71,43 +71,43 @@ static void _rtgui_hz_bitmap_font_draw_text(struct rtgui_font_bitmap* bmp_font, } } -static void rtgui_hz_bitmap_font_draw_text (struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t length, struct rtgui_rect* rect) -{ - rt_uint32_t len; - struct rtgui_font *efont; +static void rtgui_hz_bitmap_font_draw_text (struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t length, struct rtgui_rect* rect) +{ + rt_uint32_t len; + struct rtgui_font *efont; struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data); - - RT_ASSERT(dc != RT_NULL); - - /* get English font */ - efont = rtgui_font_refer("asc", bmp_font->height); - if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */ - - while (length > 0) - { - len = 0; - while (((rt_uint8_t)*(text + len)) < 0x80 && *(text + len)) len ++; - /* draw text with English font */ - if (len > 0) - { - rtgui_font_draw(efont, dc, text, len, rect); - - text += len; - length -= len; - } - - len = 0; - while (((rt_uint8_t)*(text + len)) >= 0x80) len ++; - if (len > 0) - { - _rtgui_hz_bitmap_font_draw_text(bmp_font, dc, text, len, rect); - - text += len; - length -= len; - } - } - - rtgui_font_derefer(efont); + + RT_ASSERT(dc != RT_NULL); + + /* get English font */ + efont = rtgui_font_refer("asc", bmp_font->height); + if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */ + + while (length > 0) + { + len = 0; + while (((rt_uint8_t)*(text + len)) < 0x80 && *(text + len)) len ++; + /* draw text with English font */ + if (len > 0) + { + rtgui_font_draw(efont, dc, text, len, rect); + + text += len; + length -= len; + } + + len = 0; + while (((rt_uint8_t)*(text + len)) >= 0x80) len ++; + if (len > 0) + { + _rtgui_hz_bitmap_font_draw_text(bmp_font, dc, text, len, rect); + + text += len; + length -= len; + } + } + + rtgui_font_derefer(efont); } static void rtgui_hz_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect) diff --git a/components/rtgui/common/font_hz_file.c b/components/rtgui/common/font_hz_file.c index 9cfa92fc5042ec0ab2c2f8b9d906965526950fbe..ff32b542e94acd4473fe9b560f643f6fc82ea3df 100644 --- a/components/rtgui/common/font_hz_file.c +++ b/components/rtgui/common/font_hz_file.c @@ -161,44 +161,44 @@ static void _rtgui_hz_file_font_draw_text(struct rtgui_hz_file_font* hz_file_fon } } -static void rtgui_hz_file_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t length, struct rtgui_rect* rect) -{ - rt_uint32_t len; - struct rtgui_font *efont; +static void rtgui_hz_file_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t length, struct rtgui_rect* rect) +{ + rt_uint32_t len; + struct rtgui_font *efont; struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data; - - RT_ASSERT(dc != RT_NULL); + + RT_ASSERT(dc != RT_NULL); RT_ASSERT(hz_file_font != RT_NULL); - - /* get English font */ - efont = rtgui_font_refer("asc", hz_file_font->font_size); - if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */ - - while (length > 0) - { - len = 0; - while (((rt_uint8_t)*(text + len)) < 0x80 && *(text + len)) len ++; - /* draw text with English font */ - if (len > 0) - { - rtgui_font_draw(efont, dc, text, len, rect); - - text += len; - length -= len; - } - - len = 0; - while (((rt_uint8_t)*(text + len)) >= 0x80) len ++; - if (len > 0) - { - _rtgui_hz_file_font_draw_text(hz_file_font, dc, text, len, rect); - - text += len; - length -= len; - } - } - - rtgui_font_derefer(efont); + + /* get English font */ + efont = rtgui_font_refer("asc", hz_file_font->font_size); + if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */ + + while (length > 0) + { + len = 0; + while (((rt_uint8_t)*(text + len)) < 0x80 && *(text + len)) len ++; + /* draw text with English font */ + if (len > 0) + { + rtgui_font_draw(efont, dc, text, len, rect); + + text += len; + length -= len; + } + + len = 0; + while (((rt_uint8_t)*(text + len)) >= 0x80) len ++; + if (len > 0) + { + _rtgui_hz_file_font_draw_text(hz_file_font, dc, text, len, rect); + + text += len; + length -= len; + } + } + + rtgui_font_derefer(efont); } static void rtgui_hz_file_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect) diff --git a/components/rtgui/common/image.c b/components/rtgui/common/image.c index 23f869d9874f00fba06c3b3bc3764d349b97133e..bec10a058f78916bc0b60044c7db37b1b6def159 100644 --- a/components/rtgui/common/image.c +++ b/components/rtgui/common/image.c @@ -14,12 +14,16 @@ #include #include -#include #include #include +#include #include +#ifdef RTGUI_IMAGE_XPM +#include +#endif + #ifdef RTGUI_IMAGE_BMP #include #endif @@ -32,12 +36,15 @@ static rtgui_list_t _rtgui_system_image_list = {RT_NULL}; -/* init rtgui image system */ +/* initialize rtgui image system */ void rtgui_system_image_init(void) { - /* always support XPM image */ - rtgui_image_xpm_init(); + /* always support HDC image */ rtgui_image_hdc_init(); + +#ifdef RTGUI_IMAGE_XPM + rtgui_image_xpm_init(); +#endif #ifdef RTGUI_IMAGE_BMP rtgui_image_bmp_init(); @@ -50,6 +57,11 @@ void rtgui_system_image_init(void) #ifdef RTGUI_IMAGE_PNG rtgui_image_png_init(); #endif + +#ifdef RTGUI_IMAGE_CONTAINER + /* initialize image container */ + rtgui_system_image_container_init(RT_FALSE); +#endif } static struct rtgui_image_engine* rtgui_image_get_engine_by_filename(const char* fn) diff --git a/components/rtgui/common/image_container.c b/components/rtgui/common/image_container.c index fd33ff6e1d88a3d42df2c544f7fe03b70501ebd9..9146f4f829cb8bd2abe480d524d3ba5c3000cf6b 100644 --- a/components/rtgui/common/image_container.c +++ b/components/rtgui/common/image_container.c @@ -1,5 +1,6 @@ #include +#ifdef RTGUI_IMAGE_CONTAINER typedef unsigned int (*rtgui_hash_func_t) (const void* key); typedef struct _rtgui_hash_table rtgui_hash_table_t; typedef rt_bool_t (*rtgui_equal_func_t) (const void* a, const void* b); @@ -352,92 +353,93 @@ rt_bool_t string_equal_func(const void* a, const void* b) return RT_FALSE; } -/* hash node for imnage */ -struct image_hash_node -{ - rt_image_t *image; - char *filename; -}; - static rtgui_hash_table_t* image_hash_table; -rt_bool_t load_image = RT_FALSE; -void image_container_system_init(rt_bool_t is_load) +static rt_bool_t load_image = RT_FALSE; +void rtgui_system_image_container_init(rt_bool_t load) { /* create image hash table */ image_hash_table = hash_table_create(string_hash_func, string_equal_func); RT_ASSERT(image_hash_table != RT_NULL); - load_image = is_load; + + /* set load type */ + load_image = load; } -rtgui_image_t* rtgui_image_container_get(const char* filename) +rtgui_image_item_t* rtgui_image_container_get(const char* filename) { - struct image_hash_node* node; + struct rtgui_image_item* item; - node = hash_table_find(image_hash_table, filename); - if (node == RT_NULL) + item = hash_table_find(image_hash_table, filename); + if (item == RT_NULL) { - rtgui_image_t *image; - - node = (struct image_hash_node*) rt_malloc (sizeof(struct image_hash_node)); - if (node == RT_NULL) return RT_NULL; + item = (struct rtgui_image_item*) rt_malloc (sizeof(struct rtgui_image_item)); + if (item == RT_NULL) return RT_NULL; /* create a image object */ - image = rtgui_image_create(filename, load_image); - if (image == RT_NULL) + item->image = rtgui_image_create(filename, load_image); + if (item->image == RT_NULL) { - rt_free(node); + rt_free(item); return RT_NULL; /* create image failed */ } - node->image = image; - node->filename = rt_strdup(filename); - hash_table_add(image_hash_table, node->filename, node); + item->refcount = 1; + item->filename = rt_strdup(filename); + hash_table_insert(image_hash_table, item->filename, item); } else { - node->image->ref ++; /* increase refcount */ + item->refcount ++; /* increase refcount */ } - return node->image; + return item; } -rtgui_image_t* rtgui_image_container_get_memref(const rt_uint8_t* memory, const char* type) +rtgui_image_item_t* rtgui_image_container_get_memref(const char* type, const rt_uint8_t* memory, rt_uint32_t length) { - struct image_hash_node* node; char filename[32]; + struct rtgui_image_item* item; /* create filename for image identification */ rt_snprintf(filename, sizeof(filename), "0x%08x_%s", memory, type); /* search in container */ - node = hash_table_find(image_hash_table, filename); - if (node == RT_NULL) + item = hash_table_find(image_hash_table, filename); + if (item == RT_NULL) { - node = (struct image_hash_node*) rt_malloc (sizeof(struct image_hash_node)); - if (node == RT_NULL) return RT_NULL; + item = (struct rtgui_image_item*) rt_malloc (sizeof(struct rtgui_image_item)); + if (item == RT_NULL) return RT_NULL; /* create image object */ - image = rtgui_image_create_from_mem(memory, type, load_image); - if (image == RT_NULL) + item->image = rtgui_image_create_from_mem(type, memory, length, load_image); + if (item->image == RT_NULL) { - rt_free(node); + rt_free(item); return RT_NULL; /* create image failed */ } - node->image = image; - node->filename = rt_strdup(image_id); - hash_table_add(image_hash_table, node->filename, node); + item->refcount = 1; + item->filename = rt_strdup(filename); + hash_table_insert(image_hash_table, item->filename, item); } - else node->image->ref ++; + else item->refcount ++; - return node->image; + return item; } -void rtgui_image_container_put(rtgui_image_t* image) +void rtgui_image_container_put(rtgui_image_item_t* item) { - image->ref --; - if (image->ref == 0) + item->refcount --; + if (item->refcount == 0) { + /* remove item from container */ + hash_table_remove(image_hash_table, item->filename); + + /* destroy image and image item */ + rt_free(item->filename); + rtgui_image_destroy(item->image); + rt_free(item); } } +#endif diff --git a/components/rtgui/include/rtgui/image.h b/components/rtgui/include/rtgui/image.h index 1c71ef2f32f8c5c0e3bab02921cb9b3b18d77bc8..99e3348de74ee266089fe29a5e2e60833d697c12 100644 --- a/components/rtgui/include/rtgui/image.h +++ b/components/rtgui/include/rtgui/image.h @@ -1,77 +1,76 @@ -/* - * File : image.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-10-16 Bernard first version - */ -#ifndef __RTGUI_IMAGE_H__ -#define __RTGUI_IMAGE_H__ - -#include -#include -#include - -struct rtgui_image; -struct rtgui_image_engine -{ - const char* name; - struct rtgui_list_node list; - - /* image engine function */ - rt_bool_t (*image_check)(struct rtgui_filerw* file); - - rt_bool_t (*image_load)(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load); - void (*image_unload)(struct rtgui_image* image); - - void (*image_blit)(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); -}; - -struct rtgui_image_palette -{ - rtgui_color_t* colors; - rt_uint32_t ncolors; -}; -typedef struct rtgui_image_palette rtgui_image_palette_t; - -struct rtgui_image -{ - /* image metrics */ - rt_uint16_t w, h; - - /* image engine */ - const struct rtgui_image_engine* engine; - - /* image palette */ - rtgui_image_palette_t* palette; - - /* image private data */ - void* data; -}; -typedef struct rtgui_image rtgui_image_t; - -/* init rtgui image system */ -void rtgui_system_image_init(void); - -#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW) -struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load); -struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load); -#endif -struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load); -void rtgui_image_destroy(struct rtgui_image* image); - -/* register an image engine */ -void rtgui_image_register_engine(struct rtgui_image_engine* engine); - -/* blit an image on DC */ -void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); -struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors); - - -#endif +/* + * File : image.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_IMAGE_H__ +#define __RTGUI_IMAGE_H__ + +#include +#include +#include + +struct rtgui_image; +struct rtgui_image_engine +{ + const char* name; + struct rtgui_list_node list; + + /* image engine function */ + rt_bool_t (*image_check)(struct rtgui_filerw* file); + + rt_bool_t (*image_load)(struct rtgui_image* image, struct rtgui_filerw* file, rt_bool_t load); + void (*image_unload)(struct rtgui_image* image); + + void (*image_blit)(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); +}; + +struct rtgui_image_palette +{ + rtgui_color_t* colors; + rt_uint32_t ncolors; +}; +typedef struct rtgui_image_palette rtgui_image_palette_t; + +struct rtgui_image +{ + /* image metrics */ + rt_uint16_t w, h; + + /* image engine */ + const struct rtgui_image_engine* engine; + + /* image palette */ + rtgui_image_palette_t* palette; + + /* image private data */ + void* data; +}; +typedef struct rtgui_image rtgui_image_t; + +/* init rtgui image system */ +void rtgui_system_image_init(void); + +#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW) +struct rtgui_image* rtgui_image_create_from_file(const char* type, const char* filename, rt_bool_t load); +struct rtgui_image* rtgui_image_create(const char* filename, rt_bool_t load); +#endif +struct rtgui_image* rtgui_image_create_from_mem(const char* type, const rt_uint8_t* data, rt_size_t length, rt_bool_t load); +void rtgui_image_destroy(struct rtgui_image* image); + +/* register an image engine */ +void rtgui_image_register_engine(struct rtgui_image_engine* engine); + +/* blit an image on DC */ +void rtgui_image_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* rect); +struct rtgui_image_palette* rtgui_image_palette_create(rt_uint32_t ncolors); + +#endif diff --git a/components/rtgui/include/rtgui/image_container.h b/components/rtgui/include/rtgui/image_container.h index 8708ef09447e8597979a85c075f13a2c577d6dda..b48058372c7ca4e7821eedc64752a0694f7c3206 100644 --- a/components/rtgui/include/rtgui/image_container.h +++ b/components/rtgui/include/rtgui/image_container.h @@ -4,9 +4,23 @@ #include #include -void image_container_system_init(void); +#ifdef RTGUI_IMAGE_CONTAINER +/* image item in image container */ +struct rtgui_image_item +{ + rtgui_image_t *image; + char *filename; -rtgui_image_t* image_container_get(const char* filename); -void image_container_put(rtgui_image_t* image); + rt_uint32_t refcount; +}; +typedef struct rtgui_image_item rtgui_image_item_t; + +void rtgui_system_image_container_init(rt_bool_t load); + +rtgui_image_item_t* rtgui_image_container_get(const char* filename); +rtgui_image_item_t* rtgui_image_container_get_from_mem(const rt_uint8_t* mem, const char* type, rt_uint32_t len); + +void rtgui_image_container_put(rtgui_image_item_t* item); +#endif #endif diff --git a/components/rtgui/include/rtgui/rtgui_config.h b/components/rtgui/include/rtgui/rtgui_config.h index 225225a7a4fffef06dd6ba72d9a253c78919770a..84c9c819f566105f4ce72b9b47220ded3028932d 100644 --- a/components/rtgui/include/rtgui/rtgui_config.h +++ b/components/rtgui/include/rtgui/rtgui_config.h @@ -35,6 +35,8 @@ #define RTGUI_DEFAULT_FONT_SIZE 12 #define RTGUI_USING_STDIO_FILERW + #define RTGUI_IMAGE_CONTAINER + #define RTGUI_IMAGE_XPM #define RTGUI_IMAGE_BMP // #define RTGUI_IMAGE_PNG // #define RTGUI_IMAGE_JPEG