oxidize-pdf 2.4.2

A pure Rust PDF generation and manipulation library with zero external dependencies
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! TreeMap Visualization Component
//!
//! This module implements tree maps for hierarchical data visualization,
//! showing nested rectangles proportional to data values.

use super::{
    component::ComponentConfig, ComponentPosition, ComponentSpan, DashboardComponent,
    DashboardTheme,
};
use crate::error::PdfError;
use crate::graphics::Color;
use crate::page::Page;

/// TreeMap visualization component
#[derive(Debug, Clone)]
pub struct TreeMap {
    /// Component configuration
    config: ComponentConfig,
    /// Tree map data
    data: Vec<TreeMapNode>,
    /// Configuration options
    options: TreeMapOptions,
}

impl TreeMap {
    /// Create a new tree map
    pub fn new(data: Vec<TreeMapNode>) -> Self {
        Self {
            config: ComponentConfig::new(ComponentSpan::new(6)), // Half width by default
            data,
            options: TreeMapOptions::default(),
        }
    }

    /// Set tree map options
    pub fn with_options(mut self, options: TreeMapOptions) -> Self {
        self.options = options;
        self
    }

    /// Simple squarified treemap layout (recursive)
    fn layout_nodes(
        &self,
        nodes: &[TreeMapNode],
        x: f64,
        y: f64,
        width: f64,
        height: f64,
        rects: &mut Vec<(TreeMapNode, f64, f64, f64, f64)>,
    ) {
        if nodes.is_empty() || width <= 0.0 || height <= 0.0 {
            return;
        }

        let total: f64 = nodes.iter().map(|n| n.value).sum();
        if total <= 0.0 {
            return;
        }

        let mut current_x = x;
        let mut current_y = y;
        let mut remaining_width = width;
        let mut remaining_height = height;

        for node in nodes {
            let ratio = node.value / total;
            let area = width * height * ratio;

            // Decide whether to split horizontally or vertically
            let (rect_width, rect_height) = if remaining_width > remaining_height {
                // Split horizontally
                let w = area / remaining_height;
                (w.min(remaining_width), remaining_height)
            } else {
                // Split vertically
                let h = area / remaining_width;
                (remaining_width, h.min(remaining_height))
            };

            // Add padding
            let padding = self.options.padding;
            let rect_x = current_x + padding;
            let rect_y = current_y + padding;
            let rect_w = (rect_width - 2.0 * padding).max(0.0);
            let rect_h = (rect_height - 2.0 * padding).max(0.0);

            rects.push((node.clone(), rect_x, rect_y, rect_w, rect_h));

            // Update position for next rectangle
            if remaining_width > remaining_height {
                current_x += rect_width;
                remaining_width -= rect_width;
            } else {
                current_y += rect_height;
                remaining_height -= rect_height;
            }
        }
    }
}

impl DashboardComponent for TreeMap {
    fn render(
        &self,
        page: &mut Page,
        position: ComponentPosition,
        theme: &DashboardTheme,
    ) -> Result<(), PdfError> {
        let title = self.options.title.as_deref().unwrap_or("TreeMap");

        let title_height = 30.0;
        let plot_x = position.x;
        let plot_y = position.y;
        let plot_width = position.width;
        let plot_height = position.height - title_height;

        // Render title
        page.text()
            .set_font(crate::Font::HelveticaBold, theme.typography.heading_size)
            .set_fill_color(theme.colors.text_primary)
            .at(position.x, position.y + position.height - 15.0)
            .write(title)?;

        // Calculate layout
        let mut rects = Vec::new();
        self.layout_nodes(
            &self.data,
            plot_x,
            plot_y,
            plot_width,
            plot_height,
            &mut rects,
        );

        // Default colors if not specified
        let default_colors = vec![
            Color::hex("#1f77b4"),
            Color::hex("#ff7f0e"),
            Color::hex("#2ca02c"),
            Color::hex("#d62728"),
            Color::hex("#9467bd"),
            Color::hex("#8c564b"),
            Color::hex("#e377c2"),
            Color::hex("#7f7f7f"),
            Color::hex("#bcbd22"),
            Color::hex("#17becf"),
        ];

        // Render rectangles
        for (idx, (node, x, y, w, h)) in rects.iter().enumerate() {
            let color = node
                .color
                .unwrap_or(default_colors[idx % default_colors.len()]);

            // Draw rectangle
            page.graphics()
                .set_fill_color(color)
                .rect(*x, *y, *w, *h)
                .fill();

            // Draw border
            page.graphics()
                .set_stroke_color(Color::white())
                .set_line_width(1.5)
                .rect(*x, *y, *w, *h)
                .stroke();

            // Draw label if enabled and rectangle is large enough
            if self.options.show_labels && *w > 40.0 && *h > 20.0 {
                // Determine text color based on background
                let text_color = if self.is_dark_color(&color) {
                    Color::white()
                } else {
                    Color::black()
                };

                // Draw name
                page.text()
                    .set_font(crate::Font::HelveticaBold, 9.0)
                    .set_fill_color(text_color)
                    .at(x + 5.0, y + h - 15.0)
                    .write(&node.name)?;

                // Draw value
                page.text()
                    .set_font(crate::Font::Helvetica, 8.0)
                    .set_fill_color(text_color)
                    .at(x + 5.0, y + h - 28.0)
                    .write(&format!("{:.0}", node.value))?;
            }
        }

        Ok(())
    }

