Skip to content

Add SVG export - #1729

Merged
laurmaedje merged 42 commits into
typst:mainfrom
Enter-tainer:mgt/svg
Aug 8, 2023
Merged

laurmaedje merged 42 commits into
typst:mainfrom
Enter-tainer:mgt/svg

Conversation

@Enter-tainer

@Enter-tainer Enter-tainer commented Jul 16, 2023 •

Copy link
Copy Markdown
Contributor

This PR is based on @Myriad-Dreamin's PR #1517 but in a simplified fashion.

possibly fix #863

TODO:
- [ ] Snapshot test Tests will come in the following PRs. Perhaps using snapshot test technique.

  • svg glyph.
  • bug in bitmap glyph
  • refactor the code using xml writer
  • Memorize glyph and path render
  • add comments about svg glyph rendering

This PR is like a fast prototype and there are still many things to be done to make it more polished and stable.

I think it is ready now!

@laurmaedje

laurmaedje commented Jul 17, 2023 •

Copy link
Copy Markdown
Member

Tests are difficult, we also don't have tests for PDF export for the same reason. At least the bug surface is not too large in 400 lines of code.

Comparing against the reference images would be great, but I imagine we'd need some fuzzy comparison because different rendering engines produce every so slightly different results.

The transforms in render_svg_glyph are indeed somewhat involved. Are you wondering primarily about the first part with the viewBox or the second part with svg_ts (which comes from this PR: #431)?

@Myriad-Dreamin

Myriad-Dreamin commented Jul 18, 2023 •

Copy link
Copy Markdown
Contributor

The previous PR did test SVG in typst like PDF, and you can simply cherry pick this commit f95cb97.


Another solution is to hash the images by perceptual-hash and keep tracking them in VCS and CI. You are free to maintain the simplified SVG exporter together with integration test of typst.ts.

For example of VCS & snapshot testing, see the function typst.ts@79bfdeb1:test_wasm_renderer_functionality.

For example of CI, see Action: typst.ts#974, which attaches a chrome browser's rendering report, wasm-renderer-chrome.test_points.json.gz. Also see a error report by cargo test in Action: typst.ts#349.

These two integration tests work well so far. They never bother you until a pixel-by-pixel difference happens between commits.

@laurmaedje laurmaedje changed the title feat: add svg export Add SVG export Jul 18, 2023
@laurmaedje

Copy link
Copy Markdown
Member

I think having an SVG folder is good for now. We can then look into perceptual hashing and more automated SVG/PDF testing separately.

Comment thread crates/typst-cli/src/compile.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
@Enter-tainer

Enter-tainer commented Jul 18, 2023 •

Copy link
Copy Markdown
Contributor Author

It appears that the test code in png export creates a long png file by connecting the pages in the document. Perhaps we can do the same for SVG export. Inaddition to individual SVG files, we can rasterize SVG pages into pixmap and concat them together. To validate it, we can compare it to the result of a PNG export.

@laurmaedje

laurmaedje commented Jul 18, 2023 •

Copy link
Copy Markdown
Member

Merging SVG files seems sensible. Validating them by rasterizing them and comparing them might work but could very well fail because of slightly different rendering.

Comment thread crates/typst/src/export/svg.rs Resolved
Comment thread crates/typst/src/export/svg.rs Resolved
@Enter-tainer

Enter-tainer commented Jul 20, 2023 •

Copy link
Copy Markdown
Contributor Author

I am not an expert in layout and publishing. Can anyone tell me how much px equal 1 pt? Currently, I just assume that 1px = 1pt in code and the everything in SVG is somewhat small. I guess that if I stick to 1px = 1pt, the result svg would looks fine.

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

that's why the PNG exporter takes a pixel_per_pt argument, which the CLI exposes as PPI (pixel per inch, 1 inch = 72pt). You can do the same here.

Get it. But I'd rather stick to ppi = 72. Unlike PNG export, ppi doesn't affect the sharpness of the SVG file. Another problem is that implementing ppi control would require significant changes to current code , which I am not inclined to do.

Comment thread crates/typst/src/export/svg.rs Resolved
@laurmaedje

Copy link
Copy Markdown
Member

I'd prefer to leave testing out of this PR.

@RazrFalcon

Copy link
Copy Markdown

@Enter-tainer

Resvg doesn't seem to support b64 encoded images.

It does. You're either writing them wrong or it's a bug.

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

It does. You're either writing them wrong or it's a bug

Thanks for figure it out. I used the wrong mimetype. Another problem is that typst disables default features of resvg so raster-images doesn't work.

Comment thread crates/typst/src/export/svg.rs Resolved
@Enter-tainer

Enter-tainer commented Jul 27, 2023 •

Copy link
Copy Markdown
Contributor Author

Just have a performance test on https://github.com/werifu/HUST-typst-template/blob/main/sample.typ?rgh-link-date=2023-07-27T13%3A39%3A23Z#L32. It takes about 50ms to export from scratch and 30ms to incrementalize. I expect it to be faster on your machine because I'm using an old arm64 cpu: Kunpeng 920, developed by Huawei serveral years ago.

@laurmaedje

Copy link
Copy Markdown
Member

Just wanted to say that I'll probably won't get to taking a last look and merging this week because I'm quite busy with web app stuff, but I'll definitely get to it next week!

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Aug 8, 2023
@laurmaedje
laurmaedje merged commit 61e4ad6 into typst:main Aug 8, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thank you, great work!

@callegar

Copy link
Copy Markdown

Just discovered typst in amazement and also discovered from this bug report that svg export might also be coming soon. Looking forward for this feature as it will make it possible to use typst also in conjunction with WYSIWYG word processors and presentation editors such as Libreoffice Writer and Impress as an equation editor in the lines of TeXMaths that is quite nice but slow. Hope a TypMaths can come soon!

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

svg export might also be coming soon.

it is already included in typst v0.8.0

@callegar

Copy link
Copy Markdown

Missed that, some bit of the documentation might still mention only PDF and PNG.

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

Missed that, some bit of the documentation might still mention only PDF and PNG.

can you show some pointers to these docs? so we can update them 💪

@callegar

Copy link
Copy Markdown

Looked for it, but no, it was not a bug in the Typst documentation, it was a bug in my memory!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Apr 1, 2024
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.

Output SVG

5 participants