0.2.0-dev Download ZIP
239 lines ·6.5 KB Raw
| 1 | /* |
| 2 | * Copyright (c) 2020 - 2026 ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _TVG_FILL_H_ |
| 24 | #define _TVG_FILL_H_ |
| 25 | |
| 26 | #include "tvgCommon.h" |
| 27 | #include "tvgMath.h" |
| 28 | |
| 29 | #define LINEAR(A) static_cast<LinearGradientImpl*>(A) |
| 30 | #define CONST_LINEAR(A) static_cast<const LinearGradientImpl*>(A) |
| 31 | |
| 32 | #define RADIAL(A) static_cast<RadialGradientImpl*>(A) |
| 33 | #define CONST_RADIAL(A) static_cast<const RadialGradientImpl*>(A) |
| 34 | |
| 35 | #define CONIC(A) static_cast<ConicGradientImpl*>(A) |
| 36 | #define CONST_CONIC(A) static_cast<const ConicGradientImpl*>(A) |
| 37 | |
| 38 | struct Fill::Impl |
| 39 | { |
| 40 | ColorStop* colorStops = nullptr; |
| 41 | Matrix transform = tvg::identity(); |
| 42 | uint16_t cnt = 0; |
| 43 | FillSpread spread = FillSpread::Pad; |
| 44 | |
| 45 | virtual ~Impl() |
| 46 | { |
| 47 | tvg::free(colorStops); |
| 48 | } |
| 49 | |
| 50 | void copy(const Fill::Impl& dup) |
| 51 | { |
| 52 | cnt = dup.cnt; |
| 53 | spread = dup.spread; |
| 54 | colorStops = tvg::malloc<ColorStop>(sizeof(ColorStop) * dup.cnt); |
| 55 | if (dup.cnt > 0) memcpy(colorStops, dup.colorStops, sizeof(ColorStop) * dup.cnt); |
| 56 | transform = dup.transform; |
| 57 | } |
| 58 | |
| 59 | Result update(const ColorStop* colorStops, uint32_t cnt) |
| 60 | { |
| 61 | if ((!colorStops && cnt > 0) || (colorStops && cnt == 0)) return Result::InvalidArguments; |
| 62 | |
| 63 | if (cnt == 0) { |
| 64 | if (this->colorStops) { |
| 65 | tvg::free(this->colorStops); |
| 66 | this->colorStops = nullptr; |
| 67 | this->cnt = 0; |
| 68 | } |
| 69 | return Result::Success; |
| 70 | } |
| 71 | |
| 72 | if (cnt != this->cnt) { |
| 73 | this->colorStops = tvg::realloc<ColorStop>(this->colorStops, cnt * sizeof(ColorStop)); |
| 74 | } |
| 75 | |
| 76 | this->cnt = cnt; |
| 77 | memcpy(this->colorStops, colorStops, cnt * sizeof(ColorStop)); |
| 78 | |
| 79 | return Result::Success; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | struct RadialGradientImpl : RadialGradient |
| 85 | { |
| 86 | Fill::Impl impl; |
| 87 | Point center{}, focal{}; |
| 88 | float r = 0.0f, fr = 0.0f; |
| 89 | |
| 90 | RadialGradientImpl() |
| 91 | { |
| 92 | Fill::pImpl = &impl; |
| 93 | } |
| 94 | |
| 95 | Fill* duplicate() const |
| 96 | { |
| 97 | auto ret = RadialGradient::gen(); |
| 98 | RADIAL(ret)->impl.copy(this->impl); |
| 99 | RADIAL(ret)->center = center; |
| 100 | RADIAL(ret)->r = r; |
| 101 | RADIAL(ret)->focal = focal; |
| 102 | RADIAL(ret)->fr = fr; |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | Result radial(float cx, float cy, float r, float fx, float fy, float fr) |
| 108 | { |
| 109 | if (r < 0 || fr < 0) return Result::InvalidArguments; |
| 110 | |
| 111 | this->center = {cx, cy}; |
| 112 | this->r = r; |
| 113 | this->focal = {fx, fy}; |
| 114 | this->fr = fr; |
| 115 | |
| 116 | return Result::Success; |
| 117 | } |
| 118 | |
| 119 | Result radial(float* cx, float* cy, float* r, float* fx, float* fy, float* fr) const |
| 120 | { |
| 121 | if (cx) *cx = center.x; |
| 122 | if (cy) *cy = center.y; |
| 123 | if (r) *r = this->r; |
| 124 | if (fx) *fx = focal.x; |
| 125 | if (fy) *fy = focal.y; |
| 126 | if (fr) *fr = this->fr; |
| 127 | |
| 128 | return Result::Success; |
| 129 | } |
| 130 | |
| 131 | //TODO: remove this logic once SVG 2.0 is adopted by sw and wg engines (gl already supports it); lottie-specific handling will then be delegated entirely to the loader |
| 132 | //clamp focal point and shrink start circle if needed to avoid invalid gradient setup |
| 133 | bool correct(float& fx, float& fy, float& fr) const |
| 134 | { |
| 135 | constexpr float PRECISION = 0.02f; |
| 136 | if (r < PRECISION) return false; // too small, treated as solid fill |
| 137 | |
| 138 | auto dist = tvg::length(center, focal); |
| 139 | |
| 140 | // clamp focal point to inside end circle if outside |
| 141 | if (this->r - dist < PRECISION) { |
| 142 | auto diff = center - focal; |
| 143 | if (dist < PRECISION) dist = diff.x = PRECISION; |
| 144 | auto scale = this->r * (1.0f - PRECISION) / dist; |
| 145 | diff *= scale; |
| 146 | dist *= scale; // update effective dist after scaling |
| 147 | fx = center.x - diff.x; |
| 148 | fy = center.y - diff.y; |
| 149 | } else { |
| 150 | fx = focal.x; |
| 151 | fy = focal.y; |
| 152 | } |
| 153 | // ensure start circle radius fr doesn't exceed the difference |
| 154 | auto maxFr = (r - dist) * (1.0f - PRECISION); |
| 155 | fr = (this->fr > maxFr) ? std::max(0.0f, maxFr) : this->fr; |
| 156 | return true; |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | struct LinearGradientImpl : LinearGradient |
| 162 | { |
| 163 | Fill::Impl impl; |
| 164 | Point p1{}, p2{}; |
| 165 | |
| 166 | LinearGradientImpl() |
| 167 | { |
| 168 | Fill::pImpl = &impl; |
| 169 | } |
| 170 | |
| 171 | Fill* duplicate() const |
| 172 | { |
| 173 | auto ret = LinearGradient::gen(); |
| 174 | LINEAR(ret)->impl.copy(this->impl); |
| 175 | LINEAR(ret)->p1 = p1; |
| 176 | LINEAR(ret)->p2 = p2; |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | Result linear(float x1, float y1, float x2, float y2) noexcept |
| 182 | { |
| 183 | p1 = {x1, y1}; |
| 184 | p2 = {x2, y2}; |
| 185 | |
| 186 | return Result::Success; |
| 187 | } |
| 188 | |
| 189 | Result linear(float* x1, float* y1, float* x2, float* y2) const noexcept |
| 190 | { |
| 191 | if (x1) *x1 = p1.x; |
| 192 | if (x2) *x2 = p2.x; |
| 193 | if (y1) *y1 = p1.y; |
| 194 | if (y2) *y2 = p2.y; |
| 195 | |
| 196 | return Result::Success; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | struct ConicGradientImpl : ConicGradient |
| 201 | { |
| 202 | Fill::Impl impl; |
| 203 | Point center{}; |
| 204 | float angle = 0.0f; |
| 205 | |
| 206 | ConicGradientImpl() |
| 207 | { |
| 208 | Fill::pImpl = &impl; |
| 209 | } |
| 210 | |
| 211 | Fill* duplicate() const |
| 212 | { |
| 213 | auto ret = ConicGradient::gen(); |
| 214 | CONIC(ret)->impl.copy(this->impl); |
| 215 | CONIC(ret)->center = center; |
| 216 | CONIC(ret)->angle = angle; |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | Result conic(float cx, float cy, float angle) noexcept |
| 222 | { |
| 223 | this->center = {cx, cy}; |
| 224 | this->angle = angle; |
| 225 | |
| 226 | return Result::Success; |
| 227 | } |
| 228 | |
| 229 | Result conic(float* cx, float* cy, float* angle) const noexcept |
| 230 | { |
| 231 | if (cx) *cx = center.x; |
| 232 | if (cy) *cy = center.y; |
| 233 | if (angle) *angle = this->angle; |
| 234 | |
| 235 | return Result::Success; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | #endif //_TVG_FILL_H_ |