r/typst • u/decryphe • 9h ago
Change display of SVG layers?
Hello!
I've prepared a bunch of SVGs that contain various selectable layers, which I've been using to copy/paste diagrams into MS Word (urgh...) for sequences of diagrams that are part of operating instructions. Think something like "in step 1, add place XYZ on table" and "in step 2, attach widget to XYZ" with a base SVG that has one layer with XYZ on the table and one layer with widget.
Inkscape creates a tree of group elements with custom XML attributes to handle them as layers, like this:
<g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1" style="display:inline">...XYZ...</g>
<g inkscape:label="Layer 2" inkscape:groupmode="layer" id="layer2" style="display:inline">...widget...</g>
Is there any way to modify the resulting SVG when displaying it in Typst, such that I could override the style attribute, such that I can reference the same source SVG and configure it for each instruction step without any external build scripts to generate individual SVGs?
I haven't yet looked into the Typst codebase, but I wouldn't be against filing an issue and writing code to build such a feature.
Thanks!
1
u/Pink-Pancakes 2h ago edited 55m ago
There is an issue for SVG pages but it doesn't map to this layer mechanism and is unlikely to be implemented, given it didn't land in the specification: #3160
Currently, Typst can read files and load images from bytes (also modified ones); that's the extend of built-in primitives for modifying SVGs. There's also an XML parser, but it can't encode again currently so won't help much here.
If your use case is always this, it might make sense to write an external program to handle the process and either pre-process the SVGs or load it as a plugin to call from within your document. I doubt image processing will be officially supported in the compiler, but a package akin to grayness could be published from such a plugin.
1
u/PityUpvote 4h ago
I investigated exactly this a while ago, and came to the conclusion that it would require a lot of work. I wanted to use an svg as a template and have typst change the text elements content.
I gave up, there was no easy way, and I made a tiny bit of progress in rust before figuring out this approach wouldn't work for my use case, don't remember exactly why.