From 6829593ac6f6232cbc8cd1efafc659a3ef5912dd Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Sat, 8 Jun 2024 15:55:56 +0200 Subject: [PATCH] examples: capi updated with text examples --- examples/Capi.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/examples/Capi.cpp b/examples/Capi.cpp index dfa37b16..949a85e5 100644 --- a/examples/Capi.cpp +++ b/examples/Capi.cpp @@ -22,6 +22,7 @@ #include #include +#include #define WIDTH 800 #define HEIGHT 800 @@ -223,6 +224,55 @@ void testCapi() elm_transit_go(transit); } +//////7. Text + //load from a file + if (tvg_font_load(EXAMPLE_DIR"/font/SentyCloud.ttf") != TVG_RESULT_SUCCESS) { + printf("Problem with loading the font from the file. Did you enable TTF Loader?\n"); + } else { + Tvg_Paint *text = tvg_text_new(); + tvg_text_set_font(text, "SentyCloud", 25.0f, ""); + tvg_text_set_fill_color(text, 0, 0, 255); + tvg_text_set_text(text, "\xE7\xB4\xA2\xE5\xB0\x94\x56\x47\x20\xE6\x98\xAF\xE6\x9C\x80\xE5\xA5\xBD\xE7\x9A\x84"); + tvg_paint_translate(text, 50.0f, 380.0f); + tvg_canvas_push(canvas, text); + } + //load from a memory + FILE *file = fopen(EXAMPLE_DIR"/font/SentyCloud.ttf", "rb"); + if (file == NULL) return; + fseek(file, 0, SEEK_END); + long data_size = ftell(file); + fseek(file, 0, SEEK_SET); + char* data = (char*)malloc(data_size); + if (!data) return; + if (fread(data, 1, data_size, file) != data_size) { + free(data); + fclose(file); + return; + } + if (tvg_font_load_data("Arial", data, data_size, "ttf", true) != TVG_RESULT_SUCCESS) { + printf("Problem with loading the font file from a memory. Did you enable TTF Loader?\n"); + } else { + //Radial gradient + Tvg_Gradient* grad = tvg_radial_gradient_new(); + tvg_radial_gradient_set(grad, 200.0f, 200.0f, 20.0f); + Tvg_Color_Stop color_stops[2] = + { + {0.0f, 255, 0, 255, 255}, + {1.0f, 0, 0, 255, 255} + }; + tvg_gradient_set_color_stops(grad, color_stops, 2); + tvg_gradient_set_spread(grad, TVG_STROKE_FILL_REFLECT); + + Tvg_Paint *text = tvg_text_new(); + tvg_text_set_font(text, "Arial", 20.0f, "italic"); + tvg_text_set_linear_gradient(text, grad); + tvg_text_set_text(text, "ThorVG is the best"); + tvg_paint_translate(text, 70.0f, 420.0f); + tvg_canvas_push(canvas, text); + } + free(data); + fclose(file); + //////Draw the canvas tvg_canvas_draw(canvas); tvg_canvas_sync(canvas);