mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Tweak usvg
options (#4163)
This commit is contained in:
parent
2188a4bf48
commit
00f7588755
@ -4,8 +4,9 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use comemo::Tracked;
|
use comemo::Tracked;
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use siphasher::sip128::Hasher128;
|
use siphasher::sip128::Hasher128;
|
||||||
use usvg::{Node, PostProcessingSteps, TreeParsing, TreePostProc};
|
use usvg::{ImageHrefResolver, Node, PostProcessingSteps, TreeParsing, TreePostProc};
|
||||||
|
|
||||||
use crate::diag::{format_xml_like_error, StrResult};
|
use crate::diag::{format_xml_like_error, StrResult};
|
||||||
use crate::foundations::Bytes;
|
use crate::foundations::Bytes;
|
||||||
@ -31,7 +32,7 @@ impl SvgImage {
|
|||||||
#[comemo::memoize]
|
#[comemo::memoize]
|
||||||
pub fn new(data: Bytes) -> StrResult<SvgImage> {
|
pub fn new(data: Bytes) -> StrResult<SvgImage> {
|
||||||
let mut tree =
|
let mut tree =
|
||||||
usvg::Tree::from_data(&data, &options()).map_err(format_usvg_error)?;
|
usvg::Tree::from_data(&data, &OPTIONS).map_err(format_usvg_error)?;
|
||||||
tree.calculate_bounding_boxes();
|
tree.calculate_bounding_boxes();
|
||||||
Ok(Self(Arc::new(Repr {
|
Ok(Self(Arc::new(Repr {
|
||||||
data,
|
data,
|
||||||
@ -50,7 +51,7 @@ impl SvgImage {
|
|||||||
families: &[String],
|
families: &[String],
|
||||||
) -> StrResult<SvgImage> {
|
) -> StrResult<SvgImage> {
|
||||||
let mut tree =
|
let mut tree =
|
||||||
usvg::Tree::from_data(&data, &options()).map_err(format_usvg_error)?;
|
usvg::Tree::from_data(&data, &OPTIONS).map_err(format_usvg_error)?;
|
||||||
let mut font_hash = 0;
|
let mut font_hash = 0;
|
||||||
if tree.has_text_nodes() {
|
if tree.has_text_nodes() {
|
||||||
let (fontdb, hash) = load_svg_fonts(world, &mut tree, families);
|
let (fontdb, hash) = load_svg_fonts(world, &mut tree, families);
|
||||||
@ -124,20 +125,27 @@ impl Hash for Repr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The conversion options.
|
/// The conversion options.
|
||||||
fn options() -> usvg::Options {
|
static OPTIONS: Lazy<usvg::Options> = Lazy::new(|| usvg::Options {
|
||||||
// Disable usvg's default to "Times New Roman". Instead, we default to
|
// Disable usvg's default to "Times New Roman". Instead, we default to
|
||||||
// the empty family and later, when we traverse the SVG, we check for
|
// the empty family and later, when we traverse the SVG, we check for
|
||||||
// empty and non-existing family names and replace them with the true
|
// empty and non-existing family names and replace them with the true
|
||||||
// fallback family. This way, we can memoize SVG decoding with and without
|
// fallback family. This way, we can memoize SVG decoding with and
|
||||||
// fonts if the SVG does not contain text.
|
// without fonts if the SVG does not contain text.
|
||||||
usvg::Options {
|
|
||||||
font_family: String::new(),
|
font_family: String::new(),
|
||||||
|
|
||||||
// We override the DPI here so that we get the correct the size when
|
// We override the DPI here so that we get the correct the size when
|
||||||
// scaling the image to its natural size.
|
// scaling the image to its natural size.
|
||||||
dpi: Image::DEFAULT_DPI as f32,
|
dpi: Image::DEFAULT_DPI as f32,
|
||||||
|
|
||||||
|
// Override usvg's resource loading defaults.
|
||||||
|
resources_dir: None,
|
||||||
|
image_href_resolver: ImageHrefResolver {
|
||||||
|
resolve_data: ImageHrefResolver::default_data_resolver(),
|
||||||
|
resolve_string: Box::new(|_, _| None),
|
||||||
|
},
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/// Discover and load the fonts referenced by an SVG.
|
/// Discover and load the fonts referenced by an SVG.
|
||||||
fn load_svg_fonts(
|
fn load_svg_fonts(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user