summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-02-14 16:07:38 +0100
committerGitHub <noreply@github.com>2024-02-14 15:07:38 +0000
commit79e37ccbac080212dc42e996d760664c75d1a56f (patch)
tree70c926c62535dd0df6f93d5a5a3e84326ed8aa9c /crates
parent601118652de5b3151e96eba3c0c8c285f3fab8fd (diff)
Don't round SVG size (#3415)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst-pdf/src/image.rs2
-rw-r--r--crates/typst-render/src/lib.rs4
-rw-r--r--crates/typst-svg/src/lib.rs6
-rw-r--r--crates/typst/src/visualize/image/mod.rs8
-rw-r--r--crates/typst/src/visualize/image/svg.rs10
5 files changed, 15 insertions, 15 deletions
diff --git a/crates/typst-pdf/src/image.rs b/crates/typst-pdf/src/image.rs
index a4833e6e..312c2abb 100644
--- a/crates/typst-pdf/src/image.rs
+++ b/crates/typst-pdf/src/image.rs
@@ -18,7 +18,7 @@ pub fn deferred_image(image: Image) -> Deferred<EncodedImage> {
Deferred::new(move || match image.kind() {
ImageKind::Raster(raster) => {
let raster = raster.clone();
- let (width, height) = (image.width(), image.height());
+ let (width, height) = (raster.width(), raster.height());
let (data, filter, has_color) = encode_raster_image(&raster);
let icc = raster.icc().map(deflate);
diff --git a/crates/typst-render/src/lib.rs b/crates/typst-render/src/lib.rs
index afdfeeef..8c519795 100644
--- a/crates/typst-render/src/lib.rs
+++ b/crates/typst-render/src/lib.rs
@@ -352,7 +352,7 @@ fn render_bitmap_glyph(
// and maybe also for Noto Color Emoji. And: Is the size calculation
// correct?
let h = text.size;
- let w = (image.width() as f64 / image.height() as f64) * h;
+ let w = (image.width() / image.height()) * h;
let dx = (raster.x as f32) / (image.width() as f32) * size;
let dy = (raster.y as f32) / (image.height() as f32) * size;
render_image(
@@ -742,7 +742,7 @@ fn scaled_texture(image: &Image, w: u32, h: u32) -> Option<Arc<sk::Pixmap>> {
let mut pixmap = sk::Pixmap::new(w, h)?;
match image.kind() {
ImageKind::Raster(raster) => {
- let downscale = w < image.width();
+ let downscale = w < raster.width();
let filter =
if downscale { FilterType::Lanczos3 } else { FilterType::CatmullRom };
let buf = raster.dynamic().resize(w, h, filter);
diff --git a/crates/typst-svg/src/lib.rs b/crates/typst-svg/src/lib.rs
index 2c69e6d9..4d542296 100644
--- a/crates/typst-svg/src/lib.rs
+++ b/crates/typst-svg/src/lib.rs
@@ -409,8 +409,8 @@ impl SVGRenderer {
let glyph_hash = hash128(&(&text.font, id));
let id = self.glyphs.insert_with(glyph_hash, || {
- let width = image.width() as f64;
- let height = image.height() as f64;
+ let width = image.width();
+ let height = image.height();
let url = convert_image_to_base64_url(&image);
let ts = Transform::translate(
Abs::pt(bitmap_x_offset),
@@ -426,7 +426,7 @@ impl SVGRenderer {
// The image is stored with the height of `image.height()`, but we want
// to render it with a height of `target_height`. So we need to scale
// it.
- let scale_factor = target_height / image.height() as f64;
+ let scale_factor = target_height / image.height();
self.xml.write_attribute("x", &(x_offset / scale_factor));
self.xml.write_attribute_fmt(
"transform",
diff --git a/crates/typst/src/visualize/image/mod.rs b/crates/typst/src/visualize/image/mod.rs
index abf7a77f..dc58bf80 100644
--- a/crates/typst/src/visualize/image/mod.rs
+++ b/crates/typst/src/visualize/image/mod.rs
@@ -379,17 +379,17 @@ impl Image {
}
/// The width of the image in pixels.
- pub fn width(&self) -> u32 {
+ pub fn width(&self) -> f64 {
match &self.0.kind {
- ImageKind::Raster(raster) => raster.width(),
+ ImageKind::Raster(raster) => raster.width() as f64,
ImageKind::Svg(svg) => svg.width(),
}
}
/// The height of the image in pixels.
- pub fn height(&self) -> u32 {
+ pub fn height(&self) -> f64 {
match &self.0.kind {
- ImageKind::Raster(raster) => raster.height(),
+ ImageKind::Raster(raster) => raster.height() as f64,
ImageKind::Svg(svg) => svg.height(),
}
}
diff --git a/crates/typst/src/visualize/image/svg.rs b/crates/typst/src/visualize/image/svg.rs
index 20e1b0b9..fe496807 100644
--- a/crates/typst/src/visualize/image/svg.rs
+++ b/crates/typst/src/visualize/image/svg.rs
@@ -20,7 +20,7 @@ pub struct SvgImage(Arc<Repr>);
/// The internal representation.
struct Repr {
data: Bytes,
- size: Axes<u32>,
+ size: Axes<f64>,
font_hash: u128,
tree: sync::SyncTree,
}
@@ -76,12 +76,12 @@ impl SvgImage {
}
/// The SVG's width in pixels.
- pub fn width(&self) -> u32 {
+ pub fn width(&self) -> f64 {
self.0.size.x
}
/// The SVG's height in pixels.
- pub fn height(&self) -> u32 {
+ pub fn height(&self) -> f64 {
self.0.size.y
}
@@ -222,8 +222,8 @@ where
}
/// The ceiled pixel size of an SVG.
-fn tree_size(tree: &usvg::Tree) -> Axes<u32> {
- Axes::new(tree.size.width().ceil() as u32, tree.size.height().ceil() as u32)
+fn tree_size(tree: &usvg::Tree) -> Axes<f64> {
+ Axes::new(tree.size.width() as f64, tree.size.height() as f64)
}
/// Format the user-facing SVG decoding error message.