    fn get_span(&self) -> ComponentSpan {
        self.config.span
    }
    fn set_span(&mut self, span: ComponentSpan) {
        self.config.span = span;
    }
    fn preferred_height(&self, _available_width: f64) -> f64 {
        250.0
    }
    fn component_type(&self) -> &'static str {
        "TreeMap"
    }
    fn complexity_score(&self) -> u8 {
        70
    }
}

impl TreeMap {
    /// Check if a color is dark (for text contrast)
    fn is_dark_color(&self, color: &Color) -> bool {
        let (r, g, b) = match color {
            Color::Rgb(r, g, b) => (*r, *g, *b),
            Color::Gray(v) => (*v, *v, *v),
            Color::Cmyk(c, m, y, k) => {
                let r = (1.0 - c) * (1.0 - k);
                let g = (1.0 - m) * (1.0 - k);
                let b = (1.0 - y) * (1.0 - k);
                (r, g, b)
            }
        };
        let luminance = 0.299 * r + 0.587 * g + 0.114 * b;
        luminance < 0.5
    }
}

/// TreeMap node data
#[derive(Debug, Clone)]
pub struct TreeMapNode {
    pub name: String,
    pub value: f64,
    pub color: Option<Color>,
    pub children: Vec<TreeMapNode>,
}

/// TreeMap options
#[derive(Debug, Clone)]
pub struct TreeMapOptions {
    pub title: Option<String>,
    pub show_labels: bool,
    pub padding: f64,
}

impl Default for TreeMapOptions {
    fn default() -> Self {
        Self {
            title: None,
            show_labels: true,
            padding: 2.0,
        }
    }
}

/// Builder for TreeMap
pub struct TreeMapBuilder;

