| 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/SkTypes.h" | 
|---|
| 11 | #include "include/private/SkTArray.h" | 
|---|
| 12 | #include "include/private/SkTDArray.h" | 
|---|
| 13 |  | 
|---|
| 14 | class SkPath; | 
|---|
| 15 | struct SkRect; | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | // FIXME: move everything below into the SkPath class | 
|---|
| 19 | /** | 
|---|
| 20 | *  The logical operations that can be performed when combining two paths. | 
|---|
| 21 | */ | 
|---|
| 22 | enum SkPathOp { | 
|---|
| 23 | kDifference_SkPathOp,         //!< subtract the op path from the first path | 
|---|
| 24 | kIntersect_SkPathOp,          //!< intersect the two paths | 
|---|
| 25 | kUnion_SkPathOp,              //!< union (inclusive-or) the two paths | 
|---|
| 26 | kXOR_SkPathOp,                //!< exclusive-or the two paths | 
|---|
| 27 | kReverseDifference_SkPathOp,  //!< subtract the first path from the op path | 
|---|
| 28 | }; | 
|---|
| 29 |  | 
|---|
| 30 | /** Set this path to the result of applying the Op to this path and the | 
|---|
| 31 | specified path: this = (this op operand). | 
|---|
| 32 | The resulting path will be constructed from non-overlapping contours. | 
|---|
| 33 | The curve order is reduced where possible so that cubics may be turned | 
|---|
| 34 | into quadratics, and quadratics maybe turned into lines. | 
|---|
| 35 |  | 
|---|
| 36 | Returns true if operation was able to produce a result; | 
|---|
| 37 | otherwise, result is unmodified. | 
|---|
| 38 |  | 
|---|
| 39 | @param one The first operand (for difference, the minuend) | 
|---|
| 40 | @param two The second operand (for difference, the subtrahend) | 
|---|
| 41 | @param op The operator to apply. | 
|---|
| 42 | @param result The product of the operands. The result may be one of the | 
|---|
| 43 | inputs. | 
|---|
| 44 | @return True if the operation succeeded. | 
|---|
| 45 | */ | 
|---|
| 46 | bool SK_API Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result); | 
|---|
| 47 |  | 
|---|
| 48 | /** Set this path to a set of non-overlapping contours that describe the | 
|---|
| 49 | same area as the original path. | 
|---|
| 50 | The curve order is reduced where possible so that cubics may | 
|---|
| 51 | be turned into quadratics, and quadratics maybe turned into lines. | 
|---|
| 52 |  | 
|---|
| 53 | Returns true if operation was able to produce a result; | 
|---|
| 54 | otherwise, result is unmodified. | 
|---|
| 55 |  | 
|---|
| 56 | @param path The path to simplify. | 
|---|
| 57 | @param result The simplified path. The result may be the input. | 
|---|
| 58 | @return True if simplification succeeded. | 
|---|
| 59 | */ | 
|---|
| 60 | bool SK_API Simplify(const SkPath& path, SkPath* result); | 
|---|
| 61 |  | 
|---|
| 62 | /** Set the resulting rectangle to the tight bounds of the path. | 
|---|
| 63 |  | 
|---|
| 64 | @param path The path measured. | 
|---|
| 65 | @param result The tight bounds of the path. | 
|---|
| 66 | @return True if the bounds could be computed. | 
|---|
| 67 | */ | 
|---|
| 68 | bool SK_API TightBounds(const SkPath& path, SkRect* result); | 
|---|
| 69 |  | 
|---|
| 70 | /** Set the result with fill type winding to area equivalent to path. | 
|---|
| 71 | Returns true if successful. Does not detect if path contains contours which | 
|---|
| 72 | contain self-crossings or cross other contours; in these cases, may return | 
|---|
| 73 | true even though result does not fill same area as path. | 
|---|
| 74 |  | 
|---|
| 75 | Returns true if operation was able to produce a result; | 
|---|
| 76 | otherwise, result is unmodified. The result may be the input. | 
|---|
| 77 |  | 
|---|
| 78 | @param path The path typically with fill type set to even odd. | 
|---|
| 79 | @param result The equivalent path with fill type set to winding. | 
|---|
| 80 | @return True if winding path was set. | 
|---|
| 81 | */ | 
|---|
| 82 | bool SK_API AsWinding(const SkPath& path, SkPath* result); | 
|---|
| 83 |  | 
|---|
| 84 | /** Perform a series of path operations, optimized for unioning many paths together. | 
|---|
| 85 | */ | 
|---|
| 86 | class SK_API SkOpBuilder { | 
|---|
| 87 | public: | 
|---|
| 88 | /** Add one or more paths and their operand. The builder is empty before the first | 
|---|
| 89 | path is added, so the result of a single add is (emptyPath OP path). | 
|---|
| 90 |  | 
|---|
| 91 | @param path The second operand. | 
|---|
| 92 | @param _operator The operator to apply to the existing and supplied paths. | 
|---|
| 93 | */ | 
|---|
| 94 | void add(const SkPath& path, SkPathOp _operator); | 
|---|
| 95 |  | 
|---|
| 96 | /** Computes the sum of all paths and operands, and resets the builder to its | 
|---|
| 97 | initial state. | 
|---|
| 98 |  | 
|---|
| 99 | @param result The product of the operands. | 
|---|
| 100 | @return True if the operation succeeded. | 
|---|
| 101 | */ | 
|---|
| 102 | bool resolve(SkPath* result); | 
|---|
| 103 |  | 
|---|
| 104 | private: | 
|---|
| 105 | SkTArray<SkPath> fPathRefs; | 
|---|
| 106 | SkTDArray<SkPathOp> fOps; | 
|---|
| 107 |  | 
|---|
| 108 | static bool FixWinding(SkPath* path); | 
|---|
| 109 | static void ReversePath(SkPath* path); | 
|---|
| 110 | void reset(); | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | #endif | 
|---|
| 114 |  | 
|---|