Compare commits

...
2 Commits
Author SHA1 Message Date
Martin Ball 1cfbfa91ae Merge branch 'main' into land/5447 2026-11-17 06:19:22 +01:00
6a0fbf89a6 Pictures: an inserted or pasted picture fits the table cell, column or section it goes in (#5428) (#5447)
Lands #5447 by @D13GOOOO (approved by @benandez), rebased onto main; only the docs conflicts in ROADMAP.md, docs/gaps.md, docs/ui-parity.md were resolved, keeping both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: D13GOOOO <106814760+D13GOOOO@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCXtSjB6e5jmy9Uq7u8
2026-11-17 02:42:35 +00:00
7 changed files with 158 additions and 7 deletions
+3 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -117,9 +117,9 @@ fn place_picture(s: &mut Session, bytes: Vec<u8>, width: Option<f32>, alt: &str)
None => return Err(CmdError::Failed("not a supported image (PNG, JPEG, GIF, WebP, BMP, WMF, EMF)".into())),
},
};
// Fitted to the room where it goes: the table cell, the column, its own section (#5428).
let max_w = s.room_at(&s.sel.ordered().0);
let key = s.doc.add_media(bytes, ext);
// Fitted to the text width.
let max_w = s.doc.sections().first().map(|(_, sp)| sp.text_width()).unwrap_or(468.0);
let (mut w, mut h) = (natural_w, natural_h);
if let Some(want) = width {
// At most 22 inches, as for icons: a huge width made the height of a tall picture infinite.
+3 -2
View File
@@ -433,9 +433,10 @@ pub(crate) fn unlink_sections(blocks: &mut [Block]) {
/// Turn an imported document into a fragment of the session's document: bring over the media,
/// lists and styles it refers to (renumbered so they can't collide with ours).
fn adopt(s: &mut Session, src: Document) -> Fragment {
// Pasted pictures fit the room where they go, like Insert › Pictures: the table cell, the
// column, its own section (#5428; layout keeps their size).
let max_w = s.room_at(&s.sel.ordered().0);
let mut frag = s.doc.adopt(src);
// Pasted pictures fit our text width, like Insert › Pictures (layout keeps their size).
let max_w = crate::io::text_width(&s.doc);
for b in &mut frag.blocks {
crate::io::fit_block_pictures(b, max_w);
}
+30
View File
@@ -182,6 +182,36 @@ impl Session {
Arc::new(wordcraft_layout::layout(doc, &mut LayoutCache::new(), &opts))
}
/// How wide a picture put in at `pos` may be, as Word fits an inserted or pasted picture
/// (#5428): the room its paragraph's lines have (inside a table cell, the innermost cell's
/// text area, nested tables included; the paragraph's indents left out), never wider than the
/// column it is in, of its own section. The first section's text width when `pos` isn't laid
/// out. It is measured on the pages, whatever the view: Web Layout and Read Mode reflow the
/// text to the window, and a picture put in there keeps the size it gets in the document.
pub(crate) fn room_at(&mut self, pos: &wordcraft_doc::Pos) -> f32 {
let first = self.doc.sections().first().map(|(_, sp)| sp.text_width()).unwrap_or(468.0);
let l = if !self.view.read_mode && self.view.mode == ViewMode::Print { self.layout() } else { self.export_layout_of(&self.doc, false) };
let Some((caret, page)) = l.caret(pos).and_then(|c| Some((c, l.pages.get(c.page)?))) else { return first };
let section = self.doc.sections().get(page.section).map(|(_, sp)| (*sp).clone());
let column = match (&section, pos.story) {
(Some(sp), StoryRef::Body) => {
let boxes = sp.column_boxes();
let x = caret.x - page.body.x;
boxes.iter().find(|(cx, w)| x >= cx - 0.5 && x <= cx + w + 0.5).map_or_else(|| sp.text_width(), |(_, w)| *w)
}
(Some(sp), StoryRef::Part(_)) => sp.text_width(),
(None, _) => first,
};
let lines = wordcraft_layout::hit::page_lines(page, pos.story)
.iter()
.filter(|h| *h.path == pos.path)
.map(|h| h.right - h.left)
.filter(|w| w.is_finite())
.fold(0.0f32, f32::max);
let room = if lines >= 1.0 { lines.min(column) } else { column };
if room.is_finite() && room >= 1.0 { room } else { first }
}
/// Invalidate the cached layout (fonts changed etc.).
pub fn relayout(&mut self) {
self.layout = Default::default();
+116
View File
@@ -13404,6 +13404,122 @@ fn page_down_extends_a_selection_that_ends_in_collapsed_outline_text() {
assert!(s.layout().caret(&s.sel.focus).is_some(), "to a place the outline shows");
}
/// #5428: an inserted or pasted picture fits where it goes, as in Word: a table cell's width
/// (a nested cell's too), a column's, not the whole page's text width.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn an_inserted_or_pasted_picture_fits_where_it_goes() {
let png = |w: u32, h: u32| {
let mut b = Vec::new();
image::RgbaImage::from_pixel(w, h, image::Rgba([30, 90, 200, 255]))
.write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png)
.unwrap();
cmd::insert::base64_encode(&b)
};
let widths = |s: &Session| {
let mut v = Vec::new();
for b in s.doc.body.iter() {
let mut b = (**b).clone();
cmd::paste::each_para(&mut b, 0, &mut |p| {
for o in &p.objects {
if let wordcraft_doc::InlineObject::Image { w, h, .. } = o {
v.push((*w, *h));
}
}
});
}
v
};
let text_w = crate::io::text_width(&Session::new(wordcraft_doc::Document::new()).doc);
let in_cell = |cols: u32| {
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "insert.table", json!({"rows": 1, "cols": cols}));
assert!(s.sel.focus.path.depth() > 0, "the caret is in the first cell");
s
};
// Insert › Pictures and Paste in a cell of a three-column table.
let cell = text_w / 3.0;
let mut s = in_cell(3);
run(&mut s, "insert.picture", json!({"data": png(1000, 200)}));
let mut p = in_cell(3);
let html = format!("<p><img src=\"data:image/png;base64,{}\"></p>", png(1000, 200));
run(&mut p, "edit.pasteSpecial", json!({"as": "html", "html": html}));
for (how, s) in [("inserted", &s), ("pasted", &p)] {
let got = widths(s);
let [(w, h)] = got.as_slice() else { panic!("{how}: {got:?}") };
assert!(*w <= cell && *w > cell - 20.0, "{how}: {w} fits the cell's text area ({cell} less its margins)");
assert!((h * 5.0 - w).abs() < 0.1, "{how}: proportions kept: {w} × {h}");
}
// A table in that cell: its own narrower cell.
let mut s = in_cell(2);
let outer = s.room_at(&s.sel.focus.clone());
// (A table inserted in a cell takes the page's text width for its columns, so four columns
// make its cells narrower than the outer one.)
run(&mut s, "insert.table", json!({"rows": 1, "cols": 4}));
assert!(s.sel.focus.path.depth() > 1, "the caret is in the nested cell");
let nested = s.room_at(&s.sel.focus.clone());
assert!(nested < outer - 50.0, "the nested cell ({nested}) is narrower than the outer one ({outer})");
run(&mut s, "insert.picture", json!({"data": png(1000, 200)}));
let w = widths(&s)[0].0;
assert!((w - nested).abs() < 0.5 && w <= text_w / 4.0, "{w} fits the nested cell ({nested})");
// Two text columns: one column's width.
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "layout.columns", json!({"count": 2}));
let column = s.doc.sections()[0].1.column_boxes()[0].1;
run(&mut s, "insert.picture", json!({"data": png(1000, 200)}));
let w = widths(&s)[0].0;
assert!((w - column).abs() < 0.5, "{w} fits the column ({column})");
// Body text keeps the full text width.
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "insert.picture", json!({"data": png(1000, 200)}));
assert!((widths(&s)[0].0 - text_w).abs() < 0.5);
}
/// #5428 review: Web Layout and Read Mode reflow the text to the window, but a picture put in
/// there is fitted to the page's text width, as in Print Layout: the size stays in the document.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_picture_put_in_in_web_layout_fits_the_page_not_the_window() {
let png = |w: u32, h: u32| {
let mut b = Vec::new();
image::RgbaImage::from_pixel(w, h, image::Rgba([30, 90, 200, 255]))
.write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png)
.unwrap();
cmd::insert::base64_encode(&b)
};
let width = |s: &Session| {
s.doc.body.iter().filter_map(|b| b.as_para()).flat_map(|p| p.objects.iter()).find_map(|o| match o {
wordcraft_doc::InlineObject::Image { w, .. } => Some(*w),
_ => None,
})
};
let text_w = crate::io::text_width(&Session::new(wordcraft_doc::Document::new()).doc);
let web = || {
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "view.webLayout", json!({}));
// A narrow window, or a high zoom.
s.view.web_width = 350.0;
assert!(s.layout().pages[0].w < text_w, "the window is narrower than the page's text");
s
};
let mut s = web();
run(&mut s, "insert.picture", json!({"data": png(1000, 200)}));
let mut p = web();
let html = format!("<p><img src=\"data:image/png;base64,{}\"></p>", png(1000, 200));
run(&mut p, "edit.pasteSpecial", json!({"as": "html", "html": html}));
for (how, s) in [("inserted", &s), ("pasted", &p)] {
let w = width(s).unwrap();
assert!((w - text_w).abs() < 0.5, "{how}: {w} fits the page's text width ({text_w})");
}
// Read Mode's narrow pages don't set it either.
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "text.insert", json!({"text": "Read"}));
run(&mut s, "view.readMode", json!({}));
s.view.read_width = 240.0;
let room = s.room_at(&s.sel.focus.clone());
assert!((room - text_w).abs() < 0.5, "Read Mode: {room} is the page's text width ({text_w})");
}
/// A picture an OpenDocument file gives no size is shown at its own, fitted to the text as other
/// formats' are; one with a stated size keeps it, in line too.
#[test]
+2 -1
View File
File diff suppressed because one or more lines are too long
+2 -1
View File
File diff suppressed because one or more lines are too long