Skip to main content

AbsoluteAxis

Enum AbsoluteAxis 

Source
pub enum AbsoluteAxis {
    Horizontal,
    Vertical,
}
Expand description

The simple absolute horizontal and vertical axis

Variants§

§

Horizontal

The horizontal axis

§

Vertical

The vertical axis

Implementations§

Source§

impl AbsoluteAxis

Source

pub const fn other_axis(&self) -> Self

Returns the other variant of the enum

Examples found in repository?
examples/common/text.rs (line 32)
19pub fn text_measure_function(
20    known_dimensions: takia::geometry::Size<Option<f32>>,
21    available_space: takia::geometry::Size<takia::style::AvailableSpace>,
22    text_context: &TextContext,
23    font_metrics: &FontMetrics,
24) -> takia::geometry::Size<f32> {
25    use takia::geometry::AbsoluteAxis;
26    use takia::prelude::*;
27
28    let inline_axis = match text_context.writing_mode {
29        WritingMode::Horizontal => AbsoluteAxis::Horizontal,
30        WritingMode::Vertical => AbsoluteAxis::Vertical,
31    };
32    let block_axis = inline_axis.other_axis();
33    let words: Vec<&str> = text_context.text_content.split_whitespace().collect();
34
35    if words.is_empty() {
36        return Size::ZERO;
37    }
38
39    let min_line_length: usize = words.iter().map(|line| line.len()).max().unwrap_or(0);
40    let max_line_length: usize = words.iter().map(|line| line.len()).sum();
41    let inline_size =
42        known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
43            AvailableSpace::MinContent => min_line_length as f32 * font_metrics.char_width,
44            AvailableSpace::MaxContent => max_line_length as f32 * font_metrics.char_width,
45            AvailableSpace::Definite(inline_size) => inline_size
46                .min(max_line_length as f32 * font_metrics.char_width)
47                .max(min_line_length as f32 * font_metrics.char_width),
48        });
49    let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
50        let inline_line_length = (inline_size / font_metrics.char_width).floor() as usize;
51        let mut line_count = 1;
52        let mut current_line_length = 0;
53        for word in &words {
54            if current_line_length == 0 {
55                // first word
56                current_line_length = word.len();
57            } else if current_line_length + word.len() + 1 > inline_line_length {
58                // every word past the first needs to check for line length including the space between words
59                // note: a real implementation of this should handle whitespace characters other than ' '
60                // and do something more sophisticated for long words
61                line_count += 1;
62                current_line_length = word.len();
63            } else {
64                // add the word and a space
65                current_line_length += word.len() + 1;
66            };
67        }
68        (line_count as f32) * font_metrics.char_height
69    });
70
71    match text_context.writing_mode {
72        WritingMode::Horizontal => Size { width: inline_size, height: block_size },
73        WritingMode::Vertical => Size { width: block_size, height: inline_size },
74    }
75}

Trait Implementations§

Source§

impl Clone for AbsoluteAxis

Source§

fn clone(&self) -> Self

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 AbsoluteAxis

Source§

impl Debug for AbsoluteAxis

Source§

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

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

impl Eq for AbsoluteAxis

Source§

impl From<AbsoluteAxis> for RequestedAxis

Source§

fn from(value: AbsoluteAxis) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for AbsoluteAxis

Source§

fn eq(&self, other: &Self) -> 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 AbsoluteAxis

Source§

impl TryFrom<RequestedAxis> for AbsoluteAxis

Source§

type Error = ()

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

fn try_from(value: RequestedAxis) -> Result<Self, Self::Error>

Performs the conversion.

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<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.