takumi 0.73.0

Render your React components 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
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
mod container;
mod image;
mod text;

use ::image::RgbaImage;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::sync::Arc;
use taffy::{AvailableSpace, Layout, Point, Size};
use zeno::Fill;

use crate::{
  Result,
  layout::{
    Viewport,
    inline::InlineContentKind,
    style::{Affine, BackgroundClip, BlendMode, Sides, Style, tw::TailwindValues},
  },
  rendering::{
    BackgroundTile, BorderProperties, Canvas, RenderContext, SizedShadow,
    collect_background_layers, rasterize_layers,
  },
  resources::task::FetchTaskCollection,
};

use self::{
  container::{
    container_children_ref, deserialize_children, drop_container_children, take_container_children,
    take_container_style_layers,
  },
  image::{
    draw_image_node_content, image_collect_fetch_tasks, image_inline_content, measure_image_node,
    take_image_style_layers,
  },
  text::{draw_text_node_content, measure_text_node, take_text_style_layers, text_inline_content},
};

pub(crate) use self::image::resolve_image;

/// Shared metadata stored by every renderable node.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeMetadata {
  /// The element's tag name.
  pub(crate) tag_name: Option<Box<str>>,
  /// The element's class name.
  pub(crate) class_name: Option<Box<str>>,
  /// The element's id.
  pub(crate) id: Option<Box<str>>,
  /// Additional element attributes for selector matching and serialization.
  pub(crate) attributes: Option<BTreeMap<Box<str>, Box<str>>>,
  /// Default style presets from HTML element type (lowest priority).
  pub(crate) preset: Option<Style>,
  /// The styling properties for this node.
  pub(crate) style: Option<Style>,
  /// The tailwind properties for this node.
  pub(crate) tw: Option<TailwindValues>,
}

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Variant-specific text node data.
pub struct TextData {
  pub(crate) text: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Variant-specific image node data.
pub struct ImageData {
  pub(crate) src: Arc<str>,
  pub(crate) width: Option<f32>,
  pub(crate) height: Option<f32>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
/// A renderable node with shared metadata and variant-specific content.
pub struct Node {
  #[serde(flatten)]
  pub(crate) metadata: NodeMetadata,
  #[serde(flatten)]
  pub(crate) kind: NodeKind,
}

/// Represents the nodes enum.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum NodeKind {
  /// A node that contains other nodes.
  Container {
    /// The container child nodes.
    #[serde(default, deserialize_with = "deserialize_children")]
    children: Vec<Node>,
  },
  /// A node that displays an image.
  Image(ImageData),
  /// A node that displays text.
  Text(TextData),
}

impl Default for Node {
  fn default() -> Self {
    Self::container([])
  }
}

impl Drop for Node {
  fn drop(&mut self) {
    drop_container_children(&mut self.kind);
  }
}

impl Node {
  /// Creates a container node with the provided child nodes.
  pub fn container(children: impl Into<Vec<Node>>) -> Self {
    Self {
      metadata: NodeMetadata::default(),
      kind: NodeKind::Container {
        children: children.into(),
      },
    }
  }

  /// Creates an image node from any supported image input shape.
  pub fn image(data: impl Into<ImageData>) -> Self {
    Self {
      metadata: NodeMetadata::default(),
      kind: NodeKind::Image(data.into()),
    }
  }

  /// Creates a text node from the provided text.
  pub fn text(text: impl Into<String>) -> Self {
    Self {
      metadata: NodeMetadata::default(),
      kind: NodeKind::Text(TextData { text: text.into() }),
    }
  }

  pub(crate) fn children_ref(&self) -> Option<&[Node]> {
    container_children_ref(&self.kind)
  }

  pub(crate) fn take_children(&mut self) -> Option<Box<[Node]>> {
    take_container_children(&mut self.kind)
  }

  /// Sets the tag name and returns the updated node.
  pub fn with_tag_name(mut self, tag_name: impl Into<Box<str>>) -> Self {
    self.metadata.tag_name = Some(tag_name.into());
    self
  }

