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
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
use std::fmt;

use cssparser::{Parser, match_ignore_ascii_case};
use parley::LineMetrics;

use crate::{
  layout::style::{ToCss, tw::TailwindPropertyParser, *},
  rendering::Sizing,
};

/// Keyword values for the CSS `vertical-align` property.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[non_exhaustive]
pub enum VerticalAlignKeyword {
  /// Aligns the baseline of the box with the baseline of the parent box.
  #[default]
  Baseline,
  /// Aligns the top of the box with the top of the line box.
  Top,
  /// Aligns the middle of the box with the baseline of the parent box plus half the x-height of the parent.
  Middle,
  /// Aligns the bottom of the box with the bottom of the line box.
  Bottom,
  /// Aligns the top of the box with the top of the parent's font.
  TextTop,
  /// Aligns the bottom of the box with the bottom of the parent's font.
  TextBottom,
  /// Aligns the baseline of the box with the subscript-baseline of the parent box.
  Sub,
  /// Aligns the baseline of the box with the superscript-baseline of the parent box.
  Super,
}

declare_enum_from_css_impl!(
  VerticalAlignKeyword,
  "baseline" => VerticalAlignKeyword::Baseline,
  "top" => VerticalAlignKeyword::Top,
  "middle" => VerticalAlignKeyword::Middle,
  "bottom" => VerticalAlignKeyword::Bottom,
  "text-top" => VerticalAlignKeyword::TextTop,
  "text-bottom" => VerticalAlignKeyword::TextBottom,
  "sub" => VerticalAlignKeyword::Sub,
  "super" => VerticalAlignKeyword::Super
);

/// Defines the vertical alignment of an inline-level box.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum VerticalAlign {
  /// A keyword-based alignment mode.
  Keyword(VerticalAlignKeyword),
  /// A baseline shift in `<length-percentage>` form.
  Length(Length),
}

impl ToCss for VerticalAlign {
  fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
    match self {
      Self::Keyword(kw) => kw.to_css(dest),
      Self::Length(l) => l.to_css(dest),
    }
  }
}

/// Computed `vertical-align` data used for placement.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum ResolvedVerticalAlign {
  /// A keyword-based alignment mode.
  Keyword(VerticalAlignKeyword),
  /// A baseline shift resolved as: `px + line_height_relative * metrics_line_height`.
  BaselineShift {
    /// The absolute pixel component added to the final baseline shift.
    px: f32,
    /// The multiplier applied to metrics-derived line height.
    line_height_relative: f32,
  },
}

impl Default for VerticalAlign {
  fn default() -> Self {
    Self::Keyword(VerticalAlignKeyword::default())
  }
}

impl Default for ResolvedVerticalAlign {
  fn default() -> Self {
    Self::Keyword(VerticalAlignKeyword::default())
  }
}

impl<'i> FromCss<'i> for VerticalAlign {
  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
    if let Ok(keyword) = input.try_parse(VerticalAlignKeyword::from_css) {
      return Ok(Self::Keyword(keyword));
    }

    Ok(Self::Length(Length::from_css(input)?))
  }

  const VALID_TOKENS: &'static [CssToken] = &[
    CssToken::Keyword("baseline"),
    CssToken::Keyword("top"),
    CssToken::Keyword("middle"),
    CssToken::Keyword("bottom"),
    CssToken::Keyword("text-top"),
    CssToken::Keyword("text-bottom"),
    CssToken::Keyword("sub"),
    CssToken::Keyword("super"),
    CssToken::Syntax(CssSyntaxKind::Length),
  ];
}

impl VerticalAlign {
  pub(crate) fn resolve(
    self,
    sizing: &Sizing,
    font_size: f32,
    line_height: LineHeight,
  ) -> ResolvedVerticalAlign {
    match self {
      Self::Keyword(keyword) => ResolvedVerticalAlign::Keyword(keyword),
      Self::Length(length) => {
        if line_height == LineHeight::Normal {
          if let Length::Percentage(value) = length {
            return ResolvedVerticalAlign::BaselineShift {
              px: 0.0,
              line_height_relative: value / 100.0,
            };
          }

          if let Length::Calc(formula) = length {
            let linear = formula.resolve(sizing);
            let (px, percent) = linear.components();

            return ResolvedVerticalAlign::BaselineShift {
              px,
              line_height_relative: percent,
            };
          }
        }

        let shift = match line_height {
          LineHeight::Normal => length.to_px(sizing, 0.0),
          LineHeight::Unitless(value) => length.to_px(sizing, value * font_size),
          LineHeight::Length(value) => length.to_px(sizing, value.to_px(sizing, font_size)),
        };
        ResolvedVerticalAlign::BaselineShift {
          px: shift,
          line_height_relative: 0.0,
        }
      }
    }
  }
}

impl MakeComputed for VerticalAlign {
  fn make_computed(&mut self, sizing: &Sizing) {
    if let Self::Length(length) = self {
      length.make_computed(sizing);
    }
  }
}

