scoop:scoop.graphics:0.1.0
src/drawing/path.scoop
Selected production source · src/drawing/path.scoop
1
package scoop.graphics3
/** The winding rule used consistently by drawing, clipping and contains. */4
public enum FillRule {5
NonZero, EvenOdd6
}8
/** An immutable native path. Builder changes and builder closure do not change it. */9
public class Path internal constructor(internal val resource: GraphicsResource): Closeable {10
public val isClosed: Boolean get() = resource.isClosed11
public val bounds: Rect get() {12
@Unsafe13
{14
val result = pathBounds(resource.get());15
checkRect(result);16
return result17
}18
}19
public fun contains(point: Point): Boolean {20
checkPoint(point)21
@Unsafe22
{23
return pathContains(resource.get(), point) != 024
}25
}26
public fun measure(): PathMeasure {27
@Unsafe28
{29
return PathMeasure(ownedGraphics(pathMeasure(resource.get()), "measure path"))30
}31
}32
public override fun close() {33
resource.close()34
}35
}37
/**38
* Builds an owned, immutable path and closes the temporary builder on every exit.39
* The configuration runs once, synchronously. An empty configuration creates an empty path.40
*/41
public fun path(42
fillRule: FillRule = FillRule.NonZero,43
configure: PathBuilder.() -> Unit = {}44
): Path =45
PathBuilder(fillRule).use {46
builder ->47
builder.configure()48
builder.build()49
}51
/** Incremental native path construction. build returns a snapshot and preserves the builder. */52
public class PathBuilder public constructor(fillRule: FillRule = FillRule.NonZero): Closeable {53
private val resource: GraphicsResource = createPathBuilder(fillRule)54
public fun moveTo(point: Point): Unit = command(0, point)55
public fun lineTo(point: Point): Unit = command(1, point)56
public fun quadraticTo(control: Point, end: Point): Unit = command(2, control, end)57
public fun cubicTo(control1: Point, control2: Point, end: Point): Unit =58
command(3, control1, control2, end)59
public fun closeContour(): Unit = command(4)60
public fun addRect(rect: Rect) {61
checkRect(rect)62
@Unsafe63
{64
graphicsResult(pathAddRect(resource.get(), rect, 0), "add rectangle")65
}66
}67
public fun addOval(rect: Rect) {68
checkRect(rect)69
@Unsafe70
{71
graphicsResult(pathAddRect(resource.get(), rect, 1), "add oval")72
}73
}74
/** Adds the same rounded outline used by drawing, clipping and hit geometry. */75
public fun addRoundRect(rect: Rect, radiusX: Double, radiusY: Double): Unit =76
addRoundRect(rect, CornerRadii.all(Size(radiusX, radiusY)))77
public fun addRoundRect(rect: Rect, radii: CornerRadii) {78
val outline = RoundedOutline(rect, radii).normalized()79
val corners = outline.radii80
@Unsafe81
{82
graphicsResult(83
pathAddRoundRect(84
resource.get(),85
rect,86
cornerPoint(corners.topLeft),87
cornerPoint(corners.topRight),88
cornerPoint(corners.bottomRight),89
cornerPoint(corners.bottomLeft)90
),91
"add rounded rectangle"92
)93
}94
}95
/** Adds a clockwise arc in radians, optionally starting a new contour. */96
public fun arcTo(oval: Rect, start: Double, sweep: Double, moveToStart: Boolean = false) {97
checkRect(oval);98
checkScalar(start);99
checkScalar(sweep)100
@Unsafe101
{102
graphicsResult(pathArc(resource.get(), oval, start, sweep, moveToStart), "add arc")103
}104
}105
/** Returns an independent snapshot without clearing the accumulated geometry. */106
public fun build(): Path {107
@Unsafe108
{109
return Path(ownedGraphics(pathBuild(resource.get()), "build path"))110
}111
}112
/** Clears the geometry and preserves the winding rule. */113
public fun reset(): Unit = command(5)114
private fun command(115
kind: Int,116
first: Point = Point(0.0, 0.0),117
second: Point = Point(0.0, 0.0),118
third: Point = Point(0.0, 0.0)119
) {120
checkPoint(first);121
checkPoint(second);122
checkPoint(third)123
@Unsafe124
{125
graphicsResult(pathCommand(resource.get(), kind, first, second, third), "path command")126
}127
}128
public override fun close() {129
resource.close()130
}131
}133
/** Position and unit tangent at a distance along the path's contours. */134
public struct PathSample(val position: Point, val tangent: Point)136
/** A measurement snapshot that remains valid after closing the original path. */137
public class PathMeasure internal constructor(private val resource: GraphicsResource): Closeable {138
public val length: Double get() {139
@Unsafe140
{141
return measureLength(resource.get())142
}143
}144
public fun sample(distance: Double): PathSample? {145
requireGraphics(146
distance.isFinite() && distance >= 0.0 && distance <= length,147
"distance outside path"148
)149
@Unsafe150
{151
val value = measureSample(resource.get(), distance)152
return if (value.found == 0)153
None154
else155
Some(PathSample(value.position, value.tangent))156
}157
}158
public override fun close() {159
resource.close()160
}161
}163
private fun createPathBuilder(rule: FillRule): GraphicsResource {164
@Unsafe165
{166
return ownedGraphics(167
pathBuilder(168
if (rule == FillRule.EvenOdd)169
1170
else171
0172
),173
"create path builder"174
)175
}176
}178
@CLayout179
internal struct NativePathSample(val found: Int, val position: Point, val tangent: Point)181
@Extern(name = "scoop_graphics_path_builder")182
private fun pathBuilder(rule: Int): Ptr<Unit>?184
@Extern(name = "scoop_graphics_path_command")185
private fun pathCommand(path: Ptr<Unit>, kind: Int, first: Point, second: Point, third: Point): Int187
@Extern(name = "scoop_graphics_path_rect")188
private fun pathAddRect(path: Ptr<Unit>, rect: Rect, oval: Int): Int190
@Extern(name = "scoop_graphics_path_round_rect")191
private fun pathAddRoundRect(192
path: Ptr<Unit>,193
rect: Rect,194
topLeft: Point,195
topRight: Point,196
bottomRight: Point,197
bottomLeft: Point198
): Int200
@Extern(name = "scoop_graphics_path_arc")201
private fun pathArc(path: Ptr<Unit>, oval: Rect, start: Double, sweep: Double, move: Boolean): Int203
@Extern(name = "scoop_graphics_path_build")204
private fun pathBuild(path: Ptr<Unit>): Ptr<Unit>?206
@Extern(name = "scoop_graphics_path_bounds")207
private fun pathBounds(path: Ptr<Unit>): Rect209
@Extern(name = "scoop_graphics_path_contains")210
private fun pathContains(path: Ptr<Unit>, point: Point): Int212
@Extern(name = "scoop_graphics_path_measure")213
private fun pathMeasure(path: Ptr<Unit>): Ptr<Unit>?215
@Extern(name = "scoop_graphics_measure_length")216
private fun measureLength(measure: Ptr<Unit>): Double218
@Extern(name = "scoop_graphics_measure_sample")219
private fun measureSample(measure: Ptr<Unit>, distance: Double): NativePathSample