Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ its reason is in [docs/decisions.md](docs/decisions.md).
Desk Writer came first, because the text engine is the hardest part and
the other two apps depend on it. It has its text engine, document
structure, writing tools, objects, tables and charts
(<!-- parity -->242 of the 329 rows of its parity matrix have tests, 88 of them against what Pages itself laid out, all of them alike<!-- /parity -->):
(<!-- parity -->243 of the 329 rows of its parity matrix have tests, 88 of them against what Pages itself laid out, all of them alike<!-- /parity -->):
it opens ODT and Pages '09 documents and saves ODT, edits and formats text
with styles and a screen reader, checks spelling and grammar, lays out
sections, headers, footers, notes and tables of contents as Pages does,
Expand Down
2 changes: 2 additions & 0 deletions crates/desk-layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ mod notes;
mod objects;
mod outline;
mod pages;
mod screen;
mod tables;

pub use objects::{PlacedObject, flow_text_boxes, object_at, place_object_text, text_box_lines};
pub use outline::{outline_document, paginate_outline, paginate_view};
pub use screen::{SCREEN_MARGIN, paginate_screen, screen_document};
pub use tables::{
CellLayout, ChartLabel, ChartLayout, TableLayout, document_locale, lay_out_chart,
lay_out_table, table_size, today,
Expand Down
187 changes: 187 additions & 0 deletions crates/desk-layout/src/screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// SPDX-FileCopyrightText: 2026 Desk contributors
// SPDX-License-Identifier: GPL-3.0-or-later

//! Screen View, as Pages 15.4 shows a document (PXV-01): the text in one
//! continuous flow as wide as the window, without the edges of pages,
//! their margins, headers and footers. Page, column and section breaks
//! do not start a new page, the columns of a layout become one, and notes
//! go to the end. A floating object shows in the flow, across the column,
//! after the paragraph it is anchored to; an object fixed on a page goes
//! after the last paragraph that starts on that page. Objects of the
//! masters do not show.
//!
//! The paragraphs and their text are those of the document, so a place
//! in the text is the same in both; only how they are laid out changes.

use desk_model::{Anchor, Document, Layout, Length, NoteKind, Op, ParaAttrs, TextSettings, Wrap};
use desk_text::TextSystem;

use crate::pages::{DocumentLayout, paginate};

/// The room between the edges of the flow and its text, in points.
pub const SCREEN_MARGIN: f64 = 24.0;

/// The height of the one page that holds the flow while it is laid out:
/// more than any document needs, and small enough that points keep their
/// fractions in an `f32`.
const FLOW_HEIGHT: f64 = 2_000_000.0;

/// The room between two objects in the flow, in points.
const OBJECT_GAP: f64 = 6.0;

/// The narrowest flow, in points.
const NARROWEST: f64 = 144.0;

/// Lays out a document in Screen View, `width` points wide: one page as
/// tall as its text, whose `setup` gives that size.
pub fn paginate_screen(document: &Document, width: f64, system: &mut TextSystem) -> DocumentLayout {
let screen = screen_document(document, width, system);
let mut layout = paginate(&screen, system);
// The flow ends below the last thing in it.
let bottom = layout
.pages
.iter()
.flat_map(|page| {
page.lines
.iter()
.map(|l| l.bottom())
.chain(page.note_lines.iter().map(|l| l.bottom()))
.chain(page.objects.iter().map(|o| o.y + o.height))
})
.fold(0.0f32, f32::max);
layout.setup.height = Length::from_pt(f64::from(bottom) + SCREEN_MARGIN);
layout
}

/// The document as Screen View lays it out: see the module.
pub fn screen_document(document: &Document, width: f64, system: &mut TextSystem) -> Document {
let mut screen = document.clone();
let mut page = *screen.page_setup();
// Where the objects fixed on pages go: after the last paragraph that
// starts on their page, as the pages lay it out.
let fixed_on_pages = screen
.objects()
.iter()
.any(|o| matches!(o.anchor, Anchor::Page(_)));
let last_on_page: Vec<Option<usize>> = if fixed_on_pages && page.layout_pages.is_none() {
let pages = paginate(document, system);
pages
.pages
.iter()
.map(|p| p.lines.iter().map(|l| l.paragraph).max())
.collect()
} else {
Vec::new()
};

page.width = Length::from_pt(width.max(NARROWEST));
page.height = Length::from_pt(FLOW_HEIGHT);
let margin = Length::from_pt(SCREEN_MARGIN);
page.margin_top = margin;
page.margin_bottom = margin;
page.margin_left = margin;
page.margin_right = margin;
page.facing_pages = false;
page.headers = false;
page.footers = false;
page.layout_pages = None;
page.layout = Layout {
columns: desk_model::Columns::ONE,
before: Length::ZERO,
after: Length::ZERO,
left: Length::ZERO,
right: Length::ZERO,
new_page: false,
balanced: false,
};
apply(&mut screen, Op::SetPageSetup { page });

// Notes go to the end, since the flow has no foot.
let settings = screen.text_settings();
if settings.notes.kind == NoteKind::Footnotes
|| settings.notes.kind == NoteKind::SectionEndnotes
{
let mut notes = settings.notes;
notes.kind = NoteKind::DocumentEndnotes;
let settings = TextSettings {
notes,
..settings.clone()
};
apply(&mut screen, Op::SetTextSettings { settings });
}

// Nothing breaks the flow.
let formats: Vec<Op> = screen
.body()
.iter()
.filter_map(|paragraph| {
let mut attrs = paragraph.attrs().clone();
attrs.merge(&ParaAttrs {
start: Some(None),
page_break_before: Some(false),
column_break_before: Some(false),
..ParaAttrs::default()
});
(&attrs != paragraph.attrs()).then(|| Op::SetParagraphFormat {
paragraph: paragraph.id(),
style: paragraph.style(),
attrs,
})
})
.collect();
for op in formats {
apply(&mut screen, op);
}

// Floating objects go in the flow, after their paragraph; those of
// the masters do not show.
let body: Vec<_> = screen.body().iter().map(|p| p.id()).collect();
let index_of = |id| body.iter().position(|p| *p == id);
let column = width.max(NARROWEST) - 2.0 * SCREEN_MARGIN;
let objects: Vec<_> = screen.objects().iter().cloned().collect();
// How far down the objects already above the text of each paragraph
// reach, so that the next goes under them.
let mut stacked: std::collections::HashMap<desk_model::NodeId, f64> = Default::default();
for mut object in objects {
let after = match object.anchor {
Anchor::InText => continue,
Anchor::Master(..) => {
apply(&mut screen, Op::RemoveObject { object: object.id });
continue;
}
Anchor::Paragraph(id) => index_of(id),
Anchor::Page(n) => last_on_page
.get(n as usize)
.or(last_on_page.last())
.copied()
.flatten(),
};
let Some(after) = after.or(if body.is_empty() { None } else { Some(0) }) else {
continue;
};
// The paragraph after it holds the object above its text; the
// last paragraph holds those after it.
let holder = body.get(after + 1).copied().unwrap_or(body[after]);
let (w, h) = (object.frame.width.to_pt(), object.frame.height.to_pt());
let scale = if w > column && w > 0.0 {
column / w
} else {
1.0
};
object.frame.width = Length::from_pt(w * scale);
object.frame.height = Length::from_pt(h * scale);
object.frame.x = Length::from_pt(SCREEN_MARGIN);
let top = stacked.entry(holder).or_insert(0.0);
object.frame.y = Length::from_pt(*top);
*top += h * scale + OBJECT_GAP;
object.anchor = Anchor::Paragraph(holder);
object.wrap = Wrap::AboveAndBelow;
apply(&mut screen, Op::ReplaceObject { object });
}
screen
}

fn apply(document: &mut Document, op: Op) {
// The operations come from the document itself, so they apply.
let _ = document.apply(op);
}
89 changes: 89 additions & 0 deletions crates/desk-layout/tests/screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2026 Desk contributors
// SPDX-License-Identifier: GPL-3.0-or-later

//! Screen View (PXV-01): the text in one flow as wide as the window.

use desk_layout::{SCREEN_MARGIN, paginate, paginate_screen};
use desk_model::{
Anchor, Document, Editor, Frame, Length, MarginPages, Object, ObjectKind, ParaAttrs, Shape,
};
use desk_text::TextSystem;

#[test]
fn pxv_01_screen_view_flows_the_text_without_pages() {
let mut editor = Editor::new(Document::new_word_processing());
let start = editor.document().start();
let (fixed, wide, master) = editor
.edit("Text", |edit| {
let end = edit.insert_text(start, &"words of text that wrap ".repeat(300))?;
let second = edit.split_paragraph(end)?;
let end = edit.insert_text(second, &"more words after a break ".repeat(100))?;
// The second paragraph starts a new page.
edit.set_paragraph_attrs(
second,
end,
&ParaAttrs {
page_break_before: Some(true),
..ParaAttrs::default()
},
)?;
let mut add = |anchor, width| {
let mut object = Object::new(
edit.new_id(),
ObjectKind::Shape(Shape::Rectangle),
Frame::from_pt(300.0, 400.0, width, 80.0),
);
object.anchor = anchor;
let id = object.id;
edit.add_object(object).map(|()| id)
};
let fixed = add(Anchor::Page(0), 100.0)?;
let wide = add(Anchor::Page(0), 2000.0)?;
let master = add(Anchor::Master(None, MarginPages::Default), 50.0)?;
Ok((fixed, wide, master))
})
.unwrap();
let document = editor.document();
let mut system = TextSystem::new();
let pages = paginate(document, &mut system);
assert!(pages.pages.len() >= 3, "{}", pages.pages.len());

let screen = paginate_screen(document, 500.0, &mut system);
// One page as wide as asked, as tall as its text.
assert_eq!(screen.pages.len(), 1);
assert_eq!(screen.setup.width, Length::from_pt(500.0));
let page = &screen.pages[0];
let last = page.lines.last().unwrap().bottom();
let height = screen.setup.height.to_pt() as f32;
assert!(height > last && height < last + 200.0, "{height} {last}");
// Both paragraphs, without the page break between them, and no
// headers or footers.
let second = page.lines.iter().position(|l| l.paragraph == 1).unwrap();
let objects: f32 = page.objects.iter().map(|o| o.height).sum();
let gap = page.lines[second].top() - page.lines[second - 1].bottom();
assert!(gap < objects + 40.0, "{gap}");
assert!(page.margin_lines.is_empty());
for line in &page.lines {
assert!((line.origin.0 - SCREEN_MARGIN as f32).abs() < 0.01);
}
// The objects fixed on the first page show in the flow, after the
// last paragraph that starts there, across the column at most; the
// object of the masters does not show.
let shown: Vec<_> = page.objects.iter().map(|o| o.object.id).collect();
assert!(shown.contains(&fixed) && shown.contains(&wide));
assert!(!shown.contains(&master));
let wide = page.objects.iter().find(|o| o.object.id == wide).unwrap();
assert!((wide.width - (500.0 - 2.0 * SCREEN_MARGIN as f32)).abs() < 0.5);
assert!((wide.x - SCREEN_MARGIN as f32).abs() < 0.5);
// The text goes around them: no line runs under an object.
for object in &page.objects {
for line in &page.lines {
assert!(
line.bottom() <= object.y + 0.5 || line.top() >= object.y + object.height - 0.5,
"a line at {} under an object at {}",
line.top(),
object.y
);
}
}
}
24 changes: 23 additions & 1 deletion crates/desk-ui/src/editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod objects;
mod outline;
mod pen;
mod points;
mod screen;
mod sections;
mod slides;
mod stories;
Expand Down Expand Up @@ -459,6 +460,25 @@ struct State {
cells: Option<tables::CellSelection>,
/// The field that edits the text of a cell.
cell_editor: Option<tables::CellEditor>,
/// How wide the text flows in Screen View, in points, while it is on
/// (PXV-01).
screen: Option<f64>,
}

impl State {
/// Lays out a document as the view shows it: as pages, or in Screen
/// View.
fn lay_out(&self, document: &Document) -> DocumentLayout {
lay_out_in(self.screen, document)
}
}

/// Lays out a document as pages, or in Screen View `screen` points wide.
fn lay_out_in(screen: Option<f64>, document: &Document) -> DocumentLayout {
match screen {
Some(width) => crate::lay_out_screen(document, width),
None => crate::lay_out(document),
}
}

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -606,10 +626,12 @@ impl TextEditor {
guides: Vec::new(),
cells: None,
cell_editor: None,
screen: None,
})),
};
this.show_pages();
this.update_squiggles();
this.follow_screen_width();
this.connect_keyboard();
this.connect_pointer();
this.connect_drop();
Expand Down Expand Up @@ -2358,7 +2380,7 @@ impl TextEditor {
fn after_change(&self, selection: Selection) {
if !self.sync_story() {
let mut state = self.state.borrow_mut();
state.layout = crate::lay_out(state.editor.document());
state.layout = state.lay_out(state.editor.document());
}
if self.story().is_some_and(|story| !self.story_exists(story)) {
self.leave_story(None);
Expand Down
3 changes: 2 additions & 1 deletion crates/desk-ui/src/editing/guides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ impl TextEditor {
pub(super) fn show_outlines(&self) {
let outlines = {
let state = self.state.borrow();
if state.show_layout {
// Screen View has no layout to show (PXV-01).
if state.show_layout && state.screen.is_none() {
outlines(Self::main_document(&state), Self::main_layout(&state))
} else {
Vec::new()
Expand Down
9 changes: 9 additions & 0 deletions crates/desk-ui/src/editing/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ impl TextEditor {
.filter(|id| ids.contains(id))
.collect();
ids.dedup();
// The objects in the flow of Screen View are not selected
// (PXV-01).
if state.screen.is_some() {
ids.clear();
}
state.objects = ids;
}
self.show_objects();
Expand Down Expand Up @@ -1995,6 +2000,10 @@ impl TextEditor {
y: f64,
modifiers: gdk::ModifierType,
) -> bool {
// The objects in the flow of Screen View are not moved (PXV-01).
if self.screen_view() {
return false;
}
let Some((page, px, py)) = self.view.page_at(x, y) else {
return false;
};
Expand Down
Loading
Loading