summaryrefslogtreecommitdiffstats
path: root/servo/components/style/properties/shorthands/svg.mako.rs
blob: cf34b116eeadeb82709e1dfbfb96544eae2d49cc (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

<%namespace name="helpers" file="/helpers.mako.rs" />

<%helpers:shorthand name="mask" engines="gecko" extra_prefixes="webkit"
                    flags="SHORTHAND_IN_GETCS"
                    sub_properties="mask-mode mask-repeat mask-clip mask-origin mask-composite mask-position-x
                                    mask-position-y mask-size mask-image"
                    spec="https://drafts.fxtf.org/css-masking/#propdef-mask">
    use crate::properties::longhands::{mask_mode, mask_repeat, mask_clip, mask_origin, mask_composite, mask_position_x,
                                mask_position_y};
    use crate::properties::longhands::{mask_size, mask_image};
    use crate::values::specified::{Position, PositionComponent};
    use crate::parser::Parse;

    // FIXME(emilio): These two mask types should be the same!
    impl From<mask_origin::single_value::SpecifiedValue> for mask_clip::single_value::SpecifiedValue {
        fn from(origin: mask_origin::single_value::SpecifiedValue) -> mask_clip::single_value::SpecifiedValue {
            match origin {
                mask_origin::single_value::SpecifiedValue::ContentBox =>
                    mask_clip::single_value::SpecifiedValue::ContentBox,
                mask_origin::single_value::SpecifiedValue::PaddingBox =>
                    mask_clip::single_value::SpecifiedValue::PaddingBox ,
                mask_origin::single_value::SpecifiedValue::BorderBox =>
                    mask_clip::single_value::SpecifiedValue::BorderBox,
                % if engine == "gecko":
                mask_origin::single_value::SpecifiedValue::FillBox =>
                    mask_clip::single_value::SpecifiedValue::FillBox ,
                mask_origin::single_value::SpecifiedValue::StrokeBox =>
                    mask_clip::single_value::SpecifiedValue::StrokeBox,
                mask_origin::single_value::SpecifiedValue::ViewBox=>
                    mask_clip::single_value::SpecifiedValue::ViewBox,
                % endif
            }
        }
    }

    pub fn parse_value<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Longhands, ParseError<'i>> {
        % for name in "image mode position_x position_y size repeat origin clip composite".split():
        // Vec grows from 0 to 4 by default on first push().  So allocate with
        // capacity 1, so in the common case of only one item we don't way
        // overallocate, then shrink.  Note that we always push at least one
        // item if parsing succeeds.
        let mut mask_${name} = Vec::with_capacity(1);
        % endfor

        input.parse_comma_separated(|input| {
            % for name in "image mode position size repeat origin clip composite".split():
                let mut ${name} = None;
            % endfor
            loop {
                if image.is_none() {
                    if let Ok(value) = input.try_parse(|input| mask_image::single_value
                                                                   ::parse(context, input)) {
                        image = Some(value);
                        continue
                    }
                }
                if position.is_none() {
                    if let Ok(value) = input.try_parse(|input| Position::parse(context, input)) {
                        position = Some(value);

                        // Parse mask size, if applicable.
                        size = input.try_parse(|input| {
                            input.expect_delim('/')?;
                            mask_size::single_value::parse(context, input)
                        }).ok();

                        continue
                    }
                }
                % for name in "repeat origin clip composite mode".split():
                    if ${name}.is_none() {
                        if let Ok(value) = input.try_parse(|input| mask_${name}::single_value
                                                                               ::parse(context, input)) {
                            ${name} = Some(value);
                            continue
                        }
                    }
                % endfor
                break
            }
            if clip.is_none() {
                if let Some(origin) = origin {
                    clip = Some(mask_clip::single_value::SpecifiedValue::from(origin));
                }
            }
            let mut any = false;
            % for name in "image mode position size repeat origin clip composite".split():
                any = any || ${name}.is_some();
            % endfor
            if any {
                if let Some(position) = position {
                    mask_position_x.push(position.horizontal);
                    mask_position_y.push(position.vertical);
                } else {
                    mask_position_x.push(PositionComponent::zero());
                    mask_position_y.push(PositionComponent::zero());
                }
                % for name in "image mode size repeat origin clip composite".split():
                    if let Some(m_${name}) = ${name} {
                        mask_${name}.push(m_${name});
                    } else {
                        mask_${name}.push(mask_${name}::single_value
                                                        ::get_initial_specified_value());
                    }
                % endfor
                Ok(())
            } else {
                Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
            }
        })?;

        Ok(expanded! {
            % for name in "image mode position_x position_y size repeat origin clip composite".split():
                mask_${name}: mask_${name}::SpecifiedValue(mask_${name}.into()),
            % endfor
         })
    }

    impl<'a> ToCss for LonghandsToSerialize<'a>  {
        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
            use crate::properties::longhands::mask_origin::single_value::computed_value::T as Origin;
            use crate::properties::longhands::mask_clip::single_value::computed_value::T as Clip;
            use style_traits::values::SequenceWriter;

            let len = self.mask_image.0.len();
            if len == 0 {
                return Ok(());
            }
            % for name in "mode position_x position_y size repeat origin clip composite".split():
                if self.mask_${name}.0.len() != len {
                    return Ok(());
                }
            % endfor

            // For each <mask-layer>, we serialize it according to the following order:
            // <mask-layer> =
            //   <mask-reference> ||
            //   <position> [ / <bg-size> ]? ||
            //   <repeat-style> ||
            //   <geometry-box> ||
            //   [ <geometry-box> | no-clip ] ||
            //   <compositing-operator> ||
            //   <masking-mode>
            // https://drafts.fxtf.org/css-masking-1/#the-mask
            for i in 0..len {
                if i > 0 {
                    dest.write_str(", ")?;
                }

                % for name in "image mode position_x position_y size repeat origin clip composite".split():
                    let ${name} = &self.mask_${name}.0[i];
                % endfor

                let mut has_other = false;
                % for name in "image mode size repeat composite".split():
                    let has_${name} =
                        *${name} != mask_${name}::single_value::get_initial_specified_value();
                    has_other |= has_${name};
                % endfor
                let has_position = *position_x != PositionComponent::zero()
                    || *position_y != PositionComponent::zero();
                let has_origin = *origin != Origin::BorderBox;
                let has_clip = *clip != Clip::BorderBox;

                // If all are initial values, we serialize mask-image.
                if !has_other && !has_position && !has_origin && !has_clip {
                    return image.to_css(dest);
                }

                let mut writer = SequenceWriter::new(dest, " ");
                // <mask-reference>
                if has_image {
                    writer.item(image)?;
                }

                // <position> [ / <bg-size> ]?
                if has_position || has_size {
                    writer.item(&Position {
                        horizontal: position_x.clone(),
                        vertical: position_y.clone()
                    })?;

                    if has_size {
                        writer.raw_item("/")?;
                        writer.item(size)?;
                    }
                }

                // <repeat-style>
                if has_repeat {
                    writer.item(repeat)?;
                }

                // <geometry-box>
                if has_origin {
                    writer.item(origin)?;
                }

                // [ <geometry-box> | no-clip ]
                if has_clip && *clip != From::from(*origin) {
                    writer.item(clip)?;
                }

                // <compositing-operator>
                if has_composite {
                    writer.item(composite)?;
                }

                // <masking-mode>
                if has_mode {
                    writer.item(mode)?;
                }
            }

            Ok(())
        }
    }
