example: load font from memory in Text.cpp

This commit is contained in:
Mira Grudzinska 2024-06-05 01:17:13 +02:00 committed by Hermet Park
parent d8a720fb7e
commit 50d1d2fd36

View file

@ -21,6 +21,7 @@
*/ */
#include "Common.h" #include "Common.h"
#include <fstream>
/************************************************************************/ /************************************************************************/
/* Drawing Commands */ /* Drawing Commands */
@ -50,10 +51,20 @@ void tvgDrawCmds(tvg::Canvas* canvas)
return; return;
} }
if (tvg::Text::load(EXAMPLE_DIR"/font/SentyCloud.ttf") != tvg::Result::Success) { //Load from memory
cout << "TTF is not supported. Did you enable TTF Loader?" << endl; ifstream file(EXAMPLE_DIR"/font/SentyCloud.ttf", ios::binary);
return; 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(); auto text = tvg::Text::gen();
text->font("Arial", 80); text->font("Arial", 80);