Skip to main content

RhythmBlockMetrics

Struct RhythmBlockMetrics 

Source
pub struct RhythmBlockMetrics { /* private fields */ }
Expand description

A text block on the rhythm grid as pure geometry: one line’s metrics plus baseline or ink-top anchors, with both fragment geometry and the exact row arithmetic a virtualized renderer needs. new is the pure form of rhythm_block; ink_anchored pairs an ink opening with its whole-row close. Either mode spans a whole number of rhythm rows for any number of lines, so blocks and fragments compose without breaking the page rhythm.

§Fragments

A block split at visual-line boundaries keeps its rhythm phase because every line advances by whole rows. The first / middle / last height methods expose concrete fragment geometry, while first_rows, middle_rows, and last_rows expose exact i32 cursor deltas. A virtualizer can accumulate those deltas in an i64, rebase near the viewport, then derive only a visible fragment’s baseline with baseline_at_row and line-box top with RhythmLineMetrics::paint_origin_for.

§Examples

use rhythm_gpui::{Rhythm, RhythmBlockMetrics, RhythmLineMetrics};

let grid = Rhythm::new(8.0);
// Georgia-like 16px body on a 3-unit (24px) line.
let line = RhythmLineMetrics::new(14.67, 3.51, 3, grid);
let block = RhythmBlockMetrics::new(line, 3, 1);

// Five wrapped lines cover sixteen whole rhythm rows…
assert_eq!(block.rows(5), 3 + 1 + 4 * 3);

// …and the first baseline sits `top` grid lines below the block top.
assert_eq!(block.first_baseline(), grid.height(3));
let origin_y = line.paint_origin_for(block.first_baseline());
assert!((origin_y + line.baseline_above() - grid.height(3)).abs() < 1e-4);

Implementations§

Source§

impl RhythmBlockMetrics

Source

pub const fn new(line: RhythmLineMetrics, top: i32, bottom: i32) -> Self

A block opening top rhythm units above the first baseline and closing bottom units below the last. Negative counts are meaningful for margin-style layouts, matching baseline_top / baseline_bottom.

Source

pub fn ink_anchored( line: RhythmLineMetrics, ink_ascent: f32, top: i32, bottom: i32, ) -> Self

An ink-anchored block: opens top rhythm units above the anchored ink top and closes with the paired trimmed space, retaining that ink phase across fragments while the whole block still spans integer rows.

ink_ascent is the anchored ink’s height above the baseline: pass a Latin cap height, or a caller-supplied CJK ICF ascent to anchor the ideographic character face instead.

§Panics

Panics when ink_ascent is zero, negative, or non-finite.

Source

pub const fn line(&self) -> RhythmLineMetrics

The line metrics the block is set in.

Source

pub const fn top_rhythms(&self) -> i32

Opening rhythm units above the anchor (baseline or ink top). A row count, not a length — opening is the pixel spacing it produces.

Source

pub const fn bottom_rhythms(&self) -> i32

Closing rhythm units below the last baseline, or the paired ink close. A row count, not a length; see closing.

Source

pub fn opening(&self) -> f32

Space from the block’s top edge down to its first line box. Negative values are meaningful as margins rather than padding.

Source

pub fn closing(&self) -> f32

Space from the last line box down to the block’s whole-row bottom edge.

Source

pub fn first_baseline(&self) -> f32

Distance from the block’s top edge down to the first baseline: top whole rhythm units for baseline anchors, plus the ink ascent for ink anchors. Applies to first and single fragments; in a middle/last fragment the first baseline sits RhythmLineMetrics::baseline_above below the fragment top.

Source

pub fn height(&self, lines: u32) -> f32

Height of the whole block containing lines visual lines.

§Panics

Panics when lines is zero.

Source

pub fn first_height(&self, lines: u32) -> f32

Height of a first fragment: opening plus lines whole line boxes.

§Panics

Panics when lines is zero.

Source

pub fn middle_height(&self, lines: u32) -> f32

Height of a middle fragment containing lines whole line boxes.

§Panics

Panics when lines is zero.

Source

pub fn last_height(&self, lines: u32) -> f32

Height of a last fragment: lines whole line boxes plus the closing.

§Panics

Panics when lines is zero.

Source

