Skip to main content

ass_renderer/layout/
alignment.rs

1//! Alignment types for subtitle positioning
2
3/// Alignment types for subtitle positioning
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum Alignment {
6    // ASS alignments (numpad layout)
7    /// Bottom left alignment
8    BottomLeft = 1,
9    /// Bottom center alignment
10    BottomCenter = 2,
11    /// Bottom right alignment
12    BottomRight = 3,
13    /// Middle left alignment
14    MiddleLeft = 4,
15    /// Center alignment
16    Center = 5,
17    /// Middle right alignment
18    MiddleRight = 6,
19    /// Top left alignment
20    TopLeft = 7,
21    /// Top center alignment
22    TopCenter = 8,
23    /// Top right alignment
24    TopRight = 9,
25}
26
27impl Alignment {
28    /// Convert from numeric alignment value
29    pub fn from_value(value: u8) -> Self {
30        match value {
31            1 => Self::BottomLeft,
32            2 => Self::BottomCenter,
33            3 => Self::BottomRight,
34            4 => Self::MiddleLeft,
35            5 => Self::Center,
36            6 => Self::MiddleRight,
37            7 => Self::TopLeft,
38            8 => Self::TopCenter,
39            9 => Self::TopRight,
40            _ => Self::BottomCenter, // Default
41        }
42    }
43
44    /// Check if this is a legacy SSA alignment value
45    pub fn from_ssa_legacy(value: u8) -> Self {
46        // SSA legacy: 1=left, 2=center, 3=right
47        // +0=sub(bottom), +4=title(middle), +8=top, +128=mid-title
48        match value & 3 {
49            1 => {
50                // Left alignment
51                match value & 12 {
52                    0 => Self::BottomLeft,
53                    4 => Self::MiddleLeft,
54                    8 | 12 => Self::TopLeft,
55                    _ => Self::BottomLeft,
56                }
57            }
58            2 => {
59                // Center alignment
60                match value & 12 {
61                    0 => Self::BottomCenter,
62                    4 => Self::Center,
63                    8 | 12 => Self::TopCenter,
64                    _ => Self::BottomCenter,
65                }
66            }
67            3 => {
68                // Right alignment
69                match value & 12 {
70                    0 => Self::BottomRight,
71                    4 => Self::MiddleRight,
72                    8 | 12 => Self::TopRight,
73                    _ => Self::BottomRight,
74                }
75            }
76            _ => Self::BottomCenter,
77        }
78    }
79}