1 pub const LOREM_IPSUM : &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
2
3 pub struct FontMetrics {
4 pub char_width: f32,
5 pub char_height: f32,
6 }
7
8 #[allow(dead_code)]
9 pub enum WritingMode {
10 Horizontal,
11 Vertical,
12 }
13
14 pub struct TextContext {
15 pub text_content: String,
16 pub writing_mode: WritingMode,
17 }
18
text_measure_function( known_dimensions: taffy::geometry::Size<Option<f32>>, available_space: taffy::geometry::Size<taffy::style::AvailableSpace>, text_context: &TextContext, font_metrics: &FontMetrics, ) -> taffy::geometry::Size<f32>19 pub fn text_measure_function(
20 known_dimensions: taffy::geometry::Size<Option<f32>>,
21 available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
22 text_context: &TextContext,
23 font_metrics: &FontMetrics,
24 ) -> taffy::geometry::Size<f32> {
25 use taffy::geometry::AbsoluteAxis;
26 use taffy::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 }
76