Repository navigation
Description
Description
I propose adding document-like set-rule only elements for export targets to control target specific settings:
png- controlling PPI from within Typst documents
- although this may open a small DOS attack surface from template packages choosing astronomical values
- controlling PPI from within Typst documents
pdf- PDF document metadata, which is currently set using
document
- PDF document metadata, which is currently set using
Use document either for only the common settings or as a convenient default for various settings from multiple targets.
Alongside this, a variable such as target should be added, which is set depending on the current export target. This allows code to act according to the export target, preventing incorrect output or panics when a target specific feature is used, such as Forms #2421 or OCGs #2613.
The newly added elements and variable could be put into an export module, i.e. set export.pdf(...), if export.target == export.pdf { ... }, as they should not require easy access for the majority of users.
While I thought about the target variable being the element of the active export target for nice looking checks (target == pdf), this comes with some downsides:
- elements with no fields must be available too (
svg) - sub-targets, such as PDF/A-2 support #2942 are not easily accounted for unless they receive potentially useless sub-elements like
pdf.a2orpdf.x - if sub-elements are added, they would complicate the simple checks as the equality would no longer hold
target == pdfwould be false fortarget == pdf.a2, users would need to usetarget in (pdf, pdf.a2, pdf.x), listing all possible sub-targets
Using strings solves this more easily, as these sub-target checks could use string methods like target.starts-with("pdf") or even target.starts-with("pdf/a") for all versions of PDF/A.
Most of the sub-target specific settings will likely live on the "encompassing" element anyway (i.e. pdf for PDF/A).
Use Case
-
Export target specific features which are likely to be more "low-level" than normal Typst features if they can't be easily generalized will likely need packages to be made usable for the average Typst user.
Such packages need to be able to:
- use export target specific features without panics (if they do when used on an unsupported target)
- provide reasonable fallback behavior, when the target specific feature is not supported
- this is especially important for preview of features which are not available for PNGs, such as forms
- provide example images of target specific features without the need to take screenshots of PDF viewers to show of export target specific packages
-
Export targets such as HTML HTML Export #721 likely need more export target specific code, as it is fundamentally different from how Typst currently works.
Activity
emilyyyylime commentedon May 18, 2024
targetsounds like it'd fit inside thesysmodule, but I'm not too againstexporteitherlaurmaedje commentedon Jun 11, 2024
I think the question of a
targetvariable and elements for setting target-specific metadata is fairly separate.I want to have a
targetvariable (and I agree thatsyswould be a good place for it), but I'm not sure how fine-grained it should be, e.g. whether we should make it possible to distinguish SVG and PNG. Potentially, it could be a type with a few methods likeis-html()andis-pdf()rather than a enum or a string. However, it also feels a bit over-engineered and unnecessarily complex. Maybe there isn't much extra harm in allowing SVG and PNG to be distinguished as well.As for the target-specific metadata: I see how adding it all to
documentis not great, but I'm not sure configuring the DPI for PNG from within Typst is really necessary. To me, it seems just like the other command line flags, which are also not configurable from within Typst (which is a separate story and maybe not for all of them great, but allowing specifically DPI just because it is target-specific feels strange).I would prefer to get some experience with target-specific things with
targetand forms before settling on a design for target-specific metadata.tingerrr commentedon Jun 11, 2024
Yeah, to be completely honest, it probably only serves as an example of things that one may want to control for other output formats. Since I haven't really touched SVG at all, yet I wouldn't know if there is anything for that and for PNG this is the only thing that seemed similar to PDF specific metadata. It will probably be more relevant for HTML.
Somewhat understandable, but it does impose something on PDF forms, namely that they must always be exported at this moment if there is no way to turn them off conditionally. But maybe their implementation will help in exploring the right API.
laurmaedje commentedon Jun 11, 2024
I don't follow. Wouldn't a
sys.targetallow us to do that? My comment was only about theset pdf(..).tingerrr commentedon Jun 11, 2024
Oh, pardon me, I misunderstood.
laurmaedje commentedon Jun 13, 2024
I had a thought regarding
sys.target: We might need the mechanism to allow for local changes. E.g. in HTML export we could need a way to layout some piece of content (e.g. a figure) with Typst's layout and produce an SVG. So maybe we need some contextual mechanism.tingerrr commentedon Jun 13, 2024
Would this need another mechanism to expose the value then? Since it can't just be a regular value on a module if it's contextual I believe.
laurmaedje commentedon Jun 13, 2024
It would need to be a function.
laurmaedje commentedon Jun 13, 2024
Or a style property I guess
tingerrr commentedon Jun 16, 2024
One problem with it being contextual is that anything else it depends on ends up being contextual too. I wonder if this can actually cause any deeper problems or not, ideally this property should only influence small portions of a document so it would probably be fine.
Style properties for modules seem more idiomatic for this. If you don't mind, I would go ahead and implement style properties for modules first, as this is currently an element function only thing. And then rebase #4385 on top of that.
laurmaedje commentedon Jun 16, 2024
Wait, style properties on modules seem like an entirely new proposal. Have we discussed that before?
tingerrr commentedon Jun 16, 2024
Well a
sys.targetproperty can only be contextual if it's a style property, no? Isn't this what you meant with "Or a style property I guess"?laurmaedje commentedon Jun 16, 2024
With "or a style property" I meant that it that it uses the style chain like fields, but not necessarily as
sys.targetbut rather on some element. Adding style properties on anything but elements (/ types in the future) would be a very large change that doesn't seem sufficiently motivated to me by just this. It opens up many questions.laurmaedje commentedon Jun 16, 2024
I think a contextual function would be simpler.
PgBiel commentedon Jun 17, 2024
I'd like to add that my main concern regarding this feature is ensuring it isn't "abused" to the point of causing a significant distinction between PDF and PNG exports, mostly because Typst's instant preview relies heavily on PNG export (or on SVG export for
typst-preview), and ensuring PNG export looks as close as possible to the final PDF is very important.I'm not sure how we could best mitigate this. Documentation is one way, but not very effective.
Regardless, this feature will certainly be necessary (most importantly for HTML export), so I'm not proposing we drop it. I just wanted to ensure this is considered for the final design.
laurmaedje commentedon Jun 17, 2024
I totally agree. For HTML export alone, it would be sufficient to provide the distinction between HTML and layouted targets (or perhaps modes or some other name for the concept since we can embed layouted in HTML). What's not yet 100% clear to me is what concretely we need for PDF specific things: Do we just want extra stuff to be ignored (doesn't necessarily needed target-specific code) or do we really need flow control?
tingerrr commentedon Jun 17, 2024
Fine with me, I'd go with that then.
Especially regarding the last part of this, take a look at #4368 and see what you think, this is my main concern with styling of PDF forms which is implemented purely by the PDF reader.
I would say that PDF is sufficiently different from other "layouted" targets, not in layout, but in functionality. Perhaps all future features can be neatly implemented in a way that they always appear the same on all non-HTML targets, but I think that's maybe a little too optimistic.
In the end, I don't think this is something we should or would ever see in user code, but strictly in package code (I don't think this should be enforced, but it could technically). Once Typst starts supporting features that heavily rely on more fine grained control on the export target we'd take away the possibility of creating novel solutions out of fear of misuse.
People will always misuse features, that shouldn't mean we take away othere's ability to use it for what it was made for.
We could block the design here for now and see how we go on with PDF forms to see if distinction with PDF is required now. If we go with a more restrictive design (only exposing some targets), then we should go with a
is-<target>approach to avoid excessive API breakage down the line when this changes.T0mstone commentedon Oct 25, 2025
I think a way to set the default target from within the document would also be very nice and fit with this proposal.
lordkekz commentedon Jan 7, 2026
I think it may be nice to also be able to differentiate between
target() == "paged"being due to PDF/PNG/SVG andtarget() == "paged"inside ahtml.frame. This could be achieved using aglobal-target()function that always returns the outermost target (i.e. the kind of file that Typst is going to produce) and could be non-contextual.In my case, I am building a pure-Typst package for exporting either as PDF, or as multiple HTML documents. Of course, this means that when I want to link across documents, I need to use different link targets in HTML and PDF export, including inside
html.frame. Currently the outermost target could be provided manually usingsys.inputsor by storing it in astate. I read that there are plans for multi-file HTML export in Typst itself, which might also solve this particular challenge, but I do not know of any details.laurmaedje commentedon Aug 31, 2026
#8496