Skip to main content

ass_renderer/pipeline/build/
mod.rs

1//! Fixed software pipeline implementation with proper style resolution
2
3#[cfg(feature = "nostd")]
4use alloc::string::String;
5#[cfg(not(feature = "nostd"))]
6use std::string::String;
7
8use ahash::AHashMap;
9use fontdb::Database as FontDatabase;
10
11use crate::pipeline::shaping::GlyphRenderer;
12
13mod animation;
14mod drawing;
15mod position;
16mod run;
17mod style;
18mod text;
19mod wrap;
20use style::OwnedStyle;
21
22/// Software rendering pipeline with proper style inheritance
23pub struct SoftwarePipeline {
24    /// Font database for text rendering
25    font_database: FontDatabase,
26    /// Glyph renderer
27    #[allow(dead_code)] // Glyph rendering component - used in future rendering features
28    glyph_renderer: GlyphRenderer,
29    /// Collision resolver for subtitle positioning
30    collision_resolver: crate::collision::CollisionResolver,
31    /// Render cache for performance
32    cache: crate::cache::RenderCache,
33    /// Current script styles map for quick lookup
34    styles_map: AHashMap<String, OwnedStyle>,
35    /// Default style for fallback
36    default_style: Option<OwnedStyle>,
37    /// Script playback resolution from PlayResX/PlayResY
38    play_res_x: f32,
39    play_res_y: f32,
40    /// Script layout resolution from LayoutResX/LayoutResY (if present)
41    layout_res_x: Option<f32>,
42    layout_res_y: Option<f32>,
43    /// Whether to scale border and shadow with video resolution
44    scaled_border_and_shadow: bool,
45    /// DPI scale factor for font rendering (default: 0.9)
46    /// libass uses 72 DPI, some systems use 96 DPI
47    /// Empirically adjusted to 0.9 for better libass visual match
48    dpi_scale: f32,
49    /// Script `WrapStyle` header (0 smart, 1 greedy, 2 none, 3 smart); the `\q`
50    /// override takes precedence per event. Defaults to 0.
51    wrap_style: u8,
52}
53
54impl Default for SoftwarePipeline {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl SoftwarePipeline {
61    /// Create a new fixed software pipeline
62    pub fn new() -> Self {
63        let mut font_database = FontDatabase::new();
64        font_database.load_system_fonts();
65
66        Self {
67            font_database,
68            glyph_renderer: GlyphRenderer::new(),
69            collision_resolver: crate::collision::CollisionResolver::new(1920.0, 1080.0),
70            cache: crate::cache::RenderCache::with_limits(5000, 2000),
71            styles_map: AHashMap::new(),
72            default_style: None,
73            play_res_x: 1920.0, // Default resolution
74            play_res_y: 1080.0, // Default resolution
75            layout_res_x: None,
76            layout_res_y: None,
77            scaled_border_and_shadow: true, // Default to true per ASS spec
78            dpi_scale: 0.9,                 // Adjusted for better libass compatibility (was 0.75)
79            wrap_style: 0,
80        }
81    }
82
83    /// Create with specific dimensions
84    pub fn with_dimensions(width: f32, height: f32) -> Self {
85        let mut font_database = FontDatabase::new();
86        font_database.load_system_fonts();
87
88        Self {
89            font_database,
90            glyph_renderer: GlyphRenderer::new(),
91            collision_resolver: crate::collision::CollisionResolver::new(width, height),
92            cache: crate::cache::RenderCache::with_limits(5000, 2000),
93            styles_map: AHashMap::new(),
94            default_style: None,
95            play_res_x: width,  // Use provided dimensions as default
96            play_res_y: height, // Use provided dimensions as default
97            layout_res_x: None,
98            layout_res_y: None,
99            scaled_border_and_shadow: true, // Default to true per ASS spec
100            dpi_scale: 0.9,                 // Adjusted for better libass compatibility (was 0.75)
101            wrap_style: 0,
102        }
103    }
104
105    /// Set DPI scale factor (default is 0.9 for libass compatibility)
106    /// Use 1.0 for 96 DPI, 0.9 for empirically matched libass rendering
107    pub fn set_dpi_scale(&mut self, scale: f32) {
108        self.dpi_scale = scale;
109    }
110
111    /// Get current DPI scale factor
112    pub fn dpi_scale(&self) -> f32 {
113        self.dpi_scale
114    }
115}