leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Responsive Typography for leptos-helios
//!
//! This module provides responsive typography management for charts and visualizations,
//! including font scaling, responsive text sizing, and device-specific typography.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use thiserror::Error;
use tokio::sync::RwLock;

use super::breakpoints::{Breakpoint, DeviceType, Orientation};

/// Typography size scale
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum TypographySize {
    XSmall,
    Small,
    Medium,
    Large,
    XLarge,
    XXLarge,
    XXXLarge,
}

/// Typography scale configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypographyScale {
    pub base_size: f64,
    pub scale_factor: f64,
    pub line_height: f64,
    pub letter_spacing: f64,
    pub font_weight: f64,
}

impl Default for TypographyScale {
    fn default() -> Self {
        Self {
            base_size: 16.0,
            scale_factor: 1.2,
            line_height: 1.5,
            letter_spacing: 0.0,
            font_weight: 400.0,
        }
    }
}

/// Font scale configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FontScale {
    pub xsmall: f64,
    pub small: f64,
    pub medium: f64,
    pub large: f64,
    pub xlarge: f64,
    pub xxlarge: f64,
    pub xxxlarge: f64,
}

impl Default for FontScale {
    fn default() -> Self {
        Self {
            xsmall: 12.0,
            small: 14.0,
            medium: 16.0,
            large: 18.0,
            xlarge: 20.0,
            xxlarge: 24.0,
            xxxlarge: 32.0,
        }
    }
}

/// Responsive typography configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponsiveTypography {
    pub id: String,
    pub font_family: String,
    pub font_scale: FontScale,
    pub responsive_scales: HashMap<String, TypographyScale>,
    pub breakpoint_overrides: HashMap<String, TypographyOverride>,
    pub accessibility_enabled: bool,
    pub high_contrast_mode: bool,
}

/// Typography override for specific breakpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypographyOverride {
    pub font_scale: Option<FontScale>,
    pub base_size: Option<f64>,
    pub scale_factor: Option<f64>,
    pub line_height: Option<f64>,
    pub letter_spacing: Option<f64>,
    pub font_weight: Option<f64>,
}

/// Typography manager for responsive design
pub struct TypographyManager {
    config: TypographyConfig,
    typography_systems: Arc<RwLock<HashMap<String, ResponsiveTypography>>>,
    stats: Arc<RwLock<TypographyStats>>,
}

/// Typography configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypographyConfig {
    pub default_font_family: String,
    pub default_font_scale: FontScale,
    pub responsive_scaling: bool,
    pub accessibility_scaling: bool,
    pub high_contrast_support: bool,
    pub mobile_scale_factor: f64,
    pub tablet_scale_factor: f64,
    pub desktop_scale_factor: f64,
    pub large_desktop_scale_factor: f64,
    pub ultra_wide_scale_factor: f64,
}

impl Default for TypographyConfig {
    fn default() -> Self {
        Self {
            default_font_family: "system-ui, -apple-system, sans-serif".to_string(),
            default_font_scale: FontScale::default(),
            responsive_scaling: true,
            accessibility_scaling: true,
            high_contrast_support: true,
            mobile_scale_factor: 0.875,
            tablet_scale_factor: 1.0,
            desktop_scale_factor: 1.125,
            large_desktop_scale_factor: 1.25,
            ultra_wide_scale_factor: 1.375,
        }
    }
}

/// Typography statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypographyStats {
    pub total_typography_systems: usize,
    pub responsive_typography: usize,
    pub mobile_typography: usize,
    pub tablet_typography: usize,
    pub desktop_typography: usize,
    pub accessibility_applications: usize,
    pub high_contrast_applications: usize,
    pub font_scale_calculations: usize,
    pub responsive_adjustments: usize,
}

