Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: stroke dash offset support added #1607

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions inc/thorvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,11 @@ class TVG_API Shape final : public Paint
* @param[in] r The red 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] a The alpha channel value in the range [0 ~ 255], where 0 is completely transparent and 255 is opaque. The default value is 0.
* @param[in] 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::FailedAllocation otherwise.
*/
Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hermet our policy is that we can remove the def value from api just like that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two options:

  1. bring a new api name about stroke dash
  2. change the existing conflict api.

You know, you already suggested this change. I have no any objection of this.

Abou policy? Well... you know most apis have no default values but if it's useful for users then why not?

Result stroke(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;

/**
* @brief Sets the gradient fill of the stroke for all of the figures from the path.
Expand All @@ -1002,6 +1002,7 @@ class TVG_API Shape final : public Paint
*
* @param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
* @param[in] cnt The length of the @p dashPattern array.
* @param[in] offset The shift of the starting point within the repeating dash pattern from which the path's dashing begins.
*
* @retval Result::Success When succeed.
* @retval Result::FailedAllocation An internal error with a memory allocation for an object to be dashed.
Expand All @@ -1010,7 +1011,7 @@ class TVG_API Shape final : public Paint
* @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;
Result stroke(const float* dashPattern, uint32_t cnt, float offset = 0.0f) noexcept;

/**
* @brief Sets the cap style of the stroke in the open sub-paths.
Expand Down Expand Up @@ -1157,7 +1158,7 @@ class TVG_API Shape final : public Paint
*
* @return Result::Success when succeed, Result::InsufficientCondition otherwise.
*/
Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a = nullptr) const noexcept;
Result strokeColor(uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) const noexcept;

/**
* @brief Gets the pointer to the gradient fill of the stroke.
Expand All @@ -1170,10 +1171,11 @@ class TVG_API Shape final : public Paint
* @brief Gets the dash pattern of the stroke.
*
* @param[out] dashPattern The pointer to the memory, where the dash pattern array is stored.
* @param[out] offset The shift of the starting point within the repeating dash pattern.
*
* @return The length of the @p dashPattern array.
*/
uint32_t strokeDash(const float** dashPattern) const noexcept;
uint32_t strokeDash(const float** dashPattern, float* offset = nullptr) const noexcept;

/**
* @brief Gets the cap style used for stroking the path.
Expand Down
17 changes: 6 additions & 11 deletions src/bindings/capi/thorvg_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1316,18 +1316,12 @@ TVG_API Tvg_Result tvg_shape_get_stroke_gradient(const Tvg_Paint* paint, Tvg_Gra


/*!
* \brief Sets the shape's stroke dash pattern.
*
* \code
* //dash pattern examples
* float dashPattern[2] = {20, 10}; // -- -- --
* float dashPattern[2] = {40, 20}; // ---- ---- ----
* float dashPattern[4] = {10, 20, 30, 40} // - --- - ---
* \endcode
* \brief Sets the shape's stroke dash pattern. (BETA_API)
*
* \param[in] paint A Tvg_Paint pointer to the shape object.
* \param[in] dashPattern The array of consecutive pair values of the dash length and the gap length.
* \param[in] cnt The size of the @p dashPattern array.
* \param[in] offset The shift of the starting point within the repeating dash pattern from which the path's dashing begins.
*
* \return Tvg_Result enumeration.
* \retval TVG_RESULT_SUCCESS Succeed.
Expand All @@ -1336,23 +1330,24 @@ TVG_API Tvg_Result tvg_shape_get_stroke_gradient(const Tvg_Paint* paint, Tvg_Gra
*
* \note To reset the stroke dash pattern, pass @c nullptr to @p dashPattern and zero to @p cnt.
*/
TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt);
TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt, float offset);


