diff options
| author | Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> | 2023-09-13 16:41:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-13 16:41:45 +0200 |
| commit | a5e39d814c2b33cb2d37675dd3206ae64cdcc333 (patch) | |
| tree | 8611eaccae5fa92d25ea8c3e8f15ceaaf4aa5fb5 /crates | |
| parent | fb8d3dd6eb847e571d31b530cd98906198ce8811 (diff) | |
Update to usvg 0.35 (#2113)
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst/Cargo.toml | 10 | ||||
| -rw-r--r-- | crates/typst/src/export/render.rs | 49 |
2 files changed, 25 insertions, 34 deletions
diff --git a/crates/typst/Cargo.toml b/crates/typst/Cargo.toml index d9918b11..78f920e3 100644 --- a/crates/typst/Cargo.toml +++ b/crates/typst/Cargo.toml @@ -24,7 +24,7 @@ bytemuck = "1" comemo = "0.3" ecow = { version = "0.1.2", features = ["serde"] } flate2 = "1" -fontdb = { version = "0.13", default-features = false } +fontdb = { version = "0.14", default-features = false } if_chain = "1" image = { version = "0.24", default-features = false, features = ["png", "jpeg", "gif"] } indexmap = { version = "2", features = ["serde"] } @@ -35,14 +35,14 @@ once_cell = "1" pdf-writer = "0.8.1" pixglyph = "0.2" regex = "1" -resvg = { version = "0.32", default-features = false, features = ["raster-images"] } +resvg = { version = "0.35.0", default-features = false, features = ["raster-images"] } roxmltree = "0.18" rustybuzz = "0.7" serde = { version = "1.0.184", features = ["derive"] } siphasher = "0.3" subsetter = "0.1.1" -svg2pdf = "0.6" -tiny-skia = "0.9.0" +svg2pdf = "0.7" +tiny-skia = "0.10.0" toml = { version = "0.8", default-features = false, features = ["parse"] } tracing = "0.1.37" ttf-parser = "0.19.2" @@ -51,7 +51,7 @@ unicode-ident = "1.0" unicode-math-class = "0.1" unicode-segmentation = "1" unscanny = "0.1" -usvg = { version = "0.32", default-features = false, features = ["text"] } +usvg = { version = "0.35", default-features = false, features = ["text"] } xmlwriter = "0.1.0" xmp-writer = "0.1" time = { version = "0.3.20", features = ["std", "formatting", "macros"] } diff --git a/crates/typst/src/export/render.rs b/crates/typst/src/export/render.rs index ac2a8b3d..262bb2e2 100644 --- a/crates/typst/src/export/render.rs +++ b/crates/typst/src/export/render.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use image::imageops::FilterType; use image::{GenericImageView, Rgba}; use pixglyph::Bitmap; -use resvg::FitTo; +use resvg::tiny_skia::IntRect; use tiny_skia as sk; use ttf_parser::{GlyphId, OutlineBuilder}; use usvg::{NodeExt, TreeParsing}; @@ -213,7 +213,8 @@ fn render_svg_glyph( // Parse SVG. let opts = usvg::Options::default(); - let tree = usvg::Tree::from_xmltree(&document, &opts).ok()?; + let usvg_tree = usvg::Tree::from_xmltree(&document, &opts).ok()?; + let tree = resvg::Tree::from_usvg(&usvg_tree); let view_box = tree.view_box.rect; // If there's no viewbox defined, use the em square for our scale @@ -223,12 +224,12 @@ fn render_svg_glyph( // ... but if there's a viewbox or width, use that. if root.has_attribute("viewBox") || root.has_attribute("width") { - width = view_box.width() as f32; + width = view_box.width(); } // Same as for width. if root.has_attribute("viewBox") || root.has_attribute("height") { - height = view_box.height() as f32; + height = view_box.height(); } let size = text.size.to_f32(); @@ -237,41 +238,30 @@ fn render_svg_glyph( // Compute the space we need to draw our glyph. // See https://github.com/RazrFalcon/resvg/issues/602 for why // using the svg size is problematic here. - let mut bbox = usvg::Rect::new_bbox(); - for node in tree.root.descendants() { - if let Some(rect) = node.calculate_bbox().and_then(|b| b.to_rect()) { + let mut bbox = usvg::BBox::default(); + for node in usvg_tree.root.descendants() { + if let Some(rect) = node.calculate_bbox() { bbox = bbox.expand(rect); } } - let canvas_rect = usvg::ScreenRect::new(0, 0, canvas.width(), canvas.height())?; - // Compute the bbox after the transform is applied. // We add a nice 5px border along the bounding box to // be on the safe size. We also compute the intersection // with the canvas rectangle - let svg_ts = usvg::Transform::new( - ts.sx.into(), - ts.kx.into(), - ts.ky.into(), - ts.sy.into(), - ts.tx.into(), - ts.ty.into(), - ); - let bbox = bbox.transform(&svg_ts)?.to_screen_rect(); - let bbox = usvg::ScreenRect::new( + let bbox = bbox.transform(ts)?.to_rect()?.round_out()?; + let bbox = IntRect::from_xywh( bbox.left() - 5, bbox.y() - 5, bbox.width() + 10, bbox.height() + 10, - )? - .fit_to_rect(canvas_rect); + )?; let mut pixmap = sk::Pixmap::new(bbox.width(), bbox.height())?; // We offset our transform so that the pixmap starts at the edge of the bbox. let ts = ts.post_translate(-bbox.left() as f32, -bbox.top() as f32); - resvg::render(&tree, FitTo::Original, ts, pixmap.as_mut())?; + tree.render(ts, &mut pixmap.as_mut()); canvas.draw_pixmap( bbox.left(), @@ -411,7 +401,8 @@ fn render_outline_glyph( // Premultiply the text color. let Paint::Solid(color) = text.fill; let c = color.to_rgba(); - let color = sk::ColorU8::from_rgba(c.r, c.g, c.b, 255).premultiply().get(); + let color = + bytemuck::cast(sk::ColorU8::from_rgba(c.r, c.g, c.b, 255).premultiply()); // Blend the glyph bitmap with the existing pixels on the canvas. let pixels = bytemuck::cast_slice_mut::<u8, u32>(canvas.data_mut()); @@ -601,12 +592,12 @@ fn scaled_texture(image: &Image, w: u32, h: u32) -> Option<Arc<sk::Pixmap>> { } } DecodedImage::Svg(tree) => { - resvg::render( - tree, - FitTo::Size(w, h), - sk::Transform::identity(), - pixmap.as_mut(), - )?; + let tree = resvg::Tree::from_usvg(tree); + let ts = tiny_skia::Transform::from_scale( + w as f32 / tree.size.width(), + h as f32 / tree.size.height(), + ); + tree.render(ts, &mut pixmap.as_mut()) } } Some(Arc::new(pixmap)) |
