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