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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
use typst::font::FontWeight;
use super::{Counter, CounterUpdate, LocalName, Numbering, Refable};
use crate::layout::{BlockElem, HElem, VElem};
use crate::meta::{Count, Supplement};
use crate::prelude::*;
use crate::text::{SpaceElem, TextElem, TextSize};
/// A section heading.
///
/// With headings, you can structure your document into sections. Each heading
/// has a _level,_ which starts at one and is unbounded upwards. This level
/// indicates the logical role of the following content (section, subsection,
/// etc.) A top-level heading indicates a top-level section of the document
/// (not the document's title).
///
/// Typst can automatically number your headings for you. To enable numbering,
/// specify how you want your headings to be numbered with a
/// [numbering pattern or function]($func/numbering).
///
/// Independently from the numbering, Typst can also automatically generate an
/// [outline]($func/outline) of all headings for you. To exclude one or more
/// headings from this outline, you can set the `outlined` parameter to
/// `{false}`.
///
/// ## Example
/// ```example
/// #set heading(numbering: "1.a)")
///
/// = Introduction
/// In recent years, ...
///
/// == Preliminaries
/// To start, ...
/// ```
///
/// ## Syntax
/// Headings have dedicated syntax: They can be created by starting a line with
/// one or multiple equals signs, followed by a space. The number of equals
/// signs determines the heading's logical nesting depth.
///
/// Display: Heading
/// Category: meta
#[element(Locatable, Synthesize, Count, Show, Finalize, LocalName, Refable)]
pub struct HeadingElem {
/// The logical nesting depth of the heading, starting from one.
#[default(NonZeroUsize::ONE)]
pub level: NonZeroUsize,
/// How to number the heading. Accepts a
/// [numbering pattern or function]($func/numbering).
///
/// ```example
/// #set heading(numbering: "1.a.")
///
/// = A section
/// == A subsection
/// === A sub-subsection
/// ```
pub numbering: Option<Numbering>,
/// A supplement for the heading.
///
/// For references to headings, this is added before the
/// referenced number.
///
/// ```example
/// #set heading(numbering: "1.", supplement: "Chapter")
///
/// = Introduction <intro>
/// In @intro, we see how to turn
/// Sections into Chapters. And
/// in @intro[Part], it is done
/// manually.
/// ```
#[default(Smart::Auto)]
pub supplement: Smart<Option<Supplement>>,
/// Whether the heading should appear in the outline.
///
/// ```example
/// #outline()
///
/// #heading[Normal]
/// This is a normal heading.
///
/// #heading(outlined: false)[Hidden]
/// This heading does not appear
/// in the outline.
/// ```
#[default(true)]
pub outlined: bool,
/// The heading's title.
#[required]
pub body: Content,
}
impl Synthesize for HeadingElem {
fn synthesize(&mut self, _vt: &mut Vt, styles: StyleChain) -> SourceResult<()> {
self.push_level(self.level(styles));
self.push_numbering(self.numbering(styles));
self.push_supplement(self.supplement(styles));
self.push_outlined(self.outlined(styles));
self.push_supplement(self.supplement(styles));
Ok(())
}
}
impl Show for HeadingElem {
#[tracing::instrument(name = "HeadingElem::show", skip_all)]
fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let mut realized = self.body();
if let Some(numbering) = self.numbering(styles) {
realized = Counter::of(Self::func())
.display(Some(numbering), false)
.spanned(self.span())
+ HElem::new(Em::new(0.3).into()).with_weak(true).pack()
+ realized;
}
Ok(BlockElem::new().with_body(Some(realized)).pack())
}
}
impl Finalize for HeadingElem {
fn finalize(&self, realized: Content, styles: StyleChain) -> Content {
let level = self.level(styles).get();
let scale = match level {
1 => 1.4,
2 => 1.2,
_ => 1.0,
};
let size = Em::new(scale);
let above = Em::new(if level == 1 { 1.8 } else { 1.44 }) / scale;
let below = Em::new(0.75) / scale;
let mut styles = Styles::new();
styles.set(TextElem::set_size(TextSize(size.into())));
styles.set(TextElem::set_weight(FontWeight::BOLD));
styles.set(BlockElem::set_above(VElem::block_around(above.into())));
styles.set(BlockElem::set_below(VElem::block_around(below.into())));
styles.set(BlockElem::set_sticky(true));
realized.styled_with_map(styles)
}
}
impl Count for HeadingElem {
fn update(&self) -> Option<CounterUpdate> {
self.numbering(StyleChain::default())
.is_some()
.then(|| CounterUpdate::Step(self.level(StyleChain::default())))
}
}
cast_from_value! {
HeadingElem,
v: Content => v.to::<Self>().ok_or("expected heading")?.clone(),
}
impl Refable for HeadingElem {
fn reference(
&self,
vt: &mut Vt,
supplement: Option<Content>,
lang: Lang,
region: Option<Region>,
) -> SourceResult<Content> {
// Create the supplement of the heading.
let mut supplement = if let Some(supplement) = supplement {
supplement
} else {
match self.supplement(StyleChain::default()) {
Smart::Auto => TextElem::packed(self.local_name(lang, region)),
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => {
supplement.resolve(vt, std::iter::once(Value::from(self.clone())))?
}
}
};
// Append a non-breaking space if the supplement is not empty.
if !supplement.is_empty() {
supplement += TextElem::packed('\u{a0}')
};
// Check for a numbering.
let Some(numbering) = self.numbering(StyleChain::default()) else {
bail!(self.span(), "only numbered headings can be referenced");
};
// Get the counter and display it.
let numbers = Counter::of(Self::func())
.at(vt, self.0.location().unwrap())?
.display(vt, &numbering.trimmed())?;
Ok(supplement + numbers)
}
fn level(&self) -> usize {
self.level(StyleChain::default()).get()
}
fn numbering(&self) -> Option<Numbering> {
self.numbering(StyleChain::default())
}
fn counter(&self) -> Counter {
Counter::of(Self::func())
}
fn outline(
&self,
vt: &mut Vt,
_: Lang,
_: Option<Region>,
) -> SourceResult<Option<Content>> {
// Check whether the heading is outlined.
if !self.outlined(StyleChain::default()) {
return Ok(None);
}
// Build the numbering followed by the title.
let mut start = self.body();
if let Some(numbering) = self.numbering(StyleChain::default()) {
let numbers = Counter::of(HeadingElem::func())
.at(vt, self.0.location().unwrap())?
.display(vt, &numbering)?;
start = numbers + SpaceElem::new().pack() + start;
};
Ok(Some(start))
}
}
impl LocalName for HeadingElem {
fn local_name(&self, lang: Lang, _: Option<Region>) -> &'static str {
match lang {
Lang::ARABIC => "الفصل",
Lang::BOKMÅL => "Kapittel",
Lang::CHINESE => "小节",
Lang::CZECH => "Kapitola",
Lang::FRENCH => "Chapitre",
Lang::GERMAN => "Abschnitt",
Lang::ITALIAN => "Sezione",
Lang::NYNORSK => "Kapittel",
Lang::POLISH => "Sekcja",
Lang::PORTUGUESE => "Seção",
Lang::RUSSIAN => "Раздел",
Lang::SLOVENIAN => "Poglavje",
Lang::SPANISH => "Sección",
Lang::UKRAINIAN => "Розділ",
Lang::VIETNAMESE => "Phần", // TODO: This may be wrong.
Lang::ENGLISH | _ => "Section",
}
}
}
|