Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter to select pages to be exported by CLI #4039

Merged
merged 23 commits into from May 10, 2024

Conversation

PgBiel
Copy link
Contributor

@PgBiel PgBiel commented Apr 30, 2024

Closes #3095

Adds the --pages parameter to typst compile and typst watch which allows selecting which pages should be exported by the CLI. Supports all export formats (PDF, PNG, SVG).

  • Syntax:

    • Simple: typst compile --pages 5,6,11 a.typ a.pdf to export a PDF with just pages 5, 6, 11.
    • Ranges: typst watch --pages 11-13,15- a.typ a-{n}.png to export pages 11 to 13 (inclusive), as well as any pages after page 15.
  • Implementation:

    • Had to refactor typst-pdf in several places to consider non-exported pages. Changed ctx.pages to Vec<Option<EncodedPage>> to preserve the relation between indices and real page numbers.
    • For PNG and SVG, the implementation was trivial, since each page is a separate exported file.
  • Decisions:

    • (PDF) Should non-exported headings be excluded from PDF bookmarks? Currently, I have excluded them, as this seems to make sense (otherwise they would be broken links).
    • (PDF) The PDF's xmp.num_pages metadata should only contain the amount of exported pages (and should thus always correspond to the number of pages in the actual PDF, not necessarily in the original Typst document), correct? (I have assumed so for now)
    • (PNG/SVG) When using exported-name-{n}.png (for example), should {n} correspond to the real page number in the original typst document? (i.e. with --pages 5,6,7 you'd get name-5.png, name-6.png and name-7.png instead of name-1.png, name-2.png and name-3.png) I've assumed so for now as well, as I'd say this would be expected.
  • TODO:

    • Support ranges in --pages
    • Recheck PDF changes
    • Undraft

Copy link
Member

@laurmaedje laurmaedje left a comment

Choose a reason for hiding this comment

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

Syntax looks good. It should be the same as in the web app.

When using exported-name-{n}.png (for example), should {n} correspond to the real page number in the original typst document? (i.e. with --pages 5,6,7 you'd get name-5.png, name-6.png and name-7.png instead of name-1.png, name-2.png and name-3.png) I've assumed so for now as well, as I'd say this would be expected.

I agree that it should be name-5.png etc.

crates/typst-pdf/src/page.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/page.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/outline.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/outline.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/lib.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/lib.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/lib.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/lib.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/lib.rs Outdated Show resolved Hide resolved
@PgBiel PgBiel marked this pull request as ready for review May 4, 2024 04:43
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label May 6, 2024
crates/typst-cli/src/args.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/args.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/args.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/compile.rs Outdated Show resolved Hide resolved
crates/typst-pdf/src/outline.rs Outdated Show resolved Hide resolved
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label May 7, 2024
@PgBiel
Copy link
Contributor Author

PgBiel commented May 8, 2024

I noticed something interesting: without page labels (custom page numbering), the PDF page numbers (in the outline or otherwise) do not correspond to their real page numbers in the original Typst document. Consider the document below compiled with --pages "1,2,3,4,8-10", where each page has a "Bruh" heading:

  1. Without page labels - Okular PDF reader shows 5,6,7 (real PDF page number) instead of 8,9,10 (page number in Typst).
    image

  2. With page labels - Okular PDF reader shows h., i. and j., matching how Typst displays page numbers, although the real PDF page numbers are kept and displayed separately.
    image

Same happens for other PDF readers, e.g. in Firefox. The default, if Typst doesn't specify custom page numbering labels, is to just display the real PDF page number instead of the Typst page number.

I wonder if there's much that can be done about this, e.g. always specify page labels if some pages are excluded from exporting. But perhaps we could consider this to be "intended behavior" in a way. Or maybe it's just not worth it to fix this / could fix later (doesn't seem to be that bad of a problem).

@laurmaedje
Copy link
Member

always specify page labels if some pages are excluded from exporting

That might make sense indeed, especially since it already happens if a numbering is specified.

@PgBiel
Copy link
Contributor Author

PgBiel commented May 9, 2024

Alright, I've implemented this, though there are two important notes (and thus potential design decisions):

  1. I noticed that, when page numbering was enabled by the Typst document, the page numbers used for the labels are the logical page numbers, not the real page numbers. When page numbering is disabled, however, the page numbers used are always the real page numbers (since just the PDF's final page numbers are used, given no labels are added). In my implementation of adding labels to pages without numbering after non-exported pages, I added labels with the real page numbers, not logical, for consistency with the case of no page numbering. But let me know if that wasn't the right choice.

  2. Consider the situation where the first N pages were exported, but page N + 1 wasn't exported, so pages N + 2 and onwards have PDF page numbers which don't correspond to their real page numbers in the Typst document (as shown in my last reply). The solution I implemented was to add labels to pages N + 2 onwards with their respective real page numbers in Typst. However, there was one thing I wasn't sure about: should I have added labels to the first N pages too (redundant labels, equal to the final PDF page numbers in every way) just for consistency? Initially I experimented with doing so in 0ffac2e, but ended up reverting it because it felt unnecessary. Therefore, only pages N + 2 onwards are labeled (just to fix the problem of "drifting" page numbers after non-exported pages). Let me know if this wasn't the right choice as well - I see arguments both in favor (consistency with setting page.numbering from the start, and consistency with the remaining pages) and against (not being necessary since the labels are fully redundant) labeling the first N pages, so I can see this going either way, haha.

@laurmaedje laurmaedje added this pull request to the merge queue May 10, 2024
@laurmaedje
Copy link
Member

Your choices sound fine. Thank you!

Merged via the queue into typst:main with commit 7905de6 May 10, 2024
6 checks passed
@PgBiel PgBiel deleted the select-page branch May 10, 2024 18:09
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.

Export specific page / page ranges
2 participants