use super::*;
use pdf_oxide::annotation_types::AnnotationSubtype;
use pdf_oxide::geometry::Rect;
use crate::document::{RawString, choose_text, decode_pdf_string, hex_color, is_interesting};
use crate::glyphs::to_page_frame;
use crate::matching::{
Line, Matching, collapse_spaces, covers, join_lines, join_plain, order_reading,
overlap_fraction, quad_texts, quads_reach_segments, text_under_quads,
};
fn glyph(ch: char, x: f32, baseline: f32, w: f32, h: f32) -> TextChar {
TextChar {
char: ch,
bbox: Rect::new(x, baseline, w, h),
font_size: 10.0,
origin_x: x,
origin_y: baseline,
rendered_advance: w,
..Default::default()
}
}
fn ligature_glyph(ch: char, x: f32, baseline: f32, ink_w: f32, advance: f32) -> TextChar {
TextChar {
rendered_advance: advance,
..glyph(ch, x, baseline, ink_w, 10.0)
}
}
fn rec(page: usize, kind: &str, covered: Option<&str>, comment: Option<&str>) -> Record {
Record {
page,
kind: kind.to_string(),
covered_text: covered.map(str::to_string),
comment: comment.map(str::to_string),
author: None,
modified: None,
color: None,
section: None,
rect: [0.0; 4],
link: String::new(),
diagnostics: Vec::new(),
}
}
#[test]
fn decodes_utf16be_with_bom() {
let bytes = b"\xFE\xFF\x00T\x00o\x00o";
assert_eq!(decode_pdf_string(bytes), "Too");
}
#[test]
fn decodes_utf16le_with_bom() {
let bytes = b"\xFF\xFET\x00o\x00o\x00";
assert_eq!(decode_pdf_string(bytes), "Too");
}
#[test]
fn decodes_utf16_surrogate_pair() {
let bytes = b"\xFE\xFF\xD8\x35\xDC\x4D";
assert_eq!(decode_pdf_string(bytes), "\u{1D44D}");
}
#[test]
fn decodes_ascii_unchanged() {
assert_eq!(decode_pdf_string(b"plain comment"), "plain comment");
}
#[test]
fn decodes_pdfdoc_high_bytes_as_latin1() {
assert_eq!(decode_pdf_string(b"caf\xE9"), "café");
}
#[test]
fn decodes_truncated_utf16_without_panicking() {
assert_eq!(decode_pdf_string(b"\xFE\xFF\x00T\x00"), "T");
}
#[test]
fn decodes_empty_and_bom_only() {
assert_eq!(decode_pdf_string(b""), "");
assert_eq!(decode_pdf_string(b"\xFE\xFF"), "");
}
#[test]
fn empty_raw_string_does_not_fall_back() {
assert_eq!(
choose_text(RawString::Empty, Some("\u{FFFD}\u{FFFD}")),
None
);
}
#[test]
fn absent_raw_string_falls_back() {
assert_eq!(
choose_text(RawString::Absent, Some(" a comment ")),
Some("a comment".to_string())
);
assert_eq!(choose_text(RawString::Absent, Some(" ")), None);
assert_eq!(choose_text(RawString::Absent, None), None);
}
#[test]
fn decoded_raw_string_wins() {
assert_eq!(
choose_text(RawString::Text("Café".to_string()), Some("Caf?")),
Some("Café".to_string())
);
}
#[test]
fn bare_bom_decodes_to_nothing() {
assert_eq!(decode_pdf_string(b"\xFE\xFF"), "");
}
#[test]
fn top_down_bottom_places_glyph_above_baseline() {
let b = Rect::new(10.0, 100.0, 5.0, 12.0);
let (top, bottom) = GlyphSpace::TopDownBottom.to_top_down(&b, 792.0);
assert_eq!((top, bottom), (88.0, 100.0));
}
#[test]
fn top_down_top_places_glyph_below_y() {
let b = Rect::new(10.0, 100.0, 5.0, 12.0);
let (top, bottom) = GlyphSpace::TopDownTop.to_top_down(&b, 792.0);
assert_eq!((top, bottom), (100.0, 112.0));
}
#[test]
fn bottom_up_flips_against_page_height() {
let b = Rect::new(10.0, 100.0, 5.0, 12.0);
let (top, bottom) = GlyphSpace::BottomUp.to_top_down(&b, 792.0);
assert_eq!((top, bottom), (680.0, 692.0));
}
#[test]
fn infers_top_down_bottom_from_baseline_semantics() {
let chars: Vec<TextChar> = (0..24)
.map(|i| glyph('a', 50.0, 50.0 + i as f32 * 20.0, 5.0, 10.0))
.collect();
assert_eq!(
GlyphSpace::infer(&chars, 792.0, false),
Some(GlyphSpace::TopDownBottom)
);
}
#[test]
fn infers_bottom_up_when_y_decreases_through_the_page() {
let chars: Vec<TextChar> = (0..24)
.map(|i| glyph('a', 50.0, 740.0 - i as f32 * 20.0, 5.0, 10.0))
.collect();
assert_eq!(
GlyphSpace::infer(&chars, 792.0, false),
Some(GlyphSpace::BottomUp)
);
}
#[test]
fn declines_to_infer_from_too_few_glyphs() {
let chars: Vec<TextChar> = (0..5)
.map(|i| glyph('a', 50.0, 50.0 + i as f32 * 20.0, 5.0, 10.0))
.collect();
assert_eq!(GlyphSpace::infer(&chars, 792.0, false), None);
}
#[test]
fn infer_uses_off_page_count_when_y_is_a_real_edge() {
let page_height = 100.0;
let chars: Vec<TextChar> = (0..24)
.map(|i| {
let baseline = 60.0 + i as f32 * 1.5;
TextChar {
origin_y: baseline + 9.0,
..glyph('a', 50.0, baseline, 5.0, 12.0)
}
})
.collect();
assert_eq!(
GlyphSpace::infer(&chars, page_height, false),
Some(GlyphSpace::TopDownBottom)
);
let chars: Vec<TextChar> = (0..24)
.map(|i| {
let baseline = 0.5 + i as f32 * 0.2;
TextChar {
origin_y: baseline + 9.0,
..glyph('a', 50.0, baseline, 5.0, 12.0)
}
})
.collect();
assert_eq!(
GlyphSpace::infer(&chars, page_height, false),
Some(GlyphSpace::TopDownTop)
);
}
#[test]
fn moves_glyphs_off_the_media_box_corner() {
let chars = to_page_frame(vec![glyph('A', 80.0, 120.0, 6.0, 12.0)], 20.0, 20.0);
assert_eq!(chars[0].origin_x, 60.0);
assert_eq!(chars[0].origin_y, 100.0);
assert_eq!(chars[0].bbox.x, 60.0);
assert_eq!(chars[0].bbox.y, 100.0);
}
#[test]
fn leaves_glyphs_alone_at_the_origin() {
let chars = to_page_frame(vec![glyph('A', 80.0, 120.0, 6.0, 12.0)], 0.0, 0.0);
assert_eq!((chars[0].origin_x, chars[0].origin_y), (80.0, 120.0));
}
const QTOP: f32 = 88.0;
const QBOTTOM: f32 = 100.0;
const QX0: f32 = 50.0;
const QX1: f32 = 150.0;
fn covers_default(c: &TextChar, gtop: f32, gbottom: f32) -> bool {
covers(c, gtop, gbottom, QX0, QX1, QTOP, QBOTTOM, 0.5)
}
#[test]
fn covers_glyph_inside_the_band() {
let g = glyph('A', 60.0, 100.0, 6.0, 12.0);
assert!(covers_default(&g, 88.0, 100.0));
}
#[test]
fn rejects_glyph_on_the_next_line() {
let g = glyph('B', 60.0, 112.0, 6.0, 12.0);
assert!(!covers_default(&g, 100.0, 112.0));
}
#[test]
fn accepts_zero_height_synthetic_space() {
let g = glyph(' ', 100.0, 94.0, 0.0, 0.0);
assert!(covers_default(&g, 94.0, 94.0));
assert_eq!(
overlap_fraction(100.0, 100.0, 94.0, 94.0, QX0, QX1, QTOP, QBOTTOM),
0.0
);
}
#[test]
fn accepts_the_final_glyph_of_a_selection() {
let g = glyph('s', 143.0, 100.0, 6.0, 12.0); assert!(covers_default(&g, 88.0, 100.0));
}
#[test]
fn rejects_a_trailing_period_outside_the_quad() {
let g = glyph('.', 150.2, 100.0, 2.7, 3.0); assert!(!covers_default(&g, 97.0, 100.0));
}
#[test]
fn rejects_glyph_far_beyond_the_quad() {
let g = glyph('z', 200.0, 100.0, 6.0, 12.0);
assert!(!covers_default(&g, 88.0, 100.0));
}
#[test]
fn admits_a_large_operator_spanning_the_band() {
let g = glyph('\u{2211}', 60.0, 104.0, 6.0, 44.0);
assert!(covers(&g, 60.0, 104.0, QX0, QX1, QTOP, QBOTTOM, 0.5));
assert!(covers(&g, 60.0, 104.0, QX0, QX1, QTOP, QBOTTOM, 0.95));
}
#[test]
fn band_coverage_gate_excludes_merely_taller_glyphs() {
let g = glyph('B', 60.0, 114.0, 6.0, 14.0);
assert!(!covers_default(&g, 100.0, 114.0));
}
#[test]
fn overlap_fraction_measures_the_glyph_not_the_quad() {
let f = overlap_fraction(45.0, 55.0, 88.0, 100.0, QX0, QX1, QTOP, QBOTTOM);
assert!((f - 0.5).abs() < 1e-6, "expected 0.5, got {f}");
}
#[test]
fn overlap_fraction_is_zero_when_disjoint() {
assert_eq!(
overlap_fraction(200.0, 210.0, 88.0, 100.0, QX0, QX1, QTOP, QBOTTOM),
0.0
);
}
const SPACE: GlyphSpace = GlyphSpace::TopDownBottom;
const H: f32 = 792.0;
#[test]
fn orders_glyphs_left_to_right_regardless_of_input_order() {
let a = glyph('a', 10.0, 100.0, 6.0, 10.0);
let b = glyph('b', 16.0, 100.0, 6.0, 10.0);
let c = glyph('c', 22.0, 100.0, 6.0, 10.0);
let hits = vec![&c, &b, &a];
assert_eq!(order_reading(&hits, SPACE, H, 0.25), "abc");
}
#[test]
fn orders_lines_down_the_page_and_joins_with_a_space() {
let a = glyph('a', 10.0, 100.0, 6.0, 10.0);
let b = glyph('b', 10.0, 112.0, 6.0, 10.0);
let hits = vec![&b, &a];
assert_eq!(order_reading(&hits, SPACE, H, 0.25), "a b");
}
#[test]
fn inserts_a_space_across_a_wide_gap() {
let a = glyph('a', 10.0, 100.0, 6.0, 10.0);
let b = glyph('b', 20.0, 100.0, 6.0, 10.0);
let hits = vec![&a, &b];
assert_eq!(order_reading(&hits, SPACE, H, 0.25), "a b");
}
#[test]
fn does_not_insert_a_space_after_a_ligature() {
let f = ligature_glyph('f', 10.0, 100.0, 1.0, 4.0);
let i = ligature_glyph('i', 14.0, 100.0, 1.0, 4.0);
let x = glyph('x', 18.0, 100.0, 6.0, 10.0);
let hits = vec![&f, &i, &x];
assert_eq!(order_reading(&hits, SPACE, H, 0.25), "fix");
}
#[test]
fn keeps_a_subscript_on_its_own_line() {
let k = glyph('K', 10.0, 100.0, 7.0, 10.0);
let v = glyph('v', 17.0, 103.0, 5.0, 7.0);
let hits = vec![&k, &v];
assert_eq!(order_reading(&hits, SPACE, H, 0.25), "Kv");
}
#[test]
fn order_reading_handles_no_hits() {
assert_eq!(order_reading(&[], SPACE, H, 0.25), "");
}
fn quad(x0: f64, x1: f64, uy0: f64, uy1: f64) -> [f64; 8] {
[x1, uy0, x0, uy1, x1, uy1, x0, uy0]
}
#[test]
fn extracts_only_the_glyphs_under_the_quad() {
let line_a = glyph('A', 60.0, 100.0, 6.0, 12.0);
let line_b = glyph('B', 60.0, 112.0, 6.0, 12.0);
let chars = vec![line_a, line_b];
let quads = vec![quad(50.0, 150.0, 100.0, 112.0)];
let out = quad_texts(&quads, &chars, SPACE, 0.0, 0.0, 200.0, 0.5, 0.25, false);
assert_eq!(out, vec!["A".to_string()]);
}
#[test]
fn sorts_quads_into_reading_order() {
let line_a = glyph('A', 60.0, 100.0, 6.0, 12.0);
let line_b = glyph('B', 60.0, 112.0, 6.0, 12.0);
let chars = vec![line_a, line_b];
let quads = vec![
quad(50.0, 150.0, 88.0, 100.0), quad(50.0, 150.0, 100.0, 112.0), ];
let out = quad_texts(&quads, &chars, SPACE, 0.0, 0.0, 200.0, 0.5, 0.25, false);
assert_eq!(out, vec!["A".to_string(), "B".to_string()]);
}
#[test]
fn empty_quad_yields_an_empty_string_not_a_dropped_entry() {
let chars = vec![glyph('A', 60.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(400.0, 500.0, 100.0, 112.0)];
let out = quad_texts(&quads, &chars, SPACE, 0.0, 0.0, 200.0, 0.5, 0.25, false);
assert_eq!(out.len(), 1);
assert_eq!(out[0], "");
}
#[test]
fn a_glyph_is_claimed_by_only_one_quad() {
let g = glyph('i', 60.0, 100.0, 6.0, 12.0);
let chars = vec![g];
let quads = vec![
quad(50.0, 150.0, 100.0, 112.0), quad(50.0, 150.0, 97.0, 109.0), ];
let out = quad_texts(&quads, &chars, SPACE, 0.0, 0.0, 200.0, 0.5, 0.25, false);
assert_eq!(out, vec!["i".to_string()]);
}
#[test]
fn merges_per_glyph_quads_on_one_line() {
let a = glyph('a', 10.0, 100.0, 6.0, 12.0);
let b = glyph('b', 16.0, 100.0, 6.0, 12.0);
let c = glyph('c', 22.0, 100.0, 6.0, 12.0);
let chars = vec![a, b, c];
let quads = vec![
quad(9.0, 16.0, 100.0, 112.0),
quad(15.0, 22.0, 100.0, 112.0),
quad(21.0, 29.0, 100.0, 112.0),
];
let out = quad_texts(&quads, &chars, SPACE, 0.0, 0.0, 200.0, 0.5, 0.25, false);
assert_eq!(out, vec!["abc".to_string()]);
}
#[test]
fn honours_a_non_zero_mediabox_origin() {
let chars = vec![glyph('A', 60.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(70.0, 170.0, 120.0, 132.0)];
let out = quad_texts(&quads, &chars, SPACE, 20.0, 20.0, 200.0, 0.5, 0.25, false);
assert_eq!(out, vec!["A".to_string()]);
}
fn recovered(chars: &[TextChar], quads: &[[f64; 8]], fold: bool) -> Vec<String> {
text_under_quads(
quads,
chars,
&Matching {
space: SPACE,
frame: Frame {
x0: 0.0,
y0: 0.0,
height: 200.0,
},
min_overlap: 0.5,
space_gap: 0.25,
fold_ligatures: fold,
debug: false,
units: &[],
},
)
.into_iter()
.map(|l| l.text)
.collect()
}
#[test]
fn folds_a_presentation_form_to_its_letters() {
let chars = vec![glyph('\u{FB00}', 60.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(50.0, 150.0, 100.0, 112.0)];
assert_eq!(recovered(&chars, &quads, true), vec!["ff"]);
}
#[test]
fn keep_ligatures_leaves_the_glyph_as_the_font_maps_it() {
let chars = vec![glyph('\u{FB00}', 60.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(50.0, 150.0, 100.0, 112.0)];
assert_eq!(recovered(&chars, &quads, false), vec!["\u{FB00}"]);
}
#[test]
fn folding_does_not_touch_letters_that_were_once_ligatures() {
let chars = vec![
glyph('Æ', 60.0, 100.0, 6.0, 12.0),
glyph('Å“', 66.0, 100.0, 6.0, 12.0),
glyph('ß', 72.0, 100.0, 6.0, 12.0),
];
let quads = vec![quad(50.0, 150.0, 100.0, 112.0)];
assert_eq!(recovered(&chars, &quads, true), vec!["Æœß"]);
}
#[test]
fn folding_divides_the_advance_between_the_letters() {
let chars = vec![
glyph('\u{FB00}', 60.0, 100.0, 6.0, 12.0),
glyph('x', 66.0, 100.0, 6.0, 12.0),
];
let quads = vec![quad(50.0, 150.0, 100.0, 112.0)];
assert_eq!(recovered(&chars, &quads, true), vec!["ffx"]);
}
fn declared(text: &str, x: f32, baseline: f32, advance: f32) -> Unit {
Unit {
text: text.to_string(),
segments: vec![actual_text::Segment {
x,
baseline,
advance,
}],
}
}
fn page_frame() -> Frame {
Frame {
x0: 0.0,
y0: 0.0,
height: 200.0,
}
}
fn declared_lines(quads: &[[f64; 8]], chars: &[TextChar], units: &[Unit]) -> Vec<String> {
text_under_quads(
quads,
chars,
&Matching {
space: GlyphSpace::BottomUp,
frame: page_frame(),
min_overlap: 0.5,
space_gap: 0.25,
fold_ligatures: true,
debug: false,
units,
},
)
.into_iter()
.map(|l| l.text)
.collect()
}
#[test]
fn a_majority_covered_declaration_is_emitted_whole() {
let chars = vec![glyph('A', 50.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(50.0, 130.0, 100.0, 112.0)];
let units = vec![declared("Figure 3", 100.0, 100.0, 40.0)];
assert_eq!(declared_lines(&quads, &chars, &units), vec!["A Figure 3"]);
}
#[test]
fn a_minority_covered_declaration_is_omitted_entirely() {
let chars = vec![glyph('A', 50.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(50.0, 110.0, 100.0, 112.0)];
let units = vec![declared("Figure 3", 100.0, 100.0, 40.0)];
assert_eq!(declared_lines(&quads, &chars, &units), vec!["A"]);
}
#[test]
fn a_declaration_across_two_quads_is_emitted_once() {
let chars = vec![
glyph('A', 50.0, 100.0, 6.0, 12.0), glyph('B', 10.0, 88.0, 6.0, 12.0), ];
let quads = vec![
quad(50.0, 140.0, 100.0, 112.0), quad(5.0, 60.0, 88.0, 100.0), ];
let units = vec![Unit {
text: "Figure 3".to_string(),
segments: vec![
actual_text::Segment {
x: 100.0,
baseline: 100.0,
advance: 40.0,
},
actual_text::Segment {
x: 10.0,
baseline: 88.0,
advance: 20.0,
},
],
}];
let out = declared_lines(&quads, &chars, &units);
assert_eq!(out, vec!["A Figure 3", "B"]);
assert_eq!(
out.iter().filter(|l| l.contains("Figure 3")).count(),
1,
"the declared text appeared more than once"
);
}
#[test]
fn a_declared_span_is_never_folded() {
let chars = vec![glyph('A', 50.0, 100.0, 6.0, 12.0)];
let quads = vec![quad(50.0, 130.0, 100.0, 112.0)];
let units = vec![declared("\u{FB01}xed", 100.0, 100.0, 40.0)];
assert_eq!(
declared_lines(&quads, &chars, &units),
vec!["A \u{FB01}xed"]
);
}
#[test]
fn synthesised_characters_span_the_declared_extent() {
let template = glyph('x', 0.0, 100.0, 6.0, 10.0);
let out = actual_text::synthesise(
&template,
"abcd",
actual_text::Segment {
x: 100.0,
baseline: 100.0,
advance: 40.0,
},
page_frame(),
);
assert_eq!(out.len(), 4);
assert_eq!(out[0].origin_x, 100.0);
assert_eq!(out[3].origin_x, 130.0);
assert_eq!(out[0].rendered_advance, 10.0);
assert_eq!(out[0].origin_y, 100.0);
}
#[test]
fn an_empty_declaration_joins_the_two_sides_contiguously() {
let lines = vec![
Line {
text: "infra".to_string(),
band: (88.0, 100.0),
right: Some(120.0),
},
Line {
text: "structure".to_string(),
band: (100.0, 112.0),
right: Some(80.0),
},
];
let units = vec![declared("", 116.0, 100.0, 4.0)];
assert_eq!(
join_lines(&lines, false, &units, page_frame()),
"infrastructure"
);
assert_eq!(
join_lines(&lines, true, &units, page_frame()),
"infrastructure"
);
assert_eq!(
join_lines(&lines, false, &[], page_frame()),
"infra structure"
);
}
#[test]
fn joins_hyphenated_words_across_lines() {
let lines = vec!["soft-".to_string(), "ware".to_string()];
assert_eq!(join_plain(&lines, false), "software");
}
#[test]
fn keep_hyphens_preserves_the_break() {
let lines = vec!["soft-".to_string(), "ware".to_string()];
assert_eq!(join_plain(&lines, true), "soft- ware");
}
#[test]
fn does_not_join_when_the_next_line_starts_uppercase() {
let lines = vec!["Basel-".to_string(), "Committee".to_string()];
assert_eq!(join_plain(&lines, false), "Basel- Committee");
}
#[test]
fn separates_plain_lines_with_one_space() {
let lines = vec!["first".to_string(), "second".to_string()];
assert_eq!(join_plain(&lines, false), "first second");
}
#[test]
fn collapses_runs_of_whitespace() {
assert_eq!(collapse_spaces("a \t\n b"), "a b");
}
#[test]
fn leaves_single_spaces_alone() {
assert_eq!(collapse_spaces("a b c"), "a b c");
}
#[test]
fn converts_device_rgb() {
assert_eq!(hex_color(&[1.0, 0.0, 0.0]), Some("#ff0000".to_string()));
}
#[test]
fn converts_device_gray() {
assert_eq!(hex_color(&[0.5]), Some("#808080".to_string()));
}
#[test]
fn converts_device_cmyk() {
assert_eq!(
hex_color(&[0.0, 1.0, 1.0, 0.0]),
Some("#ff0000".to_string())
);
assert_eq!(
hex_color(&[0.0, 0.0, 0.0, 1.0]),
Some("#000000".to_string())
);
}
#[test]
fn rejects_unknown_colour_arity() {
assert_eq!(hex_color(&[]), None);
assert_eq!(hex_color(&[0.1, 0.2]), None);
}
#[test]
fn clamps_out_of_range_components() {
assert_eq!(hex_color(&[2.0, -1.0, 0.0]), Some("#ff0000".to_string()));
}
#[test]
fn markup_and_note_subtypes_are_interesting() {
assert!(is_interesting(&AnnotationSubtype::Highlight));
assert!(is_interesting(&AnnotationSubtype::Underline));
assert!(is_interesting(&AnnotationSubtype::Text));
assert!(is_interesting(&AnnotationSubtype::FreeText));
}
#[test]
fn structural_subtypes_are_not_interesting() {
assert!(!is_interesting(&AnnotationSubtype::Popup));
assert!(!is_interesting(&AnnotationSubtype::Link));
assert!(!is_interesting(&AnnotationSubtype::Widget));
}
#[test]
fn sorts_by_page_then_down_then_across() {
let mut records = vec![
{
let mut r = rec(2, "note", None, Some("second page"));
r.rect = [100.0, 700.0, 120.0, 712.0];
r
},
{
let mut r = rec(1, "highlight", None, Some("lower left"));
r.rect = [50.0, 200.0, 90.0, 212.0];
r
},
{
let mut r = rec(1, "highlight", None, Some("upper right"));
r.rect = [300.0, 700.0, 340.0, 712.0];
r
},
{
let mut r = rec(1, "highlight", None, Some("upper left"));
r.rect = [50.0, 700.0, 90.0, 712.0];
r
},
];
sort_records(&mut records);
let order: Vec<&str> = records
.iter()
.map(|r| r.comment.as_deref().unwrap())
.collect();
assert_eq!(
order,
vec!["upper left", "upper right", "lower left", "second page"]
);
}
fn mojibake_chain(x: f32, baseline: f32) -> Vec<TextChar> {
['â', '„', '¢']
.iter()
.enumerate()
.map(|(i, &c)| glyph(c, x + i as f32 * 3.0, baseline, 3.0, 10.0))
.collect()
}
fn declaration(text: &str, x: f32, baseline: f32, advance: f32) -> actual_text::Declaration {
actual_text::Declaration {
text: text.to_string(),
segments: vec![actual_text::Segment {
x,
baseline,
advance,
}],
}
}
#[test]
fn a_complete_chain_is_removed_and_becomes_a_unit() {
let mut chars = mojibake_chain(100.0, 700.0);
let decls = vec![declaration("\u{2122}", 100.0, 700.0, 9.0)];
let (units, broken) = actual_text::repair(&mut chars, &decls, 1);
assert_eq!(units.len(), 1);
assert_eq!(units[0].text, "\u{2122}");
assert!(broken.is_empty());
assert!(chars.is_empty(), "the replacement should have been removed");
}
#[test]
fn a_broken_chain_leaves_the_mojibake_in_the_glyph_list() {
let mut chars = mojibake_chain(100.0, 700.0);
chars.pop();
let decls = vec![declaration("\u{2122}", 100.0, 700.0, 9.0)];
let (units, broken) = actual_text::repair(&mut chars, &decls, 1);
assert!(units.is_empty(), "a guess must not be emitted as a unit");
assert_eq!(
chars.iter().map(|c| c.char).collect::<String>(),
"\u{e2}\u{201e}",
"the corrupted glyphs must be left in place"
);
assert_eq!(broken.len(), 1);
assert_eq!(
broken[0].diagnostic,
Diagnostic::DeclarationUnrepaired {
page: 1,
text: "\u{2122}".to_string(),
found: 2,
wanted: 3,
x: 100.0,
}
);
}
#[test]
fn a_broken_chain_reports_the_extent_so_it_can_be_attributed() {
let mut chars = mojibake_chain(100.0, 700.0);
chars.pop();
let decls = vec![declaration("\u{2122}", 100.0, 700.0, 9.0)];
let (_, broken) = actual_text::repair(&mut chars, &decls, 1);
assert_eq!(broken[0].segments.len(), 1);
assert_eq!(broken[0].segments[0].x, 100.0);
assert_eq!(broken[0].segments[0].baseline, 700.0);
assert_eq!(broken[0].segments[0].advance, 9.0);
}
fn quad2(x0: f64, y0: f64, x1: f64, y1: f64) -> [f64; 8] {
[x0, y1, x1, y1, x0, y0, x1, y0]
}
fn seg(x: f32, baseline: f32, advance: f32) -> actual_text::Segment {
actual_text::Segment {
x,
baseline,
advance,
}
}
#[test]
fn a_quad_over_the_declaration_reaches_it() {
let quads = [quad2(90.0, 695.0, 200.0, 710.0)];
assert!(quads_reach_segments(&quads, &[seg(100.0, 700.0, 9.0)]));
}
#[test]
fn a_quad_on_another_line_does_not() {
let quads = [quad2(90.0, 675.0, 200.0, 690.0)];
assert!(!quads_reach_segments(&quads, &[seg(100.0, 700.0, 9.0)]));
}
#[test]
fn a_quad_left_of_the_declaration_does_not() {
let quads = [quad2(10.0, 695.0, 99.0, 710.0)];
assert!(!quads_reach_segments(&quads, &[seg(100.0, 700.0, 9.0)]));
}
#[test]
fn touching_at_an_edge_counts_as_reaching() {
let quads = [quad2(10.0, 695.0, 100.0, 710.0)];
assert!(quads_reach_segments(&quads, &[seg(100.0, 700.0, 9.0)]));
}
#[test]
fn any_quad_reaching_any_segment_is_enough() {
let quads = [
quad2(10.0, 675.0, 80.0, 690.0),
quad2(90.0, 695.0, 200.0, 710.0),
];
let segments = [seg(300.0, 720.0, 9.0), seg(100.0, 700.0, 9.0)];
assert!(quads_reach_segments(&quads, &segments));
}
#[test]
fn a_declaration_with_no_extent_reaches_nothing() {
let quads = [quad2(0.0, 0.0, 1000.0, 1000.0)];
assert!(!quads_reach_segments(&quads, &[]));
}
#[test]
fn corner_order_within_a_quad_does_not_matter() {
let rotated = [200.0, 710.0, 90.0, 710.0, 200.0, 695.0, 90.0, 695.0];
assert!(quads_reach_segments(&[rotated], &[seg(100.0, 700.0, 9.0)]));
}