6ef13b2cc8a6
vertical.rs
//! Vertical sections (`w:textDirection` in `w:sectPr`): lines run down the page and stack from
//! the right (`tbRl`), or run up and stack from the left (`btLr`).
use crate::*;
use wordcraft_doc::Pos;
fn lay(doc: &Document) -> DocLayout {
let mut c = LayoutCache::new();
layout(doc, &mut c, &LayoutOptions::default())
}
fn vertical_doc(dir: TextDirection) -> Document {
let text = "Vertical sections lay their lines out down the page, one after another from right to left. ".repeat(4);
let mut d = Document::from_text(&format!("{text}\næ¥æ¬èªã®ããã¹ã"));
d.last_section.text_direction = dir;
d
}
/// Body lines on page 0: (x, y, turn) of each piece.
fn body_lines(l: &DocLayout) -> Vec<(f32, f32, TextDirection)> {
l.pages[0]
.items
.iter()
.filter_map(|it| match it {
Placed::Lines { story: StoryRef::Body, x, y, turn, .. } => Some((*x, *y, *turn)),
_ => None,
})
.collect()
}
#[test]
fn top_to_bottom_section_stacks_lines_from_the_right() {
crate::test_fonts();
let d = vertical_doc(TextDirection::Down);
let l = lay(&d);
let page = &l.pages[0];
// The page keeps its size; the text area is the real one (inside the margins).
assert_eq!((page.w, page.h), (d.last_section.page_w, d.last_section.page_h));
assert!(page.body.x >= d.last_section.margin_left - 0.5 && page.body.right() <= page.w - d.last_section.margin_right + 0.5);
let lines = body_lines(&l);
assert!(lines.len() >= 2);
assert!(lines.iter().all(|(.., t)| *t == TextDirection::Down));
// The first paragraph's first line is the rightmost, the next paragraph left of it.
let p0 = Pos::body(0, 0);
let c0 = l.caret(&p0).unwrap();
let c1 = l.caret(&Pos::body(1, 0)).unwrap();
assert!(c1.x < c0.x, "second paragraph left of the first: {c1:?} vs {c0:?}");
assert!(c0.x <= page.body.right() + 0.5 && c0.top >= page.body.y - 0.5);
// Characters progress downward along a line; the caret lies across it.
let c5 = l.caret(&Pos::body(0, 2)).unwrap();
assert!(c5.top > c0.top && (c5.x - c0.x).abs() < 0.5, "{c5:?} vs {c0:?}");
assert!(c0.width > c0.height);
// Line two of the first paragraph is left of line one.
let p = d.para(StoryRef::Body, &Path::top(0)).unwrap();
let wrap = l.index.get(&(StoryRef::Body, Path::top(0))).map(|v| v[0]).unwrap();
let Placed::Lines { para, .. } = &l.pages[wrap.0].items[wrap.1] else { panic!() };
assert!(para.lines.len() >= 2 && para.lines[1].start < p.len());
let c_next = l.caret(&Pos::body(0, para.lines[1].start + 1)).unwrap();
assert!(c_next.x < c0.x - 1.0);
// Clicking a caret position finds it again.
for off in [0, 2, para.lines[1].start + 1] {
let c = l.caret(&Pos::body(0, off)).unwrap();
let hit = l.hit(c.page, c.x + c.width / 2.0, c.top + 0.2, StoryRef::Body).unwrap();
assert_eq!(hit.off, off);
}
// Clicking in the empty space below a short line lands on that line (its end).
let c_cjk = l.caret(&Pos::body(1, 0)).unwrap();
let below = l.hit(0, c_cjk.x + c_cjk.width / 2.0, page.body.bottom() - 2.0, StoryRef::Body).unwrap();
assert_eq!(below.path, Path::top(1));
}
#[test]
fn bottom_to_top_section_stacks_lines_from_the_left() {
crate::test_fonts();
let l = lay(&vertical_doc(TextDirection::Up));
assert!(body_lines(&l).iter().all(|(.., t)| *t == TextDirection::Up));
let c0 = l.caret(&Pos::body(0, 0)).unwrap();
let c1 = l.caret(&Pos::body(1, 0)).unwrap();
let c5 = l.caret(&Pos::body(0, 2)).unwrap();
assert!(c1.x > c0.x, "second paragraph right of the first");
assert!(c5.top < c0.top, "characters progress upward");
}
#[test]
fn east_asian_characters_stand_upright_in_top_to_bottom_text() {
crate::test_fonts();
let l = lay(&vertical_doc(TextDirection::Down));
let d = vertical_doc(TextDirection::Down);
let draws = display::page_display(&d, &l.pages[0], &Default::default());
let turned: Vec<&Vec<display::Draw>> =
draws.iter().filter_map(|x| if let display::Draw::Turned { items, .. } = x { Some(items) } else { None }).collect();
let text_of = |ds: &[display::Draw]| -> String {
ds.iter()
.map(|x| match x {
display::Draw::Glyphs { text, .. } => text.clone(),
display::Draw::Rotated { items, .. } => {
items.iter().map(|i| if let display::Draw::Glyphs { text, .. } = i { text.clone() } else { String::new() }).collect()
}
_ => String::new(),
})
.collect()
};
// Each ideograph turned back upright on its own; the Latin text lies with the line.
let cjk = turned.iter().find(|ds| text_of(ds).contains('æ¥')).expect("the Japanese line is drawn turned");
let upright = cjk.iter().filter(|x| matches!(x, display::Draw::Rotated { spin, .. } if spin.deg == 270.0)).count();
assert_eq!(upright, "æ¥æ¬èªã®ããã¹ã".chars().count());
assert!(turned.iter().any(|ds| text_of(ds).contains("Vertical") && ds.iter().all(|x| !matches!(x, display::Draw::Rotated { .. }))));
}
#[test]
fn headers_stay_horizontal_and_a_vertical_section_starts_a_page() {
crate::test_fonts();
use wordcraft_doc::section::SectionStart;
let mut d = Document::from_text("Horizontal\nVertical");
// Section 1 ends after paragraph 0 (continuous), section 2 is vertical.
let first = SectionProps { start: SectionStart::Continuous, ..Default::default() };
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().section = Some(Box::new(first));
d.last_section.start = SectionStart::Continuous;
d.last_section.text_direction = TextDirection::Down;
let l = lay(&d);
assert_eq!(l.pages.len(), 2);
assert_eq!(l.caret(&Pos::body(0, 0)).unwrap().width, 0.0);
assert!(l.caret(&Pos::body(1, 0)).unwrap().width > 0.0);
}
/// #2038 in a vertical section, where every line is turned: a click outside a floating table
/// goes to the text beside or after it, not to a cell of the table nearer the click.
#[test]
fn a_click_outside_a_floating_table_in_a_vertical_section_skips_its_cells() {
use wordcraft_doc::para::Anchor;
use wordcraft_doc::props::TableFloat;
let mut d = Document::from_text("Text");
d.last_section.text_direction = TextDirection::Down;
let mut t = wordcraft_doc::Table::new(3, 2, 200.0);
t.props.float = Some(TableFloat {
h_rel: Anchor::Column,
v_rel: Anchor::Paragraph,
x: 150.0,
dist: [9.0, 0.0, 9.0, 0.0],
overlap: true,
..Default::default()
});
d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap();
let l = lay(&d);
let page = &l.pages[0];
assert_eq!(page.float_tables, [Path::top(0)]);
let area =
page.items.iter().filter_map(|it| if let Placed::Cell { rect, .. } = it { Some(*rect) } else { None }).reduce(|a, b| a.union(&b)).unwrap();
let turned: Vec<(&Path, Rect)> = page
.items
.iter()
.filter_map(|it| match it {
Placed::Lines { story: StoryRef::Body, path, .. } => it.turned_bounds().map(|r| (path, r)),
_ => None,
})
.collect();
assert!(!turned.is_empty() && hit::page_lines(page, StoryRef::Body).is_empty(), "only turned lines on the page");
let band = |v: f32, lo: f32, len: f32| (lo - v).max(v - lo - len).max(0.0);
let dist = |r: &Rect, x: f32, y: f32| band(x, r.x, r.w).hypot(band(y, r.y, r.h));
// Points just outside the table, off every line, whose nearest line is one of the table's.
let (cx, cy) = (area.x + area.w / 2.0, area.y + area.h / 2.0);
let points: Vec<(f32, f32)> = [(area.x - 12.0, cy), (area.right() + 12.0, cy), (cx, area.y - 12.0), (cx, area.bottom() + 12.0)]
.into_iter()
.filter(|&(x, y)| page.body.contains(Point::new(x, y)) && turned.iter().all(|(_, r)| !r.expand(2.0).contains(Point::new(x, y))))
.filter(|&(x, y)| turned.iter().min_by(|a, b| dist(&a.1, x, y).total_cmp(&dist(&b.1, x, y))).is_some_and(|(p, _)| p.0.len() > 1))
.collect();
assert!(!points.is_empty(), "a click there would land in the table: {area:?}");
for (x, y) in points {
assert_eq!(l.hit(0, x, y, StoryRef::Body).unwrap().path, Path::top(1), "a click at ({x}, {y})");
}
// Inside the table a click still lands in a cell.
assert!(l.hit(0, cx, cy, StoryRef::Body).unwrap().path.0.len() > 1);
}
No file is open. Choose one in the tree, or press Ctrl+O (âO on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.