Skip to main content

ffmpeg_the_third/codec/subtitle/
rect.rs

1use std::marker::PhantomData;
2
3use super::{Flags, Type};
4use crate::ffi::*;
5use crate::utils;
6
7pub enum Rect<'a> {
8    None(*const AVSubtitleRect),
9    Bitmap(Bitmap<'a>),
10    Text(Text<'a>),
11    Ass(Ass<'a>),
12}
13
14impl<'a> Rect<'a> {
15    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
16        match Type::from((*ptr).type_) {
17            Type::None => Rect::None(ptr),
18            Type::Bitmap => Rect::Bitmap(Bitmap::wrap(ptr)),
19            Type::Text => Rect::Text(Text::wrap(ptr)),
20            Type::Ass => Rect::Ass(Ass::wrap(ptr)),
21        }
22    }
23
24    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
25        match *self {
26            Rect::None(ptr) => ptr,
27            Rect::Bitmap(ref b) => b.as_ptr(),
28            Rect::Text(ref t) => t.as_ptr(),
29            Rect::Ass(ref a) => a.as_ptr(),
30        }
31    }
32}
33
34impl<'a> Rect<'a> {
35    pub fn flags(&self) -> Flags {
36        unsafe {
37            Flags::from_bits_truncate(match *self {
38                Rect::None(ptr) => (*ptr).flags,
39                Rect::Bitmap(ref b) => (*b.as_ptr()).flags,
40                Rect::Text(ref t) => (*t.as_ptr()).flags,
41                Rect::Ass(ref a) => (*a.as_ptr()).flags,
42            })
43        }
44    }
45}
46
47pub struct Bitmap<'a> {
48    ptr: *const AVSubtitleRect,
49
50    _marker: PhantomData<&'a ()>,
51}
52
53impl<'a> Bitmap<'a> {
54    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
55        Bitmap {
56            ptr,
57            _marker: PhantomData,
58        }
59    }
60
61    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
62        self.ptr
63    }
64}
65
66impl<'a> Bitmap<'a> {
67    pub fn x(&self) -> usize {
68        unsafe { (*self.as_ptr()).x as usize }
69    }
70
71    pub fn y(&self) -> usize {
72        unsafe { (*self.as_ptr()).y as usize }
73    }
74
75    pub fn width(&self) -> u32 {
76        unsafe { (*self.as_ptr()).w as u32 }
77    }
78
79    pub fn height(&self) -> u32 {
80        unsafe { (*self.as_ptr()).h as u32 }
81    }
82
83    pub fn colors(&self) -> usize {
84        unsafe { (*self.as_ptr()).nb_colors as usize }
85    }
86}
87
88pub struct Text<'a> {
89    ptr: *const AVSubtitleRect,
90
91    _marker: PhantomData<&'a ()>,
92}
93
94impl<'a> Text<'a> {
95    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
96        Text {
97            ptr,
98            _marker: PhantomData,
99        }
100    }
101
102    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
103        self.ptr
104    }
105}
106
107impl<'a> Text<'a> {
108    pub fn get(&self) -> &str {
109        unsafe { utils::str_from_c_ptr((*self.as_ptr()).text) }
110    }
111}
112
113pub struct Ass<'a> {
114    ptr: *const AVSubtitleRect,
115
116    _marker: PhantomData<&'a ()>,
117}
118
119impl<'a> Ass<'a> {
120    pub unsafe fn wrap(ptr: *const AVSubtitleRect) -> Self {
121        Ass {
122            ptr,
123            _marker: PhantomData,
124        }
125    }
126
127    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
128        self.ptr
129    }
130}
131
132impl<'a> Ass<'a> {
133    pub fn get(&self) -> &str {
134        unsafe { utils::str_from_c_ptr((*self.as_ptr()).ass) }
135    }
136}