oxvg_collections 0.0.6

Collections of data and utilities about SVG
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Container for all the possible content that can be used by an attribute
use super::attribute::animation::{AttributeType, BeginEnd, CalcMode, ControlPoint};
use super::attribute::animation_addition::{Accumulate, Additive};
use super::attribute::animation_timing::{
    ClockValue, Dur, Fill, MinMax, RepeatCount, RepeatDur, Restart,
};
use super::attribute::aria::{
    AriaAutocomplete, AriaCurrent, AriaDropEffect, AriaHasPopup, AriaInvalid, AriaLive,
    AriaOrientation, AriaRelevant, AriaSort, IDReference, Role, Tristate,
};
use super::attribute::core_attrs::{
    Angle, Anything, Boolean, Class, Color, Frequency, FuncIRI, Id, Integer, Length, Name,
    NonWhitespace, Number, NumberOptionalNumber, Opacity, Paint, Percentage, SVGTransformList,
    Style, Time, TokenList, Url, IRI,
};
use super::attribute::filter_effect::{
    ChannelSelector, EdgeMode, In, OperatorFeComposite, OperatorFeMorphology,
    StitchTilesFeTurbulence, TypeFeColorMatrix, TypeFeTurbulence,
};
use super::attribute::fonts::{ArabicForm, Orientation};
use super::attribute::inheritable::Inheritable;
use super::attribute::list_of::{ListOf, Separators};
use super::attribute::path::{Path, Points};
use super::attribute::presentation::{
    AlignmentBaseline, BaselineShift, Clip, ClipPath, ColorInterpolation, ColorProfile,
    ColorRendering, Cursor, Direction, Display, DominantBaseline, EnableBackground, FillRule,
    FilterList, Font, FontFamily, FontSize, FontSizeAdjust, FontStretch, FontStyle, FontVariant,
    FontWeight, GlyphOrientationHorizontal, GlyphOrientationVertical, ImageRendering, Kerning,
    LengthOrNumber, LengthPercentage, Marker, Mask, MaskType, Overflow, PaintOrder, PointerEvents,
    Position, ShapeRendering, Spacing, StrokeDasharray, StrokeLinecap, StrokeLinejoin, TextAnchor,
    TextDecoration, TextRendering, UnicodeBidi, VectorEffect, Visibility, WritingMode,
};
use super::attribute::transfer_function::TransferFunctionType;
use super::attribute::uncategorised::{
    BlendMode, ColorProfileName, CrossOrigin, LengthAdjust, LinkType, MarkerUnits, MediaQueryList,
    MediaType, NumberPercentage, Orient, Origin, Playbackorder, PreserveAspectRatio, Radius, RefX,
    RefY, ReferrerPolicy, RenderingIntent, Rotate, SpreadMethod, Target, TextPathMethod,
    TextPathSide, TextPathSpacing, Timelinebegin, Transform, TrueFalse, TrueFalseUndefined,
    TypeAnimateTransform, Units, ViewBox, ZoomAndPan,
};
use super::attribute::xlink::{XLinkActuate, XLinkShow, XLinkType};
use super::attribute::xml::XmlSpace;
use lightningcss::values::string::CowArcStr;
use lightningcss::values::{
    alpha::AlphaValue, length::LengthValue, percentage::DimensionPercentage,
};
use lightningcss::visitor::Visit as _;
use lightningcss::{visit_types, visitor};
use std::ops::{Deref, DerefMut};

use crate::atom::Atom;

#[derive(Debug, PartialEq)]
/// A reference to the content type, as received from [`crate::attribute::Attr::value`]
/// or [`crate::attribute::Attr::value_mut`]
pub enum ContentTypeRef<'a, T: std::fmt::Debug + PartialEq> {
    /// A reference received from [`crate::attribute::Attr::value`]
    Ref(&'a T),
    /// A reference received from [`crate::attribute::Attr::value_mut`]
    RefMut(&'a mut T),
}
impl<T: std::fmt::Debug + PartialEq> Deref for ContentTypeRef<'_, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        match self {
            Self::Ref(t) => t,
            Self::RefMut(t) => t,
        }
    }
}
impl<T: std::fmt::Debug + PartialEq> DerefMut for ContentTypeRef<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            Self::Ref(_) => panic!("Cannot mutably deref ContentTypeRef::Ref"),
            Self::RefMut(t) => t,
        }
    }
}

