renderer/loader: support ttf loader.

Applied 2 more internal LoaderMgr interfaces for
gobally manage the font data resources.

The next function is introduced for lookup the existing loader
with the font name (key)
- static LoaderMgr::LoadModule* loader(const char* key);

The next function is introduced to free the existing loader
with the loader source(file path)
- static bool retrieve(const string& path);

Additionally implements the base loader to bind the ttf loader.
This commit is contained in:
Hermet Park 2023-12-15 17:09:20 +09:00
parent 03c53d3227
commit e53ee5881f
4 changed files with 55 additions and 8 deletions

View file

@ -62,7 +62,7 @@ using namespace tvg;
#define TVG_CLASS_ID_LINEAR 4
#define TVG_CLASS_ID_RADIAL 5
enum class FileType { Tvg = 0, Svg, Lottie, Raw, Png, Jpg, Webp, Gif, Unknown };
enum class FileType { Tvg = 0, Svg, Ttf, Lottie, Raw, Png, Jpg, Webp, Gif, Unknown };
using Size = Point;

View file

@ -47,10 +47,7 @@ struct LoadModule
virtual bool open(const string& path) { return false; }
virtual bool open(const char* data, uint32_t size, bool copy) { return false; }
//Override this if the vector-format has own resizing policy.
virtual bool resize(Paint* paint, float w, float h) { return false; }
virtual void sync() {}; //finish immediately if any async update jobs.
virtual bool read()

View file

@ -43,6 +43,10 @@
#include "tvgWebpLoader.h"
#endif
#ifdef THORVG_TTF_LOADER_SUPPORT
#include "tvgTtfLoader.h"
#endif
#ifdef THORVG_LOTTIE_LOADER_SUPPORT
#include "tvgLottieLoader.h"
#endif
@ -74,6 +78,12 @@ static LoadModule* _find(FileType type)
case FileType::Svg: {
#ifdef THORVG_SVG_LOADER_SUPPORT
return new SvgLoader;
#endif
break;
}
case FileType::Ttf: {
#ifdef THORVG_TTF_LOADER_SUPPORT
return new TtfLoader;
#endif
break;
}
@ -121,6 +131,10 @@ static LoadModule* _find(FileType type)
format = "SVG";
break;
}
case FileType::Ttf: {
format = "TTF";
break;
}
case FileType::Lottie: {
format = "lottie(json)";
break;
@ -162,6 +176,8 @@ static LoadModule* _findByPath(const string& path)
if (!ext.compare("png")) return _find(FileType::Png);
if (!ext.compare("jpg")) return _find(FileType::Jpg);
if (!ext.compare("webp")) return _find(FileType::Webp);
if (!ext.compare("ttf") || !ext.compare("ttc")) return _find(FileType::Ttf);
if (!ext.compare("otf") || !ext.compare("otc")) return _find(FileType::Ttf);
return nullptr;
}
@ -172,6 +188,7 @@ static FileType _convert(const string& mimeType)
if (mimeType == "tvg") type = FileType::Tvg;
else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg;
else if (mimeType == "ttf" || mimeType == "otf") type = FileType::Ttf;
else if (mimeType == "lottie") type = FileType::Lottie;
else if (mimeType == "raw") type = FileType::Raw;
else if (mimeType == "png") type = FileType::Png;
@ -237,18 +254,28 @@ bool LoaderMgr::init()
bool LoaderMgr::term()
{
auto loader = _activeLoaders.head;
//clean up the remained font loaders which is globally used.
while (loader && loader->type == FileType::Ttf) {
auto ret = loader->close();
auto tmp = loader;
loader = loader->next;
_activeLoaders.remove(tmp);
if (ret) delete(loader);
}
return true;
}
void LoaderMgr::retrieve(LoadModule* loader)
bool LoaderMgr::retrieve(LoadModule* loader)
{
if (!loader) return;
if (!loader) return false;
if (loader->close()) {
_activeLoaders.remove(loader);
delete(loader);
}
return true;
}
@ -271,6 +298,27 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
}
bool LoaderMgr::retrieve(const string& path)
{
return retrieve(_findFromCache(path));
}
LoadModule* LoaderMgr::loader(const char* key)
{
auto loader = _activeLoaders.head;
while (loader) {
if (loader->hashpath && strstr(loader->hashpath, key)) {
++loader->sharing;
return loader;
}
loader = loader->next;
}
return nullptr;
}
LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy)
{
if (auto loader = _findFromCache(data, size, mimeType)) return loader;

View file

@ -32,7 +32,9 @@ struct LoaderMgr
static LoadModule* loader(const string& path, bool* invalid);
static LoadModule* loader(const char* data, uint32_t size, const string& mimeType, bool copy);
static LoadModule* loader(const uint32_t* data, uint32_t w, uint32_t h, bool copy);
static void retrieve(LoadModule* loader);
static LoadModule* loader(const char* key);
static bool retrieve(const string& path);
static bool retrieve(LoadModule* loader);
};
#endif //_TVG_LOADER_H_