  /// Sets the class name and returns the updated node.
  pub fn with_class_name(mut self, class_name: impl Into<Box<str>>) -> Self {
    self.metadata.class_name = Some(class_name.into());
    self
  }

  /// Sets the element id and returns the updated node.
  pub fn with_id(mut self, id: impl Into<Box<str>>) -> Self {
    self.metadata.id = Some(id.into());
    self
  }

  /// Sets the element attributes and returns the updated node.
  pub fn with_attributes(mut self, attributes: BTreeMap<Box<str>, Box<str>>) -> Self {
    self.metadata.attributes = Some(attributes);
    self
  }

  /// Sets the preset style and returns the updated node.
  pub fn with_preset(mut self, preset: Style) -> Self {
    self.metadata.preset = Some(preset);
    self
  }

  /// Sets the inline style and returns the updated node.
  pub fn with_style(mut self, style: Style) -> Self {
    self.metadata.style = Some(style);
    self
  }

  /// Sets the Tailwind-derived style input and returns the updated node.
  pub fn with_tw(mut self, tw: TailwindValues) -> Self {
    self.metadata.tw = Some(tw);
    self
  }

  pub(crate) fn take_style_layers(&mut self) -> NodeStyleLayers {
    if matches!(self.kind, NodeKind::Container { .. }) {
      return take_container_style_layers(self);
    }

    if let NodeKind::Image(image) = &self.kind {
      return take_image_style_layers(self, image.width, image.height);
    }

    take_text_style_layers(self)
  }

  pub(crate) fn inline_content(&self) -> Option<InlineContentKind<'_>> {
    match &self.kind {
      NodeKind::Container { .. } => None,
      NodeKind::Image(_) => image_inline_content(&self.kind),
      NodeKind::Text(text) => text_inline_content(text),
    }
  }

  pub(crate) fn measure(
    &self,
    context: &RenderContext,
    available_space: Size<AvailableSpace>,
    known_dimensions: Size<Option<f32>>,
    style: &taffy::Style,
  ) -> Size<f32> {
    match &self.kind {
      NodeKind::Container { .. } => Size::ZERO,
      NodeKind::Image(image) => {
        measure_image_node(image, context, available_space, known_dimensions, style)
      }
      NodeKind::Text(text) => measure_text_node(text, context, available_space, known_dimensions),
    }
  }

  /// Collects resource URLs referenced by this node tree.
  pub fn collect_fetch_tasks(&self, collection: &mut FetchTaskCollection) {
    match &self.kind {
      NodeKind::Container { .. } => {
        let Some(children) = self.children_ref() else {
          return;
        };

        for child in children {
          child.collect_fetch_tasks(collection);
        }
      }
      NodeKind::Image(image) => image_collect_fetch_tasks(image, collection),
      NodeKind::Text(_) => {}
    }
  }

  /// Collects resource URLs referenced by this node tree's styles.
  pub fn collect_style_fetch_tasks(&self, collection: &mut FetchTaskCollection) {
    if let Some(preset) = self.metadata.preset.as_ref() {
      preset.collect_fetch_tasks(collection);
    }

    if let Some(author_tw) = self.metadata.tw.as_ref() {
      author_tw.collect_fetch_tasks(Viewport::new(None, None), collection);
    }

    if let Some(inline) = self.metadata.style.as_ref() {
      inline.collect_fetch_tasks(collection);
    }

    let Some(children) = self.children_ref() else {
      return;
    };

    for child in children {
      child.collect_style_fetch_tasks(collection);
    }
  }

  pub(crate) fn is_replaced_element(&self) -> bool {
    matches!(self.kind, NodeKind::Image(_))
  }

  pub(crate) fn draw_outset_box_shadow(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    let Some(box_shadow) = context.style.box_shadow.as_ref() else {
      return Ok(());
    };

    let element_border_radius = BorderProperties::from_context(context, layout.size, layout.border);

    for shadow in box_shadow.iter() {
      if shadow.inset {
        continue;
      }

      let mut paths = Vec::new();
      let mut element_paths = Vec::new();

      let mut border_radius = element_border_radius;
      let resolved_spread_radius = shadow
        .spread_radius
        .to_px(&context.sizing, layout.size.width);

      border_radius.expand_by(Sides([resolved_spread_radius; 4]).into());

      let shadow =
        SizedShadow::from_box_shadow(*shadow, &context.sizing, context.current_color, layout.size);

      let spread_size = Size {
        width: (layout.size.width + 2.0 * resolved_spread_radius).max(0.0),
        height: (layout.size.height + 2.0 * resolved_spread_radius).max(0.0),
      };

      border_radius.append_mask_commands(
        &mut paths,
        spread_size,
        Point {
          x: -resolved_spread_radius,
          y: -resolved_spread_radius,
        },
      );

      element_border_radius.append_mask_commands(&mut element_paths, layout.size, Point::ZERO);

      shadow.draw_outset(
        canvas,
        &paths,
        context.transform,
        Fill::NonZero.into(),
        Some(&element_paths),
      )?;
    }

    Ok(())
  }

  pub(crate) fn draw_inset_box_shadow(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    if let Some(box_shadow) = context.style.box_shadow.as_ref() {
      let border_radius = BorderProperties::from_context(context, layout.size, layout.border);

      for shadow in box_shadow.iter() {
        if !shadow.inset {
          continue;
        }

        let shadow = SizedShadow::from_box_shadow(
          *shadow,
          &context.sizing,
          context.current_color,
          layout.size,
        );
        shadow.draw_inset(context.transform, border_radius, canvas, layout)?;
      }
    }
    Ok(())
  }

  pub(crate) fn draw_background(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    let mut border_radius = BorderProperties::from_context(context, layout.size, layout.border);

    match context.style.background_clip {
      BackgroundClip::BorderBox => {
        let tiles = collect_background_layers(context, layout.size, &mut canvas.buffer_pool)?;

        for tile in tiles {
          for y in &tile.ys {
            for x in &tile.xs {
              canvas.overlay_image(
                &tile.tile,
                border_radius,
                context.transform * Affine::translation(*x as f32, *y as f32),
                context.style.image_rendering,
                tile.blend_mode,
              );
            }
          }
        }
      }
      BackgroundClip::PaddingBox => {
        border_radius.inset_by_border_width();

        let layers = collect_background_layers(context, layout.size, &mut canvas.buffer_pool)?;

        if let Some(tile) = rasterize_layers(
          layers,
          Size {
            width: (layout.size.width - layout.border.left - layout.border.right) as u32,
            height: (layout.size.height - layout.border.top - layout.border.bottom) as u32,
          },
          context,
          border_radius,
          Affine::translation(-layout.border.left, -layout.border.top),
          &mut canvas.mask_memory,
          &mut canvas.buffer_pool,
        )? {
          canvas.overlay_image(
            &tile,
            BorderProperties::default(),
            context.transform * Affine::translation(layout.border.left, layout.border.top),
            context.style.image_rendering,
            BlendMode::Normal,
          );

          if let BackgroundTile::Image(image) = tile {
            canvas.buffer_pool.release_image(image);
          }
        }
      }
      BackgroundClip::ContentBox => {
        border_radius.inset_by_border_width();
        border_radius.expand_by(layout.padding.map(|size| -size));

        let layers = collect_background_layers(context, layout.size, &mut canvas.buffer_pool)?;

        if let Some(tile) = rasterize_layers(
          layers,
          layout.content_box_size().map(|x| x as u32),
          context,
          border_radius,
          Affine::translation(
            -layout.padding.left - layout.border.left,
            -layout.padding.top - layout.border.top,
          ),
          &mut canvas.mask_memory,
          &mut canvas.buffer_pool,
        )? {
          canvas.overlay_image(
            &tile,
            BorderProperties::default(),
            context.transform
              * Affine::translation(
                layout.padding.left + layout.border.left,
                layout.padding.top + layout.border.top,
              ),
            context.style.image_rendering,
            BlendMode::Normal,
          );

          if let BackgroundTile::Image(image) = tile {
            canvas.buffer_pool.release_image(image);
          }
        }
      }
      _ => {}
    }

    Ok(())
  }

