From 2fd830c940efd9c0217b3a1d3637e1f8212f46fc Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 1 Jul 2021 20:04:30 +0900 Subject: [PATCH] common shape: allow to reset stroke dash. user may need to reset stroke dash to off, now stroke api allows it. --- inc/thorvg.h | 5 +++-- src/lib/tvgShape.cpp | 4 +++- src/lib/tvgShapeImpl.h | 32 ++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 26df36d0..67833a0d 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -799,10 +799,11 @@ public: * * @retval Result::Success When succeed. * @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed. - * @retval Result::InvalidArguments In case a @c nullptr is passed as the @p dashPattern, - * the given length of the array is less than two or any of the @p dashPattern values is zero or less. + * @retval Result::InvalidArguments In case that either @p dashPattern is @c nullptr or @p cnt is zero. * * @note If any of the dash pattern values is zero, this function has no effect. + * @note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt. + * @warning @p cnt must be greater than 1 if the dash pattern is valid. */ Result stroke(const float* dashPattern, uint32_t cnt) noexcept; diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index 0af9ca1c..3e671953 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -358,7 +358,9 @@ const Fill* Shape::strokeFill() const noexcept Result Shape::stroke(const float* dashPattern, uint32_t cnt) noexcept { - if (cnt < 2 || !dashPattern) return Result::InvalidArguments; + if ((cnt == 1) || (!dashPattern && cnt > 0) || (dashPattern && cnt == 0)) { + return Result::InvalidArguments; + } for (uint32_t i = 0; i < cnt; i++) if (dashPattern[i] < FLT_EPSILON) return Result::InvalidArguments; diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index b978221a..53e9931f 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -330,22 +330,26 @@ struct Shape::Impl bool strokeDash(const float* pattern, uint32_t cnt) { - if (!stroke) stroke = new ShapeStroke(); - if (!stroke) return false; - - if (stroke->dashCnt != cnt) { - if (stroke->dashPattern) free(stroke->dashPattern); + //Reset dash + if (!pattern && cnt == 0) { + free(stroke->dashPattern); stroke->dashPattern = nullptr; + } else { + if (!stroke) stroke = new ShapeStroke(); + if (!stroke) return false; + + if (stroke->dashCnt != cnt) { + free(stroke->dashPattern); + stroke->dashPattern = nullptr; + } + if (!stroke->dashPattern) { + stroke->dashPattern = static_cast(malloc(sizeof(float) * cnt)); + if (!stroke->dashPattern) return false; + } + for (uint32_t i = 0; i < cnt; ++i) { + stroke->dashPattern[i] = pattern[i]; + } } - - if (!stroke->dashPattern) { - stroke->dashPattern = static_cast(malloc(sizeof(float) * cnt)); - if (!stroke->dashPattern) return false; - } - - for (uint32_t i = 0; i < cnt; ++i) - stroke->dashPattern[i] = pattern[i]; - stroke->dashCnt = cnt; flag |= RenderUpdateFlag::Stroke;