usvg/parser/options.rs
1// Copyright 2018 the Resvg Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4#[cfg(feature = "text")]
5use std::sync::Arc;
6
7#[cfg(feature = "text")]
8use crate::FontResolver;
9use crate::{ImageHrefResolver, ImageRendering, ShapeRendering, Size, TextRendering};
10
11/// The default nesting limit when parsing.
12pub const DEFAULT_MAX_DEPTH: u32 = 1024;
13
14/// Processing options.
15#[derive(Debug)]
16pub struct Options<'a> {
17 /// Directory that will be used during relative paths resolving.
18 ///
19 /// Expected to be the same as the directory that contains the SVG file,
20 /// but can be set to any.
21 ///
22 /// Default: `None`
23 pub resources_dir: Option<std::path::PathBuf>,
24
25 /// Target DPI.
26 ///
27 /// Impacts units conversion.
28 ///
29 /// Default: 96.0
30 pub dpi: f32,
31
32 /// A default font family.
33 ///
34 /// Will be used when no `font-family` attribute is set in the SVG.
35 ///
36 /// Default: Times New Roman
37 pub font_family: String,
38
39 /// A default font size.
40 ///
41 /// Will be used when no `font-size` attribute is set in the SVG.
42 ///
43 /// Default: 12
44 pub font_size: f32,
45
46 /// A list of languages.
47 ///
48 /// Will be used to resolve a `systemLanguage` conditional attribute.
49 ///
50 /// Format: en, en-US.
51 ///
52 /// Default: `[en]`
53 pub languages: Vec<String>,
54
55 /// Specifies the default shape rendering method.
56 ///
57 /// Will be used when an SVG element's `shape-rendering` property is set to `auto`.
58 ///
59 /// Default: GeometricPrecision
60 pub shape_rendering: ShapeRendering,
61
62 /// Specifies the default text rendering method.
63 ///
64 /// Will be used when an SVG element's `text-rendering` property is set to `auto`.
65 ///
66 /// Default: OptimizeLegibility
67 pub text_rendering: TextRendering,
68
69 /// Specifies the default image rendering method.
70 ///
71 /// Will be used when an SVG element's `image-rendering` property is set to `auto`.
72 ///
73 /// Default: OptimizeQuality
74 pub image_rendering: ImageRendering,
75
76 /// Default viewport size to assume if there is no `viewBox` attribute and
77 /// the `width` or `height` attributes are relative.
78 ///
79 /// Default: `(100, 100)`
80 pub default_size: Size,
81
82 /// Specifies the way `xlink:href` in `<image>` elements should be handled.
83 ///
84 /// Default: see type's documentation for details
85 pub image_href_resolver: ImageHrefResolver<'a>,
86
87 /// Specifies how fonts should be resolved and loaded.
88 #[cfg(feature = "text")]
89 pub font_resolver: FontResolver<'a>,
90
91 /// A database of fonts usable by text.
92 ///
93 /// This is a base database. If a custom `font_resolver` is specified,
94 /// additional fonts can be loaded during parsing. Those will be added to a
95 /// copy of this database. The full database containing all fonts referenced
96 /// in a `Tree` becomes available as [`Tree::fontdb`](crate::Tree::fontdb)
97 /// after parsing. If no fonts were loaded dynamically, that database will
98 /// be the same as this one.
99 #[cfg(feature = "text")]
100 pub fontdb: Arc<fontdb::Database>,
101 /// A CSS stylesheet that should be injected into the SVG. Can be used to overwrite
102 /// certain attributes.
103 pub style_sheet: Option<String>,
104
105 /// The maximum nesting allowed while parsing. If this limit is exceeded,
106 /// the parser will return an error.
107 pub max_depth: u32,
108}
109
110impl Default for Options<'_> {
111 fn default() -> Options<'static> {
112 Options {
113 resources_dir: None,
114 dpi: 96.0,
115 // Default font is user-agent dependent so we can use whichever we like.
116 font_family: "Times New Roman".to_owned(),
117 font_size: 12.0,
118 languages: vec!["en".to_string()],
119 shape_rendering: ShapeRendering::default(),
120 text_rendering: TextRendering::default(),
121 image_rendering: ImageRendering::default(),
122 default_size: Size::from_wh(100.0, 100.0).unwrap(),
123 image_href_resolver: ImageHrefResolver::default(),
124 #[cfg(feature = "text")]
125 font_resolver: FontResolver::default(),
126 #[cfg(feature = "text")]
127 fontdb: Arc::new(fontdb::Database::new()),
128 style_sheet: None,
129 max_depth: DEFAULT_MAX_DEPTH,
130 }
131 }
132}
133
134impl Options<'_> {
135 /// Converts a relative path into absolute relative to the SVG file itself.
136 ///
137 /// If `Options::resources_dir` is not set, returns itself.
138 pub fn get_abs_path(&self, rel_path: &std::path::Path) -> std::path::PathBuf {
139 match self.resources_dir {
140 Some(ref dir) => dir.join(rel_path),
141 None => rel_path.into(),
142 }
143 }
144
145 /// Mutably acquires the database.
146 ///
147 /// This clones the database if it is currently shared.
148 #[cfg(feature = "text")]
149 pub fn fontdb_mut(&mut self) -> &mut fontdb::Database {
150 Arc::make_mut(&mut self.fontdb)
151 }
152}