enum ReferenceType {
    Url,
    Id,
    Class,
}
/// A reference to some ident found in the document
pub enum Reference<'a, 'input> {
    /// A reference to some ident found in an SVG value
    Atom(&'a mut Atom<'input>),
    /// A reference to some ident found in an SVG or CSS value
    Css(&'a mut CowArcStr<'input>),
}
impl Deref for Reference<'_, '_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Atom(atom) => atom,
            Self::Css(css) => css,
        }
    }
}
struct ReferenceVisitor<'input, F: FnMut(Reference<'_, 'input>)> {
    f: F,
    reference_type: ReferenceType,
    marker: std::marker::PhantomData<&'input ()>,
}
impl<'input, F: FnMut(Reference<'_, 'input>)> visitor::Visitor<'input>
    for ReferenceVisitor<'input, F>
{
    type Error = ();

    fn visit_types(&self) -> visitor::VisitTypes {
        if matches!(self.reference_type, ReferenceType::Url) {
            visit_types!(URLS)
        } else {
            visit_types!(SELECTORS)
        }
    }
    fn visit_url(
        &mut self,
        url: &mut lightningcss::values::url::Url<'input>,
    ) -> Result<(), Self::Error> {
        (self.f)(Reference::Css(&mut url.url));
        Ok(())
    }
    fn visit_selector(
        &mut self,
        selector: &mut lightningcss::selector::Selector<'input>,
    ) -> Result<(), Self::Error> {
        use lightningcss::{selector::Component, values::ident::Ident};
        selector.iter_mut_raw_match_order().for_each(|c| match c {
            Component::Class(Ident(ident))
                if matches!(self.reference_type, ReferenceType::Class) =>
            {
                (self.f)(Reference::Css(ident));
            }
            Component::ID(Ident(ident)) if matches!(self.reference_type, ReferenceType::Id) => {
                (self.f)(Reference::Css(ident));
            }
            _ => {}
        });
        Ok(())
    }
}