</%helpers:shorthand>

<%helpers:shorthand name="mask-position" engines="gecko" extra_prefixes="webkit"
                    flags="SHORTHAND_IN_GETCS"
                    sub_properties="mask-position-x mask-position-y"
                    spec="https://drafts.csswg.org/css-masks-4/#the-mask-position">
    use crate::properties::longhands::{mask_position_x,mask_position_y};
    use crate::values::specified::position::Position;
    use crate::parser::Parse;

    pub fn parse_value<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Longhands, ParseError<'i>> {
        // Vec grows from 0 to 4 by default on first push().  So allocate with
        // capacity 1, so in the common case of only one item we don't way
        // overallocate, then shrink.  Note that we always push at least one
        // item if parsing succeeds.
        let mut position_x = Vec::with_capacity(1);
        let mut position_y = Vec::with_capacity(1);
        let mut any = false;

        input.parse_comma_separated(|input| {
            let value = Position::parse(context, input)?;
            position_x.push(value.horizontal);
            position_y.push(value.vertical);
            any = true;
            Ok(())
        })?;

        if !any {
            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }


        Ok(expanded! {
            mask_position_x: mask_position_x::SpecifiedValue(position_x.into()),
            mask_position_y: mask_position_y::SpecifiedValue(position_y.into()),
        })
    }

    impl<'a> ToCss for LonghandsToSerialize<'a>  {
        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
            let len = self.mask_position_x.0.len();
            if len == 0 || self.mask_position_y.0.len() != len {
                return Ok(());
            }

            for i in 0..len {
                Position {
                    horizontal: self.mask_position_x.0[i].clone(),
                    vertical: self.mask_position_y.0[i].clone()
                }.to_css(dest)?;

                if i < len - 1 {
                    dest.write_str(", ")?;
                }
            }

            Ok(())
        }
    }
</%helpers:shorthand>