blob: 1e8093f2a8139948954ee4ca170cb26252e2a2ad [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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
bungemanbf521ff2016-02-17 13:13:44 -080010#include "../private/SkTArray.h"
bungemana7e9f052016-02-18 08:53:33 -080011#include "../private/SkTDArray.h"
commit-bot@chromium.org13b3aa12013-07-09 19:43:35 +000012#include "SkPreConfig.h"
13
caryclark@google.com07393ca2013-04-08 11:47:37 +000014class SkPath;
caryclarka8d2ffb2014-06-24 07:55:11 -070015struct SkRect;
caryclark@google.com07393ca2013-04-08 11:47:37 +000016
caryclarkd8bc16b2015-03-26 09:05:12 -070017
caryclark@google.com7dfbb072013-04-22 14:37:05 +000018// FIXME: move everything below into the SkPath class
19/**
20 * The logical operations that can be performed when combining two paths.
21 */
caryclark@google.com07393ca2013-04-08 11:47:37 +000022enum SkPathOp {
caryclark54359292015-03-26 07:52:43 -070023 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
caryclark@google.com07393ca2013-04-08 11:47:37 +000028};
29
caryclark@google.com66560ca2013-04-26 19:51:16 +000030/** 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.
skia.committer@gmail.com214c8702013-04-27 07:02:53 +000033 The curve order is reduced where possible so that cubics may be turned
caryclark@google.com66560ca2013-04-26 19:51:16 +000034 into quadratics, and quadratics maybe turned into lines.
caryclark@google.com07393ca2013-04-08 11:47:37 +000035
skia.committer@gmail.com214c8702013-04-27 07:02:53 +000036 Returns true if operation was able to produce a result;
caryclark@google.com66560ca2013-04-26 19:51:16 +000037 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)
caryclark54359292015-03-26 07:52:43 -070041 @param op The operator to apply.
caryclark@google.com66560ca2013-04-26 19:51:16 +000042 @param result The product of the operands. The result may be one of the
43 inputs.
caryclark54359292015-03-26 07:52:43 -070044 @return True if the operation succeeded.
caryclark@google.com07393ca2013-04-08 11:47:37 +000045 */
commit-bot@chromium.org13b3aa12013-07-09 19:43:35 +000046bool SK_API Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result);
caryclark@google.com66560ca2013-04-26 19:51:16 +000047
48/** Set this path to a set of non-overlapping contours that describe the
skia.committer@gmail.com214c8702013-04-27 07:02:53 +000049 same area as the original path.
caryclark@google.com66560ca2013-04-26 19:51:16 +000050 The curve order is reduced where possible so that cubics may
51 be turned into quadratics, and quadratics maybe turned into lines.
52
skia.committer@gmail.com214c8702013-04-27 07:02:53 +000053 Returns true if operation was able to produce a result;
caryclark@google.com66560ca2013-04-26 19:51:16 +000054 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 */
commit-bot@chromium.org13b3aa12013-07-09 19:43:35 +000060bool SK_API Simplify(const SkPath& path, SkPath* result);
caryclark@google.com07393ca2013-04-08 11:47:37 +000061
caryclarka8d2ffb2014-06-24 07:55:11 -070062/** 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 */
68bool SK_API TightBounds(const SkPath& path, SkRect* result);
69
caryclark54359292015-03-26 07:52:43 -070070/** Perform a series of path operations, optimized for unioning many paths together.
71 */
72class SK_API SkOpBuilder {
73public:
74 /** Add one or more paths and their operand. The builder is empty before the first
75 path is added, so the result of a single add is (emptyPath OP path).
76
77 @param path The second operand.
78 @param _operator The operator to apply to the existing and supplied paths.
79 */
80 void add(const SkPath& path, SkPathOp _operator);
81
82 /** Computes the sum of all paths and operands, and resets the builder to its
83 initial state.
84
85 @param result The product of the operands.
86 @return True if the operation succeeded.
87 */
88 bool resolve(SkPath* result);
89
90private:
91 SkTArray<SkPath> fPathRefs;
92 SkTDArray<SkPathOp> fOps;
93
caryclark51c56782016-11-07 05:09:28 -080094 static bool FixWinding(SkPath* path);
95 static void ReversePath(SkPath* path);
caryclark54359292015-03-26 07:52:43 -070096 void reset();
97};
98
caryclark@google.com07393ca2013-04-08 11:47:37 +000099#endif