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