1 | use std::ffi::CString; |
2 | use std::ops::Deref; |
3 | |
4 | use super::{Ass, Bitmap, Flags, Text, Type}; |
5 | use ffi::*; |
6 | use libc::c_int; |
7 | |
8 | pub enum RectMut<'a> { |
9 | None(*mut AVSubtitleRect), |
10 | Bitmap(BitmapMut<'a>), |
11 | Text(TextMut<'a>), |
12 | Ass(AssMut<'a>), |
13 | } |
14 | |
15 | impl<'a> RectMut<'a> { |
16 | pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self { |
17 | match Type::from((*ptr).type_) { |
18 | Type::None => RectMut::None(ptr), |
19 | Type::Bitmap => RectMut::Bitmap(BitmapMut::wrap(ptr)), |
20 | Type::Text => RectMut::Text(TextMut::wrap(ptr)), |
21 | Type::Ass => RectMut::Ass(AssMut::wrap(ptr)), |
22 | } |
23 | } |
24 | |
25 | pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect { |
26 | match *self { |
27 | RectMut::None(ptr) => ptr as *const _, |
28 | RectMut::Bitmap(ref b) => b.as_ptr(), |
29 | RectMut::Text(ref t) => t.as_ptr(), |
30 | RectMut::Ass(ref a) => a.as_ptr(), |
31 | } |
32 | } |
33 | |
34 | pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect { |
35 | match *self { |
36 | RectMut::None(ptr) => ptr, |
37 | RectMut::Bitmap(ref mut b) => b.as_mut_ptr(), |
38 | RectMut::Text(ref mut t) => t.as_mut_ptr(), |
39 | RectMut::Ass(ref mut a) => a.as_mut_ptr(), |
40 | } |
41 | } |
42 | } |
43 | |
44 | impl<'a> RectMut<'a> { |
45 | pub fn flags(&self) -> Flags { |
46 | unsafe { |
47 | Flags::from_bits_truncate(bits:match *self { |
48 | RectMut::None(ptr: *mut {unknown}) => (*ptr).flags, |
49 | RectMut::Bitmap(ref b: &BitmapMut<'_>) => (*b.as_ptr()).flags, |
50 | RectMut::Text(ref t: &TextMut<'_>) => (*t.as_ptr()).flags, |
51 | RectMut::Ass(ref a: &AssMut<'_>) => (*a.as_ptr()).flags, |
52 | }) |
53 | } |
54 | } |
55 | } |
56 | |
57 | pub struct BitmapMut<'a> { |
58 | immutable: Bitmap<'a>, |
59 | } |
60 | |
61 | impl<'a> BitmapMut<'a> { |
62 | pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self { |
63 | BitmapMut { |
64 | immutable: Bitmap::wrap(ptr as *const _), |
65 | } |
66 | } |
67 | |
68 | pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect { |
69 | self.as_ptr() as *mut _ |
70 | } |
71 | } |
72 | |
73 | impl<'a> BitmapMut<'a> { |
74 | pub fn set_x(&mut self, value: usize) { |
75 | unsafe { |
76 | (*self.as_mut_ptr()).x = value as c_int; |
77 | } |
78 | } |
79 | |
80 | pub fn set_y(&mut self, value: usize) { |
81 | unsafe { |
82 | (*self.as_mut_ptr()).y = value as c_int; |
83 | } |
84 | } |
85 | |
86 | pub fn set_width(&mut self, value: u32) { |
87 | unsafe { |
88 | (*self.as_mut_ptr()).w = value as c_int; |
89 | } |
90 | } |
91 | |
92 | pub fn set_height(&mut self, value: u32) { |
93 | unsafe { |
94 | (*self.as_mut_ptr()).h = value as c_int; |
95 | } |
96 | } |
97 | |
98 | pub fn set_colors(&mut self, value: usize) { |
99 | unsafe { |
100 | (*self.as_mut_ptr()).nb_colors = value as c_int; |
101 | } |
102 | } |
103 | } |
104 | |
105 | impl<'a> Deref for BitmapMut<'a> { |
106 | type Target = Bitmap<'a>; |
107 | |
108 | fn deref(&self) -> &Self::Target { |
109 | &self.immutable |
110 | } |
111 | } |
112 | |
113 | pub struct TextMut<'a> { |
114 | immutable: Text<'a>, |
115 | } |
116 | |
117 | impl<'a> TextMut<'a> { |
118 | pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self { |
119 | TextMut { |
120 | immutable: Text::wrap(ptr as *const _), |
121 | } |
122 | } |
123 | |
124 | pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect { |
125 | self.as_ptr() as *mut _ |
126 | } |
127 | } |
128 | |
129 | impl<'a> TextMut<'a> { |
130 | pub fn set(&mut self, value: &str) { |
131 | let value: CString = CString::new(value).unwrap(); |
132 | |
133 | unsafe { |
134 | (*self.as_mut_ptr()).text = av_strdup(value.as_ptr()); |
135 | } |
136 | } |
137 | } |
138 | |
139 | impl<'a> Deref for TextMut<'a> { |
140 | type Target = Text<'a>; |
141 | |
142 | fn deref(&self) -> &Self::Target { |
143 | &self.immutable |
144 | } |
145 | } |
146 | |
147 | pub struct AssMut<'a> { |
148 | immutable: Ass<'a>, |
149 | } |
150 | |
151 | impl<'a> AssMut<'a> { |
152 | pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self { |
153 | AssMut { |
154 | immutable: Ass::wrap(ptr), |
155 | } |
156 | } |
157 | |
158 | pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect { |
159 | self.as_ptr() as *mut _ |
160 | } |
161 | } |
162 | |
163 | impl<'a> AssMut<'a> { |
164 | pub fn set(&mut self, value: &str) { |
165 | let value: CString = CString::new(value).unwrap(); |
166 | |
167 | unsafe { |
168 | (*self.as_mut_ptr()).ass = av_strdup(value.as_ptr()); |
169 | } |
170 | } |
171 | } |
172 | |
173 | impl<'a> Deref for AssMut<'a> { |
174 | type Target = Ass<'a>; |
175 | |
176 | fn deref(&self) -> &Self::Target { |
177 | &self.immutable |
178 | } |
179 | } |
180 | |