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