oxvg_optimiser 0.0.5

The OXVG optimiser is library for optimising SVG documents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use oxvg_ast::{
    element::Element,
    node::Ref,
    visitor::{ContextFlags, Info, PrepareOutcome, Visitor},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use serde_with::skip_serializing_none;

use crate::error::JobsError;

#[cfg(feature = "wasm")]
use tsify::Tsify;

macro_rules! jobs {
    ($($name:ident: $job:ident$(< $($t:ty),* >)? $((is_default: $default:ident))?,)+) => {
        $(mod $name;)+

        $(pub use self::$name::$job;)+

        #[cfg_attr(feature = "serde", skip_serializing_none)]
        #[cfg_attr(feature = "napi", napi(object))]
        #[cfg_attr(feature = "wasm", derive(Tsify))]
        #[cfg_attr(feature = "wasm", tsify(from_wasm_abi, into_wasm_abi))]
        #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
        #[derive(Clone, Debug)]
        #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
        /// Each task for optimising an SVG document.
        pub struct Jobs {
            $(
                #[doc=concat!("See [`", stringify!($job), "`]")]
                pub $name: Option<$job $( < 'arena, $($t),* >)?>
            ),+
        }

        impl Default for Jobs {
            fn default() -> Self {
                macro_rules! is_default {
                    ($_job:ident $_default:ident) => { Some($_job::default()) };
                    ($_job:ident) => { None };
                }
                Self {
                    $($name: is_default!($job $($default)?)),+
                }
            }
        }

        impl Jobs {
            /// Runs each job in the config, returning the number of non-skipped jobs
            fn run_jobs<'input, 'arena>(
                &self,
                element: &Element<'input, 'arena>,
                info: &Info<'input, 'arena>
            ) -> Result<usize, JobsError<'input>> {
                let mut count = 0;
                $(if let Some(job) = self.$name.as_ref() {
                    log::debug!(concat!("💼 starting ", stringify!($name)));
                    match job.start_with_info(element, info, None) {
                        Err(e) if e.is_important() => return Err(e),
                        Err(e) => log::error!("{} failed {e}", stringify!($name)),
                        Ok(r) => if !r.contains(PrepareOutcome::skip) {
                            count += 1;
                        }
                    }
                })+
                Ok(count)
            }

            #[cfg(feature = "napi")]
            #[doc(hidden)]
            pub fn napi_from_svgo_plugin_config(value: Option<Vec<napi::bindgen_prelude::Unknown>>) -> napi::Result<Self> {
                use napi::{bindgen_prelude::{Status, Object}, ValueType};
                let Some(plugins) = value else { return Ok(Self::default()) };

                let mut oxvg_config = Self::none();
                for plugin in plugins {
                    match plugin.get_type()? {
                        ValueType::String => unsafe {
                            let svgo_name: String = plugin.cast()?;
                            oxvg_config
                                .from_svgo_plugin_string(&svgo_name)
                                .map_err(|_| napi::Error::new(Status::InvalidArg, format!("unknown job `{svgo_name}`")))?;
                        }
                        ValueType::Object => unsafe {
                            let plugin: Object = plugin.cast()?;
                            let svgo_name: Option<String> = plugin.get("name")?;
                            let Some(svgo_name) = svgo_name else {
                                return Err(napi::Error::new(Status::InvalidArg, "expected name to be string"));
                            };
                            let name = Self::from_svgo_plugin_to_snake_case(&svgo_name);
                            match name.as_str() {
                                $(stringify!($name) => oxvg_config.$name = Some(plugin.get("params")?.unwrap_or_default()),)+
                                _ => return Err(napi::Error::new(Status::InvalidArg, format!("unknown job `{name}`"))),
                            }
                        }
                        _ => return Err(napi::Error::new(Status::InvalidArg, "unexpected type")),
                    }
                }
                Ok(oxvg_config)
            }

            /// Converts a JSON value of SVGO's `Config["plugins"]` into [`Jobs`].
            ///
            /// Note that this will deduplicate any plugins listed.
            ///
            /// # Errors
            ///
            /// If a config file cannot be deserialized into jobs. This may fail even if
            /// the config is valid for SVGO, such as if
            ///
            /// - The config contains custom plugins
            /// - The plugin parameters are incompatible with OXVG
            /// - The underlying deserialization process fails
            ///
            /// If you believe an errors should be fixed, please raise an issue
            /// [here](https://github.com/noahbald/oxvg/issues)
            #[cfg(feature = "serde")]
            pub fn from_svgo_plugin_config(value: Option<Vec<serde_json::Value>>) -> Result<Self, serde_json::Error> {
                use serde::de::Error as _;

                let Some(plugins) = value else { return Ok(Self::default()) };

                let mut oxvg_config = Self::none();
                for plugin in plugins {
                    match plugin {
                        serde_json::Value::String(svgo_name) => {
                            oxvg_config
                                .from_svgo_plugin_string(&svgo_name)
                                .map_err(|_| serde_json::Error::custom(format!("unknown job `{svgo_name}`")))?;
                        }
                        serde_json::Value::Object(mut plugin) => {
                            let svgo_name = plugin.remove("name").ok_or_else(|| serde_json::Error::missing_field("name"))?;
                            let serde_json::Value::String(svgo_name) = svgo_name else {
                                return Err(serde_json::Error::custom("expected name to be string"));
                            };
                            let name = Self::from_svgo_plugin_to_snake_case(&svgo_name);
                            let params = plugin.remove("params");
                            if let Some(params) = params {
                                match name.as_str() {
                                    $(stringify!($name) => oxvg_config.$name = Some(serde_json::from_value(params)?),)+
                                    _ => return Err(serde_json::Error::custom(format!("unknown job `{name}`"))),
                                }
                            } else {
                                match name.as_str() {
                                    $(stringify!($name) => oxvg_config.$name = Some($job::default()),)+
                                    _ => return Err(serde_json::Error::custom(format!("unknown job `{name}`"))),
                                };
                            }
                        }
                        _ => return Err(serde_json::Error::custom(format!("unexpected type"))),
                    }
                }

                Ok(oxvg_config)
            }

            fn from_svgo_plugin_string(&mut self, svgo_name: &str) -> Result<(), ()> {
                if svgo_name == "preset-default" {
                    macro_rules! is_default {
                        ($_name:ident $_job:ident $_default:ident) => {
                            self.$_name = Some($_job::default())
                        };
                        ($_name:ident $_job:ident) => { () };
                    }
                    $(is_default!($name $job $($default)?);)+
                    return Ok(());
                }
                let name = Self::from_svgo_plugin_to_snake_case(&svgo_name);
                match name.as_str() {
                    $(stringify!($name) => self.$name = Some($job::default()),)+
                    _ => return Err(()),
                };
                Ok(())
            }

            fn from_svgo_plugin_to_snake_case(name: &str) -> String {
                let caps = name.chars().filter(|char| char.is_uppercase()).count();
                let mut snake = String::with_capacity(name.len() + caps);
                for char in name.chars() {
                    if char.is_uppercase() {
                        snake.push('_');
                    }
                    snake.push(char.to_ascii_lowercase());
                }
                snake
            }

            /// Overwrites `self`'s fields with the `Some` fields of `other`
            pub fn extend(&mut self, other: &Self) {
                $(if other.$name.is_some() {
                    self.$name = other.$name.clone();
                })+
            }

            /// Removes a job from the config via the specified name of the field, in `snake_case`
            pub fn omit(&mut self, name: &str) {
                match name {
                    $(stringify!($name) => self.$name = None,)+
                    _ => {}
                }
            }

            /// Produces a preset with nothing
            pub fn none() -> Self {
                Self {
                    $($name: None),+
                }
            }
        }
    };
}

jobs! {
    // Non default plugins
    precheck: Precheck,
    add_attributes_to_s_v_g_element: AddAttributesToSVGElement,
    add_classes_to_s_v_g_element: AddClassesToSVGElement,
    cleanup_list_of_values: CleanupListOfValues,
    convert_one_stop_gradients: ConvertOneStopGradients,
    convert_style_to_attrs: ConvertStyleToAttrs,
    remove_attributes_by_selector: RemoveAttributesBySelector,
    remove_attrs: RemoveAttrs,
    remove_dimensions: RemoveDimensions,
    remove_elements_by_attr: RemoveElementsByAttr,
    remove_off_canvas_paths: RemoveOffCanvasPaths,
    remove_raster_images: RemoveRasterImages,
    remove_scripts: RemoveScripts,
    remove_style_element: RemoveStyleElement,
    remove_title: RemoveTitle,
    remove_view_box: RemoveViewBox,
    reuse_paths: ReusePaths,
    remove_x_m_l_n_s: RemoveXMLNS,

    // Default plugins
    remove_doctype: RemoveDoctype (is_default: true),
    remove_x_m_l_proc_inst: RemoveXMLProcInst (is_default: true),
    remove_comments: RemoveComments (is_default: true),
    remove_deprecated_attrs: RemoveDeprecatedAttrs (is_default: true),
    remove_metadata: RemoveMetadata (is_default: true),
    remove_editors_n_s_data: RemoveEditorsNSData (is_default: true),
    cleanup_attrs: CleanupAttrs (is_default: true),
    merge_styles: MergeStyles (is_default: true),
    inline_styles: InlineStyles (is_default: true),
    minify_styles: MinifyStyles (is_default: true),
    cleanup_ids: CleanupIds (is_default: true),
    remove_useless_defs: RemoveUselessDefs (is_default: true),
    cleanup_numeric_values: CleanupNumericValues (is_default: true),
    convert_colors: ConvertColors (is_default: true),
    remove_unknowns_and_defaults: RemoveUnknownsAndDefaults (is_default: true),
    remove_non_inheritable_group_attrs: RemoveNonInheritableGroupAttrs (is_default: true),
    remove_useless_stroke_and_fill: RemoveUselessStrokeAndFill (is_default: true),
    cleanup_enable_background: CleanupEnableBackground (is_default: true),
    remove_hidden_elems: RemoveHiddenElems (is_default: true),
    remove_empty_text: RemoveEmptyText (is_default: true),
    convert_shape_to_path: ConvertShapeToPath (is_default: true),
    convert_ellipse_to_circle: ConvertEllipseToCircle (is_default: true),
    move_elems_attrs_to_group: MoveElemsAttrsToGroup (is_default: true),
    move_group_attrs_to_elems: MoveGroupAttrsToElems (is_default: true),
    collapse_groups: CollapseGroups (is_default: true),
    // NOTE: `apply_transforms` should be before `convert_path_data` in case the order is ever changed
    apply_transforms: ApplyTransforms (is_default: true),
    convert_path_data: ConvertPathData (is_default: true),
    convert_transform: ConvertTransform (is_default: true),
    remove_empty_attrs: RemoveEmptyAttrs (is_default: true),
    remove_empty_containers: RemoveEmptyContainers (is_default: true),
    remove_unused_n_s: RemoveUnusedNS (is_default: true),
    merge_paths: MergePaths (is_default: true),
    sort_attrs: SortAttrs (is_default: true),
    sort_defs_children: SortDefsChildren (is_default: true),
    remove_desc: RemoveDesc (is_default: true),

    // Final non-default plugins
    prefix_ids: PrefixIds, // Should run after `cleanup_ids`
    remove_xlink: RemoveXlink, // Should remove xlinks added by other jobs
}

impl Jobs {
    /// # Errors
    /// When any job fails for the first time
    pub fn run<'input, 'arena>(
        &self,
        root: Ref<'input, 'arena>,
        info: &Info<'input, 'arena>,
    ) -> Result<(), JobsError<'input>> {
        let Some(root_element) = Element::from_parent(root) else {
            log::warn!("No elements found in the document, skipping");
            return Ok(());
        };

        let count = self.run_jobs(&root_element, info)?;
        log::debug!("completed {count} jobs");
        Ok(())
    }

    /// Produces a preset focused on correctness
    pub fn safe() -> Self {
        Self {
            precheck: Some(Precheck::default()),
            remove_doctype: Some(RemoveDoctype::default()),
            remove_x_m_l_proc_inst: Some(RemoveXMLProcInst::default()),
            remove_comments: Some(RemoveComments::default()),
            remove_deprecated_attrs: Some(RemoveDeprecatedAttrs::default()),
            remove_metadata: Some(RemoveMetadata::default()),
            remove_editors_n_s_data: Some(RemoveEditorsNSData::default()),
            cleanup_attrs: Some(CleanupAttrs::default()),
            merge_styles: Some(MergeStyles::default()),
            inline_styles: Some(InlineStyles::default()),
            minify_styles: Some(MinifyStyles::default()),
            cleanup_ids: Some(CleanupIds::default()),
            remove_useless_defs: Some(RemoveUselessDefs::default()),
            cleanup_numeric_values: Some(CleanupNumericValues::default()),
            convert_colors: Some(ConvertColors::default()),
            remove_unknowns_and_defaults: Some(RemoveUnknownsAndDefaults::default()),
            remove_non_inheritable_group_attrs: Some(RemoveNonInheritableGroupAttrs::default()),
            remove_useless_stroke_and_fill: Some(RemoveUselessStrokeAndFill::default()),
            cleanup_enable_background: Some(CleanupEnableBackground::default()),
            remove_hidden_elems: Some(RemoveHiddenElems::default()),
            remove_empty_text: Some(RemoveEmptyText::default()),
            convert_shape_to_path: Some(ConvertShapeToPath::default()),
            convert_ellipse_to_circle: Some(ConvertEllipseToCircle::default()),
            move_elems_attrs_to_group: Some(MoveElemsAttrsToGroup::default()),
            move_group_attrs_to_elems: Some(MoveGroupAttrsToElems::default()),
            collapse_groups: Some(CollapseGroups::default()),
            apply_transforms: Some(ApplyTransforms::default()),
            convert_path_data: Some(ConvertPathData::default()),
            convert_transform: Some(ConvertTransform::default()),
            remove_empty_attrs: Some(RemoveEmptyAttrs::default()),
            remove_empty_containers: Some(RemoveEmptyContainers::default()),
            remove_unused_n_s: Some(RemoveUnusedNS::default()),
            merge_paths: Some(MergePaths::default()),
            sort_attrs: Some(SortAttrs::default()),
            sort_defs_children: Some(SortDefsChildren::default()),
            remove_desc: Some(RemoveDesc::default()),
            ..Self::none()
        }
    }
}

