print: Render SVG for correct size

Closes #140

Part-of: <https://gitlab.gnome.org/Incubator/loupe/-/merge_requests/165>
This commit is contained in:
Sophie Herold 2023-04-17 22:38:52 +02:00 • committed by Marge Bot
parent ae5ca76176
commit 587599f50b
4 changed files with 50 additions and 17 deletions

View file

@ -1014,6 +1014,7 @@ impl LpImage {
&snapshot,
-orientation.rotation as f32,
orientation.mirrored,
scale,
);
self.imp()

View file

@ -1,5 +1,6 @@
//! The print layout widget that also coordinates the print process
use crate::decoder;
use crate::deps::*;
use crate::widgets::{LpImage, LpPrintPreview};
@ -395,30 +396,41 @@ impl LpPrint {
let cairo_dpi = print_context.dpi_x();
let (orig_width, _) = image.image_size();
let (orig_width, orig_height) = image.image_size();
let texture_scale = self.user_width() / orig_width as f64;
let cairo_scale = cairo_dpi / self.dpi();
let texture = image.print_data(texture_scale).unwrap();
let cairo_surface = if let Some(decoder::ImageFormat::Svg) = image.format() {
// Render SVG to exact needed sizes
// TODO: This should be async
decoder::formats::Svg::render_print(
&image.file().unwrap(),
(orig_width as f64 * texture_scale) as i32,
(orig_height as f64 * texture_scale) as i32,
)
.unwrap()
} else {
let texture = image.print_data(texture_scale).unwrap();
let mut downloader = gdk::TextureDownloader::new(&texture);
downloader.set_format(gdk::MemoryFormat::B8g8r8a8Premultiplied);
let mut downloader = gdk::TextureDownloader::new(&texture);
downloader.set_format(gdk::MemoryFormat::B8g8r8a8Premultiplied);
let (data, stride) = downloader.download_bytes();
let data = data.to_vec();
let (data, stride) = downloader.download_bytes();
let data = data.to_vec();
let width = texture.width();
let height = texture.height();
let width = texture.width();
let height = texture.height();
let cairo_surface = cairo::ImageSurface::create_for_data(
data,
cairo::Format::ARgb32,
width,
height,
stride as i32,
)
.unwrap();
cairo::ImageSurface::create_for_data(
data,
cairo::Format::ARgb32,
width,
height,
stride as i32,
)
.unwrap()
};
let cairo_context = print_context.cairo_context();
cairo_context.scale(cairo_scale, cairo_scale);