renderer: text refactoring

- assign the shape instance as mandatory.
- assign the text instance internally.
This commit is contained in:
Hermet Park 2024-08-20 16:41:06 +09:00
parent 0a59e02a21
commit fac8ba3d9f
5 changed files with 23 additions and 51 deletions

View file

@ -1113,7 +1113,6 @@ public:
* @param[out] b The blue color channel value in the range [0 ~ 255].
* @param[out] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque.
*
* @return Result::Success when succeed.
*/
Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept;
@ -1492,8 +1491,6 @@ public:
* @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0.
* @param[in] b The blue color channel value in the range [0 ~ 255]. The default value is 0.
*
* @retval Result::InsufficientCondition when the font has not been set up prior to this operation.
*
* @see Text::font()
*
* @note Experimental API
@ -1507,8 +1504,6 @@ public:
*
* @param[in] f The unique pointer to the gradient fill.
*
* @retval Result::InsufficientCondition when the font has not been set up prior to this operation.
*
* @note Either a solid color or a gradient fill is applied, depending on what was set as last.
* @note Experimental API
*

View file

@ -2233,7 +2233,6 @@ TVG_API Tvg_Result tvg_text_set_text(Tvg_Paint* paint, const char* text);
*
* \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument.
* \retval TVG_RESULT_INSUFFICIENT_CONDITION The font has not been set up prior to this operation.
*
* \note Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API
@ -2252,7 +2251,6 @@ TVG_API Tvg_Result tvg_text_set_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t
* \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument.
* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer.
* \retval TVG_RESULT_INSUFFICIENT_CONDITION The font has not been set up prior to this operation.
*
* \note Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API
@ -2271,7 +2269,6 @@ TVG_API Tvg_Result tvg_text_set_linear_gradient(Tvg_Paint* paint, Tvg_Gradient*
* \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument.
* \retval TVG_RESULT_MEMORY_CORRUPTION An invalid Tvg_Gradient pointer.
* \retval TVG_RESULT_INSUFFICIENT_CONDITION The font has not been set up prior to this operation.
*
* \note Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API

View file

@ -35,7 +35,7 @@
/************************************************************************/
Text::Text() : pImpl(new Impl)
Text::Text() : pImpl(new Impl(this))
{
}
@ -94,20 +94,13 @@ Result Text::unload(const std::string& path) noexcept
Result Text::fill(uint8_t r, uint8_t g, uint8_t b) noexcept
{
if (!pImpl->paint) return Result::InsufficientCondition;
return pImpl->fill(r, g, b);
return pImpl->shape->fill(r, g, b);
}
Result Text::fill(unique_ptr<Fill> f) noexcept
{
if (!pImpl->paint) return Result::InsufficientCondition;
auto p = f.release();
if (!p) return Result::MemoryCorruption;
return pImpl->fill(p);
return pImpl->shape->fill(std::move(f));
}

View file

@ -36,27 +36,22 @@
struct Text::Impl
{
FontLoader* loader = nullptr;
Shape* paint = nullptr;
Text* paint;
Shape* shape;
char* utf8 = nullptr;
float fontSize;
bool italic = false;
bool changed = false;
Impl(Text* p) : paint(p), shape(Shape::gen().release())
{
}
~Impl()
{
free(utf8);
LoaderMgr::retrieve(loader);
delete(paint);
}
Result fill(uint8_t r, uint8_t g, uint8_t b)
{
return paint->fill(r, g, b);
}
Result fill(Fill* f)
{
return paint->fill(cast<Fill>(f));
delete(shape);
}
Result text(const char* utf8)
@ -83,8 +78,6 @@ struct Text::Impl
}
this->loader = static_cast<FontLoader*>(loader);
if (!paint) paint = Shape::gen().release();
fontSize = size;
if (style && strstr(style, "italic")) italic = true;
changed = true;
@ -93,14 +86,12 @@ struct Text::Impl
RenderRegion bounds(RenderMethod* renderer)
{
if (paint) return P(paint)->bounds(renderer);
else return {0, 0, 0, 0};
return P(shape)->bounds(renderer);
}
bool render(RenderMethod* renderer)
{
if (paint) return PP(paint)->render(renderer);
return true;
return PP(shape)->render(renderer);
}
bool load()
@ -109,15 +100,11 @@ struct Text::Impl
//reload
if (changed) {
loader->request(paint, utf8, italic);
loader->request(shape, utf8, italic);
loader->read();
changed = false;
}
if (paint) {
loader->resize(paint, fontSize, fontSize);
return true;
}
return false;
return loader->resize(shape, fontSize, fontSize);
}
RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, TVG_UNUSED bool clipper)
@ -125,8 +112,8 @@ struct Text::Impl
if (!load()) return nullptr;
//transform the gradient coordinates based on the final scaled font.
auto fill = P(paint)->rs.fill;
if (fill && P(paint)->flag & RenderUpdateFlag::Gradient) {
auto fill = P(shape)->rs.fill;
if (fill && P(shape)->flag & RenderUpdateFlag::Gradient) {
auto scale = 1.0f / loader->scale;
if (fill->type() == Type::LinearGradient) {
P(static_cast<LinearGradient*>(fill))->x1 *= scale;
@ -142,13 +129,13 @@ struct Text::Impl
P(static_cast<RadialGradient*>(fill))->fr *= scale;
}
}
return PP(paint)->update(renderer, transform, clips, opacity, pFlag, false);
return PP(shape)->update(renderer, transform, clips, opacity, pFlag, false);
}
bool bounds(float* x, float* y, float* w, float* h, TVG_UNUSED bool stroking)
{
if (!load() || !paint) return false;
PP(paint)->bounds(x, y, w, h, true, true, false);
if (!load()) return false;
PP(shape)->bounds(x, y, w, h, true, true, false);
return true;
}
@ -160,7 +147,7 @@ struct Text::Impl
auto text = Text::gen().release();
auto dup = text->pImpl;
if (paint) dup->paint = static_cast<Shape*>(paint->duplicate());
dup->shape = static_cast<Shape*>(shape->duplicate());
if (loader) {
dup->loader = loader;

View file

@ -128,7 +128,7 @@ TEST_CASE("Set solid text fill", "[capiText]")
REQUIRE(tvg_font_load(TEST_DIR"/Arial.ttf") == TVG_RESULT_SUCCESS);
REQUIRE(tvg_text_set_fill_color(text, 10, 20, 30) == TVG_RESULT_INSUFFICIENT_CONDITION);
REQUIRE(tvg_text_set_fill_color(text, 10, 20, 30) == TVG_RESULT_SUCCESS);
REQUIRE(tvg_text_set_font(text, "Arial", 10.0f, "") == TVG_RESULT_SUCCESS);
@ -153,8 +153,8 @@ TEST_CASE("Set gradient text fill", "[capiText]")
REQUIRE(tvg_font_load(TEST_DIR"/Arial.ttf") == TVG_RESULT_SUCCESS);
REQUIRE(tvg_text_set_linear_gradient(text, NULL) == TVG_RESULT_INSUFFICIENT_CONDITION);
REQUIRE(tvg_text_set_radial_gradient(text, NULL) == TVG_RESULT_INSUFFICIENT_CONDITION);
REQUIRE(tvg_text_set_linear_gradient(text, NULL) == TVG_RESULT_MEMORY_CORRUPTION);
REQUIRE(tvg_text_set_radial_gradient(text, NULL) == TVG_RESULT_MEMORY_CORRUPTION);
REQUIRE(tvg_text_set_font(text, "Arial", 10.0f, "") == TVG_RESULT_SUCCESS);