use crate::tex::dimensions::PT_TO_SP;
use crate::tex::{Dimension, LineBreaker};
pub struct PdfTextRenderer;
impl PdfTextRenderer {
pub fn normalize_text(text: &str) -> Option<String> {
let mut normalized = String::with_capacity(text.len());
let mut previous_was_whitespace = false;
for ch in text.chars() {
if ch.is_control() && !ch.is_whitespace() {
continue;
}
if ch.is_whitespace() {
if !previous_was_whitespace {
normalized.push(' ');
previous_was_whitespace = true;
}
} else {
normalized.push(ch);
previous_was_whitespace = false;
}
}
let trimmed = normalized.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}
pub fn char_count(text: &str) -> usize {
text.chars().count()
}
pub fn split_by_char_count(text: &str, max_chars: usize) -> Vec<String> {
if max_chars == 0 {
return vec![text.to_string()];
}
let mut chunks = Vec::new();
let mut current = String::new();
let mut current_len = 0;
for ch in text.chars() {
if current_len == max_chars {
chunks.push(current);
current = String::new();
current_len = 0;
}
current.push(ch);
current_len += 1;
}
if !current.is_empty() {
chunks.push(current);
}
chunks
}
pub fn wrap_text(text: &str, max_chars: usize) -> Vec<String> {
let mut lines = Vec::new();
let mut current_line = String::new();
let mut current_line_chars = 0;
for word in text.split_whitespace() {
let word_chars = Self::char_count(word);
if word_chars > max_chars {
if !current_line.is_empty() {
lines.push(current_line);
current_line = String::new();
current_line_chars = 0;
}
for chunk in Self::split_by_char_count(word, max_chars) {
lines.push(chunk);
}
continue;
}
if current_line_chars == 0 {
current_line = word.to_string();
current_line_chars = word_chars;
} else if current_line_chars + 1 + word_chars <= max_chars {
current_line.push(' ');
current_line.push_str(word);
current_line_chars += 1 + word_chars;
} else {
lines.push(current_line);
current_line = word.to_string();
current_line_chars = word_chars;
}
}
if !current_line.is_empty() {
lines.push(current_line);
}
lines
}
pub fn wrap_text_by_width(text: &str, line_width_pt: f32, font_size_pt: f32) -> Vec<String> {
if line_width_pt <= 0.0 || font_size_pt <= 0.0 {
return Self::wrap_text(text, 80);
}
let char_width_sp = ((font_size_pt * 0.55) * PT_TO_SP as f32).round() as i64;
let line_width_sp = (line_width_pt * PT_TO_SP as f32).round() as i64;
let breaker = LineBreaker::new(Dimension::from_sp(line_width_sp))
.with_char_width(Dimension::from_sp(char_width_sp.max(1)));
breaker.break_text(text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wrap_text_by_width() {
let text = "This is a long line that needs wrapping at a fixed width";
let lines = PdfTextRenderer::wrap_text_by_width(text, 120.0, 11.0);
assert!(lines.len() > 1);
for line in &lines {
assert!(!line.is_empty());
}
}
#[test]
fn test_normalize_text() {
let text = "Hello world \n test";
let normalized = PdfTextRenderer::normalize_text(text).unwrap();
assert_eq!(normalized, "Hello world test");
}
#[test]
fn test_char_count() {
assert_eq!(PdfTextRenderer::char_count("hello"), 5);
assert_eq!(PdfTextRenderer::char_count("日本語"), 3);
}
#[test]
fn test_wrap_text() {
let text = "This is a long line that needs wrapping";
let lines = PdfTextRenderer::wrap_text(text, 20);
assert!(lines.len() > 1);
for line in &lines {
assert!(PdfTextRenderer::char_count(line) <= 20);
}
}
#[test]
fn test_normalize_text_removes_control_chars() {
let text = "Hello\x00world\x01test";
let normalized = PdfTextRenderer::normalize_text(text).unwrap();
assert_eq!(normalized, "Helloworldtest");
}
#[test]
fn test_normalize_text_returns_none_for_empty() {
assert_eq!(PdfTextRenderer::normalize_text(" "), None);
assert_eq!(PdfTextRenderer::normalize_text("\n\n\t"), None);
}
#[test]
fn test_split_by_char_count() {
let chunks = PdfTextRenderer::split_by_char_count("HelloWorld", 3);
assert_eq!(chunks, vec!["Hel", "loW", "orl", "d"]);
}
#[test]
fn test_split_by_char_count_zero_max() {
let chunks = PdfTextRenderer::split_by_char_count("hello", 0);
assert_eq!(chunks, vec!["hello"]);
}
#[test]
fn test_wrap_text_splits_long_word() {
let text = "supercalifragilisticexpialidocious";
let lines = PdfTextRenderer::wrap_text(text, 10);
assert!(lines.len() > 1);
for line in &lines {
assert!(PdfTextRenderer::char_count(line) <= 10);
}
}
#[test]
fn test_wrap_text_empty() {
let lines = PdfTextRenderer::wrap_text("", 10);
assert!(lines.is_empty());
}
#[test]
fn test_wrap_text_single_short_word() {
let lines = PdfTextRenderer::wrap_text("hello", 10);
assert_eq!(lines, vec!["hello"]);
}
}