|
| 1 | +use crate::comment::{Comment, CommentPosition}; |
| 2 | +use crate::error::BiliassError; |
| 3 | +use crate::writer; |
| 4 | +use crate::writer::rows; |
| 5 | +use regex::Regex; |
| 6 | + |
| 7 | +#[allow(clippy::too_many_arguments)] |
| 8 | +pub fn process_comments( |
| 9 | + comments: &Vec<Comment>, |
| 10 | + width: u32, |
| 11 | + height: u32, |
| 12 | + bottom_reserved: u32, |
| 13 | + fontface: &str, |
| 14 | + fontsize: f32, |
| 15 | + alpha: f32, |
| 16 | + duration_marquee: f64, |
| 17 | + duration_still: f64, |
| 18 | + filters_regex: Vec<String>, |
| 19 | + reduced: bool, |
| 20 | +) -> Result<String, BiliassError> { |
| 21 | + let styleid = "biliass"; |
| 22 | + let mut ass_result = "".to_owned(); |
| 23 | + ass_result += &writer::ass::write_head(width, height, fontface, fontsize, alpha, styleid); |
| 24 | + let mut rows = rows::init_rows(4, (height - bottom_reserved + 1) as usize); |
| 25 | + let compiled_regexes_res: Result<Vec<Regex>, regex::Error> = filters_regex |
| 26 | + .into_iter() |
| 27 | + .map(|pattern| Regex::new(&pattern)) |
| 28 | + .collect(); |
| 29 | + let compiled_regexes = compiled_regexes_res.map_err(BiliassError::from)?; |
| 30 | + for comment in comments { |
| 31 | + match comment.pos { |
| 32 | + CommentPosition::Scroll |
| 33 | + | CommentPosition::Bottom |
| 34 | + | CommentPosition::Top |
| 35 | + | CommentPosition::Reversed => { |
| 36 | + if compiled_regexes |
| 37 | + .iter() |
| 38 | + .any(|regex| regex.is_match(&comment.comment)) |
| 39 | + { |
| 40 | + continue; |
| 41 | + }; |
| 42 | + ass_result += &writer::ass::write_normal_comment( |
| 43 | + rows.as_mut(), |
| 44 | + comment, |
| 45 | + width, |
| 46 | + height, |
| 47 | + bottom_reserved, |
| 48 | + fontsize, |
| 49 | + duration_marquee, |
| 50 | + duration_still, |
| 51 | + styleid, |
| 52 | + reduced, |
| 53 | + ); |
| 54 | + } |
| 55 | + CommentPosition::Special => { |
| 56 | + ass_result += &writer::ass::write_special_comment(comment, width, height, styleid); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + Ok(ass_result) |
| 61 | +} |
| 62 | + |
| 63 | +#[allow(clippy::too_many_arguments)] |
| 64 | +pub fn convert_to_ass<Reader, Input>( |
| 65 | + inputs: Vec<Input>, |
| 66 | + reader: Reader, |
| 67 | + stage_width: u32, |
| 68 | + stage_height: u32, |
| 69 | + reserve_blank: u32, |
| 70 | + font_face: &str, |
| 71 | + font_size: f32, |
| 72 | + text_opacity: f32, |
| 73 | + duration_marquee: f64, |
| 74 | + duration_still: f64, |
| 75 | + comment_filters: Vec<String>, |
| 76 | + is_reduce_comments: bool, |
| 77 | +) -> Result<String, BiliassError> |
| 78 | +where |
| 79 | + Reader: Fn(Input, f32) -> Result<Vec<Comment>, BiliassError>, |
| 80 | +{ |
| 81 | + let comments_result: Result<Vec<Vec<Comment>>, BiliassError> = inputs |
| 82 | + .into_iter() |
| 83 | + .map(|input| reader(input, font_size)) |
| 84 | + .collect(); |
| 85 | + let comments = comments_result?; |
| 86 | + let mut comments = comments.concat(); |
| 87 | + comments.sort_by(|a, b| { |
| 88 | + ( |
| 89 | + a.timeline, |
| 90 | + a.timestamp, |
| 91 | + a.no, |
| 92 | + &a.comment, |
| 93 | + &a.pos, |
| 94 | + a.color, |
| 95 | + a.size, |
| 96 | + a.height, |
| 97 | + a.width, |
| 98 | + ) |
| 99 | + .partial_cmp(&( |
| 100 | + b.timeline, |
| 101 | + b.timestamp, |
| 102 | + b.no, |
| 103 | + &b.comment, |
| 104 | + &b.pos, |
| 105 | + b.color, |
| 106 | + a.size, |
| 107 | + a.height, |
| 108 | + a.width, |
| 109 | + )) |
| 110 | + .unwrap_or(std::cmp::Ordering::Less) |
| 111 | + }); |
| 112 | + process_comments( |
| 113 | + &comments, |
| 114 | + stage_width, |
| 115 | + stage_height, |
| 116 | + reserve_blank, |
| 117 | + font_face, |
| 118 | + font_size, |
| 119 | + text_opacity, |
| 120 | + duration_marquee, |
| 121 | + duration_still, |
| 122 | + comment_filters, |
| 123 | + is_reduce_comments, |
| 124 | + ) |
| 125 | +} |
0 commit comments