dymokomi/luced-2d
Source of release 0.5.0
0.5.0 / src / vector_outline.luc
## A vector element's outline as the canvas flattens it, in document pixels:
## what the view draws round the active element (and through a free
## transform's box), what the Vector panel's thumbnails show, and the box a
## free transform of the element starts from.
from luce_image.canvas import Canvas
from transform_box import TransformBox
from luce_std import math
## x, y pairs, and where each run starts (its first point's index).
pub class Outline:
pub var points: list[float]
pub var starts: list[int]
pub func init(self):
self.points = []
self.starts = []
## The box round it: left, top, right, bottom; none when it has no points.
pub func bounds(self) -> list[float]?:
if self.points.length < 2:
return none
var box = [self.points[0], self.points[1], self.points[0], self.points[1]]
var at = 2
while at + 1 < self.points.length:
box[0] = math.min(box[0], self.points[at])
box[1] = math.min(box[1], self.points[at + 1])
box[2] = math.max(box[2], self.points[at])
box[3] = math.max(box[3], self.points[at + 1])
at += 2
return box
## Whether point `point` begins a run (so no line joins it to the one before).
pub func starts_run(self, point: int) -> bool:
return point in self.starts
## Element `element` of the layer at `index`, outlined.
pub func element_outline(canvas: Canvas, index: int, element: int) -> Outline!:
let outline = Outline()
let count = canvas.element_outline(index, element)
var point = 0
while point < count:
if canvas.outline_starts(point):
outline.starts.append(point)
outline.points.append(canvas.outline_x(point))
outline.points.append(canvas.outline_y(point))
point += 1
return outline
## The box a free transform of element `element` of the layer at `index`
## starts from; none when it has no size.
pub func outline_box(canvas: Canvas, index: int, element: int) -> TransformBox?!:
let box = element_outline(canvas, index, element).bounds() else return none
if box[2] - box[0] < 1.0 and box[3] - box[1] < 1.0:
return none
return TransformBox(box[0], box[1], math.max(1.0, box[2] - box[0]), math.max(1.0, box[3] - box[1]))