Skip to content

Commit 690a34c

Browse files
authored
♻️ refactor: rustify ass writer write_normal_comment (#326)
1 parent 9e7c27a commit 690a34c

File tree

7 files changed

+141
-65
lines changed

7 files changed

+141
-65
lines changed

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
3737
m.add_function(wrap_pyfunction!(python::py_mark_comment_row, m)?)?;
3838
m.add_function(wrap_pyfunction!(python::py_write_head, m)?)?;
3939
m.add_function(wrap_pyfunction!(python::py_write_comment, m)?)?;
40+
m.add_function(wrap_pyfunction!(python::py_write_normal_comment, m)?)?;
4041
Ok(())
4142
}

packages/biliass/rust/src/python/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
99
pub use writer::{
1010
py_ass_escape, py_convert_color, py_convert_flash_rotation, py_convert_timestamp,
1111
py_find_alternative_row, py_get_zoom_factor, py_mark_comment_row, py_test_free_rows,
12-
py_write_comment, py_write_head, PyRows,
12+
py_write_comment, py_write_head, py_write_normal_comment, PyRows,
1313
};

packages/biliass/rust/src/python/writer.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::python;
2-
use crate::writer::ass::write_comment;
32
use crate::writer::{self, rows};
43

54
use pyo3::prelude::*;
@@ -101,8 +100,8 @@ pub fn py_test_free_rows(
101100
pub fn py_find_alternative_row(
102101
rows: &python::writer::PyRows,
103102
comment: &crate::python::PyComment,
104-
height: usize,
105-
bottom_reserved: usize,
103+
height: u32,
104+
bottom_reserved: u32,
106105
) -> PyResult<usize> {
107106
Ok(writer::rows::find_alternative_row(
108107
&rows.inner,
@@ -145,11 +144,11 @@ pub fn py_write_comment(
145144
height: u32,
146145
bottom_reserved: u32,
147146
fontsize: f32,
148-
duration_marquee: f32,
149-
duration_still: f32,
147+
duration_marquee: f64,
148+
duration_still: f64,
150149
styleid: &str,
151150
) -> PyResult<String> {
152-
Ok(write_comment(
151+
Ok(writer::ass::write_comment(
153152
&comment.inner,
154153
row,
155154
width,
@@ -161,3 +160,43 @@ pub fn py_write_comment(
161160
styleid,
162161
))
163162
}
163+
164+
#[allow(clippy::too_many_arguments)]
165+
#[pyfunction(name = "write_normal_comment")]
166+
pub fn py_write_normal_comment(
167+
rows: &mut python::writer::PyRows,
168+
comment: &crate::python::PyComment,
169+
width: u32,
170+
height: u32,
171+
bottom_reserved: u32,
172+
fontsize: f32,
173+
duration_marquee: f64,
174+
duration_still: f64,
175+
styleid: &str,
176+
reduced: bool,
177+
) -> PyResult<String> {
178+
Ok(writer::ass::write_normal_comment(
179+
&mut rows.inner,
180+
&comment.inner,
181+
width,
182+
height,
183+
bottom_reserved,
184+
fontsize,
185+
duration_marquee,
186+
duration_still,
187+
styleid,
188+
reduced,
189+
))
190+
}
191+
// pub fn write_normal_comment(
192+
// rows: &mut rows::Rows,
193+
// comment: &Comment,
194+
// width: u32,
195+
// height: u32,
196+
// bottom_reserved: u32,
197+
// fontsize: f32,
198+
// duration_marquee: f64,
199+
// duration_still: f64,
200+
// styleid: &str,
201+
// reduced: bool,
202+
// ) -> String {

packages/biliass/rust/src/writer/ass.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::comment::{Comment, CommentPosition};
2+
use crate::writer::rows;
23
use crate::writer::utils;
34

45
pub fn write_head(
@@ -47,8 +48,8 @@ pub fn write_comment(
4748
height: u32,
4849
bottom_reserved: u32,
4950
fontsize: f32,
50-
duration_marquee: f32,
51-
duration_still: f32,
51+
duration_marquee: f64,
52+
duration_still: f64,
5253
styleid: &str,
5354
) -> String {
5455
let text = utils::ass_escape(&comment.comment);
@@ -91,7 +92,68 @@ pub fn write_comment(
9192
}
9293
}
9394
let start = utils::convert_timestamp(comment.timeline);
94-
let end = utils::convert_timestamp(comment.timeline + duration as f64);
95+
let end = utils::convert_timestamp(comment.timeline + duration);
9596
let styles = styles.join("");
9697
format!("Dialogue: 2,{start},{end},{styleid},,0000,0000,0000,,{{{styles}}}{text}\n")
9798
}
99+
100+
#[allow(clippy::too_many_arguments)]
101+
pub fn write_normal_comment(
102+
rows: &mut rows::Rows,
103+
comment: &Comment,
104+
width: u32,
105+
height: u32,
106+
bottom_reserved: u32,
107+
fontsize: f32,
108+
duration_marquee: f64,
109+
duration_still: f64,
110+
styleid: &str,
111+
reduced: bool,
112+
) -> String {
113+
let mut row: usize = 0;
114+
let rowmax = height - bottom_reserved - comment.height as u32;
115+
while row <= rowmax as usize {
116+
let freerows = rows::test_free_rows(
117+
rows,
118+
comment,
119+
row,
120+
width,
121+
height,
122+
bottom_reserved,
123+
duration_marquee,
124+
duration_still,
125+
);
126+
if freerows >= comment.height as usize {
127+
rows::mark_comment_row(rows, comment, row);
128+
return write_comment(
129+
comment,
130+
row,
131+
width,
132+
height,
133+
bottom_reserved,
134+
fontsize,
135+
duration_marquee,
136+
duration_still,
137+
styleid,
138+
);
139+
} else {
140+
row += if freerows == 0 { 1 } else { freerows };
141+
}
142+
}
143+
if !reduced {
144+
row = rows::find_alternative_row(rows, comment, height, bottom_reserved);
145+
rows::mark_comment_row(rows, comment, row);
146+
return write_comment(
147+
comment,
148+
row,
149+
width,
150+
height,
151+
bottom_reserved,
152+
fontsize,
153+
duration_marquee,
154+
duration_still,
155+
styleid,
156+
);
157+
}
158+
"".to_owned()
159+
}

packages/biliass/rust/src/writer/rows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ pub fn test_free_rows(
6161
pub fn find_alternative_row(
6262
rows: &Rows,
6363
comment: &Comment,
64-
height: usize,
65-
bottom_reserved: usize,
64+
height: u32,
65+
bottom_reserved: u32,
6666
) -> usize {
6767
let mut res = 0;
6868
let comment_pos_id = comment.pos.clone() as usize;
69-
for row in 0..(height - bottom_reserved - comment.height.ceil() as usize) {
69+
for row in 0..(height as usize - bottom_reserved as usize - comment.height.ceil() as usize) {
7070
match &rows[comment_pos_id][row] {
7171
None => return row,
7272
Some(comment) => {

packages/biliass/src/biliass/_core.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ def write_comment(
103103
duration_still: float,
104104
styleid: str,
105105
) -> str: ...
106+
def write_normal_comment(
107+
rows: Rows,
108+
comment: Comment,
109+
width: int,
110+
height: int,
111+
bottom_reserved: int,
112+
fontsize: float,
113+
duration_marquee: float,
114+
duration_still: float,
115+
styleid: str,
116+
reduced: bool,
117+
) -> str: ...

packages/biliass/src/biliass/biliass.py

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
convert_color,
1818
convert_flash_rotation,
1919
convert_timestamp,
20-
find_alternative_row,
2120
get_zoom_factor,
22-
mark_comment_row,
2321
read_comments_from_protobuf,
2422
read_comments_from_xml,
25-
test_free_rows,
2623
write_comment,
2724
write_head,
25+
write_normal_comment,
2826
)
2927

3028
if TYPE_CHECKING:
@@ -185,51 +183,19 @@ def write_normal_comment(
185183
duration_still: float,
186184
styleid: str,
187185
reduced: bool,
188-
):
189-
row = 0
190-
rowmax = height - bottom_reserved - comment.height
191-
while row <= rowmax:
192-
freerows = test_free_rows(
193-
rows,
194-
comment,
195-
row,
196-
width,
197-
height,
198-
bottom_reserved,
199-
duration_marquee,
200-
duration_still,
201-
)
202-
if freerows >= comment.height:
203-
mark_comment_row(rows, comment, row)
204-
self.write_comment(
205-
comment,
206-
row,
207-
width,
208-
height,
209-
bottom_reserved,
210-
fontsize,
211-
duration_marquee,
212-
duration_still,
213-
styleid,
214-
)
215-
break
216-
else:
217-
row += freerows or 1
218-
else:
219-
if not reduced:
220-
row = find_alternative_row(rows, comment, height, bottom_reserved)
221-
mark_comment_row(rows, comment, row)
222-
self.write_comment(
223-
comment,
224-
row,
225-
width,
226-
height,
227-
bottom_reserved,
228-
fontsize,
229-
duration_marquee,
230-
duration_still,
231-
styleid,
232-
)
186+
) -> None:
187+
self._text += write_normal_comment(
188+
rows,
189+
comment,
190+
width,
191+
height,
192+
bottom_reserved,
193+
fontsize,
194+
duration_marquee,
195+
duration_still,
196+
styleid,
197+
reduced,
198+
)
233199

234200
def to_string(self):
235201
return self._text
@@ -290,10 +256,6 @@ def process_comments(
290256
return ass.to_string()
291257

292258

293-
def convert_type2(row, height, bottom_reserved):
294-
return height - bottom_reserved - row
295-
296-
297259
class safe_list(list):
298260
def get(self, index, default=None):
299261
def is_empty(value):

0 commit comments

Comments
 (0)