pub struct Position {
pub x: f32,
pub y: f32,
pub alignment: Alignment,
pub vertical_alignment: VerticalAlignment,
}Expand description
Subtitle position on screen.
Fields§
§x: f32Horizontal position (0.0 = left, 1.0 = right).
y: f32Vertical position (0.0 = top, 1.0 = bottom).
alignment: AlignmentHorizontal alignment.
vertical_alignment: VerticalAlignmentVertical alignment.
Implementations§
Source§impl Position
impl Position
Sourcepub const fn bottom_center() -> Self
pub const fn bottom_center() -> Self
Bottom center position (default).
Examples found in repository?
examples/render_subtitle.rs (line 66)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 // Example SRT subtitle data
9 let srt_data = r"1
1000:00:01,000 --> 00:00:04,000
11Hello, World!
12
132
1400:00:05,000 --> 00:00:08,000
15This is a subtitle example
16with multiple lines.
17
183
1900:00:10,000 --> 00:00:13,000
20<i>Styled subtitle with HTML tags</i>
21";
22
23 // Parse SRT subtitles
24 let subtitles = srt::parse(srt_data.as_bytes())?;
25 println!("Parsed {} subtitles", subtitles.len());
26
27 for (i, subtitle) in subtitles.iter().enumerate() {
28 println!(
29 "Subtitle {}: {}ms - {}ms: {}",
30 i + 1,
31 subtitle.start_time,
32 subtitle.end_time,
33 subtitle.text
34 );
35 }
36
37 // Create a sample video frame
38 let mut frame = VideoFrame::new(PixelFormat::Rgb24, 1920, 1080);
39 frame.allocate();
40
41 // Fill with blue background
42 if let Some(plane) = frame.planes.get_mut(0) {
43 let data_len = plane.data.len();
44 let mut new_data = vec![0u8; data_len];
45 for pixel in new_data.chunks_exact_mut(3) {
46 pixel[0] = 0; // R
47 pixel[1] = 0; // G
48 pixel[2] = 100; // B
49 }
50 plane.data = new_data;
51 }
52
53 // Note: In a real application, you would load a font file
54 // For this example, we'll show how to create the renderer
55 println!("\nTo use the subtitle renderer:");
56 println!("1. Load a font: Font::from_file(\"path/to/font.ttf\")?");
57 println!("2. Configure style with colors, size, outline, etc.");
58 println!("3. Create renderer: SubtitleRenderer::new(font, style)");
59 println!("4. Render: renderer.render_subtitle(&subtitle, &mut frame, timestamp)?");
60
61 // Example style configuration
62 let style = SubtitleStyle::default()
63 .with_font_size(48.0)
64 .with_color(255, 255, 255, 255) // White text
65 .with_outline(OutlineStyle::new(Color::black(), 2.0))
66 .with_position(Position::bottom_center())
67 .with_alignment(Alignment::Center)
68 .with_margins(40, 40, 40, 80);
69
70 println!(
71 "\nStyle configuration: Font size={}, Color=white, Outline=black 2px",
72 style.font_size
73 );
74
75 // Example timestamps
76 let ts1 = Timestamp::new(2000, Rational::new(1, 1000)); // 2 seconds
77 let ts2 = Timestamp::new(6000, Rational::new(1, 1000)); // 6 seconds
78
79 // Show which subtitles would be active at different times
80 for subtitle in &subtitles {
81 #[allow(clippy::cast_possible_truncation)]
82 let ts1_ms = (ts1.to_seconds() * 1000.0).round() as i64;
83 #[allow(clippy::cast_possible_truncation)]
84 let ts2_ms = (ts2.to_seconds() * 1000.0).round() as i64;
85 if subtitle.is_active(ts1_ms) {
86 println!(
87 "\nAt t=2s, active subtitle: '{}'",
88 subtitle.text.lines().next().unwrap_or("")
89 );
90 }
91 if subtitle.is_active(ts2_ms) {
92 println!(
93 "At t=6s, active subtitle: '{}'",
94 subtitle.text.lines().next().unwrap_or("")
95 );
96 }
97 }
98
99 // Example: Write subtitles back to SRT format
100 let output_srt = srt::write(&subtitles)?;
101 println!("\nGenerated SRT output:\n{output_srt}");
102
103 Ok(())
104}Sourcepub const fn top_center() -> Self
pub const fn top_center() -> Self
Top center position.
Sourcepub const fn middle_center() -> Self
pub const fn middle_center() -> Self
Middle center position.
Sourcepub const fn with_alignment(self, alignment: Alignment) -> Self
pub const fn with_alignment(self, alignment: Alignment) -> Self
Set alignment.
Sourcepub const fn with_vertical_alignment(
self,
vertical_alignment: VerticalAlignment,
) -> Self
pub const fn with_vertical_alignment( self, vertical_alignment: VerticalAlignment, ) -> Self
Set vertical alignment.
Trait Implementations§
impl Copy for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnsafeUnpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more