common shape: allow to reset stroke dash.

user may need to reset stroke dash to off,
now stroke api allows it.
This commit is contained in:
Hermet Park 2021-07-01 20:04:30 +09:00 committed by Hermet Park
parent 97302d2983
commit 2fd830c940
3 changed files with 24 additions and 17 deletions

View 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;

View 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;

View 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;