Skip to content

feat(outputters): Expose writing of PDF resources to Lua#2308

Open
Omikhleia wants to merge 1 commit into
masterfrom
feat-2251-expose-libtexpdf-add-resource
Open

feat(outputters): Expose writing of PDF resources to Lua#2308
Omikhleia wants to merge 1 commit into
masterfrom
feat-2251-expose-libtexpdf-add-resource

Conversation

@Omikhleia

@Omikhleia Omikhleia commented Aug 24, 2025

Copy link
Copy Markdown
Member

(It's the minimal C part of #2251)

This commit extends the low-level C wrapper to libtexpdf to let Lua code add page resources directly to the PDF. This makes it possible to prototype extensions to the Lua outputter without recompiling SILE, and opens the door to features such as gradient support.

EDIT: Now in may 2026 I have more understanding than in August 2025... Proposal amended:

  • pdf.add_page_resource
  • get_page_resources
  • add_page_annotation

See comment further below.

@Omikhleia Omikhleia added this to the v0.15.14 milestone Aug 24, 2025
@Omikhleia Omikhleia self-assigned this Aug 24, 2025
@Omikhleia
Omikhleia requested a review from alerque as a code owner August 24, 2025 07:10
@Omikhleia Omikhleia added the enhancement Software improvement or feature request label Aug 24, 2025
@Omikhleia Omikhleia modified the milestones: v0.15.14, v0.x.y Feb 8, 2026
@Omikhleia

Copy link
Copy Markdown
Member Author

Removed 0.15.14 milestone (switched to 0.x.y):
There's potentially a way to do this without extending our Lua C bindings, with lower-level libtexpdf construct (as I did in resilient.packages.forms and resilient.packages.attachments) -- I might experiment with it, and this PR is perhaps not needed.

@Omikhleia Omikhleia modified the milestones: v0.x.y, v0.15.x Apr 24, 2026
@Omikhleia

Omikhleia commented Apr 24, 2026

Copy link
Copy Markdown
Member Author

There's potentially a way to do this without extending our Lua C bindings, with lower-level libtexpdf construct (as I did in resilient.packages.forms and resilient.packages.attachments) -- I might experiment with it, and this PR is perhaps not needed.

Having experimented, it is needed.
libtexpdf handles most page-related dictionnaries via internal variables (here, currentpage->resources).
The (quite ugly) trick I used resilient.packages.forms1 cannot work here, as the Resources dictionary is always reset by the library (when creating and updating it for font references, etc.).
So any attempt creating and modifying it from Lua won't work, it's cleared by libtextpdf's logic.
The Resources are indirect references after the page is shipped out, so we cannot either access them in an ouputter hook (e.g. just after the pdf.endpage calls. Here we can access it, but only get an indirect object, and we don't have low-level methods to de-reference it).

Footnotes

  1. EDIT -- libtexpdf also handles the annotation dictionary in internal variables (page->annots), but it only resets it when texpdf_doc_add_annot is called -- in our case, via pdf.end_annotation used for hyperlinks. The dirty trick consists in bypassing that call and manually creating the annotation entries from Lua. It's not nice, but it works.
    For resources, however, they are used for lots of things (fonts, proc sets, etc.) and there's no way to bypass that. (I don't know if I am very clear here...).
    Would we want to also get rid of the annotation trick, we'd need for instancepdf.add_dict to accept as argument both a string (as it currently does) or a userdata (built from Lua with nested references etc.) EDIT: Or a dedicated C call for adding annotations, similar to the method for adding ressources.

This commit extends the low-level C wrapper to libtexpdf to let Lua
code:

 - get the resources dictionary for the current page
 - add page resources (built with other low-level pdf constructs)
 - add page annotations (idem)

This makes it possible to implement extensions without recompiling SILE,
and opens the door to features such as gradient support and better form
support.
@Omikhleia
Omikhleia force-pushed the feat-2251-expose-libtexpdf-add-resource branch from cbf2bd3 to a8f860a Compare May 2, 2026 19:22
@Omikhleia

Omikhleia commented May 2, 2026

Copy link
Copy Markdown
Member Author

Revisited my old PR and made it better.

The new constructs are planned to be used here

  • Removing the dirty hack in resilient.forms to play well with links
  • Adding gradient support

The latter is (at this day) still experimental, and needs extra support in my "framebox" package (amongst others), and the underlying "grail" drawing layer -- but the basic steps and first results can already be seen in the aforementioned PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Software improvement or feature request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant