mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Don't round SVG size (#3415)
This commit is contained in:
parent
601118652d
commit
79e37ccbac
@ -18,7 +18,7 @@ pub fn deferred_image(image: Image) -> Deferred<EncodedImage> {
|
|||||||
Deferred::new(move || match image.kind() {
|
Deferred::new(move || match image.kind() {
|
||||||
ImageKind::Raster(raster) => {
|
ImageKind::Raster(raster) => {
|
||||||
let raster = raster.clone();
|
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 (data, filter, has_color) = encode_raster_image(&raster);
|
||||||
let icc = raster.icc().map(deflate);
|
let icc = raster.icc().map(deflate);
|
||||||
|
|
||||||
|
@ -352,7 +352,7 @@ fn render_bitmap_glyph(
|
|||||||
// and maybe also for Noto Color Emoji. And: Is the size calculation
|
// and maybe also for Noto Color Emoji. And: Is the size calculation
|
||||||
// correct?
|
// correct?
|
||||||
let h = text.size;
|
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 dx = (raster.x as f32) / (image.width() as f32) * size;
|
||||||
let dy = (raster.y as f32) / (image.height() as f32) * size;
|
let dy = (raster.y as f32) / (image.height() as f32) * size;
|
||||||
render_image(
|
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)?;
|
let mut pixmap = sk::Pixmap::new(w, h)?;
|
||||||
match image.kind() {
|
match image.kind() {
|
||||||
ImageKind::Raster(raster) => {
|
ImageKind::Raster(raster) => {
|
||||||
let downscale = w < image.width();
|
let downscale = w < raster.width();
|
||||||
let filter =
|
let filter =
|
||||||
if downscale { FilterType::Lanczos3 } else { FilterType::CatmullRom };
|
if downscale { FilterType::Lanczos3 } else { FilterType::CatmullRom };
|
||||||
let buf = raster.dynamic().resize(w, h, filter);
|
let buf = raster.dynamic().resize(w, h, filter);
|
||||||
|
@ -409,8 +409,8 @@ impl SVGRenderer {
|
|||||||
|
|
||||||
let glyph_hash = hash128(&(&text.font, id));
|
let glyph_hash = hash128(&(&text.font, id));
|
||||||
let id = self.glyphs.insert_with(glyph_hash, || {
|
let id = self.glyphs.insert_with(glyph_hash, || {
|
||||||
let width = image.width() as f64;
|
let width = image.width();
|
||||||
let height = image.height() as f64;
|
let height = image.height();
|
||||||
let url = convert_image_to_base64_url(&image);
|
let url = convert_image_to_base64_url(&image);
|
||||||
let ts = Transform::translate(
|
let ts = Transform::translate(
|
||||||
Abs::pt(bitmap_x_offset),
|
Abs::pt(bitmap_x_offset),
|
||||||
@ -426,7 +426,7 @@ impl SVGRenderer {
|
|||||||
// The image is stored with the height of `image.height()`, but we want
|
// 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
|
// to render it with a height of `target_height`. So we need to scale
|
||||||
// it.
|
// 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("x", &(x_offset / scale_factor));
|
||||||
self.xml.write_attribute_fmt(
|
self.xml.write_attribute_fmt(
|
||||||
"transform",
|
"transform",
|
||||||
|
@ -379,17 +379,17 @@ impl Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The width of the image in pixels.
|
/// The width of the image in pixels.
|
||||||
pub fn width(&self) -> u32 {
|
pub fn width(&self) -> f64 {
|
||||||
match &self.0.kind {
|
match &self.0.kind {
|
||||||
ImageKind::Raster(raster) => raster.width(),
|
ImageKind::Raster(raster) => raster.width() as f64,
|
||||||
ImageKind::Svg(svg) => svg.width(),
|
ImageKind::Svg(svg) => svg.width(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The height of the image in pixels.
|
/// The height of the image in pixels.
|
||||||
pub fn height(&self) -> u32 {
|
pub fn height(&self) -> f64 {
|
||||||
match &self.0.kind {
|
match &self.0.kind {
|
||||||
ImageKind::Raster(raster) => raster.height(),
|
ImageKind::Raster(raster) => raster.height() as f64,
|
||||||
ImageKind::Svg(svg) => svg.height(),
|
ImageKind::Svg(svg) => svg.height(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ pub struct SvgImage(Arc<Repr>);
|
|||||||
/// The internal representation.
|
/// The internal representation.
|
||||||
struct Repr {
|
struct Repr {
|
||||||
data: Bytes,
|
data: Bytes,
|
||||||
size: Axes<u32>,
|
size: Axes<f64>,
|
||||||
font_hash: u128,
|
font_hash: u128,
|
||||||
tree: sync::SyncTree,
|
tree: sync::SyncTree,
|
||||||
}
|
}
|
||||||
@ -76,12 +76,12 @@ impl SvgImage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The SVG's width in pixels.
|
/// The SVG's width in pixels.
|
||||||
pub fn width(&self) -> u32 {
|
pub fn width(&self) -> f64 {
|
||||||
self.0.size.x
|
self.0.size.x
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The SVG's height in pixels.
|
/// The SVG's height in pixels.
|
||||||
pub fn height(&self) -> u32 {
|
pub fn height(&self) -> f64 {
|
||||||
self.0.size.y
|
self.0.size.y
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,8 +222,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The ceiled pixel size of an SVG.
|
/// The ceiled pixel size of an SVG.
|
||||||
fn tree_size(tree: &usvg::Tree) -> Axes<u32> {
|
fn tree_size(tree: &usvg::Tree) -> Axes<f64> {
|
||||||
Axes::new(tree.size.width().ceil() as u32, tree.size.height().ceil() as u32)
|
Axes::new(tree.size.width() as f64, tree.size.height() as f64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format the user-facing SVG decoding error message.
|
/// Format the user-facing SVG decoding error message.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user