diff --git a/_typos.toml b/_typos.toml index 8dfcf355c..c042f2f47 100644 --- a/_typos.toml +++ b/_typos.toml @@ -1,2 +1,5 @@ +[files] +extend-exclude = ["tests/test_biliass/test_corpus"] + [default.extend-identifiers] pn = "pn" # Abbr. for "page number" diff --git a/justfile b/justfile index 510d3fb5c..7e8fa4785 100644 --- a/justfile +++ b/justfile @@ -40,6 +40,7 @@ release: clean: fd \ + -u \ -E tests/test_biliass/test_corpus/ \ -e m4s \ -e mp4 \ diff --git a/packages/biliass/src/biliass/biliass.py b/packages/biliass/src/biliass/biliass.py index 18e292366..b457f5ca9 100755 --- a/packages/biliass/src/biliass/biliass.py +++ b/packages/biliass/src/biliass/biliass.py @@ -8,7 +8,7 @@ import random import re import xml.dom.minidom -from typing import TYPE_CHECKING, NamedTuple, TypeVar, Union +from typing import TYPE_CHECKING, NamedTuple, TypeVar from biliass._core import DmSegMobileReply @@ -19,32 +19,31 @@ T = TypeVar("T") -# Comment = tuple[float, float, int, str, Union[int, str], int, float, float, float] class Comment(NamedTuple): # The position when the comment is replayed - timeline: float + timeline: float # 0 # The UNIX timestamp when the comment is submitted - timestamp: float + timestamp: float # 1 # A sequence of 1, 2, 3, ..., used for sorting - no: int + no: int # 2 # The content of the comment - comment: str + comment: str # 3 # 0 for regular moving comment, # 1 for bottom centered comment, # 2 for top centered comment, # 3 for reversed moving comment - pos: int | str + pos: int | str # 4 # Font color represented in 0xRRGGBB, # e.g. 0xffffff for white - color: int + color: int # 5 # Font size - size: float + size: float # 6 # The estimated height in pixels # i.e. (comment.count('\n')+1)*size - height: float + height: float # 7 # The estimated width in pixels # i.e. calculate_length(comment)*size - width: float + width: float # 8 def read_comments_bilibili_xml(text: str | bytes, fontsize: float) -> Generator[Comment, None, None]: @@ -537,12 +536,21 @@ def process_comments( return ass.to_string() -def test_free_rows(rows, comment: Comment, row, width, height, bottom_reserved, duration_marquee, duration_still): +def test_free_rows( + rows: list[Comment | None], + comment: Comment, + row: int, + width, + height, + bottom_reserved, + duration_marquee, + duration_still, +): res = 0 rowmax = height - bottom_reserved target_row = None if comment.pos in (1, 2): - while row < rowmax and res < comment[7]: + while row < rowmax and res < comment.height: if target_row != rows[comment.pos][row]: target_row = rows[comment.pos][row] if target_row and target_row[0] + duration_still > comment.timeline: @@ -570,20 +578,20 @@ def test_free_rows(rows, comment: Comment, row, width, height, bottom_reserved, return res -def find_alternative_row(rows, c, height, bottom_reserved): +def find_alternative_row(rows, comment: Comment, height, bottom_reserved): res = 0 - for row in range(height - bottom_reserved - math.ceil(c[7])): - if not rows[c[4]][row]: + for row in range(height - bottom_reserved - math.ceil(comment.height)): + if not rows[comment.pos][row]: return row - elif rows[c[4]][row][0] < rows[c[4]][res][0]: + elif rows[comment.pos][row][0] < rows[comment.pos][res][0]: res = row return res -def mark_comment_row(rows, c, row): +def mark_comment_row(rows: list[Comment | None], comment: Comment, row: int): try: - for i in range(row, row + math.ceil(c[7])): - rows[c[4]][i] = c + for i in range(row, row + math.ceil(comment.height)): + rows[comment.pos][i] = comment except IndexError: pass diff --git a/tests/test_biliass/test_corpus b/tests/test_biliass/test_corpus index 9afc2ace5..2f036bef4 160000 --- a/tests/test_biliass/test_corpus +++ b/tests/test_biliass/test_corpus @@ -1 +1 @@ -Subproject commit 9afc2ace5fa82a0ffbc837c7297e43195fbfbcd0 +Subproject commit 2f036bef40442b441ae724b8ef213ed53d1db25c