|
| 1 | +use crate::comment::{Comment, CommentPosition}; |
| 2 | +use crate::writer::utils; |
| 3 | + |
1 | 4 | pub fn write_head( |
2 | | - width: i32, |
3 | | - height: i32, |
| 5 | + width: u32, |
| 6 | + height: u32, |
4 | 7 | fontface: &str, |
5 | 8 | fontsize: f32, |
6 | 9 | alpha: f32, |
@@ -31,3 +34,64 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text |
31 | 34 | " |
32 | 35 | ) |
33 | 36 | } |
| 37 | + |
| 38 | +fn convert_type2(row: usize, height: u32, bottom_reserved: u32) -> usize { |
| 39 | + height as usize - bottom_reserved as usize - row |
| 40 | +} |
| 41 | + |
| 42 | +#[allow(clippy::too_many_arguments)] |
| 43 | +pub fn write_comment( |
| 44 | + comment: &Comment, |
| 45 | + row: usize, |
| 46 | + width: u32, |
| 47 | + height: u32, |
| 48 | + bottom_reserved: u32, |
| 49 | + fontsize: f32, |
| 50 | + duration_marquee: f32, |
| 51 | + duration_still: f32, |
| 52 | + styleid: &str, |
| 53 | +) -> String { |
| 54 | + let text = utils::ass_escape(&comment.comment); |
| 55 | + let (style, duration) = match comment.pos { |
| 56 | + CommentPosition::Bottom => { |
| 57 | + let halfwidth = width / 2; |
| 58 | + (format!("\\an8\\pos({halfwidth}, {row})"), duration_still) |
| 59 | + } |
| 60 | + CommentPosition::Top => { |
| 61 | + let halfwidth = width / 2; |
| 62 | + let row = convert_type2(row, height, bottom_reserved); |
| 63 | + (format!("\\an2\\pos({halfwidth}, {row})"), duration_still) |
| 64 | + } |
| 65 | + CommentPosition::Reversed => { |
| 66 | + let neglen = -(comment.width.ceil()) as i32; |
| 67 | + ( |
| 68 | + format!("\\move({neglen}, {row}, {width}, {row})"), |
| 69 | + duration_marquee, |
| 70 | + ) |
| 71 | + } |
| 72 | + _ => { |
| 73 | + let neglen = -(comment.width.ceil()) as i32; |
| 74 | + ( |
| 75 | + format!("\\move({width}, {row}, {neglen}, {row})"), |
| 76 | + duration_marquee, |
| 77 | + ) |
| 78 | + } |
| 79 | + }; |
| 80 | + let mut styles = vec![style]; |
| 81 | + if comment.size - fontsize <= -1. || comment.size - fontsize >= 1. { |
| 82 | + styles.push(format!("\\fs{:.0}", comment.size)); |
| 83 | + } |
| 84 | + if comment.color != 0xFFFFFF { |
| 85 | + styles.push(format!( |
| 86 | + "\\c&H{}&", |
| 87 | + utils::convert_color(comment.color, None, None) |
| 88 | + )); |
| 89 | + if comment.color == 0x000000 { |
| 90 | + styles.push("\\3c&HFFFFFF&".to_owned()); |
| 91 | + } |
| 92 | + } |
| 93 | + let start = utils::convert_timestamp(comment.timeline); |
| 94 | + let end = utils::convert_timestamp(comment.timeline + duration as f64); |
| 95 | + let styles = styles.join(""); |
| 96 | + format!("Dialogue: 2,{start},{end},{styleid},,0000,0000,0000,,{{{styles}}}{text}\n") |
| 97 | +} |
0 commit comments