mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-01 18:24:54 -06:00
* Layout: a paragraph with a hidden mark is shown on one line with the next one (#3986) (#4134) Lands #4134 rebased onto main; only docs conflicts were resolved (both sides kept). Co-Authored-By: Menonro <25549525+Menonro@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FypgCXtSjB6e5jmy9Uq7u8 * Layout: hidden-mark join fixes from review of #6913 - A paragraph moved off the shared line (next column or page) gets its own top and bottom border room again (flow.rs `Rooms` were stale). - Hit testing cuts a line at a joined paragraph only when that paragraph still shares the line (`shares_line`), not when its first word wrapped below. - A cell or header paragraph going on from a hidden mark takes the shared line's top as its paragraph origin, so paragraph-anchored drawings follow it. - A text frame is placed apart: it is not joined, keeps its number. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #4134: joined text's later lines start below a taller shared line; table frames don't join A paragraph going on from the line of one whose mark is hidden kept only its first line clear of that line: under a taller heading its second line started inside the heading's line, back at the left margin. Its later lines now keep clear of the rest of the shared line, with the room for its bottom border, in the body and in boxes (cells, headers, text boxes). The guard against joining a text frame resolves the paragraph with the table's paragraph formatting, as its layout does, so a frame given by the table style is placed apart with its list number instead of joining. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #4134: joined text's first line shares the line again with exact spacing The area that holds a joined paragraph's later lines below a taller shared line started just below its first line. The line breaker checks a row against wrap areas at its own estimate, the mark's font height, which is taller than an exact line height below it: the first row then met the area and moved below the shared line. The area now starts below whichever is taller, so the first line shares the line under every line spacing rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx * #4134: joined text's later lines clear the shared line when a large word wraps The area that keeps a continuation's later lines below the shared line now starts just below the first row at the line breaker's row estimate, which is what the breaker tests rows with. The probe's first line can be taller than the real one when a large word fits it but wraps off the narrower shared line, and starting the area below that taller line let the real second line start inside the heading's line. The test adds a 30 pt word at three places that wrap it to the second line, in the body and in a cell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx --------- Co-authored-by: Menonro <25549525+Menonro@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
200 lines
8.2 KiB
Rust
200 lines
8.2 KiB
Rust
use super::ctx::Outlines;
|
|
use super::placed::Placed;
|
|
use super::{
|
|
Arc, Blocks, Document, Hash, HashMap, HashSet, Hasher, HideDeleted, InlineObject, LayoutOptions, Page, ParaLayout, Paragraph, SectionProps,
|
|
StoryRef, ViewMode, fields, styleref,
|
|
};
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
pub(crate) struct Key {
|
|
pub(crate) rev: u64,
|
|
pub(crate) width: u32,
|
|
pub(crate) label: Option<String>,
|
|
pub(crate) page: Option<(u32, u32, u32, u32, u64, wordcraft_doc::section::NumFormat)>,
|
|
/// The pages a split paragraph's fields are on after its first (`FieldCtx::page_from`).
|
|
pub(crate) page_from: u64,
|
|
pub(crate) table: u64,
|
|
pub(crate) notes: u64,
|
|
pub(crate) excl: u64,
|
|
pub(crate) eq: u32,
|
|
pub(crate) read_spacing: Option<u32>,
|
|
pub(crate) grid: (u32, u32, u32, bool),
|
|
pub(crate) fit: bool,
|
|
/// Heights of the paragraph's inline boxes that resize to fit their text.
|
|
pub(crate) fitted: u64,
|
|
pub(crate) hide_deleted: HideDeleted,
|
|
/// STYLEREF results in headers and footers ([`FieldCtx::styleref_key`]).
|
|
pub(crate) sref: u64,
|
|
/// The word being typed in this paragraph, left without a spelling squiggle.
|
|
pub(crate) typing: Option<(usize, usize)>,
|
|
/// A copy of the paragraph going on from the line of the one before (`ctx::Joining`): its
|
|
/// space before and whether it shares that line ([`ParaEnv::joined`](crate::para::ParaEnv)).
|
|
pub(crate) joined: Option<(u32, bool)>,
|
|
}
|
|
|
|
/// Memoised paragraph layouts.
|
|
#[derive(Default)]
|
|
pub struct LayoutCache {
|
|
pub(crate) paras: HashMap<Key, Arc<ParaLayout>>,
|
|
/// Narrowest and widest content of paragraphs measured for table autofit ([`Ctx::para_measured`]).
|
|
pub(crate) measures: HashMap<Key, (f32, f32)>,
|
|
/// Outlines of pictures' opaque pixels (Tight and Through wrapping), by media key, the
|
|
/// media's bytes (address and length) and crop. Kept from one layout to the next, so each
|
|
/// entry holds a weak reference to its bytes: their allocation isn't freed while the entry is
|
|
/// kept, so no other picture's bytes can come to have its address ([`Outlines`]).
|
|
pub(crate) outlines: Outlines,
|
|
pub(crate) env: u64,
|
|
pub(crate) used: HashSet<Key>,
|
|
/// The STYLEREF style index of the last pass and what it was built from: the body blocks
|
|
/// (shared, so an edited block is a different pointer) and `env` (styles and numbering). Kept
|
|
/// while both are unchanged, so headers using STYLEREF don't rebuild it on every pass.
|
|
pub(crate) styleref: Option<(Blocks, u64, Arc<styleref::StyleIndex>)>,
|
|
pub hits: u64,
|
|
pub misses: u64,
|
|
}
|
|
|
|
impl LayoutCache {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
pub fn len(&self) -> usize {
|
|
self.paras.len()
|
|
}
|
|
pub fn is_empty(&self) -> bool {
|
|
self.paras.is_empty()
|
|
}
|
|
pub fn clear(&mut self) {
|
|
self.paras.clear();
|
|
self.measures.clear();
|
|
self.outlines.clear();
|
|
self.styleref = None;
|
|
}
|
|
|
|
/// The STYLEREF style index for `doc`, rebuilt only when the body or the styles changed.
|
|
pub(crate) fn style_index(&mut self, doc: &Document) -> Arc<styleref::StyleIndex> {
|
|
if let Some((body, env, ix)) = &self.styleref
|
|
&& *env == self.env
|
|
&& body.len() == doc.body.len()
|
|
&& body.iter().zip(doc.body.iter()).all(|(a, b)| Arc::ptr_eq(a, b))
|
|
{
|
|
return ix.clone();
|
|
}
|
|
let ix = Arc::new(styleref::StyleIndex::build(doc));
|
|
self.styleref = Some((doc.body.to_vec(), self.env, ix.clone()));
|
|
ix
|
|
}
|
|
}
|
|
|
|
pub(crate) fn hash_of<T: Hash>(t: &T) -> u64 {
|
|
let mut h = std::collections::hash_map::DefaultHasher::new();
|
|
t.hash(&mut h);
|
|
h.finish()
|
|
}
|
|
|
|
pub(crate) fn env_hash(doc: &Document, opts: &LayoutOptions) -> u64 {
|
|
let s = serde_json::to_string(&(&doc.styles, &doc.numbering, doc.settings.default_tab, &doc.settings.footnote_format)).unwrap_or_default();
|
|
// Outline view lays paragraphs out changed (no indents; plain without text formatting).
|
|
let outline = (opts.view == ViewMode::Outline).then(|| opts.outline.as_ref().is_some_and(|o| o.plain));
|
|
let mut h = std::collections::hash_map::DefaultHasher::new();
|
|
s.hash(&mut h);
|
|
opts.show_hidden.hash(&mut h);
|
|
opts.hide_deleted.hash(&mut h);
|
|
opts.hide_marked.hash(&mut h);
|
|
opts.deletions_in_balloons.hash(&mut h);
|
|
opts.proofing.hash(&mut h);
|
|
opts.numeral.hash(&mut h);
|
|
opts.field_codes.hash(&mut h);
|
|
opts.proof.hash(&mut h);
|
|
wordcraft_proof::dictionary_generation().hash(&mut h);
|
|
hyphenation_key(doc).hash(&mut h);
|
|
outline.hash(&mut h);
|
|
opts.filename.hash(&mut h);
|
|
opts.filepath.hash(&mut h);
|
|
// Output shows page references the editor leaves stored (a missing bookmark's error too).
|
|
opts.output_page_refs.hash(&mut h);
|
|
opts.bookmark_error.hash(&mut h);
|
|
h.finish()
|
|
}
|
|
|
|
/// The hyphenation settings, hashable (the zone as its bits).
|
|
fn hyphenation_key(doc: &Document) -> (bool, u32, bool, u32) {
|
|
let s = &doc.settings;
|
|
(s.auto_hyphenation, s.hyphenation_zone.to_bits(), s.hyphenate_caps, s.consecutive_hyphen_limit)
|
|
}
|
|
|
|
pub(crate) fn has_page_fields(p: &Paragraph) -> bool {
|
|
p.objects.iter().any(|o| match o {
|
|
InlineObject::Field { instr, .. } => {
|
|
matches!(fields::field_name(instr).as_str(), "PAGE" | "NUMPAGES" | "SECTIONPAGES" | "SECTION" | "PAGEREF")
|
|
}
|
|
_ => false,
|
|
})
|
|
}
|
|
|
|
/// Does `p` show a PAGE field, whose result is the number of the page it lands on?
|
|
pub(crate) fn has_page_number_fields(p: &Paragraph) -> bool {
|
|
p.objects.iter().any(|o| matches!(o, InlineObject::Field { instr, .. } if fields::field_name(instr) == "PAGE"))
|
|
}
|
|
|
|
/// Does `p` show a page total, which depends on how many pages the document has?
|
|
pub(crate) fn has_total_fields(p: &Paragraph) -> bool {
|
|
p.objects.iter().any(|o| match o {
|
|
InlineObject::Field { instr, .. } => matches!(fields::field_name(instr).as_str(), "NUMPAGES" | "SECTIONPAGES"),
|
|
_ => false,
|
|
})
|
|
}
|
|
|
|
/// Page totals: of the document, and of each section (by index).
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub(crate) struct Totals {
|
|
pub(crate) pages: u32,
|
|
sections: Vec<u32>,
|
|
}
|
|
|
|
impl Totals {
|
|
/// What a first pass assumes: one page.
|
|
pub(crate) fn first_guess(sections: usize) -> Totals {
|
|
Totals { pages: 1, sections: vec![1; sections] }
|
|
}
|
|
/// Count each section on every page containing its body, including a shared continuous
|
|
/// start page. The page owner still selects headers and footers; empty sections count one.
|
|
pub(crate) fn of(pages: &[Page], sections: &[(usize, &SectionProps)]) -> Totals {
|
|
let mut per = vec![0u32; sections.len()];
|
|
for page in pages {
|
|
let mut present = HashSet::from([page.section]);
|
|
for item in &page.items {
|
|
let path = match item {
|
|
Placed::Lines { story: StoryRef::Body, path, .. }
|
|
| Placed::Image { story: StoryRef::Body, path, .. }
|
|
| Placed::Graphic { story: StoryRef::Body, path, .. }
|
|
| Placed::Object { story: StoryRef::Body, path, .. }
|
|
| Placed::Cell { story: StoryRef::Body, table: path, .. } => path,
|
|
_ => continue,
|
|
};
|
|
let Some(&block) = path.0.first() else { continue };
|
|
present.insert(sections.partition_point(|(end, _)| *end < block as usize));
|
|
}
|
|
for section in present {
|
|
if let Some(n) = per.get_mut(section) {
|
|
*n = n.saturating_add(1);
|
|
}
|
|
}
|
|
}
|
|
for n in &mut per {
|
|
*n = (*n).max(1);
|
|
}
|
|
Totals { pages: u32::try_from(pages.len()).unwrap_or(u32::MAX).max(1), sections: per }
|
|
}
|
|
pub(crate) fn section(&self, si: usize) -> u32 {
|
|
self.sections.get(si).copied().unwrap_or(1)
|
|
}
|
|
}
|
|
|
|
/// The most extra layout passes for page totals in the body: a total that changes the page
|
|
/// count (its digits pushing text to another page) is tried again, this many times at most.
|
|
pub(crate) const MAX_TOTAL_PASSES: usize = 3;
|
|
|
|
/// The most extra layout passes for output's body `PAGEREF`s: a page number whose digits move a
|
|
/// bookmark to another page is tried again, this many times at most.
|
|
pub(crate) const MAX_PAGE_REF_PASSES: usize = 3;
|