sauron_core/svg/
attributes.rs

1//! provides functions and macros for building svg attributes
2use crate::vdom::AttributeValue;
3use crate::vdom::Value;
4use crate::vdom::{attr, attr_ns};
5pub use commons::*;
6pub use special::*;
7
8pub(crate) const XLINK_NAMESPACE: &str = "http://www.w3.org/1999/xlink";
9
10macro_rules! declare_xlink_attributes {
11    ( $(
12         $(#[$attr:meta])*
13         $name:ident => $attribute:tt;
14       )*
15     ) => {
16        $(
17            /// creates a function where the function name is the attribute name of the svg element
18            $(#[$attr])*
19            #[inline]
20            #[allow(non_snake_case)]
21            pub fn $name<V, MSG>(v: V) -> crate::vdom::Attribute<MSG>
22                where V: Into<Value>,
23                {
24                    attr_ns(Some(XLINK_NAMESPACE), $attribute, AttributeValue::from(v.into()))
25                }
26         )*
27
28        #[cfg(feature = "with-lookup")]
29        /// Svg attributes with xlink namespace
30        pub const SVG_ATTRS_XLINK:&[(&'static str,&'static str)] = &[$((stringify!($name),$attribute),)*];
31    }
32}
33
34/// declare svg attributes, at the same time fill up the
35/// SVG_ATTR const with all the common svg attributes
36macro_rules! declare_svg_attributes{
37    ( $(
38         $(#[$attr:meta])*
39         $name:ident;
40       )*
41     ) => {
42        declare_attributes!{ $($name;)*}
43
44        #[cfg(feature = "with-lookup")]
45        /// These are most commonly used svg attributes
46        pub const SVG_ATTRS:&[&'static str] = &[$(stringify!($name),)*];
47    }
48}
49
50macro_rules! declare_svg_attributes_non_common{
51    ( $(
52         $(#[$attr:meta])*
53         $name:ident;
54       )*
55     ) => {
56        declare_attributes!{ $($name;)*}
57
58        #[cfg(feature = "with-lookup")]
59        /// These are most commonly used svg attributes
60        pub const SVG_ATTRS_NON_COMMON:&[&'static str] = &[$(stringify!($name),)*];
61    }
62}
63
64macro_rules! declare_svg_attributes_special{
65    ( $(
66         $(#[$attr:meta])*
67         $name:ident => $attribute:tt;
68       )*
69     ) => {
70        declare_attributes!{ $($name => $attribute;)*}
71
72        #[cfg(feature = "with-lookup")]
73        /// These are svg attributes with names that are non proper rust identifier therefore they
74        /// are handled differently. ie: (color-profile, accent-height, etc)
75        pub const SVG_ATTRS_SPECIAL:&[(&'static str,&'static str)] = &[$((stringify!($name),$attribute),)*];
76    }
77}
78
79/// common svg attributes
80pub mod commons {
81    use super::*;
82
83    // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
84    // complete list svg attributes
85    declare_svg_attributes! {
86        accumulate;
87        additive;
88        alphabetic;
89        amplitude;
90        ascent;
91        azimuth;
92        bbox;
93        begin;
94        bias;
95        by;
96        clip;
97        cursor;
98        cx;
99        cy;
100        d;
101        decelerate;
102        descent;
103        direction;
104        display;
105        divisor;
106        dur;
107        dx;
108        dy;
109        elevation;
110        end;
111        exponent;
112        fill;
113        format;
114        from;
115        fr;
116        fx;
117        fy;
118        g1;
119        g2;
120        hanging;
121        ideographic;
122        in2;
123        intercept;
124        k;
125        k1;
126        k2;
127        k3;
128        k4;
129        kerning;
130        local;
131        mathematical;
132        mode;
133        offset;
134        opacity;
135        operator;
136        order;
137        orient;
138        orientation;
139        origin;
140        overflow;
141        points;
142        r;
143        radius;
144        restart;
145        result;
146        rotate;
147        rx;
148        ry;
149        scale;
150        seed;
151        slope;
152        spacing;
153        speed;
154        stemh;
155        stemv;
156        string;
157        stroke;
158        to;
159        transform;
160        u1;
161        u2;
162        unicode;
163        values;
164        version;
165        visibility;
166        widths;
167        x;
168        x1;
169        x2;
170        xmlns;
171        y;
172        y1;
173        y2;
174        z;
175    }
176}
177
178declare_svg_attributes_non_common! {
179        color; //conflicts with html::attributes::color
180        filter; //conflicts with svg::filter
181        height; //conflicts with html::attributes::height
182        href; //conflicts with html::attributes::href;
183        hreflang;
184        lang;
185        mask; //conflicts with svg::mask
186        max;
187        media;
188        method;
189        min;
190        name;
191        ping;
192        rel;
193        tabindex;
194        target;
195        width;
196}
197
198/// special svg attributes
199pub mod special {
200    use super::*;
201    // These are attributes that is exposed in such a way that is consistent to rust conventions
202    // This includes exposing the following:
203    // - reserved keywords
204    // - kebab-case attributes
205    // - namespaced/colon separated attributes such as xml::lang
206    // - camelCase attributes
207    declare_svg_attributes_special! {
208
209        ///////////////////////////////
210        // rust reserved keywords that are svg attributes
211        ///////////////////////////////
212        r#in => "in";
213
214        /////////////////////////////////
215        // kebab-case svg attributes
216        /////////////////////////////////
217        accent_height => "accent-height";
218        alignment_baseline => "alignment-baseline";
219        arabic_form => "arabic-form";
220        baseline_shift => "baseline-shift";
221        cap_height => "cap-height";
222        clip_path => "clip-path";
223        clip_rule => "clip-rule";
224        color_interpolation => "color-interpolation";
225        color_interpolation_filters => "color-interpolation-filters";
226        color_rendering => "color-rendering";
227        dominant_baseline => "dominant-baseline";
228        enable_background => "enable-background";
229        fill_opacity => "fill-opacity";
230        fill_rule => "fill-rule";
231        flood_color => "flood-color";
232        flood_opacity => "flood-opacity";
233        font_size_adjust => "font-size-adjust";
234        font_stretch => "font-stretch";
235        font_style => "font-style";
236        font_variant => "font-variant";
237        font_weight => "font-weight";
238        glyph_name => "glyph-name";
239        glyph_orientation_horizontal => "glyph-orientation-horizontal";
240        glyph_orientation_vertical => "glyph-orientation-vertical";
241        horiz_adv_x => "horiz-adv-x";
242        horiz_origin_x => "horiz-origin-x";
243        image_rendering => "image-rendering";
244        letter_spacing => "letter-spacing";
245        lighting_color => "lighting-color";
246        marker_end => "marker-end";
247        marker_mid => "marker-mid";
248        marker_start => "marker-start";
249        overline_position => "overline-position";
250        overline_thickness => "overline-thickness";
251        panose_1 => "panose-1";
252        paint_order => "paint-order";
253        pointer_events => "pointer-events";
254        rendering_intent => "rendering-intent";
255        shape_rendering => "shape-rendering";
256        stop_color => "stop-color";
257        stop_opacity => "stop-opacity";
258        strikethrough_position => "strikethrough-position";
259        strikethrough_thickness => "strikethrough-thickness";
260        stroke_dasharray => "stroke-dasharray";
261        stroke_dashoffset => "stroke-dashoffset";
262        stroke_linecap => "stroke-linecap";
263        stroke_linejoin => "stroke-linejoin";
264        stroke_miterlimit => "stroke-miterlimit";
265        stroke_opacity => "stroke-opacity";
266        stroke_width => "stroke-width";
267        text_anchor => "text-anchor";
268        text_decoration => "text-decoration";
269        text_rendering => "text-rendering";
270        underline_position => "underline-position";
271        underline_thickness => "underline-thickness";
272        unicode_bidi => "unicode-bidi";
273        unicode_range => "unicode-range";
274        units_per_em => "units-per-em";
275        v_alphabetic => "v-alphabetic";
276        v_hanging => "v-hanging";
277        v_ideographic => "v-ideographic";
278        v_mathematical => "v-mathematical";
279        vector_effect => "vector-effect";
280        vert_adv_y => "vert-adv-y";
281        vert_origin_x => "vert-origin-x";
282        vert_origin_y => "vert-origin-y";
283        word_spacing => "word-spacing";
284        writing_mode => "writing-mode";
285        x_height => "x-height";
286
287        ////////////////////////////////////////
288        // namespaced svg attributes
289        ////////////////////////////////////////
290        xml_base => "xml:base";
291        xml_lang => "xml:lang";
292        xml_space => "xml:space";
293        xmlns_xlink => "xmlns:xlink";
294
295        /////////////////////////////////
296        // camelCase svg attributes
297        /////////////////////////////////
298        allow_reorder => "allowReorder";
299        attribute_name => "attributeName";
300        attribute_type => "attributeType";
301        auto_reverse => "autoReverse";
302        base_frequency => "baseFrequency";
303        base_profile => "baseProfile";
304        calc_mode => "calcMode";
305        clip_path_units => "clipPathUnits";
306        content_script_type => "contentScriptType";
307        content_style_type => "contentStyleType";
308        diffuse_constant => "diffuseConstant";
309        edge_mode => "edgeMode";
310        external_resources_required => "externalResourcesRequired";
311        filter_res => "filterRes";
312        filter_units => "filterUnits";
313        glyph_ref => "glyphRef";
314        gradient_transform => "gradientTransform";
315        gradient_units => "gradientUnits";
316        kernel_matrix => "kernelMatrix";
317        kernel_unit_length => "kernelUnitLength";
318        key_points => "keyPoints";
319        key_splines => "keySplines";
320        key_times => "keyTimes";
321        length_adjust => "lengthAdjust";
322        limiting_coneAngle => "limitingConeAngle";
323        marker_height => "markerHeight";
324        marker_units => "markerUnits";
325        marker_width => "markerWidth";
326        mask_content_units => "maskContentUnits";
327        mask_units => "maskUnits";
328        num_octaves => "numOctaves";
329        path_length => "pathLength";
330        pattern_content_units => "patternContentUnits";
331        pattern_transform => "patternTransform";
332        pattern_units => "patternUnits";
333        points_at_x => "pointsAtX";
334        points_at_y => "pointsAtY";
335        points_at_z => "pointsAtZ";
336        preserve_alpha => "preserveAlpha";
337        preserve_aspect_ratio => "preserveAspectRatio";
338        primitive_units => "primitiveUnits";
339        referrer_policy => "referrerPolicy";
340        ref_x => "refX";
341        ref_y => "refY";
342        repeat_count => "repeatCount";
343        repeat_dur => "repeatDur";
344        required_extensions => "requiredExtensions";
345        required_features => "requiredFeatures";
346        specular_constant => "specularConstant";
347        specular_exponent => "specularExponent";
348        spread_method => "spreadMethod";
349        start_offset => "startOffset";
350        std_deviation => "stdDeviation";
351        stitch_tiles => "stitchTiles";
352        surface_scale => "surfaceScale";
353        system_language => "systemLanguage";
354        table_values => "tableValues";
355        target_x => "targetX";
356        target_y => "targetY";
357        text_length => "textLength";
358        view_box => "viewBox";
359        view_target => "viewTarget";
360        x_channel_selector => "xChannelSelector";
361        y_channel_selector => "yChannelSelector";
362        zoom_and_pan => "zoomAndPan";
363    }
364
365    declare_xlink_attributes! {
366        xlink_actuate => "actuate";
367        xlink_arcrole => "arcrole";
368        xlink_href => "href";
369        xlink_role => "role";
370        xlink_show => "show";
371        xlink_title => "title";
372        xlink_type => "type";
373    }
374}