From e6739ecc2f78f023b7b6b1729983af30f657e6dd Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 29 May 2024 15:17:05 +0200 Subject: [PATCH] Fix figure centering (#4276) Co-authored-by: PgBiel <9021226+PgBiel@users.noreply.github.com> --- crates/typst/src/model/figure.rs | 15 +++++++-------- tests/ref/figure-align.png | Bin 0 -> 926 bytes tests/suite/model/figure.typ | 8 ++++++++ 3 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 tests/ref/figure-align.png diff --git a/crates/typst/src/model/figure.rs b/crates/typst/src/model/figure.rs index 30708711c..a21e5af5e 100644 --- a/crates/typst/src/model/figure.rs +++ b/crates/typst/src/model/figure.rs @@ -14,8 +14,8 @@ use crate::introspection::{ Count, Counter, CounterKey, CounterUpdate, Locatable, Location, }; use crate::layout::{ - Alignment, BlockElem, Em, HAlignment, Length, OuterVAlignment, PlaceElem, VAlignment, - VElem, + AlignElem, Alignment, BlockElem, Em, HAlignment, Length, OuterVAlignment, PlaceElem, + VAlignment, VElem, }; use crate::model::{Numbering, NumberingPattern, Outlinable, Refable, Supplement}; use crate::text::{Lang, Region, TextElem}; @@ -317,11 +317,7 @@ impl Show for Packed { } // Wrap the contents in a block. - realized = BlockElem::new() - .with_body(Some(realized)) - .pack() - .spanned(self.span()) - .aligned(Alignment::CENTER); + realized = BlockElem::new().with_body(Some(realized)).pack().spanned(self.span()); // Wrap in a float. if let Some(align) = self.placement(styles) { @@ -340,7 +336,10 @@ impl ShowSet for Packed { fn show_set(&self, _: StyleChain) -> Styles { // Still allows breakable figures with // `show figure: set block(breakable: true)`. - BlockElem::set_breakable(false).wrap().into() + let mut map = Styles::new(); + map.set(BlockElem::set_breakable(false)); + map.set(AlignElem::set_alignment(Alignment::CENTER)); + map } } diff --git a/tests/ref/figure-align.png b/tests/ref/figure-align.png new file mode 100644 index 0000000000000000000000000000000000000000..be287161998589e585f6a04526021dba5ba3174f GIT binary patch literal 926 zcmV;P17ZA$P)nxs(xtVsRzP5YZCRveLriKyAlz{f4U;WOuDc`ekm$Z~ zU~+Y$;9)HF%n-f9g6IZ%w1lhkAf$267XgO08lumdclOP^+lNMDh3MXPT8=b{0AL|=;$pDpV=;B|mBpd#ts_e@5#bf4u z1N-Yn4ys2Xs8n99{mLaxE(yM0;lD2d!0eB6;==Y#0IiqE4CnUic9YIP5hBAY-L7%*;i-*0b%6kDxGal{2^+T$4#^tF`to~c-?2)Q zOM>qXd~{{1U@0FZ0p_3LISHmn)%p(9BUQ<=7Cl0ZA^F9Cw>CMFu2NM1N z>3!TAt>CsUrwG8LfHmr6(SCl>1ORF~$7jz~0f{yPiE^$p%I&2K!Kt|CEMGElOnvaI zeDq2wCc}YCH%!_HfZ9Ub1+-`RL6LDXYS;Th^4?O{s^%$R%HBN7*6XLMn6YWpT?cxb zlo8IGy9jGPOBI42`8wm)P#$m2v^G=dFhtu`pc;pj-1nyj#f`=U_RX z6#haQ`2CTVNrQO-j;y0*puYrDUU=7;;bLEUplB?Cp|n|Z zn4f=oIQp0XY}j-Z4&``0E!?r6;X>7R~Cqu0Z+jmf3O8OQvOXF6S+1=d0Xy9 z+57)cS$z}G)gRvJ%vRf8x?m}g?Vj1UE3l(1T2f|;Idipnwk4h8ND(yt&a#t% +--- figure-align --- +#show figure: set align(start) +#figure( + rect[This is \ left], + caption: [Start-aligned] +) + --- figure-table --- // Testing figures with tables. #figure( @@ -37,6 +44,7 @@ We can clearly see that @fig-cylinder and --- figure-theorem --- // Testing show rules with figures with a simple theorem display #show figure.where(kind: "theorem"): it => { + set align(start) let name = none if not it.caption == none { name = [ #emph(it.caption.body)]