pub enum AbsoluteAxis {
Horizontal,
Vertical,
}Expand description
The simple absolute horizontal and vertical axis
Variants§
Implementations§
Source§impl AbsoluteAxis
impl AbsoluteAxis
Sourcepub const fn other_axis(&self) -> Self
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
impl Clone for AbsoluteAxis
impl Copy for AbsoluteAxis
Source§impl Debug for AbsoluteAxis
impl Debug for AbsoluteAxis
impl Eq for AbsoluteAxis
Source§impl From<AbsoluteAxis> for RequestedAxis
impl From<AbsoluteAxis> for RequestedAxis
Source§fn from(value: AbsoluteAxis) -> Self
fn from(value: AbsoluteAxis) -> Self
Converts to this type from the input type.
Source§impl PartialEq for AbsoluteAxis
impl PartialEq for AbsoluteAxis
impl StructuralPartialEq for AbsoluteAxis
Source§impl TryFrom<RequestedAxis> for AbsoluteAxis
impl TryFrom<RequestedAxis> for AbsoluteAxis
Auto Trait Implementations§
impl Freeze for AbsoluteAxis
impl RefUnwindSafe for AbsoluteAxis
impl Send for AbsoluteAxis
impl Sync for AbsoluteAxis
impl Unpin for AbsoluteAxis
impl UnsafeUnpin for AbsoluteAxis
impl UnwindSafe for AbsoluteAxis
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more