Don't round SVG size (#3415)

This commit is contained in:
Laurenz 2024-02-14 16:07:38 +01:00 committed by GitHub
parent 601118652d
commit 79e37ccbac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 15 additions and 15 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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",

View File

@ -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(),
}
}

View File

@ -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.