1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
//! Colorable geometrical shapes.
use std::f64::consts::SQRT_2;
use super::prelude::*;
use super::LinkNode;
/// `rect`: A rectangle with optional content.
pub fn rect(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
shape_impl(args, ShapeKind::Rect, width, height)
}
/// `square`: A square with optional content.
pub fn square(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let size = args.named::<Length>("size")?.map(Linear::from);
let width = match size {
None => args.named("width")?,
size => size,
};
let height = match size {
None => args.named("height")?,
size => size,
};
shape_impl(args, ShapeKind::Square, width, height)
}
/// `ellipse`: An ellipse with optional content.
pub fn ellipse(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
shape_impl(args, ShapeKind::Ellipse, width, height)
}
/// `circle`: A circle with optional content.
pub fn circle(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let diameter = args.named("radius")?.map(|r: Length| 2.0 * Linear::from(r));
let width = match diameter {
None => args.named("width")?,
diameter => diameter,
};
let height = match diameter {
None => args.named("height")?,
diameter => diameter,
};
shape_impl(args, ShapeKind::Circle, width, height)
}
fn shape_impl(
args: &mut Args,
kind: ShapeKind,
width: Option<Linear>,
height: Option<Linear>,
) -> TypResult<Value> {
// The default appearance of a shape.
let default = Stroke {
paint: RgbaColor::BLACK.into(),
thickness: Length::pt(1.0),
};
// Parse fill & stroke.
let fill = args.named("fill")?.unwrap_or(None);
let stroke = match (args.named("stroke")?, args.named("thickness")?) {
(None, None) => fill.is_none().then(|| default),
(color, thickness) => color.unwrap_or(Some(default.paint)).map(|paint| Stroke {
paint,
thickness: thickness.unwrap_or(default.thickness),
}),
};
// Shorthand for padding.
let mut padding = args.named::<Linear>("padding")?.unwrap_or_default();
// Padding with this ratio ensures that a rectangular child fits
// perfectly into a circle / an ellipse.
if kind.is_round() {
padding.rel += Relative::new(0.5 - SQRT_2 / 4.0);
}
// The shape's contents.
let child = args
.find()
.map(|body: Node| body.into_block().padded(Sides::splat(padding)));
Ok(Value::inline(
ShapeNode { kind, fill, stroke, child }
.pack()
.sized(Spec::new(width, height)),
))
}
/// Places its child into a sizable and fillable shape.
#[derive(Debug, Hash)]
pub struct ShapeNode {
/// Which shape to place the child into.
pub kind: ShapeKind,
/// How to fill the shape.
pub fill: Option<Paint>,
/// How the stroke the shape.
pub stroke: Option<Stroke>,
/// The child node to place into the shape, if any.
pub child: Option<PackedNode>,
}
impl Layout for ShapeNode {
fn layout(
&self,
ctx: &mut LayoutContext,
regions: &Regions,
) -> Vec<Constrained<Rc<Frame>>> {
let mut frames;
if let Some(child) = &self.child {
let mut pod = Regions::one(regions.current, regions.base, regions.expand);
frames = child.layout(ctx, &pod);
// Relayout with full expansion into square region to make sure
// the result is really a square or circle.
if self.kind.is_quadratic() {
let length = if regions.expand.x || regions.expand.y {
let target = regions.expand.select(regions.current, Size::zero());
target.x.max(target.y)
} else {
let size = frames[0].item.size;
let desired = size.x.max(size.y);
desired.min(regions.current.x).min(regions.current.y)
};
pod.current = Size::splat(length);
pod.expand = Spec::splat(true);
frames = child.layout(ctx, &pod);
frames[0].cts = Constraints::tight(regions);
}
} else {
// The default size that a shape takes on if it has no child and
// enough space.
let mut size =
Size::new(Length::pt(45.0), Length::pt(30.0)).min(regions.current);
if self.kind.is_quadratic() {
let length = if regions.expand.x || regions.expand.y {
let target = regions.expand.select(regions.current, Size::zero());
target.x.max(target.y)
} else {
size.x.min(size.y)
};
size = Size::splat(length);
} else {
size = regions.expand.select(regions.current, size);
}
frames = vec![Frame::new(size).constrain(Constraints::tight(regions))];
}
let frame = Rc::make_mut(&mut frames[0].item);
// Add fill and/or stroke.
if self.fill.is_some() || self.stroke.is_some() {
let geometry = match self.kind {
ShapeKind::Square | ShapeKind::Rect => Geometry::Rect(frame.size),
ShapeKind::Circle | ShapeKind::Ellipse => Geometry::Ellipse(frame.size),
};
let shape = Shape {
geometry,
fill: self.fill,
stroke: self.stroke,
};
frame.prepend(Point::zero(), Element::Shape(shape));
}
// Apply link if it exists.
if let Some(url) = ctx.styles.get_ref(LinkNode::URL) {
frame.link(url);
}
frames
}
}
/// The type of a shape.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ShapeKind {
/// A rectangle with equal side lengths.
Square,
/// A quadrilateral with four right angles.
Rect,
/// An ellipse with coinciding foci.
Circle,
/// A curve around two focal points.
Ellipse,
}
impl ShapeKind {
/// Whether the shape is curved.
pub fn is_round(self) -> bool {
matches!(self, Self::Circle | Self::Ellipse)
}
/// Whether the shape has a fixed 1-1 aspect ratio.
pub fn is_quadratic(self) -> bool {
matches!(self, Self::Square | Self::Circle)
}
}
|