Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent configuration for multiline headings #3459

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 44 additions & 7 deletions crates/typst/src/model/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use std::num::NonZeroUsize;
use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{
elem, Content, NativeElement, Packed, Show, ShowSet, Smart, StyleChain, Styles,
Synthesize,
elem, Content, NativeElement, Packed, Resolve, Show, ShowSet, Smart, StyleChain,
Styles, Synthesize,
};
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{BlockElem, Em, HElem, VElem};
use crate::model::{Numbering, Outlinable, Refable, Supplement};
use crate::layout::{
Abs, Axes, BlockElem, Em, HElem, LayoutMultiple, Length, Regions, VElem,
};
use crate::model::{Numbering, Outlinable, ParElem, Refable, Supplement};
use crate::text::{FontWeight, LocalName, SpaceElem, TextElem, TextSize};
use crate::util::NonZeroExt;

Expand Down Expand Up @@ -163,6 +165,21 @@ pub struct HeadingElem {
#[default(Smart::Auto)]
pub bookmarked: Smart<bool>,

/// The indent all but the first line of a heading should have.
///
/// The default value of `{auto}` indicates that the subsequent heading
/// lines will be indented based on the width of the numbering.
///
/// Custom lengths will indent the lines at least as much as the
/// numbering.
haenoe marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```example
/// #set heading(numbering: "1.")
/// #heading[A very, very, very, very, very, very long heading]
/// ```
#[default(Smart::Auto)]
pub hanging_indent: Smart<Length>,

/// The heading's title.
#[required]
pub body: Content,
Expand Down Expand Up @@ -203,13 +220,33 @@ impl Show for Packed<HeadingElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let span = self.span();
let mut realized = self.body().clone();

let hanging_indent = self.hanging_indent(styles);

let mut indent = match self.hanging_indent(styles) {
haenoe marked this conversation as resolved.
Show resolved Hide resolved
Smart::Custom(length) => length.resolve(styles),
Smart::Auto => Abs::zero(),
};

if let Some(numbering) = (**self).numbering(styles).as_ref() {
realized = Counter::of(HeadingElem::elem())
let numbering = Counter::of(HeadingElem::elem())
.display_at_loc(engine, self.location().unwrap(), styles, numbering)?
.spanned(span)
+ HElem::new(Em::new(0.3).into()).with_weak(true).pack()
+ realized;
+ HElem::new(Em::new(0.3).into()).with_weak(true).pack();
haenoe marked this conversation as resolved.
Show resolved Hide resolved

if hanging_indent.is_auto() {
let pod = Regions::one(Axes::splat(Abs::inf()), Axes::splat(false));
let size = numbering.measure(engine, styles, pod)?.into_frame().size();

let min_indent = Length { abs: size.x, em: Em::new(0.3) }.resolve(styles);
haenoe marked this conversation as resolved.
Show resolved Hide resolved
indent = indent.max(min_indent);
haenoe marked this conversation as resolved.
Show resolved Hide resolved
}

realized = numbering + realized;
}

realized = realized.styled(ParElem::set_hanging_indent(indent.into()));
haenoe marked this conversation as resolved.
Show resolved Hide resolved

Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/typst/test.typ
haenoe marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= #lorem(1000)
Binary file modified tests/ref/outline-first-line-indent.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.