use f_office_paint::Item;
use f_office_units::Length;
use crate::grid::DOT;
use crate::layout::testing::{
EAST_ASIAN_FAKE, NORMAL, at, base, du, find, lay_out, on_base_with, p, pass_with, rects, table,
};
use crate::numbering::Numbering;
use crate::quirks::{GridSingleLine, NoBreakSpaceShare};
#[test]
fn draws_a_line_through_struck_text_at_the_strikeout_position() {
let list = lay_out(
"<w:p><w:r><w:rPr><w:strike/></w:rPr><w:t>a</w:t></w:r></w:p>",
NORMAL,
None,
);
let baseline = find(&list, 'a').2 * DOT;
let lines: Vec<(i64, i64)> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) => Some((r.y.units(), r.height.units())),
_ => None,
})
.collect();
assert_eq!(lines, vec![(baseline - 10 * DOT, 2 * DOT)]);
}
#[test]
fn underlines_an_underlined_tab_across_its_space() {
let list = lay_out(
r#"<w:p><w:pPr><w:tabs><w:tab w:val="left" w:pos="2880"/></w:tabs></w:pPr><w:r><w:rPr><w:u w:val="single"/></w:rPr><w:t>a</w:t><w:tab/></w:r></w:p>"#,
NORMAL,
None,
);
let (_, x, _) = find(&list, 'a');
let mut spans: Vec<(i64, i64)> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Rect(r) => Some((r.x.units(), r.x.units() + r.width.units())),
_ => None,
})
.collect();
spans.sort_unstable();
let glyph = f_office_units::div_round(x + 100 * Length::UNITS_PER_TWIP, DOT) * DOT;
let stop = 1440 * Length::UNITS_PER_TWIP + 2880 * Length::UNITS_PER_TWIP;
assert_eq!(spans, vec![(x, glyph), (glyph, stop)]);
}
#[test]
fn marks_a_line_with_a_tracked_change_in_the_margin() {
let changed = lay_out(
r#"<w:p><w:r><w:t>a</w:t></w:r><w:ins w:author="A"><w:r><w:t>b</w:t></w:r></w:ins></w:p><w:p><w:r><w:t>c</w:t></w:r></w:p>"#,
NORMAL,
None,
);
let bars: Vec<_> = rects(&changed, 0)
.into_iter()
.filter(|r| r.0 == 150 && r.2 == 3)
.collect();
assert_eq!(bars.len(), 1);
assert_eq!(bars[0].1, 300);
let plain = lay_out(&p("a"), NORMAL, None);
assert!(rects(&plain, 0).iter().all(|r| r.0 != 150));
}
#[test]
fn marks_the_line_of_a_changed_or_inserted_paragraph_mark() {
let bars = |mark: &str| {
let body = format!("{}<w:p><w:pPr><w:rPr>{mark}</w:rPr></w:pPr></w:p>", p("a"));
let list = lay_out(&body, NORMAL, None);
rects(&list, 0)
.into_iter()
.filter(|r| r.0 == 150 && r.2 == 3)
.map(|r| r.1)
.collect::<Vec<_>>()
};
let second = 1640 * Length::UNITS_PER_TWIP / DOT;
assert_eq!(
bars(r#"<w:rPrChange w:author="A"><w:rPr><w:b/></w:rPr></w:rPrChange>"#),
[second]
);
assert_eq!(bars(r#"<w:ins w:author="A"/>"#), [second]);
assert_eq!(bars(r#"<w:del w:author="A"/>"#), [] as [i64; 0]);
assert_eq!(bars("<w:b/>"), [] as [i64; 0]);
}
#[test]
fn puts_the_underline_on_whole_device_dots() {
let list = lay_out(
r#"<w:p><w:r><w:rPr><w:u w:val="single"/></w:rPr><w:t>a</w:t></w:r></w:p>"#,
NORMAL,
None,
);
let line = list.pages()[0].items().iter().find_map(|item| match item {
Item::Rect(rect) => Some((rect.y.units(), rect.height.units())),
_ => None,
});
let baseline = base(1440) * DOT;
assert_eq!(line, Some((baseline + 4 * DOT, 2 * DOT)));
}
fn run_defaults(fonts: &str) -> String {
format!(
r"<w:docDefaults><w:rPrDefault><w:rPr>{fonts}</w:rPr></w:rPrDefault><w:pPrDefault/></w:docDefaults>{NORMAL}"
)
}
fn east_asian_gaps(body: &str, styles: &str) -> [i64; 3] {
let list = lay_out(body, styles, None);
let x = |ch: char| find(&list, ch).1;
[x('1') - x('ä¸'), x('a') - x('1'), x('æ–‡') - x('a')]
}
#[test]
fn spaces_east_asian_letters_from_digits_and_latin_letters() {
let pt = |v: i64| v * 25_400 / 2;
let styles = run_defaults(&format!(r#"<w:rFonts w:eastAsia="{EAST_ASIAN_FAKE}"/>"#));
assert_eq!(
east_asian_gaps(&p("ä¸1aæ–‡"), &styles),
[pt(15), pt(10), pt(15)]
);
let off = r#"<w:p><w:pPr><w:autoSpaceDN w:val="0"/></w:pPr><w:r><w:t>ä¸1aæ–‡</w:t></w:r></w:p>"#;
assert_eq!(east_asian_gaps(off, &styles), [pt(10), pt(10), pt(15)]);
let themed = run_defaults(r#"<w:rFonts w:eastAsiaTheme="minorEastAsia"/>"#);
assert_eq!(
east_asian_gaps(&p("ä¸1aæ–‡"), &themed),
[pt(15), pt(10), pt(15)]
);
}
#[test]
fn gives_no_east_asian_space_without_an_east_asian_default_font() {
let pt = |v: i64| v * 25_400 / 2;
let language = format!(
r#"{NORMAL}<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:rPr><w:rFonts w:eastAsia="{EAST_ASIAN_FAKE}"/><w:lang w:eastAsia="zh-CN"/></w:rPr></w:style>"#
);
let latin = run_defaults(r#"<w:rFonts w:eastAsia="Fake Sans"/>"#);
for styles in [language.as_str(), NORMAL, &latin] {
assert_eq!(east_asian_gaps(&p("ä¸1aæ–‡"), styles), [pt(10); 3]);
}
}
fn no_break_gap(styles: &str, mode: u32, share: NoBreakSpaceShare) -> i64 {
no_break_gap_after(r#"<w:r><w:t xml:space="preserve">a"#, styles, mode, share)
}
fn no_break_gap_after(start: &str, styles: &str, mode: u32, share: NoBreakSpaceShare) -> i64 {
let text = format!("\u{a0}b {}", "c ".repeat(80));
let body =
format!(r#"<w:p><w:pPr><w:jc w:val="both"/></w:pPr>{start}{text}</w:t></w:r></w:p>"#);
let target = on_base_with(|profile| profile.with_variant(share));
let list = pass_with(
&body,
(styles, Some(mode)),
None,
Numbering::default(),
target,
)
.list;
find(&list, 'b').1 - find(&list, 'a').1
}
#[test]
fn stretches_a_no_break_space_below_mode_15_with_east_asian_run_defaults() {
use NoBreakSpaceShare::{EastAsianDefaults, None as Kept};
let east_asian = run_defaults(&format!(r#"<w:rFonts w:eastAsia="{EAST_ASIAN_FAKE}"/>"#));
let latin = run_defaults(r#"<w:rFonts w:eastAsia="Fake Sans"/>"#);
let template = format!("<w:docDefaults><w:pPrDefault/></w:docDefaults>{NORMAL}");
for styles in [east_asian.as_str(), &template] {
let kept = no_break_gap(styles, 12, Kept);
assert!(no_break_gap(styles, 12, EastAsianDefaults) > kept);
assert!(no_break_gap(styles, 14, EastAsianDefaults) > kept);
assert_eq!(
no_break_gap(styles, 15, EastAsianDefaults),
no_break_gap(styles, 15, Kept)
);
}
for styles in [latin.as_str(), NORMAL] {
assert_eq!(
no_break_gap(styles, 12, EastAsianDefaults),
no_break_gap(styles, 12, Kept)
);
}
}
#[test]
fn keeps_the_width_of_a_no_break_space_that_starts_a_text_object() {
use NoBreakSpaceShare::{EastAsianDefaults, None as Kept};
let template = format!("<w:docDefaults><w:pPrDefault/></w:docDefaults>{NORMAL}");
let gap = |start: &str, share| no_break_gap_after(start, &template, 12, share);
let kept = gap(r#"<w:r><w:t xml:space="preserve">a"#, Kept);
let apart = r#"<w:r><w:t>a</w:t></w:r><w:r w:rsidR="00100001"><w:t xml:space="preserve">"#;
assert_eq!(gap(apart, EastAsianDefaults), kept);
let alike = r#"<w:r><w:t>a</w:t></w:r><w:r><w:t xml:space="preserve">"#;
assert!(gap(alike, EastAsianDefaults) > kept);
}
#[test]
fn puts_a_multiple_of_single_spacing_in_that_many_steps_of_a_line_grid() {
let grid = r#"<w:sectPr><w:docGrid w:type="lines" w:linePitch="360"/></w:sectPr>"#;
let double = |text: &str| {
format!(
r#"<w:p><w:pPr><w:spacing w:line="480" w:lineRule="auto"/></w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>"#
)
};
let list = lay_out(&(double("a") + &double("b") + grid), NORMAL, None);
let (_, ya) = at(&list, 'a');
let (_, yb) = at(&list, 'b');
assert_eq!(yb - ya, du(720));
assert_eq!(ya, du(1440 + 260) + 33);
let half = |text: &str| {
format!(
r#"<w:p><w:pPr><w:spacing w:line="360" w:lineRule="auto"/></w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>"#
)
};
let list = lay_out(&(half("a") + &half("b") + grid), NORMAL, None);
assert_eq!(at(&list, 'b').1 - at(&list, 'a').1, du(540));
let cells = table(
"",
&[4000],
&[("", &[double("a").as_str()]), ("", &[double("b").as_str()])],
);
let list = lay_out(&(cells + "<w:p/>" + grid), NORMAL, None);
assert_eq!(at(&list, 'b').1 - at(&list, 'a').1, du(400));
}
#[test]
fn places_a_single_line_in_its_grid_steps_from_the_parts_that_the_target_rounds() {
let line = |text: &str, size: u32| {
format!(
r#"<w:p><w:pPr><w:spacing w:line="240" w:lineRule="auto" w:after="0"/></w:pPr><w:r><w:rPr><w:sz w:val="{size}"/></w:rPr><w:t>{text}</w:t></w:r></w:p>"#
)
};
for (pitch, size, middle, rounded) in [
(360, 22, (351, 426), (352, 427)),
(305, 20, (345, 408), (345, 409)),
(300, 20, (344, 407), (344, 407)),
(276, 20, (342, 399), (341, 398)),
(220, 22, (337, 383), (337, 383)),
] {
let grid =
format!(r#"<w:sectPr><w:docGrid w:type="lines" w:linePitch="{pitch}"/></w:sectPr>"#);
let body = line("a", size) + &line("b", size) + &grid;
for (variant, (first, second)) in [
(GridSingleLine::Middle, middle),
(GridSingleLine::PartsRounded, rounded),
] {
let target = on_base_with(|profile| profile.with_variant(variant));
let list = pass_with(&body, (NORMAL, None), None, Numbering::default(), target).list;
assert_eq!(at(&list, 'a').1, first, "{pitch} {variant:?}");
assert_eq!(at(&list, 'b').1, second, "{pitch} {variant:?}");
}
}
}
#[test]
fn puts_the_baseline_of_an_at_least_line_up_from_its_rounded_bottom() {
let line = |text: &str| {
format!(
r#"<w:p><w:pPr><w:spacing w:line="282" w:lineRule="atLeast" w:after="0"/></w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>"#
)
};
let list = lay_out(&(line("a") + &line("b")), NORMAL, None);
assert_eq!(at(&list, 'a').1, 351);
assert_eq!(at(&list, 'b').1, 410);
}
#[test]
fn gives_lines_of_east_asian_fonts_room_above_and_below() {
let run = |text: &str| {
format!(
r#"<w:p><w:r><w:rPr><w:rFonts w:ascii="{EAST_ASIAN_FAKE}" w:hAnsi="{EAST_ASIAN_FAKE}"/></w:rPr><w:t>{text}</w:t></w:r></w:p>"#
)
};
let list = lay_out(&(run("a") + &run("b")), NORMAL, None);
let (_, ya) = at(&list, 'a');
let (_, yb) = at(&list, 'b');
assert_eq!(yb - ya, du(260));
assert_eq!(ya, du(1440) + 39);
}
#[test]
fn gives_no_east_asian_room_to_complex_script_text() {
let pitch = |props: &str| {
let run =
|text: &str| format!(r"<w:p><w:r><w:rPr>{props}</w:rPr><w:t>{text}</w:t></w:r></w:p>");
let list = lay_out(&(run("a") + &run("b")), NORMAL, None);
at(&list, 'b').1 - at(&list, 'a').1
};
let complex = pitch(&format!(r#"<w:rFonts w:cs="{EAST_ASIAN_FAKE}"/><w:rtl/>"#));
assert_eq!(complex, pitch(""));
assert!(complex < du(260), "{complex}");
}
struct Em2048;
impl crate::Fonts for Em2048 {
fn face(
&self,
family: &f_office_fonts::FamilyName,
bold: bool,
italic: bool,
) -> Option<crate::ChosenFace> {
let mut face = crate::layout::testing::Fake.face(family, bold, italic)?;
face.metrics.units_per_em = 2048;
face.metrics.hhea_ascender = 1950;
face.metrics.hhea_descender = -550;
face.metrics.line_top = 1950;
face.metrics.script_top = 1950;
face.metrics.descent = 550;
Some(face)
}
fn glyph(&self, font: f_office_fonts::FontId, ch: char) -> Option<u32> {
crate::layout::testing::Fake.glyph(font, ch)
}
fn advance(&self, font: f_office_fonts::FontId, glyph: u32) -> Option<u16> {
crate::layout::testing::Fake.advance(font, glyph)
}
}
#[test]
fn adds_the_heights_of_lines_of_faces_of_2048_units_per_em_in_the_targets_unit() {
use crate::quirks::LineHeightUnit;
let paragraph = r#"<w:p><w:pPr><w:spacing w:after="0" w:line="276" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="21"/></w:rPr><w:t>q</w:t></w:r></w:p>"#;
let body = paragraph.repeat(40);
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let bytes = crate::tests::package(&body, "", page);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let top = 1440 * Length::UNITS_PER_TWIP;
let mut seen = Vec::new();
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let unit = profile.quirks().line_height_unit;
let dot = profile.device_dot().units();
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let list = crate::layout(&package, (&Em2048, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
let baselines: Vec<i64> = list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Glyphs(run) => run.glyphs.first().map(|g| g.y.units()),
_ => None,
})
.collect();
assert_eq!(baselines.len(), 40, "{target}");
let step = match unit {
LineHeightUnit::Whole4096ths => 60_375 * 3_175,
LineHeightUnit::HalfEmus => 374_396 * 512,
};
let dots = |k: i64| f_office_units::div_round(top * 512 + k * step, dot * 512);
let expected: Vec<i64> = (0..40)
.map(|k| baselines[0] + (dots(k) - dots(0)) * dot)
.collect();
assert_eq!(baselines, expected, "{target}");
seen.push(unit);
}
assert!(seen.contains(&LineHeightUnit::Whole4096ths));
assert!(seen.contains(&LineHeightUnit::HalfEmus));
}
#[test]
fn adds_a_list_labels_rise_in_whole_4096ths_where_the_target_holds_heights_in_them() {
use crate::quirks::LineHeightUnit;
use crate::testing::WordParts;
let numbering = r#"<w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val="q"/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr><w:rPr><w:sz w:val="26"/></w:rPr></w:lvl></w:abstractNum><w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>"#;
let paragraph = r#"<w:p><w:pPr><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr><w:spacing w:after="0" w:line="243" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="22"/></w:rPr><w:t>q</w:t></w:r></w:p>"#;
let page = r#"<w:sectPr><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/></w:sectPr>"#;
let bytes = crate::testing::docx(&format!("{}{page}", paragraph.repeat(40)))
.word_part("n1", "numbering", "numbering.xml", "numbering", numbering)
.zip()
.unwrap();
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let top = 1440 * Length::UNITS_PER_TWIP;
let whole = 63_488 * 3_175;
let exact = 126_975 * 3_175 / 2;
let mut seen = Vec::new();
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let unit = profile.quirks().line_height_unit;
seen.push(unit);
if unit != LineHeightUnit::Whole4096ths {
continue;
}
let dot = profile.device_dot().units();
let dots = |k: i64, step: i64| f_office_units::div_round(top * 512 + k * step, dot * 512);
let mut baselines = first_page_baselines(&package, target);
baselines.dedup();
assert_eq!(baselines.len(), 40, "{target}");
let expected: Vec<i64> = (0..40)
.map(|k| baselines[0] + (dots(k, whole) - dots(0, whole)) * dot)
.collect();
assert_eq!(baselines, expected, "{target}");
let off = (0..40).filter(|&k| {
let near = [exact - 1024, exact + 1024].map(|step| dots(k, step));
near.iter().all(|&d| d != dots(k, whole))
});
assert!(off.count() > 0, "{target}");
}
assert!(seen.contains(&LineHeightUnit::Whole4096ths));
}
fn first_page_baselines(package: &f_office_opc::Package, target: crate::Target) -> Vec<i64> {
page_baselines(package, target, 0)
}
fn page_baselines(package: &f_office_opc::Package, target: crate::Target, page: usize) -> Vec<i64> {
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let list = crate::layout(package, (&Em2048, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
list.pages()[page]
.items()
.iter()
.filter_map(|item| match item {
Item::Glyphs(run) => run.glyphs.first().map(|g| g.y.units()),
_ => None,
})
.collect()
}
#[test]
fn adds_lengths_of_whole_twips_down_the_page_in_the_targets_unit() {
use crate::quirks::{ExactSpacedBottom, TwipLengthUnit};
let cases = [("exact", 43, 0), ("exact", 40, 43), ("atLeast", 43, 0)];
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let top = 1440 * Length::UNITS_PER_TWIP;
let rest_parts = 512;
let rest_per_part = 3_175;
let parts = |twips: i64| f_office_units::div_round(twips * 4_096, 20);
let mut seen = Vec::new();
for (rule, line, after) in cases {
let paragraph = format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="{after}" w:line="{line}" w:lineRule="{rule}"/></w:pPr><w:r><w:rPr><w:sz w:val="2"/></w:rPr><w:t>q</w:t></w:r></w:p>"#
);
let body = paragraph.repeat(40);
let bytes = crate::tests::package(&body, "", page);
let package =
f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let steps = |unit: TwipLengthUnit| match unit {
TwipLengthUnit::Nearest4096ths => {
(parts(line) * rest_per_part, parts(after) * rest_per_part)
}
TwipLengthUnit::HalfEmus => {
let twip = Length::UNITS_PER_TWIP * rest_parts;
(line * twip, after * twip)
}
};
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let unit = profile.quirks().twip_length_unit;
let dot = profile.device_dot().units();
let parts_rounded = rule == "exact"
&& after > 0
&& profile.quirks().exact_spaced_bottom == ExactSpacedBottom::PartsRounded;
let dots = |unit: TwipLengthUnit, k: i64| {
let (line, after) = steps(unit);
let bottom = top * rest_parts + k * (line + after);
let bottom = if parts_rounded { bottom } else { bottom + line };
f_office_units::div_round(bottom, dot * rest_parts)
};
let moves = |unit: TwipLengthUnit| -> Vec<i64> {
(0..40).map(|k| dots(unit, k) - dots(unit, 0)).collect()
};
let apart = moves(TwipLengthUnit::Nearest4096ths) != moves(TwipLengthUnit::HalfEmus);
assert!(
apart || unit == TwipLengthUnit::HalfEmus,
"{rule} {line} {after} {target}"
);
let baselines = first_page_baselines(&package, target);
assert_eq!(baselines.len(), 40, "{rule} {line} {after} {target}");
let expected: Vec<i64> = moves(unit)
.iter()
.map(|moved| baselines[0] + moved * dot)
.collect();
assert_eq!(baselines, expected, "{rule} {line} {after} {target}");
seen.push(unit);
}
}
assert!(seen.contains(&TwipLengthUnit::Nearest4096ths));
assert!(seen.contains(&TwipLengthUnit::HalfEmus));
}
fn stack_moves(top: i64, (first, line): (i64, i64), dot: i64, count: i64) -> Vec<i64> {
let dots = |k: i64| f_office_units::div_round(top + (k + first) * line, dot);
(0..count).map(|k| dots(k) - dots(0)).collect()
}
fn check_long_stack(
package: &f_office_opc::Package,
target: crate::Target,
(twips, parts, count): (i64, i64, i64),
) -> crate::quirks::TwipLengthUnit {
use crate::quirks::{ExactBaseline, TwipLengthUnit};
let profile = crate::profile::Profile::of(target).unwrap();
let quirks = profile.quirks();
let dot = profile.device_dot().units();
let height = twips * Length::UNITS_PER_TWIP;
let tops = quirks.exact_baseline == ExactBaseline::BoxForms && (height % dot) * 2 < dot;
let first = i64::from(!tops);
let top = 720 * Length::UNITS_PER_TWIP * 512;
let whole_parts = stack_moves(top, (first, parts * 3_175), dot * 512, count);
let exactly = stack_moves(top, (first, height * 512), dot * 512, count);
let unit = quirks.twip_length_unit;
assert!(
whole_parts != exactly || unit == TwipLengthUnit::HalfEmus,
"{twips} {target}"
);
let baselines = first_page_baselines(package, target);
assert_eq!(
baselines.len(),
usize::try_from(count).unwrap(),
"{twips} {target}"
);
let expected = match unit {
TwipLengthUnit::Nearest4096ths => whole_parts,
TwipLengthUnit::HalfEmus => exactly,
};
let expected: Vec<i64> = expected
.iter()
.map(|moved| baselines[0] + moved * dot)
.collect();
assert_eq!(baselines, expected, "{twips} {target}");
unit
}
#[test]
fn carries_a_long_sum_of_lengths_of_whole_twips_across_half_a_dot() {
use crate::quirks::TwipLengthUnit;
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="720" w:right="1440" w:bottom="1440" w:left="1440" w:header="360" w:footer="720"/>"#;
let mut seen = Vec::new();
for case in [(47, 9_626, 288), (41, 8_397, 330)] {
let (twips, _, count) = case;
let exact = format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="0" w:line="{twips}" w:lineRule="exact"/></w:pPr><w:r><w:rPr><w:sz w:val="2"/></w:rPr><w:t>q</w:t></w:r></w:p>"#
);
let body = exact.repeat(usize::try_from(count).unwrap());
let bytes = crate::tests::package(&body, "", page);
let package =
f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
for target in crate::Target::all() {
seen.push(check_long_stack(&package, target, case));
}
}
assert!(seen.contains(&TwipLengthUnit::Nearest4096ths));
}
#[test]
fn starts_the_twips_rest_again_on_the_page_of_a_new_section() {
use crate::quirks::TwipLengthUnit;
let spacing = r#"<w:spacing w:before="0" w:after="0" w:line="43" w:lineRule="exact"/>"#;
let run = r#"<w:r><w:rPr><w:sz w:val="2"/></w:rPr><w:t>q</w:t></w:r>"#;
let margins = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let exact = format!("<w:p><w:pPr>{spacing}</w:pPr>{run}</w:p>");
let end = format!(
r#"<w:p><w:pPr>{spacing}<w:sectPr><w:type w:val="nextPage"/>{margins}</w:sectPr></w:pPr>{run}</w:p>"#
);
let body = format!("{}{end}{}", exact.repeat(2), exact.repeat(40));
let bytes = crate::tests::package(&body, "", margins);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let top = 1440 * Length::UNITS_PER_TWIP * 512;
let mut seen = Vec::new();
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let unit = profile.quirks().twip_length_unit;
let dot = profile.device_dot().units();
let line = match unit {
TwipLengthUnit::Nearest4096ths => 8_806 * 3_175,
TwipLengthUnit::HalfEmus => 43 * Length::UNITS_PER_TWIP * 512,
};
let moves = stack_moves(top, (1, line), dot * 512, 40);
let baselines = page_baselines(&package, target, 1);
assert_eq!(baselines.len(), 40, "{target}");
let expected: Vec<i64> = moves
.iter()
.map(|moved| baselines[0] + moved * dot)
.collect();
assert_eq!(baselines, expected, "{target}");
seen.push(unit);
}
assert!(seen.contains(&TwipLengthUnit::Nearest4096ths));
}
#[test]
fn carries_the_twips_rest_past_a_line_of_whole_4096ths() {
use crate::quirks::{LineHeightUnit, TwipLengthUnit};
let single = r#"<w:p><w:pPr><w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="2"/></w:rPr><w:t>q</w:t></w:r></w:p>"#;
let exact = r#"<w:p><w:pPr><w:spacing w:before="0" w:after="0" w:line="43" w:lineRule="exact"/></w:pPr><w:r><w:rPr><w:sz w:val="2"/></w:rPr><w:t>q</w:t></w:r></w:p>"#;
let body = format!("{single}{}", exact.repeat(40));
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let bytes = crate::tests::package(&body, "", page);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let top = 1440 * Length::UNITS_PER_TWIP * 512 + 5_000 * 3_175;
let mut tested = 0;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let quirks = profile.quirks();
if quirks.twip_length_unit != TwipLengthUnit::Nearest4096ths
|| quirks.line_height_unit != LineHeightUnit::Whole4096ths
{
continue;
}
let dot = profile.device_dot().units() * 512;
let moves = |line: i64| -> Vec<i64> {
let dots = |k: i64| f_office_units::div_round(top + (k + 1) * line, dot);
(0..40).map(|k| dots(k) - dots(0)).collect()
};
let entered = moves(8_806 * 3_175);
assert_ne!(
entered,
moves(43 * Length::UNITS_PER_TWIP * 512),
"{target}"
);
let baselines: Vec<i64> = first_page_baselines(&package, target)
.into_iter()
.skip(1)
.collect();
assert_eq!(baselines.len(), 40, "{target}");
let expected: Vec<i64> = entered
.iter()
.map(|moved| baselines[0] + moved * (dot / 512))
.collect();
assert_eq!(baselines, expected, "{target}");
tested += 1;
}
assert!(tested > 0);
}
#[test]
fn carries_the_height_rest_from_row_to_row_of_a_table() {
use crate::quirks::{LineHeightUnit, TwipLengthUnit};
fn baselines_in(items: &[Item], found: &mut Vec<i64>) {
for item in items {
match item {
Item::Glyphs(run) => found.extend(run.glyphs.first().map(|g| g.y.units())),
Item::Group(group) => baselines_in(&group.items, found),
_ => {}
}
}
}
let line = r#"<w:p><w:pPr><w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="22"/></w:rPr><w:t>q</w:t></w:r></w:p>"#;
let row = format!(
r#"<w:tr><w:tc><w:tcPr><w:tcW w:w="5000" w:type="dxa"/></w:tcPr>{line}</w:tc></w:tr>"#
);
let table = format!(
r#"<w:tbl><w:tblPr><w:tblW w:w="5000" w:type="dxa"/><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="108" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr><w:tblGrid><w:gridCol w:w="5000"/></w:tblGrid>{}</w:tbl><w:p/>"#,
row.repeat(48)
);
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let bytes = crate::tests::package(&table, "", page);
let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let top = 1440 * Length::UNITS_PER_TWIP * 512;
let mut tested = 0;
for target in crate::Target::all() {
let profile = crate::profile::Profile::of(target).unwrap();
let quirks = profile.quirks();
if quirks.twip_length_unit != TwipLengthUnit::Nearest4096ths
|| quirks.line_height_unit != LineHeightUnit::Whole4096ths
{
continue;
}
let dot = profile.device_dot().units() * 512;
let moves = |row: i64| -> Vec<i64> {
let dots = |k: i64| f_office_units::div_round(top + (k + 1) * row, dot);
(0..48).map(|k| dots(k) - dots(0)).collect()
};
let carried = moves(55_000 * 3_175);
assert_ne!(carried, moves(341_064 * 512), "{target}");
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let list = crate::layout(&package, (&Em2048, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
let mut baselines = Vec::new();
baselines_in(list.pages()[0].items(), &mut baselines);
baselines.truncate(48);
assert_eq!(baselines.len(), 48, "{target}");
let expected: Vec<i64> = carried
.iter()
.map(|moved| baselines[0] + moved * (dot / 512))
.collect();
assert_eq!(baselines, expected, "{target}");
tested += 1;
}
assert!(tested > 0);
}
#[test]
fn adds_the_heights_of_lines_of_a_minimum_below_their_single_line_as_lines_of_single_spacing() {
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let baselines = |spacing: &str, target| {
let paragraph = format!(
r#"<w:p><w:pPr><w:spacing w:after="0" {spacing}/></w:pPr><w:r><w:rPr><w:sz w:val="22"/></w:rPr><w:t>q</w:t></w:r></w:p>"#
);
let bytes = crate::tests::package(¶graph.repeat(48), "", page);
let package =
f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let list = crate::layout(&package, (&Em2048, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
list.pages()[0]
.items()
.iter()
.filter_map(|item| match item {
Item::Glyphs(run) => run.glyphs.first().map(|g| g.y.units()),
_ => None,
})
.collect::<Vec<i64>>()
};
for target in crate::Target::all() {
let single = baselines(r#"w:line="240" w:lineRule="auto""#, target);
assert_eq!(single.len(), 48, "{target}");
assert_eq!(
baselines(r#"w:line="20" w:lineRule="atLeast""#, target),
single,
"{target}"
);
}
}
#[test]
fn rounds_a_line_in_a_table_cell_with_its_space_after_where_the_target_holds_it() {
use std::collections::BTreeSet;
use crate::layout::testing::Fake;
use crate::quirks::CellSpaceAfter;
let paragraph = |letter: char, after: i64| {
format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="{after}" w:line="240" w:lineRule="auto"/></w:pPr><w:r><w:rPr><w:sz w:val="22"/></w:rPr><w:t>{letter}</w:t></w:r></w:p>"#
)
};
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let packages: Vec<Vec<u8>> = (0..12)
.map(|k| {
let cell = paragraph('a', 160 + k) + ¶graph('b', 0);
let body = table("", &[4000], &[("", &[cell.as_str()])]) + "<w:p/>";
crate::tests::package(&body, "", page)
})
.collect();
let mut seen = Vec::new();
for target in crate::Target::all() {
let held = crate::profile::Profile::of(target)
.unwrap()
.quirks()
.cell_space_after;
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let baselines: BTreeSet<i64> = packages
.iter()
.map(|bytes| {
let limits = f_office_opc::Limits::default();
let package = f_office_opc::Package::open(bytes, &limits).unwrap();
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
find(&list, 'a').2
})
.collect();
match held {
CellSpaceAfter::Outside => assert_eq!(baselines.len(), 1, "{target}"),
CellSpaceAfter::Held => {
assert_eq!(baselines.len(), 2, "{target}");
assert_eq!(
baselines.last().unwrap() - baselines.first().unwrap(),
1,
"{target}"
);
}
}
seen.push(held);
}
assert!(seen.contains(&CellSpaceAfter::Outside));
assert!(seen.contains(&CellSpaceAfter::Held));
}
#[test]
fn rounds_an_at_least_line_with_its_space_after_where_the_target_holds_it() {
use crate::layout::testing::Fake;
use crate::quirks::AtLeastSpaceAfter;
let baseline = |after: i64, target| {
let body = format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="{after}" w:line="181" w:lineRule="atLeast"/></w:pPr><w:r><w:rPr><w:sz w:val="17"/></w:rPr><w:t>a</w:t></w:r></w:p><w:p><w:r><w:t>b</w:t></w:r></w:p>"#
);
let page = r#"<w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"/>"#;
let bytes = crate::tests::package(&body, "", page);
let options = crate::Options {
configuration: crate::Configuration::of(target),
..crate::Options::default()
};
let limits = f_office_opc::Limits::default();
let package = f_office_opc::Package::open(&bytes, &limits).unwrap();
let list = crate::layout(&package, (&Fake, &crate::ArtPictures::new()), &options)
.unwrap()
.value;
find(&list, 'a').2
};
let mut seen = Vec::new();
for target in crate::Target::all() {
let held = crate::profile::Profile::of(target)
.unwrap()
.quirks()
.at_least_space_after;
let lower = baseline(120, target) - baseline(0, target);
match held {
AtLeastSpaceAfter::Outside => assert_eq!(lower, 0, "{target}"),
AtLeastSpaceAfter::Held => assert_eq!(lower, 1, "{target}"),
}
seen.push(held);
}
assert!(seen.contains(&AtLeastSpaceAfter::Outside));
assert!(seen.contains(&AtLeastSpaceAfter::Held));
}
#[test]
fn holds_the_space_after_of_an_at_least_line_of_single_spacing_where_the_device_holds_it() {
use crate::layout::testing::{on_base_with, pass_with};
use crate::numbering::Numbering;
use crate::quirks::{AtLeastSpaceAfter, MultipleBaseline};
let baseline = |rule: &str, held: AtLeastSpaceAfter| {
let body = format!(
r#"<w:p><w:pPr><w:spacing w:before="0" w:after="120" {rule}/></w:pPr><w:r><w:rPr><w:sz w:val="20"/></w:rPr><w:t>a</w:t></w:r></w:p><w:p><w:r><w:t>b</w:t></w:r></w:p>"#
);
let target = on_base_with(|p| {
p.with_variant(MultipleBaseline::DescentAboveExtra)
.with_variant(held)
});
let dot = target.0.dots(1);
let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
(pass.lines[0].baseline, dot)
};
let at_least = r#"w:line="20" w:lineRule="atLeast""#;
let single = r#"w:line="240" w:lineRule="auto""#;
let (held, dot) = baseline(at_least, AtLeastSpaceAfter::Held);
assert_eq!(held, baseline(single, AtLeastSpaceAfter::Held).0);
let (outside, _) = baseline(at_least, AtLeastSpaceAfter::Outside);
assert_eq!(held - outside, dot);
}
#[test]
fn a_ligature_of_right_to_left_text_writes_the_letters_of_its_group_in_their_order() {
use crate::quirks::ShapedGlyphText;
let body = "<w:p><w:pPr><w:bidi/></w:pPr><w:r><w:rPr><w:rtl/></w:rPr><w:t>\u{0628}\u{0644}\u{0627}</w:t></w:r></w:p>";
let texts = |value: ShapedGlyphText| {
let (device, quirks) = on_base_with(|p| p.with_variant(value));
let settings = crate::text::ShaperSettings::base_with(|p| p.with_variant(value));
let pass = crate::layout::testing::pass_on(
body,
(NORMAL, None),
None,
Numbering::default(),
(device, quirks, crate::layout::EarlierPass::default()),
&(
&crate::layout::testing::LamAlef as &dyn crate::Fonts,
settings,
),
);
let mut texts = Vec::new();
for item in pass.list.pages()[0].items() {
if let Item::Glyphs(run) = item {
for glyph in &run.glyphs {
let range = glyph.text_start as usize..glyph.text_end as usize;
texts.push((glyph.id, run.text.get(range).unwrap_or("").to_owned()));
}
}
}
texts
};
for value in [
ShapedGlyphText::ClusterLetters,
ShapedGlyphText::LowestMappedCharacter,
] {
assert_eq!(
texts(value),
[
(900, "\u{0644}\u{0627}".to_owned()),
(0x0628, "\u{0628}".to_owned())
]
);
}
}