77import math
88import random
99import re
10- from typing import TYPE_CHECKING , TypeVar , Union
10+ from typing import TYPE_CHECKING , TypeVar
1111
1212from biliass ._core import (
1313 Comment ,
1414 CommentPosition ,
15+ OptionComment ,
1516 ass_escape ,
1617 convert_color ,
1718 convert_flash_rotation ,
2627
2728
2829T = TypeVar ("T" )
29- Rows = list [list [Union [ Comment , None ] ]]
30+ Rows = list [list [OptionComment ]]
3031
3132
3233def read_comments_bilibili_xml (text : str | bytes , fontsize : float ) -> list [Comment ]:
@@ -228,23 +229,23 @@ def to_string(self):
228229
229230
230231def process_comments (
231- comments ,
232- width ,
233- height ,
234- bottom_reserved ,
232+ comments : list [ Comment ] ,
233+ width : int ,
234+ height : int ,
235+ bottom_reserved : int ,
235236 fontface ,
236237 fontsize ,
237238 alpha ,
238- duration_marquee ,
239- duration_still ,
239+ duration_marquee : float ,
240+ duration_still : float ,
240241 filters_regex ,
241242 reduced ,
242243 progress_callback ,
243244):
244245 styleid = f"biliass_{ random .randint (0 , 0xFFFF ):04x} "
245246 ass = AssText ()
246247 ass .write_head (width , height , fontface , fontsize , alpha , styleid )
247- rows = [[None ] * (height - bottom_reserved + 1 ) for i in range (4 )]
248+ rows = [[OptionComment . none () ] * (height - bottom_reserved + 1 ) for i in range (4 )]
248249 for idx , comment in enumerate (comments ):
249250 if progress_callback and idx % 1000 == 0 :
250251 progress_callback (idx , len (comments ))
@@ -318,21 +319,21 @@ def test_free_rows(
318319 rows : Rows ,
319320 comment : Comment ,
320321 row : int ,
321- width ,
322- height ,
323- bottom_reserved ,
324- duration_marquee ,
325- duration_still ,
326- ):
322+ width : int ,
323+ height : int ,
324+ bottom_reserved : int ,
325+ duration_marquee : float ,
326+ duration_still : float ,
327+ ) -> int :
327328 res = 0
328329 rowmax = height - bottom_reserved
329- target_row = None
330+ target_row = OptionComment . none ()
330331 comment_pos_id = comment .pos .id
331332 if comment .pos in (CommentPosition .Bottom , CommentPosition .Top ):
332333 while row < rowmax and res < comment .height :
333334 if target_row != rows [comment_pos_id ][row ]:
334335 target_row = rows [comment_pos_id ][row ]
335- if target_row and target_row .timeline + duration_still > comment .timeline :
336+ if target_row . is_some () and target_row . unwrap () .timeline + duration_still > comment .timeline :
336337 break
337338 row += 1
338339 res += 1
@@ -345,9 +346,10 @@ def test_free_rows(
345346 if target_row != rows [comment_pos_id ][row ]:
346347 target_row = rows [comment_pos_id ][row ]
347348 try :
348- if target_row and (
349- target_row .timeline > threshold_time
350- or target_row .timeline + target_row .width * duration_marquee / (target_row .width + width )
349+ if target_row .is_some () and (
350+ target_row .unwrap ().timeline > threshold_time
351+ or target_row .unwrap ().timeline
352+ + target_row .unwrap ().width * duration_marquee / (target_row .unwrap ().width + width )
351353 > comment .timeline
352354 ):
353355 break
@@ -362,9 +364,9 @@ def find_alternative_row(rows: Rows, comment: Comment, height, bottom_reserved):
362364 res = 0
363365 comment_pos_id = comment .pos .id
364366 for row in range (height - bottom_reserved - math .ceil (comment .height )):
365- if not rows [comment_pos_id ][row ]:
367+ if rows [comment_pos_id ][row ]. is_none () :
366368 return row
367- elif rows [comment_pos_id ][row ].timeline < rows [comment_pos_id ][res ].timeline :
369+ elif rows [comment_pos_id ][row ].unwrap (). timeline < rows [comment_pos_id ][res ]. unwrap () .timeline :
368370 res = row
369371 return res
370372
@@ -373,7 +375,7 @@ def mark_comment_row(rows: Rows, comment: Comment, row: int):
373375 comment_pos_id = comment .pos .id
374376 try :
375377 for i in range (row , row + math .ceil (comment .height )):
376- rows [comment_pos_id ][i ] = comment
378+ rows [comment_pos_id ][i ] = OptionComment . from_comment ( comment )
377379 except IndexError :
378380 pass
379381
@@ -402,7 +404,7 @@ def Danmaku2ASS(
402404 stage_width : int ,
403405 stage_height : int ,
404406 input_format : str = "xml" ,
405- reserve_blank : float = 0 ,
407+ reserve_blank : int = 0 ,
406408 font_face : str = "sans-serif" ,
407409 font_size : float = 25.0 ,
408410 text_opacity : float = 1.0 ,
0 commit comments