/* draw caret */ static void rtgui_edit_draw_caret(struct rtgui_edit *edit) { int x,y; rtgui_color_t color; rtgui_rect_t rect; int ofs=0; struct rtgui_dc *dc; RT_ASSERT(edit != RT_NULL); if(edit->caret == RT_NULL) return; rect = edit->caret_rect; dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit)); if(dc == RT_NULL)return; for(x=rect.x1; x<rect.x2; x++) { for(y=rect.y1; y<rect.y2; y++) { color = *(edit->caret + ofs); ofs++; if(edit->flag & RTGUI_EDIT_CARET) { color = ~color; rtgui_dc_draw_color_point(dc, x,y, color); } else { rtgui_dc_draw_color_point(dc, x,y, color); } } } rtgui_dc_end_drawing(dc); }
/* draw caret */ static void rtgui_textbox_draw_caret(rtgui_textbox_t *box, rt_uint16_t position) { int x, y; rtgui_color_t color; rtgui_rect_t rect; int ofs = 0; struct rtgui_dc *dc; RT_ASSERT(box != RT_NULL); if (box->caret == RT_NULL) return; dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); if (dc == RT_NULL) return; rect = box->caret_rect; for (x = rect.x1; x < rect.x2; x++) { for (y = rect.y1; y < rect.y2; y++) { color = *(box->caret + ofs); ofs++; if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) { color = ~color; } rtgui_dc_draw_color_point(dc, x, y, color); } } rtgui_dc_end_drawing(dc); }
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); }
static void rtgui_image_png_blit(struct rtgui_image *image, struct rtgui_dc *dc, struct rtgui_rect *rect) { rt_uint16_t x, y, w, h; rtgui_color_t *ptr; struct rtgui_image_png *png; int fg_maxsample; int ialpha; float alpha; rtgui_color_t color; rtgui_color_t c, bgcolor; int fc[3], bc[3]; struct rtgui_graphic_driver *hwdev = rtgui_graphic_get_device(); RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL); RT_ASSERT(image->data != RT_NULL); png = (struct rtgui_image_png *) image->data; w = _UI_MIN(image->w, rtgui_rect_width(*rect)); h = _UI_MIN(image->h, rtgui_rect_height(*rect)); fg_maxsample = (1 << png->info_ptr->bit_depth) - 1; if (png->pixels != RT_NULL) { ptr = (rtgui_color_t *)png->pixels; bgcolor = RTGUI_DC_BC(dc); bc[0] = RTGUI_RGB_R(bgcolor); bc[1] = RTGUI_RGB_G(bgcolor); bc[2] = RTGUI_RGB_B(bgcolor); /* draw each point within dc */ for (y = 0; y < h; y ++) { for (x = 0; x < w; x++) { c = *ptr; ialpha = RTGUI_RGB_A(c); if (ialpha == 0) { /* * Foreground image is transparent hear. * If the background image is already in the frame * buffer, there is nothing to do. */ } else if (ialpha == fg_maxsample) { /* * Copy foreground pixel to frame buffer. */ rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, c); } else { /* output = alpha * foreground + (1-alpha) * background */ /* * Compositing is necessary. * Get floating-point alpha and its complement. * Note: alpha is always linear: gamma does not * affect it. */ fc[0] = RTGUI_RGB_R(c); fc[1] = RTGUI_RGB_G(c); fc[2] = RTGUI_RGB_B(c); alpha = (float) ialpha / fg_maxsample; color = RTGUI_RGB((rt_uint8_t)(fc[0] * alpha + bc[0] * (1 - alpha)), (rt_uint8_t)(fc[1] * alpha + bc[1] * (1 - alpha)), (rt_uint8_t)(fc[2] * alpha + bc[2] * (1 - alpha))); rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, color); } /* move to next color buffer */ ptr ++; } } } else { png_bytep row; png_bytep data; row = (png_bytep) rtgui_malloc(png_get_rowbytes(png->png_ptr, png->info_ptr)); if (row == RT_NULL) return ; switch (png->info_ptr->color_type) { case PNG_COLOR_TYPE_RGB: for (y = 0; y < h; y++) { png_read_row(png->png_ptr, row, png_bytep_NULL); for (x = 0; x < w; x++) { data = &(row[x * 3]); rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, RTGUI_RGB(data[0], data[1], data[2])); } } break; case PNG_COLOR_TYPE_RGBA: for (y = 0; y < h; y++) { png_read_row(png->png_ptr, row, png_bytep_NULL); for (x = 0; x < w; x++) { data = &(row[x * 4]); if (data[3] != 0) { rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, RTGUI_ARGB(data[3], data[0], data[1], data[2])); } } } break; case PNG_COLOR_TYPE_PALETTE: for (y = 0; y < h; y++) { png_read_row(png->png_ptr, row, png_bytep_NULL); for (x = 0; x < w; x++) { data = &(row[x]); rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red, png->info_ptr->palette[data[0]].green, png->info_ptr->palette[data[0]].blue)); } } default: break; }; rtgui_free(row); } }