diff --git a/src/veg/gradient.cpp b/src/veg/gradient.cpp index 99226b1..7fb6a5d 100644 --- a/src/veg/gradient.cpp +++ b/src/veg/gradient.cpp @@ -58,7 +58,7 @@ gradient::~gradient() linear_gradient::linear_gradient(const r4::vector2& p0, const r4::vector2& p1) #if VEG_BACKEND == VEG_BACKEND_AGG : - gradient(this->linear_pad, this->linear_reflect, this->linear_repeat) + spread_wrappers({&this->linear_pad, &this->linear_reflect, &this->linear_repeat}) #endif { #if VEG_BACKEND == VEG_BACKEND_CAIRO @@ -72,6 +72,9 @@ linear_gradient::linear_gradient(const r4::vector2& p0, const r4::vector2< throw std::runtime_error("cairo_pattern_create_linear() failed"); } #elif VEG_BACKEND == VEG_BACKEND_AGG + // set the initial spread method + this->set_spread_method(gradient_spread_method::pad); + auto v = p1 - p0; auto len = v.norm(); @@ -93,7 +96,7 @@ linear_gradient::linear_gradient(const r4::vector2& p0, const r4::vector2< radial_gradient::radial_gradient(const r4::vector2& f, const r4::vector2& c, real r) #if VEG_BACKEND == VEG_BACKEND_AGG : - gradient(this->radial_pad, this->radial_reflect, this->radial_repeat) + spread_wrappers({&this->radial_pad, &this->radial_reflect, &this->radial_repeat}) #endif { #if VEG_BACKEND == VEG_BACKEND_CAIRO @@ -109,6 +112,9 @@ radial_gradient::radial_gradient(const r4::vector2& f, const r4::vector2set_spread_method(gradient_spread_method::pad); + // gradient parameters are stretched so that the gradient radius becomes // color_lut_size, this is for optimal color picking from the color_lut real lut_r = decltype(gradient::lut)::color_lut_size; @@ -156,19 +162,7 @@ void gradient::set_spread_method(gradient_spread_method spread_method) SL ); #elif VEG_BACKEND == VEG_BACKEND_AGG - switch (spread_method) { - default: - utki::assert(false, SL); - case gradient_spread_method::pad: - this->cur_grad = &this->pad; - break; - case gradient_spread_method::reflect: - this->cur_grad = &this->reflect; - break; - case gradient_spread_method::repeat: - this->cur_grad = &this->repeat; - break; - } + this->cur_grad = &this->get_spread_method_wrapper(spread_method); #endif } diff --git a/src/veg/gradient.hpp b/src/veg/gradient.hpp index d03be8c..6b43513 100644 --- a/src/veg/gradient.hpp +++ b/src/veg/gradient.hpp @@ -37,6 +37,8 @@ SOFTWARE. #elif VEG_BACKEND == VEG_BACKEND_AGG # include # include + +# include #endif namespace veg { @@ -59,7 +61,9 @@ enum class gradient_spread_method { /** * @brief Repeat gradients stops in direct order. */ - repeat + repeat, + + enum_size }; /** @@ -111,11 +115,14 @@ class gradient } }; - const gradient_wrapper_base& pad; - const gradient_wrapper_base& reflect; - const gradient_wrapper_base& repeat; + // The concrete gradient wrappers are members of the derived classes, so + // derived classes must set this in their constructor bodies and provide + // the wrappers via get_spread_method_wrapper(). + gradient_wrapper_base* cur_grad = nullptr; + + virtual gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) = 0; - const gradient_wrapper_base* cur_grad; + gradient() = default; const gradient_wrapper_base& get_agg_gradient() const { @@ -128,16 +135,6 @@ class gradient r4::matrix2 local_matrix; - gradient( - const gradient_wrapper_base& pad, - const gradient_wrapper_base& reflect, - const gradient_wrapper_base& repeat - ) : - pad(pad), - reflect(reflect), - repeat(repeat), - cur_grad(&this->pad) - {} #endif public: @@ -180,6 +177,13 @@ class linear_gradient : public gradient gradient_wrapper linear_pad; spread_gradient_wrapper linear_reflect; spread_gradient_wrapper linear_repeat; + + utki::enum_array spread_wrappers; + + gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) override + { + return *this->spread_wrappers[spread_method]; + } #endif public: @@ -203,6 +207,13 @@ class radial_gradient : public gradient gradient_wrapper radial_pad; spread_gradient_wrapper radial_reflect; spread_gradient_wrapper radial_repeat; + + utki::enum_array spread_wrappers; + + gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) override + { + return *this->spread_wrappers[spread_method]; + } #endif public: