dymokomi/luce-browser-render
Source of release 0.6.2
0.6.2 / src / raster / path.lucb
#==============================================================================================
#
# path - the Bezier path container and its segment iterator
#
# DESCRIPTION:
# Port of tiny-skia-path path.rs: PathVerb, Path (verbs, points, bounds; owns its
# storage, `release()` frees it), PathSegment and PathSegmentsIter, with Skia's conics
# (SkPath's kConic verb and weights, which tiny-skia turns into quads). Rust's
# `Path::transform(self)` consumes the path; here it transforms in place and answers
# whether the result is still a valid path.
#
#==============================================================================================
## A path verb.
pub enum PathVerb as u8:
move = 0
line = 1
quad = 2
cubic = 3
close = 4
## A conic: a quad with a weight (SkPath's kConic). Its weight is in the path's weights.
conic = 5
## A Bezier path. Can be created via `PathBuilder`. Where `PathBuilder` can be reused multiple
## times. `Path` is an immutable, finite and non-empty object.
pub struct Path:
var verbs: Vec[PathVerb]
var points: Vec[Point]
## The weights of the conics, in order.
var weights: Vec[f32]
var bounds_: Rect
## Returns the number of segments in the path.
pub func len() -> usize:
return self.verbs.len
## Return if the path is empty.
pub func is_empty() -> bool:
return self.verbs.len == 0
## Returns the bounds of the path's points. The value is already calculated.
pub func bounds() -> Rect:
return self.bounds_
## Calculates path's tight bounds. This operation can be expensive.
pub func compute_tight_bounds() -> Rect?:
# big enough to hold worst-case curve type (cubic) extremas + 1
var extremas: Point[5] = [Point.zero(), Point.zero(), Point.zero(), Point.zero(), Point.zero()]
var min = self.points.data[0]
var max = self.points.data[0]
var iter = self.segments()
var last_point = Point.zero()
while let segment = iter.next():
var count: usize = 0
match segment:
.move_to(p):
extremas[0] = p
count = 1
.line_to(p):
extremas[0] = p
count = 1
.quad_to(p0, p1):
count = compute_quad_extremas(last_point, p0, p1, extremas)
.conic_to(p0, p1, w):
count = compute_conic_extremas(last_point, p0, p1, w, extremas)
.cubic_to(p0, p1, p2):
count = compute_cubic_extremas(last_point, p0, p1, p2, extremas)
.close:
count = 0
last_point = iter.last_point
for tmp in extremas[..<count]:
min.x = fmin(min.x, tmp.x)
min.y = fmin(min.y, tmp.y)
max.x = fmax(max.x, tmp.x)
max.y = fmax(max.y, tmp.y)
return Rect.from_ltrb(min.x, min.y, max.x, max.y)
## Returns an internal vector of verbs.
pub func verbs_view() -> const PathVerb[]:
return self.verbs.view()
## Returns an internal vector of points.
pub func points_view() -> const Point[]:
return self.points.view()
## Returns the weights of the path's conics, in order.
pub func weights_view() -> const f32[]:
return self.weights.view()
## Transforms the path in place (Rust's `Path::transform`, which consumes the path).
## Answers false when the result has no valid bounds; the path must then be released.
## A matrix with perspective transforms as SkPathBuilder::transform does (the raw verbs).
pub func transform(ts: Transform) -> bool:
return self.transform_with(ts, .raw)
## `transform`, walking the path as `iteration` says when `ts` has perspective (SkPath's
## own transform walks it with SkPath::Iter).
pub func transform_with(ts: Transform, iteration: PerspectiveIteration) -> bool:
if ts.is_identity():
return true
if ts.has_perspective():
let result = path_transform_perspective(&self, ts, iteration)
self.release()
if let transformed = result:
self = transformed
return true
return false
ts.map_points(self.points.view())
# Update bounds.
let bounds = Rect.from_points(self.points.view()) else return false
self.bounds_ = bounds
return true
## Returns an iterator over path's segments.
pub func segments() -> PathSegmentsIter:
return PathSegmentsIter(path = &self, verb_index = 0, points_index = 0, weight_index = 0, is_auto_close = false, last_move_to = Point.zero(), last_point = Point.zero())
## Returns a stroked path. Returns none when the stroke is invalid or the result is
## empty. `resolution_scale` can be obtained via `PathStroker.compute_resolution_scale`.
## If you plan stroking multiple paths, you can try using `PathStroker` which will
## preserve temporary allocations required during stroking. This might improve
## performance a bit.
pub func stroke(stroke: const Stroke*, resolution_scale: f32) -> Path?:
var stroker = PathStroker.new_()
defer stroker.release()
return stroker.stroke(&self, stroke, resolution_scale)
## Converts the current path into a dashed one. `resolution_scale` can be obtained via
## `PathStroker.compute_resolution_scale`. Returns none when the path is empty, or the
## dash is too long.
pub func dash(dash: const StrokeDash*, resolution_scale: f32) -> Path?:
# SkDashImpl::filterPath for a hairline without a cull rectangle.
var rec = DashStrokeRec(width = 0.0, miter_limit = 4.0, line_cap = .butt, line_join = .miter, res_scale = resolution_scale, is_fill = false)
let result = dash_filter(&self, dash, &rec, none)
return result.path
## A copy of the path with its own storage (Rust's `Path::clone`).
pub func clone() -> Path:
return Path(verbs = self.verbs.clone(), points = self.points.clone(), weights = self.weights.clone(), bounds_ = self.bounds_)
## Clears the path and returns a `PathBuilder` that will reuse an allocated memory.
pub func clear() -> PathBuilder:
self.verbs.clear()
self.points.clear()
self.weights.clear()
let builder = PathBuilder(verbs = self.verbs, points = self.points, weights = self.weights, last_move_to_index = 0, move_to_required = true)
self.verbs = Vec[PathVerb]()
self.points = Vec[Point]()
self.weights = Vec[f32]()
return builder
## Frees the path's storage.
pub func release():
self.verbs.release()
self.points.release()
self.weights.release()
func compute_quad_extremas(p0: Point, p1: Point, p2: Point, extremas: Point[]) -> usize:
let src: Point[3] = [p0, p1, p2]
var extrema_idx: usize = 0
if let t = find_quad_extrema(p0.x, p1.x, p2.x):
extremas[extrema_idx] = eval_quad_at(src, t)
extrema_idx += 1
if let t = find_quad_extrema(p0.y, p1.y, p2.y):
extremas[extrema_idx] = eval_quad_at(src, t)
extrema_idx += 1
extremas[extrema_idx] = p2
return extrema_idx + 1
## SkPath.cpp's compute_conic_extremas: the conic's x and y extrema, then its end point.
func compute_conic_extremas(p0: Point, p1: Point, p2: Point, w: f32, extremas: Point[]) -> usize:
let conic = Conic.new_(p0, p1, p2, w)
var ts: f32[2] = [0.0, 0.0]
var n: usize = 0
if let t = conic_find_extrema(p0.x, p1.x, p2.x, w):
ts[n] = t
n += 1
if let t = conic_find_extrema(p0.y, p1.y, p2.y, w):
ts[n] = t
n += 1
for i in 0..<n:
extremas[i] = conic.eval_at(ts[i])
extremas[n] = p2
return n + 1
func compute_cubic_extremas(p0: Point, p1: Point, p2: Point, p3: Point, extremas: Point[]) -> usize:
var ts0 = new_t_values()
var ts1 = new_t_values()
let n0 = find_cubic_extrema(p0.x, p1.x, p2.x, p3.x, ts0)
let n1 = find_cubic_extrema(p0.y, p1.y, p2.y, p3.y, ts1)
let total_len = n0 + n1
let src: Point[4] = [p0, p1, p2, p3]
var extrema_idx: usize = 0
for t in ts0[..<n0]:
extremas[extrema_idx] = eval_cubic_pos_at(src, t)
extrema_idx += 1
for t in ts1[..<n1]:
extremas[extrema_idx] = eval_cubic_pos_at(src, t)
extrema_idx += 1
extremas[total_len] = p3
return total_len + 1
## A path segment.
pub enum PathSegment:
close
move_to(p: Point)
line_to(p: Point)
quad_to(p1: Point, p: Point)
cubic_to(p1: Point, p2: Point, p: Point)
## A conic (SkPath's conicTo): a quad whose middle point pulls with `weight`.
conic_to(p1: Point, p: Point, weight: f32)
## A path segments iterator.
pub struct PathSegmentsIter:
let path: const Path*
var verb_index: usize
var points_index: usize
var weight_index: usize
var is_auto_close: bool
var last_move_to: Point
var last_point: Point
## Sets the auto closing mode. Off by default. When enabled, emits an additional
## `PathSegment.line_to` from the current position to the previous `PathSegment.move_to`.
## And only then emits `PathSegment.close`.
pub func set_auto_close(flag: bool):
self.is_auto_close = flag
func auto_close() -> PathSegment:
if self.is_auto_close and not points_equal(self.last_point, self.last_move_to):
self.verb_index -= 1
return .line_to(p = self.last_move_to)
return .close
## SkStroke.cpp's has_valid_tangent: whether a segment with length follows before the
## contour ends. A segment's points are compared with its start (`pts[0]`), the point the
## iterator stood on before it.
func has_valid_tangent() -> bool:
var iter = self
var start = iter.last_point
while let segment = iter.next():
match segment:
.move_to(_):
return false
.line_to(p):
if not points_equal(start, p):
return true
.quad_to(p1, p2):
if not points_equal(start, p1) or not points_equal(start, p2):
return true
.conic_to(p1, p2, _):
if not points_equal(start, p1) or not points_equal(start, p2):
return true
.cubic_to(p1, p2, p3):
if not points_equal(start, p1) or not points_equal(start, p2) or not points_equal(start, p3):
return true
.close:
return false
start = iter.last_point
return false
## Returns the current verb.
pub func curr_verb() -> PathVerb:
return self.path.verbs.data[self.verb_index - 1]
## Returns the next verb.
pub func next_verb() -> PathVerb?:
if self.verb_index < self.path.verbs.len:
return self.path.verbs.data[self.verb_index]
return none
## The next segment.
pub func next() -> PathSegment?:
if self.verb_index < self.path.verbs.len:
let verb = self.path.verbs.data[self.verb_index]
self.verb_index += 1
let points = self.path.points.data
match verb:
.move:
self.points_index += 1
self.last_move_to = points[self.points_index - 1]
self.last_point = self.last_move_to
return .move_to(p = self.last_move_to)
.line:
self.points_index += 1
self.last_point = points[self.points_index - 1]
return .line_to(p = self.last_point)
.quad:
self.points_index += 2
self.last_point = points[self.points_index - 1]
return .quad_to(p1 = points[self.points_index - 2], p = self.last_point)
.cubic:
self.points_index += 3
self.last_point = points[self.points_index - 1]
return .cubic_to(p1 = points[self.points_index - 3], p2 = points[self.points_index - 2], p = self.last_point)
.conic:
self.points_index += 2
self.weight_index += 1
self.last_point = points[self.points_index - 1]
return .conic_to(p1 = points[self.points_index - 2], p = self.last_point, weight = self.path.weights.data[self.weight_index - 1])
.close:
let seg = self.auto_close()
self.last_point = self.last_move_to
return seg
_:
return none
return none
## `a == b` for points (Rust's derived PartialEq).
func points_equal(a: Point, b: Point) -> bool:
return a.x == b.x and a.y == b.y