impl ResolvedVerticalAlign {
  pub(crate) fn apply(
    self,
    y: &mut f32,
    metrics: &LineMetrics,
    box_height: f32,
    baseline_offset: Option<f32>,
    parent_x_height: Option<f32>,
    parent_text_metrics: Option<(f32, f32)>,
  ) {
    let baseline_offset = baseline_offset.unwrap_or(box_height).clamp(0.0, box_height);
    let baseline_top = metrics.baseline - baseline_offset;
    let (parent_text_ascent, parent_text_descent) =
      parent_text_metrics.unwrap_or((metrics.ascent, metrics.descent));

    match self {
      ResolvedVerticalAlign::Keyword(keyword) => match keyword {
        VerticalAlignKeyword::Baseline => *y = baseline_top,
        VerticalAlignKeyword::Top => {
          debug_assert!(metrics.block_min_coord.is_finite());
          *y = metrics.block_min_coord;
        }
        VerticalAlignKeyword::Middle => {
          let x_height = parent_x_height.unwrap_or(metrics.ascent * 0.5);
          *y = metrics.baseline - (x_height * 0.5) - (box_height / 2.0);
        }
        VerticalAlignKeyword::Bottom => {
          debug_assert!(metrics.block_max_coord.is_finite());
          *y = metrics.block_max_coord - box_height;
        }
        VerticalAlignKeyword::TextTop => *y = metrics.baseline - parent_text_ascent,
        VerticalAlignKeyword::TextBottom => {
          *y = metrics.baseline + parent_text_descent - box_height
        }
        VerticalAlignKeyword::Sub => *y = baseline_top + (metrics.descent * 0.2),
        VerticalAlignKeyword::Super => *y = baseline_top - metrics.ascent + (metrics.ascent * 0.4),
      },
      ResolvedVerticalAlign::BaselineShift {
        px,
        line_height_relative,
      } => {
        let line_height_component =
          (metrics.ascent - metrics.descent + metrics.leading) * line_height_relative;
        *y = baseline_top - (px + line_height_component);
      }
    }
  }
}

impl TailwindPropertyParser for VerticalAlign {
  fn parse_tw(token: &str) -> Option<Self> {
    match_ignore_ascii_case! {token,
      "baseline" => Some(Self::Keyword(VerticalAlignKeyword::Baseline)),
      "top" => Some(Self::Keyword(VerticalAlignKeyword::Top)),
      "middle" => Some(Self::Keyword(VerticalAlignKeyword::Middle)),
      "bottom" => Some(Self::Keyword(VerticalAlignKeyword::Bottom)),
      "text-top" => Some(Self::Keyword(VerticalAlignKeyword::TextTop)),
      "text-bottom" => Some(Self::Keyword(VerticalAlignKeyword::TextBottom)),
      "sub" => Some(Self::Keyword(VerticalAlignKeyword::Sub)),
      "super" => Some(Self::Keyword(VerticalAlignKeyword::Super)),
      _ => None,
    }
  }
}

#[cfg(test)]
mod tests {
  use std::rc::Rc;

  use taffy::Size;

  use crate::layout::Viewport;

  use super::*;

  fn sizing() -> Sizing {
    Sizing {
      viewport: Viewport {
        size: (200, 100).into(),
        font_size: 16.0,
        device_pixel_ratio: 2.0,
      },
      container_size: Size::NONE,
      font_size: 10.0,
      root_font_size: None,
      line_height: 0.0,
      root_line_height: None,
      calc_arena: Rc::new(CalcArena::default()),
    }
  }

  fn line_metrics() -> LineMetrics {
    LineMetrics {
      ascent: 9.0,
      descent: 3.0,
      leading: 0.0,
      line_height: 14.0,
      baseline: 20.0,
      offset: 0.0,
      advance: 100.0,
      trailing_whitespace: 0.0,
      inline_min_coord: 0.0,
      inline_max_coord: 100.0,
      block_min_coord: 10.0,
      block_max_coord: 24.0,
    }
  }

  #[test]
  fn parse_keywords_and_length_percentage() {
    assert_eq!(
      VerticalAlign::from_str("baseline"),
      Ok(VerticalAlign::Keyword(VerticalAlignKeyword::Baseline))
    );
    assert_eq!(
      VerticalAlign::from_str("10px"),
      Ok(VerticalAlign::Length(Length::Px(10.0)))
    );
    assert_eq!(
      VerticalAlign::from_str("25%"),
      Ok(VerticalAlign::Length(Length::Percentage(25.0)))
    );
    assert_eq!(
      VerticalAlign::from_str("-0.5em"),
      Ok(VerticalAlign::Length(Length::Em(-0.5)))
    );
  }

  #[test]
  fn resolve_length_to_baseline_shift_px() {
    let resolved =
      VerticalAlign::Length(Length::Px(8.0)).resolve(&sizing(), 12.0, LineHeight::Unitless(1.5));
    assert_eq!(
      resolved,
      ResolvedVerticalAlign::BaselineShift {
        px: 16.0,
        line_height_relative: 0.0
      }
    );
  }

