1313 Comment ,
1414 CommentPosition ,
1515 OptionComment ,
16+ Rows ,
1617 ass_escape ,
1718 convert_color ,
1819 convert_flash_rotation ,
1920 convert_timestamp ,
2021 get_zoom_factor ,
2122 read_comments_from_protobuf ,
2223 read_comments_from_xml ,
24+ test_free_rows ,
2325)
2426
2527if TYPE_CHECKING :
2628 from collections .abc import Callable
2729
2830
2931T = TypeVar ("T" )
30- Rows = list [list [OptionComment ]]
3132
3233
3334def read_comments_bilibili_xml (text : str | bytes , fontsize : float ) -> list [Comment ]:
@@ -245,7 +246,7 @@ def process_comments(
245246 styleid = f"biliass_{ random .randint (0 , 0xFFFF ):04x} "
246247 ass = AssText ()
247248 ass .write_head (width , height , fontface , fontsize , alpha , styleid )
248- rows = [[ OptionComment . none ()] * ( height - bottom_reserved + 1 ) for i in range ( 4 )]
249+ rows = Rows ( 4 , height - bottom_reserved + 1 )
249250 for idx , comment in enumerate (comments ):
250251 if progress_callback and idx % 1000 == 0 :
251252 progress_callback (idx , len (comments ))
@@ -315,58 +316,13 @@ def process_comments(
315316 return ass .to_string ()
316317
317318
318- def test_free_rows (
319- rows : Rows ,
320- comment : Comment ,
321- row : int ,
322- width : int ,
323- height : int ,
324- bottom_reserved : int ,
325- duration_marquee : float ,
326- duration_still : float ,
327- ) -> int :
328- res = 0
329- rowmax = height - bottom_reserved
330- target_row = OptionComment .none ()
331- comment_pos_id = comment .pos .id
332- if comment .pos in (CommentPosition .Bottom , CommentPosition .Top ):
333- while row < rowmax and res < comment .height :
334- if target_row != rows [comment_pos_id ][row ]:
335- target_row = rows [comment_pos_id ][row ]
336- if target_row .is_some () and target_row .unwrap ().timeline + duration_still > comment .timeline :
337- break
338- row += 1
339- res += 1
340- else :
341- try :
342- threshold_time = comment .timeline - duration_marquee * (1 - width / (comment .width + width ))
343- except ZeroDivisionError :
344- threshold_time = comment .timeline - duration_marquee
345- while row < rowmax and res < comment .height :
346- if target_row != rows [comment_pos_id ][row ]:
347- target_row = rows [comment_pos_id ][row ]
348- try :
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 )
353- > comment .timeline
354- ):
355- break
356- except ZeroDivisionError :
357- pass
358- row += 1
359- res += 1
360- return res
361-
362-
363319def find_alternative_row (rows : Rows , comment : Comment , height , bottom_reserved ):
364320 res = 0
365321 comment_pos_id = comment .pos .id
366322 for row in range (height - bottom_reserved - math .ceil (comment .height )):
367- if rows [ comment_pos_id ][ row ] .is_none ():
323+ if rows . get ( comment_pos_id , row ) .is_none ():
368324 return row
369- elif rows [ comment_pos_id ][ row ] .unwrap ().timeline < rows [ comment_pos_id ][ res ] .unwrap ().timeline :
325+ elif rows . get ( comment_pos_id , row ) .unwrap ().timeline < rows . get ( comment_pos_id , res ) .unwrap ().timeline :
370326 res = row
371327 return res
372328
@@ -375,7 +331,7 @@ def mark_comment_row(rows: Rows, comment: Comment, row: int):
375331 comment_pos_id = comment .pos .id
376332 try :
377333 for i in range (row , row + math .ceil (comment .height )):
378- rows [ comment_pos_id ][ i ] = OptionComment .from_comment (comment )
334+ rows . set ( comment_pos_id , i , OptionComment .from_comment (comment ) )
379335 except IndexError :
380336 pass
381337
0 commit comments