Skip to content

Commit

Permalink
Add hanging-indent option to heading
Browse files Browse the repository at this point in the history
  • Loading branch information
haenoe committed May 2, 2024
1 parent 69dcc89 commit cf2a02c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
35 changes: 29 additions & 6 deletions crates/typst/src/model/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{
elem, Content, NativeElement, Packed, Show, ShowSet, Smart, StyleChain, Styles,
Synthesize,
Synthesize, Resolve
};
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{BlockElem, Em, HElem, VElem};
use crate::model::{Numbering, Outlinable, Refable, Supplement};
use crate::layout::{BlockElem, Em, HElem, VElem, LayoutMultiple, Axes, Abs, Regions, Length};
use crate::model::{Numbering, Outlinable, Refable, Supplement, ParElem};
use crate::text::{FontWeight, LocalName, SpaceElem, TextElem, TextSize};
use crate::util::NonZeroExt;

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

#[default(Smart::Auto)]
pub hanging_indent: Smart<Length>,

/// The heading's title.
#[required]
pub body: Content,
Expand Down Expand Up @@ -203,13 +206,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) {
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();

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);
indent = indent.max(min_indent);
}

realized = numbering + realized;
}

realized = realized.styled(ParElem::set_hanging_indent(indent.into()));

Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/typst/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= #lorem(1000)

0 comments on commit cf2a02c

Please sign in to comment.