mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-29 18:48:09 +08:00
improve vgcanvas
This commit is contained in:
parent
b1fadad97e
commit
e138047228
@ -218,31 +218,11 @@ static void prepareRasterizer(AGGENVGcontext* agge, NVGscissor* scissor, NVGpain
|
||||
}
|
||||
|
||||
template <typename PixelT>
|
||||
void renderFill(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation,
|
||||
NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths,
|
||||
int npaths) {
|
||||
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
|
||||
void renderPaint(AGGENVGcontext* agge, NVGpaint* paint) {
|
||||
agge::renderer& ren = agge->ren;
|
||||
agge::rasterizer<agge::clipper<int> >& ras = agge->ras;
|
||||
agge::bitmap<PixelT, agge::raw_bitmap> surface(agge->w, agge->h, agge->data);
|
||||
|
||||
prepareRasterizer(agge, scissor, paint);
|
||||
|
||||
for (int i = 0; i < npaths; i++) {
|
||||
const NVGpath* p = paths + i;
|
||||
for (int j = 0; j < p->nfill; j++) {
|
||||
const NVGvertex* v = p->fill + j;
|
||||
if (j == 0) {
|
||||
ras.move_to(v->x, v->y);
|
||||
} else {
|
||||
ras.line_to(v->x, v->y);
|
||||
}
|
||||
}
|
||||
ras.close_polygon();
|
||||
}
|
||||
|
||||
ras.sort();
|
||||
|
||||
if (paint->image > 0) {
|
||||
float invxform[6];
|
||||
AGGENVGtexture* tex = aggenvg__findTexture(agge, paint->image);
|
||||
@ -289,19 +269,42 @@ void renderFill(void* uptr, NVGpaint* paint, NVGcompositeOperationState composit
|
||||
}
|
||||
}
|
||||
|
||||
template <typename PixelT>
|
||||
void renderFill(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation,
|
||||
NVGscissor* scissor, float fringe, const float* bounds, const NVGpath* paths,
|
||||
int npaths) {
|
||||
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
|
||||
agge::rasterizer<agge::clipper<int> >& ras = agge->ras;
|
||||
|
||||
prepareRasterizer(agge, scissor, paint);
|
||||
|
||||
for (int i = 0; i < npaths; i++) {
|
||||
const NVGpath* p = paths + i;
|
||||
for (int j = 0; j < p->nfill; j++) {
|
||||
const NVGvertex* v = p->fill + j;
|
||||
if (j == 0) {
|
||||
ras.move_to(v->x, v->y);
|
||||
} else {
|
||||
ras.line_to(v->x, v->y);
|
||||
}
|
||||
}
|
||||
ras.close_polygon();
|
||||
}
|
||||
|
||||
ras.sort();
|
||||
renderPaint<PixelT>(agge, paint);
|
||||
}
|
||||
|
||||
template <typename PixelT>
|
||||
void renderStroke(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation,
|
||||
NVGscissor* scissor, float fringe, float strokeWidth, const NVGpath* paths,
|
||||
int npaths) {
|
||||
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
|
||||
agge::renderer& ren = agge->ren;
|
||||
agge::stroke& line_style = agge->line_style;
|
||||
agge::rasterizer<agge::clipper<int> >& ras = agge->ras;
|
||||
|
||||
line_style.width(strokeWidth);
|
||||
prepareRasterizer(agge, scissor, paint);
|
||||
agge::bitmap<PixelT, agge::raw_bitmap> surface(agge->w, agge->h, agge->data);
|
||||
agge::blender_solid_color_rgb<PixelT> color(agge->r, agge->g, agge->b, agge->a);
|
||||
|
||||
for (int i = 0; i < npaths; i++) {
|
||||
const NVGpath* p = paths + i;
|
||||
@ -312,12 +315,13 @@ void renderStroke(void* uptr, NVGpaint* paint, NVGcompositeOperationState compos
|
||||
}
|
||||
|
||||
ras.sort();
|
||||
ren(surface, 0, ras, color, agge::winding<>());
|
||||
renderPaint<PixelT>(agge, paint);
|
||||
}
|
||||
|
||||
template <typename PixelT>
|
||||
void renderTriangles(void* uptr, NVGpaint* paint, NVGcompositeOperationState compositeOperation,
|
||||
NVGscissor* scissor, const NVGvertex* verts, int nverts) {
|
||||
/*XXX: not used yet*/
|
||||
AGGENVGcontext* agge = (AGGENVGcontext*)uptr;
|
||||
agge::renderer& ren = agge->ren;
|
||||
agge::rasterizer<agge::clipper<int> >& ras = agge->ras;
|
||||
@ -347,12 +351,8 @@ void renderTriangles(void* uptr, NVGpaint* paint, NVGcompositeOperationState com
|
||||
agge::nanovg_text_blender<PixelT, source_bitmap_t> color(&src, (float*)invxform, 255, 0, 0,
|
||||
255);
|
||||
ren(surface, 0, ras, color, agge::winding<>());
|
||||
|
||||
// agge::blender_solid_color_rgb<PixelT> color(255, 0, 0, 255);
|
||||
// ren(surface, 0, ras, color, agge::winding<>());
|
||||
} else {
|
||||
agge::blender_solid_color_rgb<PixelT> color(agge->r, agge->g, agge->b, agge->a);
|
||||
|
||||
ren(surface, 0, ras, color, agge::winding<>());
|
||||
}
|
||||
}
|
||||
|
@ -307,6 +307,7 @@ static void draw_basic_shapes(vgcanvas_t* vg, bool_t stroke) {
|
||||
else
|
||||
vgcanvas_fill(vg);
|
||||
|
||||
vgcanvas_begin_path(vg);
|
||||
vgcanvas_translate(vg, 65, 0);
|
||||
vgcanvas_arc(vg, 20, 20, 20, 0, 2 * 3.15, FALSE);
|
||||
if (stroke)
|
||||
@ -314,6 +315,7 @@ static void draw_basic_shapes(vgcanvas_t* vg, bool_t stroke) {
|
||||
else
|
||||
vgcanvas_fill(vg);
|
||||
|
||||
vgcanvas_begin_path(vg);
|
||||
vgcanvas_translate(vg, 50, 0);
|
||||
vgcanvas_move_to(vg, 0, 0);
|
||||
vgcanvas_line_to(vg, 40, 0);
|
||||
@ -324,6 +326,7 @@ static void draw_basic_shapes(vgcanvas_t* vg, bool_t stroke) {
|
||||
else
|
||||
vgcanvas_fill(vg);
|
||||
|
||||
vgcanvas_begin_path(vg);
|
||||
vgcanvas_restore(vg);
|
||||
}
|
||||
|
||||
@ -464,8 +467,6 @@ static ret_t on_paint_vg_simple(void* ctx, event_t* e) {
|
||||
vgcanvas_rect(vg, 5, 5, 100, 100);
|
||||
vgcanvas_fill(vg);
|
||||
|
||||
/*
|
||||
*/
|
||||
vgcanvas_set_fill_color(vg, color_init(0, 0xff, 0, 0xff));
|
||||
vgcanvas_rect(vg, 110, 5, 100, 100);
|
||||
vgcanvas_fill(vg);
|
||||
@ -482,9 +483,11 @@ static ret_t on_paint_vg_simple(void* ctx, event_t* e) {
|
||||
vgcanvas_set_stroke_color(vg, color_init(0, 0xff, 0, 0xff));
|
||||
vgcanvas_rect(vg, 110, 5, 100, 100);
|
||||
vgcanvas_stroke(vg);
|
||||
|
||||
vgcanvas_set_stroke_color(vg, color_init(0, 0, 0xff, 0xff));
|
||||
vgcanvas_rect(vg, 215, 5, 100, 100);
|
||||
vgcanvas_stroke(vg);
|
||||
|
||||
vgcanvas_translate(vg, 0, 105);
|
||||
image_manager_load(image_manager(), "rgb", &img);
|
||||
vgcanvas_draw_image(vg, &img, 0, 0, img.w, img.h, 0, 0, img.w, img.h);
|
||||
@ -497,12 +500,36 @@ static ret_t on_paint_vg_simple(void* ctx, event_t* e) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t on_paint_vg_paint(void* ctx, event_t* e) {
|
||||
bitmap_t img;
|
||||
paint_event_t* evt = (paint_event_t*)e;
|
||||
canvas_t* c = evt->c;
|
||||
vgcanvas_t* vg = lcd_get_vgcanvas(c->lcd);
|
||||
image_manager_load(image_manager(), "1", &img);
|
||||
|
||||
vgcanvas_save(vg);
|
||||
|
||||
vgcanvas_set_line_width(vg, 10);
|
||||
vgcanvas_translate(vg, 100, 100);
|
||||
vgcanvas_rounded_rect(vg, 0, 0, 200, 30, 15);
|
||||
vgcanvas_paint(vg, TRUE, &img);
|
||||
|
||||
vgcanvas_translate(vg, 0, 200);
|
||||
vgcanvas_rounded_rect(vg, 0, 0, 200, 30, 15);
|
||||
vgcanvas_paint(vg, FALSE, &img);
|
||||
|
||||
vgcanvas_restore(vg);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t application_init() {
|
||||
widget_t* win = window_create(NULL, 0, 0, 0, 0);
|
||||
widget_t* canvas = view_create(win, 0, 0, win->w, win->h);
|
||||
|
||||
// widget_on(canvas, EVT_PAINT, on_paint_vg_simple, NULL);
|
||||
widget_on(canvas, EVT_PAINT, on_paint_vg, NULL);
|
||||
widget_on(canvas, EVT_PAINT, on_paint_vg_simple, NULL);
|
||||
// widget_on(canvas, EVT_PAINT, on_paint_vg, NULL);
|
||||
// widget_on(canvas, EVT_PAINT, on_paint_vg_paint, NULL);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -87,6 +87,12 @@ ret_t vgcanvas_stroke(vgcanvas_t* vg) {
|
||||
return vg->vt->stroke(vg);
|
||||
}
|
||||
|
||||
ret_t vgcanvas_paint(vgcanvas_t* vg, bool_t stroke, bitmap_t* img) {
|
||||
return_value_if_fail(vg != NULL && vg->vt->paint != NULL && img != NULL, RET_BAD_PARAMS);
|
||||
|
||||
return vg->vt->paint(vg, stroke, img);
|
||||
}
|
||||
|
||||
ret_t vgcanvas_destroy(vgcanvas_t* vg) {
|
||||
return_value_if_fail(vg != NULL && vg->vt->destroy != NULL, RET_BAD_PARAMS);
|
||||
|
||||
@ -102,6 +108,7 @@ ret_t vgcanvas_begin_path(vgcanvas_t* vg) {
|
||||
ret_t vgcanvas_rounded_rect(vgcanvas_t* vg, float_t x, float_t y, float_t w, float_t h, float_t r) {
|
||||
return_value_if_fail(vg != NULL && vg->vt->rounded_rect != NULL, RET_BAD_PARAMS);
|
||||
|
||||
vgcanvas_begin_path(vg);
|
||||
return vg->vt->rounded_rect(vg, x, y, w, h, r);
|
||||
}
|
||||
|
||||
@ -114,6 +121,7 @@ ret_t vgcanvas_rect(vgcanvas_t* vg, float_t x, float_t y, float_t w, float_t h)
|
||||
ret_t vgcanvas_ellipse(vgcanvas_t* vg, float_t x, float_t y, float_t rx, float_t ry) {
|
||||
return_value_if_fail(vg != NULL && vg->vt->ellipse != NULL, RET_BAD_PARAMS);
|
||||
|
||||
vgcanvas_begin_path(vg);
|
||||
return vg->vt->ellipse(vg, x, y, rx, ry);
|
||||
}
|
||||
|
||||
|
@ -73,9 +73,10 @@ typedef ret_t (*vgcanvas_transform_t)(vgcanvas_t* vg, float_t a, float_t b, floa
|
||||
typedef ret_t (*vgcanvas_set_transform_t)(vgcanvas_t* vg, float_t a, float_t b, float_t c,
|
||||
float_t d, float_t e, float_t f);
|
||||
|
||||
typedef ret_t (*vgcanvas_fill_t)(vgcanvas_t* vg);
|
||||
typedef ret_t (*vgcanvas_clip_rect_t)(vgcanvas_t* vg, float_t x, float_t y, float_t w, float_t h);
|
||||
typedef ret_t (*vgcanvas_fill_t)(vgcanvas_t* vg);
|
||||
typedef ret_t (*vgcanvas_stroke_t)(vgcanvas_t* vg);
|
||||
typedef ret_t (*vgcanvas_paint_t)(vgcanvas_t* vg, bool_t stroke, bitmap_t* img);
|
||||
|
||||
typedef ret_t (*vgcanvas_set_font_t)(vgcanvas_t* vg, const char* font);
|
||||
typedef ret_t (*vgcanvas_set_font_size_t)(vgcanvas_t* vg, float_t size);
|
||||
@ -133,9 +134,10 @@ typedef struct _vgcanvas_vtable_t {
|
||||
vgcanvas_transform_t transform;
|
||||
vgcanvas_set_transform_t set_transform;
|
||||
|
||||
vgcanvas_fill_t fill;
|
||||
vgcanvas_clip_rect_t clip_rect;
|
||||
vgcanvas_fill_t fill;
|
||||
vgcanvas_stroke_t stroke;
|
||||
vgcanvas_paint_t paint;
|
||||
|
||||
vgcanvas_set_font_t set_font;
|
||||
vgcanvas_set_font_size_t set_font_size;
|
||||
@ -562,15 +564,6 @@ ret_t vgcanvas_transform(vgcanvas_t* vg, float_t a, float_t b, float_t c, float_
|
||||
ret_t vgcanvas_set_transform(vgcanvas_t* vg, float_t a, float_t b, float_t c, float_t d, float_t e,
|
||||
float_t f);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_fill
|
||||
* fill。为了让不同实现上的效果一致,自动清除之前的path。
|
||||
* @param {vgcanvas_t*} vg vgcanvas对象。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t vgcanvas_fill(vgcanvas_t* vg);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_clip_rect
|
||||
* clip_rect
|
||||
@ -584,15 +577,36 @@ ret_t vgcanvas_fill(vgcanvas_t* vg);
|
||||
*/
|
||||
ret_t vgcanvas_clip_rect(vgcanvas_t* vg, float_t x, float_t y, float_t w, float_t h);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_fill
|
||||
* 填充多边形。
|
||||
* @param {vgcanvas_t*} vg vgcanvas对象。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t vgcanvas_fill(vgcanvas_t* vg);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_stroke
|
||||
* stroke。为了让不同实现上的效果一致,自动清除之前的path。
|
||||
* 画线。
|
||||
* @param {vgcanvas_t*} vg vgcanvas对象。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t vgcanvas_stroke(vgcanvas_t* vg);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_paint
|
||||
* 用图片填充/画多边形(可能存在可移植性问题,除非必要请勿使用)。
|
||||
* 多边形的顶点必须在图片范围内,可以通过矩阵变化画到不同的位置。
|
||||
* @param {vgcanvas_t*} vg vgcanvas对象。
|
||||
* @param {bool_t} stroke TRUE表示画线FALSE表示填充。
|
||||
* @param {bitmap_t*} img 图片。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t vgcanvas_paint(vgcanvas_t* vg, bool_t stroke, bitmap_t* img);
|
||||
|
||||
/**
|
||||
* @method vgcanvas_set_font
|
||||
* set font
|
||||
|
@ -120,8 +120,7 @@ widget_t* switch_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
|
||||
}
|
||||
|
||||
widget_t* switch_cast(widget_t* widget) {
|
||||
return_value_if_fail(widget != NULL && (widget->vt == &s_switch_vtable),
|
||||
NULL);
|
||||
return_value_if_fail(widget != NULL && (widget->vt == &s_switch_vtable), NULL);
|
||||
|
||||
return widget;
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ static ret_t text_selector_on_paint_self(widget_t* widget, canvas_t* c) {
|
||||
|
||||
canvas_get_clip_rect(c, &r_save);
|
||||
r = rect_intersect(&r_save, &r);
|
||||
if(r.w > 0 && r.h > 0) {
|
||||
if (r.w > 0 && r.h > 0) {
|
||||
canvas_set_clip_rect(c, &r);
|
||||
text_selector_paint_self(widget, c);
|
||||
canvas_set_clip_rect(c, &r_save);
|
||||
|
@ -232,15 +232,6 @@ static ret_t vgcanvas_nanovg_close_path(vgcanvas_t* vgcanvas) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t vgcanvas_nanovg_fill(vgcanvas_t* vgcanvas) {
|
||||
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
|
||||
|
||||
nvgFill(vg);
|
||||
nvgBeginPath(vg);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t vgcanvas_nanovg_clip_rect(vgcanvas_t* vgcanvas, float_t x, float_t y, float_t w,
|
||||
float_t h) {
|
||||
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
|
||||
@ -250,11 +241,41 @@ static ret_t vgcanvas_nanovg_clip_rect(vgcanvas_t* vgcanvas, float_t x, float_t
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t vgcanvas_nanovg_fill(vgcanvas_t* vgcanvas) {
|
||||
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
|
||||
|
||||
nvgFill(vg);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t vgcanvas_nanovg_stroke(vgcanvas_t* vgcanvas) {
|
||||
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
|
||||
|
||||
nvgStroke(vg);
|
||||
nvgBeginPath(vg);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t vgcanvas_nanovg_paint(vgcanvas_t* vgcanvas, bool_t stroke, bitmap_t* img) {
|
||||
int iw = img->w;
|
||||
int ih = img->h;
|
||||
NVGpaint imgPaint;
|
||||
NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg;
|
||||
vgcanvas_nanovg_t* canvas = (vgcanvas_nanovg_t*)vgcanvas;
|
||||
int id = vgcanvas_nanovg_ensure_image(canvas, img);
|
||||
return_value_if_fail(id >= 0, RET_BAD_PARAMS);
|
||||
|
||||
imgPaint = nvgImagePattern(vg, 0, 0, iw, ih, 0, id, 1);
|
||||
|
||||
if (stroke) {
|
||||
nvgStrokePaint(vg, imgPaint);
|
||||
nvgStroke(vg);
|
||||
} else {
|
||||
nvgFillPaint(vg, imgPaint);
|
||||
nvgClosePath(vg);
|
||||
nvgFill(vg);
|
||||
}
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
@ -735,9 +756,10 @@ static const vgcanvas_vtable_t vt = {vgcanvas_nanovg_reinit,
|
||||
vgcanvas_nanovg_translate,
|
||||
vgcanvas_nanovg_transform,
|
||||
vgcanvas_nanovg_set_transform,
|
||||
vgcanvas_nanovg_fill,
|
||||
vgcanvas_nanovg_clip_rect,
|
||||
vgcanvas_nanovg_fill,
|
||||
vgcanvas_nanovg_stroke,
|
||||
vgcanvas_nanovg_paint,
|
||||
vgcanvas_nanovg_set_font,
|
||||
vgcanvas_nanovg_set_font_size,
|
||||
vgcanvas_nanovg_set_text_align,
|
||||
|
Loading…
Reference in New Issue
Block a user