Skip to content

Commit

Permalink
[SVG] Added support for <set> and <animateColor>
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-manias committed Apr 15, 2024
1 parent 52523b3 commit 1c579a6
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/xml/modules/classes/vector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
<param type="LONG" name="Transform">Set to TRUE if all transforms applicable to the vector should be applied to the path.</param>
</input>
<description>
<p>Any vector that generates a path can be traced by calling this method. Tracing allows the caller to follow the path for each pixel that would be drawn if the path were to be rendered with a stroke size of 1. The prototype of the callback function is <code>ERR Function(OBJECTPTR Vector, LONG Index, LONG Command, DOUBLE X, DOUBLE Y, APTR Meta)</code>.</p>
<p>Any vector that generates a path can be traced by calling this method. Tracing allows the caller to follow the path from point-to-point if the path were to be rendered with a stroke. The prototype of the callback function is <code>ERR Function(OBJECTPTR Vector, LONG Index, LONG Command, DOUBLE X, DOUBLE Y, APTR Meta)</code>.</p>
<p>The Vector parameter refers to the vector targeted by the method. The Index is an incrementing counter that reflects the currently plotted point. The X and Y parameters reflect the coordinate of a point on the path.</p>
<p>If the Callback returns <code>ERR::Terminate</code>, then no further coordinates will be processed.</p>
</description>
Expand Down
2 changes: 1 addition & 1 deletion docs/xml/modules/vector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ Z: Close Path
<param type="DOUBLE" name="Scale">Set to 1.0 (recommended) to trace the path at a scale of 1 to 1.</param>
</input>
<description>
<p>Generated paths can be traced by calling this function. Tracing allows the caller to follow the path for each pixel that would be drawn if the path were to be rendered with a stroke size of 1. The prototype of the callback function is <code>ERR Function(APTR Path, LONG Index, LONG Command, DOUBLE X, DOUBLE Y, APTR Meta)</code>.</p>
<p>Any vector that generates a path can be traced by calling this method. Tracing allows the caller to follow the path from point-to-point if the path were to be rendered with a stroke. The prototype of the callback function is <code>ERR Function(OBJECTPTR Vector, LONG Index, LONG Command, DOUBLE X, DOUBLE Y, APTR Meta)</code>.</p>
<p>The Index is an incrementing counter that reflects the currently plotted point. The X and Y parameters reflect the coordinate of a point on the path.</p>
<p>If the Callback returns <code>ERR::Terminate</code>, then no further coordinates will be processed.</p>
</description>
Expand Down
1 change: 1 addition & 0 deletions include/parasol/modules/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,7 @@ inline void SET_VECTOR_COLOUR(objVectorColour *Colour, DOUBLE Red, DOUBLE Green,
#define SVF_SCALE 0x1057f68d
#define SVF_SCREEN 0x1b5ffd45
#define SVF_SEED 0x7c9dda26
#define SVF_SET 0x0b88a991
#define SVF_SHAPE_RENDERING 0xeecea7a1
#define SVF_SOFTLIGHT 0x78b6e7b9
#define SVF_SOURCEALPHA 0xbe4b853c
Expand Down
22 changes: 15 additions & 7 deletions src/svg/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ double anim_base::get_numeric_value(objVector &Vector, FIELD Field)
}
}

//********************************************************************************************************************
// Suitable for <set> instructions only. Very straight-forward as there is no interpolation.

std::string anim_base::get_string()
{
if (not to.empty()) return to;
else return "";
}

//********************************************************************************************************************
// Return an interpolated value based on the values or from/to/by settings.

Expand Down Expand Up @@ -650,13 +659,6 @@ static ERR set_anim_property(extSVG *Self, anim_base &Anim, objVector *Vector, X
return ERR::Okay;
}

//********************************************************************************************************************

void anim_colour::perform()
{

}

//********************************************************************************************************************
// Rotation angles are pre-calculated once.

Expand Down Expand Up @@ -945,6 +947,12 @@ void anim_value::perform()
vector->set(FID_Height, val);
break;
}

