ass_renderer/renderer/
context.rs1use fontdb::Database as FontDatabase;
4
5#[cfg(feature = "nostd")]
6use alloc::sync::Arc;
7#[cfg(not(feature = "nostd"))]
8use std::sync::Arc;
9
10#[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 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 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 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 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 pub fn set_frame_rate(&mut self, fps: f32) {
72 self.frame_rate = fps;
73 }
74
75 pub fn set_pixel_aspect_ratio(&mut self, par: f32) {
77 self.par = par;
78 }
79
80 pub fn width(&self) -> u32 {
82 self.width
83 }
84
85 pub fn height(&self) -> u32 {
87 self.height
88 }
89
90 pub fn font_database(&self) -> &FontDatabase {
92 &self.font_database
93 }
94
95 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 pub fn playback_res_x(&self) -> u32 {
102 self.playback_res_x
103 }
104
105 pub fn playback_res_y(&self) -> u32 {
107 self.playback_res_y
108 }
109
110 pub fn storage_res_x(&self) -> u32 {
112 self.storage_res_x
113 }
114
115 pub fn storage_res_y(&self) -> u32 {
117 self.storage_res_y
118 }
119
120 pub fn frame_rate(&self) -> f32 {
122 self.frame_rate
123 }
124
125 pub fn pixel_aspect_ratio(&self) -> f32 {
127 self.par
128 }
129
130 pub fn scale_x(&self) -> f32 {
132 self.playback_res_x as f32 / self.storage_res_x.max(1) as f32
133 }
134
135 pub fn scale_y(&self) -> f32 {
137 self.playback_res_y as f32 / self.storage_res_y.max(1) as f32
138 }
139
140 pub fn render_scale_x(&self) -> f32 {
142 self.width as f32 / self.playback_res_x.max(1) as f32
143 }
144
145 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}