pub fn rows(&self, lines: u32) -> i32

The whole number of rhythm rows a single-fragment block of lines visual lines covers, as exact integer arithmetic: top + bottom + (lines − 1) × line_rhythms for baseline anchors, or top + bottom + lines × line_rhythms for ink anchors. The block’s height in pixels is grid.height(rows(lines)).

§Panics

Panics when lines is zero or the resulting row count does not fit in i32.

Source

pub fn first_rows(&self, lines: u32) -> i32

Row-cursor delta from the block top to the baseline immediately after a first fragment of lines visual lines. Add it to the block’s starting row, then pass the result to baseline_at_row to place the first line of the next fragment.

This and its two siblings partition rows exactly: first_rows(a) + middle_rows(b) + last_rows(c) == rows(a + b + c). These values are cursor transitions, not fragment heights converted to rows: continuation fragments start at a line-box top, generally between grid lines. Keeping the cursor as rows and deriving only the visible fragment’s baseline prevents accumulated floating-point drift.

§Examples
use rhythm_gpui::{Rhythm, RhythmBlockMetrics, RhythmLineMetrics};

let grid = Rhythm::new(8.0);
let line = RhythmLineMetrics::new(14.67, 3.51, 3, grid);
let block = RhythmBlockMetrics::new(line, 3, 1);

// A nine-line block split 2 / 4 / 3 across three viewport pages: the
// middle fragment starts two line advances below the first line box.
let first_origin = line.paint_origin_for(block.first_baseline());
let mut cursor = i64::from(block.first_rows(2));
let middle_origin = line.paint_origin_for(block.baseline_at_row(cursor));
assert!((middle_origin - (first_origin + 2.0 * line.line_height())).abs() < 1e-3);
cursor = cursor.checked_add(i64::from(block.middle_rows(4))).unwrap();
cursor = cursor.checked_add(i64::from(block.last_rows(3))).unwrap();
assert_eq!(cursor, i64::from(block.rows(9)));
§Panics

Panics when lines is zero or the row count does not fit in i32.

Source

pub fn middle_rows(&self, lines: u32) -> i32

Row-cursor delta across a middle fragment of lines visual lines: lines × line_rhythms. The cursor points to the fragment’s first baseline before this delta and the following fragment’s first baseline after it. See first_rows for the full contract.

§Panics

Panics when lines is zero or the row count does not fit in i32.

Source

pub fn baseline_at_row(&self, row: i64) -> f32

The baseline at an accumulated i64 grid-row cursor. Baseline-anchored blocks land on row × grid size; ink-anchored blocks retain their ink phase.

Keep the cursor as exact integer rows and rebase it near the viewport before this conversion. The returned coordinate is f32, so accepting i64 avoids an arbitrary saturating cast but cannot make enormous absolute pixel coordinates exact.

Pass this to RhythmLineMetrics::paint_origin_for to derive the fragment’s line-box top without accumulating preceding f32 heights.

Source

pub fn last_rows(&self, lines: u32) -> i32

Row-cursor delta from the first baseline of a last fragment of lines visual lines to the block’s whole-row bottom edge: lines − 1 line advances plus the closing for baseline anchors, or lines advances plus the paired ink close for ink anchors. See first_rows for the full contract.

§Panics

Panics when lines is zero or the row count does not fit in i32.

Source§

impl RhythmBlockMetrics

Pixels-typed mirrors of the two block values that stay inside a paint path’s Pixels chain: both are summed with grid lengths into the target baseline RhythmLineMetrics::paint_origin_for_px consumes. See those mirrors for the integration-boundary rule.

Source

pub fn first_baseline_px(&self) -> Pixels

Available on crate feature gpui only.

first_baseline in Pixels.

Source

pub fn baseline_at_row_px(&self, row: i64) -> Pixels

Available on crate feature gpui only.

baseline_at_row in Pixels.

Trait Implementations§

Source§

impl Clone for RhythmBlockMetrics

Source§

fn clone(&self) -> RhythmBlockMetrics

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for RhythmBlockMetrics

Source§

impl Debug for RhythmBlockMetrics

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for RhythmBlockMetrics

Source§

fn eq(&self, other: &RhythmBlockMetrics) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RhythmBlockMetrics

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more