macro_rules! define_content_types {
    ($($name:ident($ty:ty)$(<$i:lifetime>)?,)+) => {
        #[derive(Debug, PartialEq)]
        /// A reference to an attribute's value, mutably mapped to its content type
        pub enum ContentType<'a, 'input> {
            $(
                #[doc=concat!("a `", stringify!($name), "` value")]
                $name(ContentTypeRef<'a, $ty>)$(<$i>)?,
            )+
            /// A set of a content-type separated by some deliminator
            ListOf(ListOf<Box<ContentType<'a, 'input>>, Separators>),
            /// A content type that can be inherited with the `"inherited"` keyword
            Inheritable(Inheritable<Box<ContentType<'a, 'input>>>),
        }

        /// An identifier for an attribute's content type
        #[derive(Debug)]
        pub enum ContentTypeId {
            $(
                #[doc=concat!("a `", stringify!($name), "` value")]
                $name,
            )+
            /// A set of a content-type separated by some deliminator
            ListOf(Box<ContentTypeId>, Separators),
            /// A content type that can be inherited with the `"inherited"` keyword
            Inheritable(Box<ContentTypeId>),
        }

        #[cfg(feature = "serialize")]
        impl oxvg_serialize::ToValue for ContentType<'_, '_> {
            fn write_value<W>(&self, dest: &mut oxvg_serialize::Printer<W>) -> Result<(), oxvg_serialize::error::PrinterError>
                where
                    W: std::fmt::Write {
                match self {
                    $(Self::$name(value) => value.write_value(dest),)+
                    Self::Inheritable(value) => value.write_value(dest),
                    Self::ListOf(value) => value.write_value(dest),
                }
            }
        }
    };
}
impl<'input> ContentType<'_, 'input> {
    /// Returns `true` when the attribute is equivalent to the attribute
    /// being omitted.
    ///
    /// e.g. `ContentType::Boolean(Boolean(None))` may resolve to `""`,
    /// but is not empty because it still provides a useful value.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Anything(value)
            | Self::IRI(value)
            | Self::Name(value)
            | Self::Url(value)
            | Self::MediaType(value) => value.is_empty(),
            Self::Id(value) | Self::IDReference(value) | Self::Class(value) => value.is_empty(),
            Self::Style(style) => {
                style.0.declarations.is_empty() && style.important_declarations.is_empty()
            }
            Self::SVGTransformList(transform_list) => transform_list.0.is_empty(),
            Self::ListOf(ListOf { list, .. }) => list.is_empty(),
            Self::ColorProfile(color_profile) => match &**color_profile {
                ColorProfile::NameOrIRI(value) => value.is_empty(),
                _ => false,
            },
            Self::ColorProfileName(color_profile_name) => match &**color_profile_name {
                ColorProfileName::Name(value) => value.is_empty(),
                ColorProfileName::Srgb => false,
            },
            Self::FilterList(filter_list) => match &**filter_list {
                FilterList::Filters(list) => list.is_empty(),
                FilterList::None => false,
            },
            Self::FontFamily(font_family) => font_family.0.list.is_empty(),
            Self::Mask(mark) => mark.0.list.is_empty(),
            Self::StrokeDasharray(stroke_dasharray) => match &**stroke_dasharray {
                StrokeDasharray::Values(values) => values.is_empty(),
                StrokeDasharray::None => false,
            },
            Self::TokenList(token_list) => token_list.0 .0.is_empty(),
            Self::In(value) => match &**value {
                In::Reference(value) => value.is_empty(),
                _ => false,
            },
            Self::MediaQueryList(media_query_list) => media_query_list.0.media_queries.is_empty(),
            Self::Path(path) => path.0 .0.is_empty(),
            Self::Points(points) => points.0 .0.is_empty(),
            Self::Target(target) => match &**target {
                Target::XMLName(value) => value.is_empty(),
                _ => false,
            },
            Self::Inheritable(Inheritable::Defined(value)) => value.is_empty(),
            _ => false,
        }
    }

    /// For a value, visits any urls it may contain.
    pub fn visit_url<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        if let Self::ListOf(ListOf { list, .. }) = self {
            for item in list.iter_mut() {
                item.visit_url_not_list(&mut f);
            }
            return;
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_url(f);
            return;
        }
        self.visit_url_not_list(f);
    }
    fn visit_url_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        use lightningcss::values::url::Url;
        use std::marker::PhantomData;
        match self {
            Self::ColorProfile(ContentTypeRef::RefMut(ColorProfile::NameOrIRI(url))) => {
                f(Reference::Atom(url));
            }
            Self::FuncIRI(ContentTypeRef::RefMut(url)) | Self::Url(ContentTypeRef::RefMut(url)) => {
                f(Reference::Atom(url));
            }
            Self::ClipPath(ContentTypeRef::RefMut(ClipPath::Url(Url { url, .. })))
            | Self::Paint(ContentTypeRef::RefMut(Paint::Url {
                url: Url { url, .. },
                ..
            }))
            | Self::Marker(ContentTypeRef::RefMut(Marker::Url(Url { url, .. }))) => {
                f(Reference::Css(url));
            }
            Self::Mask(ContentTypeRef::RefMut(Mask(mask))) => {
                use lightningcss::values::image::{Image, ImageSet};
                fn visit_image<'input, F>(image: &mut Image<'input>, f: &mut F)
                where
                    F: FnMut(Reference<'_, 'input>),
                {
                    match image {
                        Image::Url(Url { url, .. }) => f(Reference::Css(url)),
                        Image::ImageSet(ImageSet { options, .. }) => options
                            .iter_mut()
                            .map(|options| &mut options.image)
                            .for_each(|image| visit_image(image, f)),
                        _ => {}
                    }
                }
                mask.list
                    .iter_mut()
                    .map(|mask| &mut mask.image)
                    .for_each(|image| visit_image(image, &mut f));
            }
            Self::FilterList(filters) => {
                filters
                    .visit(&mut ReferenceVisitor {
                        f,
                        reference_type: ReferenceType::Url,
                        marker: PhantomData,
                    })
                    .ok();
            }
            Self::Style(style) => {
                style
                    .0
                    .visit(&mut ReferenceVisitor {
                        f,
                        reference_type: ReferenceType::Url,
                        marker: PhantomData,
                    })
                    .ok();
            }
            _ => {}
        }
    }

    /// For an attribute, visits any IDs it may contain or reference, including [`crate::attribute::Attr::Id`]
    pub fn visit_id<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        if let Self::ListOf(ListOf { list, .. }) = self {
            for item in list.iter_mut() {
                item.visit_id_not_list(&mut f);
            }
            return;
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_id(f);
            return;
        }
        self.visit_id_not_list(f);
    }
    fn visit_id_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        match self {
            Self::IDReference(value) | Self::Id(value) => f(Reference::Atom(&mut (value).0)),
            Self::BeginEnd(begin_end) => match &mut **begin_end {
                BeginEnd::SyncbaseValue { id, .. }
                | BeginEnd::EventValue { id: Some(id), .. }
                | BeginEnd::RepeatValue { id: Some(id), .. } => f(Reference::Atom(id)),
                _ => {}
            },
            Self::Style(style) => {
                style
                    .0
                    .visit(&mut ReferenceVisitor {
                        f,
                        reference_type: ReferenceType::Id,
                        marker: std::marker::PhantomData,
                    })
                    .ok();
            }
            _ => {}
        }
    }

    /// For an attribute, visits any classes it may contain or reference, including [`crate::attribute::Attr::Class`]
    pub fn visit_class<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        if let Self::ListOf(ListOf { list, .. }) = self {
            for item in list.iter_mut() {
                item.visit_class_not_list(&mut f);
            }
            return;
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_class(f);
            return;
        }
        self.visit_class_not_list(f);
    }
    fn visit_class_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(Reference<'_, 'input>),
    {
        match self {
            Self::Class(ContentTypeRef::RefMut(NonWhitespace(class))) => f(Reference::Atom(class)),
            Self::Style(style) => {
                style
                    .0
                    .visit(&mut ReferenceVisitor {
                        f,
                        reference_type: ReferenceType::Class,
                        marker: std::marker::PhantomData,
                    })
                    .ok();
            }
            _ => {}
        }
    }

    /// Visits any color values in the content type, excluding from [`ContentType::Style`]
    pub fn visit_color<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Color),
    {
        if let Self::ListOf(ListOf { list, .. }) = self {
            for item in list.iter_mut() {
                item.visit_color_not_list(&mut f);
            }
            return;
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_color(f);
            return;
        }
        self.visit_color_not_list(f);
    }
    fn visit_color_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Color),
    {
        match self {
            Self::Color(color) => f(color),
            Self::Paint(paint) => {
                if let Paint::Color(color) = &mut **paint {
                    f(color);
                }
            }
            Self::TextDecoration(text_decoration) => {
                let TextDecoration { color, .. } = &mut **text_decoration;
                f(color);
            }
            _ => {}
        }
    }

    /// Visits any length values in the content type.
    ///
    /// # Arguments
    ///
    /// * `f` - A closure that will run for a visited value
    /// * `follow_list` - Whether to visit list values or not. If this is set to `true`, single values will not be visited
    pub fn visit_length_value<F>(&mut self, mut f: F, follow_list: bool)
    where
        F: FnMut(&mut LengthValue),
    {
        // Assertion should panic when not `ContentTypeRef::RefMut`
        debug_assert!(Some(&mut *self).is_some());

        if follow_list {
            match self {
                Self::StrokeDasharray(ContentTypeRef::RefMut(StrokeDasharray::Values(values))) => {
                    for d in values.iter_mut() {
                        if let DimensionPercentage::Dimension(l) = d {
                            f(l);
                        }
                    }
                    return;
                }
                Self::ListOf(ListOf { list, .. }) => {
                    for item in list.iter_mut() {
                        item.visit_length_value_not_list(&mut f);
                    }
                    return;
                }
                _ => {}
            }
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_length_value(f, follow_list);
            return;
        }
        self.visit_length_value_not_list(f);
    }
    fn visit_length_value_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut LengthValue),
    {
        match self {
            Self::Spacing(ContentTypeRef::RefMut(Spacing::Length(
                lightningcss::values::length::Length::Value(l),
            )))
            | Self::Kerning(ContentTypeRef::RefMut(Kerning::Length(Length::Length(l))))
            | Self::LengthPercentage(ContentTypeRef::RefMut(LengthPercentage(
                DimensionPercentage::Dimension(l),
            )))
            | Self::Length(ContentTypeRef::RefMut(Length::Length(l))) => f(l),
            _ => {}
        }
    }

    /// Visits any floating numbers in the content type, excluding numbers that are part of a [`LengthValue`].
    ///
    /// Use [`Self::visit_length_value`] to visit numbers within a [`LengthValue`].
    ///
    /// # Arguments
    ///
    /// * `f` - A closure that will run for a visited value
    /// * `follow_list` - Whether to visit list values or not. If this is set to `true`, single values will not be visited
    pub fn visit_float<F>(&mut self, mut f: F, follow_list: bool)
    where
        F: FnMut(&mut f32),
    {
        // Assertion should panic when not `ContentTypeRef::RefMut`
        debug_assert!(Some(&mut *self).is_some());

        self.visit_length_value(
            |l| {
                #[allow(clippy::enum_glob_use)]
                use LengthValue::*;
                match l {
                    Px(n) | In(n) | Cm(n) | Mm(n) | Q(n) | Pt(n) | Pc(n) | Em(n) | Rem(n)
                    | Ex(n) | Rex(n) | Ch(n) | Rch(n) | Cap(n) | Rcap(n) | Ic(n) | Ric(n)
                    | Lh(n) | Rlh(n) | Vw(n) | Lvw(n) | Svw(n) | Dvw(n) | Cqw(n) | Vh(n)
                    | Lvh(n) | Svh(n) | Dvh(n) | Cqh(n) | Vi(n) | Svi(n) | Lvi(n) | Dvi(n)
                    | Cqi(n) | Vb(n) | Svb(n) | Lvb(n) | Dvb(n) | Cqb(n) | Vmin(n) | Svmin(n)
                    | Lvmin(n) | Dvmin(n) | Cqmin(n) | Vmax(n) | Svmax(n) | Lvmax(n) | Dvmax(n)
                    | Cqmax(n) => f(n),
                }
            },
            follow_list,
        );
        if follow_list {
            match self {
                Self::EnableBackground(ContentTypeRef::RefMut(EnableBackground::New(Some((
                    x,
                    y,
                    width,
                    height,
                ))))) => {
                    f(x);
                    f(y);
                    f(width);
                    f(height);
                    return;
                }
                Self::StrokeDasharray(ContentTypeRef::RefMut(StrokeDasharray::Values(values))) => {
                    for d in values.iter_mut() {
                        if let DimensionPercentage::Percentage(n) = d {
                            n.0 *= 100.0;
                            f(&mut n.0);
                            n.0 /= 100.0;
                        }
                    }
                    return;
                }
                Self::ControlPoint(ContentTypeRef::RefMut(ControlPoint(values))) => {
                    values.iter_mut().for_each(f);
                    return;
                }
                Self::ListOf(ListOf { list, .. }) => {
                    for item in list.iter_mut() {
                        item.visit_float_not_list(&mut f);
                    }
                    return;
                }
                _ => {}
            }
        } else if let Self::Inheritable(Inheritable::Defined(value)) = self {
            value.visit_float(f, follow_list);
            return;
        }
        self.visit_float_not_list(f);
    }
    fn visit_float_not_list<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut f32),
    {
        match self {
            // Length
            Self::Length(ContentTypeRef::RefMut(Length::Number(n)))
            | Self::Kerning(ContentTypeRef::RefMut(Kerning::Length(Length::Number(n))))
            // Angle
            | Self::Angle(ContentTypeRef::RefMut(
                Angle::Deg(n) | Angle::Rad(n) | Angle::Grad(n) | Angle::Turn(n),
            ))
            // Frequency
            | Self::Frequency(ContentTypeRef::RefMut(Frequency::Hz(n) | Frequency::KHz(n)))
            // Opacity
            | Self::Opacity(ContentTypeRef::RefMut(AlphaValue(n)))
            // RepeatCount
            | Self::RepeatCount(ContentTypeRef::RefMut(RepeatCount::Number(n)))
            // Rotate
            | Self::Rotate(ContentTypeRef::RefMut(Rotate::Number(n)))
            // Number
            | Self::NumberPercentage(ContentTypeRef::RefMut(NumberPercentage::Number(n)))
            // LengthOrNumber
            | Self::LengthOrNumber(ContentTypeRef::RefMut(LengthOrNumber::Number(n)))
            | Self::RefX(ContentTypeRef::RefMut(RefX::LengthOrNumber(LengthOrNumber::Number(n))))
            | Self::RefY(ContentTypeRef::RefMut(RefY::LengthOrNumber(LengthOrNumber::Number(n)))) => f(n),
            // Number
            Self::Number(ContentTypeRef::RefMut(n)) => f(n),
            Self::Percentage(ContentTypeRef::RefMut(Percentage(p)))
            | Self::Radius(ContentTypeRef::RefMut(Radius::LengthPercentage(LengthPercentage(DimensionPercentage::Percentage(Percentage(p))))))
            | Self::LengthPercentage(ContentTypeRef::RefMut(LengthPercentage(DimensionPercentage::Percentage(Percentage(p)))))
            | Self::Length(ContentTypeRef::RefMut(Length::Percentage(Percentage(p))))
            | Self::Kerning(ContentTypeRef::RefMut(Kerning::Length(Length::Percentage(Percentage(p)))))
            | Self::NumberPercentage(ContentTypeRef::RefMut(NumberPercentage::Percentage(Percentage(p)))) => {
                // Normalise percentage to display value
                *p *= 100.0;
                f(p);
                *p /= 100.0;
            }
            Self::NumberOptionalNumber(ContentTypeRef::RefMut(NumberOptionalNumber(n, n2))) => {
                f(n);
                if let Some(n2) = n2 {
                    f(n2);
                }
            }
            Self::ViewBox(ContentTypeRef::RefMut(ViewBox {
                min_x,
                min_y,
                width,
                height,
            })) => {
                f(min_x);
                f(min_y);
                f(width);
                f(height);
            }
            _ => {}
        }
    }

    /// Rounds any safely roundable numbers in the content type
    pub fn round(&mut self, float_precision: i32, convert_px: bool, round_list: bool) {
        debug_assert!(
            float_precision <= 5,
            "rounding precision should be no greater than 5"
        );
        if let Self::Points(points) = self {
            let factor = 10.0_f64.powi(float_precision);
            let round_float = |n: &mut f64| *n = (*n * factor).round() / factor;
            for point in &mut points.0 .0 {
                point.args_mut().iter_mut().for_each(round_float);
            }
            return;
        }

        let factor = 10.0_f32.powi(float_precision);
        let round_float = |n: &mut f32| *n = (*n * factor).round() / factor;
        if convert_px {
            self.visit_length_value(
                |l| {
                    if let Some(px) = l.to_px() {
                        *l = LengthValue::Px(px);
                    }
                },
                round_list,
            );
        }
        self.visit_float(round_float, round_list);
    }
}