#[cfg(test)]
#[macro_export]
#[doc(hidden)]
macro_rules! test_config {
    ($config_json:literal, comment: $comment:literal$(,)?) => {
        $crate::jobs::test_config(
            $config_json,
            Some(concat!(
                r#"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- "#,
                $comment,
                r#" -->
    test
</svg>"#
            )),
        )
    };
    ($config_json:literal, svg: $svg:literal$(,)?) => {
        $crate::jobs::test_config($config_json, Some($svg))
    };
    ($config_json:literal) => {
        $crate::jobs::test_config($config_json, None)
    };
}

#[cfg(test)]
pub(crate) fn test_config(config_json: &str, svg: Option<&'static str>) -> anyhow::Result<String> {
    use oxvg_ast::{
        parse::roxmltree::{parse_with_options, ParsingOptions},
        serialize::{Node as _, Options, Space},
    };

    let jobs: Jobs = serde_json::from_str(config_json)?;
    parse_with_options(
        svg.unwrap_or(
            r#"<svg xmlns="http://www.w3.org/2000/svg">
    test
</svg>"#,
        ),
        ParsingOptions {
            allow_dtd: true,
            ..ParsingOptions::default()
        },
        |dom, allocator| {
            jobs.run(dom, &Info::new(allocator))
                .map_err(|e| anyhow::Error::msg(format!("{e}")))?;
            Ok(dom.serialize_with_options(Options {
                trim_whitespace: Space::Default,
                minify: true,
                ..Options::pretty()
            })?)
        },
    )?
}

#[test]
fn test_jobs() -> anyhow::Result<()> {
    test_config(
        r#"{ "addAttributesToSvgElement": {
            "attributes": { "foo": "bar" }
        } }"#,
        None,
    )
    .map(|_| ())
}