Skip to content

Commit f6a523f

Browse files
authored
♻️ refactor: rustify write_special_comment (#331)
1 parent aedf272 commit f6a523f

File tree

7 files changed

+88
-73
lines changed

7 files changed

+88
-73
lines changed

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
2727
m.add_function(wrap_pyfunction!(python::py_read_comments_from_xml, m)?)?;
2828
m.add_function(wrap_pyfunction!(python::py_read_comments_from_protobuf, m)?)?;
2929
m.add_function(wrap_pyfunction!(python::py_parse_special_comment, m)?)?;
30-
m.add_function(wrap_pyfunction!(python::py_get_zoom_factor, m)?)?;
3130
m.add_function(wrap_pyfunction!(python::py_write_head, m)?)?;
3231
m.add_function(wrap_pyfunction!(python::py_write_normal_comment, m)?)?;
3332
m.add_function(wrap_pyfunction!(
3433
python::py_write_comment_with_animation,
3534
m
3635
)?)?;
36+
m.add_function(wrap_pyfunction!(python::py_write_special_comment, m)?)?;
3737
Ok(())
3838
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ pub use reader::{
99
py_parse_special_comment, py_read_comments_from_protobuf, py_read_comments_from_xml,
1010
};
1111
pub use writer::{
12-
py_get_zoom_factor, py_write_comment_with_animation, py_write_head, py_write_normal_comment,
13-
PyRows,
12+
py_write_comment_with_animation, py_write_head, py_write_normal_comment,
13+
py_write_special_comment, PyRows,
1414
};

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ use crate::writer::{self, rows};
33

44
use pyo3::prelude::*;
55

6-
#[pyfunction(name = "get_zoom_factor")]
7-
pub fn py_get_zoom_factor(
8-
source_size: (u32, u32),
9-
target_size: (u32, u32),
10-
) -> PyResult<(f32, f32, f32)> {
11-
Ok(writer::utils::get_zoom_factor(source_size, target_size))
12-
}
13-
146
#[pyclass(name = "Rows")]
157
pub struct PyRows {
168
pub inner: rows::Rows,
@@ -80,18 +72,18 @@ pub fn py_write_comment_with_animation(
8072
comment: &crate::python::PyComment,
8173
width: u32,
8274
height: u32,
83-
rotate_y: f64,
84-
rotate_z: f64,
75+
rotate_y: i64,
76+
rotate_z: i64,
8577
from_x: f64,
8678
from_y: f64,
8779
to_x: f64,
8880
to_y: f64,
8981
from_alpha: u8,
9082
to_alpha: u8,
9183
text: &str,
92-
delay: f64,
84+
delay: i64,
9385
lifetime: f64,
94-
duration: f64,
86+
duration: i64,
9587
fontface: &str,
9688
is_border: bool,
9789
styleid: &str,
@@ -119,3 +111,18 @@ pub fn py_write_comment_with_animation(
119111
zoom_factor,
120112
))
121113
}
114+
115+
#[pyfunction(name = "write_special_comment")]
116+
pub fn py_write_special_comment(
117+
comment: &crate::python::PyComment,
118+
width: u32,
119+
height: u32,
120+
styleid: &str,
121+
) -> PyResult<String> {
122+
Ok(writer::ass::write_special_comment(
123+
&comment.inner,
124+
width,
125+
height,
126+
styleid,
127+
))
128+
}

packages/biliass/rust/src/reader/special.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::error::{BiliassError, DecodeError, ParseError};
22
use crate::reader::utils;
33

4-
// const BILI_PLAYER_SIZE: (u32, u32) = (512, 384); // Bilibili player version 2010
5-
// const BILI_PLAYER_SIZE: (u32, u32) = (540, 384); // Bilibili player version 2012
6-
// const BILI_PLAYER_SIZE: (u32, u32) = (672, 438); // Bilibili player version 2014
7-
const BILI_PLAYER_SIZE: (u32, u32) = (891, 589); // Bilibili player version 2021 (flex)
4+
// pub const BILI_PLAYER_SIZE: (u32, u32) = (512, 384); // Bilibili player version 2010
5+
// pub const BILI_PLAYER_SIZE: (u32, u32) = (540, 384); // Bilibili player version 2012
6+
// pub const BILI_PLAYER_SIZE: (u32, u32) = (672, 438); // Bilibili player version 2014
7+
pub const BILI_PLAYER_SIZE: (u32, u32) = (891, 589); // Bilibili player version 2021 (flex)
88

99
fn get_position(input_pos: f64, is_height: bool, zoom_factor: (f32, f32, f32)) -> f64 {
1010
let (zoom, size) = if is_height {

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

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

@@ -163,33 +164,39 @@ pub fn write_comment_with_animation(
163164
comment: &Comment,
164165
width: u32,
165166
height: u32,
166-
rotate_y: f64,
167-
rotate_z: f64,
167+
rotate_y: i64,
168+
rotate_z: i64,
168169
from_x: f64,
169170
from_y: f64,
170171
to_x: f64,
171172
to_y: f64,
172173
from_alpha: u8,
173174
to_alpha: u8,
174175
text: &str,
175-
delay: f64,
176+
delay: i64,
176177
lifetime: f64,
177-
duration: f64,
178+
duration: i64,
178179
fontface: &str,
179180
is_border: bool,
180181
styleid: &str,
181182
zoom_factor: (f32, f32, f32),
182183
) -> String {
183184
let from_rotarg = utils::convert_flash_rotation(
184-
rotate_y,
185-
rotate_z,
185+
rotate_y as f64,
186+
rotate_z as f64,
186187
from_x,
187188
from_y,
188189
width as f64,
189190
height as f64,
190191
);
191-
let to_rotarg =
192-
utils::convert_flash_rotation(rotate_y, rotate_z, to_x, to_y, width as f64, height as f64);
192+
let to_rotarg = utils::convert_flash_rotation(
193+
rotate_y as f64,
194+
rotate_z as f64,
195+
to_x,
196+
to_y,
197+
width as f64,
198+
height as f64,
199+
);
193200
if vec![
194201
from_rotarg.0,
195202
from_rotarg.1,
@@ -279,3 +286,45 @@ pub fn write_comment_with_animation(
279286
let text = utils::ass_escape(text);
280287
format!("Dialogue: -1,{start},{end},{styleid},,0,0,0,,{{{styles}}}{text}\n")
281288
}
289+
290+
pub fn write_special_comment(comment: &Comment, width: u32, height: u32, styleid: &str) -> String {
291+
let zoom_factor =
292+
utils::get_zoom_factor(crate::reader::special::BILI_PLAYER_SIZE, (width, height));
293+
let parsed_res = parse_special_comment(&comment.comment, zoom_factor);
294+
if parsed_res.is_err() {
295+
// eprintln!("Invalid comment: {}", comment.comment);
296+
return "".to_owned();
297+
}
298+
let (
299+
(rotate_y, rotate_z, from_x, from_y, to_x, to_y),
300+
from_alpha,
301+
to_alpha,
302+
text,
303+
delay,
304+
lifetime,
305+
duration,
306+
fontface,
307+
is_border,
308+
) = parsed_res.unwrap();
309+
write_comment_with_animation(
310+
comment,
311+
width,
312+
height,
313+
rotate_y,
314+
rotate_z,
315+
from_x,
316+
from_y,
317+
to_x,
318+
to_y,
319+
from_alpha,
320+
to_alpha,
321+
&text,
322+
delay,
323+
lifetime,
324+
duration,
325+
&fontface,
326+
is_border,
327+
styleid,
328+
zoom_factor,
329+
)
330+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Comment:
5757

5858
def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
5959
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
60-
def get_zoom_factor(source_size: tuple[int, int], target_size: tuple[int, int]) -> tuple[float, float, float]: ...
6160

6261
class Rows:
6362
def __init__(self, num_types: int, capacity: int) -> None: ...
@@ -99,3 +98,4 @@ def write_comment_with_animation(
9998
def parse_special_comment(
10099
content: str, zoom_factor: tuple[float, float, float]
101100
) -> tuple[tuple[int, int, float, float, float, float], int, int, str, int, float, int, str, bool]: ...
101+
def write_special_comment(comment: Comment, width: int, height: int, styleid: str) -> str: ...

packages/biliass/src/biliass/biliass.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
Comment,
1212
CommentPosition,
1313
Rows,
14-
get_zoom_factor,
15-
parse_special_comment,
1614
read_comments_from_protobuf,
1715
read_comments_from_xml,
1816
write_comment_with_animation,
1917
write_head,
2018
write_normal_comment,
19+
write_special_comment,
2120
)
2221

2322
if TYPE_CHECKING:
@@ -38,52 +37,12 @@ def read_comments_bilibili_protobuf(protobuf: bytes | str, fontsize: float) -> l
3837
return read_comments_from_protobuf(protobuf, fontsize)
3938

4039

41-
BILI_PLACYER_SIZE = (891, 589)
42-
43-
4440
class AssText:
4541
def __init__(self):
4642
self._text = ""
4743

48-
def write_comment_special(self, comment: Comment, width, height, styleid):
49-
zoom_factor = get_zoom_factor(BILI_PLACYER_SIZE, (width, height))
50-
try:
51-
(
52-
(rotate_y, rotate_z, from_x, from_y, to_x, to_y),
53-
from_alpha,
54-
to_alpha,
55-
text,
56-
delay,
57-
lifetime,
58-
duration,
59-
fontface,
60-
is_border,
61-
) = parse_special_comment(comment.comment, zoom_factor)
62-
63-
self.write_comment_with_animation(
64-
comment,
65-
width,
66-
height,
67-
rotate_y,
68-
rotate_z,
69-
from_x,
70-
from_y,
71-
to_x,
72-
to_y,
73-
from_alpha,
74-
to_alpha,
75-
text,
76-
delay,
77-
lifetime,
78-
duration,
79-
fontface,
80-
is_border,
81-
styleid,
82-
zoom_factor,
83-
)
84-
85-
except ValueError:
86-
logging.warning(f"Invalid comment: {comment.comment!r}")
44+
def write_special_comment(self, comment: Comment, width, height, styleid) -> None:
45+
self._text += write_special_comment(comment, width, height, styleid)
8746

8847
def write_head(self, width: int, height: int, fontface: str, fontsize: float, alpha: float, styleid: str) -> None:
8948
self._text += write_head(width, height, fontface, fontsize, alpha, styleid)
@@ -209,7 +168,7 @@ def process_comments(
209168
reduced,
210169
)
211170
elif comment.pos == CommentPosition.Special:
212-
ass.write_comment_special(comment, width, height, styleid)
171+
ass.write_special_comment(comment, width, height, styleid)
213172
else:
214173
logging.warning(f"Invalid comment: {comment.comment!r}")
215174
if progress_callback:

0 commit comments

Comments
 (0)