mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
Merge pull request #3471 from storytold/fix/wordcraft-7686-cm-ruler-labels
ui-canvas rulers: a centimetre document ruler labels every whole centimetre (storytold/wordcraft#7686)
This commit is contained in:
@@ -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};
|
||||
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -13,6 +13,12 @@ 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;
|
||||
/// 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 +77,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 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`],
|
||||
/// [`INCH_EIGHTHS`]).
|
||||
@@ -81,9 +90,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 +126,14 @@ 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);
|
||||
// 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
|
||||
/// 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 +141,7 @@ fn inch_step(points_per_inch: f64, finest: &[u32]) -> (f64, u32) {
|
||||
pub fn ticks(units: &RulerUnits, rule: TickRule<'_>, zoom: f64, values: RangeInclusive<f64>) -> Vec<RulerTick> {
|
||||
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);
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user