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 | use crate::{ImageHrefResolver, ImageRendering, ShapeRendering, Size, TextRendering}; |
6 | |
7 | /// Processing options. |
8 | #[derive (Debug)] |
9 | pub struct Options { |
10 | /// Directory that will be used during relative paths resolving. |
11 | /// |
12 | /// Expected to be the same as the directory that contains the SVG file, |
13 | /// but can be set to any. |
14 | /// |
15 | /// Default: `None` |
16 | pub resources_dir: Option<std::path::PathBuf>, |
17 | |
18 | /// Target DPI. |
19 | /// |
20 | /// Impacts units conversion. |
21 | /// |
22 | /// Default: 96.0 |
23 | pub dpi: f32, |
24 | |
25 | /// A default font family. |
26 | /// |
27 | /// Will be used when no `font-family` attribute is set in the SVG. |
28 | /// |
29 | /// Default: Times New Roman |
30 | pub font_family: String, |
31 | |
32 | /// A default font size. |
33 | /// |
34 | /// Will be used when no `font-size` attribute is set in the SVG. |
35 | /// |
36 | /// Default: 12 |
37 | pub font_size: f32, |
38 | |
39 | /// A list of languages. |
40 | /// |
41 | /// Will be used to resolve a `systemLanguage` conditional attribute. |
42 | /// |
43 | /// Format: en, en-US. |
44 | /// |
45 | /// Default: `[en]` |
46 | pub languages: Vec<String>, |
47 | |
48 | /// Specifies the default shape rendering method. |
49 | /// |
50 | /// Will be used when an SVG element's `shape-rendering` property is set to `auto`. |
51 | /// |
52 | /// Default: GeometricPrecision |
53 | pub shape_rendering: ShapeRendering, |
54 | |
55 | /// Specifies the default text rendering method. |
56 | /// |
57 | /// Will be used when an SVG element's `text-rendering` property is set to `auto`. |
58 | /// |
59 | /// Default: OptimizeLegibility |
60 | pub text_rendering: TextRendering, |
61 | |
62 | /// Specifies the default image rendering method. |
63 | /// |
64 | /// Will be used when an SVG element's `image-rendering` property is set to `auto`. |
65 | /// |
66 | /// Default: OptimizeQuality |
67 | pub image_rendering: ImageRendering, |
68 | |
69 | /// Default viewport size to assume if there is no `viewBox` attribute and |
70 | /// the `width` or `height` attributes are relative. |
71 | /// |
72 | /// Default: `(100, 100)` |
73 | pub default_size: Size, |
74 | |
75 | /// Specifies the way `xlink:href` in `<image>` elements should be handled. |
76 | /// |
77 | /// Default: see type's documentation for details |
78 | pub image_href_resolver: ImageHrefResolver, |
79 | } |
80 | |
81 | impl Default for Options { |
82 | fn default() -> Options { |
83 | Options { |
84 | resources_dir: None, |
85 | dpi: 96.0, |
86 | // Default font is user-agent dependent so we can use whichever we like. |
87 | font_family: "Times New Roman" .to_owned(), |
88 | font_size: 12.0, |
89 | languages: vec!["en" .to_string()], |
90 | shape_rendering: ShapeRendering::default(), |
91 | text_rendering: TextRendering::default(), |
92 | image_rendering: ImageRendering::default(), |
93 | default_size: Size::from_wh(width:100.0, height:100.0).unwrap(), |
94 | image_href_resolver: ImageHrefResolver::default(), |
95 | } |
96 | } |
97 | } |
98 | |
99 | impl Options { |
100 | /// Converts a relative path into absolute relative to the SVG file itself. |
101 | /// |
102 | /// If `Options::resources_dir` is not set, returns itself. |
103 | pub fn get_abs_path(&self, rel_path: &std::path::Path) -> std::path::PathBuf { |
104 | match self.resources_dir { |
105 | Some(ref dir: &PathBuf) => dir.join(rel_path), |
106 | None => rel_path.into(), |
107 | } |
108 | } |
109 | } |
110 | |