diff --git a/library/src/layout/enum.rs b/library/src/layout/enum.rs index a0b23945..0a11a751 100644 --- a/library/src/layout/enum.rs +++ b/library/src/layout/enum.rs @@ -147,6 +147,30 @@ pub struct EnumElem { /// If set to `{auto}`, uses the spacing [below blocks]($func/block.below). pub spacing: Smart, + /// The horizontal alignment that enum numbers should have. + /// + /// By default, this is set to `{end}`, which aligns enum numbers + /// towards end of the current text direction (in left-to-right script, + /// for example, this is the same as `{right}`). The choice of `{end}` + /// for horizontal alignment of enum numbers is usually preferred over + /// `{start}`, as numbers then grow away from the text instead of towards + /// it, avoiding certain visual issues. This option lets you override this + /// behavior, however. + /// + /// ````example + /// #set enum(number-align: start) + /// + /// Here are some powers of two: + /// 1. One + /// 2. Two + /// 4. Four + /// 8. Eight + /// 16. Sixteen + /// 32. Thirty two + /// ```` + #[default(HorizontalAlign(GenAlign::End))] + pub number_align: HorizontalAlign, + /// The numbered list's items. /// /// When using the enum syntax, adjacent items are automatically collected @@ -191,6 +215,13 @@ impl Layout for EnumElem { let mut parents = self.parents(styles); let full = self.full(styles); + // Horizontally align based on the given respective parameter. + // Vertically align to the top to avoid inheriting 'horizon' or + // 'bottom' alignment from the context and having the number be + // displaced in relation to the item it refers to. + let number_align: Axes> = + Axes::new(self.number_align(styles).into(), Align::Top.into()).map(Some); + for item in self.children() { number = item.number(styles).unwrap_or(number); @@ -208,9 +239,13 @@ impl Layout for EnumElem { } }; + // Disable overhang as a workaround to end-aligned dots glitching + // and decreasing spacing between numbers and items. + let resolved = + resolved.aligned(number_align).styled(TextElem::set_overhang(false)); + cells.push(Content::empty()); - // avoid '#set align' interference with the enum - cells.push(resolved.aligned(Align::LEFT_TOP.into())); + cells.push(resolved); cells.push(Content::empty()); cells.push(item.body().styled(Self::set_parents(Parent(number)))); number = number.saturating_add(1); diff --git a/tests/ref/compiler/construct.png b/tests/ref/compiler/construct.png index 3dac041f..6ce83371 100644 Binary files a/tests/ref/compiler/construct.png and b/tests/ref/compiler/construct.png differ diff --git a/tests/ref/layout/enum-align.png b/tests/ref/layout/enum-align.png new file mode 100644 index 00000000..5f3f66f9 Binary files /dev/null and b/tests/ref/layout/enum-align.png differ diff --git a/tests/ref/layout/enum-numbering.png b/tests/ref/layout/enum-numbering.png index 7c39da4f..28755dcb 100644 Binary files a/tests/ref/layout/enum-numbering.png and b/tests/ref/layout/enum-numbering.png differ diff --git a/tests/ref/layout/enum.png b/tests/ref/layout/enum.png index 94a9ed51..62f1e4ab 100644 Binary files a/tests/ref/layout/enum.png and b/tests/ref/layout/enum.png differ diff --git a/tests/typ/layout/enum-align.typ b/tests/typ/layout/enum-align.typ new file mode 100644 index 00000000..9d11235c --- /dev/null +++ b/tests/typ/layout/enum-align.typ @@ -0,0 +1,41 @@ +// Test the alignment of enum numbers. + +--- +// Alignment shouldn't affect number +#set align(horizon) + ++ ABCDEF\ GHIJKL\ MNOPQR + + INNER\ INNER\ INNER ++ BACK\ HERE + +--- +// Enum number alignment should be 'end' by default +1. a +10. b +100. c + +#set enum(number-align: start) +1. a +8. b +16. c + +--- +// Number align option should not be affected by the context +#set align(center) +#set enum(number-align: start) + +4. c +8. d +16. e\ f + 2. f\ g + 32. g + 64. h + +--- +// Test valid number align values (horizontal) +#set enum(number-align: start) +#set enum(number-align: end) +#set enum(number-align: left) +#set enum(number-align: right) +// Error: 25-28 alignment must be horizontal +#set enum(number-align: top) diff --git a/tests/typ/layout/enum.typ b/tests/typ/layout/enum.typ index a90e1896..f9fe2648 100644 --- a/tests/typ/layout/enum.typ +++ b/tests/typ/layout/enum.typ @@ -46,11 +46,3 @@ a + 0. [Second], enum.item(5)[Fifth] ) - ---- -// Alignment shouldn't affect number -#set align(horizon) - -+ ABCDEF\ GHIJKL\ MNOPQR - + INNER\ INNER\ INNER -+ BACK\ HERE