takumi 1.7.0

Render UI component trees to images.
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
use std::{borrow::Cow, fmt, fmt::Debug};

use cssparser::{BasicParseErrorKind, ParseError, Parser};
use typed_builder::TypedBuilder;

use crate::{
  layout::style::{
    Animatable, Color, ColorInput, CssSyntaxKind, CssToken, FromCss, Length, LengthDefaultsToZero,
    ListInterpolationStrategy, MakeComputed, ParseResult, ToCss, next_is_comma,
  },
  rendering::Sizing,
};

/// Represents a box shadow with all its properties.
/// Construct with [`BoxShadow::builder`].
#[derive(Debug, Clone, PartialEq, Copy, Default, TypedBuilder)]
#[non_exhaustive]
#[builder(field_defaults(default))]
pub struct BoxShadow {
  /// Whether the shadow is inset (inside the element) or outset (outside the element).
  #[builder(default = false)]
  pub inset: bool,
  /// Horizontal offset of the shadow.
  pub offset_x: LengthDefaultsToZero,
  /// Vertical offset of the shadow.
  pub offset_y: LengthDefaultsToZero,
  /// Blur radius of the shadow. Higher values create a more blurred shadow.
  pub blur_radius: LengthDefaultsToZero,
  /// Spread radius of the shadow. Positive values expand the shadow, negative values shrink it.
  pub spread_radius: LengthDefaultsToZero,
  /// Color of the shadow.
  pub color: ColorInput,
}

/// Represents a collection of box shadows, have custom `FromCss` implementation for comma-separated values.
pub type BoxShadows = Box<[BoxShadow]>;

impl<'i> FromCss<'i> for BoxShadows {
  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
    Ok(
      input
        .parse_comma_separated(BoxShadow::from_css)?
        .into_boxed_slice(),
    )
  }

  const VALID_TOKENS: &'static [CssToken] = BoxShadow::VALID_TOKENS;
}

impl<'i> FromCss<'i> for BoxShadow {
  /// Parses a box-shadow value from CSS input.
  ///
  /// The box-shadow syntax allows for the following components in any order:
  /// - inset keyword (optional)
  /// - Two length values for horizontal and vertical offsets (required)
  /// - Two optional length values for blur radius and spread radius
  /// - A color value (optional)
  ///
  /// Examples:
  /// - `box-shadow: 2px 4px;`
  /// - `box-shadow: 2px 4px 6px;`
  /// - `box-shadow: 2px 4px 6px 8px;`
  /// - `box-shadow: 2px 4px red;`
  /// - `box-shadow: inset 2px 4px 6px red;`
  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, BoxShadow> {
    let mut color = None;
    let mut lengths = None;
    let mut inset = false;

    while !input.is_exhausted() && !next_is_comma(input) {
      if !inset
        && input
          .try_parse(|input| input.expect_ident_matching("inset"))
          .is_ok()
      {
        inset = true;
        continue;
      }

      if lengths.is_none() {
        let value = input.try_parse::<_, _, ParseError<Cow<'i, str>>>(|input| {
          let horizontal = Length::from_css(input)?;
          let vertical = Length::from_css(input)?;

          let blur = input.try_parse(Length::from_css).unwrap_or(Length::zero());

          let spread = input.try_parse(Length::from_css).unwrap_or(Length::zero());

          Ok((horizontal, vertical, blur, spread))
        });

        if let Ok(value) = value {
          lengths = Some(value);
          continue;
        }
      }

      if color.is_none()
        && let Ok(value) = input.try_parse(ColorInput::from_css)
      {
        color = Some(value);
        continue;
      }

      break;
    }

    let lengths = lengths.ok_or(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))?;

    Ok(BoxShadow {
      color: color.unwrap_or(ColorInput::Value(Color::transparent())),
      offset_x: lengths.0,
      offset_y: lengths.1,
      blur_radius: lengths.2,
      spread_radius: lengths.3,
      inset,
    })
  }

  const VALID_TOKENS: &'static [CssToken] = &[
    CssToken::Keyword("inset"),
    CssToken::Syntax(CssSyntaxKind::Length),
    CssToken::Syntax(CssSyntaxKind::Color),
  ];
}

impl crate::layout::style::tw::TailwindPropertyParser for BoxShadow {
  fn parse_tw(token: &str) -> Option<Self> {
    Self::from_str(token).ok()
  }
}

impl MakeComputed for BoxShadow {
  fn make_computed(&mut self, sizing: &Sizing) {
    self.offset_x.make_computed(sizing);
    self.offset_y.make_computed(sizing);
    self.blur_radius.make_computed(sizing);
    self.spread_radius.make_computed(sizing);
  }
}

impl Animatable for BoxShadow {
  fn list_interpolation_strategy() -> ListInterpolationStrategy {
    ListInterpolationStrategy::PadToLongestWithNeutral
  }

  fn neutral_value_like(other: &Self) -> Option<Self> {
    Some(Self {
      inset: other.inset,
      offset_x: Length::zero(),
      offset_y: Length::zero(),
      blur_radius: Length::zero(),
      spread_radius: Length::zero(),
      color: Color::transparent().into(),
    })
  }

