typst_library/visualize/line.rs
1use crate::diag::SourceResult;
2use crate::engine::Engine;
3use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
4use crate::layout::{Abs, Angle, Axes, BlockElem, Length, Rel};
5use crate::visualize::Stroke;
6
7/// A line from one point to another.
8///
9/// # Example
10/// ```example
11/// #set page(height: 100pt)
12///
13/// #line(length: 100%)
14/// #line(end: (50%, 50%))
15/// #line(
16/// length: 4cm,
17/// stroke: 2pt + maroon,
18/// )
19/// ```
20#[elem(Show)]
21pub struct LineElem {
22 /// The start point of the line.
23 ///
24 /// Must be an array of exactly two relative lengths.
25 #[resolve]
26 pub start: Axes<Rel<Length>>,
27
28 /// The point where the line ends.
29 #[resolve]
30 pub end: Option<Axes<Rel<Length>>>,
31
32 /// The line's length. This is only respected if `end` is `{none}`.
33 #[resolve]
34 #[default(Abs::pt(30.0).into())]
35 pub length: Rel<Length>,
36
37 /// The angle at which the line points away from the origin. This is only
38 /// respected if `end` is `{none}`.
39 pub angle: Angle,
40
41 /// How to [stroke] the line.
42 ///
43 /// ```example
44 /// #set line(length: 100%)
45 /// #stack(
46 /// spacing: 1em,
47 /// line(stroke: 2pt + red),
48 /// line(stroke: (paint: blue, thickness: 4pt, cap: "round")),
49 /// line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")),
50 /// line(stroke: (paint: blue, thickness: 1pt, dash: ("dot", 2pt, 4pt, 2pt))),
51 /// )
52 /// ```
53 #[resolve]
54 #[fold]
55 pub stroke: Stroke,
56}
57
58impl Show for Packed<LineElem> {
59 fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
60 Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_line)
61 .pack()
62 .spanned(self.span()))
63 }
64}