use super::*;
use crate::model::paragraph::{CharShapeRef, LineSeg, Paragraph};
fn the_frame_splits_a_fallback_run_by_char_shapes_on_both_the_body_and_the_cell_route() {
let para = Paragraph {
text: "abcdefghij".to_string(),
char_offsets: (0..10).collect(),
char_count: 11,
char_shapes: vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 0,
},
CharShapeRef {
start_pos: 5,
char_shape_id: 1,
},
],
..Default::default()
};
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let inner_width_px = 2000.0;
assert_eq!(
compose_paragraph(¶).lines[0]
.runs
.iter()
.map(|r| r.char_style_id)
.collect::<Vec<u32>>(),
vec![0, 1],
"전제: compose_lines 의 NO_LS fallback 도 char_shapes 로 run 을 재분할한다"
);
let mut cell_variant = compose_paragraph(¶);
recompose_cell_lines_in_frame(
&mut cell_variant,
¶,
ParagraphBox::content_width_px(inner_width_px, 96.0),
&styles,
96.0,
false,
);
let cell_run_ids: Vec<u32> = cell_variant.lines[0]
.runs
.iter()
.map(|r| r.char_style_id)
.collect();
let composed = compose_paragraph(¶);
let body_variant = recompose_stored_lines_in_frame(
&composed,
¶,
ParagraphBox::content(0..crate::renderer::px_to_hwpunit(inner_width_px, 96.0)),
inner_width_px,
&styles,
96.0,
false,
StoredRowMissPolicy::Reflow,
&[],
)
.expect("the frame owns the NO_LS rebuild");
let body_run_ids: Vec<u32> = body_variant.lines[0]
.runs
.iter()
.map(|r| r.char_style_id)
.collect();
assert_eq!(
body_run_ids,
vec![0, 1],
"프레임 fill 은 char_shapes 로 토큰화해 두 글자모양이 드러나야 함"
);
assert_eq!(
cell_run_ids, body_run_ids,
"[#5193] 셀 경로도 같은 프레임을 쓰므로 본문과 같은 재분할이 나와야 함"
);
}
fn clean_cell_cache_is_admitted_only_for_its_exact_content_box() {
let text = "clean cell geometry changes independently of text";
let mut para = Paragraph {
text: text.to_string(),
char_offsets: (0..text.chars().count() as u32).collect(),
char_count: text.chars().count() as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 1_000,
text_height: 900,
baseline_distance: 800,
segment_width: 1,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
..Default::default()
}],
..Default::default()
};
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let dpi = 96.0;
let measured = estimate_composed_line_width(&compose_paragraph(¶).lines[0], &styles);
let exact_width = crate::renderer::px_to_hwpunit(measured * 1.2, dpi);
para.line_segs[0].segment_width = exact_width;
assert!(!para.stored_text_partition_is_dirty());
let source = compose_paragraph(¶);
let mut exact = source.clone();
recompose_cell_lines_in_frame(
&mut exact,
¶,
ParagraphBox::content(0..exact_width),
&styles,
dpi,
false,
);
assert_eq!(exact.lines.len(), 1, "an exact cache hit stays stored");
assert_eq!(exact.lines[0].segment_width, exact_width);
let changed_width = crate::renderer::px_to_hwpunit(measured / 1.2, dpi);
let changed_width_px = crate::renderer::hwpunit_to_px(changed_width, dpi);
assert!(
measured < changed_width_px * 1.8,
"the defensive stale heuristic is not the invalidator"
);
let mut changed = source;
recompose_cell_lines_in_frame(
&mut changed,
¶,
ParagraphBox::content(0..changed_width),
&styles,
dpi,
false,
);
assert_eq!(
changed.lines.len(),
1,
"a clean imported cell-box miss is unmodelled without mutation provenance"
);
let mut dirty_para = para.clone();
dirty_para.invalidate_layout_inputs();
let mut dirty_changed = compose_paragraph(&dirty_para);
recompose_cell_lines_in_frame(
&mut dirty_changed,
&dirty_para,
ParagraphBox::content(0..changed_width),
&styles,
dpi,
false,
);
assert!(
dirty_changed.lines.len() > 1,
"a proven cell geometry/text mutation makes the same miss reflowable"
);
assert!(dirty_changed
.lines
.iter()
.all(|line| line.segment_width == changed_width));
}
fn synthetic_multirow_cell_compatibility_applies_only_while_clean() {
let text = "alpha beta gamma delta";
let mut clean_para = Paragraph {
text: text.to_string(),
char_offsets: (0..text.chars().count() as u32).collect(),
char_count: text.chars().count() as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![
LineSeg {
text_start: 0,
line_height: 1_000,
segment_width: 10_000,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE | LineSeg::TAG_IMPLEMENTATION_PROPERTY,
..Default::default()
},
LineSeg {
text_start: 1,
vertical_pos: 1_000,
line_height: 1_000,
segment_width: 10_000,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE | LineSeg::TAG_IMPLEMENTATION_PROPERTY,
..Default::default()
},
],
..Default::default()
};
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let dpi = 96.0;
let clean_source = compose_paragraph(&clean_para);
let mut clean = clean_source.clone();
recompose_cell_lines_in_frame(
&mut clean,
&clean_para,
ParagraphBox::content(0..10_000),
&styles,
dpi,
false,
);
assert_eq!(
clean
.lines
.iter()
.map(|line| line.char_start)
.collect::<Vec<_>>(),
vec![0, 1],
"clean adapter rows retain their compatibility owner"
);
clean_para.insert_text_at(text.chars().count(), " epsilon zeta eta");
clean_para.invalidate_layout_inputs();
assert!(clean_para.stored_text_partition_is_dirty());
let mut dirty = compose_paragraph(&clean_para);
let measured = dirty
.lines
.iter()
.map(|line| estimate_composed_line_width(line, &styles))
.sum::<f64>();
let width_hwp = crate::renderer::px_to_hwpunit(measured / 1.2, dpi);
recompose_cell_lines_in_frame(
&mut dirty,
&clean_para,
ParagraphBox::content(0..width_hwp),
&styles,
dpi,
false,
);
assert!(dirty.lines.len() > 1);
assert!(
dirty.lines[1].char_start > 1,
"dirty synthetic boundaries must not survive the fresh Frame fill"
);
}
fn context_display_overlay_preserves_frame_style_partitions_around_the_field() {
let para = Paragraph {
text: "abc\u{0017}def".to_string(),
char_offsets: (0..7).collect(),
char_count: 8,
char_shapes: vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 0,
},
CharShapeRef {
start_pos: 4,
char_shape_id: 1,
},
],
..Default::default()
};
let mut source = compose_paragraph(¶);
let stale = source.lines[0].runs[0].clone();
source.lines[0].runs = vec![
ComposedTextRun {
text: "abc".to_string(),
..stale.clone()
},
ComposedTextRun {
text: "\u{0017}".to_string(),
display_text: Some("report.hwp".to_string()),
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
..stale.clone()
},
ComposedTextRun {
text: "def".to_string(),
..stale
},
];
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let rebuilt = recompose_stored_lines_in_frame(
&source,
¶,
ParagraphBox::content(0..100_000),
crate::renderer::hwpunit_to_px(100_000, 96.0),
&styles,
96.0,
false,
StoredRowMissPolicy::Reflow,
&[],
)
.expect("Frame owns the NO_LS rebuild");
let runs: Vec<_> = rebuilt.lines[0]
.runs
.iter()
.map(|run| {
(
run.text.as_str(),
run.char_style_id,
run.display_text.as_deref(),
)
})
.collect();
assert_eq!(
runs,
vec![
("abc", 0, None),
("\u{0017}", 0, Some("report.hwp")),
("def", 1, None),
]
);
}
fn modest_field_fill_reflows_a_geometry_matching_stored_partition() {
let initial = "old";
let mut para = Paragraph {
text: initial.to_string(),
char_offsets: (0..initial.chars().count() as u32).collect(),
char_count: initial.chars().count() as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 1_000,
text_height: 900,
baseline_distance: 800,
segment_width: 1,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
..Default::default()
}],
controls: vec![crate::model::control::Control::Field(
crate::model::control::Field::default(),
)],
..Default::default()
};
para.delete_text_at(0, initial.chars().count());
para.insert_text_at(0, "moderately wider field value");
para.invalidate_layout_inputs();
assert!(para.stored_text_partition_is_dirty());
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let dpi = 96.0;
let composed = compose_paragraph(¶);
let measured = estimate_composed_line_width(&composed.lines[0], &styles);
let width_hwp = crate::renderer::px_to_hwpunit(measured / 1.2, dpi);
let inner_width = crate::renderer::hwpunit_to_px(width_hwp, dpi);
para.line_segs[0].segment_width = width_hwp;
assert!(measured > inner_width);
assert!(
measured < inner_width * 1.8,
"the heuristic must stay false"
);
let mut clean_probe = para.clone();
clean_probe.replace_line_segs(clean_probe.line_segs.clone());
let clean_composed = compose_paragraph(&clean_probe);
assert!(!stored_rows_are_stale(
&clean_composed,
&clean_probe,
inner_width,
&styles,
false
));
let mut clean_frame = ParagraphBox::content(0..width_hwp).frame(0);
assert!(matches!(
line_breaking::resolve_stored_line_segs_in_frame(
&clean_probe,
&mut clean_frame,
&styles,
dpi,
false,
StoredRowMissPolicy::Reflow,
false,
&[],
false,
),
Some(line_breaking::StoredRowResolution::Stored)
));
let stale = stored_rows_are_stale(&composed, ¶, inner_width, &styles, false);
assert!(
stale,
"the mutation bit, not overflow magnitude, is decisive"
);
let mut dirty_frame = ParagraphBox::content(0..width_hwp).frame(0);
assert!(matches!(
line_breaking::resolve_stored_line_segs_in_frame(
¶,
&mut dirty_frame,
&styles,
dpi,
false,
StoredRowMissPolicy::Reflow,
stale,
&[],
false,
),
Some(line_breaking::StoredRowResolution::Reflowed)
));
let rebuilt = dirty_frame.project_line_segs();
assert!(rebuilt.len() > 1, "the field value needs a new boundary");
assert!(rebuilt[1].text_start > 0);
let mut materialized = para;
reflow_line_segs(
&mut materialized,
ParagraphBox::content(0..width_hwp),
&styles,
dpi,
);
assert!(
!materialized.stored_text_partition_is_dirty(),
"a model-writing reflow publishes a current partition"
);
{
let text = "alpha\tomega";
let extensions = vec![[7500, 0, 0x0203, 0, 0, 0, 9]];
let mut para = Paragraph {
text: text.into(),
char_count: text.chars().count() as u32 + 1,
char_offsets: (0..text.chars().count() as u32).collect(),
tab_extended: extensions.clone(),
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
segment_width: 15000,
line_height: 1000,
text_height: 900,
baseline_distance: 800,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(
composed.tab_extended, extensions,
"저장 줄 수용 경로는 전진을 보존한다"
);
para.invalidate_layout_inputs();
assert!(para.stored_text_partition_is_dirty());
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let rebuilt = recompose_stored_lines_in_frame(
&composed,
¶,
ParagraphBox::content(0..15000),
200.0,
&styles,
96.0,
false,
StoredRowMissPolicy::Reflow,
&[],
)
.expect("편집으로 무효화한 저장 줄은 현재 프레임에서 재구성한다");
assert!(
rebuilt.tab_extended.is_empty(),
"옛 줄의 전진을 새 줄에서 다시 수용하지 않는다"
);
assert_eq!(
para.tab_extended, extensions,
"원본·직렬화 데이터는 보존한다"
);
let visible: String = rebuilt
.lines
.iter()
.flat_map(|line| &line.runs)
.map(|run| run.text.as_str())
.collect();
assert_eq!(visible, text, "재조판은 내용 순서·누락·중복을 보존한다");
}
}
fn a_field_paragraph_whose_stored_row_cannot_hold_its_text_is_rewrapped() {
let text = "가나다라마바사아자차".repeat(60);
let char_count = text.chars().count();
let para = Paragraph {
text,
char_offsets: (0..char_count as u32).collect(),
char_count: char_count as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 1000,
text_height: 1000,
baseline_distance: 850,
segment_width: 40_000,
tag: LineSeg::TAG_FIRST_SEGMENT | LineSeg::TAG_LAST_SEGMENT,
..Default::default()
}],
controls: vec![crate::model::control::Control::Field(
crate::model::control::Field::default(),
)],
..Default::default()
};
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let inner_width_px = crate::renderer::hwpunit_to_px(40_000, 96.0);
let composed = compose_paragraph(¶);
assert_eq!(
composed.lines.len(),
1,
"전제: 저장 줄이 하나라 조합도 한 줄이다"
);
let rewrapped = recompose_stored_lines_in_frame(
&composed,
¶,
ParagraphBox::content(0..40_000),
inner_width_px,
&styles,
96.0,
false,
StoredRowMissPolicy::Reflow,
&[],
)
.expect("필드를 가진 본문 문단도 프레임이 소유한다");
assert!(
rewrapped.lines.len() > 1,
"내폭을 크게 넘긴 저장 줄은 다시 나뉘어야 한다 (줄수 {})",
rewrapped.lines.len()
);
}
fn an_impossible_paragraph_box_publishes_nothing_on_either_route() {
let stored = LineSeg {
text_start: 0,
line_height: 1000,
text_height: 1000,
baseline_distance: 850,
column_start: 0,
segment_width: 40_000,
tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
..Default::default()
};
let para = Paragraph {
text: "가나다라마바사".to_string(),
char_offsets: (0..7).collect(),
char_count: 8,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![stored.clone()],
..Default::default()
};
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let impossible = ParagraphBox::body(100.0, 80.0, 40.0, 96.0);
assert!(
impossible.width_hwp() < 0,
"전제: 여백이 열을 넘으면 상자 폭이 음수다 ({})",
impossible.width_hwp()
);
assert!(!impossible.is_usable());
let mut edited = para.clone();
reflow_line_segs(&mut edited, impossible.clone(), &styles, 96.0);
let extents = |segs: &[LineSeg]| {
segs.iter()
.map(|seg| (seg.text_start, seg.column_start, seg.segment_width))
.collect::<Vec<_>>()
};
assert_eq!(
extents(&edited.line_segs),
extents(std::slice::from_ref(&stored)),
"불가능한 상자는 저장 기록을 그대로 두어야 한다 — 음수 폭도, 날조된 폭도 발행 금지"
);
let composed = compose_paragraph(¶);
assert!(
recompose_stored_lines_in_frame(
&composed,
¶,
impossible,
0.0,
&styles,
96.0,
false,
StoredRowMissPolicy::Reflow,
&[],
)
.is_none(),
"렌더 경로도 같은 규칙을 따른다"
);
}
#[test]
fn test_compose_single_line_single_style() {
the_frame_splits_a_fallback_run_by_char_shapes_on_both_the_body_and_the_cell_route();
clean_cell_cache_is_admitted_only_for_its_exact_content_box();
synthetic_multirow_cell_compatibility_applies_only_while_clean();
context_display_overlay_preserves_frame_style_partitions_around_the_field();
modest_field_fill_reflows_a_geometry_matching_stored_partition();
a_field_paragraph_whose_stored_row_cannot_hold_its_text_is_rewrapped();
an_impossible_paragraph_box_publishes_nothing_on_either_route();
let para = Paragraph {
text: "안녕하세요".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6, char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 3,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 800,
baseline_distance: 640,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 1);
assert_eq!(composed.lines[0].runs.len(), 1);
assert_eq!(composed.lines[0].runs[0].text, "안녕하세요");
assert_eq!(composed.lines[0].runs[0].char_style_id, 3);
}
#[test]
fn test_compose_single_line_multi_style() {
let para = Paragraph {
text: "ABCDE".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6,
char_shapes: vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 1,
},
CharShapeRef {
start_pos: 3,
char_shape_id: 2,
},
],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 1);
assert_eq!(composed.lines[0].runs.len(), 2);
assert_eq!(composed.lines[0].runs[0].text, "ABC");
assert_eq!(composed.lines[0].runs[0].char_style_id, 1);
assert_eq!(composed.lines[0].runs[1].text, "DE");
assert_eq!(composed.lines[0].runs[1].char_style_id, 2);
}
#[test]
fn test_compose_multi_line() {
let para = Paragraph {
text: "첫줄텍스트두번째줄".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8],
char_count: 10,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 5,
}],
line_segs: vec![
LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
LineSeg {
text_start: 5,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 2);
assert_eq!(composed.lines[0].runs[0].text, "첫줄텍스트");
assert_eq!(composed.lines[1].runs[0].text, "두번째줄");
}
#[test]
fn test_compose_internal_forced_line_break_splits_visual_lines() {
let para = Paragraph {
text: "가나\n다라".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 7,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 2);
assert_eq!(composed.lines[0].runs[0].text, "가나");
assert!(composed.lines[0].has_line_break);
assert_eq!(composed.lines[0].char_start, 0);
assert_eq!(composed.lines[1].runs[0].text, "다라");
assert!(!composed.lines[1].has_line_break);
assert_eq!(composed.lines[1].char_start, 3);
}
#[test]
fn test_compose_trailing_forced_line_break_keeps_single_marked_line() {
let para = Paragraph {
text: "가나\n".to_string(),
char_offsets: vec![0, 1, 2],
char_count: 4,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 7,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 1);
assert_eq!(composed.lines[0].runs[0].text, "가나");
assert!(composed.lines[0].has_line_break);
assert_eq!(composed.lines[0].char_start, 0);
}
#[test]
fn test_compose_multi_line_multi_style() {
let para = Paragraph {
text: "AAABBBCCCC".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
char_count: 11,
char_shapes: vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 1,
},
CharShapeRef {
start_pos: 3,
char_shape_id: 2,
},
CharShapeRef {
start_pos: 6,
char_shape_id: 3,
},
],
line_segs: vec![
LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
LineSeg {
text_start: 6,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 2);
assert_eq!(composed.lines[0].runs.len(), 2);
assert_eq!(composed.lines[0].runs[0].text, "AAA");
assert_eq!(composed.lines[0].runs[0].char_style_id, 1);
assert_eq!(composed.lines[0].runs[1].text, "BBB");
assert_eq!(composed.lines[0].runs[1].char_style_id, 2);
assert_eq!(composed.lines[1].runs.len(), 1);
assert_eq!(composed.lines[1].runs[0].text, "CCCC");
assert_eq!(composed.lines[1].runs[0].char_style_id, 3);
}
#[test]
fn test_char_overlap_multi_component_is_single_advance() {
let chars = vec!['\u{F02BA}', '\u{F02C3}'];
assert_eq!(decode_pua_overlap_number(&chars).as_deref(), Some("10"));
assert_eq!(pua_overlap_number_border(&chars), 3);
assert_eq!(char_overlap_advance_units(&chars), 1);
assert_eq!(
decode_pua_overlap_number(&['\u{F029D}', '\u{F02A8}']).as_deref(),
Some("12")
);
assert_eq!(pua_overlap_number_border(&['\u{F02D7}', '\u{F02E2}']), 4);
}
#[test]
fn char_overlap_signed_size_steps_follow_hancom_print() {
assert_eq!(char_overlap_size_ratio(0, -4), 1.0);
assert_eq!(char_overlap_size_ratio(0, 90), 1.0);
for (char_size, expected) in [(-3, 0.7), (0, 1.0), (10, 2.0), (20, 3.0), (50, 6.0)] {
assert!((char_overlap_size_ratio(1, char_size) - expected).abs() < 1e-9);
}
assert_eq!(char_overlap_size_ratio(4, -2), 0.8);
assert_eq!(char_overlap_size_ratio(3, 0), 1.0);
}
#[test]
fn test_compose_with_ctrl_char_gap() {
let para = Paragraph {
text: "ABC".to_string(),
char_offsets: vec![8, 9, 10],
char_count: 12,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 1,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 1);
assert_eq!(composed.lines[0].runs[0].text, "ABC");
assert_eq!(composed.lines[0].runs[0].char_style_id, 1);
}
#[test]
fn test_identify_inline_controls_table() {
use crate::model::table::Table;
let mut table = Table::default();
table.common.treat_as_char = true;
let para = Paragraph {
text: "표 앞 텍스트".to_string(),
controls: vec![Control::Table(Box::new(table))],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.inline_controls.len(), 1);
assert_eq!(
composed.inline_controls[0].control_type,
InlineControlType::Table
);
assert_eq!(composed.inline_controls[0].control_index, 0);
}
#[test]
fn test_utf16_range_to_text_range() {
let offsets = vec![0u32, 1, 2, 8, 9, 10];
let (s, e) = utf16_range_to_text_range(&offsets, 0, 3, 6);
assert_eq!(s, 0);
assert_eq!(e, 3);
let (s, e) = utf16_range_to_text_range(&offsets, 8, 11, 6);
assert_eq!(s, 3);
assert_eq!(e, 6);
}
#[test]
fn test_utf16_range_no_offsets() {
let (s, e) = utf16_range_to_text_range(&[], 0, 5, 10);
assert_eq!(s, 0);
assert_eq!(e, 5);
}
#[test]
fn test_compose_decreasing_lineseg_text_start_uses_empty_range() {
let para = Paragraph {
text: "ABCDE".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 1,
}],
line_segs: vec![
LineSeg {
text_start: 4,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 2);
assert!(composed.lines[0].runs.is_empty());
assert_eq!(composed.lines[0].char_start, 4);
assert_eq!(composed.lines[1].runs[0].text, "ABCDE");
}
#[test]
fn test_find_active_char_shape() {
let shapes = vec![
CharShapeRef {
start_pos: 0,
char_shape_id: 1,
},
CharShapeRef {
start_pos: 10,
char_shape_id: 2,
},
CharShapeRef {
start_pos: 20,
char_shape_id: 3,
},
];
assert_eq!(find_active_char_shape(&shapes, 0), 1);
assert_eq!(find_active_char_shape(&shapes, 5), 1);
assert_eq!(find_active_char_shape(&shapes, 10), 2);
assert_eq!(find_active_char_shape(&shapes, 15), 2);
assert_eq!(find_active_char_shape(&shapes, 25), 3);
}
fn make_styles_with_font_size(font_size: f64) -> ResolvedStyleSet {
use crate::renderer::style_resolver::{ResolvedCharStyle, ResolvedParaStyle, ResolvedStyleSet};
ResolvedStyleSet {
hwp3_variant: false,
hft_ascii_halfwidth: false,
char_styles: vec![ResolvedCharStyle {
font_size,
ratio: 1.0,
..Default::default()
}],
para_styles: vec![ResolvedParaStyle::default()],
..Default::default()
}
}
#[test]
fn test_reflow_short_text_single_line() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "안녕".to_string(),
char_offsets: vec![0, 1],
char_count: 3,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(500.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 1);
assert_eq!(para.line_segs[0].text_start, 0);
}
#[test]
fn test_reflow_long_text_multi_line() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "가나다라마바사아자차".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
char_count: 11,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(80.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2);
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(para.line_segs[1].text_start, 5); }
#[test]
fn test_reflow_empty_text() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph::default();
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(500.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 1);
assert_eq!(para.line_segs[0].text_start, 0);
}
#[test]
fn test_reflow_inline_only_paragraph_uses_utf16_text_start() {
use crate::model::image::Picture;
use crate::model::shape::CommonObjAttr;
let styles = make_styles_with_font_size(16.0);
let make_pic = |width: u32| {
Control::Picture(Box::new(Picture {
common: CommonObjAttr {
width,
height: 3000,
treat_as_char: true,
..Default::default()
},
..Default::default()
}))
};
let mut para = Paragraph {
controls: vec![make_pic(3000), make_pic(3000)],
char_count: 17, ..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(50.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2, "개체마다 한 줄");
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(
para.line_segs[1].text_start, 8,
"둘째 개체의 UTF-16 오프셋은 8 (컨트롤 인덱스 1 이 아니다)"
);
}
#[test]
fn test_reflow_latin_text() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "ABCDEFGHIJ".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
char_count: 11,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(40.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2);
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(para.line_segs[1].text_start, 5);
}
#[test]
fn test_reflow_line_height() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "가".to_string(),
char_offsets: vec![0],
char_count: 2,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(500.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 1);
assert_eq!(para.line_segs[0].line_height, 1200);
}
#[test]
fn test_split_runs_by_lang_korean_english() {
let runs = vec![ComposedTextRun {
text: "안녕Hello세계".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}];
let result = split_runs_by_lang(runs);
assert_eq!(result.len(), 3);
assert_eq!(result[0].text, "안녕");
assert_eq!(result[0].lang_index, 0); assert_eq!(result[1].text, "Hello");
assert_eq!(result[1].lang_index, 1); assert_eq!(result[2].text, "세계");
assert_eq!(result[2].lang_index, 0); }
#[test]
fn test_split_runs_by_lang_no_split() {
let runs = vec![ComposedTextRun {
text: "안녕하세요".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}];
let result = split_runs_by_lang(runs);
assert_eq!(result.len(), 1);
assert_eq!(result[0].text, "안녕하세요");
assert_eq!(result[0].lang_index, 0);
}
#[test]
fn test_split_runs_by_lang_space_follows_prev() {
let runs = vec![ComposedTextRun {
text: "안녕 Hello 세계".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}];
let result = split_runs_by_lang(runs);
assert_eq!(result.len(), 3);
assert_eq!(result[0].text, "안녕 ");
assert_eq!(result[0].lang_index, 0); assert_eq!(result[1].text, "Hello ");
assert_eq!(result[1].lang_index, 1); assert_eq!(result[2].text, "세계");
assert_eq!(result[2].lang_index, 0); }
#[test]
fn test_split_runs_by_lang_empty() {
let runs = vec![ComposedTextRun {
text: "".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}];
let result = split_runs_by_lang(runs);
assert_eq!(result.len(), 1);
assert_eq!(result[0].text, "");
}
#[test]
fn test_split_runs_by_lang_english_only() {
let runs = vec![ComposedTextRun {
text: "Hello World".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}];
let result = split_runs_by_lang(runs);
assert_eq!(result.len(), 1);
assert_eq!(result[0].text, "Hello World");
assert_eq!(result[0].lang_index, 1); }
#[test]
fn test_is_lang_neutral() {
assert!(is_lang_neutral(' '));
assert!(is_lang_neutral('.'));
assert!(is_lang_neutral(','));
assert!(is_lang_neutral('!'));
assert!(is_lang_neutral('('));
assert!(!is_lang_neutral('A'));
assert!(!is_lang_neutral('가'));
assert!(!is_lang_neutral('漢'));
}
#[test]
fn test_reflow_lang_aware_mixed() {
use crate::renderer::style_resolver::{ResolvedCharStyle, ResolvedParaStyle, ResolvedStyleSet};
let styles = ResolvedStyleSet {
hwp3_variant: false,
hft_ascii_halfwidth: false,
char_styles: vec![ResolvedCharStyle {
font_family: "함초롬돋움".to_string(),
font_families: vec![
"함초롬돋움".to_string(), "Arial".to_string(), "".to_string(),
"".to_string(),
"".to_string(),
"".to_string(),
"".to_string(),
],
font_size: 16.0,
ratio: 1.0,
ratios: vec![1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
letter_spacing: 0.0,
letter_spacings: vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
..Default::default()
}],
para_styles: vec![ResolvedParaStyle::default()],
..Default::default()
};
let mut para = Paragraph {
text: "가나다ABC".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5],
char_count: 7,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(500.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 1);
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(30.0, 96.0),
&styles,
96.0,
);
assert!(
para.line_segs.len() > 1,
"좁은 너비에서 줄 바꿈이 발생해야 함"
);
}
#[test]
fn test_estimate_composed_line_width() {
let styles = make_styles_with_font_size(16.0);
let line = ComposedLine {
runs: vec![ComposedTextRun {
text: "가나다".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}],
line_height: 400,
baseline_distance: 320,
segment_width: 0,
column_start: 0,
line_spacing: 0,
has_line_break: false,
char_start: 0,
};
let width = estimate_composed_line_width(&line, &styles);
assert!(width > 0.0, "폭이 0보다 커야 함");
}
#[test]
fn test_reflow_korean_eojeol_wrap() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "안녕하세요 반갑습니다".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
char_count: 12,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(100.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2, "어절 경계에서 줄 바꿈");
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(para.line_segs[1].text_start, 6);
}
#[test]
fn test_reflow_korean_break_unit_contract() {
let mut word_styles = make_styles_with_font_size(16.0);
word_styles.para_styles[0].korean_break_unit = 0;
let mut char_styles = make_styles_with_font_size(16.0);
char_styles.para_styles[0].korean_break_unit = 1;
let make_para = || Paragraph {
text: "가나 다라".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
let mut word_para = make_para();
reflow_line_segs(
&mut word_para,
ParagraphBox::content_width_px(60.0, 96.0),
&word_styles,
96.0,
);
let mut char_para = make_para();
reflow_line_segs(
&mut char_para,
ParagraphBox::content_width_px(60.0, 96.0),
&char_styles,
96.0,
);
let word_starts: Vec<u32> = word_para
.line_segs
.iter()
.map(|seg| seg.text_start)
.collect();
let char_starts: Vec<u32> = char_para
.line_segs
.iter()
.map(|seg| seg.text_start)
.collect();
assert_eq!(word_starts, vec![0, 3], "어절 모드는 공백 뒤에서 줄바꿈");
assert_eq!(char_starts, vec![0, 4], "글자 모드는 다음 어절 일부를 채움");
}
#[test]
fn test_reflow_english_word_wrap() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "Hello World".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
char_count: 12,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(60.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2, "단어 경계에서 줄 바꿈");
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(para.line_segs[1].text_start, 6); }
#[test]
fn issue_4442_corrected_noto_ascii_advances_change_threshold_wrap_with_kerning_off() {
use crate::renderer::layout::{estimate_text_width_unrounded, resolved_to_text_style};
use crate::renderer::style_resolver::ResolvedCharStyle;
let mut styles = make_styles_with_font_size(1000.0);
styles.char_styles[0] = ResolvedCharStyle {
font_family: "Noto Sans KR".to_string(),
font_size: 1000.0,
kerning: false,
..Default::default()
};
let text_style = resolved_to_text_style(&styles, 0, 1);
let corrected_width = estimate_text_width_unrounded("AVATAR", &text_style);
let prior_table_width = 3416.0;
assert!(
(corrected_width - 68118.0 * 4.0 / 75.0).abs() < 1e-9,
"{corrected_width}"
);
let threshold = (prior_table_width + corrected_width) / 2.0;
let mut para = Paragraph {
text: "AVATAR".to_string(),
char_offsets: (0..6).collect(),
char_count: 7,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(threshold, 96.0),
&styles,
96.0,
);
assert_eq!(
para.line_segs
.iter()
.map(|line| line.text_start)
.collect::<Vec<_>>(),
vec![0, 5]
);
}
fn reflow_after_prior_break_line_starts(text: &str, indent_px: f64) -> Vec<u32> {
let mut styles = make_styles_with_font_size(16.0);
styles.para_styles[0].indent = indent_px;
let mut utf16_len = 0u32;
let char_offsets = text
.chars()
.map(|ch| {
let offset = utf16_len;
utf16_len += ch.len_utf16() as u32;
offset
})
.collect();
let mut para = Paragraph {
text: text.to_string(),
char_offsets,
char_count: utf16_len + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(40.0, 96.0),
&styles,
96.0,
);
para.line_segs.iter().map(|seg| seg.text_start).collect()
}
#[test]
fn issue_3822_reflow_overlong_latin_token_after_prior_break() {
assert_eq!(
reflow_after_prior_break_line_starts("가 ABCDEFGHIJKL", 0.0),
vec![0, 2, 7, 12],
"이전 공백 뒤 긴 Latin 토큰도 새 줄 폭을 넘을 때 계속 글자 단위로 분할해야 함"
);
}
#[test]
fn issue_3822_reflow_overlong_korean_word_after_prior_break() {
assert_eq!(
reflow_after_prior_break_line_starts("A 가나다라마바사", 0.0),
vec![0, 2, 4, 6, 8],
"이전 공백 뒤 긴 한글 어절도 새 줄 폭을 넘을 때 계속 글자 단위로 분할해야 함"
);
}
#[test]
fn issue_3822_reflow_overlong_digit_token_after_prior_break() {
assert_eq!(
reflow_after_prior_break_line_starts("A 123456789012", 0.0),
vec![0, 2, 7, 12],
"이전 공백 뒤 긴 숫자 토큰도 새 줄 폭을 넘을 때 계속 글자 단위로 분할해야 함"
);
}
#[test]
fn issue_3822_reflow_overlong_digit_preserves_nonempty_post_break_width() {
assert_eq!(
reflow_after_prior_break_line_starts("A 가.123456789012", 0.0),
vec![0, 2, 6, 11],
"이전 break 뒤 잔여 글자 폭을 보존한 상태에서 긴 숫자를 분할해야 함"
);
}
#[test]
fn issue_3822_reflow_overlong_token_uses_subsequent_line_indent_width() {
assert_eq!(
reflow_after_prior_break_line_starts("A ABCDEFGHIJKL", -8.0),
vec![0, 2, 6, 10],
"첫 줄 뒤에는 hanging indent가 적용된 후속 줄 폭으로 긴 토큰을 분할해야 함"
);
}
#[test]
fn test_reflow_condense_shrinks_measured_space_width() {
let mut styles = make_styles_with_font_size(10.0);
styles.para_styles[0].condense_min_space = 20;
let mut para = Paragraph {
text: "A B ABCDEF".to_string(),
char_offsets: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
char_count: 10,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(48.1, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 1);
}
#[test]
fn test_reflow_forced_line_break() {
let styles = make_styles_with_font_size(16.0);
let mut para = Paragraph {
text: "가나\n다라".to_string(),
char_offsets: vec![0, 1, 2, 3, 4],
char_count: 6,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
..Default::default()
}],
..Default::default()
};
reflow_line_segs(
&mut para,
ParagraphBox::content_width_px(500.0, 96.0),
&styles,
96.0,
);
assert_eq!(para.line_segs.len(), 2, "\\n에서 강제 줄 바꿈");
assert_eq!(para.line_segs[0].text_start, 0);
assert_eq!(para.line_segs[1].text_start, 3); }
#[test]
fn test_geumchik_functions() {
assert!(is_line_start_forbidden(')'));
assert!(is_line_start_forbidden('.'));
assert!(is_line_start_forbidden(','));
assert!(is_line_start_forbidden('!'));
assert!(is_line_start_forbidden('%'));
assert!(!is_line_start_forbidden('가'));
assert!(!is_line_start_forbidden('A'));
assert!(is_line_end_forbidden('('));
assert!(is_line_end_forbidden('['));
assert!(is_line_end_forbidden('$'));
assert!(is_line_end_forbidden('\u{20A9}')); assert!(!is_line_end_forbidden('가'));
assert!(!is_line_end_forbidden('A'));
assert!(!is_line_end_forbidden('"'));
assert!(is_line_start_forbidden('"'));
assert!(is_line_end_forbidden('\u{201C}'));
assert!(is_line_end_forbidden('\u{300C}'));
}
#[test]
fn test_tokenize_korean_eojeol() {
let styles = make_styles_with_font_size(16.0);
let text: Vec<char> = "가나 다라".chars().collect();
let offsets: Vec<u32> = (0..text.len() as u32).collect();
let shapes = vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}];
let tokens = tokenize_paragraph(&text, &offsets, &shapes, &styles, 0, 0);
assert_eq!(tokens.len(), 3);
assert!(matches!(
tokens[0],
BreakToken::Text {
start_idx: 0,
end_idx: 2,
..
}
));
assert!(matches!(tokens[1], BreakToken::Space { idx: 2, .. }));
assert!(matches!(
tokens[2],
BreakToken::Text {
start_idx: 3,
end_idx: 5,
..
}
));
}
#[test]
fn test_tokenize_korean_character_unit() {
let styles = make_styles_with_font_size(16.0);
let text: Vec<char> = "가나".chars().collect();
let offsets: Vec<u32> = (0..text.len() as u32).collect();
let shapes = vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}];
let tokens = tokenize_paragraph(&text, &offsets, &shapes, &styles, 0, 1);
assert_eq!(tokens.len(), 2);
assert!(matches!(
tokens[0],
BreakToken::Text {
start_idx: 0,
end_idx: 1,
..
}
));
assert!(matches!(
tokens[1],
BreakToken::Text {
start_idx: 1,
end_idx: 2,
..
}
));
}
#[test]
fn test_tokenize_english_words() {
let styles = make_styles_with_font_size(16.0);
let text: Vec<char> = "AB CD".chars().collect();
let offsets: Vec<u32> = (0..text.len() as u32).collect();
let shapes = vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}];
let tokens = tokenize_paragraph(&text, &offsets, &shapes, &styles, 0, 0);
assert_eq!(tokens.len(), 3);
assert!(matches!(
tokens[0],
BreakToken::Text {
start_idx: 0,
end_idx: 2,
..
}
));
assert!(matches!(tokens[1], BreakToken::Space { idx: 2, .. }));
assert!(matches!(
tokens[2],
BreakToken::Text {
start_idx: 3,
end_idx: 5,
..
}
));
}
#[test]
fn test_tokenize_line_break() {
let styles = make_styles_with_font_size(16.0);
let text: Vec<char> = "가\n나".chars().collect();
let offsets: Vec<u32> = (0..text.len() as u32).collect();
let shapes = vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}];
let tokens = tokenize_paragraph(&text, &offsets, &shapes, &styles, 0, 0);
assert_eq!(tokens.len(), 3);
assert!(matches!(tokens[1], BreakToken::LineBreak { idx: 1 }));
}
#[test]
fn test_555_effective_text_for_metrics_uses_display_text_when_present() {
let run = ComposedTextRun {
text: "\u{F861}".to_string(), char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: Some("《".to_string()), supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
};
let effective = super::effective_text_for_metrics(&run);
assert_eq!(
effective, "《",
"PUA 옛한글 변환 후 폰트 매트릭스는 display_text (자모 시퀀스) 기준이어야 함. \
현재 STUB 은 text (PUA 1글자) 반환 → 자모 시퀀스 폭과 불일치."
);
}
#[test]
fn test_555_effective_text_for_metrics_multi_jamo_cluster() {
let run = ComposedTextRun {
text: "\u{F8E0}".to_string(), char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: Some("ᄃᆞᄫᆡ".to_string()), supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
};
let effective = super::effective_text_for_metrics(&run);
assert_eq!(
effective.chars().count(),
4,
"옛한글 합자 PUA → 4-jamo 시퀀스 변환 시 폰트 매트릭스 char count 도 4 이어야 함."
);
assert_eq!(effective, "ᄃᆞᄫᆡ");
}
#[test]
fn test_555_effective_text_for_metrics_no_display_text_falls_back_to_text() {
let run = ComposedTextRun {
text: "한글".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
};
let effective = super::effective_text_for_metrics(&run);
assert_eq!(
effective, "한글",
"display_text=None 인 경우 text 그대로 반환. 비-PUA fallback 회귀 가드."
);
}
#[test]
fn test_677_effective_text_for_metrics_preserves_f081c_filler() {
let run = ComposedTextRun {
text: "\u{F081C}\u{F081C}".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: Some("□□".to_string()),
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
};
let effective = super::effective_text_for_metrics(&run);
assert_eq!(
effective, "\u{F081C}\u{F081C}",
"U+F081C filler 는 글자 수를 보존하기 위해 원문으로 측정해야 함."
);
}
#[test]
fn test_expand_tone_marks_to_spacing_dot() {
let out = expand_pua_render_text("\u{302E} 각");
assert!(!out.contains('\u{302E}'), "원본 방점이 남으면 안 됨");
assert!(!out.contains('\u{25CC}'), "dotted-circle 아티팩트 금지");
assert_eq!(out, "\u{00B7} 각", "선두 방점은 가운데 점으로 치환");
let out2 = expand_pua_render_text("\u{302F}가");
assert_eq!(out2, "\u{205A}가", "쌍방점은 세로 두 점으로 치환");
}
#[test]
fn test_expand_hancom_relationship_line_pua_to_box_drawing() {
let out = expand_pua_render_text("\u{F0811}\u{F0817}\u{F081A}");
assert_eq!(
out, "┌└─",
"한컴 관계도 PUA 선문자는 공개 폰트 환경에서 두부가 아닌 box drawing 문자로 표시되어야 함"
);
}
#[test]
fn legacy_hancom_product_names_use_display_projection_only() {
let text = "ᄒᆞᆫ글, ᄒᆞᆫ메일, ᄒᆞᆫ팩스, ᄒᆞᆫ소프트, ᄒᆞᆫ겨울";
let char_count = text.chars().count();
let para = Paragraph {
text: text.to_string(),
char_offsets: (0..char_count as u32).collect(),
char_count: char_count as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
}],
..Default::default()
};
let composed = compose_paragraph(¶);
let run = &composed.lines[0].runs[0];
assert_eq!(run.text, text, "원문 IR은 바꾸지 않는다");
assert_eq!(
run.display_text.as_deref(),
Some("한글, 한메일, 한팩스, 한소프트, ᄒᆞᆫ겨울"),
"닫힌 legacy 제품명 어휘만 한컴 PDF 표기처럼 투영한다"
);
}
#[test]
fn legacy_hancom_product_projection_survives_line_boundary() {
let text = "ᄒᆞᆫ글";
let para = Paragraph {
text: text.to_string(),
char_offsets: vec![0, 1, 2, 3],
char_count: 5,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![
LineSeg {
text_start: 0,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
LineSeg {
text_start: 3,
line_height: 400,
baseline_distance: 320,
..Default::default()
},
],
..Default::default()
};
let composed = compose_paragraph(¶);
assert_eq!(composed.lines.len(), 2);
assert_eq!(composed.lines[0].runs[0].text, "ᄒᆞᆫ");
assert_eq!(
composed.lines[0].runs[0].display_text.as_deref(),
Some("한")
);
assert_eq!(composed.lines[1].runs[0].text, "글");
assert_eq!(composed.lines[1].runs[0].display_text, None);
}
#[test]
fn test_kbu1_line_start_forbidden_retraction() {
let styles = make_styles_with_font_size(16.0);
let line = ComposedLine {
runs: vec![ComposedTextRun {
text: "적용한다.111111".to_string(),
char_style_id: 0,
lang_index: 0,
neutral_space_origin: None,
char_overlap: None,
footnote_marker: None,
display_text: None,
supplemental_metrics_blocked: false,
inserted_control_text: false,
space_metric: crate::renderer::composer::SpaceMetric::Stored,
regenerated_font_metrics: false,
literal_figure_space: false,
literal_soft_hyphen: false,
}],
line_height: 400,
baseline_distance: 320,
segment_width: 0,
column_start: 0,
line_spacing: 0,
has_line_break: false,
char_start: 0,
};
let frags =
split_composed_line_by_width(&line, 68.0, 68.0, &styles, true, 0.0, false, 0.0, false);
assert!(
frags.len() >= 2,
"두 줄 이상으로 분할되어야 함: {:?}",
frags.len()
);
let line2_text: String = frags[1].runs.iter().map(|r| r.text.as_str()).collect();
assert!(
!line2_text.starts_with('.'),
"새 줄이 행두 금칙 '.'로 시작하면 안 됨 (한컴: 직전 글자 동반 이월): {:?}",
line2_text
);
assert!(
line2_text.starts_with("다."),
"한컴 오라클 정합: 둘째 줄은 '다.'로 시작해야 함: {:?}",
line2_text
);
assert_eq!(
frags[1].char_start, 3,
"retraction 후 char_start 는 '다' 위치"
);
}
fn issue4149_guard_para(text: &str) -> Paragraph {
let n = text.chars().count();
Paragraph {
text: text.to_string(),
char_offsets: (0..n as u32).collect(),
char_count: n as u32 + 1,
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
line_segs: vec![LineSeg {
text_start: 0,
line_height: 800,
baseline_distance: 640,
..Default::default()
}],
..Default::default()
}
}
#[test]
fn issue4149_renderer_cache_survives_fresh_compositions() {
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let para = issue4149_guard_para(&"가".repeat(60));
let width = 50.0; let cache = SingleLineOverflowCache::default();
let mut composed = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut composed,
¶,
width,
&styles,
96.0,
Some(&cache),
);
assert!(composed.lines.len() > 1);
let mut composed2 = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut composed2,
¶,
width,
&styles,
96.0,
Some(&cache),
);
assert!(
composed2.lines.len() > 1,
"cache hit에도 derived rewrap은 새 composition에 적용돼야 함"
);
}
#[test]
fn issue4149_renderer_cache_keys_actual_cell_width() {
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let para = issue4149_guard_para(&"가".repeat(60));
let cache = SingleLineOverflowCache::default();
for (width, expected_lines) in [(50.0, 2..usize::MAX), (5000.0, 1..2)] {
let mut composed = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut composed,
¶,
width,
&styles,
96.0,
Some(&cache),
);
assert!(
expected_lines.contains(&composed.lines.len()),
"frame width {width} must select its own cached judgment"
);
}
}
#[test]
fn issue4149_renderer_cache_clear_owns_source_invalidation() {
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let mut para = issue4149_guard_para("가나다");
let cache = SingleLineOverflowCache::default();
let mut composed = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut composed,
¶,
50.0,
&styles,
96.0,
Some(&cache),
);
assert_eq!(composed.lines.len(), 1);
para.insert_text_at(0, &"가".repeat(60));
cache.clear();
let mut recomposed = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut recomposed,
¶,
50.0,
&styles,
96.0,
Some(&cache),
);
assert!(
recomposed.lines.len() > 1,
"renderer owner clear must retire the fit judgment after source mutation"
);
}
#[test]
fn issue4149_fit_judgment_is_cached_without_rewrap() {
let styles = crate::renderer::style_resolver::ResolvedStyleSet::default();
let para = issue4149_guard_para("가나다");
let width = 5000.0;
let cache = SingleLineOverflowCache::default();
let mut composed = compose_paragraph(¶);
recompose_stored_single_line_if_overflowing_cached(
&mut composed,
¶,
width,
&styles,
96.0,
Some(&cache),
);
assert_eq!(
composed.lines.len(),
1,
"정합 단일줄은 재래핑하지 않아야 함"
);
}
#[test]
fn owned_rowbreak_tac_height_selects_current_or_multirow_frames() {
use crate::model::control::Control;
use crate::model::table::{Table, TablePageBreak};
let para_with_rows = |row_count, line_height| Paragraph {
controls: vec![Control::Table(Box::new(Table {
row_count,
page_break: TablePageBreak::RowBreak,
common: crate::model::shape::CommonObjAttr {
treat_as_char: true,
height: 32_339,
..Default::default()
},
..Default::default()
}))],
line_segs: vec![LineSeg {
line_height,
segment_width: 49_324,
..Default::default()
}],
..Default::default()
};
let multirow = para_with_rows(4, 32_339);
assert_eq!(owned_rowbreak_tac_height(&multirow, 0), Some(32_339));
let mut single_row = para_with_rows(1, 32_339);
single_row.line_segs[0].line_spacing = 1_200;
assert_eq!(owned_rowbreak_tac_height(&single_row, 0), None);
assert_eq!(
stored_first_tac_line(&single_row).map(|line| line.line_height),
Some(32_339)
);
assert_eq!(
tac_host_trailing_spacing(&single_row, 0, &single_row.line_segs[0], true, false, 96.0),
16.0
);
let mut current_single_row = para_with_rows(1, 32_339);
current_single_row.line_segs[0].tag = LineSeg::TAG_IMPLEMENTATION_PROPERTY;
current_single_row.line_segs[0].line_spacing = 1_200;
assert_eq!(
owned_rowbreak_tac_height(¤t_single_row, 0),
Some(32_339)
);
assert_eq!(
tac_host_trailing_spacing(
¤t_single_row,
0,
¤t_single_row.line_segs[0],
true,
false,
96.0,
),
8.0
);
assert_eq!(
tac_host_trailing_spacing(
¤t_single_row,
0,
¤t_single_row.line_segs[0],
false,
false,
96.0,
),
16.0
);
let mut moved = single_row.clone();
moved.line_segs[0].vertical_pos = moved.line_segs[0].line_height * 2;
let mut next = Paragraph {
line_segs: vec![LineSeg {
vertical_pos: moved.line_segs[0].line_height + moved.line_segs[0].line_spacing,
line_height: moved.line_segs[0].line_spacing,
..Default::default()
}],
..Default::default()
};
let closes = |host: &Paragraph, following: &Paragraph| {
tac_next_line_full_spacing(
host,
Some(following),
0.0,
0.0,
&host.line_segs[0],
crate::model::provenance::LayoutCompatibilityProfile::default(),
96.0,
)
};
assert!(closes(&moved, &next));
next.line_segs[0].vertical_pos -= moved.line_segs[0].line_spacing;
assert!(
!closes(&moved, &next),
"되감김만으로 전량 간격을 소유하지 않는다"
);
next.line_segs[0].vertical_pos += moved.line_segs[0].line_spacing;
next.cell_format_vpos_dirty = true;
assert!(!closes(&moved, &next));
next.cell_format_vpos_dirty = false;
next.line_segs[0].tag = LineSeg::TAG_IMPLEMENTATION_PROPERTY;
assert!(
crate::renderer::float_placement::restarted_tac::restarted_tac_line_advance(
&moved, &next, 0.0, 0.0, 96.0,
)
.is_none()
);
assert!(closes(&moved, &next));
next.line_segs[0].tag = 0;
moved.line_segs[0].line_height -= 1;
next.line_segs[0].vertical_pos -= 1;
assert!(
!closes(&moved, &next),
"불완전한 개체 높이의 닫힘을 채택하지 않는다"
);
let undersized = para_with_rows(4, 32_338);
assert_eq!(owned_rowbreak_tac_height(&undersized, 0), None);
assert!(stored_first_tac_line(&undersized).is_none());
assert!(stored_first_tac_line(¤t_single_row).is_none());
single_row.text = "본문".to_owned();
assert!(
stored_first_tac_line(&single_row).is_none(),
"가시 텍스트와 같은 줄은 별도 구성 계약"
);
}
#[test]
fn default_tabs_use_the_physical_row_origin() {
let styles = ResolvedStyleSet {
char_styles: vec![crate::renderer::style_resolver::ResolvedCharStyle {
font_size: 10.0,
..Default::default()
}],
para_styles: vec![crate::renderer::style_resolver::ResolvedParaStyle {
default_tab_width: 80.0,
..Default::default()
}],
..Default::default()
};
let para = Paragraph {
text: "\t가나다라".into(),
char_count: 6,
char_offsets: (0..5).collect(),
char_shapes: vec![CharShapeRef {
start_pos: 0,
char_shape_id: 0,
}],
..Default::default()
};
let rows = |left| {
let mut frame =
crate::renderer::layout_frame::LayoutFrame::new(left..left + 7500, 0, vec![]);
super::line_breaking::layout_paragraph_in_frame(¶, &mut frame, &styles, 96.0).unwrap()
};
assert_eq!(rows(0).len(), 2);
assert_eq!(
rows(1500).len(),
1,
"탭 격자는 줄 내부 0이 아니라 물리 행의 시작 좌표에서 이어진다"
);
}
#[test]
fn recalculating_vpos_keeps_the_room_of_a_paragraph_without_stored_lines() {
let line = |vertical_pos| LineSeg {
vertical_pos,
line_height: 1000,
text_height: 1000,
baseline_distance: 850,
line_spacing: 600,
segment_width: 42520,
..Default::default()
};
let lined = |vertical_pos| Paragraph {
line_segs: vec![line(vertical_pos)],
..Default::default()
};
let mut paragraphs = vec![lined(0), Paragraph::default(), lined(3600), lined(5200)];
let styles = ResolvedStyleSet::default();
recalculate_section_vpos(&mut paragraphs, 0, None, Some(1600), &styles, 96.0, false);
assert_eq!(paragraphs[2].line_segs[0].vertical_pos, 3600);
assert_eq!(paragraphs[3].line_segs[0].vertical_pos, 5200);
paragraphs[0].line_segs.push(line(1600));
recalculate_section_vpos(&mut paragraphs, 0, None, Some(1600), &styles, 96.0, false);
assert_eq!(paragraphs[2].line_segs[0].vertical_pos, 5200);
assert_eq!(paragraphs[3].line_segs[0].vertical_pos, 6800);
}