  pub(crate) fn draw_content(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    match &self.kind {
      NodeKind::Container { .. } => Ok(()),
      NodeKind::Image(image) => draw_image_node_content(image, context, canvas, layout),
      NodeKind::Text(text) => draw_text_node_content(text, context, canvas, layout),
    }
  }

  pub(crate) fn draw_border(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    let clip_image = if context.style.background_clip == BackgroundClip::BorderArea {
      rasterize_layers(
        collect_background_layers(context, layout.size, &mut canvas.buffer_pool)?,
        layout.size.map(|x| x as u32),
        context,
        BorderProperties::default(),
        Affine::IDENTITY,
        &mut canvas.mask_memory,
        &mut canvas.buffer_pool,
      )?
    } else {
      None
    };

    BorderProperties::from_context(context, layout.size, layout.border).draw(
      canvas,
      layout.size,
      context.transform,
      clip_image.as_ref(),
    );

    if let Some(BackgroundTile::Image(image)) = clip_image {
      canvas.buffer_pool.release_image(image);
    }
    Ok(())
  }

  pub(crate) fn draw_outline(
    &self,
    context: &RenderContext,
    canvas: &mut Canvas,
    layout: Layout,
  ) -> Result<()> {
    let width = context
      .style
      .outline_width
      .to_px(&context.sizing, layout.size.width)
      .max(0.0);

    let offset = context
      .style
      .outline_offset
      .to_px(&context.sizing, layout.size.width);

    let mut border = BorderProperties {
      width: Sides([width; 4]).into(),
      color: context.style.outline_color.resolve(context.current_color),
      style: context.style.outline_style,
      image_rendering: context.style.image_rendering,
      radius: BorderProperties::resolve_radius_part(context, layout.size),
    };

    border.expand_by(Sides([offset + width; 4]).into());

    let transform = Affine::translation(-offset - width, -offset - width) * context.transform;
    let size = layout.size.map(|x| x + (offset + width) * 2.0);

    border.draw::<RgbaImage>(canvas, size, transform, None);

    Ok(())
  }
}

/// Style layers contributed by a node before cascade/inheritance assembly.
#[derive(Debug, Default, Clone)]
pub struct NodeStyleLayers {
  /// UA/default style preset for the element.
  pub preset: Option<Style>,
  /// Tailwind-derived author style for the element.
  pub author_tw: Option<TailwindValues>,
  /// Inline style attached directly to the element.
  pub inline: Option<Style>,
}

#[cfg(test)]
mod tests {
  use std::str::FromStr;

  use crate::layout::style::{BackgroundImage, Style, StyleDeclaration, tw::TailwindValues};

  use super::*;

  #[test]
  fn collect_style_fetch_tasks_collects_nested_background_image_urls() {
    let background_url = "https://placehold.co/80x80/22c55e/white";
    let node = Node::container([Node::container([]).with_style(Style::default().with(
      StyleDeclaration::background_image(Some(
        [BackgroundImage::Url(background_url.into())].into(),
      )),
    ))]);

    let mut collection = FetchTaskCollection::default();
    node.collect_style_fetch_tasks(&mut collection);
    let tasks = collection
      .into_inner()
      .iter()
      .map(ToString::to_string)
      .collect::<Vec<_>>();

    assert_eq!(tasks, vec![background_url.to_string()]);
  }

  #[test]
  fn collect_style_fetch_tasks_collects_preset_and_tailwind_image_urls() {
    let preset_url = "https://placehold.co/64x64/f97316/white";
    let tailwind_url = "/bg.png";
    let Ok(tw) = TailwindValues::from_str("bg-[url(/bg.png)]") else {
      unreachable!()
    };
    let node = Node::container([])
      .with_preset(
        Style::default().with(StyleDeclaration::background_image(Some(
          [BackgroundImage::Url(preset_url.into())].into(),
        ))),
      )
      .with_tw(tw);

    let mut collection = FetchTaskCollection::default();
    node.collect_style_fetch_tasks(&mut collection);

    let tasks = collection
      .into_inner()
      .iter()
      .map(ToString::to_string)
      .collect::<Vec<_>>();

    assert_eq!(
      tasks,
      vec![preset_url.to_string(), tailwind_url.to_string()]
    );
  }
}