Skip to content

Let base be stretch()-able to accommodate wide attachments - #4569

Closed
Leedehai wants to merge 5 commits into
typst:mainfrom
Leedehai:attach-long
Closed

Leedehai wants to merge 5 commits into
typst:mainfrom
Leedehai:attach-long

Conversation

@Leedehai

Copy link
Copy Markdown
Contributor

No description provided.

@laurmaedje

Copy link
Copy Markdown
Member

Looking at the new reference image, it may be that this is actually only desirable for a subset of symbols. For arrows it seems very sensible, but for = it looks weird.

cc: @Enivex due to #1304 (comment)

@Enivex

Enivex commented Jul 17, 2024 •

Copy link
Copy Markdown
Collaborator

Looking at the new reference image, it may be that this is actually only desirable for a subset of symbols. For arrows it seems very sensible, but for = it looks weird.

cc: @Enivex due to #1304 (comment)

It makes more sense for some symbols than others, but I'd argue that this should be opt-in regardless. Perhaps through something like stretch().

Whether the behavior is desirable is both subjective and situational.

@Leedehai
Leedehai marked this pull request as draft July 18, 2024 14:28
@Leedehai
Leedehai marked this pull request as ready for review July 19, 2024 03:10
@Leedehai

Copy link
Copy Markdown
Contributor Author

@laurmaedje Introduced stretch(); please see the reference images.

@Enivex

Enivex commented Jul 19, 2024 •

Copy link
Copy Markdown
Collaborator

@laurmaedje Introduced stretch(); please see the reference images.

The image good to my eyes, but maybe you should also add tests with absolute lengths as well? Like 1cm or 2em, not just %-based.

Edit: And length < 100%. Maybe I'd like to scale an arrow, but not all the way to the length of the attachment.

@Leedehai Leedehai changed the title Let base be stretchable in width to accommodate wide attachments Let base be stretch()-able to accommodate wide attachments Jul 20, 2024
@Leedehai Leedehai changed the title Let base be stretch()-able to accommodate wide attachments Let base be stretch()-able to accommodate wide attachments Jul 20, 2024
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Jul 22, 2024
Comment thread crates/typst/src/math/attach.rs Resolved
Comment thread crates/typst/src/math/attach.rs Resolved
Comment thread crates/typst/src/math/fragment.rs Resolved

@mkorje mkorje left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add an element function to allow stretching vertically as well, and so we'd maybe have math.hstretch and math.vstretch. Whilst math.lr kind of does this, its really just for delimiters.

Adding an axis argument or something similar would make things a bit annoying I think - though maybe this could be done automatically by searching for the glyph in both the horizontal and vertical variants/constructions. I'm not sure if there exists any glyph that have both horizontal and vertical variants, which would cause problems then.

Comment on lines +167 to +183
/// Stretches a base to accommodate wide top or bottom attachments.
///
/// ```example
/// $ H stretch(=)^"define" U + p V $
/// $ P => Q stretch(=>, width: #200%) R $
/// $ V stretch(->, width: #(100% + 1.5em))^("surjection") ZZ $
/// ```
#[elem(LayoutMath)]
pub struct StretchElem {
/// The base to stretch.
#[required]
pub body: Content,

/// The width of the base to stretch to, relative to the maximum width of
/// the base and the attachments (if any).
pub width: Smart<Rel<Length>>,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be generalising the StretchElem since you might just want to stretch a glyph like an arrow, but have no intention of adding attachments; and so the docs should reflect this.

stretch(equiv)^(forall i)_"Chern-Weil" R
stretch(equiv, width: #(120% + 2mm))^(forall i)_"Chern-Weil" S \
P &-> Q
stretch(->) R // Not stretched: no width specifid and no attachments

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stretch(->) R // Not stretched: no width specifid and no attachments
stretch(->) R // Not stretched: no width specified and no attachments

Comment on lines +185 to +189
#[derive(Clone, Copy, Debug)]
pub struct StretchInfo {
from: Abs,
to: Smart<Rel<Length>>,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the change in impl LayoutMath for Packed<AttachElem> is made then we can get rid of this struct as only to needs to be passed around on the GlyphFragment. The from field will always just be the fragment's width then.

fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> {
let mut fragment = ctx.layout_into_fragment(self.body(), styles)?;
let stretch = StretchInfo { from: fragment.width(), to: self.width(styles) };
fragment.set_stretch(Some(stretch));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any point in adding stretch information to the frame before we check if the fragment can actually be stretched?

let b = layout!(b, sub_style_chain)?;
if let Some(stretch) = base.stretch() {
let relative_to_width =
measure!(t, width).max(measure!(b, width)).max(stretch.from);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think including stretch.from here makes interactions between math.stretch and math.attach a bit inconsistent. For example, a long attachment can cause the base to be stretched, but a shorter one doesn't cause the base to be compressed. If having width: auto with attachments means sizing the base relative to the attachments, then it should go both ways IMO.

#let base = math.stretch($=$, width: 250%)

$ // This gets shortened.
  stretch(base, width: #50%)
  // But this doesn't.
  stretch(base)^"a" $

Comment on lines +519 to +525
fn stretch_fragment(
ctx: &mut MathContext,
styles: StyleChain,
stretch: StretchInfo,
relative_to_width: Abs,
fragment: MathFragment,
) -> MathFragment {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if we're passing in the fragment already, we may as well pass it with a mutable reference and instead not return anything.

@laurmaedje

Copy link
Copy Markdown
Member

@mkorje Thanks for the review!

I'm not sure if there exists any glyph that have both horizontal and vertical variants, which would cause problems then.

I can't really think of a glyph where that would make sense. I feel like the hstretch/vstretch naming doesn't feel Typst-y, so I'd prefer to avoid it.

@mkorje

mkorje commented Sep 6, 2024

Copy link
Copy Markdown
Collaborator

Me neither, though I guess there's nothing stopping a font from including such things for a glyph. It wouldn't hurt then to have an axis parameter (or something like that) which is auto, just in case (but this is probably overkill).

And yeah I agree that it isn't very Typst like. Adding the word horizontal or vertical felt too verbose and I couldn't think of a nice shortening of both words.

@Enivex

Enivex commented Sep 24, 2024

Copy link
Copy Markdown
Collaborator

Is there anything blocking this?

@Leedehai

Copy link
Copy Markdown
Contributor Author

Hi - I became a bit busy recently so I'm fine with someone taking this work and create a new PR (hence the drop in my recent PR counts). I could come back to this later if that's not already done. Also, is a 0.12 release imminent?

@mkorje

mkorje commented Sep 25, 2024

Copy link
Copy Markdown
Collaborator

I'd be happy to continue on this PR's work. It'd be nice to get this into 0.12.

@laurmaedje

Copy link
Copy Markdown
Member

Closing in favor of #5030. Thanks all!

@laurmaedje laurmaedje closed this Sep 25, 2024
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants