style(geom): M14: remove review churn

This commit is contained in:
Cédric Legrand committed 2026-11-14 17:55:18 +01:00
1 parent 3bf7a5dbbe
commit 235a38c665
4 files changed
+2 -52

No files matched your search

+1 -36
View File
@@ -1,5 +1,4 @@
//! `WordCraft` geometry and units.
//!
//! WordCraft geometry and units.
//! Layout works in **points** (1/72 inch, `f32`). File formats use other units: OOXML uses
//! twentieths of a point (twips) for most lengths, half-points for font sizes, eighths of a point
//! for borders and English Metric Units (EMU, 914 400 per inch) for drawings. The conversions live
@@ -25,44 +24,37 @@ pub const EMU_PER_PT: f32 = 12_700.0;
pub const PT_PER_CM: f32 = 72.0 / 2.54;
/// Twips → points.
#[must_use]
#[expect(clippy::cast_precision_loss, reason = "The f32 return type intentionally rounds i64 inputs to f32 precision.")]
pub fn twips(v: i64) -> f32 {
v as f32 / TWIPS_PER_PT
}
/// Points → twips (rounded).
#[must_use]
#[expect(clippy::cast_possible_truncation, reason = "The value is rounded first; Rust's float-to-integer cast saturates outside the i64 range.")]
pub fn to_twips(pt: f32) -> i64 {
finite(pt * TWIPS_PER_PT).round() as i64
}
/// EMU → points.
#[must_use]
#[expect(clippy::cast_precision_loss, reason = "The f32 return type intentionally rounds i64 inputs to f32 precision.")]
pub fn emu(v: i64) -> f32 {
v as f32 / EMU_PER_PT
}
/// Points → EMU (rounded).
#[must_use]
#[expect(clippy::cast_possible_truncation, reason = "The value is rounded first; Rust's float-to-integer cast saturates outside the i64 range.")]
pub fn to_emu(pt: f32) -> i64 {
(f64::from(finite(pt)) * f64::from(EMU_PER_PT)).round() as i64
}
/// Half-points (OOXML font sizes) → points.
#[must_use]
#[expect(clippy::cast_precision_loss, reason = "The f32 return type intentionally rounds i64 inputs to f32 precision.")]
pub fn half_points(v: i64) -> f32 {
v as f32 / 2.0
}
/// Points → half-points (rounded).
#[must_use]
#[expect(clippy::cast_possible_truncation, reason = "The value is rounded first; Rust's float-to-integer cast saturates outside the i64 range.")]
pub fn to_half_points(pt: f32) -> i64 {
(finite(pt) * 2.0).round() as i64
}
/// Replace NaN/inf with 0 so casts and arithmetic stay defined.
#[must_use]
pub fn finite(v: f32) -> f32 {
if v.is_finite() { v } else { 0.0 }
}
@@ -80,7 +72,6 @@ pub enum Unit {
}
impl Unit {
#[must_use]
pub fn suffix(self) -> &'static str {
match self {
Unit::Inches => "\"",
@@ -90,7 +81,6 @@ impl Unit {
Unit::Picas => " pi",
}
}
#[must_use]
pub fn pt_per_unit(self) -> f32 {
match self {
Unit::Inches => PT_PER_IN,
@@ -101,17 +91,14 @@ impl Unit {
}
}
/// Format `pt` in this unit the way Word's fields show it (`1"`, `2.54 cm`, `12 pt`).
#[must_use]
pub fn format(self, pt: f32) -> String {
self.format_with(pt, '.')
}
/// [`Unit::format`] with the region's decimal separator (`2,54 cm` with `,`).
#[must_use]
pub fn format_with(self, pt: f32, decimal: char) -> String {
format!("{}{}", self.number(pt, decimal), self.suffix())
}
/// `pt` as a number in this unit, at most two decimals, without the unit (`2,54` with `,`).
#[must_use]
pub fn number(self, pt: f32, decimal: char) -> String {
let v = finite(pt) / self.pt_per_unit();
let s = format!("{v:.2}");
@@ -124,7 +111,6 @@ impl Unit {
/// Parse a measurement typed by a user: `1"`, `1 in`, `2.5cm`, `12pt`, `3 pi`, `10 mm`, `1.5 li`
/// (lines are 12 pt). A bare number is in `default`. Returns points. Either decimal separator
/// works (`2,5 cm` and `2.5 cm`), whatever the region.
#[must_use]
pub fn parse_measure(s: &str, default: Unit) -> Option<f32> {
let t = s.trim().to_ascii_lowercase().replace('\u{2212}', "-");
if t.is_empty() {
@@ -161,7 +147,6 @@ pub struct Point {
}
impl Point {
#[must_use]
pub const fn new(x: f32, y: f32) -> Self {
Point { x, y }
}
@@ -177,45 +162,36 @@ pub struct Rect {
}
impl Rect {
#[must_use]
pub fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
Rect { x: finite(x), y: finite(y), w: finite(w).max(0.0), h: finite(h).max(0.0) }
}
#[must_use]
pub fn right(&self) -> f32 {
self.x + self.w
}
#[must_use]
pub fn bottom(&self) -> f32 {
self.y + self.h
}
#[must_use]
pub fn contains(&self, p: Point) -> bool {
p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
}
#[must_use]
pub fn intersects(&self, o: &Rect) -> bool {
self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
}
#[must_use]
pub fn union(&self, o: &Rect) -> Rect {
let x = self.x.min(o.x);
let y = self.y.min(o.y);
Rect::new(x, y, self.right().max(o.right()) - x, self.bottom().max(o.bottom()) - y)
}
#[must_use]
pub fn inset(&self, l: f32, t: f32, r: f32, b: f32) -> Rect {
Rect::new(self.x + l, self.y + t, self.w - l - r, self.h - t - b)
}
/// Grown by `d` on every side (shrunk for negative `d`, down to zero size).
#[must_use]
pub fn expand(&self, d: f32) -> Rect {
self.inset(-d, -d, -d, -d)
}
}
/// An angle in degrees brought into `0..360` (NaN and infinities become 0).
#[must_use]
pub fn normalize_degrees(deg: f32) -> f32 {
if !deg.is_finite() {
return 0.0;
@@ -237,13 +213,11 @@ pub struct Spin {
impl Spin {
/// A spin with its angle normalised to `0..360`.
#[must_use]
pub fn new(deg: f32, flip_h: bool, flip_v: bool) -> Spin {
Spin { deg: normalize_degrees(deg), flip_h, flip_v }
}
/// No rotation and no mirroring.
#[must_use]
pub fn is_identity(&self) -> bool {
normalize_degrees(self.deg) == 0.0 && !self.flip_h && !self.flip_v
}
@@ -259,7 +233,6 @@ impl Spin {
/// The affine map (a, b, c, d, e, f: x' = a·x + c·y + e, y' = b·x + d·y + f) turning the page
/// about point (`cx`, `cy`).
#[must_use]
pub fn matrix(&self, cx: f32, cy: f32) -> [f32; 6] {
let [a, b, c, d] = self.linear();
let (cx, cy) = (finite(cx), finite(cy));
@@ -267,7 +240,6 @@ impl Spin {
}
/// Where point (`x`, `y`) goes when turned about (`cx`, `cy`).
#[must_use]
pub fn apply(&self, cx: f32, cy: f32, x: f32, y: f32) -> (f32, f32) {
let [x_from_x, y_from_x, x_from_y, y_from_y] = self.linear();
let (relative_x, relative_y) = (x - cx, y - cy);
@@ -275,7 +247,6 @@ impl Spin {
}
/// The point that [`Spin::apply`] takes to (`x`, `y`).
#[must_use]
pub fn unapply(&self, cx: f32, cy: f32, x: f32, y: f32) -> (f32, f32) {
// The linear part is orthogonal: its inverse is its transpose.
let [x_from_x, y_from_x, x_from_y, y_from_y] = self.linear();
@@ -284,14 +255,12 @@ impl Spin {
}
/// The axis-aligned box `r` covers once turned about its centre.
#[must_use]
pub fn bounds(&self, r: Rect) -> Rect {
let (bw, bh) = self.extent(r.w, r.h);
Rect::new(r.x + (r.w - bw) / 2.0, r.y + (r.h - bh) / 2.0, bw, bh)
}
/// The width and height of the box a `w` × `h` rectangle covers once turned.
#[must_use]
pub fn extent(&self, w: f32, h: f32) -> (f32, f32) {
let (width, height) = (finite(w).abs(), finite(h).abs());
let radians = normalize_degrees(self.deg).to_radians();
@@ -301,7 +270,6 @@ impl Spin {
/// This spin applied inside `outer` (a group member's own spin within its group's): the one
/// spin doing both.
#[must_use]
pub fn within(self, outer: Spin) -> Spin {
// outer = R(θo)·Fo, self = R(θs)·Fs; Fo·R(θ) = R(±θ)·Fo (one mirror reverses the turn).
let one_mirror = outer.flip_h != outer.flip_v;
@@ -327,7 +295,6 @@ pub const PAPER_SIZES: &[(&str, f32, f32)] = &[
];
/// The paper size whose dimensions match (either orientation, within half a point).
#[must_use]
pub fn paper_name(w: f32, h: f32) -> Option<&'static str> {
PAPER_SIZES
.iter()
@@ -336,7 +303,6 @@ pub fn paper_name(w: f32, h: f32) -> Option<&'static str> {
}
/// The standard paper size named `name` (case-insensitive), as (name, width, height) in points.
#[must_use]
pub fn paper_by_name(name: &str) -> Option<(&'static str, f32, f32)> {
let name = name.trim();
PAPER_SIZES.iter().find(|(n, _, _)| n.eq_ignore_ascii_case(name)).copied()
@@ -359,7 +325,6 @@ pub struct PaperFit {
/// page's orientation, the page is scaled up or down by one factor to fill it and centred, as
/// Word's Pages per sheet › Scale to paper size does. Degenerate or non-finite sizes give the
/// page as it is (scale 1 on paper of its own size).
#[must_use]
pub fn fit_to_paper(page_w: f32, page_h: f32, paper_w: f32, paper_h: f32) -> PaperFit {
let ok = |v: f32| v.is_finite() && v >= 1.0;
let as_is = PaperFit { paper_w: page_w, paper_h: page_h, k: 1.0, dx: 0.0, dy: 0.0 };
+1 -3
View File
@@ -13,7 +13,7 @@ use image::{DynamicImage, ImageDecoder, ImageFormat};
/// Why a picture isn't decoded.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Refusal {
/// Not a raster picture `WordCraft` decodes (a metafile, SVG or other bytes).
/// Not a raster picture WordCraft decodes (a metafile, SVG or other bytes).
NotAPicture,
/// A kind of picture that is never decoded, because its decoder can't be held to a budget.
Unsupported(&'static str),
@@ -159,7 +159,6 @@ fn sized(width: u32, height: u32, native: u64, held: u64) -> Result<(u32, u32, u
impl Probe<'_> {
/// Bytes decoding takes at its peak.
#[must_use]
pub fn peak_bytes(&self) -> u64 {
self.peak
}
@@ -177,7 +176,6 @@ impl Probe<'_> {
}
/// Whether decoding fits `budget` bytes.
#[must_use]
pub fn fits(&self, budget: u64) -> bool {
self.peak <= budget
}
-5
View File
@@ -8,13 +8,11 @@ pub const MAX_DECODED_BYTES: u64 = 512 * 1024 * 1024;
pub const MAX_RASTER_PIXELS: u64 = 100_000_000;
/// Bytes of a `w`×`h` RGBA8 raster, None on overflow.
#[must_use]
pub fn rgba_bytes(w: u32, h: u32) -> Option<u64> {
u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)
}
/// Whether a `w`×`h` RGBA8 raster fits `budget` bytes.
#[must_use]
pub fn rgba_fits(w: u32, h: u32, budget: u64) -> bool {
w > 0 && h > 0 && rgba_bytes(w, h).is_some_and(|b| b <= budget)
}
@@ -22,7 +20,6 @@ pub fn rgba_fits(w: u32, h: u32, budget: u64) -> bool {
/// Whether decoding a `w`×`h` picture fits `budget` bytes at its peak: the decoder's buffer and
/// the RGBA conversion (or its premultiplied copy) are alive together, so two RGBA buffers. For a
/// decoder whose buffer isn't four bytes a pixel, see [`decode_peak_fits`].
#[must_use]
pub fn decode_fits(w: u32, h: u32, budget: u64) -> bool {
decode_peak_fits(w, h, 4, budget)
}
@@ -30,14 +27,12 @@ pub fn decode_fits(w: u32, h: u32, budget: u64) -> bool {
/// [`decode_fits`] for a decoder that hands back `native` bytes a pixel (8 for 16-bit RGBA, 16 for
/// float RGBA): its buffer lives alongside the RGBA8 conversion, then the conversion alongside its
/// premultiplied copy, so the peak is the larger of `native + 4` and `8` bytes a pixel.
#[must_use]
pub fn decode_peak_fits(w: u32, h: u32, native: u64, budget: u64) -> bool {
let per_pixel = native.saturating_add(4).max(8);
w > 0 && h > 0 && u64::from(w).checked_mul(u64::from(h)).and_then(|p| p.checked_mul(per_pixel)).is_some_and(|b| b <= budget)
}
/// `w`×`h` shrunk (aspect kept, each side at least 1) until it holds at most `max_pixels`.
#[must_use]
pub fn fit_pixels(w: u32, h: u32, max_pixels: u64) -> (u32, u32) {
let (w, h) = (w.max(1), h.max(1));
let max_pixels = max_pixels.max(1);
-8
View File
@@ -63,7 +63,6 @@ impl Region {
/// The region a BCP 47 or POSIX locale tag names (`pl-PL`, `pl_PL.UTF-8@euro`,
/// `zh-Hans-CN`, `en-US-u-ms-metric`). An empty or unreadable tag, `C` and `POSIX` are
/// `en-US`; a bare `en` is the United States, any other bare language has no territory.
#[must_use]
pub fn from_tag(tag: &str) -> Region {
// Locale tags are short; anything long is junk and only its start is read.
let tag: String = tag.chars().take(64).collect();
@@ -98,7 +97,6 @@ impl Region {
/// This region with another territory (from a platform setting that names only the
/// territory, like Windows' home location). A value that isn't a territory code is ignored.
#[must_use]
pub fn with_territory(mut self, territory: &str) -> Region {
let t = territory.trim();
let ok = (t.len() == 2 && t.chars().all(|c| c.is_ascii_alphabetic())) || (t.len() == 3 && t.chars().all(|c| c.is_ascii_digit()));
@@ -111,7 +109,6 @@ impl Region {
/// This region with the paper and the measurement system taken from their own locale tags,
/// when the platform sets them apart (Unix `LC_PAPER`, `LC_MEASUREMENT`): `LANG=pl_PL.UTF-8`
/// with `LC_PAPER=en_US.UTF-8` writes `2,54 cm` on Letter.
#[must_use]
pub fn with_paper_and_measurement(mut self, paper: Option<&str>, measurement: Option<&str>) -> Region {
if let Some(t) = paper {
self.letter = Some(Region::from_tag(t).uses_letter());
@@ -137,25 +134,21 @@ impl Region {
}
/// Does a new document start on US Letter here (otherwise A4)?
#[must_use]
pub fn uses_letter(&self) -> bool {
self.letter.unwrap_or_else(|| LETTER.contains(&self.territory.as_str()))
}
/// The default paper's name and portrait size in points: Letter (612 × 792) or A4.
#[must_use]
pub fn paper(&self) -> (&'static str, f32, f32) {
if self.uses_letter() { ("Letter", 612.0, 792.0) } else { ("A4", 595.28, 841.89) }
}
/// The unit lengths are shown in by default: inches in the US, centimetres elsewhere.
#[must_use]
pub fn units(&self) -> Unit {
if self.inches.unwrap_or_else(|| INCHES.contains(&self.territory.as_str())) { Unit::Inches } else { Unit::Centimeters }
}
/// The decimal separator numbers are written with here (`,` or `.`).
#[must_use]
pub fn decimal(&self) -> char {
let lang = self.language.as_str();
let t = self.territory.as_str();
@@ -167,7 +160,6 @@ impl Region {
}
/// `pt` in `unit` written the way this region writes numbers (`2,54 cm` in Poland).
#[must_use]
pub fn format(&self, unit: Unit, pt: f32) -> String {
unit.format_with(pt, self.decimal())
}