mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-02 02:36:14 -06:00
text: a tab stop past the right margin sits at it (#1663)
This commit is contained in:
@@ -179,7 +179,7 @@ pub(crate) fn break_lines(p: &ParaShaped, params: &BreakParams) -> Vec<RawLine>
|
||||
break;
|
||||
}
|
||||
let adv = if c == '\t' {
|
||||
tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i))
|
||||
tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i), avail)
|
||||
} else {
|
||||
p.cells.get(i).map(|c| c.adv).unwrap_or(0.0)
|
||||
};
|
||||
@@ -305,11 +305,15 @@ pub(crate) fn tab_segment(cells: &[Cell], i: usize) -> (f64, f64) {
|
||||
/// Advance of a tab at `x` (from the start edge) to the next stop, placing the text after it
|
||||
/// (`seg`: its width, and its width before the decimal point) as the stop aligns it but never
|
||||
/// moving back, as PowerPoint does; past the last stop, to the next default stop.
|
||||
pub(crate) fn tab_advance(x: f64, stops: &[Tab], default_tab: f64, seg: (f64, f64)) -> f64 {
|
||||
///
|
||||
/// `avail` is the line's width from the start edge: a stop set past the right edge acts as one at
|
||||
/// the edge, as PowerPoint treats it (a right-aligned stop there ends the text at the margin).
|
||||
pub(crate) fn tab_advance(x: f64, stops: &[Tab], default_tab: f64, seg: (f64, f64), avail: f64) -> f64 {
|
||||
// `stops` is in position order (`tabs_of`): the next stop is the first past x.
|
||||
// A NaN `x` (a NaN spacing upstream) skips the lookup and takes the finite default-stop path.
|
||||
if let Some(s) = stops.get(stops.partition_point(|s| s.pos <= x + 0.5)).filter(|_| !x.is_nan()) {
|
||||
let to = s.pos - x;
|
||||
let pos = if avail.is_finite() && avail > 0.0 { s.pos.min(avail) } else { s.pos };
|
||||
let to = (pos - x).max(0.0);
|
||||
return match s.align {
|
||||
TabAlign::Left => to,
|
||||
TabAlign::Right => (to - seg.0).max(0.0),
|
||||
@@ -329,7 +333,16 @@ mod tab_tests {
|
||||
#[test]
|
||||
fn a_nan_position_takes_the_finite_default_stop() {
|
||||
let stops = [Tab { pos: 100.0, align: TabAlign::Left }];
|
||||
assert!(tab_advance(f64::NAN, &stops, 72.0, (0.0, 0.0)).is_finite());
|
||||
assert_eq!(tab_advance(10.0, &stops, 72.0, (0.0, 0.0)), 90.0);
|
||||
assert!(tab_advance(f64::NAN, &stops, 72.0, (0.0, 0.0), 600.0).is_finite());
|
||||
assert_eq!(tab_advance(10.0, &stops, 72.0, (0.0, 0.0), 600.0), 90.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stop_past_the_line_ends_at_its_edge() {
|
||||
let stops = [Tab { pos: 700.0, align: TabAlign::Right }];
|
||||
// Right-aligned text after the tab ends at the line's right edge, not 700.
|
||||
assert_eq!(tab_advance(0.0, &stops, 72.0, (100.0, 100.0), 600.0), 500.0);
|
||||
// A stop inside the line keeps its own position.
|
||||
assert_eq!(tab_advance(0.0, &stops, 72.0, (100.0, 100.0), 800.0), 600.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,7 +376,7 @@ fn line_extent(p: &ParaShaped, rl: &RawLine) -> (f64, f64, f64) {
|
||||
}
|
||||
|
||||
/// Logical advance of each cell on the line (tabs measure from the start edge) and their sum.
|
||||
fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64) -> (Vec<f64>, f64) {
|
||||
fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64, avail: f64) -> (Vec<f64>, f64) {
|
||||
let p = env.p;
|
||||
let line_cells = p.cells.get(rl.start..rl.end).unwrap_or(&[]);
|
||||
let n = rl.end.saturating_sub(rl.start);
|
||||
@@ -388,7 +388,7 @@ fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64) -> (Vec<f6
|
||||
let a = if shy_end && k + 1 == n {
|
||||
p.faces.get(c.face).zip(p.styles.get(c.style)).map(|(f, st)| measure_str(f, st.size, "-").0).unwrap_or(0.0)
|
||||
} else if c.ch == '\t' {
|
||||
tab_advance(x0 + x, env.tabs, p.props.default_tab.unwrap_or(72.0), tab_segment(&p.cells, rl.start + k))
|
||||
tab_advance(x0 + x, env.tabs, p.props.default_tab.unwrap_or(72.0), tab_segment(&p.cells, rl.start + k), avail)
|
||||
} else {
|
||||
c.adv
|
||||
};
|
||||
@@ -520,7 +520,7 @@ fn place_line(env: &ParaEnv, pi: usize, rl: &RawLine, li: usize, is_last: bool,
|
||||
let avail = (g.col_w - x0 - env.mr).max(0.0);
|
||||
let n = rl.end.saturating_sub(rl.start);
|
||||
let line_cells = p.cells.get(rl.start..rl.end).unwrap_or(&[]);
|
||||
let (mut adv, width) = line_advances(env, rl, is_last, x0);
|
||||
let (mut adv, width) = line_advances(env, rl, is_last, x0, avail);
|
||||
let align = props.align.unwrap_or(Align::Left);
|
||||
let slack = (avail - width).max(0.0);
|
||||
let stretch = !is_last && g.wrap && slack > 0.0;
|
||||
|
||||
@@ -1416,6 +1416,17 @@ fn tab_stops_align_the_text_after_them() {
|
||||
assert!((l.lines[0].edges[4].0 - x0(&l) - 72.0).abs() < 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_tab_stop_past_the_margin_ends_the_text_at_the_margin() {
|
||||
// #1663: a layout may set a right tab stop a little past the text area's right edge.
|
||||
// PowerPoint treats it as one at the margin: the text after it stays on one line and ends
|
||||
// at the right edge. DeckCraft started it too far right and wrapped the rest.
|
||||
let l = tabbed("x\tCorrie Ten Boom", &[(700.0, "r")], 600.0);
|
||||
assert_eq!(l.lines.len(), 1, "{:?}", l.lines.iter().map(|x| (x.start, x.end, x.extent)).collect::<Vec<_>>());
|
||||
let right = l.inner.x0 + l.inner.width();
|
||||
assert!((l.lines[0].extent.1 - right).abs() < 0.01, "ends at {:?}, margin {right}", l.lines[0].extent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_tabs_over_many_stops_lay_out_by_position() {
|
||||
// 20,000 stops given out of order, and 2,000 tabs: each tab finds the next stop by position.
|
||||
|
||||
Reference in New Issue
Block a user