text: allow the unnamed font

If the font name is not specified,
ThorVG will select any available font candidate.

This could be useful for font fallback mechanism.
This commit is contained in:
Hermet Park 2025-02-20 12:12:29 +09:00 committed by Hermet Park
parent bed38a4521
commit 8e5ca40250
7 changed files with 18 additions and 2 deletions

View file

@ -72,7 +72,7 @@ struct UserExample : tvgexam::Example
canvas->push(text2);
auto text3 = tvg::Text::gen();
text3->font("Arial", 40);
text3->font(nullptr, 40); //Use any font
text3->text("Kerning Test: VA, AV, TJ, JT");
text3->fill(255, 255, 255);
text3->translate(0, 225);

View file

@ -1528,6 +1528,7 @@ public:
*
* @retval Result::InsufficientCondition when the specified @p name cannot be found.
*
* @note If the @p name is not specified, ThorVG will select any available font candidate.
* @note Experimental API
*/
Result font(const char* name, float size, const char* style = nullptr) noexcept;

View file

@ -2021,6 +2021,7 @@ TVG_API Tvg_Paint* tvg_text_new(void);
* @retval TVG_RESULT_INVALID_ARGUMENT A @c nullptr passed as the @p paint argument.
* @retval TVG_RESULT_INSUFFICIENT_CONDITION The specified @p name cannot be found.
*
* @note If the @p name is not specified, ThorVG will select any available font candidate.
* @note Experimental API
*/
TVG_API Tvg_Result tvg_text_set_font(Tvg_Paint* paint, const char* name, float size, const char* style);

View file

@ -341,6 +341,18 @@ LoadModule* LoaderMgr::loader(const char* key)
}
LoadModule* LoaderMgr::anyfont()
{
INLIST_FOREACH(_activeLoaders, loader) {
if ((loader->type == FileType::Ttf) && loader->pathcache) {
++loader->sharing;
return loader;
}
}
return nullptr;
}
LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const char* mimeType, const char* rpath, bool copy)
{
//Note that users could use the same data pointer with the different content.

View file

@ -34,6 +34,7 @@ struct LoaderMgr
static LoadModule* loader(const uint32_t* data, uint32_t w, uint32_t h, ColorSpace cs, bool copy);
static LoadModule* loader(const char* name, const char* data, uint32_t size, const char* mimeType, bool copy);
static LoadModule* loader(const char* key);
static LoadModule* anyfont();
static bool retrieve(const char* filename);
static bool retrieve(LoadModule* loader);
};

View file

@ -62,7 +62,7 @@ struct Text::Impl : Paint::Impl
Result font(const char* name, float size, const char* style)
{
auto loader = LoaderMgr::loader(name);
auto loader = name ? LoaderMgr::loader(name) : LoaderMgr::anyfont();
if (!loader) return Result::InsufficientCondition;
if (style && strstr(style, "italic")) italic = true;

View file

@ -108,6 +108,7 @@ TEST_CASE("Text Font", "[tvgText]")
REQUIRE(text->font("Arial", 80) == tvg::Result::Success);
REQUIRE(text->font("Arial", 1) == tvg::Result::Success);
REQUIRE(text->font("Arial", 50) == tvg::Result::Success);
REQUIRE(text->font(nullptr, 50) == tvg::Result::Success);
REQUIRE(text->font("InvalidFont", 80) == tvg::Result::InsufficientCondition);
Initializer::term();