LD-Converter/src-tauri/tests/pdf_to_docx_quality_tests.rs

616 lines
24 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! LD Converter v3.6.0 Saf Rust PDF -> DOCX & Geometrik Matematik Motoru Kalite Testleri.
//!
//! 8 Mimari Katmanın tamamını doğrular:
//! - Afin Matris ve Koordinat Dönüşümleri
//! - 5 Kademeli Font Çözücü & Türkçe/TeX Eşlemeleri
//! - 2B Uzaysal Matematik Rekonstrüksiyonu (OMML <m:f>, <m:rad>, <m:nary>, <m:sSubSup>)
//! - Tablo Çözümleme ve Hücre Arkaplanları
//! - Uçtan Uca PDF -> DOCX Dönüşümü ve ZIP/OpenXML Doğrulaması
use std::fs::File;
use std::io::Read;
use zip::ZipArchive;
use app_lib::services::pdf_parser::matrix::{AffineMatrix, PageBox};
use app_lib::services::pdf_parser::tex_font_maps::*;
use app_lib::services::pdf_parser::content_stream::{PdfChar, VectorLine, FilledRect};
use app_lib::services::pdf_parser::math_reconstructor::MathReconstructor;
use app_lib::services::pdf_parser::table_reconstructor::TableReconstructor;
use app_lib::services::pdf_parser::convert_pdf_to_docx;
#[test]
fn test_affine_matrix_operations() {
let m = AffineMatrix::new(2.0, 0.0, 0.0, 3.0, 10.0, 20.0);
let (px, py) = m.transform_point(5.0, 5.0);
assert_eq!(px, 20.0); // 2*5 + 10 = 20
assert_eq!(py, 35.0); // 3*5 + 20 = 35
let inv = m.inverse().expect("Matris tersi alınabilir olmalı");
let (ix, iy) = inv.transform_point(px, py);
assert!((ix - 5.0).abs() < 1e-6);
assert!((iy - 5.0).abs() < 1e-6);
}
#[test]
fn test_page_box_top_left_normalization() {
// 600x800 pt standart A4 dikey sayfa
let page_box = PageBox::new([0.0, 0.0, 600.0, 800.0], 0);
// Sol-alt orijinli (100, 700) -> Sol-üst orijinli (100, 100) olmalı
let (tx, ty) = page_box.to_top_left_point(100.0, 700.0);
assert_eq!(tx, 100.0);
assert_eq!(ty, 100.0);
}
#[test]
fn test_turkish_agl_and_tex_cmmi_mappings() {
assert_eq!(agl_glyph_name_to_unicode("gbreve").as_deref(), Some("ğ"));
assert_eq!(agl_glyph_name_to_unicode("Gbreve").as_deref(), Some("Ğ"));
assert_eq!(agl_glyph_name_to_unicode("scedilla").as_deref(), Some("ş"));
assert_eq!(agl_glyph_name_to_unicode("Scedilla").as_deref(), Some("Ş"));
assert_eq!(agl_glyph_name_to_unicode("dotlessi").as_deref(), Some("ı"));
assert_eq!(agl_glyph_name_to_unicode("Idotaccent").as_deref(), Some("İ"));
// TeX CMMI Yunan karakterleri
assert_eq!(tex_cmmi_to_unicode(0x0B), Some('α'));
assert_eq!(tex_cmmi_to_unicode(0x0C), Some('β'));
assert_eq!(tex_cmmi_to_unicode(0x19), Some('π'));
assert_eq!(tex_cmmi_to_unicode(0x40), Some('∂'));
// TeX CMSY sembolleri
assert_eq!(tex_cmsy_to_unicode(0x06), Some('±'));
assert_eq!(tex_cmsy_to_unicode(0x14), Some('≤'));
assert_eq!(tex_cmsy_to_unicode(0x15), Some('≥'));
assert_eq!(tex_cmsy_to_unicode(0x31), Some('∞'));
}
#[test]
fn test_math_reconstruction_fraction() {
// Yatay kesir çizgisi y = 100, x: [50..100]
let line = VectorLine {
x1: 50.0,
y1: 100.0,
x2: 100.0,
y2: 100.0,
is_horizontal: true,
thickness: 1.0,
color: [0, 0, 0],
};
// Pay: y = 85 (çizginin üstü)
let char_a = PdfChar {
unicode: "a".to_string(),
x: 65.0,
y: 85.0,
width: 8.0,
height: 10.0,
font_size: 10.0,
font_name: "CMMI10".to_string(),
is_math: true,
is_bold: false,
is_italic: true,
color: [0, 0, 0],
};
// Payda: y = 105 (çizginin altı)
let char_b = PdfChar {
unicode: "b".to_string(),
x: 65.0,
y: 105.0,
width: 8.0,
height: 10.0,
font_size: 10.0,
font_name: "CMMI10".to_string(),
is_math: true,
is_bold: false,
is_italic: true,
color: [0, 0, 0],
};
let is_math = MathReconstructor::is_math_zone(&[char_a.clone(), char_b.clone()], std::slice::from_ref(&line));
assert!(is_math, "Kesir çizgisi ve pay/payda matematik bölgesi olarak tanınmalı");
let omml = MathReconstructor::reconstruct_math(&[char_a, char_b], &[line])
.expect("Kesir OMML XML'ine dönüştürülebilmeli");
assert!(omml.contains("<m:oMath>"));
assert!(omml.contains("<m:f>"));
assert!(omml.contains("<m:num>"));
assert!(omml.contains("<m:den>"));
}
#[test]
fn test_table_reconstructor_with_shading() {
// 2 satır, 2 sütun çizgileri
let h1 = VectorLine { x1: 50.0, y1: 50.0, x2: 250.0, y2: 50.0, is_horizontal: true, thickness: 1.0, color: [0, 0, 0] };
let h2 = VectorLine { x1: 50.0, y1: 100.0, x2: 250.0, y2: 100.0, is_horizontal: true, thickness: 1.0, color: [0, 0, 0] };
let h3 = VectorLine { x1: 50.0, y1: 150.0, x2: 250.0, y2: 150.0, is_horizontal: true, thickness: 1.0, color: [0, 0, 0] };
let v1 = VectorLine { x1: 50.0, y1: 50.0, x2: 50.0, y2: 150.0, is_horizontal: false, thickness: 1.0, color: [0, 0, 0] };
let v2 = VectorLine { x1: 150.0, y1: 50.0, x2: 150.0, y2: 150.0, is_horizontal: false, thickness: 1.0, color: [0, 0, 0] };
let v3 = VectorLine { x1: 250.0, y1: 50.0, x2: 250.0, y2: 150.0, is_horizontal: false, thickness: 1.0, color: [0, 0, 0] };
let fill_header = FilledRect {
x: 50.0,
y: 50.0,
width: 200.0,
height: 50.0,
color: [220, 230, 242], // Açık mavi başlık
};
let text_cell1 = PdfChar {
unicode: "Hücre1".to_string(),
x: 60.0,
y: 65.0,
width: 40.0,
height: 10.0,
font_size: 10.0,
font_name: "Arial".to_string(),
is_math: false,
is_bold: true,
is_italic: false,
color: [0, 0, 0],
};
let mut consumed = Vec::new();
let tables = TableReconstructor::extract_tables(
&[h1, h2, h3, v1, v2, v3],
&[fill_header],
&[text_cell1],
&[],
&mut consumed,
);
assert_eq!(tables.len(), 1, "1 tablo tespit edilmeli");
let table = &tables[0];
assert_eq!(table.rows.len(), 2, "2 satır olmalı");
assert_eq!(table.rows[0].len(), 2, "2 sütun olmalı");
assert_eq!(table.rows[0][0].shade, Some([220, 230, 242]), "Başlık hücresi dolgusu korunmalı");
assert!(consumed.contains(&0), "Tablo içindeki karakterler tüketilmeli");
}
#[test]
fn test_end_to_end_pdf_to_docx_with_math() {
let temp_dir = std::env::temp_dir().join(format!("ld_test_{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&temp_dir).expect("Geçici klasör oluşturulmalı");
let pdf_path = temp_dir.join("test_sample.pdf");
let docx_path = temp_dir.join("output_sample.docx");
// Dahili Saf Rust PDF motoru ile test belgesi üret
let mut doc = app_lib::services::office_model::Document::default();
doc.blocks.push(app_lib::services::office_model::Block::Paragraph(
app_lib::services::office_model::Paragraph {
style: Default::default(),
runs: vec![app_lib::services::office_model::Run::plain("Türkçe Metin: LD Converter v3.6.0 Saf Rust Motoru")],
}
));
doc.blocks.push(app_lib::services::office_model::Block::Paragraph(
app_lib::services::office_model::Paragraph {
style: Default::default(),
runs: vec![app_lib::services::office_model::Run::plain("Denklem: E = mc^2 ve a/b")],
}
));
app_lib::services::pdf_writer::write_pdf(&doc, &pdf_path).expect("PDF oluşturulmalı");
assert!(pdf_path.exists(), "PDF dosyası mevcut olmalı");
let result = convert_pdf_to_docx(&pdf_path.to_string_lossy(), &docx_path, true);
assert!(result.is_ok(), "Dönüşüm başarılı olmalı: {:?}", result.err());
assert!(docx_path.exists(), "DOCX çıktısı üretilmiş olmalı");
// DOCX paketini aç ve içeriği doğrula
let docx_file = File::open(&docx_path).expect("DOCX dosyası açılmalı");
let mut zip = ZipArchive::new(docx_file).expect("Geçerli bir ZIP/DOCX paketi olmalı");
let mut doc_xml = String::new();
let mut xml_file = zip.by_name("word/document.xml").expect("word/document.xml pakette bulunmalı");
xml_file.read_to_string(&mut doc_xml).expect("XML okunabilmeli");
println!("DOC_XML CONTENT:\n{}", doc_xml);
assert!(doc_xml.contains("xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\""), "Math XML isim alanı bulunmalı");
assert!(doc_xml.contains("Türkçe Metin: LD Converter"), "Türkçe metin DOCX içine aktarılmış olmalı");
let _ = std::fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_comprehensive_v362_quality_verification() {
use app_lib::services::pdf_parser::layout_analyzer::{LayoutAnalyzer, PageContent};
use app_lib::services::docx_writer::DocxWriter;
let page_box = PageBox::new([0.0, 0.0, 600.0, 800.0], 0);
// 1) Heading 1 (18pt, Kalın)
let mut chars = Vec::new();
let h1_text = "1. GİRİŞ VE GENEL BAKIŞ";
let mut cur_x = 50.0;
for ch in h1_text.chars() {
chars.push(PdfChar {
unicode: ch.to_string(),
x: cur_x,
y: 50.0,
width: 10.0,
height: 18.0,
font_size: 18.0,
font_name: "Arial-Bold".to_string(),
is_math: false,
is_bold: true,
is_italic: false,
color: [0, 0, 0],
});
cur_x += 10.5;
}
// 2) 5 Satırlık Gövde Paragrafı (11pt, satırlar arası 14pt aralık -> tek bir <w:p> olmalı)
let body_sentences = [
"Bu paragrafın birinci satırıdır ve akıcı bir şekilde devam eder.",
"İkinci satırda kelimeler arasında kesinlikle yapışma veya kopma olmaz.",
"Üçüncü satırda Türkçe karakterler ve boşluklar başarıyla korunur.",
"Dördüncü satırda satır sonu birleştirmesi kusursuz çalışır.",
"Beşinci satır ile birlikte bu paragraf tek bir Word paragrafı olmalıdır.",
];
let mut cur_y = 90.0;
for sentence in &body_sentences {
let mut x = 50.0;
for ch in sentence.chars() {
let w = if ch == ' ' { 4.0 } else { 6.5 };
chars.push(PdfChar {
unicode: ch.to_string(),
x,
y: cur_y,
width: w,
height: 11.0,
font_size: 11.0,
font_name: "Arial".to_string(),
is_math: false,
is_bold: false,
is_italic: false,
color: [0, 0, 0],
});
x += w + 0.5;
}
cur_y += 14.0; // line_gap = 14.0 - 11.0 = 3.0 pt <= 0.65 * 11.0 (7.15 pt)
}
// 3) Heading 2 (14pt, Kalın)
cur_y += 20.0; // Paragraf boşluğu
let h2_text = "1.1 Temel Matematiksel Bağıntılar";
let mut h2_x = 50.0;
for ch in h2_text.chars() {
chars.push(PdfChar {
unicode: ch.to_string(),
x: h2_x,
y: cur_y,
width: 8.0,
height: 14.0,
font_size: 14.0,
font_name: "Arial-Bold".to_string(),
is_math: false,
is_bold: true,
is_italic: false,
color: [0, 0, 0],
});
h2_x += 8.5;
}
// 4) Nesir Cümlesi ve Satır İçi Matematik (Burada x ve y için...)
cur_y += 20.0;
let prose_math_sentence = "Burada x ve y için formül geçerlidir.";
let mut pm_x = 50.0;
for ch in prose_math_sentence.chars() {
let is_math_var = ch == 'x' || ch == 'y';
let w = if ch == ' ' { 4.0 } else { 6.0 };
chars.push(PdfChar {
unicode: ch.to_string(),
x: pm_x,
y: cur_y,
width: w,
height: 11.0,
font_size: 11.0,
font_name: if is_math_var { "CMMI10".to_string() } else { "Arial".to_string() },
is_math: is_math_var,
is_bold: false,
is_italic: is_math_var,
color: [0, 0, 0],
});
pm_x += w + 0.5;
}
// 5) Blok Kesirli Denklem: a / b
cur_y += 30.0;
let frac_line = VectorLine {
x1: 100.0,
y1: cur_y + 10.0,
x2: 140.0,
y2: cur_y + 10.0,
is_horizontal: true,
thickness: 1.0,
color: [0, 0, 0],
};
chars.push(PdfChar {
unicode: "a".to_string(),
x: 118.0,
y: cur_y,
width: 8.0,
height: 10.0,
font_size: 10.0,
font_name: "CMMI10".to_string(),
is_math: true,
is_bold: false,
is_italic: true,
color: [0, 0, 0],
});
chars.push(PdfChar {
unicode: "b".to_string(),
x: 118.0,
y: cur_y + 15.0,
width: 8.0,
height: 10.0,
font_size: 10.0,
font_name: "CMMI10".to_string(),
is_math: true,
is_bold: false,
is_italic: true,
color: [0, 0, 0],
});
let page = PageContent {
page_box,
chars,
lines: vec![frac_line],
filled_rects: Vec::new(),
images: Vec::new(),
};
let office_doc = LayoutAnalyzer::analyze_document(vec![page], true);
let temp_dir = std::env::temp_dir().join(format!("ld_test_v362_{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&temp_dir).expect("Geçici klasör");
let docx_path = temp_dir.join("v362_quality.docx");
DocxWriter::create_from_document(&docx_path, &office_doc).expect("DOCX yazılabilmeli");
let docx_file = File::open(&docx_path).expect("DOCX dosyası açılmalı");
let mut zip = ZipArchive::new(docx_file).expect("ZIP arşivi");
let mut doc_xml = String::new();
{
let mut xml_file = zip.by_name("word/document.xml").expect("document.xml");
xml_file.read_to_string(&mut doc_xml).expect("XML okunabilmeli");
}
println!("V362 DOCUMENT XML:\n{}", doc_xml);
// KONTROL 1: Başlık 1 ve Başlık 2 stilleri ile doğru <w:r><w:rPr><w:b/> yapısı
assert!(doc_xml.contains("<w:pStyle w:val=\"Heading1\"/>"), "Heading 1 stili bulunmalı");
assert!(doc_xml.contains("<w:pStyle w:val=\"Heading2\"/>"), "Heading 2 stili bulunmalı");
assert!(doc_xml.contains("<w:r><w:rPr><w:b/>"), "Kalın özelliği <w:r> içinde <w:rPr><w:b/> olarak bulunmalı");
let mut styles_xml = String::new();
zip.by_name("word/styles.xml")
.expect("word/styles.xml bulunmalı")
.read_to_string(&mut styles_xml)
.expect("styles.xml okunabilmeli");
assert!(styles_xml.contains("w:styleId=\"Heading1\""), "styles.xml içinde Heading1 tanımı olmalı");
assert!(styles_xml.contains("w:styleId=\"Heading2\""), "styles.xml içinde Heading2 tanımı olmalı");
// KONTROL 2: Gövde paragraf boşlukları (1.08 satır aralığı)
assert!(doc_xml.contains("w:spacing w:before=\"0\" w:after=\"100\" w:line=\"260\""), "Gövde paragraflarında makul satır aralığı ve boşluk olmalı");
// KONTROL 3: 5 satırlık paragrafın tek bir <w:p> içinde birleştiği kontrolü
let merged_p_count = doc_xml
.split("</w:p>")
.filter(|p| {
p.contains("Bu paragrafın birinci satırıdır")
&& p.contains("Beşinci satır ile birlikte bu paragraf tek bir Word paragrafı olmalıdır")
})
.count();
assert_eq!(merged_p_count, 1, "5 satırlık gövde paragrafı tek bir <w:p> içinde birleşmeli");
// KONTROL 4: Kelimeler arasında yapışma olmamalı
assert!(!doc_xml.contains("Buparagrafın"), "Kelimeler birbirine yapışmamalı");
assert!(!doc_xml.contains("B u p a r a g r a f"), "Harfler birbirinden ayrılmamalı");
// KONTROL 5: Gereksiz sayfa kırılması (<w:br w:type="page"/>) olmamalı
assert!(!doc_xml.contains("<w:br w:type=\"page\"/>"), "Tek sayfalı akışta gereksiz sayfa kırılması olmamalı");
// KONTROL 6: Kesirli blok denklem ve satır içi matematik + Türkçe kelime korunması
assert!(doc_xml.contains("<m:f>"), "Kesir formülü (<m:f>) bulunmalı");
assert!(doc_xml.contains("<m:oMath>"), "Matematik XML (<m:oMath>) bulunmalı");
assert!(doc_xml.contains("Burada "), "Satır içi matematik öncesi Türkçe kelime korunmalı");
assert!(doc_xml.contains(" ve "), "Satır içi matematik arası Türkçe bağlaç korunmalı");
assert!(doc_xml.contains("için formül geçerlidir."), "Satır içi matematik sonrası Türkçe metin korunmalı");
let _ = std::fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_real_multipage_pdf_tm_scale_headings_inline_math_and_no_pagebreak() {
use lopdf::content::{Content, Operation};
use lopdf::{dictionary, Document, Object, Stream};
let temp_dir = std::env::temp_dir().join(format!("ld_real_pdf_{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&temp_dir).expect("Geçici klasör");
let pdf_path = temp_dir.join("gercek_cok_sayfali.pdf");
let docx_path = temp_dir.join("gercek_cikti.docx");
// Gerçek PDF yapısı üret (/F1 1 Tf ile Tm ölçekleme, sahte kalın, 5 satırlı paragraf,
// %70 üzeri dolu 1. sayfa + 2. sayfada Heading 2, satır içi E = mc^2 ve kesirli denklem)
let mut doc = Document::with_version("1.4");
let pages_id = doc.new_object_id();
let font_regular_id = doc.add_object(dictionary! {
"Type" => "Font",
"Subtype" => "Type1",
"BaseFont" => "Helvetica",
"Encoding" => "WinAnsiEncoding",
});
let font_bold_id = doc.add_object(dictionary! {
"Type" => "Font",
"Subtype" => "Type1",
"BaseFont" => "Helvetica-Bold",
"Encoding" => "WinAnsiEncoding",
});
let resources_id = doc.add_object(dictionary! {
"Font" => dictionary! {
"F1" => font_regular_id,
"F2" => font_bold_id,
},
});
let push_text = |ops: &mut Vec<Operation>, font: &str, tf_sz: i64, tm_scale: i64, x: i64, y: i64, text: &str| {
ops.extend([
Operation::new("BT", vec![]),
Operation::new("Tf", vec![font.into(), tf_sz.into()]),
Operation::new(
"Tm",
vec![tm_scale.into(), 0.into(), 0.into(), tm_scale.into(), x.into(), y.into()],
),
Operation::new("Tj", vec![Object::string_literal(text)]),
Operation::new("ET", vec![]),
]);
};
// Sayfa 1: /F1 1 Tf altında Tm [18 0 0 18] başlık + 5 satırlı uzun gövde paragrafı + sayfa alt %75'e inen satırlar
let mut ops1: Vec<Operation> = Vec::new();
push_text(&mut ops1, "F2", 1, 18, 50, 760, "1. GIRIS VE AMAC");
// 5 satırlı gövde paragrafı (y: 720, 706, 692, 678, 664 -> step 14pt on 10pt font => line_gap=4pt <= 6.5pt)
let para_lines = [
"Bu belge cok sayfalik PDF donusum kalitesini test etmek icin uretilmistir.",
"Ikinci satir birinci satirin hemen altinda yer alir ve ayri paragraf olmamalidir.",
"Ucuncu satir boyunca kelimeler arasindaki bosluklar duzgun sekilde korunmalidir.",
"Dorduncu satir ile birlikte metin akisi kesintisiz sekilde devam etmektedir.",
"Besinci satir paragraf birlestirme algoritmasinin basarisini dogrulamaktadir.",
];
for (idx, line_str) in para_lines.iter().enumerate() {
let y = 720 - (idx as i64) * 14;
push_text(&mut ops1, "F1", 1, 10, 50, y, line_str);
}
// Sayfa 1'in %70'in uzerinde doldugunu gostermek icin alt bolumde devam eden govde metni (y=180 on 842pt => top_y ~ 662 / 842 = 78.6%)
for row in 0..8 {
let y = 600 - row * 60;
push_text(
&mut ops1,
"F1",
1,
10,
50,
y,
"Akademik govde metni sayfa boyunca duzenli araliklarla ilerlemektedir.",
);
}
let content1 = Content { operations: ops1 }.encode().expect("Stream 1 encode");
let content1_id = doc.add_object(Stream::new(dictionary! {}, content1));
// Sayfa 2: 1.1 Temel Kavramlar (14pt baslik), satir ici E = mc^2 ve x_1 + x_2 = b/a kesirli denklem
let mut ops2: Vec<Operation> = Vec::new();
push_text(&mut ops2, "F2", 1, 14, 50, 760, "1.1 Temel Kavramlar");
// Satir ici matematik iceren normal cumle: "Burada E = mc^2 formulu gecerlidir."
push_text(&mut ops2, "F1", 10, 1, 50, 720, "Burada E = mc");
push_text(&mut ops2, "F1", 7, 1, 111, 724, "2");
push_text(&mut ops2, "F1", 10, 1, 117, 720, " formulu gecerlidir.");
// Blok denklem: x_1 + x_2 = \frac{b}{a} (y_pdf = 660 civari)
// Yatay kesir cizgisi: (160, 663) -> (186, 663)
ops2.extend([
Operation::new("w", vec![1.0.into()]),
Operation::new("m", vec![160.into(), 663.into()]),
Operation::new("l", vec![186.into(), 663.into()]),
Operation::new("S", vec![]),
]);
// Orta denklem: x_1 + x_2 =
push_text(&mut ops2, "F1", 10, 1, 100, 660, "x");
push_text(&mut ops2, "F1", 7, 1, 106, 657, "1");
push_text(&mut ops2, "F1", 10, 1, 112, 660, "+ x");
push_text(&mut ops2, "F1", 7, 1, 128, 657, "2");
push_text(&mut ops2, "F1", 10, 1, 138, 660, "=");
// Pay 'b' (y=669) ve Payda 'a' (y=650)
push_text(&mut ops2, "F1", 10, 1, 170, 669, "b");
push_text(&mut ops2, "F1", 10, 1, 170, 650, "a");
let content2 = Content { operations: ops2 }.encode().expect("Stream 2 encode");
let content2_id = doc.add_object(Stream::new(dictionary! {}, content2));
let page1_id = doc.add_object(dictionary! {
"Type" => "Page",
"Parent" => pages_id,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
"Resources" => resources_id,
"Contents" => content1_id,
});
let page2_id = doc.add_object(dictionary! {
"Type" => "Page",
"Parent" => pages_id,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
"Resources" => resources_id,
"Contents" => content2_id,
});
let pages = dictionary! {
"Type" => "Pages",
"Kids" => vec![page1_id.into(), page2_id.into()],
"Count" => 2,
};
doc.objects.insert(pages_id, Object::Dictionary(pages));
let catalog_id = doc.add_object(dictionary! {
"Type" => "Catalog",
"Pages" => pages_id,
});
doc.trailer.set("Root", catalog_id);
doc.save(&pdf_path).expect("PDF kaydedilmeli");
let out = convert_pdf_to_docx(&pdf_path.to_string_lossy(), &docx_path, true)
.expect("Çok sayfalı PDF -> DOCX başarısız");
assert!(out.exists());
let docx_file = File::open(&docx_path).expect("DOCX açılmalı");
let mut zip = ZipArchive::new(docx_file).expect("ZIP");
let mut doc_xml = String::new();
zip.by_name("word/document.xml")
.expect("document.xml")
.read_to_string(&mut doc_xml)
.expect("XML okunmalı");
println!("REAL MULTIPAGE DOCUMENT.XML:\n{}", doc_xml);
// 1) Tm ölçekli başlıklar (/F1 1 Tf + Tm 18/14) doğru algılanıp Heading1 ve Heading2 + <w:b/> almış mı?
assert!(doc_xml.contains("<w:pStyle w:val=\"Heading1\"/>"), "Tm 18pt başlık Heading1 olmalı");
assert!(doc_xml.contains("<w:pStyle w:val=\"Heading2\"/>"), "Tm 14pt başlık Heading2 olmalı");
assert!(doc_xml.contains("<w:r><w:rPr><w:b/>"), "Başlıklarda <w:r><w:rPr><w:b/> bulunmalı");
assert!(doc_xml.contains("1. GIRIS VE AMAC"));
assert!(doc_xml.contains("1.1 Temel Kavramlar"));
// 2) 5 satırlı paragraf tek <w:p> içinde birleşmiş mi?
let merged_count = doc_xml
.split("</w:p>")
.filter(|p| {
p.contains("Bu belge cok sayfalik PDF donusum kalitesini test etmek icin uretilmistir.")
&& p.contains("Besinci satir paragraf birlestirme algoritmasinin basarisini dogrulamaktadir.")
})
.count();
assert_eq!(merged_count, 1, "5 satırlık paragraf tek <w:p> içinde birleşmeli");
// 3) Dolu 1. sayfadan 2. sayfaya gereksiz <w:br w:type="page"/> konmamış mı?
assert!(!doc_xml.contains("<w:br w:type=\"page\"/>"), "Dolu sayfadan sonra gereksiz pageBreak basılmamalı");
// 4) Satır içi matematik: nesir metni normal <w:t>, E = mc^2 satır içi <m:oMath> (<m:sSup>) olmuş mu?
assert!(doc_xml.contains("Burada "), "Nesir başlangıcı korunmalı");
assert!(doc_xml.contains("formulu gecerlidir."), "Nesir devamı korunmalı");
assert!(doc_xml.contains("<m:sSup>"), "mc^2 üst simgesi <m:sSup> olmalı");
// 5) Blok denklem: x_1 + x_2 = \frac{b}{a} hem <m:sSub> hem <m:f> üretmiş mi ve kesir solda kalmamış mı?
assert!(doc_xml.contains("<m:oMathPara>"), "Blok denklem <m:oMathPara> içinde olmalı");
assert!(doc_xml.contains("<m:sSub>"), "x_1 / x_2 alt simgesi <m:sSub> olmalı");
assert!(doc_xml.contains("<m:f>"), "b/a kesri <m:f> olmalı");
let x1_pos = doc_xml.find("<m:t>x</m:t>").unwrap_or(usize::MAX);
let frac_pos = doc_xml.find("<m:f>").unwrap_or(0);
assert!(x1_pos < frac_pos, "Kesir x_1 + x_2 = ifadesinin sağına yerleştirilmeli (sol-sağ sırası)");
let _ = std::fs::remove_dir_all(&temp_dir);
}