blob: 50e50d61482e9b3f71e17d6cab63c4360e563f33 [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
Cary Clark7d06e262018-08-16 11:53:54 -040070/** 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 */
82bool SK_API AsWinding(const SkPath& path, SkPath* result);
83
caryclark54359292015-03-26 07:52:43 -070084/** Perform a series of path operations, optimized for unioning many paths together.
85 */
86class SK_API SkOpBuilder {
87public:
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.
Ben Wagner63fd7602017-10-09 15:45:33 -040093 */
caryclark54359292015-03-26 07:52:43 -070094 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.
Ben Wagner63fd7602017-10-09 15:45:33 -040098
caryclark54359292015-03-26 07:52:43 -070099 @param result The product of the operands.
100 @return True if the operation succeeded.
101 */
102 bool resolve(SkPath* result);
103
104private:
105 SkTArray<SkPath> fPathRefs;
106 SkTDArray<SkPathOp> fOps;
107
caryclark51c56782016-11-07 05:09:28 -0800108 static bool FixWinding(SkPath* path);
109 static void ReversePath(SkPath* path);
caryclark54359292015-03-26 07:52:43 -0700110 void reset();
111};
112
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113#endif