define_content_types! {
    Angle(Angle),
    Anything(Anything<'input>),
    ArabicForm(ArabicForm),
    Boolean(Boolean<'input>),
    ClockValue(ClockValue),
    Color(Color),
    Frequency(Frequency),
    FuncIRI(FuncIRI<'input>),
    Integer(Integer),
    IRI(IRI<'input>),
    Length(Length),
    Name(Name<'input>),
    Number(Number),
    NumberOptionalNumber(NumberOptionalNumber),
    Opacity(Opacity),
    Paint(Paint<'input>),
    Percentage(Percentage),
    Style(Style<'input>),
    SVGTransformList(SVGTransformList),
    Time(Time),
    Url(Url<'input>),

    // ARIA specific
    AriaAutocomplete(AriaAutocomplete),
    AriaCurrent(AriaCurrent),
    AriaDropEffect(AriaDropEffect),
    AriaHasPopup(AriaHasPopup),
    AriaInvalid(AriaInvalid),
    AriaLive(AriaLive),
    AriaOrientation(AriaOrientation),
    AriaRelevant(AriaRelevant),
    AriaSort(AriaSort),
    IDReference(IDReference<'input>),
    Role(Role),
    Tristate(Tristate),

    // CSS/Presentation values
    // https://www.w3.org/TR/2011/REC-SVG11-20110816/propidx.html
    AlignmentBaseline(AlignmentBaseline),
    BaselineShift(BaselineShift),
    Clip(Clip),
    ClipPath(ClipPath<'input>),
    ColorInterpolation(ColorInterpolation),
    ColorInterpolationFilters(ColorInterpolation),
    ColorProfile(ColorProfile<'input>),
    ColorRendering(ColorRendering),
    Cursor(Cursor<'input>),
    Direction(Direction),
    Display(Display),
    DominantBaseline(DominantBaseline),
    EnableBackground(EnableBackground),
    FillRule(FillRule),
    FilterList(FilterList<'input>),
    Font(Font<'input>),
    FontFamily(FontFamily<'input>),
    FontSize(FontSize),
    FontSizeAdjust(FontSizeAdjust),
    FontStretch(FontStretch),
    FontStyle(FontStyle),
    FontVariant(FontVariant),
    FontWeight(FontWeight),
    GlyphOrientationHorizontal(GlyphOrientationHorizontal),
    GlyphOrientationVertical(GlyphOrientationVertical),
    ImageRendering(ImageRendering),
    Kerning(Kerning),
    LengthPercentage(LengthPercentage),
    LengthOrNumber(LengthOrNumber),
    Marker(Marker<'input>),
    MarkerUnits(MarkerUnits),
    Mask(Mask<'input>),
    MaskType(MaskType),
    Orientation(Orientation),
    Overflow(Overflow),
    PaintOrder(PaintOrder),
    PointerEvents(PointerEvents),
    Position(Position),
    ShapeRendering(ShapeRendering),
    Spacing(Spacing),
    StrokeDasharray(StrokeDasharray),
    StrokeLinecap(StrokeLinecap),
    StrokeLinejoin(StrokeLinejoin),
    TextAnchor(TextAnchor),
    TextDecoration(TextDecoration),
    TextRendering(TextRendering),
    TokenList(TokenList<'input>),
    UnicodeBidi(UnicodeBidi),
    VectorEffect(VectorEffect),
    Visibility(Visibility),
    WritingMode(WritingMode),

    // Attr specific
    Accumulate(Accumulate),
    Additive(Additive),
    AttributeType(AttributeType),
    BeginEnd(BeginEnd<'input>),
    BlendMode(BlendMode),
    CalcMode(CalcMode),
    ChannelSelector(ChannelSelector),
    Class(Class<'input>),
    ColorProfileName(ColorProfileName<'input>),
    ControlPoint(ControlPoint),
    CrossOrigin(CrossOrigin),
    Dur(Dur),
    TypeFeColorMatrix(TypeFeColorMatrix),
    OperatorFeComposite(OperatorFeComposite),
    EdgeMode(EdgeMode),
    OperatorFeMorphology(OperatorFeMorphology),
    StitchTilesFeTurbulence(StitchTilesFeTurbulence),
    TypeFeTurbulence(TypeFeTurbulence),
    Fill(Fill),
    Units(Units),
    Id(Id<'input>),
    In(In<'input>),
    LengthAdjust(LengthAdjust),
    LinkType(LinkType),
    MediaType(MediaType<'input>),
    MediaQueryList(MediaQueryList<'input>),
    MinMax(MinMax),
    NumberPercentage(NumberPercentage),
    Orient(Orient),
    Origin(Origin),
    Path(Path<'input>),
    Playbackorder(Playbackorder),
    Points(Points<'input>),
    PreserveAspectRatio(PreserveAspectRatio),
    Radius(Radius),
    RefX(RefX),
    RefY(RefY),
    ReferrerPolicy(ReferrerPolicy),
    RenderingIntent(RenderingIntent),
    RepeatCount(RepeatCount),
    RepeatDur(RepeatDur),
    Restart(Restart),
    Rotate(Rotate),
    SpreadMethod(SpreadMethod),
    Target(Target<'input>),
    TextPathMethod(TextPathMethod),
    TextPathSpacing(TextPathSpacing),
    TextPathSide(TextPathSide),
    Timelinebegin(Timelinebegin),
    TransferFunctionType(TransferFunctionType),
    Transform(Transform),
    TrueFalse(TrueFalse),
    TrueFalseUndefined(TrueFalseUndefined),
    TypeAnimateTransform(TypeAnimateTransform),
    ViewBox(ViewBox),
    XLinkActuate(XLinkActuate),
    XLinkShow(XLinkShow),
    XLinkType(XLinkType),
    XmlSpace(XmlSpace),
    ZoomAndPan(ZoomAndPan),
}