| 1 | /* |
| 2 | * Copyright (c) 2020 - 2024 the 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 | #include "tvgMath.h" |
| 24 | #include "tvgPaint.h" |
| 25 | #include "tvgShape.h" |
| 26 | #include "tvgPicture.h" |
| 27 | #include "tvgScene.h" |
| 28 | #include "tvgText.h" |
| 29 | |
| 30 | /************************************************************************/ |
| 31 | /* Internal Class Implementation */ |
| 32 | /************************************************************************/ |
| 33 | |
| 34 | #define PAINT_METHOD(ret, METHOD) \ |
| 35 | switch (paint->type()) { \ |
| 36 | case Type::Shape: ret = P((Shape*)paint)->METHOD; break; \ |
| 37 | case Type::Scene: ret = P((Scene*)paint)->METHOD; break; \ |
| 38 | case Type::Picture: ret = P((Picture*)paint)->METHOD; break; \ |
| 39 | case Type::Text: ret = P((Text*)paint)->METHOD; break; \ |
| 40 | default: ret = {}; \ |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static bool _clipRect(RenderMethod* renderer, const Point* pts, const Matrix& m, RenderRegion& before) |
| 45 | { |
| 46 | Point c[4]; //corners |
| 47 | for (int i = 0; i < 4; ++i) { |
| 48 | c[i] = pts[i] * m; |
| 49 | } |
| 50 | |
| 51 | //figure out if the clipper is a superset of the current viewport(before) region |
| 52 | auto pointInConvexQuad = [](const Point& p, const Point* quad) { |
| 53 | auto sign = [](const Point& p1, const Point& p2, const Point& p3) { |
| 54 | return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); |
| 55 | }; |
| 56 | auto b1 = sign(p, quad[0], quad[1]) < 0.0f; |
| 57 | auto b2 = sign(p, quad[1], quad[2]) < 0.0f; |
| 58 | auto b3 = sign(p, quad[2], quad[3]) < 0.0f; |
| 59 | auto b4 = sign(p, quad[3], quad[0]) < 0.0f; |
| 60 | return ((b1 == b2) && (b2 == b3) && (b3 == b4)); |
| 61 | }; |
| 62 | |
| 63 | if (!pointInConvexQuad({float(before.x), float(before.y)}, c)) return false; |
| 64 | if (!pointInConvexQuad({float(before.x + before.w), float(before.y)}, c)) return false; |
| 65 | if (!pointInConvexQuad({float(before.x + before.w), float(before.y + before.h)}, c)) return false; |
| 66 | if (!pointInConvexQuad({float(before.x), float(before.y + before.h)}, c)) return false; |
| 67 | |
| 68 | //same viewport |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static bool _compFastTrack(RenderMethod* renderer, Paint* cmpTarget, const Matrix& pm, RenderRegion& before) |
| 74 | { |
| 75 | /* Access Shape class by Paint is bad... but it's ok still it's an internal usage. */ |
| 76 | auto shape = static_cast<Shape*>(cmpTarget); |
| 77 | |
| 78 | //Trimming likely makes the shape non-rectangular |
| 79 | if (P(shape)->rs.strokeTrim()) return false; |
| 80 | |
| 81 | //Rectangle Candidates? |
| 82 | const Point* pts; |
| 83 | auto ptsCnt = shape->pathCoords(&pts); |
| 84 | |
| 85 | //No rectangle format |
| 86 | if (ptsCnt != 4) return false; |
| 87 | |
| 88 | //No rotation and no skewing, still can try out clipping the rect region. |
| 89 | auto tm = pm * cmpTarget->transform(); |
| 90 | |
| 91 | //Perpendicular Rectangle? |
| 92 | if (rightAngle(tm) && !skewed(tm)) { |
| 93 | auto pt1 = pts + 0; |
| 94 | auto pt2 = pts + 1; |
| 95 | auto pt3 = pts + 2; |
| 96 | auto pt4 = pts + 3; |
| 97 | |
| 98 | if ((tvg::equal(pt1->x, pt2->x) && tvg::equal(pt2->y, pt3->y) && tvg::equal(pt3->x, pt4->x) && tvg::equal(pt1->y, pt4->y)) || |
| 99 | (tvg::equal(pt2->x, pt3->x) && tvg::equal(pt1->y, pt2->y) && tvg::equal(pt1->x, pt4->x) && tvg::equal(pt3->y, pt4->y))) { |
| 100 | |
| 101 | RenderRegion after; |
| 102 | |
| 103 | auto v1 = *pt1; |
| 104 | auto v2 = *pt3; |
| 105 | v1 *= tm; |
| 106 | v2 *= tm; |
| 107 | |
| 108 | //sorting |
| 109 | if (v1.x > v2.x) std::swap(v1.x, v2.x); |
| 110 | if (v1.y > v2.y) std::swap(v1.y, v2.y); |
| 111 | |
| 112 | after.x = static_cast<int32_t>(nearbyint(v1.x)); |
| 113 | after.y = static_cast<int32_t>(nearbyint(v1.y)); |
| 114 | after.w = static_cast<int32_t>(nearbyint(v2.x)) - after.x; |
| 115 | after.h = static_cast<int32_t>(nearbyint(v2.y)) - after.y; |
| 116 | |
| 117 | if (after.w < 0) after.w = 0; |
| 118 | if (after.h < 0) after.h = 0; |
| 119 | |
| 120 | after.intersect(before); |
| 121 | renderer->viewport(after); |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | return _clipRect(renderer, pts, tm, before); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | RenderRegion Paint::Impl::bounds(RenderMethod* renderer) const |
| 130 | { |
| 131 | RenderRegion ret; |
| 132 | PAINT_METHOD(ret, bounds(renderer)); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | Iterator* Paint::Impl::iterator() |
| 138 | { |
| 139 | Iterator* ret; |
| 140 | PAINT_METHOD(ret, iterator()); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | Paint* Paint::Impl::duplicate(Paint* ret) |
| 146 | { |
| 147 | if (ret) ret->composite(nullptr, CompositeMethod::None); |
| 148 | |
| 149 | PAINT_METHOD(ret, duplicate(ret)); |
| 150 | |
| 151 | //duplicate Transform |
| 152 | ret->pImpl->tr = tr; |
| 153 | ret->pImpl->renderFlag |= RenderUpdateFlag::Transform; |
| 154 | |
| 155 | ret->pImpl->opacity = opacity; |
| 156 | |
| 157 | if (compData) ret->pImpl->composite(ret, compData->target->duplicate(), compData->method); |
| 158 | if (clipper) ret->pImpl->clip(clipper->duplicate()); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | bool Paint::Impl::rotate(float degree) |
| 165 | { |
| 166 | if (tr.overriding) return false; |
| 167 | if (tvg::equal(degree, tr.degree)) return true; |
| 168 | tr.degree = degree; |
| 169 | renderFlag |= RenderUpdateFlag::Transform; |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | bool Paint::Impl::scale(float factor) |
| 176 | { |
| 177 | if (tr.overriding) return false; |
| 178 | if (tvg::equal(factor, tr.scale)) return true; |
| 179 | tr.scale = factor; |
| 180 | renderFlag |= RenderUpdateFlag::Transform; |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | bool Paint::Impl::translate(float x, float y) |
| 187 | { |
| 188 | if (tr.overriding) return false; |
| 189 | if (tvg::equal(x, tr.m.e13) && tvg::equal(y, tr.m.e23)) return true; |
| 190 | tr.m.e13 = x; |
| 191 | tr.m.e23 = y; |
| 192 | renderFlag |= RenderUpdateFlag::Transform; |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | bool Paint::Impl::render(RenderMethod* renderer) |
| 199 | { |
| 200 | if (opacity == 0) return true; |
| 201 | |
| 202 | RenderCompositor* cmp = nullptr; |
| 203 | |
| 204 | if (compData && !(compData->target->pImpl->ctxFlag & ContextFlag::FastTrack)) { |
| 205 | RenderRegion region; |
| 206 | PAINT_METHOD(region, bounds(renderer)); |
| 207 | |
| 208 | auto cData = compData; |
| 209 | while (cData) { |
| 210 | if (MASK_REGION_MERGING(cData->method)) region.add(P(cData->target)->bounds(renderer)); |
| 211 | if (region.w == 0 || region.h == 0) return true; |
| 212 | cData = P(cData->target)->compData; |
| 213 | } |
| 214 | cmp = renderer->target(region, COMPOSITE_TO_COLORSPACE(renderer, compData->method), CompositionFlag::Masking); |
| 215 | if (renderer->beginComposite(cmp, CompositeMethod::None, 255)) { |
| 216 | compData->target->pImpl->render(renderer); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (cmp) renderer->beginComposite(cmp, compData->method, compData->target->pImpl->opacity); |
| 221 | |
| 222 | bool ret; |
| 223 | PAINT_METHOD(ret, render(renderer)); |
| 224 | |
| 225 | if (cmp) renderer->endComposite(cmp); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | RenderData Paint::Impl::update(RenderMethod* renderer, const Matrix& pm, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper) |
| 232 | { |
| 233 | if (this->renderer != renderer) { |
| 234 | if (this->renderer) TVGERR("RENDERER", "paint's renderer has been changed!"); |
| 235 | renderer->ref(); |
| 236 | this->renderer = renderer; |
| 237 | } |
| 238 | |
| 239 | if (renderFlag & RenderUpdateFlag::Transform) tr.update(); |
| 240 | |
| 241 | /* 1. Composition Pre Processing */ |
| 242 | RenderData trd = nullptr; //composite target render data |
| 243 | RenderRegion viewport; |
| 244 | auto compFastTrack = false; |
| 245 | |
| 246 | if (compData) { |
| 247 | auto target = compData->target; |
| 248 | auto method = compData->method; |
| 249 | P(target)->ctxFlag &= ~ContextFlag::FastTrack; //reset |
| 250 | |
| 251 | /* If the transformation has no rotational factors and the Alpha(InvAlpha)Masking involves a simple rectangle, |
| 252 | we can optimize by using the viewport instead of the regular AlphaMasking sequence for improved performance. */ |
| 253 | if (target->type() == Type::Shape) { |
| 254 | auto shape = static_cast<Shape*>(target); |
| 255 | uint8_t a; |
| 256 | shape->fillColor(nullptr, nullptr, nullptr, &a); |
| 257 | //no gradient fill & no compositions of the composition target. |
| 258 | if (!shape->fill() && !(PP(shape)->compData)) { |
| 259 | if ((method == CompositeMethod::AlphaMask && a == 255 && PP(shape)->opacity == 255) || (method == CompositeMethod::InvAlphaMask && (a == 0 || PP(shape)->opacity == 0))) { |
| 260 | viewport = renderer->viewport(); |
| 261 | if ((compFastTrack = _compFastTrack(renderer, target, pm, viewport))) { |
| 262 | P(target)->ctxFlag |= ContextFlag::FastTrack; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | if (!compFastTrack) { |
| 268 | trd = P(target)->update(renderer, pm, clips, 255, pFlag, false); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* 2. Clipping */ |
| 273 | if (this->clipper) { |
| 274 | auto pclip = P(this->clipper); |
| 275 | if (pclip->renderFlag | static_cast<Shape*>(this->clipper)->pImpl->rFlag) renderFlag |= RenderUpdateFlag::Clip; |
| 276 | pclip->ctxFlag &= ~ContextFlag::FastTrack; //reset |
| 277 | viewport = renderer->viewport(); |
| 278 | /* TODO: Intersect the clipper's clipper, if both are FastTrack. |
| 279 | Update the subsequent clipper first and check its ctxFlag. */ |
| 280 | if (!pclip->clipper && static_cast<Shape*>(this->clipper)->strokeWidth() == 0.0f && _compFastTrack(renderer, this->clipper, pm, viewport)) { |
| 281 | pclip->ctxFlag |= ContextFlag::FastTrack; |
| 282 | compFastTrack = true; |
| 283 | } else { |
| 284 | trd = pclip->update(renderer, pm, clips, 255, pFlag, true); |
| 285 | clips.push(trd); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* 3. Main Update */ |
| 290 | auto newFlag = static_cast<RenderUpdateFlag>(pFlag | renderFlag); |
| 291 | renderFlag = RenderUpdateFlag::None; |
| 292 | opacity = MULTIPLY(opacity, this->opacity); |
| 293 | |
| 294 | RenderData rd = nullptr; |
| 295 | |
| 296 | tr.cm = pm * tr.m; |
| 297 | PAINT_METHOD(rd, update(renderer, tr.cm, clips, opacity, newFlag, clipper)); |
| 298 | |
| 299 | /* 4. Composition Post Processing */ |
| 300 | if (compFastTrack) renderer->viewport(viewport); |
| 301 | else if (this->clipper) clips.pop(); |
| 302 | |
| 303 | return rd; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | bool Paint::Impl::bounds(float* x, float* y, float* w, float* h, bool transformed, bool stroking, bool origin) |
| 308 | { |
| 309 | bool ret; |
| 310 | const auto& m = this->transform(origin); |
| 311 | |
| 312 | //Case: No transformed, quick return! |
| 313 | if (!transformed || identity(&m)) { |
| 314 | PAINT_METHOD(ret, bounds(x, y, w, h, stroking)); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | //Case: Transformed |
| 319 | auto tx = 0.0f; |
| 320 | auto ty = 0.0f; |
| 321 | auto tw = 0.0f; |
| 322 | auto th = 0.0f; |
| 323 | |
| 324 | PAINT_METHOD(ret, bounds(&tx, &ty, &tw, &th, stroking)); |
| 325 | |
| 326 | //Get vertices |
| 327 | Point pt[4] = {{tx, ty}, {tx + tw, ty}, {tx + tw, ty + th}, {tx, ty + th}}; |
| 328 | |
| 329 | //New bounding box |
| 330 | auto x1 = FLT_MAX; |
| 331 | auto y1 = FLT_MAX; |
| 332 | auto x2 = -FLT_MAX; |
| 333 | auto y2 = -FLT_MAX; |
| 334 | |
| 335 | //Compute the AABB after transformation |
| 336 | for (int i = 0; i < 4; i++) { |
| 337 | pt[i] *= m; |
| 338 | |
| 339 | if (pt[i].x < x1) x1 = pt[i].x; |
| 340 | if (pt[i].x > x2) x2 = pt[i].x; |
| 341 | if (pt[i].y < y1) y1 = pt[i].y; |
| 342 | if (pt[i].y > y2) y2 = pt[i].y; |
| 343 | } |
| 344 | |
| 345 | if (x) *x = x1; |
| 346 | if (y) *y = y1; |
| 347 | if (w) *w = x2 - x1; |
| 348 | if (h) *h = y2 - y1; |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | void Paint::Impl::reset() |
| 355 | { |
| 356 | if (clipper) { |
| 357 | delete(clipper); |
| 358 | clipper = nullptr; |
| 359 | } |
| 360 | |
| 361 | if (compData) { |
| 362 | if (P(compData->target)->unref() == 0) delete(compData->target); |
| 363 | free(compData); |
| 364 | compData = nullptr; |
| 365 | } |
| 366 | |
| 367 | tvg::identity(&tr.m); |
| 368 | tr.degree = 0.0f; |
| 369 | tr.scale = 1.0f; |
| 370 | tr.overriding = false; |
| 371 | |
| 372 | blendMethod = BlendMethod::Normal; |
| 373 | renderFlag = RenderUpdateFlag::None; |
| 374 | ctxFlag = ContextFlag::Default; |
| 375 | opacity = 255; |
| 376 | paint->id = 0; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /************************************************************************/ |
| 381 | /* External Class Implementation */ |
| 382 | /************************************************************************/ |
| 383 | |
| 384 | Paint :: Paint() : pImpl(new Impl(this)) |
| 385 | { |
| 386 | } |
| 387 | |
| 388 | |
| 389 | Paint :: ~Paint() |
| 390 | { |
| 391 | delete(pImpl); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | Result Paint::rotate(float degree) noexcept |
| 396 | { |
| 397 | if (pImpl->rotate(degree)) return Result::Success; |
| 398 | return Result::InsufficientCondition; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | Result Paint::scale(float factor) noexcept |
| 403 | { |
| 404 | if (pImpl->scale(factor)) return Result::Success; |
| 405 | return Result::InsufficientCondition; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | Result Paint::translate(float x, float y) noexcept |
| 410 | { |
| 411 | if (pImpl->translate(x, y)) return Result::Success; |
| 412 | return Result::InsufficientCondition; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | Result Paint::transform(const Matrix& m) noexcept |
| 417 | { |
| 418 | if (pImpl->transform(m)) return Result::Success; |
| 419 | return Result::InsufficientCondition; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | Matrix Paint::transform() noexcept |
| 424 | { |
| 425 | return pImpl->transform(); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | TVG_DEPRECATED Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept |
| 430 | { |
| 431 | return this->bounds(x, y, w, h, false); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | Result Paint::bounds(float* x, float* y, float* w, float* h, bool transformed) const noexcept |
| 436 | { |
| 437 | if (pImpl->bounds(x, y, w, h, transformed, true, transformed)) return Result::Success; |
| 438 | return Result::InsufficientCondition; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | Paint* Paint::duplicate() const noexcept |
| 443 | { |
| 444 | return pImpl->duplicate(); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | Result Paint::clip(std::unique_ptr<Paint> clipper) noexcept |
| 449 | { |
| 450 | auto p = clipper.release(); |
| 451 | |
| 452 | if (p && p->type() != Type::Shape) { |
| 453 | TVGERR("RENDERER", "Clipping only supports the Shape!"); |
| 454 | return Result::NonSupport; |
| 455 | } |
| 456 | pImpl->clip(p); |
| 457 | return Result::Success; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept |
| 462 | { |
| 463 | //TODO: remove. Keep this for the backward compatibility |
| 464 | if (target && method == CompositeMethod::ClipPath) return clip(std::move(target)); |
| 465 | |
| 466 | auto p = target.release(); |
| 467 | if (pImpl->composite(this, p, method)) return Result::Success; |
| 468 | |
| 469 | delete(p); |
| 470 | return Result::InvalidArguments; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | CompositeMethod Paint::composite(const Paint** target) const noexcept |
| 475 | { |
| 476 | if (pImpl->compData) { |
| 477 | if (target) *target = pImpl->compData->target; |
| 478 | return pImpl->compData->method; |
| 479 | } else { |
| 480 | //TODO: remove. Keep this for the backward compatibility |
| 481 | if (pImpl->clipper) { |
| 482 | if (target) *target = pImpl->clipper; |
| 483 | return CompositeMethod::ClipPath; |
| 484 | } |
| 485 | if (target) *target = nullptr; |
| 486 | return CompositeMethod::None; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | |
| 491 | Result Paint::opacity(uint8_t o) noexcept |
| 492 | { |
| 493 | if (pImpl->opacity == o) return Result::Success; |
| 494 | |
| 495 | pImpl->opacity = o; |
| 496 | pImpl->renderFlag |= RenderUpdateFlag::Color; |
| 497 | |
| 498 | return Result::Success; |
| 499 | } |
| 500 | |
| 501 | |
| 502 | uint8_t Paint::opacity() const noexcept |
| 503 | { |
| 504 | return pImpl->opacity; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | TVG_DEPRECATED uint32_t Paint::identifier() const noexcept |
| 509 | { |
| 510 | return (uint32_t) type(); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | Result Paint::blend(BlendMethod method) noexcept |
| 515 | { |
| 516 | //TODO: Remove later |
| 517 | if (method == BlendMethod::Hue || method == BlendMethod::Saturation || method == BlendMethod::Color || method == BlendMethod::Luminosity || method == BlendMethod::HardMix) return Result::NonSupport; |
| 518 | |
| 519 | if (pImpl->blendMethod != method) { |
| 520 | pImpl->blendMethod = method; |
| 521 | pImpl->renderFlag |= RenderUpdateFlag::Blend; |
| 522 | } |
| 523 | |
| 524 | return Result::Success; |
| 525 | } |