From f5337015e971d24379d2ee664895503ab8945e13 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 20 Aug 2024 16:41:06 +0900 Subject: [PATCH] renderer: text refactoring - assign the shape instance as mandatory. - assign the text instance internally. --- inc/thorvg.h | 5 ---- src/bindings/capi/thorvg_capi.h | 3 --- src/renderer/tvgText.cpp | 13 +++------ src/renderer/tvgText.h | 47 ++++++++++++--------------------- test/capi/capiText.cpp | 6 ++--- 5 files changed, 23 insertions(+), 51 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 0a81f4b9..4303092a 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1063,7 +1063,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; @@ -1421,8 +1420,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 @@ -1436,8 +1433,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 * diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 128f4054..50402d16 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -2201,7 +2201,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 @@ -2220,7 +2219,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 @@ -2239,7 +2237,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 diff --git a/src/renderer/tvgText.cpp b/src/renderer/tvgText.cpp index 4b5eb35c..d54c7878 100644 --- a/src/renderer/tvgText.cpp +++ b/src/renderer/tvgText.cpp @@ -35,7 +35,7 @@ /************************************************************************/ -Text::Text() : pImpl(new Impl) +Text::Text() : pImpl(new Impl(this)) { Paint::pImpl->id = TVG_CLASS_ID_TEXT; } @@ -95,20 +95,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 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)); } diff --git a/src/renderer/tvgText.h b/src/renderer/tvgText.h index 7bfb5d45..fb0f51dc 100644 --- a/src/renderer/tvgText.h +++ b/src/renderer/tvgText.h @@ -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(f)); + delete(shape); } Result text(const char* utf8) @@ -83,8 +78,6 @@ struct Text::Impl } this->loader = static_cast(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& 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->identifier() == TVG_CLASS_ID_LINEAR) { P(static_cast(fill))->x1 *= scale; @@ -142,13 +129,13 @@ struct Text::Impl P(static_cast(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(paint->duplicate()); + dup->shape = static_cast(shape->duplicate()); if (loader) { dup->loader = loader; diff --git a/test/capi/capiText.cpp b/test/capi/capiText.cpp index 8a781611..8c9f2aa4 100644 --- a/test/capi/capiText.cpp +++ b/test/capi/capiText.cpp @@ -131,7 +131,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); @@ -156,8 +156,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);