typst_library/visualize/line.rs
1use crate::foundations::elem;
2use crate::layout::{Abs, Angle, Axes, Length, Rel};
3use crate::visualize::Stroke;
4
5/// A line from one point to another.
6///
7/// # Example
8/// ```example
9/// #set page(height: 100pt)
10///
11/// #line(length: 100%)
12/// #line(end: (50%, 50%))
13/// #line(
14/// length: 4cm,
15/// stroke: 2pt + maroon,
16/// )
17/// ```
18#[elem]
19pub struct LineElem {
20 /// The start point of the line.
21 ///
22 /// Must be an array of exactly two relative lengths.
23 pub start: Axes<Rel<Length>>,
24
25 /// The point where the line ends.
26 pub end: Option<Axes<Rel<Length>>>,
27
28 /// The line's length. This is only respected if `end` is `{none}`.
29 #[default(Abs::pt(30.0).into())]
30 pub length: Rel<Length>,
31
32 /// The angle at which the line points away from the origin. This is only
33 /// respected if `end` is `{none}`.
34 pub angle: Angle,
35
36 /// How to [stroke] the line.
37 ///
38 /// ```example
39 /// #set line(length: 100%)
40 /// #stack(
41 /// spacing: 1em,
42 /// line(stroke: 2pt + red),
43 /// line(stroke: (paint: blue, thickness: 4pt, cap: "round")),
44 /// line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")),
45 /// line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))),
46 /// )
47 /// ```
48 #[fold]
49 pub stroke: Stroke,
50}