font_hz_bmp.c 2.0 KB
Newer Older
qiuyiuestc's avatar
qiuyiuestc 已提交
1

B
bernard.xiong 已提交
2 3 4
#include <rtgui/dc.h>
#include <rtgui/font.h>

qiuyiuestc's avatar
qiuyiuestc 已提交
5 6
#ifdef RTGUI_USING_HZ_BMP

7 8
static void rtgui_hz_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_hz_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect);
B
bernard.xiong 已提交
9 10 11 12 13 14 15 16
struct rtgui_font_engine hz_bmp_font_engine =
{
	RT_NULL,
	RT_NULL,
	rtgui_hz_bitmap_font_draw_text,
	rtgui_hz_bitmap_font_get_metrics
};

17
static void rtgui_hz_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect)
B
bernard.xiong 已提交
18
{
19
	register rt_base_t h, word_bytes;
B
bernard.xiong 已提交
20 21 22 23 24 25 26
	rt_uint8_t* str;
	struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);

	RT_ASSERT(bmp_font != RT_NULL);

	/* drawing height */
	h = (bmp_font->height + rect->y1 > rect->y2)? rect->y2 - rect->y1 : bmp_font->height;
27
	word_bytes = (bmp_font->width + 7)/8;
B
bernard.xiong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

	str = (rt_uint8_t*)text;

	while (len > 0 && rect->x1 < rect->x2)
	{
		const rt_uint8_t* font_ptr;
		rt_ubase_t sect, index;
		register rt_base_t i, j, k;

		/* calculate section and index */
		sect  = *str - 0xA0;
		index = *(str+1) - 0xA0;

		/* get font pixel data */
		font_ptr = bmp_font->bmp + ( 94*(sect-1) + (index-1) )*(bmp_font->width + bmp_font->height);

		/* draw word */
		for (i=0; i < h; i ++)
		{
47
			for (j=0; j < word_bytes; j++)
B
bernard.xiong 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
				for (k=0; k < 8; k++)
				{
					if ( ((font_ptr[i*2 + j] >> (7-k)) & 0x01) != 0 &&
						(rect->x1 + 8 * j + k < rect->x2))
					{
						rtgui_dc_draw_point(dc, rect->x1 + 8*j + k, rect->y1 + i);
					}
				}
		}

		/* move x to next character */
		rect->x1 += bmp_font->width;
		str += 2;
		len -= 2;
	}
}

65
static void rtgui_hz_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect)
B
bernard.xiong 已提交
66 67 68 69 70 71 72 73 74 75
{
	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 = (rt_int16_t)(bmp_font->width * rt_strlen((const char*)text));
	rect->y2 = bmp_font->height;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
76 77

#endif