  fn interpolate(
    &mut self,
    from: &Self,
    to: &Self,
    progress: f32,
    sizing: &Sizing,
    current_color: Color,
  ) {
    if from.inset != to.inset {
      *self = if progress >= 0.5 { *to } else { *from };
      return;
    }

    self.inset = from.inset;
    self.offset_x.interpolate(
      &from.offset_x,
      &to.offset_x,
      progress,
      sizing,
      current_color,
    );
    self.offset_y.interpolate(
      &from.offset_y,
      &to.offset_y,
      progress,
      sizing,
      current_color,
    );
    self.blur_radius.interpolate(
      &from.blur_radius,
      &to.blur_radius,
      progress,
      sizing,
      current_color,
    );
    self.spread_radius.interpolate(
      &from.spread_radius,
      &to.spread_radius,
      progress,
      sizing,
      current_color,
    );
    self
      .color
      .interpolate(&from.color, &to.color, progress, sizing, current_color);
  }
}

impl ToCss for BoxShadow {
  fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
    if self.inset {
      dest.write_str("inset ")?;
    }
    self.offset_x.to_css(dest)?;
    dest.write_char(' ')?;
    self.offset_y.to_css(dest)?;

    let blur_zero = self.blur_radius == Length::zero();
    let spread_zero = self.spread_radius == Length::zero();
    if !spread_zero {
      dest.write_char(' ')?;
      self.blur_radius.to_css(dest)?;
      dest.write_char(' ')?;
      self.spread_radius.to_css(dest)?;
    } else if !blur_zero {
      dest.write_char(' ')?;
      self.blur_radius.to_css(dest)?;
    }

    dest.write_char(' ')?;
    self.color.to_css(dest)
  }
}
#[cfg(test)]
mod tests {
  use super::*;
  use crate::layout::style::{
    Color,
    Length::{self, Px},
  };

  #[test]
  fn test_parse_simple_box_shadow() {
    // Test parsing a simple box-shadow with just offsets
    assert_eq!(
      BoxShadow::from_str("2px 4px"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color::transparent()),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_with_blur() {
    // Test parsing box-shadow with blur radius
    assert_eq!(
      BoxShadow::from_str("2px 4px 6px"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Px(6.0),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color::transparent()),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_with_spread() {
    // Test parsing box-shadow with blur and spread radius
    assert_eq!(
      BoxShadow::from_str("2px 4px 6px 8px"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Px(6.0),
        spread_radius: Px(8.0),
        color: ColorInput::Value(Color::transparent()),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_with_color() {
    // Test parsing box-shadow with color
    assert_eq!(
      BoxShadow::from_str("2px 4px red"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color([255, 0, 0, 255])),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_inset_box_shadow() {
    // Test parsing inset box-shadow
    assert_eq!(
      BoxShadow::from_str("inset 2px 4px"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color::transparent()),
        inset: true,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_color_first() {
    // Test parsing box-shadow with color before offsets
    assert_eq!(
      BoxShadow::from_str("red 2px 4px"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color([255, 0, 0, 255])),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_inset_after_offsets() {
    // Test parsing box-shadow with inset keyword after offsets
    assert_eq!(
      BoxShadow::from_str("2px 4px inset red"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color([255, 0, 0, 255])),
        inset: true,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_hex_color() {
    // Test parsing box-shadow with hex color
    assert_eq!(
      BoxShadow::from_str("2px 4px #ff0000"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color([255, 0, 0, 255])),
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_rgba_color() {
    // Test parsing box-shadow with rgba color
    assert_eq!(
      BoxShadow::from_str("2px 4px rgba(255, 0, 0, 0.5)"),
      Ok(BoxShadow {
        offset_x: Px(2.0),
        offset_y: Px(4.0),
        blur_radius: Length::zero(),
        spread_radius: Length::zero(),
        color: ColorInput::Value(Color([255, 0, 0, 128])), // 0.5 * 255 = 128
        inset: false,
      })
    );
  }

  #[test]
  fn test_parse_box_shadow_invalid() {
    // Test parsing invalid box-shadow (missing required offsets)
    assert!(BoxShadow::from_str("2px").is_err());

    // Test parsing invalid box-shadow (no values)
    assert!(BoxShadow::from_str("").is_err());
  }

  #[test]
  fn test_parse_multiple_box_shadows_with_rgba() {
    assert_eq!(
      BoxShadows::from_str("2px 4px rgba(0, 0, 0, 0.5), 1px 2px 3px rgba(255, 0, 0, 0.25)"),
      Ok(
        [
          BoxShadow {
            offset_x: Px(2.0),
            offset_y: Px(4.0),
            blur_radius: Length::zero(),
            spread_radius: Length::zero(),
            color: ColorInput::Value(Color([0, 0, 0, 128])),
            inset: false,
          },
          BoxShadow {
            offset_x: Px(1.0),
            offset_y: Px(2.0),
            blur_radius: Px(3.0),
            spread_radius: Length::zero(),
            color: ColorInput::Value(Color([255, 0, 0, 64])),
            inset: false,
          }
        ]
        .into()
      )
    );
  }
}