Home | History | Annotate | Download | only in pathops
      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 "../private/SkTArray.h"
     11 #include "../private/SkTDArray.h"
     12 #include "SkPreConfig.h"
     13 
     14 class SkPath;
     15 struct SkRect;
     16 
     17 
     18 // FIXME: move everything below into the SkPath class
     19 /**
     20   *  The logical operations that can be performed when combining two paths.
     21   */
     22 enum SkPathOp {
     23     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
     28 };
     29 
     30 /** 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.
     33     The curve order is reduced where possible so that cubics may be turned
     34     into quadratics, and quadratics maybe turned into lines.
     35 
     36     Returns true if operation was able to produce a result;
     37     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)
     41     @param op The operator to apply.
     42     @param result The product of the operands. The result may be one of the
     43                   inputs.
     44     @return True if the operation succeeded.
     45   */
     46 bool SK_API Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result);
     47 
     48 /** Set this path to a set of non-overlapping contours that describe the
     49     same area as the original path.
     50     The curve order is reduced where possible so that cubics may
     51     be turned into quadratics, and quadratics maybe turned into lines.
     52 
     53     Returns true if operation was able to produce a result;
     54     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   */
     60 bool SK_API Simplify(const SkPath& path, SkPath* result);
     61 
     62 /** 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   */
     68 bool SK_API TightBounds(const SkPath& path, SkRect* result);
     69 
     70 /** Perform a series of path operations, optimized for unioning many paths together.
     71   */
     72 class SK_API SkOpBuilder {
     73 public:
     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 
     90 private:
     91     SkTArray<SkPath> fPathRefs;
     92     SkTDArray<SkPathOp> fOps;
     93 
     94     static bool FixWinding(SkPath* path);
     95     static void ReversePath(SkPath* path);
     96     void reset();
     97 };
     98 
     99 #endif
    100