Repository navigation
Support for bounding box text edges - #1626
Conversation
|
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. |
|
What if it was just one option that does the right thing for top-edge and bottom-edge respectively. E.g. We would still need to bikeshed the name: Off the top of my head, I can think of |
|
I was away from internet for a bit there. Yes, a larger change would certainly allow a single |
|
If you want, you can also change it such that top-edge only allows baseline-and-above metrics and bottom-edge baseline-and-below. |
|
This is updated. |
|
Seems like we both pushed here a bit interleaved. But the diff looks fine. Is this ready to merge? |
|
There was a test image that was out of date after the recent merges to |
|
Wonderful, thanks for working on this! |

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-topandbox-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::layoutfor text, which ends up creating aFlowElemat the top level, which forgets about the baseline of its contents. But the baseline is critical for math layout, so we render aParElemexplicitly instead of genericContentwhen laying out aTextElem.Before

After

Issues
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..
The parameters for
text(top-edge:"...")are a bit clumsy. Already you can say things likeand 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.A
TextElemis always rendered inside of a paragraph. What is the width of a paragraph containing aTextEleminside 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,TextEleminside of math simply never line breaks: it's laid out in to a region infinitely wide. This is essentially whatTeXdoes with its\hboxs.I wasn't sure how to set the text edge style for a
TextElemin math mode programmatically and yet still allow this to be overridden by an insistent user. I usedContent::styledbut 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 betweenbbox-topversuscap-height, or was more consistent. This last category includes things like the following behavior in the prior version: "if ayappears as a math character, it's descender affects the size of theEquationElembut if the sameyappears as part of a largerTextElem, the descender is not accounted for"