/*!
* \brief Gets the dash pattern of the stroke.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hermet I added BETA_API - not sure whether it's necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think it's ready to official release then you don't need to add BETA_API tag. Otherwise you should.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now this breaks the api compatibility.

* \brief Gets the dash pattern of the stroke. (BETA_API)
*
* The function does not allocate any memory.
*
* \param[in] paint A Tvg_Paint pointer to the shape object.
* \param[out] dashPattern The array of consecutive pair values of the dash length and the gap length.
* \param[out] cnt The size of the @p dashPattern array.
* \param[out] offset The shift of the starting point within the repeating dash pattern.
*
* \return Tvg_Result enumeration.
* \retval TVG_RESULT_SUCCESS Succeed.
* \retval TVG_RESULT_INVALID_ARGUMENT An invalid pointer passed as an argument.
*/
TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt);
TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt, float* offset);


/*!
Expand Down
10 changes: 5 additions & 5 deletions src/bindings/capi/tvgCapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,17 @@ TVG_API Tvg_Result tvg_shape_get_stroke_gradient(const Tvg_Paint* paint, Tvg_Gra
}


TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt)
TVG_API Tvg_Result tvg_shape_set_stroke_dash(Tvg_Paint* paint, const float* dashPattern, uint32_t cnt, float offset)
{
if (!paint) return TVG_RESULT_INVALID_ARGUMENT;
return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(dashPattern, cnt);
return (Tvg_Result) reinterpret_cast<Shape*>(paint)->stroke(dashPattern, cnt, offset);
}


TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt)
TVG_API Tvg_Result tvg_shape_get_stroke_dash(const Tvg_Paint* paint, const float** dashPattern, uint32_t* cnt, float* offset)
{
if (!paint || !cnt || !dashPattern) return TVG_RESULT_INVALID_ARGUMENT;
*cnt = reinterpret_cast<const Shape*>(paint)->strokeDash(dashPattern);
if (!paint || !cnt || !dashPattern || !offset) return TVG_RESULT_INVALID_ARGUMENT;
*cnt = reinterpret_cast<const Shape*>(paint)->strokeDash(dashPattern, offset);
return TVG_RESULT_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/examples/AnimateMasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
pMaskShape = maskShape.get();
maskShape->appendCircle(180, 180, 75, 75);
maskShape->fill(125, 125, 125);
maskShape->stroke(25, 25, 25);
maskShape->stroke(25, 25, 25, 255);
maskShape->stroke(tvg::StrokeJoin::Round);
maskShape->stroke(10);
canvas->push(std::move(maskShape));
Expand Down
18 changes: 9 additions & 9 deletions src/examples/Arc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,60 +33,60 @@ void tvgDrawCmds(tvg::Canvas* canvas)
//Arc Line
auto shape1 = tvg::Shape::gen();
shape1->appendArc(150, 150, 80, 10, 180, false);
shape1->stroke(255, 255, 255);
shape1->stroke(255, 255, 255, 255);
shape1->stroke(2);
if (canvas->push(std::move(shape1)) != tvg::Result::Success) return;

auto shape2 = tvg::Shape::gen();
shape2->appendArc(400, 150, 80, 0, 300, false);
shape2->stroke(255, 255, 255);
shape2->stroke(255, 255, 255, 255);
shape2->stroke(2);
if (canvas->push(std::move(shape2)) != tvg::Result::Success) return;

auto shape3 = tvg::Shape::gen();
shape3->appendArc(600, 150, 80, 300, 60, false);
shape3->stroke(255, 255, 255);
shape3->stroke(255, 255, 255, 255);
shape3->stroke(2);
if (canvas->push(std::move(shape3)) != tvg::Result::Success) return;

//Pie Line
auto shape4 = tvg::Shape::gen();
shape4->appendArc(150, 400, 80, 10, 180, true);
shape4->stroke(255, 255, 255);
shape4->stroke(255, 255, 255, 255);
shape4->stroke(2);
if (canvas->push(std::move(shape4)) != tvg::Result::Success) return;

auto shape5 = tvg::Shape::gen();
shape5->appendArc(400, 400, 80, 0, 300, true);
shape5->stroke(255, 255, 255);
shape5->stroke(255, 255, 255, 255);
shape5->stroke(2);
if (canvas->push(std::move(shape5)) != tvg::Result::Success) return;

auto shape6 = tvg::Shape::gen();
shape6->appendArc(600, 400, 80, 300, 60, true);
shape6->stroke(255, 255, 255);
shape6->stroke(255, 255, 255, 255);
shape6->stroke(2);
if (canvas->push(std::move(shape6)) != tvg::Result::Success) return;

//Pie Fill
auto shape7 = tvg::Shape::gen();
shape7->appendArc(150, 650, 80, 10, 180, true);
shape7->fill(255, 255, 255);
shape7->stroke(255, 0, 0);
shape7->stroke(255, 0, 0, 255);
shape7->stroke(2);
if (canvas->push(std::move(shape7)) != tvg::Result::Success) return;

auto shape8 = tvg::Shape::gen();
shape8->appendArc(400, 650, 80, 0, 300, true);
shape8->fill(255, 255, 255);
shape8->stroke(255, 0, 0);
shape8->stroke(255, 0, 0, 255);
shape8->stroke(2);
if (canvas->push(std::move(shape8)) != tvg::Result::Success) return;

auto shape9 = tvg::Shape::gen();
shape9->appendArc(600, 650, 80, 300, 60, true);
shape9->fill(255, 255, 255);
shape9->stroke(255, 0, 0);
shape9->stroke(255, 0, 0, 255);
shape9->stroke(2);
if (canvas->push(std::move(shape9)) != tvg::Result::Success) return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void testCapi()

//Prapare a dash for the stroke
float dashPattern[4] = {15.0f, 30.0f, 2.0f, 30.0f};
tvg_shape_set_stroke_dash(scene_shape2, dashPattern, 4);
tvg_shape_set_stroke_dash(scene_shape2, dashPattern, 4, 0.0f);
tvg_shape_set_stroke_cap(scene_shape2, TVG_STROKE_CAP_ROUND);
tvg_shape_set_stroke_color(scene_shape2, 0, 0, 255, 255);
tvg_shape_set_stroke_width(scene_shape2, 15.0f);
Expand Down
6 changes: 3 additions & 3 deletions src/examples/ClipPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
auto star1 = tvg::Shape::gen();
tvgDrawStar(star1.get());
star1->fill(255, 255, 0);
star1->stroke(255 ,0, 0);
star1->stroke(255 ,0, 0, 255);
star1->stroke(10);

//Move Star1
Expand All @@ -72,7 +72,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
auto star2 = tvg::Shape::gen();
tvgDrawStar(star2.get());
star2->fill(0, 255, 255);
star2->stroke(0 ,255, 0);
star2->stroke(0 ,255, 0, 255);
star2->stroke(10);
star2->opacity(100);

Expand Down Expand Up @@ -105,7 +105,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
fill->colorStops(colorStops, 2);
star3->fill(std::move(fill));

star3->stroke(255 ,0, 0);
star3->stroke(255 ,0, 0, 255);
star3->stroke(10);
star3->translate(400, 0);

Expand Down
2 changes: 1 addition & 1 deletion src/examples/CustomTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
shape->close();
shape->fill(0, 0, 255);
shape->stroke(3);
shape->stroke(255, 255, 255);
shape->stroke(255, 255, 255, 255);

//Transform Matrix
tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
Expand Down
4 changes: 2 additions & 2 deletions src/examples/DirectUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)

//fill property will be retained
shape->fill(127, 255, 255);
shape->stroke(0, 0, 255);
shape->stroke(0, 0, 255, 255);
shape->stroke(1);

if (canvas->push(std::move(shape)) != tvg::Result::Success) return;
Expand All @@ -68,7 +68,7 @@ void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
if (pShape->reset() == tvg::Result::Success) {
pShape->appendRect(-100 + (800 * progress), -100 + (800 * progress), 200, 200, (100 * progress), (100 * progress));
pShape->fill(127, 255, 255);
pShape->stroke(0, 0, 255);
pShape->stroke(0, 0, 255, 255);
pShape->stroke(30 * progress);

//Update shape for drawing (this may work asynchronously)
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Duplicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape1->appendRect(220, 10, 100, 100);

shape1->stroke(3);
shape1->stroke(0, 255, 0);
shape1->stroke(0, 255, 0, 255);

float dashPattern[2] = {4, 4};
shape1->stroke(dashPattern, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/examples/GradientStroke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape1->lineTo(100, 150);
shape1->close();

shape1->stroke(0, 255, 0);
shape1->stroke(0, 255, 0, 255);
shape1->stroke(20);
shape1->stroke(tvg::StrokeJoin::Miter);
shape1->stroke(tvg::StrokeCap::Butt);
Expand Down
2 changes: 1 addition & 1 deletion src/examples/InvLumaMasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
star->lineTo(546, 143);
star->close();
star->stroke(10);
star->stroke(255, 255, 255);
star->stroke(255, 255, 255, 255);

//Mask3
auto mask3 = tvg::Shape::gen();
Expand Down
2 changes: 1 addition & 1 deletion src/examples/InvMasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
star->lineTo(546, 143);
star->close();
star->stroke(10);
star->stroke(255, 255, 255);
star->stroke(255, 255, 255, 255);

//Mask3
auto mask3 = tvg::Shape::gen();
Expand Down
2 changes: 1 addition & 1 deletion src/examples/LumaMasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
star->lineTo(546, 143);
star->close();
star->stroke(10);
star->stroke(255, 255, 255);
star->stroke(255, 255, 255, 255);

//Mask3
auto mask3 = tvg::Shape::gen();
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Masking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
star->close();
star->stroke(30);
star->stroke(tvg::StrokeJoin::Miter);
star->stroke(255, 255, 255);
star->stroke(255, 255, 255, 255);

//Mask3
auto mask3 = tvg::Shape::gen();
Expand Down
6 changes: 3 additions & 3 deletions src/examples/Opacity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape2->appendRect(450, 100, 200, 200, 50, 50);
shape2->fill(0, 255, 0);
shape2->stroke(10);
shape2->stroke(255, 255, 255);
shape2->stroke(255, 255, 255, 255);
scene->push(std::move(shape2));


Expand Down Expand Up @@ -74,7 +74,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape3->close();
shape3->fill(0, 0, 255);
shape3->stroke(10);
shape3->stroke(255, 255, 255);
shape3->stroke(255, 255, 255, 255);
shape3->opacity(127);

scene2->push(std::move(shape3));
Expand All @@ -96,7 +96,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape4->close();
shape4->fill(255, 0, 0);
shape4->stroke(10);
shape4->stroke(0, 0, 255);
shape4->stroke(0, 0, 255, 255);
shape4->opacity(200);
shape4->scale(3);
scene2->push(std::move(shape4));
Expand Down
4 changes: 2 additions & 2 deletions src/examples/Retaining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape4->appendCircle(400, 400, 100, 100);
shape4->fill(255, 0, 0);
shape4->stroke(5);
shape4->stroke(255, 255, 255);
shape4->stroke(255, 255, 255, 255);
scene->push(std::move(shape4));

auto shape5 = tvg::Shape::gen();
shape5->appendCircle(550, 550, 150, 150);
shape5->fill(255, 0, 255);
shape5->stroke(5);
shape5->stroke(255, 255, 255);
shape5->stroke(255, 255, 255, 255);
scene->push(std::move(shape5));

if (canvas->push(std::move(scene)) != tvg::Result::Success) return;
Expand Down
6 changes: 3 additions & 3 deletions src/examples/SceneBlending.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape2->appendRect(450, 100, 200, 200, 50, 50);
shape2->fill(0, 255, 0);
shape2->stroke(10);
shape2->stroke(255, 255, 255);
shape2->stroke(255, 255, 255, 255);
scene->push(std::move(shape2));

//Draw the Scene onto the Canvas
Expand Down Expand Up @@ -80,7 +80,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape3->close();
shape3->fill(0, 0, 255);
shape3->stroke(10);
shape3->stroke(255, 255, 255);
shape3->stroke(255, 255, 255, 255);
shape3->opacity(127);

scene2->push(std::move(shape3));
Expand All @@ -102,7 +102,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
shape4->close();
shape4->fill(255, 0, 0);
shape4->stroke(10);
shape4->stroke(0, 0, 255);
shape4->stroke(0, 0, 255, 255);
shape4->opacity(200);
shape4->scale(3);
scene2->push(std::move(shape4));
Expand Down
Loading