1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *       http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17@file:Suppress("NOTHING_TO_INLINE")
18
19package androidx.core.graphics
20
21import android.graphics.Path
22import androidx.annotation.RequiresApi
23
24/**
25 * Flattens (or approximate) the [Path] with a series of line segments.
26 *
27 * @param error The acceptable error for a line on the Path. Typically this would be
28 *              0.5 so that the error is less than half a pixel. This value must be
29 *              positive and is set to 0.5 by default.
30 *
31 * @see Path.approximate
32 */
33@RequiresApi(26)
34fun Path.flatten(error: Float = 0.5f): Iterable<PathSegment> = PathUtils.flatten(this, error)
35
36/**
37 * Returns the union of two paths as a new [Path].
38 */
39@RequiresApi(19)
40inline operator fun Path.plus(p: Path): Path {
41    return Path(this).apply {
42        op(p, Path.Op.UNION)
43    }
44}
45
46/**
47 * Returns the difference of two paths as a new [Path].
48 */
49@RequiresApi(19)
50inline operator fun Path.minus(p: Path): Path {
51    return Path(this).apply {
52        op(p, Path.Op.DIFFERENCE)
53    }
54}
55
56/**
57 * Returns the union of two paths as a new [Path].
58 */
59@RequiresApi(19)
60inline infix fun Path.and(p: Path) = this + p
61
62/**
63 * Returns the intersection of two paths as a new [Path].
64 * If the paths do not intersect, returns an empty path.
65 */
66@RequiresApi(19)
67inline infix fun Path.or(p: Path): Path {
68    return Path().apply {
69        op(this@or, p, Path.Op.INTERSECT)
70    }
71}
72
73/**
74 * Returns the union minus the intersection of two paths as a new [Path].
75 */
76@RequiresApi(19)
77inline infix fun Path.xor(p: Path): Path {
78    return Path(this).apply {
79        op(p, Path.Op.XOR)
80    }
81}
82