From 7e6bc3591ab1a98f84587da231b51f818193df78 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Thu, 26 Apr 2018 15:01:19 +0800 Subject: [PATCH] vgcanvas over picasso draw text works --- 3rd/picasso/build/pconfig.h | 1 - .../picasso/src/gfx/gfx_font_adapter_null.cpp | 3 +++ .../src/gfx/gfx_font_load_freetype2.cpp | 3 +++ SConstruct | 10 ++++--- demos/demo2_app.c | 26 ++++++++++++++++++- src/SConscript | 1 - src/vgcanvas/vgcanvas_nanovg.c | 10 +++---- src/vgcanvas/vgcanvas_picasso.cpp | 22 +++++++--------- 8 files changed, 53 insertions(+), 23 deletions(-) rename src/vgcanvas/picasso_font.cpp => 3rd/picasso/src/gfx/gfx_font_adapter_null.cpp (99%) diff --git a/3rd/picasso/build/pconfig.h b/3rd/picasso/build/pconfig.h index 7b7439708..8587625ac 100644 --- a/3rd/picasso/build/pconfig.h +++ b/3rd/picasso/build/pconfig.h @@ -3,7 +3,6 @@ //#define ENABLE_LOW_MEMORY 1 #define ENABLE_SYSTEM_MALLOC 1 - //#define ENABLE_FAST_COPY 1 #define ENABLE_FORMAT_RGBA 1 diff --git a/src/vgcanvas/picasso_font.cpp b/3rd/picasso/src/gfx/gfx_font_adapter_null.cpp similarity index 99% rename from src/vgcanvas/picasso_font.cpp rename to 3rd/picasso/src/gfx/gfx_font_adapter_null.cpp index f1081b48e..885dcd59b 100644 --- a/src/vgcanvas/picasso_font.cpp +++ b/3rd/picasso/src/gfx/gfx_font_adapter_null.cpp @@ -18,6 +18,8 @@ #include "graphic_helper.h" #include "graphic_base.h" +#ifndef ENABLE_FREE_TYPE2 + namespace gfx { class font_adapter_impl @@ -215,3 +217,4 @@ void platform_font_shutdown(void) // win32 do nothing } +#endif diff --git a/3rd/picasso/src/gfx/gfx_font_load_freetype2.cpp b/3rd/picasso/src/gfx/gfx_font_load_freetype2.cpp index f0411dcb4..5e9e1bc32 100644 --- a/3rd/picasso/src/gfx/gfx_font_load_freetype2.cpp +++ b/3rd/picasso/src/gfx/gfx_font_load_freetype2.cpp @@ -333,6 +333,9 @@ bool _load_fonts(void) load_font_from_fontconfig(); #elif defined(__ANDROID__) load_font_from_android(); +#elif defined(FONT_FILE_NAME) + font_item* uni_font = get_font_item("sans", FONT_FILE_NAME); + g_font_map.add(uni_font); #else FILE *pf = 0; diff --git a/SConstruct b/SConstruct index 1a00ebf8b..c4d26a136 100644 --- a/SConstruct +++ b/SConstruct @@ -16,7 +16,7 @@ LCD='NANOVG' VGCANVAS='AGG' VGCANVAS='PICASSO' -COMMON_CCFLAGS=' -DLFTK_ROOT=\\\"'+LFTK_ROOT+'\\\" -DLUA_COMPAT_MODULE -DHAS_STD_MALLOC -DSTBTT_STATIC -DSTB_IMAGE_STATIC -DIMAGE_RGBA -DFT2_BUILD_LIBRARY' +COMMON_CCFLAGS=' -DLFTK_ROOT=\\\"'+LFTK_ROOT+'\\\" -DLUA_COMPAT_MODULE -DHAS_STD_MALLOC -DSTBTT_STATIC -DSTB_IMAGE_STATIC -DIMAGE_RGBA' if LCD == 'NANOVG': VGCANVAS='NANOVG' @@ -47,12 +47,16 @@ OS_LIBS=['SDL2', 'glad', 'stdc++', 'pthread', 'm'] if OS_NAME == 'Darwin': OS_LINKFLAGS='-framework OpenGL' COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DLUA_USE_POSIX -D__APPLE__' - print("Macos"); + if VGCANVAS == 'PICASSO': + OS_LIBS = ['freetype'] + OS_LIBS + COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+LFTK_ROOT+'/demos/res/raw/fonts/font.ttf\\\"' elif OS_NAME == 'Linux': OS_LIBS = ['GL'] + OS_LIBS + ['dl'] COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DLUA_USE_POSIX -DLINUX' - print("Linux"); + if VGCANVAS == 'PICASSO': + OS_LIBS = ['freetype'] + OS_LIBS + COMMON_CCFLAGS = COMMON_CCFLAGS + ' -DENABLE_FREE_TYPE2=1 -DFONT_FILE_NAME=\\\"'+LFTK_ROOT+'/demos/res/raw/fonts/font.ttf\\\"' elif OS_NAME == 'Windows': OS_LIBS=['SDL2', 'glad'] diff --git a/demos/demo2_app.c b/demos/demo2_app.c index cd76dceca..de7b2eb6a 100644 --- a/demos/demo2_app.c +++ b/demos/demo2_app.c @@ -360,12 +360,34 @@ static void draw_matrix(vgcanvas_t* vg) { float_t w = 50; float_t h = 50; + vgcanvas_save(vg); vgcanvas_translate(vg, w/2, h/2); vgcanvas_rotate(vg, 3.14/4); vgcanvas_translate(vg, -w/2, -h/2); vgcanvas_rect(vg, 0, 0, w, h); vgcanvas_fill(vg); + vgcanvas_restore(vg); +} + +static void draw_text(vgcanvas_t* vg) { + float_t h = 20; + const char* text = "Hello World"; + float_t w = 100; + + vgcanvas_set_font_size(vg, 20); + vgcanvas_set_font(vg, STR_DEFAULT_FONT); + w = vgcanvas_measure_text(vg, text); + + vgcanvas_save(vg); + vgcanvas_translate(vg, w/2, h/2); + vgcanvas_rotate(vg, 3.14/4); + vgcanvas_translate(vg, -w/2, -h/2); + + + vgcanvas_fill_text(vg, text, 10, 10, 100); + log_debug("text=%s w=%u\n", text, w); + vgcanvas_restore(vg); } static ret_t on_paint_vg(void* ctx, event_t* e) { @@ -377,7 +399,6 @@ static ret_t on_paint_vg(void* ctx, event_t* e) { vgcanvas_set_stroke_color(vg, color_init(0, 0xff, 0, 0xff)); vgcanvas_set_fill_color(vg, color_init(0xff, 0, 0, 0xff)); - draw_basic_shapes(vg, FALSE); vgcanvas_translate(vg, 0, 50); draw_basic_shapes(vg, TRUE); @@ -388,6 +409,9 @@ static ret_t on_paint_vg(void* ctx, event_t* e) { vgcanvas_translate(vg, 50, 100); draw_matrix(vg); + vgcanvas_translate(vg, 0, 100); + + draw_text(vg); return RET_OK; } diff --git a/src/SConscript b/src/SConscript index a94cfea8f..8a4efae77 100644 --- a/src/SConscript +++ b/src/SConscript @@ -24,7 +24,6 @@ elif os.environ['VGCANVAS'] == 'AGG': sources += Glob('vgcanvas/vgcanvas_agg.cpp') else: sources += Glob('vgcanvas/vgcanvas_picasso.cpp') - sources += Glob('vgcanvas/picasso_font.cpp') env=DefaultEnvironment().Clone() env.Library(os.path.join(LIB_DIR, 'lftk'), sources) diff --git a/src/vgcanvas/vgcanvas_nanovg.c b/src/vgcanvas/vgcanvas_nanovg.c index bcac6f0d8..209b427f9 100644 --- a/src/vgcanvas/vgcanvas_nanovg.c +++ b/src/vgcanvas/vgcanvas_nanovg.c @@ -312,13 +312,14 @@ static ret_t vgcanvas_nanovg_set_font(vgcanvas_t* vgcanvas, const char* name) { } if (r != NULL && r->subtype == RESOURCE_TYPE_FONT_TTF) { - canvas->font_id = nvgCreateFontMem(vg, name, (unsigned char*)r->data, r->size, 0); - /*TODO*/ + font_id = nvgCreateFontMem(vg, name, (unsigned char*)r->data, r->size, 0); } - } else { - canvas->font_id = font_id; } + + return_value_if_fail(font_id >= 0, RET_FAIL); + vgcanvas->font = name; + canvas->font_id = font_id; nvgFontFaceId(vg, font_id); return RET_OK; @@ -558,7 +559,6 @@ static ret_t vgcanvas_nanovg_bind_fbo(vgcanvas_t* vgcanvas, framebuffer_object_t static ret_t vgcanvas_nanovg_unbind_fbo(vgcanvas_t* vgcanvas, framebuffer_object_t* fbo) { NVGcontext* vg = ((vgcanvas_nanovg_t*)vgcanvas)->vg; - NVGLUframebuffer* handle = (NVGLUframebuffer*)fbo->handle; nvgEndFrame(vg); nvgluBindFramebuffer(NULL); diff --git a/src/vgcanvas/vgcanvas_picasso.cpp b/src/vgcanvas/vgcanvas_picasso.cpp index 1d40316a9..179982b3e 100644 --- a/src/vgcanvas/vgcanvas_picasso.cpp +++ b/src/vgcanvas/vgcanvas_picasso.cpp @@ -206,7 +206,7 @@ static ret_t vgcanvas_picasso_arc(vgcanvas_t* vgcanvas, float_t x, float_t y, fl ps_point cp = ps_point_init(x, y); ps_context* vg = ((vgcanvas_picasso_t*)vgcanvas)->vg; - ps_arc(vg, &cp, r, start, end, ccw); + ps_arc(vg, &cp, r, start, end, !ccw); return RET_OK; } @@ -422,26 +422,24 @@ static ret_t vgcanvas_picasso_set_text_baseline(vgcanvas_t* vgcanvas, const char static ret_t vgcanvas_picasso_fill_text(vgcanvas_t* vgcanvas, const char* text, float_t x, float_t y, float_t max_width) { + ps_color color = ps_color_init(vgcanvas->fill_color); ps_context* vg = ((vgcanvas_picasso_t*)vgcanvas)->vg; - (void)vg; - (void)text; - (void)x; - (void)y; - (void)max_width; - /*TODO*/ + ps_set_text_color(vg, &color); + ps_text_out_length(vg, x, y, text, strlen(text)); + + (void)max_width; return RET_OK; } static uint32_t vgcanvas_picasso_measure_text(vgcanvas_t* vgcanvas, const char* text) { + ps_size size = {0, 0}; ps_context* vg = ((vgcanvas_picasso_t*)vgcanvas)->vg; - (void)vg; - (void)text; + + ps_get_text_extent(vg, text, strlen(text), &size); - /*TODO*/ - - return RET_OK; + return size.w; } static ret_t vgcanvas_picasso_draw_image(vgcanvas_t* vgcanvas, bitmap_t* img, float_t sx, float_t sy,