From c35b731f0fc7593d66fdf181e099074963b8b062 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Wed, 5 Jun 2024 01:17:13 +0200 Subject: [PATCH] example: load font from memory in Text.cpp --- examples/Text.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/Text.cpp b/examples/Text.cpp index cfe4ab12..bcc6be4b 100644 --- a/examples/Text.cpp +++ b/examples/Text.cpp @@ -21,6 +21,7 @@ */ #include "Common.h" +#include /************************************************************************/ /* Drawing Commands */ @@ -50,10 +51,20 @@ void tvgDrawCmds(tvg::Canvas* canvas) return; } - if (tvg::Text::load(EXAMPLE_DIR"/font/SentyCloud.ttf") != tvg::Result::Success) { - cout << "TTF is not supported. Did you enable TTF Loader?" << endl; - return; + //Load from memory + ifstream file(EXAMPLE_DIR"/font/SentyCloud.ttf", ios::binary); + if (!file.is_open()) return; + file.seekg(0, std::ios::end); + auto size = file.tellg(); + file.seekg(0, std::ios::beg); + auto data = (char*)malloc(size); + if (!data) return; + file.read(data, size); + file.close(); + if (tvg::Text::load("SentyCloud", data, size, "ttf", true) != tvg::Result::Success) { + cout << "Error while loading TTF from memory. Did you enable TTF Loader?" << endl; } + free(data); auto text = tvg::Text::gen(); text->font("Arial", 80);