impl TreeMapBuilder {
    pub fn new() -> Self {
        Self
    }
    pub fn build(self) -> TreeMap {
        TreeMap::new(vec![])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_treemap_data() -> Vec<TreeMapNode> {
        vec![
            TreeMapNode {
                name: "Category A".to_string(),
                value: 100.0,
                color: None,
                children: vec![],
            },
            TreeMapNode {
                name: "Category B".to_string(),
                value: 50.0,
                color: Some(Color::rgb(0.0, 0.5, 1.0)),
                children: vec![],
            },
            TreeMapNode {
                name: "Category C".to_string(),
                value: 30.0,
                color: None,
                children: vec![],
            },
        ]
    }

    #[test]
    fn test_treemap_new() {
        let data = sample_treemap_data();
        let treemap = TreeMap::new(data.clone());

        assert_eq!(treemap.data.len(), 3);
        assert_eq!(treemap.data[0].name, "Category A");
        assert_eq!(treemap.data[0].value, 100.0);
    }

    #[test]
    fn test_treemap_with_options() {
        let data = sample_treemap_data();
        let options = TreeMapOptions {
            title: Some("My TreeMap".to_string()),
            show_labels: false,
            padding: 5.0,
        };

        let treemap = TreeMap::new(data).with_options(options);

        assert_eq!(treemap.options.title, Some("My TreeMap".to_string()));
        assert!(!treemap.options.show_labels);
        assert_eq!(treemap.options.padding, 5.0);
    }

    #[test]
    fn test_treemap_options_default() {
        let options = TreeMapOptions::default();

        assert!(options.title.is_none());
        assert!(options.show_labels);
        assert_eq!(options.padding, 2.0);
    }

    #[test]
    fn test_treemap_builder() {
        let builder = TreeMapBuilder::new();
        let treemap = builder.build();

        assert!(treemap.data.is_empty());
    }

    #[test]
    fn test_treemap_node_creation() {
        let node = TreeMapNode {
            name: "Test Node".to_string(),
            value: 42.0,
            color: Some(Color::rgb(1.0, 0.0, 0.0)),
            children: vec![TreeMapNode {
                name: "Child".to_string(),
                value: 10.0,
                color: None,
                children: vec![],
            }],
        };

        assert_eq!(node.name, "Test Node");
        assert_eq!(node.value, 42.0);
        assert!(node.color.is_some());
        assert_eq!(node.children.len(), 1);
        assert_eq!(node.children[0].name, "Child");
    }

    #[test]
    fn test_layout_nodes_empty() {
        let treemap = TreeMap::new(vec![]);
        let mut rects = Vec::new();

        treemap.layout_nodes(&[], 0.0, 0.0, 100.0, 100.0, &mut rects);

        assert!(rects.is_empty());
    }

    #[test]
    fn test_layout_nodes_single() {
        let data = vec![TreeMapNode {
            name: "Single".to_string(),
            value: 100.0,
            color: None,
            children: vec![],
        }];
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        treemap.layout_nodes(&data, 0.0, 0.0, 100.0, 100.0, &mut rects);

        assert_eq!(rects.len(), 1);
        assert_eq!(rects[0].0.name, "Single");
    }

    #[test]
    fn test_layout_nodes_multiple() {
        let data = sample_treemap_data();
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        treemap.layout_nodes(&data, 0.0, 0.0, 300.0, 200.0, &mut rects);

        assert_eq!(rects.len(), 3);

        // All rectangles should have positive dimensions
        for (_, x, y, w, h) in &rects {
            assert!(*x >= 0.0);
            assert!(*y >= 0.0);
            assert!(*w > 0.0);
            assert!(*h > 0.0);
        }
    }

    #[test]
    fn test_layout_nodes_proportional() {
        let data = vec![
            TreeMapNode {
                name: "A".to_string(),
                value: 75.0,
                color: None,
                children: vec![],
            },
            TreeMapNode {
                name: "B".to_string(),
                value: 25.0,
                color: None,
                children: vec![],
            },
        ];
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        treemap.layout_nodes(&data, 0.0, 0.0, 100.0, 100.0, &mut rects);

        // The larger node should have approximately 3x the area
        let area_a = rects[0].3 * rects[0].4;
        let area_b = rects[1].3 * rects[1].4;

        // Due to padding, the ratio might not be exact
        assert!(area_a > area_b);
    }

    #[test]
    fn test_layout_nodes_zero_size() {
        let data = sample_treemap_data();
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        // Zero width
        treemap.layout_nodes(&data, 0.0, 0.0, 0.0, 100.0, &mut rects);
        assert!(rects.is_empty());

        // Zero height
        rects.clear();
        treemap.layout_nodes(&data, 0.0, 0.0, 100.0, 0.0, &mut rects);
        assert!(rects.is_empty());
    }

    #[test]
    fn test_layout_nodes_zero_total_value() {
        let data = vec![
            TreeMapNode {
                name: "A".to_string(),
                value: 0.0,
                color: None,
                children: vec![],
            },
            TreeMapNode {
                name: "B".to_string(),
                value: 0.0,
                color: None,
                children: vec![],
            },
        ];
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        treemap.layout_nodes(&data, 0.0, 0.0, 100.0, 100.0, &mut rects);

        // Should not produce any rectangles when total is zero
        assert!(rects.is_empty());
    }

    #[test]
    fn test_is_dark_color_with_black() {
        let treemap = TreeMap::new(vec![]);

        assert!(treemap.is_dark_color(&Color::rgb(0.0, 0.0, 0.0)));
    }

    #[test]
    fn test_is_dark_color_with_white() {
        let treemap = TreeMap::new(vec![]);

        assert!(!treemap.is_dark_color(&Color::rgb(1.0, 1.0, 1.0)));
    }

    #[test]
    fn test_is_dark_color_with_gray() {
        let treemap = TreeMap::new(vec![]);

        // Gray(0.3) has luminance 0.3, which is < 0.5
        assert!(treemap.is_dark_color(&Color::Gray(0.3)));
        // Gray(0.7) has luminance 0.7, which is > 0.5
        assert!(!treemap.is_dark_color(&Color::Gray(0.7)));
    }

    #[test]
    fn test_is_dark_color_with_cmyk() {
        let treemap = TreeMap::new(vec![]);

        // CMYK black (0, 0, 0, 1) -> RGB (0, 0, 0)
        assert!(treemap.is_dark_color(&Color::Cmyk(0.0, 0.0, 0.0, 1.0)));
        // CMYK white-ish (0, 0, 0, 0) -> RGB (1, 1, 1)
        assert!(!treemap.is_dark_color(&Color::Cmyk(0.0, 0.0, 0.0, 0.0)));
    }

    #[test]
    fn test_is_dark_color_with_primary_colors() {
        let treemap = TreeMap::new(vec![]);

        // Red: luminance = 0.299
        assert!(treemap.is_dark_color(&Color::rgb(1.0, 0.0, 0.0)));
        // Green: luminance = 0.587
        assert!(!treemap.is_dark_color(&Color::rgb(0.0, 1.0, 0.0)));
        // Blue: luminance = 0.114
        assert!(treemap.is_dark_color(&Color::rgb(0.0, 0.0, 1.0)));
    }

    #[test]
    fn test_component_span() {
        let data = sample_treemap_data();
        let mut treemap = TreeMap::new(data);

        // Default span
        let span = treemap.get_span();
        assert_eq!(span.columns, 6);

        // Set new span
        treemap.set_span(ComponentSpan::new(12));
        assert_eq!(treemap.get_span().columns, 12);
    }

    #[test]
    fn test_component_type() {
        let treemap = TreeMap::new(vec![]);

        assert_eq!(treemap.component_type(), "TreeMap");
    }

    #[test]
    fn test_complexity_score() {
        let treemap = TreeMap::new(vec![]);

        assert_eq!(treemap.complexity_score(), 70);
    }

    #[test]
    fn test_preferred_height() {
        let treemap = TreeMap::new(vec![]);

        assert_eq!(treemap.preferred_height(1000.0), 250.0);
    }

    #[test]
    fn test_treemap_node_with_children() {
        let data = vec![TreeMapNode {
            name: "Parent".to_string(),
            value: 100.0,
            color: None,
            children: vec![
                TreeMapNode {
                    name: "Child 1".to_string(),
                    value: 60.0,
                    color: None,
                    children: vec![],
                },
                TreeMapNode {
                    name: "Child 2".to_string(),
                    value: 40.0,
                    color: None,
                    children: vec![],
                },
            ],
        }];

        let treemap = TreeMap::new(data.clone());

        assert_eq!(treemap.data[0].children.len(), 2);
        assert_eq!(treemap.data[0].children[0].value, 60.0);
        assert_eq!(treemap.data[0].children[1].value, 40.0);
    }

    #[test]
    fn test_layout_nodes_with_wide_rectangle() {
        let data = vec![
            TreeMapNode {
                name: "A".to_string(),
                value: 50.0,
                color: None,
                children: vec![],
            },
            TreeMapNode {
                name: "B".to_string(),
                value: 50.0,
                color: None,
                children: vec![],
            },
        ];
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        // Wide rectangle (width > height) should split horizontally
        treemap.layout_nodes(&data, 0.0, 0.0, 200.0, 50.0, &mut rects);

        assert_eq!(rects.len(), 2);
    }

    #[test]
    fn test_layout_nodes_with_tall_rectangle() {
        let data = vec![
            TreeMapNode {
                name: "A".to_string(),
                value: 50.0,
                color: None,
                children: vec![],
            },
            TreeMapNode {
                name: "B".to_string(),
                value: 50.0,
                color: None,
                children: vec![],
            },
        ];
        let treemap = TreeMap::new(data.clone());
        let mut rects = Vec::new();

        // Tall rectangle (height > width) should split vertically
        treemap.layout_nodes(&data, 0.0, 0.0, 50.0, 200.0, &mut rects);

        assert_eq!(rects.len(), 2);
    }

    #[test]
    fn test_layout_respects_padding() {
        let data = vec![TreeMapNode {
            name: "Single".to_string(),
            value: 100.0,
            color: None,
            children: vec![],
        }];

        let options = TreeMapOptions {
            title: None,
            show_labels: true,
            padding: 10.0,
        };
        let treemap = TreeMap::new(data.clone()).with_options(options);
        let mut rects = Vec::new();

        treemap.layout_nodes(&data, 0.0, 0.0, 100.0, 100.0, &mut rects);

        // With padding of 10.0, the rectangle should start at (10, 10)
        // and have dimensions reduced by 2*10 = 20
        let (_, x, y, w, h) = &rects[0];
        assert!((*x - 10.0).abs() < 0.01);
        assert!((*y - 10.0).abs() < 0.01);
        assert!((*w - 80.0).abs() < 0.01);
        assert!((*h - 80.0).abs() < 0.01);
    }
}