static void rtgui_freetype_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect) { int index = 0, len; FT_Error err = 0; rt_uint16_t w = 0, h = 0; rt_uint16_t *text_short, *text_ptr; struct rtgui_freetype_font *freetype; RT_ASSERT(font != RT_NULL); RT_ASSERT(rect != RT_NULL); freetype = (struct rtgui_freetype_font *) font->data; RT_ASSERT(freetype != RT_NULL); len = strlen(text); memset(rect, 0, sizeof(struct rtgui_rect)); /* allocate unicode buffer */ text_short = (rt_uint16_t *)rtgui_malloc((len + 1) * 2); if (text_short == RT_NULL) return ; /* out of memory */ /* convert gbk to unicode */ gbk_to_unicode(text_short, text, len); text_ptr = text_short; while (*text_ptr) { index = FT_Get_Char_Index(freetype->face, *text_ptr); err = FT_Load_Glyph(freetype->face, index, FT_LOAD_DEFAULT); if (err == 0) { w += freetype->face->glyph->bitmap.width; if (freetype->face->glyph->bitmap.rows > h) { h = freetype->face->glyph->bitmap.rows; } } text_ptr ++; } rect->x1 = 0; rect->y1 = 0; rect->x2 = w; rect->y2 = h; /* release unicode buffer */ rtgui_free(text_short); }
static void rtgui_freetype_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect) { int index = 0; FT_Error err = 0; rt_uint16_t *text_short, *text_ptr; struct rtgui_freetype_font* freetype; RT_ASSERT(font != RT_NULL); freetype = (struct rtgui_freetype_font*) font->data; RT_ASSERT(freetype != RT_NULL); /* allocate unicode buffer */ text_short = (rt_uint16_t*)rtgui_malloc((len + 1)* 2); if (text_short == RT_NULL) return ; /* out of memory */ /* convert gbk to unicode */ gbk_to_unicode(text_short, text, len); text_ptr = text_short; while (*text_ptr) { index = FT_Get_Char_Index(freetype->face, *text_ptr); err = FT_Load_Glyph(freetype->face, index, FT_LOAD_DEFAULT|FT_LOAD_RENDER); if (err == 0) { int rows, x; rt_uint8_t* ptr; /* render font */ ptr = (rt_uint8_t*)freetype->face->glyph->bitmap.buffer; for (rows = 0; rows < freetype->face->glyph->bitmap.rows; rows ++) for (x = 0; x < freetype->face->glyph->bitmap.width; x++) { if (*ptr > 0) rtgui_dc_draw_color_point(dc, rect->x1 + x, rect->y1 + rows, RTGUI_RGB(0xff - *ptr, 0xff - *ptr, 0xff - *ptr)); ptr ++; } } text_ptr ++; rect->x1 += freetype->face->glyph->bitmap.width; } /* release unicode buffer */ rtgui_free(text_short); }