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