Skip to content

Line numbers - #4516

Merged
laurmaedje merged 63 commits into
mainfrom
line-numbers
Aug 29, 2024
Merged

laurmaedje merged 63 commits into
mainfrom
line-numbers

Conversation

@PgBiel

@PgBiel PgBiel commented Jul 7, 2024 •

Copy link
Copy Markdown
Contributor

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

  • You will be able to enable (or locally disable/customize) line numbers with a set rule, such as #set par.line(numbering: "1") (bikesheddable).
  • You'd also be able to control alignment of line numbers, spacing between line numbers and the text, and so on.
  • In principle, the line numbers go in the left or right margin. This should also be configurable.

Here's what we have so far with the current code: the Typst code below

#set par.line(numbering: "1")
#show figure: set par.line(numbering: none)

#align(center, text(52pt)[Lorem Ipsum])

#text(32pt, grid(
  columns: (1fr,)*3,
  [Some\ Author],
  [Authorus\ Johnson],
  [Authorus\ Mister\ Johnson]
))

#lorem(100)

#figure[
  No line numbers here\
  No line numbers here
]

#lorem(100)

produces (note that this isn't final)

sample

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 y to 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 out counter(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.line set 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 to none), 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

  1. There are two new elements: ParLine (exposed as par.line to the user), which is only used for styles (and thus isn't showable), and ParLineMarker, which isn't exposed to the user and is only used for Show.
    • In principle, we'd ideally only have one, but I didn't want the user to be able to #show par.line as that'd result in some unexpected behavior, since we don't wrap entire lines in ParLine, 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).
  2. Within commit in inline paragraph layout, we add a ParLineMarker to the start of each line's frame.
    • It saves the current value of par.line.numbering. If it is none, the par line marker is not placed, and the line isn't numbered.
  3. Within a flow (flow.rs):
    1. Created a new flow item, 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 the flow.items vector right after the parent of its referenced line so that we may correctly determine its final y position in finish_region (by just adding parent's final y position to the relative height of the line number).
    2. Created 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 relative y position is added to the counter. When we find the tag, we add its own y position 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.
    3. Created 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.
      • There is a similar deduplication procedure in finish_region later, so I'm probably gonna be simplifying this still.
      • Added calls to this function within handle_item, layout_block etc.
    4. Within 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.
      • This still needs some fixing since line numbers may be placed out of order due to #place.
    5. Thus, we have line numbers.

TODO

  • Fix line number ordering with #place
  • Adjust to differing line heights
  • Add line number alignment option (default to right alignment)
  • Add line number spacing option
  • Allow picking which margin (left or right, start or end) the line numbers appear in
  • Add proper support for line numbers in 2-column documents
  • Add tests
  • Per-page number reset
  • Auto line number alignment based on (opposite to) margin
  • Prohibit intersecting line numbers (to our best ability)
  • Cleanup

Comment thread crates/typst/src/model/par.rs Resolved
@Dherse

Dherse commented Jul 8, 2024

Copy link
Copy Markdown
Contributor

That's a surprisingly small implementation, awesome 🎉

@laurmaedje

laurmaedje commented Jul 8, 2024 •

Copy link
Copy Markdown
Member

Could we do the frame search fully in finish_region and do nothing before? That would simplify things quite a lot. I think then we wouldn't even need a FlowItem.

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Jul 14, 2024
Comment thread crates/typst/src/layout/flow.rs Resolved
Comment thread crates/typst/src/model/par.rs Resolved
Comment thread crates/typst/src/layout/flow.rs Resolved
Comment thread crates/typst/src/layout/flow.rs Resolved
Comment thread crates/typst/src/layout/flow.rs Resolved
Comment thread crates/typst/src/layout/flow.rs Resolved
Comment thread crates/typst/src/layout/inline/line.rs Resolved
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Jul 15, 2024
PgBiel added 5 commits July 16, 2024 20:11
- 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.
@PgBiel
PgBiel marked this pull request as ready for review August 28, 2024 02:02
Comment thread crates/typst/src/model/par.rs Resolved
Comment thread crates/typst/src/model/par.rs Resolved
@laurmaedje
laurmaedje added this pull request to the merge queue Aug 29, 2024
Merged via the queue into main with commit e6cdcc5 Aug 29, 2024
@PgBiel
PgBiel deleted the line-numbers branch August 29, 2024 23:16
@daskol

daskol commented Sep 19, 2024

Copy link
Copy Markdown

It's great to see support for line numbers. I wanted to try it out and built typst from commit 92ec566. Everything is great, but how can I customize the font? The following rule doesn't work.

show par.line: set text(size: 6pt)

@PgBiel

PgBiel commented Sep 19, 2024

Copy link
Copy Markdown
Contributor Author

You can use #set par.line(numbering: n => text(size: 6pt)[#n]). par.line is a "virtual" element in the sense that it only exists for #set par.line(numbering: ..., /* and other options */) to work, but you can't do anything else with it.

@daskol

daskol commented Sep 20, 2024

Copy link
Copy Markdown

I see. Thank you.

@joshuabeny1999

Copy link
Copy Markdown

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...

@laurmaedje

Copy link
Copy Markdown
Member

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.

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.

Line Numbers

6 participants