font_hz_bmp.c 3.3 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);
9
const struct rtgui_font_engine hz_bmp_font_engine =
B
bernard.xiong 已提交
10 11 12 13 14 15 16
{
	RT_NULL,
	RT_NULL,
	rtgui_hz_bitmap_font_draw_text,
	rtgui_hz_bitmap_font_get_metrics
};

B
bernard.xiong@gmail.com 已提交
17
static void _rtgui_hz_bitmap_font_draw_text(struct rtgui_font_bitmap* bmp_font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect)
B
bernard.xiong 已提交
18 19
{
	rt_uint8_t* str;
20 21 22
	rtgui_color_t bc;
	rt_uint16_t style;
	register rt_base_t h, word_bytes;
B
bernard.xiong 已提交
23 24 25

	RT_ASSERT(bmp_font != RT_NULL);

26 27 28 29
	/* get text style */
	style = rtgui_dc_get_gc(dc)->textstyle;
	bc = rtgui_dc_get_gc(dc)->background;

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

	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 ++)
		{
52
			for (j=0; j < word_bytes; j++)
B
bernard.xiong 已提交
53 54 55 56 57 58 59
				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);
					}
60 61 62 63
					else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND)
					{
						rtgui_dc_draw_color_point(dc, rect->x1 + 8*j + k, rect->y1 + i, bc);
					}
B
bernard.xiong 已提交
64 65 66 67 68 69 70 71 72 73
				}
		}

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

B
bernard.xiong@gmail.com 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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);
}

113
static void rtgui_hz_bitmap_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect)
B
bernard.xiong 已提交
114 115 116 117 118 119 120
{
	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;
B
bernard.xiong@gmail.com 已提交
121
	/* Chinese font is always fixed font */
B
bernard.xiong 已提交
122 123 124
	rect->x2 = (rt_int16_t)(bmp_font->width * rt_strlen((const char*)text));
	rect->y2 = bmp_font->height;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
125 126

#endif