impl Default for TypographyStats {
    fn default() -> Self {
        Self {
            total_typography_systems: 0,
            responsive_typography: 0,
            mobile_typography: 0,
            tablet_typography: 0,
            desktop_typography: 0,
            accessibility_applications: 0,
            high_contrast_applications: 0,
            font_scale_calculations: 0,
            responsive_adjustments: 0,
        }
    }
}

impl TypographyManager {
    /// Create a new typography manager
    pub fn new(config: TypographyConfig) -> Self {
        Self {
            config,
            typography_systems: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(TypographyStats::default())),
        }
    }

    /// Create a new responsive typography system
    pub async fn create_typography_system(
        &self,
        id: String,
        font_family: String,
    ) -> Result<ResponsiveTypography, TypographyError> {
        let typography = ResponsiveTypography {
            id: id.clone(),
            font_family,
            font_scale: self.config.default_font_scale.clone(),
            responsive_scales: HashMap::new(),
            breakpoint_overrides: HashMap::new(),
            accessibility_enabled: self.config.accessibility_scaling,
            high_contrast_mode: false,
        };

        // Store typography system
        {
            let mut systems = self.typography_systems.write().await;
            systems.insert(id, typography.clone());
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_typography_systems += 1;
            stats.responsive_typography += 1;
        }

        Ok(typography)
    }

    /// Apply responsive typography to a layout
    pub async fn apply_responsive_typography(
        &self,
        layout: &mut super::layout::ResponsiveLayout,
        breakpoint: &Breakpoint,
    ) -> Result<(), TypographyError> {
        // Apply responsive scaling based on breakpoint
        let scale_factor = self.get_scale_factor_for_breakpoint(breakpoint).await;

        // Update typography systems in the layout
        for item in &mut layout.items {
            if let Some(typography_id) = item.responsive_properties.get("typography_id") {
                if let Some(typography_id_str) = typography_id.as_str() {
                    if let Some(typography) = self.get_typography_system(typography_id_str).await {
                        let scaled_typography = self
                            .scale_typography_for_breakpoint(&typography, breakpoint, scale_factor)
                            .await;

                        // Apply scaled typography to item
                        self.apply_typography_to_item(item, &scaled_typography, breakpoint)
                            .await?;
                    }
                }
            }
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.responsive_adjustments += 1;

            match breakpoint.device_type {
                DeviceType::Mobile => stats.mobile_typography += 1,
                DeviceType::Tablet => stats.tablet_typography += 1,
                DeviceType::Desktop => stats.desktop_typography += 1,
                _ => {}
            }
        }

        Ok(())
    }

    /// Get scale factor for breakpoint
    async fn get_scale_factor_for_breakpoint(&self, breakpoint: &Breakpoint) -> f64 {
        match breakpoint.device_type {
            DeviceType::Mobile => self.config.mobile_scale_factor,
            DeviceType::Tablet => self.config.tablet_scale_factor,
            DeviceType::Desktop => self.config.desktop_scale_factor,
            DeviceType::LargeDesktop => self.config.large_desktop_scale_factor,
            DeviceType::UltraWide => self.config.ultra_wide_scale_factor,
        }
    }

    /// Scale typography for breakpoint
    async fn scale_typography_for_breakpoint(
        &self,
        typography: &ResponsiveTypography,
        breakpoint: &Breakpoint,
        scale_factor: f64,
    ) -> ResponsiveTypography {
        let mut scaled_typography = typography.clone();

        // Apply breakpoint-specific overrides
        if let Some(override_config) = typography
            .breakpoint_overrides
            .get(breakpoint.name.as_str())
        {
            if let Some(override_scale) = &override_config.font_scale {
                scaled_typography.font_scale = override_scale.clone();
            }
        }

        // Apply scale factor
        scaled_typography.font_scale.xsmall *= scale_factor;
        scaled_typography.font_scale.small *= scale_factor;
        scaled_typography.font_scale.medium *= scale_factor;
        scaled_typography.font_scale.large *= scale_factor;
        scaled_typography.font_scale.xlarge *= scale_factor;
        scaled_typography.font_scale.xxlarge *= scale_factor;
        scaled_typography.font_scale.xxxlarge *= scale_factor;

        scaled_typography
    }

    /// Apply typography to layout item
    async fn apply_typography_to_item(
        &self,
        item: &mut super::layout::LayoutItem,
        typography: &ResponsiveTypography,
        breakpoint: &Breakpoint,
    ) -> Result<(), TypographyError> {
        // Apply typography properties to item
        if let Some(typography_size) = item.responsive_properties.get("typography_size") {
            if let Some(size_str) = typography_size.as_str() {
                let size = match size_str {
                    "xsmall" => TypographySize::XSmall,
                    "small" => TypographySize::Small,
                    "medium" => TypographySize::Medium,
                    "large" => TypographySize::Large,
                    "xlarge" => TypographySize::XLarge,
                    "xxlarge" => TypographySize::XXLarge,
                    "xxxlarge" => TypographySize::XXXLarge,
                    _ => TypographySize::Medium,
                };

                let font_size =
                    self.get_font_size_for_typography_size(&typography.font_scale, size);

                // Update item properties with font size
                item.responsive_properties.insert(
                    "font_size".to_string(),
                    serde_json::Value::Number(serde_json::Number::from_f64(font_size).unwrap()),
                );
            }
        }

        // Apply accessibility scaling if enabled
        if typography.accessibility_enabled && self.config.accessibility_scaling {
            self.apply_accessibility_scaling(item, breakpoint).await?;
        }

        // Apply high contrast mode if enabled
        if typography.high_contrast_mode && self.config.high_contrast_support {
            self.apply_high_contrast_mode(item).await?;
        }

        Ok(())
    }

    /// Get font size for typography size
    fn get_font_size_for_typography_size(
        &self,
        font_scale: &FontScale,
        size: TypographySize,
    ) -> f64 {
        match size {
            TypographySize::XSmall => font_scale.xsmall,
            TypographySize::Small => font_scale.small,
            TypographySize::Medium => font_scale.medium,
            TypographySize::Large => font_scale.large,
            TypographySize::XLarge => font_scale.xlarge,
            TypographySize::XXLarge => font_scale.xxlarge,
            TypographySize::XXXLarge => font_scale.xxxlarge,
        }
    }

    /// Apply accessibility scaling
    async fn apply_accessibility_scaling(
        &self,
        item: &mut super::layout::LayoutItem,
        breakpoint: &Breakpoint,
    ) -> Result<(), TypographyError> {
        // Increase font size for better accessibility
        let accessibility_factor = 1.2;

        if let Some(font_size) = item.responsive_properties.get("font_size") {
            if let Some(size_value) = font_size.as_f64() {
                let new_size = size_value * accessibility_factor;
                item.responsive_properties.insert(
                    "font_size".to_string(),
                    serde_json::Value::Number(serde_json::Number::from_f64(new_size).unwrap()),
                );
            }
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.accessibility_applications += 1;
        }

        Ok(())
    }

    /// Apply high contrast mode
    async fn apply_high_contrast_mode(
        &self,
        item: &mut super::layout::LayoutItem,
    ) -> Result<(), TypographyError> {
        // Apply high contrast styling
        item.responsive_properties
            .insert("high_contrast".to_string(), serde_json::Value::Bool(true));
        item.responsive_properties.insert(
            "font_weight".to_string(),
            serde_json::Value::Number(serde_json::Number::from_f64(600.0).unwrap()),
        );

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.high_contrast_applications += 1;
        }

        Ok(())
    }

    /// Get typography system by ID
    pub async fn get_typography_system(&self, id: &str) -> Option<ResponsiveTypography> {
        let systems = self.typography_systems.read().await;
        systems.get(id).cloned()
    }

    /// Update typography system
    pub async fn update_typography_system(
        &self,
        id: &str,
        typography: ResponsiveTypography,
    ) -> Result<(), TypographyError> {
        let mut systems = self.typography_systems.write().await;
        systems.insert(id.to_string(), typography);
        Ok(())
    }

    /// Update typography configuration
    pub async fn update_config(&mut self, config: TypographyConfig) -> Result<(), TypographyError> {
        self.config = config;
        Ok(())
    }

    /// Get typography statistics
    pub async fn get_stats(&self) -> TypographyStats {
        self.stats.read().await.clone()
    }

    /// Calculate font scale for breakpoint
    pub async fn calculate_font_scale(&self, breakpoint: &Breakpoint) -> FontScale {
        let scale_factor = self.get_scale_factor_for_breakpoint(breakpoint).await;

        FontScale {
            xsmall: self.config.default_font_scale.xsmall * scale_factor,
            small: self.config.default_font_scale.small * scale_factor,
            medium: self.config.default_font_scale.medium * scale_factor,
            large: self.config.default_font_scale.large * scale_factor,
            xlarge: self.config.default_font_scale.xlarge * scale_factor,
            xxlarge: self.config.default_font_scale.xxlarge * scale_factor,
            xxxlarge: self.config.default_font_scale.xxxlarge * scale_factor,
        }
    }

    /// Remove typography system
    pub async fn remove_typography_system(&self, id: &str) -> Result<(), TypographyError> {
        let mut systems = self.typography_systems.write().await;
        systems.remove(id);
        Ok(())
    }

    /// Get all typography systems
    pub async fn get_all_typography_systems(&self) -> HashMap<String, ResponsiveTypography> {
        self.typography_systems.read().await.clone()
    }
}

/// Typography error types
#[derive(Debug, Error)]
pub enum TypographyError {
    #[error("Typography system not found: {id}")]
    TypographySystemNotFound { id: String },

    #[error("Invalid typography configuration: {message}")]
    InvalidConfiguration { message: String },

    #[error("Font scale calculation error: {message}")]
    FontScaleError { message: String },

    #[error("Accessibility scaling error: {message}")]
    AccessibilityError { message: String },

    #[error("High contrast mode error: {message}")]
    HighContrastError { message: String },
}

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

    fn create_test_typography_config() -> TypographyConfig {
        TypographyConfig {
            default_font_family: "Arial, sans-serif".to_string(),
            default_font_scale: FontScale::default(),
            responsive_scaling: true,
            accessibility_scaling: true,
            high_contrast_support: true,
            mobile_scale_factor: 0.875,
            tablet_scale_factor: 1.0,
            desktop_scale_factor: 1.125,
            large_desktop_scale_factor: 1.25,
            ultra_wide_scale_factor: 1.375,
        }
    }

    fn create_test_breakpoint() -> Breakpoint {
        Breakpoint {
            name: "mobile".to_string(),
            min_width: 0.0,
            max_width: Some(768.0),
            device_type: DeviceType::Mobile,
            orientation: Orientation::Portrait,
        }
    }

    #[tokio::test]
    async fn test_typography_manager_creation() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_typography_systems, 0);
    }

    #[tokio::test]
    async fn test_create_typography_system() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let typography = manager
            .create_typography_system(
                "test-typography".to_string(),
                "Arial, sans-serif".to_string(),
            )
            .await
            .unwrap();

        assert_eq!(typography.id, "test-typography");
        assert_eq!(typography.font_family, "Arial, sans-serif");
        assert!(typography.accessibility_enabled);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_typography_systems, 1);
        assert_eq!(stats.responsive_typography, 1);
    }

    #[tokio::test]
    async fn test_get_typography_system() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        manager
            .create_typography_system(
                "test-typography".to_string(),
                "Arial, sans-serif".to_string(),
            )
            .await
            .unwrap();

        let typography = manager.get_typography_system("test-typography").await;
        assert!(typography.is_some());
        assert_eq!(typography.unwrap().id, "test-typography");
    }

    #[tokio::test]
    async fn test_calculate_font_scale() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let breakpoint = create_test_breakpoint();
        let font_scale = manager.calculate_font_scale(&breakpoint).await;

        // Mobile scale factor is 0.875
        assert_eq!(font_scale.medium, 16.0 * 0.875);
        assert_eq!(font_scale.large, 18.0 * 0.875);
    }

    #[tokio::test]
    async fn test_scale_factor_for_breakpoint() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let mobile_breakpoint = Breakpoint {
            name: "mobile".to_string(),
            min_width: 0.0,
            max_width: Some(768.0),
            device_type: DeviceType::Mobile,
            orientation: Orientation::Portrait,
        };

        let desktop_breakpoint = Breakpoint {
            name: "desktop".to_string(),
            min_width: 1200.0,
            max_width: None,
            device_type: DeviceType::Desktop,
            orientation: Orientation::Landscape,
        };

        let mobile_scale = manager
            .get_scale_factor_for_breakpoint(&mobile_breakpoint)
            .await;
        let desktop_scale = manager
            .get_scale_factor_for_breakpoint(&desktop_breakpoint)
            .await;

        assert_eq!(mobile_scale, 0.875);
        assert_eq!(desktop_scale, 1.125);
    }

    #[tokio::test]
    async fn test_typography_size_enum() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let font_scale = FontScale::default();

        let xsmall_size =
            manager.get_font_size_for_typography_size(&font_scale, TypographySize::XSmall);
        let medium_size =
            manager.get_font_size_for_typography_size(&font_scale, TypographySize::Medium);
        let xxxlarge_size =
            manager.get_font_size_for_typography_size(&font_scale, TypographySize::XXXLarge);

        assert_eq!(xsmall_size, 12.0);
        assert_eq!(medium_size, 16.0);
        assert_eq!(xxxlarge_size, 32.0);
    }

    #[tokio::test]
    async fn test_update_typography_system() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        let mut typography = manager
            .create_typography_system(
                "test-typography".to_string(),
                "Arial, sans-serif".to_string(),
            )
            .await
            .unwrap();
        typography.font_family = "Helvetica, sans-serif".to_string();

        manager
            .update_typography_system("test-typography", typography)
            .await
            .unwrap();

        let updated_typography = manager
            .get_typography_system("test-typography")
            .await
            .unwrap();
        assert_eq!(updated_typography.font_family, "Helvetica, sans-serif");
    }

    #[tokio::test]
    async fn test_remove_typography_system() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        manager
            .create_typography_system(
                "test-typography".to_string(),
                "Arial, sans-serif".to_string(),
            )
            .await
            .unwrap();

        let typography = manager.get_typography_system("test-typography").await;
        assert!(typography.is_some());

        manager
            .remove_typography_system("test-typography")
            .await
            .unwrap();

        let typography = manager.get_typography_system("test-typography").await;
        assert!(typography.is_none());
    }

    #[tokio::test]
    async fn test_config_update() {
        let mut config = create_test_typography_config();
        config.mobile_scale_factor = 0.9;

        let mut manager = TypographyManager::new(config);

        let new_config = create_test_typography_config();
        manager.update_config(new_config).await.unwrap();

        // Verify config was updated
        let breakpoint = create_test_breakpoint();
        let font_scale = manager.calculate_font_scale(&breakpoint).await;
        assert_eq!(font_scale.medium, 16.0 * 0.875); // Should use new config
    }

    #[tokio::test]
    async fn test_typography_stats() {
        let config = create_test_typography_config();
        let manager = TypographyManager::new(config);

        manager
            .create_typography_system(
                "test-typography".to_string(),
                "Arial, sans-serif".to_string(),
            )
            .await
            .unwrap();

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_typography_systems, 1);
        assert_eq!(stats.responsive_typography, 1);
    }
}