Repository navigation
Line numbers - #4516
Line numbers#4516
Conversation
Still need to subtract the margin
and other funny jokes you can tell yourself (this is still off by some epsilon)
but I guess that's not the problem maybe the epsilon doesn't come from the margin?
- No need to invoke introspection anymore - Seems like we need to offset by local leading (needs fix)
Our prototype is minimally good now
|
That's a surprisingly small implementation, awesome 🎉 |
|
Could we do the frame search fully in |
- However, this breaks columns even further (duplicate line numbers) as we no longer run the rootable check in `handle_block` right before collecting par lines.
Ideally, no intersection, unless a zero-height frame is used
in principle seems to be unnecessary
|
It's great to see support for line numbers. I wanted to try it out and built show par.line: set text(size: 6pt) |
|
You can use |
|
I see. Thank you. |
|
Nice work. Would need this for an assigment in my master. How can i use it now on macOS? Or when the next release will be? The last one was in May... |
|
The next release will be fairly soon. If you want to use the development version, you need to install Rust and then follow the instructions in the README. |
Closes #352
Adds line numbers.
The feature is still WIP (see the TODO at the bottom). Opening this PR to share a bit of the initial progress for now.Overview
#set par.line(numbering: "1")(bikesheddable).Here's what we have so far with the current code: the Typst code below
produces (note that this isn't final)
Implementation highlights
TL;DR
We basically perform a "manual query" of line elements while laying out the root flow of the document, by recursively searching inside frames and subframes produced by layout. On each searched subframe, we add its relative
yto a counter. When we find the line, we save the final value of the counter, and thus we will have the height of the line relative to the parent frame. We then lay outcounter(par.line).update() + counter(par.line).display()on the left margin at each detected line number height (we have the relative heights to each parent frame, so we lay out each parent frame and then add the parent frame's final height to the line numbers' relative heights).This "manual query" mechanism could potentially be reused for other features, such as margin notes, paragraph numbers, and so on. Maybe even some more general mechanism of absolute alignment between elements (e.g. easily place an element at the same height as another). But we'd need more exploration to know.
We also synthesize the
par.lineset rules in the queried line elements, so that you can#set par.line(numbering: ...)somewhere deep within your document in order to e.g. have some lines not be numbered (by setting numbering tonone), or have them produce different numbering from the rest.A similar mechanism could be potentially used to enable local styling of footnotes (see #1622), but some design discussion would be needed to determine whether that'd be desirable behavior.
Details
ParLine(exposed aspar.lineto the user), which is only used for styles (and thus isn'tshowable), andParLineMarker, which isn't exposed to the user and is only used forShow.#show par.lineas that'd result in some unexpected behavior, since we don't wrap entire lines inParLine, but rather just add a marker at the start of each line, so it wouldn't be replacing anything. But we can change this (and it's easier to replace 2 elements with 1 than go the other way).commitin inline paragraph layout, we add aParLineMarkerto the start of each line's frame.par.line.numbering. If it isnone, the par line marker is not placed, and the line isn't numbered.flow.rs):LineNumber. It holds a frame (counter update + counter display) and its height (relative to the top-level flow element the referenced line is contained in). As an invariant, it must always appear in theflow.itemsvector right after the parent of its referenced line so that we may correctly determine its finalyposition infinish_region(by just adding parent's finalyposition to the relative height of the line number).collect_par_lines, which searches for a tag element containing a par line marker. It has a counter (relative height of the searched line) which starts at zero. On each searched subframe, its relativeyposition is added to the counter. When we find the tag, we add its ownyposition to the counter, and that becomes the final relative height of the searched line (and thus of the line number) within the frame we were initially searching for lines.handle_par_lines, which takes a vector of searched lines and their relative heights and adds the corresponding line numbers into the flow as flow items. Must be called right after the parent was pushed as a flow item. This function has a deduplication procedure (since many lines may have the same height) which basically sorts the found lines by relative height and then excludes from numbering any lines that have less than 1pt of height difference from a numbered line.finish_regionlater, so I'm probably gonna be simplifying this still.handle_item,layout_blocketc.finish_region, we save the height of each flow item we push to the output frame, and then, as soon as we find line numbers, we add their relative heights to the latest saved height, as that was the height of the corresponding lines' parent frame. Then we push the line numbers to the output frame, with another deduplication mechanism since the initial deduplication procedure only runs within each parent frame, but not between frames.#place.TODO
#place