| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
#ifndef SkPathOps_DEFINED |
| 8 |
#define SkPathOps_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
#include "include/private/base/SkTArray.h" |
| 13 |
#include "include/private/base/SkTDArray.h" |
| 14 |
|
| 15 |
#include <optional> |
| 16 |
|
| 17 |
struct SkRect; |
| 18 |
|
| 19 |
// FIXME: move everything below into the SkPath class |
| 20 |
/** |
| 21 |
* The logical operations that can be performed when combining two paths. |
| 22 |
*/ |
| 23 |
enum SkPathOp { |
| 24 |
kDifference_SkPathOp, //!< subtract the op path from the first path |
| 25 |
kIntersect_SkPathOp, //!< intersect the two paths |
| 26 |
kUnion_SkPathOp, //!< union (inclusive-or) the two paths |
| 27 |
kXOR_SkPathOp, //!< exclusive-or the two paths |
| 28 |
kReverseDifference_SkPathOp, //!< subtract the first path from the op path |
| 29 |
}; |
| 30 |
|
| 31 |
/** Set this path to the result of applying the Op to this path and the |
| 32 |
specified path: this = (this op operand). |
| 33 |
The resulting path will be constructed from non-overlapping contours. |
| 34 |
The curve order is reduced where possible so that cubics may be turned |
| 35 |
into quadratics, and quadratics maybe turned into lines. |
| 36 |
|
| 37 |
Returns true if operation was able to produce a result; |
| 38 |
otherwise, result is unmodified. |
| 39 |
|
| 40 |
@param one The first operand (for difference, the minuend) |
| 41 |
@param two The second operand (for difference, the subtrahend) |
| 42 |
@param op The operator to apply. |
| 43 |
@param result The product of the operands. The result may be one of the |
| 44 |
inputs. |
| 45 |
@return True if the operation succeeded. |
| 46 |
*/ |
| 47 |
std::optional<SkPath> SK_API Op(const SkPath& one, const SkPath& two, SkPathOp op); |
| 48 |
|
| 49 |
// DEPRECATED |
| 50 |
static inline bool Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result) { |
| 51 |
if (auto res = Op(one, two, op)) { |
| 52 |
*result = *res; |
| 53 |
return true; |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
/** Return a path with a set of non-overlapping contours that describe the |
| 59 |
same area as the original path. |
| 60 |
The curve order is reduced where possible so that cubics may |
| 61 |
be turned into quadratics, and quadratics maybe turned into lines. |
| 62 |
|
| 63 |
@param path The path to simplify. |
| 64 |
@return The simplified path, or {} on failure. |
| 65 |
*/ |
| 66 |
std::optional<SkPath> SK_API Simplify(const SkPath& path); |
| 67 |
|
| 68 |
// DEPRECATED |
| 69 |
static inline bool Simplify(const SkPath& path, SkPath* result) { |
| 70 |
if (auto res = Simplify(path)) { |
| 71 |
*result = *res; |
| 72 |
return true; |
| 73 |
} |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/** Set the resulting rectangle to the tight bounds of the path. |
| 78 |
|
| 79 |
@param path The path measured. |
| 80 |
@param result The tight bounds of the path. |
| 81 |
@return True if the bounds could be computed. |
| 82 |
*/ |
| 83 |
[[deprecated]] |
| 84 |
static inline bool TightBounds(const SkPath& path, SkRect* result) { |
| 85 |
auto rect = path.computeTightBounds(); |
| 86 |
if (rect.isFinite()) { |
| 87 |
*result = rect; |
| 88 |
return true; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** Returns a path with fill type winding to area equivalent to the input. |
| 94 |
Does not detect if path contains contours which |
| 95 |
contain self-crossings or cross other contours; in these cases, may return |
| 96 |
a result even though it does not fill same area as the input. |
| 97 |
|
| 98 |
If it fails to compute a result, returns {}. |
| 99 |
|
| 100 |
@param path The path typically with fill type set to even odd. |
| 101 |
*/ |
| 102 |
std::optional<SkPath> SK_API AsWinding(const SkPath& path); |
| 103 |
|
| 104 |
// DEPRECATED |
| 105 |
static inline bool AsWinding(const SkPath& path, SkPath* result) { |
| 106 |
if (auto res = AsWinding(path)) { |
| 107 |
*result = *res; |
| 108 |
return true; |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
/** Perform a series of path operations, optimized for unioning many paths together. |
| 114 |
*/ |
| 115 |
class SK_API SkOpBuilder { |
| 116 |
public: |
| 117 |
/** Add one or more paths and their operand. The builder is empty before the first |
| 118 |
path is added, so the result of a single add is (emptyPath OP path). |
| 119 |
|
| 120 |
@param path The second operand. |
| 121 |
@param _operator The operator to apply to the existing and supplied paths. |
| 122 |
*/ |
| 123 |
void add(const SkPath& path, SkPathOp _operator); |
| 124 |
|
| 125 |
/** Computes the sum of all paths and operands, and resets the builder to its |
| 126 |
initial state. |
| 127 |
|
| 128 |
@return result The product of the operands, {} on failure. |
| 129 |
*/ |
| 130 |
std::optional<SkPath> resolve(); |
| 131 |
|
| 132 |
// DEPRECATED |
| 133 |
bool resolve(SkPath* result) { |
| 134 |
if (auto res = this->resolve()) { |
| 135 |
*result = *res; |
| 136 |
return true; |
| 137 |
} |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
skia_private::TArray<SkPath> fPathRefs; |
| 143 |
SkTDArray<SkPathOp> fOps; |
| 144 |
|
| 145 |
static bool FixWinding(SkPath* path); |
| 146 |
static void ReversePath(SkPath* path); |
| 147 |
void reset(); |
| 148 |
}; |
| 149 |
|
| 150 |
#endif |