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