diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-07-09 15:48:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-09 13:48:43 +0000 |
| commit | 3aa7e861e7ffe03193d94c2cfd249739ef746f09 (patch) | |
| tree | 063945efc6eca31a1f2703fc3127e8723e2eab2d /crates/typst-svg | |
| parent | a45c3388a642d6d7321b5c5b684d3582e91cff41 (diff) | |
Support images in HTML export (#6578)
Diffstat (limited to 'crates/typst-svg')
| -rw-r--r-- | crates/typst-svg/src/image.rs | 26 | ||||
| -rw-r--r-- | crates/typst-svg/src/lib.rs | 2 |
2 files changed, 18 insertions, 10 deletions
diff --git a/crates/typst-svg/src/image.rs b/crates/typst-svg/src/image.rs index 1868ca39..e6dd579f 100644 --- a/crates/typst-svg/src/image.rs +++ b/crates/typst-svg/src/image.rs @@ -18,21 +18,27 @@ impl SVGRenderer { self.xml.write_attribute("width", &size.x.to_pt()); self.xml.write_attribute("height", &size.y.to_pt()); self.xml.write_attribute("preserveAspectRatio", "none"); - match image.scaling() { - Smart::Auto => {} - Smart::Custom(ImageScaling::Smooth) => { - // This is still experimental and not implemented in all major browsers. - // https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering#browser_compatibility - self.xml.write_attribute("style", "image-rendering: smooth") - } - Smart::Custom(ImageScaling::Pixelated) => { - self.xml.write_attribute("style", "image-rendering: pixelated") - } + if let Some(value) = convert_image_scaling(image.scaling()) { + self.xml + .write_attribute("style", &format_args!("image-rendering: {value}")) } self.xml.end_element(); } } +/// Converts an image scaling to a CSS `image-rendering` propery value. +pub fn convert_image_scaling(scaling: Smart<ImageScaling>) -> Option<&'static str> { + match scaling { + Smart::Auto => None, + Smart::Custom(ImageScaling::Smooth) => { + // This is still experimental and not implemented in all major browsers. + // https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering#browser_compatibility + Some("smooth") + } + Smart::Custom(ImageScaling::Pixelated) => Some("pixelated"), + } +} + /// Encode an image into a data URL. The format of the URL is /// `data:image/{format};base64,`. #[comemo::memoize] diff --git a/crates/typst-svg/src/lib.rs b/crates/typst-svg/src/lib.rs index f4e81250..3931b67f 100644 --- a/crates/typst-svg/src/lib.rs +++ b/crates/typst-svg/src/lib.rs @@ -5,6 +5,8 @@ mod paint; mod shape; mod text; +pub use image::{convert_image_scaling, convert_image_to_base64_url}; + use std::collections::HashMap; use std::fmt::{self, Display, Formatter, Write}; |
