107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com/*
207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com * Copyright 2012 Google Inc.
307393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com *
407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com * Use of this source code is governed by a BSD-style license that can be
507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com * found in the LICENSE file.
607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com */
707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#ifndef SkPathOps_DEFINED
807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#define SkPathOps_DEFINED
907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com
10bf521ff9415b3bdb1acde7b8d18139df176236e5bungeman#include "../private/SkTArray.h"
11a7e9f05119174848c6be5028568f46eb5e85398ebungeman#include "../private/SkTDArray.h"
1213b3aa146680b6e3d36bd8faacd1cd7bb50c429ecommit-bot@chromium.org#include "SkPreConfig.h"
1313b3aa146680b6e3d36bd8faacd1cd7bb50c429ecommit-bot@chromium.org
1407393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comclass SkPath;
15a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclarkstruct SkRect;
1607393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com
17d8bc16b306f8453a86d0af070d68c327f4bb41ebcaryclark
187dfbb0720a133c0f63ac7be504f335bbcc62a291caryclark@google.com// FIXME: move everything below into the SkPath class
197dfbb0720a133c0f63ac7be504f335bbcc62a291caryclark@google.com/**
207dfbb0720a133c0f63ac7be504f335bbcc62a291caryclark@google.com  *  The logical operations that can be performed when combining two paths.
217dfbb0720a133c0f63ac7be504f335bbcc62a291caryclark@google.com  */
2207393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.comenum SkPathOp {
2354359294a7c9dc54802d512a5d891a35c1663392caryclark    kDifference_SkPathOp,         //!< subtract the op path from the first path
2454359294a7c9dc54802d512a5d891a35c1663392caryclark    kIntersect_SkPathOp,          //!< intersect the two paths
2554359294a7c9dc54802d512a5d891a35c1663392caryclark    kUnion_SkPathOp,              //!< union (inclusive-or) the two paths
2654359294a7c9dc54802d512a5d891a35c1663392caryclark    kXOR_SkPathOp,                //!< exclusive-or the two paths
2754359294a7c9dc54802d512a5d891a35c1663392caryclark    kReverseDifference_SkPathOp,  //!< subtract the first path from the op path
2807393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com};
2907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com
3066560ca776773858abfffd59974eac32c942acc3caryclark@google.com/** Set this path to the result of applying the Op to this path and the
3166560ca776773858abfffd59974eac32c942acc3caryclark@google.com    specified path: this = (this op operand).
3266560ca776773858abfffd59974eac32c942acc3caryclark@google.com    The resulting path will be constructed from non-overlapping contours.
33214c870f5e45f79bc5a8d695c55ec7b881535f4cskia.committer@gmail.com    The curve order is reduced where possible so that cubics may be turned
3466560ca776773858abfffd59974eac32c942acc3caryclark@google.com    into quadratics, and quadratics maybe turned into lines.
3566560ca776773858abfffd59974eac32c942acc3caryclark@google.com
36214c870f5e45f79bc5a8d695c55ec7b881535f4cskia.committer@gmail.com    Returns true if operation was able to produce a result;
3766560ca776773858abfffd59974eac32c942acc3caryclark@google.com    otherwise, result is unmodified.
3866560ca776773858abfffd59974eac32c942acc3caryclark@google.com
3966560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @param one The first operand (for difference, the minuend)
4066560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @param two The second operand (for difference, the subtrahend)
4154359294a7c9dc54802d512a5d891a35c1663392caryclark    @param op The operator to apply.
4266560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @param result The product of the operands. The result may be one of the
4366560ca776773858abfffd59974eac32c942acc3caryclark@google.com                  inputs.
4454359294a7c9dc54802d512a5d891a35c1663392caryclark    @return True if the operation succeeded.
4507393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com  */
4613b3aa146680b6e3d36bd8faacd1cd7bb50c429ecommit-bot@chromium.orgbool SK_API Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result);
4707393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com
4866560ca776773858abfffd59974eac32c942acc3caryclark@google.com/** Set this path to a set of non-overlapping contours that describe the
49214c870f5e45f79bc5a8d695c55ec7b881535f4cskia.committer@gmail.com    same area as the original path.
5066560ca776773858abfffd59974eac32c942acc3caryclark@google.com    The curve order is reduced where possible so that cubics may
5166560ca776773858abfffd59974eac32c942acc3caryclark@google.com    be turned into quadratics, and quadratics maybe turned into lines.
5266560ca776773858abfffd59974eac32c942acc3caryclark@google.com
53214c870f5e45f79bc5a8d695c55ec7b881535f4cskia.committer@gmail.com    Returns true if operation was able to produce a result;
5466560ca776773858abfffd59974eac32c942acc3caryclark@google.com    otherwise, result is unmodified.
5566560ca776773858abfffd59974eac32c942acc3caryclark@google.com
5666560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @param path The path to simplify.
5766560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @param result The simplified path. The result may be the input.
5866560ca776773858abfffd59974eac32c942acc3caryclark@google.com    @return True if simplification succeeded.
5907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com  */
6013b3aa146680b6e3d36bd8faacd1cd7bb50c429ecommit-bot@chromium.orgbool SK_API Simplify(const SkPath& path, SkPath* result);
6107393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com
62a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark/** Set the resulting rectangle to the tight bounds of the path.
63a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark
64a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark    @param path The path measured.
65a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark    @param result The tight bounds of the path.
66a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark    @return True if the bounds could be computed.
67a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark  */
68a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclarkbool SK_API TightBounds(const SkPath& path, SkRect* result);
69a8d2ffb1c2e245ae06bdd1c20f34995ab8311cf6caryclark
7054359294a7c9dc54802d512a5d891a35c1663392caryclark/** Perform a series of path operations, optimized for unioning many paths together.
7154359294a7c9dc54802d512a5d891a35c1663392caryclark  */
7254359294a7c9dc54802d512a5d891a35c1663392caryclarkclass SK_API SkOpBuilder {
7354359294a7c9dc54802d512a5d891a35c1663392caryclarkpublic:
7454359294a7c9dc54802d512a5d891a35c1663392caryclark    /** Add one or more paths and their operand. The builder is empty before the first
7554359294a7c9dc54802d512a5d891a35c1663392caryclark        path is added, so the result of a single add is (emptyPath OP path).
7654359294a7c9dc54802d512a5d891a35c1663392caryclark
7754359294a7c9dc54802d512a5d891a35c1663392caryclark        @param path The second operand.
7854359294a7c9dc54802d512a5d891a35c1663392caryclark        @param _operator The operator to apply to the existing and supplied paths.
7963fd760a37905c45d26fc3d49cac261fad1b4808Ben Wagner     */
8054359294a7c9dc54802d512a5d891a35c1663392caryclark    void add(const SkPath& path, SkPathOp _operator);
8154359294a7c9dc54802d512a5d891a35c1663392caryclark
8254359294a7c9dc54802d512a5d891a35c1663392caryclark    /** Computes the sum of all paths and operands, and resets the builder to its
8354359294a7c9dc54802d512a5d891a35c1663392caryclark        initial state.
8463fd760a37905c45d26fc3d49cac261fad1b4808Ben Wagner
8554359294a7c9dc54802d512a5d891a35c1663392caryclark        @param result The product of the operands.
8654359294a7c9dc54802d512a5d891a35c1663392caryclark        @return True if the operation succeeded.
8754359294a7c9dc54802d512a5d891a35c1663392caryclark      */
8854359294a7c9dc54802d512a5d891a35c1663392caryclark    bool resolve(SkPath* result);
8954359294a7c9dc54802d512a5d891a35c1663392caryclark
9054359294a7c9dc54802d512a5d891a35c1663392caryclarkprivate:
9154359294a7c9dc54802d512a5d891a35c1663392caryclark    SkTArray<SkPath> fPathRefs;
9254359294a7c9dc54802d512a5d891a35c1663392caryclark    SkTDArray<SkPathOp> fOps;
9354359294a7c9dc54802d512a5d891a35c1663392caryclark
9451c5678f258736736c4a5d48d4e82c73be225428caryclark    static bool FixWinding(SkPath* path);
9551c5678f258736736c4a5d48d4e82c73be225428caryclark    static void ReversePath(SkPath* path);
9654359294a7c9dc54802d512a5d891a35c1663392caryclark    void reset();
9754359294a7c9dc54802d512a5d891a35c1663392caryclark};
9854359294a7c9dc54802d512a5d891a35c1663392caryclark
9907393cab57ce74a4aae89a31fae9aaa9780fc19dcaryclark@google.com#endif
100