From f7ddd9957a5de5b5af7d90bb82711caf6149b367 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 25 Nov 2026 09:50:25 +0000 Subject: [PATCH 1/2] ui-canvas rulers: a centimetre document ruler labels every whole centimetre TickRule::of_unit gave centimetres the generic metric rule, whose least label step is 60 points, so at 100% (1 cm is about 28 points) the page and text rulers labelled every 2 cm. Add TickRule::Centimetres for document rulers: labels every centimetre down to 24 points apart, thinning to 2, 5, 10 and so on when zoomed out, with quarter-centimetre ticks (halves when tighter), as WordCraft v0.18.1 drew. Canvas and image rulers keep TickRule::Metric. Fixes storytold/wordcraft#7686. Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-canvas/src/canvas/mod.rs | 3 ++- crates/ui-canvas/src/canvas/rulers/mod.rs | 2 +- crates/ui-canvas/src/canvas/rulers/ticks.rs | 30 +++++++++++++++++++-- crates/ui-canvas/tests/it/canvas/rulers.rs | 22 +++++++++++++-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 88a0dd64d..bf6c136d8 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -82,7 +82,8 @@ pub use zoom_motion::{HoldZoom, ZoomAnimation, ZoomDrag, easing_view}; pub(crate) mod rulers; pub(crate) mod scrollbars; pub use rulers::{ - INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerCommands, RulerMargins, RulerPart, RulerTick, Rulers, RulersOutput, StepLadder, TickRule, + CENTIMETRE_LABEL_STEP_MIN, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerCommands, RulerMargins, RulerPart, RulerTick, Rulers, + RulersOutput, StepLadder, TickRule, }; pub use scrollbars::{CanvasScrollbars, CanvasScrollbarsOutput}; diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index 96acf884f..811a86d81 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -6,7 +6,7 @@ pub(crate) mod stories; mod ticks; -pub use ticks::{INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerTick, StepLadder, TickRule}; +pub use ticks::{CENTIMETRE_LABEL_STEP_MIN, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerTick, StepLadder, TickRule}; use super::words::CanvasWords; use super::{CanvasRulers, CanvasSpace, GuideDrag, RulerText, Snap, context_menu, readout}; diff --git a/crates/ui-canvas/src/canvas/rulers/ticks.rs b/crates/ui-canvas/src/canvas/rulers/ticks.rs index 2d0355edb..1566afd8a 100644 --- a/crates/ui-canvas/src/canvas/rulers/ticks.rs +++ b/crates/ui-canvas/src/canvas/rulers/ticks.rs @@ -13,6 +13,14 @@ pub const INCH_EIGHTHS: [u32; 3] = [8, 4, 2]; /// The least distance between labelled ticks, in points. pub const LABEL_STEP_MIN: f32 = 60.0; +/// The least distance between labelled ticks of a centimetre document ruler, in points: a whole centimetre at 100% +/// (about 28 points) is labelled, as Word's ruler does. +pub const CENTIMETRE_LABEL_STEP_MIN: f32 = 24.0; +/// The least distance between ticks of a centimetre document ruler, in points. +const CENTIMETRE_TICK_STEP_MIN: f64 = 4.0; +/// Labelled centimetre steps: 1, 2, 5, 10 times the base. +const CENTIMETRE_STEPS: [f64; 9] = [1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0]; + /// The metric rule's subdivisions of a labelled step. const METRIC_SUBDIVISIONS: f32 = 10.0; /// Labelled inch steps in inches: 1, 2, 5, 10 times the base. @@ -71,6 +79,9 @@ impl StepLadder { pub enum TickRule<'a> { /// The units' own table ([`RulerUnits::step`]): decimal steps and ten subdivisions, or the host's custom table. Metric, + /// Centimetres of a document ruler: a label every centimetre (every 2, 5, 10 and so on when smaller), with quarter + /// centimetre ticks while they stay [`CENTIMETRE_TICK_STEP_MIN`] apart, then halves. + Centimetres, /// Inches: a label every inch (every 2, 5, 10 and so on when smaller), subdivided by the first of these counts, /// finest first, that keeps the ticks a tenth of the least label step apart ([`INCH_SIXTEENTHS`], /// [`INCH_EIGHTHS`]). @@ -81,9 +92,14 @@ pub enum TickRule<'a> { } impl TickRule<'static> { - /// The rule for a ruler counting `kind`: Word's sixteenths for inches, `Metric` otherwise. + /// The rule for a document ruler counting `kind`: Word's sixteenths for inches, whole labelled centimetres for + /// centimetres, `Metric` otherwise. pub fn of_unit(kind: UnitKind) -> TickRule<'static> { - if kind == UnitKind::Inches { TickRule::Inches(&INCH_SIXTEENTHS) } else { TickRule::Metric } + match kind { + UnitKind::Inches => TickRule::Inches(&INCH_SIXTEENTHS), + UnitKind::Centimetres => TickRule::Centimetres, + _ => TickRule::Metric, + } } } @@ -112,6 +128,15 @@ fn inch_step(points_per_inch: f64, finest: &[u32]) -> (f64, u32) { (step, subdivisions) } +/// The labelled centimetre step and its subdivisions at `points_per_cm` screen points per centimetre. +fn centimetre_step(points_per_cm: f64) -> (f64, u32) { + let step_min = f64::from(CENTIMETRE_LABEL_STEP_MIN); + let step = CENTIMETRE_STEPS.into_iter().find(|step| step * points_per_cm >= step_min).unwrap_or(1000.0); + let subdivisions = + if step > 1.0 { 2 } else { [4_u32, 2].into_iter().find(|n| points_per_cm / f64::from(*n) >= CENTIMETRE_TICK_STEP_MIN).unwrap_or(1) }; + (step, subdivisions) +} + /// The ticks of one ruler over `values` (ruler units from the zero point): the one tick rule of every ruler, so a /// text ruler or a page view's ruler shows the same ticks as the canvas's. One labelled step beyond each end is /// included, since a label starts right of its tick. `zoom` is screen points per document unit along the axis, as @@ -119,6 +144,7 @@ fn inch_step(points_per_inch: f64, finest: &[u32]) -> (f64, u32) { pub fn ticks(units: &RulerUnits, rule: TickRule<'_>, zoom: f64, values: RangeInclusive) -> Vec { let (step, subdivisions) = match rule { TickRule::Metric => units.step(zoom, LABEL_STEP_MIN), + TickRule::Centimetres => centimetre_step(zoom / units.per_doc), TickRule::Inches(finest) => inch_step(zoom / units.per_doc, finest), TickRule::Steps(ladder) => { let (tick, label) = ladder.steps(zoom / units.per_doc); diff --git a/crates/ui-canvas/tests/it/canvas/rulers.rs b/crates/ui-canvas/tests/it/canvas/rulers.rs index 16aa58288..5fe156210 100644 --- a/crates/ui-canvas/tests/it/canvas/rulers.rs +++ b/crates/ui-canvas/tests/it/canvas/rulers.rs @@ -4,8 +4,8 @@ use craft_behavior::geometry::{SnapOptions, SnapTargets}; use craft_behavior::kurbo::{Point, Rect as DocRect}; use craft_behavior::{CancelReason, CanvasView, RulerUnits, TickLabels, Ticks}; use craft_ui::canvas::{ - Canvas, CanvasTool, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, PageFrame, Pasteboard, RulerText, Rulers, Snap, StepLadder, TickRule, - ToolAction, ToolContext, ToolPath, ToolPointer, + CENTIMETRE_LABEL_STEP_MIN, Canvas, CanvasTool, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, PageFrame, Pasteboard, RulerText, Rulers, Snap, + StepLadder, TickRule, ToolAction, ToolContext, ToolPath, ToolPointer, }; use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; use craft_ui::primitives::TickRank; @@ -60,6 +60,24 @@ fn labelled_ticks_never_touch_at_any_zoom() { } } +/// A document ruler labels every whole centimetre at 100% (28.35 points each), and thins the labels out when zoomed +/// out so they never touch. +#[trial] +fn a_centimetre_document_ruler_labels_each_centimetre_and_thins_out_zoomed_out() { + let centimetres = RulerUnits::new(2.54 / 72.0, Ticks::decimal(false)); + let rule = TickRule::of_unit(UnitKind::Centimetres); + let at_full = least_label_gap(¢imetres, rule, 1.0); + assert!(at_full < f64::from(LABEL_STEP_MIN), "labels at 100% are closer than the generic metric step: {at_full}"); + let one_cm = 72.0 / 2.54; + assert!((at_full - one_cm).abs() < 1e-6, "labels at 100% are one centimetre apart: {at_full}"); + let least = f64::from(CENTIMETRE_LABEL_STEP_MIN) - 1e-9; + for zoom in ZOOMS { + let gap = least_label_gap(¢imetres, rule, zoom); + assert!(gap >= least, "centimetre labels at zoom {zoom}: {gap} points apart"); + } + assert!(least_label_gap(¢imetres, rule, 0.25) > at_full, "labels thin out when zoomed out"); +} + /// A vertical label stacks its characters one label-role size apart; the longest label the metric rule gives at the /// least step (five characters) and its gap to the tick stay inside that step, so neighbouring labels never touch. #[trial] From d4552077e99d89e746846ac329cf0ded928e9012 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 25 Nov 2026 10:31:08 +0000 Subject: [PATCH 2/2] ui-canvas rulers: drop the centimetre rule's unreachable halves fallback A whole labelled centimetre is at least 24 points wide, so its quarter ticks are always 6 points apart; the 4-point fallback to halves never ran and the doc promised it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-canvas/src/canvas/rulers/ticks.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/ui-canvas/src/canvas/rulers/ticks.rs b/crates/ui-canvas/src/canvas/rulers/ticks.rs index 1566afd8a..8ed6dd2f9 100644 --- a/crates/ui-canvas/src/canvas/rulers/ticks.rs +++ b/crates/ui-canvas/src/canvas/rulers/ticks.rs @@ -16,8 +16,6 @@ pub const LABEL_STEP_MIN: f32 = 60.0; /// The least distance between labelled ticks of a centimetre document ruler, in points: a whole centimetre at 100% /// (about 28 points) is labelled, as Word's ruler does. pub const CENTIMETRE_LABEL_STEP_MIN: f32 = 24.0; -/// The least distance between ticks of a centimetre document ruler, in points. -const CENTIMETRE_TICK_STEP_MIN: f64 = 4.0; /// Labelled centimetre steps: 1, 2, 5, 10 times the base. const CENTIMETRE_STEPS: [f64; 9] = [1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0]; @@ -79,8 +77,8 @@ impl StepLadder { pub enum TickRule<'a> { /// The units' own table ([`RulerUnits::step`]): decimal steps and ten subdivisions, or the host's custom table. Metric, - /// Centimetres of a document ruler: a label every centimetre (every 2, 5, 10 and so on when smaller), with quarter - /// centimetre ticks while they stay [`CENTIMETRE_TICK_STEP_MIN`] apart, then halves. + /// Centimetres of a document ruler: a label every centimetre with quarter centimetre ticks, or every 2, 5, 10 and so + /// on centimetres with halves when a centimetre is narrower than [`CENTIMETRE_LABEL_STEP_MIN`]. Centimetres, /// Inches: a label every inch (every 2, 5, 10 and so on when smaller), subdivided by the first of these counts, /// finest first, that keeps the ticks a tenth of the least label step apart ([`INCH_SIXTEENTHS`], @@ -132,9 +130,8 @@ fn inch_step(points_per_inch: f64, finest: &[u32]) -> (f64, u32) { fn centimetre_step(points_per_cm: f64) -> (f64, u32) { let step_min = f64::from(CENTIMETRE_LABEL_STEP_MIN); let step = CENTIMETRE_STEPS.into_iter().find(|step| step * points_per_cm >= step_min).unwrap_or(1000.0); - let subdivisions = - if step > 1.0 { 2 } else { [4_u32, 2].into_iter().find(|n| points_per_cm / f64::from(*n) >= CENTIMETRE_TICK_STEP_MIN).unwrap_or(1) }; - (step, subdivisions) + // A whole labelled centimetre is at least 24 points wide, so its quarters are 6 points apart. + (step, if step > 1.0 { 2 } else { 4 }) } /// The ticks of one ruler over `values` (ruler units from the zero point): the one tick rule of every ruler, so a