renderer/text: fix a missing text update

previously font size & italic style had been ignored
even if its attributes are changed.

Co-Authored-By: Hermet Park <hermet@lottiefiles.com>

issue: https://github.com/thorvg/thorvg/issues/2676
This commit is contained in:
Łukasz Pomietło 2024-08-27 12:28:17 +09:00 committed by Hermet Park
parent 732a2be7e8
commit c92cf4e139
4 changed files with 15 additions and 18 deletions

View file

@ -209,12 +209,12 @@ void TtfLoader::clear()
/************************************************************************/ /************************************************************************/
bool TtfLoader::resize(Paint* paint, float sx, TVG_UNUSED float sy) bool TtfLoader::transform(Paint* paint, float fontSize, bool italic)
{ {
if (!paint) return false; if (!paint) return false;
auto shift = 0.0f; auto shift = 0.0f;
auto dpi = 96.0f / 72.0f; //dpi base? auto dpi = 96.0f / 72.0f; //dpi base?
scale = sx * dpi / reader.metrics.unitsPerEm; scale = fontSize * dpi / reader.metrics.unitsPerEm;
if (italic) shift = -scale * 0.18f; //experimental decision. if (italic) shift = -scale * 0.18f; //experimental decision.
Matrix m = {scale, shift, -(shift * reader.metrics.minw), 0, scale, 0, 0, 0, 1}; Matrix m = {scale, shift, -(shift * reader.metrics.minw), 0, scale, 0, 0, 0, 1};
paint->transform(m); paint->transform(m);
@ -258,11 +258,10 @@ bool TtfLoader::open(const char* data, uint32_t size, TVG_UNUSED const string& r
} }
bool TtfLoader::request(Shape* shape, char* text, bool italic) bool TtfLoader::request(Shape* shape, char* text)
{ {
this->shape = shape; this->shape = shape;
this->text = text; this->text = text;
this->italic = italic;
return true; return true;
} }

View file

@ -36,7 +36,6 @@ struct TtfLoader : public FontLoader
TtfReader reader; TtfReader reader;
char* text = nullptr; char* text = nullptr;
Shape* shape = nullptr; Shape* shape = nullptr;
bool italic = false;
bool nomap = false; bool nomap = false;
bool freeData = false; bool freeData = false;
@ -46,8 +45,8 @@ struct TtfLoader : public FontLoader
using FontLoader::open; using FontLoader::open;
bool open(const string& path) override; bool open(const string& path) override;
bool open(const char *data, uint32_t size, const string& rpath, bool copy) override; bool open(const char *data, uint32_t size, const string& rpath, bool copy) override;
bool resize(Paint* paint, float w, float h) override; bool transform(Paint* paint, float fontSize, bool italic) override;
bool request(Shape* shape, char* text, bool italic = false) override; bool request(Shape* shape, char* text) override;
bool read() override; bool read() override;
void clear(); void clear();
}; };

View file

@ -101,7 +101,8 @@ struct FontLoader : LoadModule
FontLoader(FileType type) : LoadModule(type) {} FontLoader(FileType type) : LoadModule(type) {}
virtual bool request(Shape* shape, char* text, bool italic = false) = 0; virtual bool request(Shape* shape, char* text) = 0;
virtual bool transform(Paint* paint, float fontSize, bool italic) = 0;
}; };
#endif //_TVG_LOAD_MODULE_H_ #endif //_TVG_LOAD_MODULE_H_

View file

@ -26,12 +26,7 @@
#include <cstring> #include <cstring>
#include "tvgShape.h" #include "tvgShape.h"
#include "tvgFill.h" #include "tvgFill.h"
#include "tvgLoader.h"
#ifdef THORVG_TTF_LOADER_SUPPORT
#include "tvgTtfLoader.h"
#else
#include "tvgLoader.h"
#endif
struct Text::Impl struct Text::Impl
{ {
@ -69,6 +64,11 @@ struct Text::Impl
auto loader = LoaderMgr::loader(name); auto loader = LoaderMgr::loader(name);
if (!loader) return Result::InsufficientCondition; if (!loader) return Result::InsufficientCondition;
if (style && strstr(style, "italic")) italic = true;
else italic = false;
fontSize = size;
//Same resource has been loaded. //Same resource has been loaded.
if (this->loader == loader) { if (this->loader == loader) {
this->loader->sharing--; //make it sure the reference counting. this->loader->sharing--; //make it sure the reference counting.
@ -78,8 +78,6 @@ struct Text::Impl
} }
this->loader = static_cast<FontLoader*>(loader); this->loader = static_cast<FontLoader*>(loader);
fontSize = size;
if (style && strstr(style, "italic")) italic = true;
changed = true; changed = true;
return Result::Success; return Result::Success;
} }
@ -98,13 +96,13 @@ struct Text::Impl
{ {
if (!loader) return false; if (!loader) return false;
loader->request(shape, utf8, italic); loader->request(shape, utf8);
//reload //reload
if (changed) { if (changed) {
loader->read(); loader->read();
changed = false; changed = false;
} }
return loader->resize(shape, fontSize, fontSize); return loader->transform(shape, fontSize, italic);
} }
RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper) RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper)