Skip to main content

ass_renderer/renderer/
context.rs

1//! Rendering context with fonts, resolution, and backend configuration
2
3use fontdb::Database as FontDatabase;
4
5#[cfg(feature = "nostd")]
6use alloc::sync::Arc;
7#[cfg(not(feature = "nostd"))]
8use std::sync::Arc;
9
10/// Rendering context containing fonts, resolution, and configuration
11#[derive(Clone)]
12pub struct RenderContext {
13    width: u32,
14    height: u32,
15    font_database: Arc<FontDatabase>,
16    playback_res_x: u32,
17    playback_res_y: u32,
18    storage_res_x: u32,
19    storage_res_y: u32,
20    frame_rate: f32,
21    par: f32,
22}
23
24impl RenderContext {
25    /// Create a new render context with the given dimensions
26    pub fn new(width: u32, height: u32) -> Self {
27        let mut font_database = FontDatabase::new();
28        font_database.load_system_fonts();
29
30        Self {
31            width,
32            height,
33            font_database: Arc::new(font_database),
34            playback_res_x: width,
35            playback_res_y: height,
36            storage_res_x: width,
37            storage_res_y: height,
38            frame_rate: 24.0,
39            par: 1.0,
40        }
41    }
42
43    /// Create context with custom font database
44    pub fn with_font_database(width: u32, height: u32, font_database: FontDatabase) -> Self {
45        Self {
46            width,
47            height,
48            font_database: Arc::new(font_database),
49            playback_res_x: width,
50            playback_res_y: height,
51            storage_res_x: width,
52            storage_res_y: height,
53            frame_rate: 24.0,
54            par: 1.0,
55        }
56    }
57
58    /// Set playback resolution (from script info)
59    pub fn set_playback_resolution(&mut self, x: u32, y: u32) {
60        self.playback_res_x = x;
61        self.playback_res_y = y;
62    }
63
64    /// Set storage resolution (original script resolution)
65    pub fn set_storage_resolution(&mut self, x: u32, y: u32) {
66        self.storage_res_x = x;
67        self.storage_res_y = y;
68    }
69
70    /// Set frame rate
71    pub fn set_frame_rate(&mut self, fps: f32) {
72        self.frame_rate = fps;
73    }
74
75    /// Set pixel aspect ratio
76    pub fn set_pixel_aspect_ratio(&mut self, par: f32) {
77        self.par = par;
78    }
79
80    /// Get render width
81    pub fn width(&self) -> u32 {
82        self.width
83    }
84
85    /// Get render height
86    pub fn height(&self) -> u32 {
87        self.height
88    }
89
90    /// Get font database
91    pub fn font_database(&self) -> &FontDatabase {
92        &self.font_database
93    }
94
95    /// Get mutable font database
96    pub fn font_database_mut(&mut self) -> &mut FontDatabase {
97        Arc::get_mut(&mut self.font_database).expect("Font database has multiple references")
98    }
99
100    /// Get playback resolution X
101    pub fn playback_res_x(&self) -> u32 {
102        self.playback_res_x
103    }
104
105    /// Get playback resolution Y
106    pub fn playback_res_y(&self) -> u32 {
107        self.playback_res_y
108    }
109
110    /// Get storage resolution X
111    pub fn storage_res_x(&self) -> u32 {
112        self.storage_res_x
113    }
114
115    /// Get storage resolution Y
116    pub fn storage_res_y(&self) -> u32 {
117        self.storage_res_y
118    }
119
120    /// Get frame rate
121    pub fn frame_rate(&self) -> f32 {
122        self.frame_rate
123    }
124
125    /// Get pixel aspect ratio
126    pub fn pixel_aspect_ratio(&self) -> f32 {
127        self.par
128    }
129
130    /// Calculate X scale factor from storage to playback resolution
131    pub fn scale_x(&self) -> f32 {
132        self.playback_res_x as f32 / self.storage_res_x.max(1) as f32
133    }
134
135    /// Calculate Y scale factor from storage to playback resolution
136    pub fn scale_y(&self) -> f32 {
137        self.playback_res_y as f32 / self.storage_res_y.max(1) as f32
138    }
139
140    /// Calculate X scale factor from playback to render resolution
141    pub fn render_scale_x(&self) -> f32 {
142        self.width as f32 / self.playback_res_x.max(1) as f32
143    }
144
145    /// Calculate Y scale factor from playback to render resolution
146    pub fn render_scale_y(&self) -> f32 {
147        self.height as f32 / self.playback_res_y.max(1) as f32
148    }
149}
150
151impl Default for RenderContext {
152    fn default() -> Self {
153        Self::new(1920, 1080)
154    }
155}