Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +10 -8
rust_highlight/src/lib.rs
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
|
|
| 1 |
// rust_highlight/src/lib.rs
|
| 2 |
use pyo3::prelude::*;
|
|
|
|
| 3 |
use opencv::core::{Mat, Point, Scalar, CV_8UC3};
|
| 4 |
use opencv::imgproc::{circle, get_text_size, line, put_text, HersheyFonts, LineTypes};
|
|
|
|
| 5 |
use std::process::{Command, Stdio};
|
| 6 |
use std::io::Write;
|
| 7 |
use std::time::Instant;
|
|
@@ -47,7 +50,7 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
|
|
| 47 |
|
| 48 |
// Wrap text
|
| 49 |
let text_area_width = width - 2 * margin_x;
|
| 50 |
-
let (wrapped_lines, line_styles) = wrap_text_cv(&text, font, default_font_scale, default_thickness, text_area_width);
|
| 51 |
|
| 52 |
let full_text = wrapped_lines.join("\n");
|
| 53 |
if full_text.is_empty() {
|
|
@@ -139,7 +142,6 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
|
|
| 139 |
anim_offset,
|
| 140 |
width as i32,
|
| 141 |
height as i32,
|
| 142 |
-
&wrapped_lines,
|
| 143 |
&line_styles,
|
| 144 |
&y_positions,
|
| 145 |
margin_x,
|
|
@@ -213,17 +215,17 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
|
|
| 213 |
Ok(Some(final_video_path))
|
| 214 |
}
|
| 215 |
|
| 216 |
-
fn wrap_text_cv(text: &str, font: i32, default_font_scale: f64, default_thickness: i32, max_width: i32) -> (Vec<String>, Vec<bool>) {
|
| 217 |
let mut wrapped_lines: Vec<String> = Vec::new();
|
| 218 |
let mut styles: Vec<bool> = Vec::new();
|
| 219 |
for para in text.lines() {
|
| 220 |
let trimmed = para.trim_start();
|
| 221 |
-
let
|
| 222 |
let mut para_str = para.to_string();
|
| 223 |
if is_header {
|
| 224 |
para_str = trimmed[3..].trim().to_string();
|
| 225 |
}
|
| 226 |
-
let font_scale = if is_header { header_font_scale } else { default_font_scale };
|
| 227 |
let thickness = if is_header { header_thickness } else { default_thickness };
|
| 228 |
if para_str.is_empty() {
|
| 229 |
wrapped_lines.push("".to_string());
|
|
@@ -242,7 +244,6 @@ fn wrap_text_cv(text: &str, font: i32, default_font_scale: f64, default_thicknes
|
|
| 242 |
if !cur.is_empty() {
|
| 243 |
wrapped_lines.push(cur);
|
| 244 |
styles.push(is_header);
|
| 245 |
-
cur = String::new();
|
| 246 |
}
|
| 247 |
let mut base_line_single = 0;
|
| 248 |
let size_single = get_text_size(w, font, font_scale, thickness, &mut base_line_single).unwrap();
|
|
@@ -338,7 +339,8 @@ fn render_frame(
|
|
| 338 |
}
|
| 339 |
|
| 340 |
#[pymodule]
|
| 341 |
-
fn rust_highlight(
|
| 342 |
m.add_function(wrap_pyfunction!(generate_video_clip, m)?)?;
|
| 343 |
Ok(())
|
| 344 |
-
}
|
|
|
|
|
|
| 1 |
+
```rust
|
| 2 |
// rust_highlight/src/lib.rs
|
| 3 |
use pyo3::prelude::*;
|
| 4 |
+
use pyo3::types::PyModule;
|
| 5 |
use opencv::core::{Mat, Point, Scalar, CV_8UC3};
|
| 6 |
use opencv::imgproc::{circle, get_text_size, line, put_text, HersheyFonts, LineTypes};
|
| 7 |
+
use opencv::prelude::*;
|
| 8 |
use std::process::{Command, Stdio};
|
| 9 |
use std::io::Write;
|
| 10 |
use std::time::Instant;
|
|
|
|
| 50 |
|
| 51 |
// Wrap text
|
| 52 |
let text_area_width = width - 2 * margin_x;
|
| 53 |
+
let (wrapped_lines, line_styles) = wrap_text_cv(&text, font, default_font_scale, default_thickness, text_area_width, header_font_scale, header_thickness);
|
| 54 |
|
| 55 |
let full_text = wrapped_lines.join("\n");
|
| 56 |
if full_text.is_empty() {
|
|
|
|
| 142 |
anim_offset,
|
| 143 |
width as i32,
|
| 144 |
height as i32,
|
|
|
|
| 145 |
&line_styles,
|
| 146 |
&y_positions,
|
| 147 |
margin_x,
|
|
|
|
| 215 |
Ok(Some(final_video_path))
|
| 216 |
}
|
| 217 |
|
| 218 |
+
fn wrap_text_cv(text: &str, font: i32, default_font_scale: f64, default_thickness: i32, max_width: i32, header_font_scale: f64, header_thickness: i32) -> (Vec<String>, Vec<bool>) {
|
| 219 |
let mut wrapped_lines: Vec<String> = Vec::new();
|
| 220 |
let mut styles: Vec<bool> = Vec::new();
|
| 221 |
for para in text.lines() {
|
| 222 |
let trimmed = para.trim_start();
|
| 223 |
+
let is_header = trimmed.starts_with("###");
|
| 224 |
let mut para_str = para.to_string();
|
| 225 |
if is_header {
|
| 226 |
para_str = trimmed[3..].trim().to_string();
|
| 227 |
}
|
| 228 |
+
let font_scale = if is_header { header_font_scale } else { default_font_scale };
|
| 229 |
let thickness = if is_header { header_thickness } else { default_thickness };
|
| 230 |
if para_str.is_empty() {
|
| 231 |
wrapped_lines.push("".to_string());
|
|
|
|
| 244 |
if !cur.is_empty() {
|
| 245 |
wrapped_lines.push(cur);
|
| 246 |
styles.push(is_header);
|
|
|
|
| 247 |
}
|
| 248 |
let mut base_line_single = 0;
|
| 249 |
let size_single = get_text_size(w, font, font_scale, thickness, &mut base_line_single).unwrap();
|
|
|
|
| 339 |
}
|
| 340 |
|
| 341 |
#[pymodule]
|
| 342 |
+
fn rust_highlight(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
| 343 |
m.add_function(wrap_pyfunction!(generate_video_clip, m)?)?;
|
| 344 |
Ok(())
|
| 345 |
+
}
|
| 346 |
+
```
|