Skip to content

Commit 0d16099

Browse files
authored
♻️ refactor: rustify ass writer write_comment (#325)
1 parent e4ae75c commit 0d16099

File tree

6 files changed

+119
-43
lines changed

6 files changed

+119
-43
lines changed

packages/biliass/rust/src/lib.rs

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

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_head, PyRows,
12+
py_write_comment, py_write_head, PyRows,
1313
};

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

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

45
use pyo3::prelude::*;
@@ -123,8 +124,8 @@ pub fn py_mark_comment_row(
123124

124125
#[pyfunction(name = "write_head")]
125126
pub fn py_write_head(
126-
width: i32,
127-
height: i32,
127+
width: u32,
128+
height: u32,
128129
fontface: &str,
129130
fontsize: f32,
130131
alpha: f32,
@@ -134,3 +135,29 @@ pub fn py_write_head(
134135
width, height, fontface, fontsize, alpha, styleid,
135136
))
136137
}
138+
139+
#[allow(clippy::too_many_arguments)]
140+
#[pyfunction(name = "write_comment")]
141+
pub fn py_write_comment(
142+
comment: &crate::python::PyComment,
143+
row: usize,
144+
width: u32,
145+
height: u32,
146+
bottom_reserved: u32,
147+
fontsize: f32,
148+
duration_marquee: f32,
149+
duration_still: f32,
150+
styleid: &str,
151+
) -> PyResult<String> {
152+
Ok(write_comment(
153+
&comment.inner,
154+
row,
155+
width,
156+
height,
157+
bottom_reserved,
158+
fontsize,
159+
duration_marquee,
160+
duration_still,
161+
styleid,
162+
))
163+
}

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use crate::comment::{Comment, CommentPosition};
2+
use crate::writer::utils;
3+
14
pub fn write_head(
2-
width: i32,
3-
height: i32,
5+
width: u32,
6+
height: u32,
47
fontface: &str,
58
fontsize: f32,
69
alpha: f32,
@@ -31,3 +34,64 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
3134
"
3235
)
3336
}
37+
38+
fn convert_type2(row: usize, height: u32, bottom_reserved: u32) -> usize {
39+
height as usize - bottom_reserved as usize - row
40+
}
41+
42+
#[allow(clippy::too_many_arguments)]
43+
pub fn write_comment(
44+
comment: &Comment,
45+
row: usize,
46+
width: u32,
47+
height: u32,
48+
bottom_reserved: u32,
49+
fontsize: f32,
50+
duration_marquee: f32,
51+
duration_still: f32,
52+
styleid: &str,
53+
) -> String {
54+
let text = utils::ass_escape(&comment.comment);
55+
let (style, duration) = match comment.pos {
56+
CommentPosition::Bottom => {
57+
let halfwidth = width / 2;
58+
(format!("\\an8\\pos({halfwidth}, {row})"), duration_still)
59+
}
60+
CommentPosition::Top => {
61+
let halfwidth = width / 2;
62+
let row = convert_type2(row, height, bottom_reserved);
63+
(format!("\\an2\\pos({halfwidth}, {row})"), duration_still)
64+
}
65+
CommentPosition::Reversed => {
66+
let neglen = -(comment.width.ceil()) as i32;
67+
(
68+
format!("\\move({neglen}, {row}, {width}, {row})"),
69+
duration_marquee,
70+
)
71+
}
72+
_ => {
73+
let neglen = -(comment.width.ceil()) as i32;
74+
(
75+
format!("\\move({width}, {row}, {neglen}, {row})"),
76+
duration_marquee,
77+
)
78+
}
79+
};
80+
let mut styles = vec![style];
81+
if comment.size - fontsize <= -1. || comment.size - fontsize >= 1. {
82+
styles.push(format!("\\fs{:.0}", comment.size));
83+
}
84+
if comment.color != 0xFFFFFF {
85+
styles.push(format!(
86+
"\\c&H{}&",
87+
utils::convert_color(comment.color, None, None)
88+
));
89+
if comment.color == 0x000000 {
90+
styles.push("\\3c&HFFFFFF&".to_owned());
91+
}
92+
}
93+
let start = utils::convert_timestamp(comment.timeline);
94+
let end = utils::convert_timestamp(comment.timeline + duration as f64);
95+
let styles = styles.join("");
96+
format!("Dialogue: 2,{start},{end},{styleid},,0000,0000,0000,,{{{styles}}}{text}\n")
97+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ class Rows:
9292
def find_alternative_row(rows: Rows, comment: Comment, height: int, bottom_reserved: int) -> int: ...
9393
def mark_comment_row(rows: Rows, comment: Comment, row: int) -> None: ...
9494
def write_head(width: int, height: int, fontface: str, fontsize: float, alpha: float, styleid: str) -> str: ...
95+
def write_comment(
96+
comment: Comment,
97+
row: int,
98+
width: int,
99+
height: int,
100+
bottom_reserved: int,
101+
fontsize: float,
102+
duration_marquee: float,
103+
duration_still: float,
104+
styleid: str,
105+
) -> str: ...

packages/biliass/src/biliass/biliass.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
read_comments_from_protobuf,
2424
read_comments_from_xml,
2525
test_free_rows,
26+
write_comment,
2627
write_head,
2728
)
2829

@@ -160,44 +161,16 @@ def write_comment(
160161
duration_still: float,
161162
styleid: str,
162163
):
163-
text = ass_escape(comment.comment)
164-
styles = []
165-
if comment.pos == CommentPosition.Bottom:
166-
styles.append("\\an8\\pos(%(halfwidth)d, %(row)d)" % {"halfwidth": width / 2, "row": row})
167-
duration = duration_still
168-
elif comment.pos == CommentPosition.Top:
169-
styles.append(
170-
"\\an2\\pos(%(halfwidth)d, %(row)d)"
171-
% {
172-
"halfwidth": width / 2,
173-
"row": convert_type2(row, height, bottom_reserved),
174-
}
175-
)
176-
duration = duration_still
177-
elif comment.pos == CommentPosition.Reversed:
178-
styles.append(
179-
"\\move(%(neglen)d, %(row)d, %(width)d, %(row)d)"
180-
% {"width": width, "row": row, "neglen": -math.ceil(comment.width)}
181-
)
182-
duration = duration_marquee
183-
else:
184-
styles.append(
185-
"\\move(%(width)d, %(row)d, %(neglen)d, %(row)d)"
186-
% {"width": width, "row": row, "neglen": -math.ceil(comment.width)}
187-
)
188-
duration = duration_marquee
189-
if not (-1 < comment.size - fontsize < 1):
190-
styles.append(f"\\fs{comment.size:.0f}")
191-
if comment.color != 0xFFFFFF:
192-
styles.append(f"\\c&H{convert_color(comment.color)}&")
193-
if comment.color == 0x000000:
194-
styles.append("\\3c&HFFFFFF&")
195-
self._text += "Dialogue: 2,{start},{end},{styleid},,0000,0000,0000,,{{{styles}}}{text}\n".format(
196-
start=convert_timestamp(comment.timeline),
197-
end=convert_timestamp(comment.timeline + duration),
198-
styles="".join(styles),
199-
text=text,
200-
styleid=styleid,
164+
self._text += write_comment(
165+
comment,
166+
row,
167+
width,
168+
height,
169+
bottom_reserved,
170+
fontsize,
171+
duration_marquee,
172+
duration_still,
173+
styleid,
201174
)
202175

203176
def to_string(self):

0 commit comments

Comments
 (0)