Skip to content

Support for bounding box text edges - #1626

Merged
laurmaedje merged 5 commits into
typst:mainfrom
damaxwell:textbbox
Jul 17, 2023
Merged

laurmaedje merged 5 commits into
typst:mainfrom
damaxwell:textbbox

Conversation

@damaxwell

Copy link
Copy Markdown
Contributor

Closes #112.

Text in Typst has a notion of upper and lower edges, which can be specified by global font metrics (x-height, descender, etc.) In a math environment, where stuff is being laid out in a genuinely 2-d fashion, you want the upper and lower edges to be determined by the tight bounding box of the text instead so that there isn't excess space above all lower-cap words, or collisions due to descenders.

This PR addresses this by introducing two new values for a text edge, bbox-top and box-bottom, which do just what they suggest. In math layout, text is set to use these values to get the right size.

There was one additional change needed. Math layout used to use Content::layout for text, which ends up creating a FlowElem at the top level, which forgets about the baseline of its contents. But the baseline is critical for math layout, so we render a ParElem explicitly instead of generic Content when laying out a TextElem.

Before
image

After
image

Issues

  1. No caching is done for glyph bounding boxes, so there is time inefficiency. This could be added if desired, but for now it seemed like premature optimization..

  2. The parameters for text(top-edge:"...") are a bit clumsy. Already you can say things like

#set text(top-edge:"baseline", bottom-edge:"x-height")

and it's not clear to me what this is actually supposed to do. The new options compound the awkwardness. Now we have #set text(top-edge:"bbox-top"), which is a little verbose and you can say weird things like #set text(top-edge:"bbox-bottom",bottom-edge:"bbox-top:"). I'm not sure what this is supposed to do, regardless of what it actually does.

  1. A TextElem is always rendered inside of a paragraph. What is the width of a paragraph containing a TextElem inside of math layout? In the previous implementation, it was the width of the region the equation element is being laid out into. But this is a little arbitrary, and if line breaks are actually encountered, the paragraph likely overfills the bounding math element anyway since it's probably not lined up with the container's left edge. In the current implementation, TextElem inside of math simply never line breaks: it's laid out in to a region infinitely wide. This is essentially what TeX does with its \hboxs.

  2. I wasn't sure how to set the text edge style for a TextElem in math mode programmatically and yet still allow this to be overridden by an insistent user. I used Content::styled but this seems to wipe out the text edge settings even for text elements created with explicit top/bottom edges.

Tests

A number of tests images needed to be updated because text elements changed size. In all cases, the resulting spacing was closer to that of TeX, or was negligibly different from the prior image due to very small differences between bbox-top versus cap-height, or was more consistent. This last category includes things like the following behavior in the prior version: "if a y appears as a math character, it's descender affects the size of the EquationElem but if the same y appears as part of a larger TextElem, the descender is not accounted for"

@Enivex

Enivex commented Jun 30, 2023 •

Copy link
Copy Markdown
Collaborator

In that case there needs to be some mechanism to align limits vertically for consecutive operators (like \adjustlimits from mathtools in latex), because your example does not look great.

Also, in general I'm not sure using the bounding box is a great idea in many situations. You don't want spacing and position to be "too" sensitive to what the content is. Consistency is also important.

@damaxwell

damaxwell commented Jun 30, 2023 •

Copy link
Copy Markdown
Contributor Author

Well, beauty is in the eye of the beholder, I suppose. Here's LaTeX, out of the box:
image
The spacing and layout looks remarkably close to what Typst produced. Which is darn cool.

TeX (and LaTeX sitting on top of it) uses bounding boxes for determining the sizes of objects. Every glyph to TeX when doing layout is just its bounding box (page 65 of the TeXBook has a nice visual of this) and baseline along with some other font metrics. Appendix G of that book describes the math layout algorithm. The spacing parameters on page 447 in that appendix have been encoded in a modern OpenType math font in its math table. Those parameters are predicated on the text being described by tight bounding boxes. Typst had done a good job with spacing in many scenarios already because texts runs of single glyphs were treated differently by the math layout engine and were inserted not as a TextElem, but as a GlyphFragment which created a frame based on its bounding box. Now a TextElem does the same thing and we don't have an inconsistency between spacing rules for one character and for a run of two or more.

As for \adjustlimits, kinda cool, I didn't know about it. To be fair, though, that's not a feature of vanilla LaTeX, but is a 2004 feature from mathtools which is an extension of amsmath, which sits on top of LaTeX (1984). Typst still needs more work before something similar could be provided by a third party package. A better version of measure is needed that returns baseline info about its argument, not just width and height. But a better measure will come, too.

@laurmaedje

laurmaedje commented Jul 5, 2023 •

Copy link
Copy Markdown
Member

What if it was just one option that does the right thing for top-edge and bottom-edge respectively. E.g. #set text(top-edge: "bounds", bottom-edge: "bounds")?

We would still need to bikeshed the name: Off the top of my head, I can think of "bounds", "tight", "bbox", and "bounding-box".

@laurmaedje laurmaedje changed the title allow text edges to be determined by bounding boxes Support for bounding box text edges Jul 10, 2023
@damaxwell

Copy link
Copy Markdown
Contributor Author

I was away from internet for a bit there. Yes, a larger change would certainly allow a single bounds (or however you want to spell it) option. It still doesn't address the weirdness that you can have a top that is descender and a bottom that is x-height. Regardless, I'll revise with this change.

@laurmaedje

Copy link
Copy Markdown
Member

If you want, you can also change it such that top-edge only allows baseline-and-above metrics and bottom-edge baseline-and-below.

@damaxwell

damaxwell commented Jul 11, 2023 •

Copy link
Copy Markdown
Contributor Author

This is updated. text(top-edge:) accepts "bounding-box" or a vertical metric of "baseline" or above. A similar change was made to bottom-edge. I have no strong opinions on how to spell "bounding-box". It's wordy, but explicit. I kinda like "tight" as an alternative.

Comment thread crates/typst-library/src/math/ctx.rs Resolved
@laurmaedje

Copy link
Copy Markdown
Member

Seems like we both pushed here a bit interleaved. But the diff looks fine. Is this ready to merge?

@damaxwell

Copy link
Copy Markdown
Contributor Author

There was a test image that was out of date after the recent merges to main. I had rebased after your update and then fixed the image. This all LGTM.

@laurmaedje
laurmaedje merged commit 7a1cd36 into typst:main Jul 17, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Wonderful, thanks for working on this!

@damaxwell
damaxwell deleted the textbbox branch July 17, 2023 16:45
@damaxwell damaxwell mentioned this pull request Aug 15, 2023
1 task done
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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.

Math limit formatting overlap

3 participants