#[cfg(test)]
mod tests {
use std::fs;
use std::path::Path;
fn generate_sample(
template_path: &str,
output_path: &str,
texts: &[&str],
) -> Result<(), String> {
generate_sample_with_options(template_path, output_path, texts, None, None)
}
fn apply_font(
core: &mut crate::document_core::DocumentCore,
ko_font: &str,
en_font: Option<&str>,
) {
let en = en_font.unwrap_or(ko_font);
let doc = &mut core.document;
let add_font = |fonts: &mut Vec<crate::model::style::Font>, name: &str| -> u16 {
if let Some(idx) = fonts.iter().position(|f| f.name == name) {
return idx as u16;
}
fonts.push(crate::model::style::Font {
raw_data: None,
name: name.to_string(),
alt_type: 0,
is_embedded: false,
bin_item_id_ref: String::new(),
resolved_bin_data_id: None,
alt_name: None,
type_info: None,
default_name: None,
subst_font: None,
native_program: None,
});
(fonts.len() - 1) as u16
};
let mut ko_ids = [0u16; 7];
let mut en_ids = [0u16; 7];
for (lang_idx, lang_fonts) in doc.doc_info.font_faces.iter_mut().enumerate() {
ko_ids[lang_idx] = add_font(lang_fonts, ko_font);
en_ids[lang_idx] = add_font(lang_fonts, en);
}
let para_cs_id = doc.sections[0].paragraphs[0]
.char_shapes
.first()
.map(|cs| cs.char_shape_id as usize)
.unwrap_or(0);
if para_cs_id < doc.doc_info.char_shapes.len() {
let cs = &mut doc.doc_info.char_shapes[para_cs_id];
for lang in 0..7 {
cs.font_ids[lang] = if lang == 1 {
en_ids[lang]
} else {
ko_ids[lang]
};
}
cs.raw_data = None;
}
doc.doc_info.raw_stream = None;
doc.doc_info.raw_stream_dirty = true;
}
fn generate_sample_with_font(
_template_path: &str,
output_path: &str,
texts: &[&str],
font_name: Option<&str>,
) -> Result<(), String> {
generate_sample_with_font_pair(output_path, texts, font_name, None, None)
}
fn generate_sample_with_font_pair(
output_path: &str,
texts: &[&str],
ko_font: Option<&str>,
en_font: Option<&str>,
alignment: Option<crate::model::style::Alignment>,
) -> Result<(), String> {
let template = match (ko_font, en_font) {
(Some("바탕"), Some("Arial")) => "template/blank-batang-arial.hwp",
(Some("맑은 고딕"), Some("Times New Roman")) => "template/blank-malgun-times.hwp",
(Some(ko), None) => match ko {
"바탕" => "template/blank-batang.hwp",
"바탕체" => "template/blank-batangche.hwp",
"돋움" => "template/blank-dotum.hwp",
"맑은 고딕" => "template/blank-malgun.hwp",
_ => "template/empty.hwp",
},
_ => "template/empty.hwp",
};
generate_sample_with_options(template, output_path, texts, None, alignment)
}
fn generate_sample_with_options(
template_path: &str,
output_path: &str,
texts: &[&str],
_font_name: Option<&str>, alignment: Option<crate::model::style::Alignment>,
) -> Result<(), String> {
if let Some(parent) = Path::new(output_path).parent() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let tmpl = Path::new(template_path);
if !tmpl.exists() {
return Err(format!("템플릿 없음: {}", template_path));
}
let data = fs::read(tmpl).map_err(|e| e.to_string())?;
let mut core = crate::document_core::DocumentCore::from_bytes(&data)
.map_err(|e| format!("{:?}", e))?;
if let Some(align) = alignment {
let para = &core.document.sections[0].paragraphs[0];
let ps_id = para.para_shape_id as usize;
if ps_id < core.document.doc_info.para_shapes.len() {
core.document.doc_info.para_shapes[ps_id].alignment = align;
core.document.doc_info.para_shapes[ps_id].raw_data = None;
core.document.doc_info.raw_stream = None;
core.document.doc_info.raw_stream_dirty = true;
}
}
if let Some(first_text) = texts.first() {
let _ = core.insert_text_native(0, 0, 0, first_text);
}
for (i, text) in texts.iter().enumerate().skip(1) {
let prev_para = &core.document.sections[0].paragraphs[i - 1];
let end_pos = prev_para.text.chars().count();
let _ = core.split_paragraph_native(0, i - 1, end_pos, None);
let _ = core.insert_text_native(0, i, 0, text);
}
core.document.sections[0].raw_stream = None;
let bytes = crate::serializer::serialize_document(&core.document)
.map_err(|e| format!("{:?}", e))?;
fs::write(output_path, &bytes).map_err(|e| e.to_string())?;
eprintln!("생성: {} ({}문단)", output_path, texts.len());
let empty_path = output_path.replace(".hwp", "-empty.hwp");
for para in &mut core.document.sections[0].paragraphs {
para.line_segs = vec![crate::model::paragraph::LineSeg::default()];
}
core.document.sections[0].raw_stream = None;
let empty_bytes = crate::serializer::serialize_document(&core.document)
.map_err(|e| format!("{:?}", e))?;
fs::write(&empty_path, &empty_bytes).map_err(|e| e.to_string())?;
eprintln!("생성: {} (LINE_SEG 비움)", empty_path);
Ok(())
}
fn hangul_repeat(pattern: &str, target_chars: usize) -> String {
let chars: Vec<char> = pattern.chars().collect();
let mut result = String::new();
for i in 0..target_chars {
result.push(chars[i % chars.len()]);
}
result
}
fn hangul_with_spaces(pattern: &str, target_chars: usize) -> String {
let chars: Vec<char> = pattern.chars().collect();
let mut result = String::new();
let mut count = 0;
let mut ci = 0;
while count < target_chars {
result.push(chars[ci % chars.len()]);
count += 1;
if count < target_chars {
result.push(' ');
count += 1;
}
ci += 1;
}
result
}
#[test]
fn test_gen_re01_hangul_only() {
let text = hangul_repeat("가나다라마바사아자차카타파하", 100);
let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-01-hangul-only.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-01 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re02_space_count() {
let text = hangul_with_spaces("가나다라마바사아자차카타파하", 100);
let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-02-space-count.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-02 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re03_latin_only() {
let text = "abcdefghijklmnopqrstuvwxyz".repeat(8); let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-03-latin-only.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-03 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re04_digit_only() {
let text = "1234567890".repeat(20); let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-04-digit-only.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-04 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re05_mixed_koen() {
let base = "한글English한글English";
let text = base.repeat(8);
let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-05-mixed-koen.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-05 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re06_punctuation() {
let base = "가,나.다!라?마(바)사[아]자{차}";
let text = base.repeat(5);
let result = generate_sample(
"samples/lseg-01-basic.hwp",
"output/re-06-punctuation.hwp",
&[&text],
);
if let Err(e) = result {
eprintln!("re-06 생성 실패: {}", e);
}
}
#[test]
fn test_gen_re_font_variations() {
let fonts = [
("batang", "바탕"),
("batangche", "바탕체"),
("gulim", "굴림"),
("gulimche", "굴림체"),
("dotum", "돋움"),
("dotumche", "돋움체"),
("malgun", "맑은 고딕"),
];
let text = "가나다라 English 12345 가,나.다! 마바사아 test 67890 자차카타파하";
let long_text = format!("{} {}", text, text);
for (suffix, font_name) in &fonts {
let output = format!("output/re-font-{}.hwp", suffix);
let result = generate_sample_with_font(
"samples/lseg-01-basic.hwp",
&output,
&[&long_text],
Some(font_name),
);
match result {
Ok(()) => eprintln!("생성: {} (폰트: {})", output, font_name),
Err(e) => eprintln!("실패: {} — {}", output, e),
}
}
}
#[test]
fn test_gen_re_alignment_variations() {
use crate::model::style::Alignment;
let aligns = [
("justify", Alignment::Justify),
("left", Alignment::Left),
("center", Alignment::Center),
("right", Alignment::Right),
];
let text = hangul_repeat("가나다라마바사아자차카타파하", 100);
for (suffix, alignment) in &aligns {
let output = format!("output/re-align-{}.hwp", suffix);
let result = generate_sample_with_options(
"samples/lseg-01-basic.hwp",
&output,
&[&text],
None,
Some(*alignment),
);
match result {
Ok(()) => eprintln!("생성: {} (정렬: {:?})", output, alignment),
Err(e) => eprintln!("실패: {} — {}", output, e),
}
}
}
#[test]
fn test_analyze_font_config() {
let templates = [
"template/empty.hwp",
"template/blank-batang.hwp",
"template/blank-dotum.hwp",
"template/blank-batangche.hwp",
"template/blank-malgun.hwp",
"samples/re-eng-mixed-batang-arial-empty.hwp",
"samples/re-mixed-malgun-timesnew-hancom.hwp",
"samples/re-eng-nospace-malgun-times.hwp", ];
for path in &templates {
let p = Path::new(path);
if !p.exists() {
continue;
}
let data = fs::read(p).unwrap();
let doc = crate::parser::parse_document(&data).unwrap();
eprintln!("\n=== {} ===", path);
let lang_names = ["한글", "영어", "한자", "일어", "기타", "기호", "사용자"];
for (li, fonts) in doc.doc_info.font_faces.iter().enumerate() {
let lang = if li < lang_names.len() {
lang_names[li]
} else {
"?"
};
let names: Vec<String> = fonts
.iter()
.map(|f| format!("{}(t{})", f.name, f.alt_type))
.collect();
eprintln!(" font_faces[{}]({}): {:?}", li, lang, names);
}
for (ci, cs) in doc.doc_info.char_shapes.iter().enumerate() {
eprintln!(
" char_shapes[{}]: font_ids={:?} base_size={} spacings={:?} ratios={:?}",
ci, cs.font_ids, cs.base_size, cs.spacings, cs.ratios
);
}
for (pi, para) in doc.sections[0].paragraphs.iter().enumerate() {
let refs: Vec<String> = para
.char_shapes
.iter()
.map(|r| format!("pos={}→cs_id={}", r.start_pos, r.char_shape_id))
.collect();
if !refs.is_empty() {
eprintln!(" para[{}] char_shapes: {:?}", pi, refs);
}
}
}
}
#[test]
fn test_gen_re_english_font_variations() {
let fonts: Vec<(&str, &str, Option<&str>)> = vec![
("batang", "바탕", None), ("batangche", "바탕체", None), ("dotum", "돋움", None), ("malgun", "맑은 고딕", None), ("batang-arial", "바탕", Some("Arial")), ("malgun-times", "맑은 고딕", Some("Times New Roman")), ("malgun-courier", "맑은 고딕", Some("Courier New")), ];
let latin_nospace = "abcdefghijklmnopqrstuvwxyz".repeat(8);
let latin_words = "The quick brown fox jumps over the lazy dog and then runs back again to test line breaking behavior in this sample document ";
let latin_words_long = latin_words.repeat(3);
let mixed = "한글과English가Mixed된Text입니다Test문장Sentence한글English한글English";
let mixed_long = mixed.repeat(3);
for (suffix, ko_font, en_font) in &fonts {
let output = format!("output/re-eng-nospace-{}.hwp", suffix);
let _ = generate_sample_with_font_pair(
&output,
&[&latin_nospace],
Some(ko_font),
*en_font,
None,
);
let output = format!("output/re-eng-words-{}.hwp", suffix);
let _ = generate_sample_with_font_pair(
&output,
&[&latin_words_long],
Some(ko_font),
*en_font,
None,
);
let output = format!("output/re-eng-mixed-{}.hwp", suffix);
let _ = generate_sample_with_font_pair(
&output,
&[&mixed_long],
Some(ko_font),
*en_font,
None,
);
}
}
#[test]
fn test_gen_re_mixed_precision() {
let samples = [
("1tr", "가나다라마바사아자차카타파하가나다라마바사아자차카타파하가나다라마바사아자차카타파ABCDEFGHIJKLMN", "전환1회: 한글43+영문14"),
("2tr", "가나다라마바사아자차카타파ABCDEFG가나다라마바사아자차카타파ABCDEFGHIJKLMNOPQ", "전환2회"),
("0tr", "가나다라마바사아자차카타파하가나다라마바사아자차카타파하가나다라마바사아자차카타파하가나다라마바사", "전환0: 한글만"),
];
for (suffix, text, desc) in &samples {
let output = format!("output/re-mixed-{}.hwp", suffix);
let _ = generate_sample_with_font_pair(&output, &[text], Some("돋움"), None, None);
eprintln!(" {}: {}", suffix, desc);
}
}
#[test]
fn test_gen_all_re_samples() {
test_gen_re01_hangul_only();
test_gen_re02_space_count();
test_gen_re03_latin_only();
test_gen_re04_digit_only();
test_gen_re05_mixed_koen();
test_gen_re06_punctuation();
eprintln!("\n=== 1차 샘플 생성 완료 ===");
eprintln!("검증 필요: samples/re-01 ~ re-06.hwp를 한컴에서 열어 확인");
}
#[test]
fn test_gen_re_multisize() {
let sizes: Vec<(&str, i32, i32)> = vec![
("10-16", 1000, 1600), ("10-20", 1000, 2000), ("8-14", 800, 1400), ("10-10", 1000, 1000), ];
fs::create_dir_all("output").unwrap();
for (suffix, base_size, big_size) in &sizes {
let output = format!("output/re-multisize-{}.hwp", suffix);
let data = fs::read("template/empty.hwp").unwrap();
let mut core = crate::document_core::DocumentCore::from_bytes(&data).unwrap();
let mut big_cs = core.document.doc_info.char_shapes[0].clone();
big_cs.base_size = *big_size;
big_cs.raw_data = None;
let big_cs_id = core.document.doc_info.char_shapes.len() as u32;
core.document.doc_info.char_shapes.push(big_cs);
core.document.doc_info.raw_stream = None;
core.document.doc_info.raw_stream_dirty = true;
core.document.doc_info.char_shapes[0].base_size = *base_size;
core.document.doc_info.char_shapes[0].raw_data = None;
let text = "가나다라마바사아자차카타파하가나다라마바사아자차카타파하가나다라마바사아자차카타파하가나다라마바사아자차카타파하";
let _ = core.insert_text_native(0, 0, 0, text);
let para = &mut core.document.sections[0].paragraphs[0];
let text_len = para.text.chars().count();
let big_start = text_len / 3;
let big_end = text_len * 2 / 3;
let big_start_utf16 = para.char_offsets.get(big_start).copied().unwrap_or(0);
let big_end_utf16 = para.char_offsets.get(big_end).copied().unwrap_or(0);
para.char_shapes = vec![
crate::model::paragraph::CharShapeRef {
start_pos: 0,
char_shape_id: 0,
},
crate::model::paragraph::CharShapeRef {
start_pos: big_start_utf16,
char_shape_id: big_cs_id,
},
crate::model::paragraph::CharShapeRef {
start_pos: big_end_utf16,
char_shape_id: 0,
},
];
core.document.sections[0].raw_stream = None;
let bytes = crate::serializer::serialize_document(&core.document).unwrap();
fs::write(&output, &bytes).unwrap();
let empty_path = output.replace(".hwp", "-empty.hwp");
for p in &mut core.document.sections[0].paragraphs {
p.line_segs = vec![crate::model::paragraph::LineSeg::default()];
}
core.document.sections[0].raw_stream = None;
let empty_bytes = crate::serializer::serialize_document(&core.document).unwrap();
fs::write(&empty_path, &empty_bytes).unwrap();
eprintln!(
"생성: {} (기본={}pt, 큰={}pt)",
output,
base_size / 100,
big_size / 100
);
}
}
}