common shape: allow to reset stroke dash.
authorHermet Park <chuneon.park@samsung.com>
Thu, 1 Jul 2021 11:04:30 +0000 (20:04 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Wed, 7 Jul 2021 02:57:02 +0000 (11:57 +0900)
user may need to reset stroke dash to off,
now stroke api allows it.

inc/thorvg.h
src/lib/tvgShape.cpp
src/lib/tvgShapeImpl.h

index f5cb0a702c0be8fe9b0c1ad1c3b49b1ced92188e..80a960c7ed5a75443856845e457dad9bad89759d 100644 (file)
@@ -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;
 
index 0af9ca1c72e1d45db43dd2a4562ac2d19a224de9..3e6719531bff4558928cc747aff5038c46e4547c 100644 (file)
@@ -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;
index b978221a9516df7f31167778851d120992bf50ee..53e9931ffd8e88e9a829bd153e1a885269d5e4fb 100644 (file)
@@ -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<float*>(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<float*>(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;