Don't change DPI in resvg (#4354)

This commit is contained in:
Laurenz Stampfl 2024-06-11 13:12:03 +02:00 committed by GitHub
parent 2b064ba197
commit 20b8d2c121
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 8 additions and 5 deletions

View File

@ -329,6 +329,9 @@ impl Image {
/// if the image doesn't contain DPI metadata. /// if the image doesn't contain DPI metadata.
pub const DEFAULT_DPI: f64 = 72.0; pub const DEFAULT_DPI: f64 = 72.0;
/// Should always be the same as the default DPI used by usvg.
pub const USVG_DEFAULT_DPI: f64 = 96.0;
/// Create an image from a buffer and a format. /// Create an image from a buffer and a format.
#[comemo::memoize] #[comemo::memoize]
#[typst_macros::time(name = "load image")] #[typst_macros::time(name = "load image")]
@ -407,7 +410,7 @@ impl Image {
pub fn dpi(&self) -> Option<f64> { pub fn dpi(&self) -> Option<f64> {
match &self.0.kind { match &self.0.kind {
ImageKind::Raster(raster) => raster.dpi(), ImageKind::Raster(raster) => raster.dpi(),
ImageKind::Svg(_) => None, ImageKind::Svg(_) => Some(Image::USVG_DEFAULT_DPI),
} }
} }

View File

@ -12,7 +12,6 @@ use crate::layout::Axes;
use crate::text::{ use crate::text::{
Font, FontBook, FontFlags, FontStretch, FontStyle, FontVariant, FontWeight, Font, FontBook, FontFlags, FontStretch, FontStyle, FontVariant, FontWeight,
}; };
use crate::visualize::Image;
use crate::World; use crate::World;
/// A decoded SVG. /// A decoded SVG.
@ -102,9 +101,10 @@ fn base_options() -> usvg::Options<'static> {
// Disable usvg's default to "Times New Roman". // Disable usvg's default to "Times New Roman".
font_family: String::new(), font_family: String::new(),
// We override the DPI here so that we get the correct the size when // We don't override the DPI here, because we already
// scaling the image to its natural size. // force the image into the corresponding DPI by setting
dpi: Image::DEFAULT_DPI as f32, // the width and height. Changing the DPI only trips up
// the logic in `resvg`.
// Override usvg's resource loading defaults. // Override usvg's resource loading defaults.
resources_dir: None, resources_dir: None,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB