2010-09-26 22:30:42 +08:00
|
|
|
/*
|
|
|
|
* 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 <rtgui/font.h>
|
|
|
|
#include <rtgui/dc.h>
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2011-01-20 17:59:45 +08:00
|
|
|
rtgui_color_t bc;
|
2010-09-26 22:30:42 +08:00
|
|
|
const rt_uint8_t* font_ptr;
|
2011-01-20 17:59:45 +08:00
|
|
|
rt_uint16_t x, y, h, style;
|
2010-09-26 22:30:42 +08:00
|
|
|
register rt_base_t i, j, k, word_bytes;
|
|
|
|
|
|
|
|
/* check first and last char */
|
|
|
|
if (ch < font->first_char || ch > font->last_char) return;
|
|
|
|
|
2011-01-20 17:59:45 +08:00
|
|
|
/* get text style */
|
|
|
|
style = rtgui_dc_get_gc(dc)->textstyle;
|
|
|
|
bc = rtgui_dc_get_gc(dc)->background;
|
|
|
|
|
2010-09-26 22:30:42 +08:00
|
|
|
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);
|
|
|
|
}
|
2011-01-20 17:59:45 +08:00
|
|
|
else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND)
|
|
|
|
{
|
|
|
|
rtgui_dc_draw_color_point(dc, k + 8 * j + x, i + y, bc);
|
|
|
|
}
|
2010-09-26 22:30:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-09-27 23:36:03 +08:00
|
|
|
rt_uint32_t length;
|
2010-09-26 22:30:42 +08:00
|
|
|
struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
|
2010-11-29 17:19:46 +08:00
|
|
|
#ifdef RTGUI_USING_FONTHZ
|
|
|
|
struct rtgui_font* hz_font;
|
2010-09-26 22:30:42 +08:00
|
|
|
|
|
|
|
RT_ASSERT(bmp_font != RT_NULL);
|
|
|
|
|
2010-09-27 23:36:03 +08:00
|
|
|
hz_font = rtgui_font_refer("hz", font->height);
|
|
|
|
while ((rect->x1 < rect->x2) && len)
|
2010-09-26 22:30:42 +08:00
|
|
|
{
|
2010-09-27 23:36:03 +08:00
|
|
|
length = 0;
|
|
|
|
while ((rt_uint8_t)*(text + length) >= 0x80) length ++; /* it's not a ascii character */
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
if (hz_font != RT_NULL)
|
|
|
|
rtgui_font_draw(hz_font, dc, text, length, rect);
|
|
|
|
text += length;
|
|
|
|
len -= length;
|
|
|
|
}
|
2010-09-26 22:30:42 +08:00
|
|
|
|
2010-09-27 23:36:03 +08:00
|
|
|
length = 0;
|
|
|
|
while (((rt_uint8_t)*(text + length) < 0x80) && *(text + length)) length ++;
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
len -= length;
|
|
|
|
while (length-- && 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 ++;
|
|
|
|
}
|
|
|
|
}
|
2010-09-26 22:30:42 +08:00
|
|
|
}
|
2010-09-27 23:36:03 +08:00
|
|
|
|
|
|
|
rtgui_font_derefer(hz_font);
|
2010-11-29 17:19:46 +08:00
|
|
|
#else
|
|
|
|
while ((rect->x1 < rect->x2) && len)
|
|
|
|
{
|
|
|
|
while (((rt_uint8_t)*(text + length) < 0x80) && *(text + length)) length ++;
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
len -= length;
|
|
|
|
while (length-- && 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 ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-09-26 22:30:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect)
|
|
|
|
{
|
2010-09-27 23:36:03 +08:00
|
|
|
rt_uint32_t length;
|
2010-09-26 22:30:42 +08:00
|
|
|
struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
|
|
|
|
|
|
|
|
RT_ASSERT(bmp_font != RT_NULL);
|
|
|
|
|
2010-09-27 23:36:03 +08:00
|
|
|
/* set init metrics rect */
|
|
|
|
rect->x1 = rect->y1 = 0;rect->x2 = 0;
|
|
|
|
rect->y2 = bmp_font->height;
|
2010-09-26 22:30:42 +08:00
|
|
|
|
2010-09-27 23:36:03 +08:00
|
|
|
while (*text)
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
while (*(text + length) >= 0x80) length ++; /* it's not a ascii character */
|
|
|
|
rect->x2 += (font->height/2) * length;
|
|
|
|
text += length;
|
|
|
|
|
|
|
|
length = 0;
|
|
|
|
while ((*(text + length) < 0x80) && *(text + length)) length ++;
|
|
|
|
if (bmp_font->char_width != NULL)
|
2010-09-26 22:30:42 +08:00
|
|
|
{
|
2010-09-27 23:36:03 +08:00
|
|
|
/* get width for each character */
|
2010-12-03 07:54:17 +08:00
|
|
|
while (*text && (*text < 0x80))
|
2010-09-27 23:36:03 +08:00
|
|
|
{
|
|
|
|
rect->x2 += bmp_font->char_width[*text - bmp_font->first_char];
|
|
|
|
text ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* set metrics rect */
|
|
|
|
rect->x2 += bmp_font->width * length;
|
|
|
|
text += length;
|
2010-09-26 22:30:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|