From e0aa007659f824d2a2ccd1a6f885d908af873bea Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Mon, 11 Oct 2021 15:20:03 +0200 Subject: [PATCH] common: new api for a grad transformation The new apii allows to transform the gradient fill. --- inc/thorvg.h | 28 ++++++++++++++++++++++++++-- src/lib/tvgFill.cpp | 16 ++++++++++++++++ src/lib/tvgFill.h | 7 ++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 502d16dc..69bd051b 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -392,10 +392,23 @@ public: */ Result spread(FillSpread s) noexcept; + /** + * @brief Sets the matrix of the affine transformation for the gradient fill. + * + * The augmented matrix of the transformation is expected to be given. + * + * @param[in] m The 3x3 augmented matrix. + * + * @return Result::Success when succeed, Result::FailedAllocation otherwise. + * + * @BETA_API + */ + Result transform(const Matrix& m) noexcept; + /** * @brief Gets the parameters of the colors of the gradient, their position and number. * - * @param[in] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored. + * @param[out] colorStops A pointer to the memory location, where the array of the gradient's ColorStop is stored. * * @return The number of colors used in the gradient. This value corresponds to the length of the @p colorStops array. */ @@ -408,6 +421,17 @@ public: */ FillSpread spread() const noexcept; + /** + * @brief Gets the matrix of the affine transformation of the gradient fill. + * + * In case no transformation was applied, the identity matrix is returned. + * + * @retval The augmented transformation matrix. + * + * @BETA_API + */ + Matrix transform() const noexcept; + /** * @brief Creates a copy of the Fill object. * @@ -1375,7 +1399,7 @@ public: * It's useful when you need to save the composed scene or image from a paint object and recreate it later. * * The file format is decided by the extension name(i.e. "*.tvg") while the supported formats depend on the TVG packaging environment. - * If it doesn't support the file format, the save() method returns the @c NonSuppport result. + * If it doesn't support the file format, the save() method returns the @c Result::NonSuppport result. * * Once you export a paint to the file successfully, you can recreate it using the Picture class. * diff --git a/src/lib/tvgFill.cpp b/src/lib/tvgFill.cpp index 757f78b9..b610a775 100644 --- a/src/lib/tvgFill.cpp +++ b/src/lib/tvgFill.cpp @@ -87,6 +87,22 @@ FillSpread Fill::spread() const noexcept } +Result Fill::transform(const Matrix& m) noexcept +{ + if (!pImpl->transform) pImpl->transform = new Matrix(); + if (!pImpl->transform) return Result::FailedAllocation; + *pImpl->transform = m; + return Result::Success; +} + + +Matrix Fill::transform() const noexcept +{ + if (pImpl->transform) return *pImpl->transform; + return {1, 0, 0, 0, 1, 0, 0, 0, 1}; +} + + Fill* Fill::duplicate() const noexcept { return pImpl->duplicate(); diff --git a/src/lib/tvgFill.h b/src/lib/tvgFill.h index f9eaddff..16a0097f 100644 --- a/src/lib/tvgFill.h +++ b/src/lib/tvgFill.h @@ -50,6 +50,7 @@ struct FillDup : DuplicateMethod struct Fill::Impl { ColorStop* colorStops = nullptr; + Matrix* transform = nullptr; uint32_t cnt = 0; FillSpread spread; DuplicateMethod* dup = nullptr; @@ -58,6 +59,7 @@ struct Fill::Impl { if (dup) delete(dup); if (colorStops) free(colorStops); + if (transform) delete(transform); } void method(DuplicateMethod* dup) @@ -74,7 +76,10 @@ struct Fill::Impl ret->pImpl->spread = spread; ret->pImpl->colorStops = static_cast(malloc(sizeof(ColorStop) * cnt)); memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt); - + if (transform) { + ret->pImpl->transform = new Matrix; + if (ret->pImpl->transform) *ret->pImpl->transform = *transform; + } return ret; } };