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 c75311d239
commit 71d3fce1dd
5 changed files with 23 additions and 51 deletions

View file

@ -1110,7 +1110,6 @@ public:
* @param[out] b The blue color channel value in the range [0 ~ 255]. * @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. * @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; Result fillColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept;
@ -1486,8 +1485,6 @@ public:
* @param[in] g The green color channel value in the range [0 ~ 255]. The default value is 0. * @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. * @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() * @see Text::font()
* *
* @note Experimental API * @note Experimental API
@ -1501,8 +1498,6 @@ public:
* *
* @param[in] f The unique pointer to the gradient fill. * @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 Either a solid color or a gradient fill is applied, depending on what was set as last.
* @note Experimental API * @note Experimental API
* *

View file

@ -2239,7 +2239,6 @@ TVG_API Tvg_Result tvg_text_set_text(Tvg_Paint* paint, const char* text);
* *
* \return Tvg_Result enumeration. * \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument. * \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 Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API * \note Experimental API
@ -2258,7 +2257,6 @@ TVG_API Tvg_Result tvg_text_set_fill_color(Tvg_Paint* paint, uint8_t r, uint8_t
* \return Tvg_Result enumeration. * \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument. * \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_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 Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API * \note Experimental API
@ -2277,7 +2275,6 @@ TVG_API Tvg_Result tvg_text_set_linear_gradient(Tvg_Paint* paint, Tvg_Gradient*
* \return Tvg_Result enumeration. * \return Tvg_Result enumeration.
* \retval TVG_RESULT_INVALID_ARGUMENT A \c nullptr passed as the \p paint argument. * \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_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 Either a solid color or a gradient fill is applied, depending on what was set as last.
* \note Experimental API * \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 Result Text::fill(uint8_t r, uint8_t g, uint8_t b) noexcept
{ {
if (!pImpl->paint) return Result::InsufficientCondition; return pImpl->shape->fill(r, g, b);
return pImpl->fill(r, g, b);
} }
Result Text::fill(unique_ptr<Fill> f) noexcept Result Text::fill(unique_ptr<Fill> f) noexcept
{ {
if (!pImpl->paint) return Result::InsufficientCondition; return pImpl->shape->fill(std::move(f));
auto p = f.release();
if (!p) return Result::MemoryCorruption;
return pImpl->fill(p);
} }

View file

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