You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#context page.margin can't get margin from an ad-hoc #page #3671
Not sure if this is really a bug, but seems to be at least a shortcoming or inconsistency of some sort, so I didn't want this to pass by unnoticed. Some packages, such as hydra, depend on knowing the page margin for certain calculations, and they can't switch to fetching the page margin through context unless it's fully reliable.
Basically, when using #set page(margin: 5pt), #context page.margin will properly report 5pt.
However, using
// initial value, for example#setpage(margin: 5pt)
#page(margin: 10pt)[#contextpage.margin]
reports 5pt, when that is not the current page margin.
Of course, it makes sense in a way, if we consider how things work in general. But I think it'd be valid for #page(...) to override the stylechain similarly to how #text(...) and #par(...) do it, i.e. the following example works properly:
// Correctly reports 0.65em / default value / current value#contextpar.leading// Correctly reports 5em#par(leading: 5em)[#contextpar.leading]
As a solution, we could try to override the stylechain within #page[...] calls with the updated fields. Not sure of which implications that might have though.
One possible fix is to always resolve page styles when laying out a page, e.g. (the code below would be shorter in practice, as we'd have some generic implementation of this in #[elem])
diff --git a/crates/typst/src/layout/page.rs b/crates/typst/src/layout/page.rs
index f6221887..27ca0154 100644
--- a/crates/typst/src/layout/page.rs+++ b/crates/typst/src/layout/page.rs@@ -9,7 +9,7 @@ use crate::diag::{bail, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, AutoValue, Cast, Content, Context, Dict, Fold, Func, NativeElement,
- Packed, Resolve, Smart, StyleChain, Value,+ Packed, Resolve, Smart, StyleChain, Styles, Value,
};
use crate::introspection::{Counter, CounterDisplayElem, CounterKey, ManualPageCounter};
use crate::layout::{
@@ -384,6 +384,33 @@ impl Packed<PageElem> {
.spanned(self.span());
}
+ // Ensure the child is aware of the page's properties.+ let page_styles = {+ let mut page_styles = Styles::new();+ page_styles.set(PageElem::set_width(self.width(styles).map(Length::from)));+ page_styles.set(PageElem::set_height(self.height(styles).map(Length::from)));+ page_styles.set(PageElem::set_flipped(self.flipped(styles)));+ page_styles.set(PageElem::set_margin(self.margin(styles)));+ page_styles.set(PageElem::set_binding(self.binding(styles)));+ page_styles.set(PageElem::set_columns(self.columns(styles)));+ page_styles.set(PageElem::set_fill(self.fill(styles).clone()));+ page_styles.set(PageElem::set_numbering(self.numbering(styles).clone()));+ page_styles.set(PageElem::set_number_align(self.number_align(styles)));+ page_styles.set(PageElem::set_header(self.header(styles).clone()));+ page_styles.set(PageElem::set_header_ascent(+ self.header_ascent(styles).map(Length::from),+ ));+ page_styles.set(PageElem::set_footer(self.footer(styles).clone()));+ page_styles.set(PageElem::set_footer_descent(+ self.footer_descent(styles).map(Length::from),+ ));+ page_styles.set(PageElem::set_background(self.background(styles).clone()));+ page_styles.set(PageElem::set_foreground(self.foreground(styles).clone()));+ page_styles+ };+ let styles = styles.chain(&page_styles);+
let area = size - margin.sum_by_axis();
let mut regions = Regions::repeat(area, area.map(Abs::is_finite));
regions.root = true;
This also fixes #context page.margin returning auto, and is probably the preferred solution (I can verify it solves this issue as well).
We could also override Construct for PageElem by taking the generated code and modifying body to apply .styled_with_map, similarly to ParElem, but that led to several regressions (likely because PageElem might be constructed internally too), so that doesn't seem like a viable solution.
changed the title [-][0.11.0-rc1] `#context page.margin` can't get margin from an ad-hoc `#page`[/-][+]`#context page.margin` can't get margin from an ad-hoc `#page`[/+]on Mar 18, 2024
Are there any other cases where this might be relevant? One thing I ran into, is page.binding which I assume is fixed by this too. But what about other elements?
This GitHub issue in particular is a bit too narrow, though other elements also suffer from not having fully resolved properties available through the style chain, such as text - I believe text.dir starts as auto, for example.
Do you mean that they can be auto? They're only auto if explicitly set, and here, unlike with page.margin, this is actually a meaningful value instead of meaning "infer from other parameters".
In general, it seems that we would only want to "resolve" auto when it's meant to be a placeholder for a value that is known completely from looking at other parameters. A perhaps crazy suggestion is adding yet another keyword bottom type, a la auto/none, which is used for those cases. But I don't think that would carry its own weight, I just think auto was a great idea alongside none but could sometimes need more context.
Yeah, but they lead to auto if that definition is in a set-ruled, not if it's coming from an argument.
page(width: 14.2024cm, context page.width)
→ e.g. 595.20pt (21cm) or even auto if defined so by a preceding set page(width: auto)
page(width: auto, context page.width)
→ Not (per sé) auto
In general, it seems that we would only want to "resolve" auto when it's meant to be a placeholder […]
That was also my comprehension of the current discourse. But, as it has been discussed in parallel on Discord #contributors, may it be that you're mixing this up with the auto auto-resolution related to #3636, despite being independent?
If it's about my thoughts on that, I'll clarify that on the other issue.
Activity
PgBiel commentedon Mar 15, 2024
One possible fix is to always resolve page styles when laying out a page, e.g. (the code below would be shorter in practice, as we'd have some generic implementation of this in
#[elem])This also fixes
#context page.marginreturningauto, and is probably the preferred solution (I can verify it solves this issue as well).We could also override
ConstructforPageElemby taking the generated code and modifyingbodyto apply.styled_with_map, similarly toParElem, but that led to several regressions (likely becausePageElemmight be constructed internally too), so that doesn't seem like a viable solution.[-][0.11.0-rc1] `#context page.margin` can't get margin from an ad-hoc `#page`[/-][+]`#context page.margin` can't get margin from an ad-hoc `#page`[/+]tingerrr commentedon Mar 19, 2024
Are there any other cases where this might be relevant? One thing I ran into, is page.binding which I assume is fixed by this too. But what about other elements?
PgBiel commentedon Mar 19, 2024
This GitHub issue in particular is a bit too narrow, though other elements also suffer from not having fully resolved properties available through the style chain, such as
text- I believetext.dirstarts asauto, for example.l0wx commentedon Mar 28, 2024
It's the same with
page.width&.height.tingerrr commentedon Mar 31, 2024
Do you mean that they can be
auto? They're onlyautoif explicitly set, and here, unlike withpage.margin, this is actually a meaningful value instead of meaning "infer from other parameters".In general, it seems that we would only want to "resolve" auto when it's meant to be a placeholder for a value that is known completely from looking at other parameters. A perhaps crazy suggestion is adding yet another keyword bottom type, a la
auto/none, which is used for those cases. But I don't think that would carry its own weight, I just thinkautowas a great idea alongsidenonebut could sometimes need more context.l0wx commentedon Apr 1, 2024
@tingerrr
Yeah, but they lead to
autoif that definition is in aset-ruled, not if it's coming from an argument.→ e.g.
595.20pt(21cm) or evenautoif defined so by a precedingset page(width: auto)→ Not (per sé)
autoThat was also my comprehension of the current discourse. But, as it has been discussed in parallel on Discord #contributors, may it be that you're mixing this up with the
autoauto-resolution related to #3636, despite being independent?If it's about my thoughts on that, I'll clarify that on the other issue.