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