From 74a97349d20d5565d08441a86ee3feb01ca467a0 Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Sun, 26 Sep 2010 14:30:42 +0000 Subject: [PATCH] add none-fixed font support. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@961 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/rtgui/SConscript | 1 + components/rtgui/common/asc12font.c | 2 + components/rtgui/common/asc16font.c | 2 + components/rtgui/common/font.c | 72 ---------------- components/rtgui/common/font_bmp.c | 118 ++++++++++++++++++++++++++ components/rtgui/common/hz12font.c | 2 + components/rtgui/common/hz16font.c | 4 +- components/rtgui/include/rtgui/font.h | 26 +++--- 8 files changed, 141 insertions(+), 86 deletions(-) create mode 100644 components/rtgui/common/font_bmp.c diff --git a/components/rtgui/SConscript b/components/rtgui/SConscript index 807db8d98f..88da95fdca 100644 --- a/components/rtgui/SConscript +++ b/components/rtgui/SConscript @@ -20,6 +20,7 @@ common/image.c common/image_xpm.c common/image_hdc.c common/font.c +common/font_bmp.c common/font_hz_file.c common/font_hz_bmp.c common/asc12font.c diff --git a/components/rtgui/common/asc12font.c b/components/rtgui/common/asc12font.c index 2ea0ca049d..cf722311a0 100644 --- a/components/rtgui/common/asc12font.c +++ b/components/rtgui/common/asc12font.c @@ -211,6 +211,8 @@ const rt_uint8_t asc12_font[] = { const struct rtgui_font_bitmap asc12 = { asc12_font, /* bmp */ + RT_NULL, /* each character width, NULL for fixed font */ + RT_NULL, /* offset for each character */ 6, /* width */ 12, /* height */ 0, /* first char */ diff --git a/components/rtgui/common/asc16font.c b/components/rtgui/common/asc16font.c index 81ee349551..0e6d7aceb7 100644 --- a/components/rtgui/common/asc16font.c +++ b/components/rtgui/common/asc16font.c @@ -276,6 +276,8 @@ const unsigned char asc16_font[] = { struct rtgui_font_bitmap asc16 = { (const rt_uint8_t*)asc16_font, /* bmp */ + RT_NULL, /* each character width, NULL for fixed font */ + RT_NULL, /* offset for each character */ 8, /* width */ 16, /* height */ 0, /* first char */ diff --git a/components/rtgui/common/font.c b/components/rtgui/common/font.c index 07c7030831..658dbded78 100644 --- a/components/rtgui/common/font.c +++ b/components/rtgui/common/font.c @@ -156,75 +156,3 @@ void rtgui_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rec rt_memset(rect, 0, sizeof(rtgui_rect_t)); } } - -static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect); -static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect); -struct rtgui_font_engine bmp_font_engine = -{ - RT_NULL, - RT_NULL, - rtgui_bitmap_font_draw_text, - rtgui_bitmap_font_get_metrics -}; - -void rtgui_bitmap_font_draw_char(struct rtgui_font_bitmap* font, struct rtgui_dc* dc, const char ch, - rtgui_rect_t* rect) -{ - const rt_uint8_t* font_ptr; - rt_uint16_t x, y, h; - register rt_base_t i, j, k, word_bytes; - - /* check first and last char */ - if (ch < font->first_char || ch > font->last_char) return; - - x = rect->x1; - y = rect->y1; - word_bytes = (((font->width - 1) / 8) + 1); - - font_ptr = font->bmp + (ch - font->first_char) * word_bytes * font->height; - - h = (font->height + y > rect->y2) ? rect->y2 - rect->y1 : font->height; - - for (i = 0; i < h; i++) - { - for (j = 0; j < word_bytes; j++) - { - for (k = 0; k < 8; k++) - { - if (((font_ptr[i * word_bytes + j] >> (7 - k)) & 0x01) != 0) - { - /* draw a pixel */ - rtgui_dc_draw_point(dc, k + 8 * j + x, i + y); - } - } - } - } -} - -static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect) -{ - struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data); - - RT_ASSERT(bmp_font != RT_NULL); - - while (len-- && rect->x1 < rect->x2) - { - rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect); - - /* move x to next character */ - rect->x1 += bmp_font->width; - text ++; - } -} - -static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect) -{ - struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data); - - RT_ASSERT(bmp_font != RT_NULL); - - /* set metrics rect */ - rect->x1 = rect->y1 = 0; - rect->x2 = bmp_font->width * (rt_int16_t)rt_strlen((const char*)text); - rect->y2 = bmp_font->height; -} diff --git a/components/rtgui/common/font_bmp.c b/components/rtgui/common/font_bmp.c new file mode 100644 index 0000000000..5ea3701ac9 --- /dev/null +++ b/components/rtgui/common/font_bmp.c @@ -0,0 +1,118 @@ +/* + * File : font.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2010, 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 + * 2010-09-15 Bernard first version + */ +#include +#include + +/* bitmap font private data */ + +static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect); +static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect); +const struct rtgui_font_engine bmp_font_engine = +{ + RT_NULL, + RT_NULL, + rtgui_bitmap_font_draw_text, + rtgui_bitmap_font_get_metrics +}; + +void rtgui_bitmap_font_draw_char(struct rtgui_font_bitmap* font, struct rtgui_dc* dc, const char ch, + rtgui_rect_t* rect) +{ + const rt_uint8_t* font_ptr; + rt_uint16_t x, y, h; + register rt_base_t i, j, k, word_bytes; + + /* check first and last char */ + if (ch < font->first_char || ch > font->last_char) return; + + x = rect->x1; + y = rect->y1; + /* get width */ + if (font->char_width == RT_NULL) + { + word_bytes = (((font->width - 1) / 8) + 1); + font_ptr = font->bmp + (ch - font->first_char) * word_bytes * font->height; + } + else + { + word_bytes = ((font->char_width[ch - font->first_char] - 1)/8) + 1; + font_ptr = font->bmp + font->offset[ch - font->first_char]; + } + + h = (font->height + y > rect->y2) ? rect->y2 - rect->y1 : font->height; + + for (i = 0; i < h; i++) + { + for (j = 0; j < word_bytes; j++) + { + for (k = 0; k < 8; k++) + { + if (((font_ptr[i * word_bytes + j] >> (7 - k)) & 0x01) != 0) + { + /* draw a pixel */ + rtgui_dc_draw_point(dc, k + 8 * j + x, i + y); + } + } + } + } +} + +static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect) +{ + struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data); + + RT_ASSERT(bmp_font != RT_NULL); + + while (len-- && rect->x1 < rect->x2) + { + rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect); + + /* move x to next character */ + if (bmp_font->char_width == RT_NULL) + rect->x1 += bmp_font->width; + else + rect->x1 += bmp_font->char_width[*text - bmp_font->first_char]; + text ++; + } +} + +static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect) +{ + struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data); + + RT_ASSERT(bmp_font != RT_NULL); + + if (bmp_font->char_width != NULL) + { + rt_uint32_t index; + + /* set metrics rect */ + rect->x1 = rect->y1 = 0;rect->x2 = 0; + rect->y2 = bmp_font->height; + + /* get width for each character */ + while (*text) + { + rect->x2 += bmp_font->char_width[*text - bmp_font->first_char]; + text ++; + } + } + else + { + /* set metrics rect */ + rect->x1 = rect->y1 = 0; + rect->x2 = bmp_font->width * (rt_int16_t)rt_strlen((const char*)text); + rect->y2 = bmp_font->height; + } +} diff --git a/components/rtgui/common/hz12font.c b/components/rtgui/common/hz12font.c index 6ebcf7ef82..215d73db37 100644 --- a/components/rtgui/common/hz12font.c +++ b/components/rtgui/common/hz12font.c @@ -12275,6 +12275,8 @@ const unsigned char hz12_font[] = { const struct rtgui_font_bitmap hz12 = { hz12_font, /* bmp */ + RT_NULL, /* each character width, NULL for fixed font */ + RT_NULL, /* offset for each character */ 12, /* width */ 12, /* height */ 0, /* first char */ diff --git a/components/rtgui/common/hz16font.c b/components/rtgui/common/hz16font.c index 9650a5fa67..184a953204 100644 --- a/components/rtgui/common/hz16font.c +++ b/components/rtgui/common/hz16font.c @@ -16733,6 +16733,8 @@ const unsigned char hz16_font[] = { const struct rtgui_font_bitmap hz16 = { hz16_font, /* bmp */ + RT_NULL, /* each character width, NULL for fixed font */ + RT_NULL, /* offset for each character */ 16, /* width */ 16, /* height */ 0, /* first char */ @@ -16746,7 +16748,7 @@ struct rtgui_font rtgui_font_hz16 = 16, /* height */ 1, /* refer count */ &hz_bmp_font_engine,/* font engine */ - (void *)&hz16, /* font private data */ + (void *)&hz16, /* font private data */ }; /* size = 267616 bytes */ diff --git a/components/rtgui/include/rtgui/font.h b/components/rtgui/include/rtgui/font.h index 512290efde..a1ec7f644a 100644 --- a/components/rtgui/include/rtgui/font.h +++ b/components/rtgui/include/rtgui/font.h @@ -35,19 +35,19 @@ struct rtgui_font_engine /* * bitmap font engine */ -/* bitmap font private data */ -struct rtgui_font_bitmap -{ - /* bitmap data */ - const rt_uint8_t* bmp; - - rt_uint16_t width; - rt_uint16_t height; - - rt_uint8_t first_char; - rt_uint8_t last_char; +struct rtgui_font_bitmap +{ + const rt_uint8_t* bmp; /* bitmap font data */ + const rt_uint8_t* char_width; /* each character width, NULL for fixed font */ + const rt_uint32_t* offset; /* offset for each character */ + + rt_uint16_t width; /* font width */ + rt_uint16_t height; /* font height */ + + rt_uint8_t first_char; + rt_uint8_t last_char; }; -extern struct rtgui_font_engine bmp_font_engine; +extern const struct rtgui_font_engine bmp_font_engine; #include SPLAY_HEAD(cache_tree, hz_cache); @@ -87,7 +87,7 @@ struct rtgui_font rt_uint32_t refer_count; /* font engine */ - struct rtgui_font_engine* engine; + const struct rtgui_font_engine* engine; /* font private data */ void* data;