Skip to content

Commit 1050744

Browse files
authored
♻️ refactor: rustify writer utility get_zoom_factor (#316)
1 parent 742f62a commit 1050744

File tree

9 files changed

+253
-37
lines changed

9 files changed

+253
-37
lines changed

packages/biliass/Cargo.lock

Lines changed: 209 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/biliass/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bytes = "1.7.1"
1414
prost = "0.13.2"
1515
thiserror = "1.0.63"
1616
quick-xml = "0.36.1"
17+
cached = "0.53.1"
1718

1819
[build-dependencies]
1920
prost-build = "0.13.2"

packages/biliass/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
2828
m.add_function(wrap_pyfunction!(python::py_convert_timestamp, m)?)?;
2929
m.add_function(wrap_pyfunction!(python::py_ass_escape, m)?)?;
3030
m.add_function(wrap_pyfunction!(python::py_convert_color, m)?)?;
31+
m.add_function(wrap_pyfunction!(python::py_get_zoom_factor, m)?)?;
3132
Ok(())
3233
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ mod writer;
66
pub use comment::{PyComment, PyCommentPosition};
77
pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
88
pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
9-
pub use writer::{py_ass_escape, py_convert_color, py_convert_timestamp};
9+
pub use writer::{py_ass_escape, py_convert_color, py_convert_timestamp, py_get_zoom_factor};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ pub fn py_ass_escape(text: &str) -> PyResult<String> {
1717
pub fn py_convert_color(rgb: u32, width: u32, height: u32) -> PyResult<String> {
1818
Ok(writer::utils::convert_color(rgb, Some(width), Some(height)))
1919
}
20+
21+
#[pyfunction(name = "get_zoom_factor")]
22+
pub fn py_get_zoom_factor(
23+
source_size: (u32, u32),
24+
target_size: (u32, u32),
25+
) -> PyResult<(f32, f32, f32)> {
26+
Ok(writer::utils::get_zoom_factor(source_size, target_size))
27+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use cached::proc_macro::cached;
2+
13
fn divmod(a: f64, b: f64) -> (f64, f64) {
24
(a / b, a % b)
35
}
@@ -74,3 +76,30 @@ pub fn convert_color(rgb: u32, width: Option<u32>, height: Option<u32>) -> Strin
7476
)
7577
}
7678
}
79+
80+
#[cached]
81+
pub fn get_zoom_factor(source_size: (u32, u32), target_size: (u32, u32)) -> (f32, f32, f32) {
82+
let source_size = (source_size.0 as f32, source_size.1 as f32);
83+
let target_size = (target_size.0 as f32, target_size.1 as f32);
84+
let source_aspect = source_size.0 / source_size.1;
85+
let target_aspect = target_size.0 / target_size.1;
86+
if target_aspect < source_aspect {
87+
// narrower
88+
let scale_factor = target_size.0 / source_size.0;
89+
(
90+
scale_factor,
91+
0.0,
92+
(target_size.1 - target_size.0 / source_aspect) / 2.0,
93+
)
94+
} else if target_aspect > source_aspect {
95+
// wider
96+
let scale_factor = target_size.1 / source_size.1;
97+
(
98+
scale_factor,
99+
(target_size.0 - target_size.1 * source_aspect) / 2.0,
100+
0.0,
101+
)
102+
} else {
103+
(target_size.0 / source_size.0, 0.0, 0.0)
104+
}
105+
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
5959
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
6060
def convert_timestamp(timestamp: float) -> float: ...
6161
def ass_escape(text: str) -> str: ...
62-
def convert_color(rgb: int, width: int = ..., height: int = ...): ...
62+
def convert_color(rgb: int, width: int = ..., height: int = ...) -> str: ...
63+
def get_zoom_factor(source_size: tuple[int, int], target_size: tuple[int, int]) -> tuple[float, float, float]: ...

0 commit comments

Comments
 (0)