  #[test]
  fn resolve_percentage_uses_line_height_basis() {
    let unitless = VerticalAlign::Length(Length::Percentage(50.0)).resolve(
      &sizing(),
      12.0,
      LineHeight::Unitless(2.0),
    );
    assert_eq!(
      unitless,
      ResolvedVerticalAlign::BaselineShift {
        px: 12.0,
        line_height_relative: 0.0
      }
    );

    let fixed = VerticalAlign::Length(Length::Percentage(50.0)).resolve(
      &sizing(),
      12.0,
      LineHeight::Length(Length::Px(20.0)),
    );
    assert_eq!(
      fixed,
      ResolvedVerticalAlign::BaselineShift {
        px: 20.0,
        line_height_relative: 0.0
      }
    );

    let normal =
      VerticalAlign::Length(Length::Percentage(50.0)).resolve(&sizing(), 12.0, LineHeight::Normal);
    assert_eq!(
      normal,
      ResolvedVerticalAlign::BaselineShift {
        px: 0.0,
        line_height_relative: 0.5
      }
    );
  }

  #[test]
  fn apply_baseline_shift_raises_and_lowers() {
    let metrics = line_metrics();
    let baseline = metrics.baseline - 4.0;

    let mut y = 0.0;
    ResolvedVerticalAlign::BaselineShift {
      px: 5.0,
      line_height_relative: 0.0,
    }
    .apply(&mut y, &metrics, 4.0, None, None, None);
    assert_eq!(y, baseline - 5.0);

    ResolvedVerticalAlign::BaselineShift {
      px: -5.0,
      line_height_relative: 0.0,
    }
    .apply(&mut y, &metrics, 4.0, None, None, None);
    assert_eq!(y, baseline + 5.0);
  }

  #[test]
  fn apply_keyword_top_uses_block_min_coord() {
    let mut y = 0.0;
    let metrics = line_metrics();

    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Top)
      .apply(&mut y, &metrics, 8.0, None, None, None);

    assert_eq!(y, metrics.block_min_coord);
  }

  #[test]
  fn apply_keyword_bottom_uses_block_max_coord() {
    let mut y = 0.0;
    let metrics = line_metrics();
    let box_height = 8.0;

    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Bottom)
      .apply(&mut y, &metrics, box_height, None, None, None);

    assert_eq!(y, metrics.block_max_coord - box_height);
  }

  #[test]
  fn apply_metrics_relative_shift_uses_line_metrics_formula() {
    let metrics = line_metrics();
    let baseline = metrics.baseline - 4.0;
    let mut y = 0.0;

    ResolvedVerticalAlign::BaselineShift {
      px: 0.0,
      line_height_relative: 0.5,
    }
    .apply(&mut y, &metrics, 4.0, None, None, None);
    assert_eq!(
      y,
      baseline - ((metrics.ascent - metrics.descent + metrics.leading) * 0.5)
    );
  }

  #[test]
  fn resolve_normal_calc_percentage_uses_metrics_relative_px() {
    let Ok(length) = Length::from_str("calc(50% + 4px)") else {
      return;
    };
    let resolved = VerticalAlign::Length(length).resolve(&sizing(), 12.0, LineHeight::Normal);
    assert_eq!(
      resolved,
      ResolvedVerticalAlign::BaselineShift {
        px: 8.0,
        line_height_relative: 0.5
      }
    );
  }

  #[test]
  fn apply_metrics_relative_px_shift_uses_line_metrics_formula_plus_px() {
    let metrics = line_metrics();
    let baseline = metrics.baseline - 4.0;
    let mut y = 0.0;

    ResolvedVerticalAlign::BaselineShift {
      px: 3.0,
      line_height_relative: 0.5,
    }
    .apply(&mut y, &metrics, 4.0, None, None, None);
    assert_eq!(
      y,
      baseline - ((metrics.ascent - metrics.descent + metrics.leading) * 0.5 + 3.0)
    );
  }

  #[test]
  fn keyword_apply_matches_previous_baseline_behavior() {
    let metrics = line_metrics();
    let mut y = 0.0;
    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Baseline)
      .apply(&mut y, &metrics, 4.0, None, None, None);
    assert_eq!(y, metrics.baseline - 4.0);
  }

  #[test]
  fn keyword_apply_uses_inline_box_baseline_when_available() {
    let metrics = line_metrics();
    let mut y = 0.0;
    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Baseline).apply(
      &mut y,
      &metrics,
      20.0,
      Some(12.0),
      None,
      None,
    );
    assert_eq!(y, metrics.baseline - 12.0);
  }

  #[test]
  fn sub_and_super_use_inline_box_baseline_when_available() {
    let metrics = line_metrics();
    let mut sub_y = 0.0;
    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Sub).apply(
      &mut sub_y,
      &metrics,
      20.0,
      Some(12.0),
      None,
      None,
    );
    assert_eq!(sub_y, (metrics.baseline - 12.0) + (metrics.descent * 0.2));

    let mut super_y = 0.0;
    ResolvedVerticalAlign::Keyword(VerticalAlignKeyword::Super).apply(
      &mut super_y,
      &metrics,
      20.0,
      Some(12.0),
      None,
      None,
    );
    assert_eq!(
      super_y,
      (metrics.baseline - 12.0) - metrics.ascent + (metrics.ascent * 0.4)
    );
  }
}