dymokomi/luce-image
Source of release 0.45.0
0.45.0 / src / canvas / vector_geometry.lucb
#==============================================================================================
#
# canvas/vector_geometry - Where a vector element is: its outline, hits and selection
#
# DESCRIPTION:
# An element's outline is what luce-svg flattens it to, parsed over the whole canvas so
# its points are document pixels, the element's transform applied. The view draws it
# (the active element's outline, and a transform box's outline preview), a press finds
# the topmost element under it, and Make Selection turns it into a selection. None of
# these touch a pixel: they cost the outline.
#
#==============================================================================================
extend Canvas:
## The topmost shown element of the layer at `index` under document point
## (`x`, `y`) — inside its fill or within `reach` pixels of its outline;
## -1 for none.
pub func element_at(index: i64, x: f64, y: f64, reach: f64) -> i64!:
let record = try self.record(index)
var at = record.elements.length
while at > 0:
at -= 1
let spec = record.elements[at].spec
if not record.elements[at].visible or not drawable(spec):
continue
if try spec_hit(spec, self.extent_x, self.extent_y, x, y, reach):
return (i64)at
return -1
## Select element `element`'s outline, combined with the selection by
## `mode` (0 replace, 1 add, 2 subtract, 3 intersect). One step.
pub func select_element(index: i64, element: i64, mode: i64 = 0) -> !:
let at_index = (usize)(try self.checked_index(index))
try self.require_open()
let spec = (try element_of(self.records[at_index], element)).spec
if not drawable(spec):
error(invalid, "the element has no outline to select")
let parsed = try spec_drawing(spec, self.extent_x, self.extent_y)
defer parsed.release()
let drawing = parsed.value()
var shape = try Selection.create(self.extent_x, self.extent_y)
defer shape.destroy()
var first_turn = 0.0
var runs: usize = 0
var points = try new f64[256] --- in memory.heap
defer free(points) in memory.heap
for contour in 0..<drawing.contour_count():
let start = drawing.contour_start(contour)
let size = drawing.contour_size(contour)
if size < 3:
continue
if size * 2 > points.length:
free(points) in memory.heap
points = try new f64[size * 2] --- in memory.heap
var turn = 0.0
for at in 0..<size:
let a = drawing.point(start + at)
let b = drawing.point(start + (at + 1) % size)
points[at * 2] = a.x
points[at * 2 + 1] = a.y
turn += a.x * b.y - b.x * a.y
var how = Mode.replace
if runs == 0:
first_turn = turn
else:
how = Mode.add if (turn > 0.0) == (first_turn > 0.0) else Mode.subtract
try shape.polygon(points[..<size * 2], how)
runs += 1
if runs == 0:
error(invalid, "the element has no area to select")
try self.remember_state("Make Selection")
try self.merge_shape(&shape, mode)
## Element `element`'s outline in document pixels, flattened, for drawing
## it: how many points; `outline_x`, `outline_y` and `outline_starts` (a
## point beginning a new run) read them until the next call.
pub func element_outline(index: i64, element: i64) -> i64!:
let at_index = (usize)(try self.checked_index(index))
self.outline_count = 0
let spec = (try element_of(self.records[at_index], element)).spec
if not drawable(spec):
return 0
let parsed = try spec_drawing(spec, self.extent_x, self.extent_y)
defer parsed.release()
let drawing = parsed.value()
for contour in 0..<drawing.contour_count():
let start = drawing.contour_start(contour)
let size = drawing.contour_size(contour)
if size == 0:
continue
for at in 0..<size:
let point = drawing.point(start + at)
try put_outline(&self, point.x, point.y, at == 0)
if drawing.contour_closed(contour) and size > 2:
let first = drawing.point(start)
try put_outline(&self, first.x, first.y, false)
return (i64)self.outline_count
pub func outline_x(point: i64) -> f64:
return self.outline[(usize)point * 3] if point >= 0 and (usize)point < self.outline_count else 0.0
pub func outline_y(point: i64) -> f64:
return self.outline[(usize)point * 3 + 1] if point >= 0 and (usize)point < self.outline_count else 0.0
pub func outline_starts(point: i64) -> bool:
return point >= 0 and (usize)point < self.outline_count and self.outline[(usize)point * 3 + 2] != 0.0
## The spec of the element the record targets (`target_element`); none when it has none.
func target_spec(record: Record) -> ShapeSpec?:
let at = target_element(&record) else return none
return record.elements[at].spec
## Element `element` of `record`, checked.
func element_of(record: Record, element: i64) -> VectorElement!:
if element < 0 or element >= (i64)record.elements.length:
error(invalid, "no such vector element")
return record.elements[(usize)element]
## `Canvas.restyle_shape`: the target element's values from sides on (`style`, 11
## of them in `shape_values` order) set, its geometry and path kept. One step.
func restyle_target(canvas: Canvas*, id: u64, style: const f64[]) -> !:
try canvas.require_open()
try canvas.require_idle()
let index = try canvas.index(id)
let at = target_element(&canvas.records[index]) else error(invalid, "the layer is not a shape")
let old = canvas.records[index].elements[at].spec
var values = shape_values(old)
for field in 0..<(usize)11:
values[5 + field] = style[field]
var spec = try shape_from_values(values[..<22])
spec.closed = old.closed
spec.path = old.path
spec.subpaths = old.subpaths
# A path keeps its anchors: a copy the new spec owns.
try copy_spec_path(&spec)
try replace_element_spec(canvas, index, at, spec, "Shape")
func put_outline(canvas: Canvas*, x: f64, y: f64, starts: bool) -> !:
if (canvas.outline_count + 1) * 3 > canvas.outline.length:
let capacity = 384 if canvas.outline.length == 0 else canvas.outline.length * 2
let grown = try new f64[capacity] --- in memory.heap
if canvas.outline_count > 0:
memory.copy(grown, canvas.outline, canvas.outline_count * 3)
if canvas.outline.length > 0:
free(canvas.outline) in memory.heap
canvas.outline = grown
let at = canvas.outline_count * 3
canvas.outline[at] = x
canvas.outline[at + 1] = y
canvas.outline[at + 2] = 1.0 if starts else 0.0
canvas.outline_count += 1