mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-19 08:55:26 +00:00
Table borders no longer look blue and uneven (#6048)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
54e82f7131
commit
4e2a5c2b8f
5 files changed
+238
-9
No files matched your search
@@ -703,12 +703,43 @@ fn draw_line(ctx: &mut RenderContext, fr: &Frame, it: &Draw) {
|
||||
stroke_segment(ctx, (x0, y0, x1, y1), (-nx, -ny));
|
||||
}
|
||||
_ => {
|
||||
let (st, off) = match snap_rule(fr.view, (x0, y0, x1, y1), w) {
|
||||
Some((w_px, off)) => (kurbo::Stroke { width: w_px / fr.view.as_coeffs()[0], ..st }, off),
|
||||
None => (st, (0.0, 0.0)),
|
||||
};
|
||||
ctx.set_stroke(st);
|
||||
stroke_segment(ctx, (x0, y0, x1, y1), (0.0, 0.0));
|
||||
stroke_segment(ctx, (x0, y0, x1, y1), off);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Snap a horizontal or vertical rule to the device pixel grid so every border of one nominal
|
||||
/// width covers the same number of pixels at any zoom (#5967). Returns the stroke width in device
|
||||
/// pixels and the offset, in points, that moves the line onto the snapped spot; `None` for slanted
|
||||
/// lines or a view that rotates or skews.
|
||||
fn snap_rule(view: Affine, (x0, y0, x1, y1): (f64, f64, f64, f64), width: f64) -> Option<(f64, (f64, f64))> {
|
||||
if !is_upright(view) {
|
||||
return None;
|
||||
}
|
||||
let [k, _, _, _, tx, ty] = view.as_coeffs();
|
||||
if y0 == y1 && x0 != x1 {
|
||||
let (w_px, c) = snap_hairline(y0 * k + ty, width * k);
|
||||
Some((w_px, (0.0, (c - ty) / k - y0)))
|
||||
} else if x0 == x1 && y0 != y1 {
|
||||
let (w_px, c) = snap_hairline(x0 * k + tx, width * k);
|
||||
Some((w_px, ((c - tx) / k - x0, 0.0)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A line `width_px` wide centred at device coordinate `centre`, moved so both edges fall on whole
|
||||
/// pixels. Returns the width, made a whole number of pixels (at least one), and the new centre.
|
||||
fn snap_hairline(centre: f64, width_px: f64) -> (f64, f64) {
|
||||
let w = width_px.round().max(1.0);
|
||||
(w, (centre - w / 2.0).round() + w / 2.0)
|
||||
}
|
||||
|
||||
/// Stroke the line from (`x0`, `y0`) to (`x1`, `y1`), moved by (`ox`, `oy`).
|
||||
fn stroke_segment(ctx: &mut RenderContext, (x0, y0, x1, y1): (f64, f64, f64, f64), (ox, oy): (f64, f64)) {
|
||||
let mut p = BezPath::new();
|
||||
|
||||
@@ -1938,6 +1938,31 @@ fn paths_far_past_every_canvas_are_not_flattened() {
|
||||
assert!(!path_extent_ok(Affine::IDENTITY, &line(f64::NAN)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rules_of_one_width_cover_the_same_whole_pixels_at_every_zoom() {
|
||||
for zoom in [0.5, 0.6, 0.75] {
|
||||
for ppp in [1.0, 1.25] {
|
||||
let k = zoom * ppp * 96.0 / 72.0;
|
||||
let view = Affine::new([k, 0.0, 0.0, k, 37.3, 12.9]);
|
||||
let mut widths = Vec::new();
|
||||
for i in 0..20 {
|
||||
let pos = 10.0 + f64::from(i) * 17.37;
|
||||
let (w_h, (_, oy)) = snap_rule(view, (5.0, pos, 400.0, pos), 0.5).unwrap();
|
||||
let (w_v, (ox, _)) = snap_rule(view, (pos, 5.0, pos, 400.0), 0.5).unwrap();
|
||||
widths.push(w_h);
|
||||
widths.push(w_v);
|
||||
for (w, centre) in [(w_h, (pos + oy) * k + 12.9), (w_v, (pos + ox) * k + 37.3)] {
|
||||
let edge = centre - w / 2.0;
|
||||
assert!((edge - edge.round()).abs() < 1e-9, "zoom {zoom} ppp {ppp}: edge {edge} is off the pixel grid");
|
||||
}
|
||||
}
|
||||
assert!(widths.iter().all(|w| *w == widths[0] && *w >= 1.0 && w.fract() == 0.0), "zoom {zoom} ppp {ppp}: {widths:?}");
|
||||
}
|
||||
}
|
||||
assert!(snap_rule(Affine::rotate(0.3), (0.0, 0.0, 9.0, 0.0), 0.5).is_none());
|
||||
assert!(snap_rule(Affine::scale(2.0), (0.0, 0.0, 9.0, 4.0), 0.5).is_none());
|
||||
}
|
||||
|
||||
/// The same three pictures that don't draw as in the PDF test: each stays a grey box, and the page's
|
||||
/// pixels are what they were before strict export existed (#6005).
|
||||
#[test]
|
||||
|
||||
@@ -64,7 +64,7 @@ use navigation::{scrollbar_page_tip, viewed_page};
|
||||
use pages::{DRAWING_GRID_MAX_LINES, DRAWING_GRID_MIN_PX};
|
||||
pub(crate) use pages::{PageKeys, dark_page, page_doc, page_screen_scale, preview_sel, screen_render_options};
|
||||
use pages::{
|
||||
drawing_grid, drawing_grid_step, gridline_cells, page_image, page_key, page_render_options, page_shows, shown_layout, texel_aligned_rect,
|
||||
drawing_grid, drawing_grid_step, gridline_segments, page_image, page_key, page_render_options, page_shows, shown_layout, texel_aligned_rect,
|
||||
view_key, visible_tiles,
|
||||
};
|
||||
pub(crate) use reading::turn_read_page;
|
||||
@@ -1278,14 +1278,12 @@ fn paint_pages(pass: PagePaint<'_>) -> PagePaintOutput {
|
||||
}
|
||||
draw_page_gridlines(app, &painter, t, page, visual_sr, page_scale);
|
||||
// The current header/footer's cells have editing guides; explicit View Gridlines
|
||||
// still outlines every table. Neither kind of guide is part of the document.
|
||||
// still outlines every table. Both are drawn only where no border is (#5967), and
|
||||
// neither kind of guide is part of the document.
|
||||
if app.session.view.table_gridlines || grid_story.is_some() {
|
||||
for rect in gridline_cells(page, app.session.view.table_gridlines, grid_story) {
|
||||
let r = Rect::from_min_size(
|
||||
pos2(visual_sr.min.x + rect.x * page_scale, visual_sr.min.y + rect.y * page_scale),
|
||||
vec2(rect.w * page_scale, rect.h * page_scale),
|
||||
);
|
||||
painter.rect_stroke(r, 0.0, Stroke::new(0.5, t.blue.linear_multiply(0.6)), egui::StrokeKind::Middle);
|
||||
let at = |p: wordcraft_geom::Point| pos2(visual_sr.min.x + p.x * page_scale, visual_sr.min.y + p.y * page_scale);
|
||||
for (a, b) in gridline_segments(page, app.session.view.table_gridlines, grid_story) {
|
||||
painter.line_segment([at(a), at(b)], Stroke::new(0.5, t.blue.linear_multiply(0.6)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,6 +171,84 @@ pub(super) fn gridline_cells(page: &Page, all: bool, editing: Option<StoryRef>)
|
||||
})
|
||||
}
|
||||
|
||||
/// The table gridline segments to draw on a page, in document points: the edges of the
|
||||
/// [`gridline_cells`] minus the parts a printed border already draws. Word shows gridlines only
|
||||
/// where a cell has no border; drawn over a border, the thin blue guide tinted every bordered
|
||||
/// table blue and made its lines look uneven when zoomed out (#5967).
|
||||
pub(super) fn gridline_segments(page: &Page, all: bool, editing: Option<StoryRef>) -> Vec<(wordcraft_geom::Point, wordcraft_geom::Point)> {
|
||||
use wordcraft_geom::Point;
|
||||
// A rule this close to a cell edge (points) is drawn on it.
|
||||
const ON_EDGE: f32 = 0.25;
|
||||
// Rules by the line they run along (horizontal or not, coordinate in quarter points), so
|
||||
// each cell edge looks only at the rules on its own line. Body rules cannot cover header or
|
||||
// footer cells, so they are left out when only those are outlined, as in `gridline_cells`.
|
||||
let key = |c: f32| (c * 4.0).round() as i64;
|
||||
let body = if all { page.items.as_slice() } else { &[] };
|
||||
let mut rules: std::collections::HashMap<(bool, i64), Vec<(f32, f32, f32)>> = std::collections::HashMap::new();
|
||||
for it in body.iter().chain(&page.header).chain(&page.footer) {
|
||||
let Placed::Rule { x0, y0, x1, y1, border } = it else { continue };
|
||||
if !border.is_visible() {
|
||||
continue;
|
||||
}
|
||||
let (horizontal, c0, c1, s0, s1) = if (y0 - y1).abs() <= ON_EDGE { (true, *y0, *y1, *x0, *x1) } else { (false, *x0, *x1, *y0, *y1) };
|
||||
if (c0 - c1).abs() <= ON_EDGE && c0.is_finite() {
|
||||
rules.entry((horizontal, key(c0))).or_default().push((c0, s0.min(s1), s0.max(s1)));
|
||||
}
|
||||
}
|
||||
// The rules covering the line at `at`, sorted and merged into disjoint spans. Cells share their
|
||||
// edges, so each distinct line is sorted once (not once per cell edge) and every edge then
|
||||
// binary-searches its start.
|
||||
let mut merged: std::collections::HashMap<(bool, u32), Vec<(f32, f32)>> = std::collections::HashMap::new();
|
||||
// The parts of [a, b] along a line at `at` that no rule on the same line covers; `horizontal`
|
||||
// picks rules running along x at y = `at`, otherwise along y at x = `at`.
|
||||
let mut uncovered = |horizontal: bool, at: f32, a: f32, b: f32| -> Vec<(f32, f32)> {
|
||||
let covers = merged.entry((horizontal, at.to_bits())).or_insert_with(|| {
|
||||
let k = key(at);
|
||||
let mut covers: Vec<(f32, f32)> = (k - 1..=k + 1)
|
||||
.filter_map(|k| rules.get(&(horizontal, k)))
|
||||
.flatten()
|
||||
.filter(|(c, _, _)| (c - at).abs() <= ON_EDGE)
|
||||
.map(|&(_, s0, s1)| (s0, s1))
|
||||
.collect();
|
||||
covers.sort_by(|p, q| p.0.total_cmp(&q.0));
|
||||
let mut spans: Vec<(f32, f32)> = Vec::with_capacity(covers.len());
|
||||
for (c0, c1) in covers {
|
||||
match spans.last_mut() {
|
||||
Some(last) if c0 <= last.1 + ON_EDGE => last.1 = last.1.max(c1),
|
||||
_ => spans.push((c0, c1)),
|
||||
}
|
||||
}
|
||||
spans
|
||||
});
|
||||
let mut out = Vec::new();
|
||||
let mut from = a;
|
||||
for &(c0, c1) in &covers[covers.partition_point(|s| s.1 < a)..] {
|
||||
if c0 > from + ON_EDGE {
|
||||
out.push((from, c0.min(b)));
|
||||
}
|
||||
from = from.max(c1);
|
||||
if from >= b {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if from + ON_EDGE < b {
|
||||
out.push((from, b));
|
||||
}
|
||||
out
|
||||
};
|
||||
let mut segs = Vec::new();
|
||||
for r in gridline_cells(page, all, editing) {
|
||||
let (l, t, rt, bt) = (r.x, r.y, r.x + r.w, r.y + r.h);
|
||||
for y in [t, bt] {
|
||||
segs.extend(uncovered(true, y, l, rt).into_iter().map(|(a, b)| (Point::new(a, y), Point::new(b, y))));
|
||||
}
|
||||
for x in [l, rt] {
|
||||
segs.extend(uncovered(false, x, t, bt).into_iter().map(|(a, b)| (Point::new(x, a), Point::new(x, b))));
|
||||
}
|
||||
}
|
||||
segs
|
||||
}
|
||||
|
||||
/// The drawing grid's spacing in points at a screen scale: the document's spacing (`w:drawingGrid*Spacing`;
|
||||
/// ECMA-376's 1/8 inch when it doesn't say), doubled until the lines are at least
|
||||
/// [`DRAWING_GRID_MIN_PX`] apart on screen.
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::pages::gridline_cells;
|
||||
use super::*;
|
||||
use crate::table_resize::tests::{H, cell, screen, steps, table_window};
|
||||
|
||||
@@ -3563,6 +3564,102 @@ fn table_gridlines_include_header_and_footer_tables() {
|
||||
assert!(ys(None).is_empty(), "closing header/footer editing removes the automatic guides");
|
||||
}
|
||||
|
||||
/// #5967: gridlines show only where a cell has no printed border, as in Word; drawn over the
|
||||
/// border they tinted every bordered table blue.
|
||||
#[test]
|
||||
fn table_gridlines_skip_edges_a_border_draws() {
|
||||
use wordcraft_geom::Point;
|
||||
let cell = Placed::Cell {
|
||||
rect: wordcraft_geom::Rect::new(0.0, 0.0, 10.0, 20.0),
|
||||
covered: false,
|
||||
table: Default::default(),
|
||||
row: 0,
|
||||
cell: 0,
|
||||
story: StoryRef::Body,
|
||||
clip: false,
|
||||
};
|
||||
let rule = |x0, y0, x1, y1, border| Placed::Rule { x0, y0, x1, y1, border };
|
||||
let line = wordcraft_doc::props::Border::single(0.5);
|
||||
let none = wordcraft_doc::props::Border::single(0.0);
|
||||
let page = Page {
|
||||
items: vec![
|
||||
// Top border over the whole edge (run on past the corners, as layout draws it).
|
||||
rule(-0.25, 0.0, 10.25, 0.0, line),
|
||||
// Left border over the top half only.
|
||||
rule(0.0, 0.0, 0.0, 10.0, line),
|
||||
// A zero-width border draws nothing, so the right edge keeps its gridline.
|
||||
rule(10.0, 0.0, 10.0, 20.0, none),
|
||||
cell,
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
// Overlapping rules out of order on one line leave only the gap between them: the bottom
|
||||
// edge keeps 4..6 alone.
|
||||
let mut gapped = page.clone();
|
||||
gapped.items.extend([
|
||||
rule(5.0, 20.0, 10.0, 20.0, line),
|
||||
rule(-0.25, 20.0, 3.0, 20.0, line),
|
||||
rule(1.0, 20.0, 4.0, 20.0, line),
|
||||
rule(6.0, 20.0, 5.5, 20.0, line),
|
||||
]);
|
||||
assert_eq!(gridline_segments(&gapped, true, None).first(), Some(&(Point::new(4.0, 20.0), Point::new(5.0, 20.0))));
|
||||
assert_eq!(gridline_segments(&gapped, true, None).len(), 3);
|
||||
let segs = gridline_segments(&page, true, None);
|
||||
assert_eq!(
|
||||
segs,
|
||||
[
|
||||
(Point::new(0.0, 20.0), Point::new(10.0, 20.0)), // bottom: no border
|
||||
(Point::new(0.0, 10.0), Point::new(0.0, 20.0)), // left: below the border
|
||||
(Point::new(10.0, 0.0), Point::new(10.0, 20.0)), // right: invisible border
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// #5967: the gridlines of a table with very many rows are found in one pass over the rules, not
|
||||
/// by re-sorting every rule on a line for each cell edge on it (a long table repainted slowly).
|
||||
#[test]
|
||||
fn table_gridlines_of_a_many_row_table_are_fast() {
|
||||
const ROWS: usize = 30_000;
|
||||
let line = wordcraft_doc::props::Border::single(0.5);
|
||||
let mut items = Vec::new();
|
||||
for i in 0..ROWS {
|
||||
let y = i as f32 * 10.0;
|
||||
items.push(Placed::Cell {
|
||||
rect: wordcraft_geom::Rect::new(0.0, y, 10.0, 10.0),
|
||||
covered: false,
|
||||
table: Default::default(),
|
||||
row: i,
|
||||
cell: 0,
|
||||
story: StoryRef::Body,
|
||||
clip: false,
|
||||
});
|
||||
// One border piece per row on each side, so both vertical lines carry a rule per row.
|
||||
for x in [0.0, 10.0] {
|
||||
items.push(Placed::Rule { x0: x, y0: y, x1: x, y1: y + 10.0, border: line });
|
||||
}
|
||||
}
|
||||
let page = Page { items, ..Default::default() };
|
||||
let start = std::time::Instant::now();
|
||||
let segs = gridline_segments(&page, true, None);
|
||||
// Both sides are covered; the top and bottom of every cell are not.
|
||||
assert_eq!(segs.len(), 2 * ROWS);
|
||||
assert!(segs.iter().all(|(a, b)| a.y == b.y));
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(5), "took {:?}", start.elapsed());
|
||||
}
|
||||
|
||||
/// #5967: a new table has borders on every edge, so it shows no gridline at all; with its
|
||||
/// borders removed every edge shows one.
|
||||
#[test]
|
||||
fn a_bordered_table_shows_no_gridlines() {
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default());
|
||||
a.run("insert.table", json!({"rows": 2, "cols": 3})).unwrap();
|
||||
assert!(a.session.view.table_gridlines, "View Gridlines is on by default");
|
||||
let segs = |a: &mut WordApp| gridline_segments(&a.session.layout().pages[0], true, None).len();
|
||||
assert_eq!(segs(&mut a), 0);
|
||||
a.run("table.borders", json!({"kind": "none"})).unwrap();
|
||||
assert_eq!(segs(&mut a), 6 * 4, "every edge of the six cells");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drawing_grid_starts_at_the_margins_and_stays_inside_them() {
|
||||
// Letter page with 1" margins: the text area is 468 x 648 pt from (72, 72).
|
||||
|
||||
Reference in new issue
Block a user