examples: capi updated with text examples

This commit is contained in:
Mira Grudzinska 2024-06-08 15:55:56 +02:00 committed by Hermet Park
parent 2be8bfd9da
commit 6829593ac6

View file

@ -22,6 +22,7 @@
#include <Elementary.h>
#include <thorvg_capi.h>
#include <stdio.h>
#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);