summaryrefslogtreecommitdiff
path: root/crates/typst-svg/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-svg/src')
-rw-r--r--crates/typst-svg/src/image.rs26
-rw-r--r--crates/typst-svg/src/lib.rs2
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};