case SVF_VISIBILITY: {
auto val = get_string();
vector->set(FID_Visibility, val);
break;
}
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/svg/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class anim_base {
double get_total_dist();
double get_dimension(objVector &, FIELD);
double get_numeric_value(objVector &, FIELD);
std::string get_string();
FRGB get_colour_value(objVector &, FIELD);
bool started(double);
void next_frame(double);
Expand Down Expand Up @@ -181,14 +182,6 @@ class anim_motion : public anim_base {

//********************************************************************************************************************

class anim_colour : public anim_base {
public:
anim_colour(OBJECTID pTarget) : anim_base(pTarget) { }
void perform();
};

//********************************************************************************************************************

class anim_value : public anim_base {
public:
anim_value(OBJECTID pTarget) : anim_base(pTarget) { }
Expand Down
34 changes: 32 additions & 2 deletions src/svg/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,7 @@ static ERR process_shape(extSVG *Self, CLASSID VectorID, svgState &State, XMLTag
case SVF_ANIMATETRANSFORM: xtag_animate_transform(Self, child, vector); break;
case SVF_ANIMATEMOTION: xtag_animate_motion(Self, child, vector); break;
case SVF_PARASOL_MORPH: xtag_morph(Self, child, vector); break;
case SVF_SET: xtag_set(Self, child, vector); break;
case SVF_TEXTPATH:
if (VectorID IS ID_VECTORTEXT) {
if (!child.Children.empty()) {
Expand Down Expand Up @@ -1623,6 +1624,7 @@ static ERR xtag_default(extSVG *Self, svgState &State, XMLTag &Tag, OBJECTPTR Pa
case SVF_ANIMATECOLOR: xtag_animate_colour(Self, Tag, Parent); break;
case SVF_ANIMATETRANSFORM: xtag_animate_transform(Self, Tag, Parent); break;
case SVF_ANIMATEMOTION: xtag_animate_motion(Self, Tag, Parent); break;
case SVF_SET: xtag_set(Self, Tag, Parent); break;
case SVF_FILTER: xtag_filter(Self, State, Tag); break;
case SVF_DEFS: xtag_defs(Self, State, Tag, Parent); break;
case SVF_CLIPPATH: xtag_clippath(Self, Tag); break;
Expand Down Expand Up @@ -2665,13 +2667,41 @@ static ERR xtag_animate(extSVG *Self, XMLTag &Tag, OBJECTPTR Parent)
}

//********************************************************************************************************************
// <set> is largely equivalent to <animate> but does not interpolate values.

static ERR xtag_set(extSVG *Self, XMLTag &Tag, OBJECTPTR Parent)
{
Self->Animated = true;

auto &new_anim = Self->Animations.emplace_front(anim_value { Parent->UID });
auto &anim = std::get<anim_value>(new_anim);

for (LONG a=1; a < std::ssize(Tag.Attribs); a++) {
auto &value = Tag.Attribs[a].Value;
if (value.empty()) continue;

auto hash = StrHash(Tag.Attribs[a].Name);
switch (hash) {
default:
set_anim_property(Self, anim, (objVector *)Parent, Tag, hash, value);
break;
}
}

if (!anim.is_valid()) Self->Animations.erase_after(Self->Animations.before_begin());
return ERR::Okay;
}

//********************************************************************************************************************
// The <animateColour> tag is considered deprecated because its functionality can be represented entirely by the
// existing <animate> tag.

static ERR xtag_animate_colour(extSVG *Self, XMLTag &Tag, OBJECTPTR Parent)
{
Self->Animated = true;

auto &new_anim = Self->Animations.emplace_front(anim_colour { Parent->UID });
auto &anim = std::get<anim_colour>(new_anim);
auto &new_anim = Self->Animations.emplace_front(anim_value { Parent->UID });
auto &anim = std::get<anim_value>(new_anim);

for (LONG a=1; a < std::ssize(Tag.Attribs); a++) {
auto &value = Tag.Attribs[a].Value;
Expand Down
3 changes: 2 additions & 1 deletion src/svg/svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class extSVG : public objSVG {
STRING Folder;
std::string Colour = "rgb(0,0,0)"; // Default colour, used for 'currentColor' references
OBJECTPTR Viewport; // First viewport (the <svg> tag) to be created on parsing the SVG document.
std::forward_list<std::variant<anim_transform, anim_motion, anim_colour, anim_value>> Animations;
std::forward_list<std::variant<anim_transform, anim_motion, anim_value>> Animations;
std::vector<std::unique_ptr<svgLink>> Links;
std::vector<svgInherit> Inherit;
TIMER AnimationTimer;
Expand Down Expand Up @@ -136,6 +136,7 @@ static void xtag_group(extSVG *, svgState &, XMLTag &, OBJECTPTR, objVector * &)
static ERR xtag_image(extSVG *, svgState &, XMLTag &, OBJECTPTR, objVector * &);
static void xtag_link(extSVG *, svgState &, XMLTag &, OBJECTPTR, objVector * &);
static void xtag_morph(extSVG *, XMLTag &, OBJECTPTR);
static ERR xtag_set(extSVG *, XMLTag &, OBJECTPTR);
static void xtag_svg(extSVG *, svgState &, XMLTag &, OBJECTPTR, objVector * &);
static void xtag_use(extSVG *, svgState &, XMLTag &, OBJECTPTR);
static ERR xtag_style(extSVG *, XMLTag &);
Expand Down
49 changes: 49 additions & 0 deletions src/svg/tests/animation/w3-animate-elem-21-t.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions src/svg/tests/animation/w3-animate-elem-22-b.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions src/svg/tests/animation/w3-animate-elem-23-t.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/vector/vector.fdl
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ inline void SET_VECTOR_COLOUR(objVectorColour *Colour, DOUBLE Red, DOUBLE Green,
"SCALE",
"SCREEN",
"SEED",
"SET",
"SHAPE-RENDERING",
"SOFTLIGHT",
"SOURCEALPHA",
Expand Down

0 comments on commit 1c579a6

Please sign in to comment.