Fix clippy::large_enum_variant lint in cosmic-text example (#811)
1mod image;
2use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping};
3use image::{image_measure_function, ImageContext};
4use taffy::prelude::*;
5
6pub 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.";
7
8struct CosmicTextContext {
9 buffer: cosmic_text::Buffer,
10}
11
12impl CosmicTextContext {
13 fn new(metrics: Metrics, text: &str, attrs: Attrs, font_system: &mut FontSystem) -> Self {
14 let mut buffer = Buffer::new_empty(metrics);
15 buffer.set_size(font_system, None, None);
16 buffer.set_text(font_system, text, attrs, Shaping::Advanced);
17 Self { buffer }
18 }
19
20 fn measure(
21 &mut self,
22 known_dimensions: taffy::Size<Option<f32>>,
23 available_space: taffy::Size<taffy::AvailableSpace>,
24 font_system: &mut FontSystem,
25 ) -> taffy::Size<f32> {
26 // Set width constraint
27 let width_constraint = known_dimensions.width.or(match available_space.width {
28 AvailableSpace::MinContent => Some(0.0),
29 AvailableSpace::MaxContent => None,
30 AvailableSpace::Definite(width) => Some(width),
31 });
32 self.buffer.set_size(font_system, width_constraint, None);
33
34 // Compute layout
35 self.buffer.shape_until_scroll(font_system, false);
36
37 // Determine measured size of text
38 let (width, total_lines) = self
39 .buffer
40 .layout_runs()
41 .fold((0.0, 0usize), |(width, total_lines), run| (run.line_w.max(width), total_lines + 1));
42 let height = total_lines as f32 * self.buffer.metrics().line_height;
43
44 taffy::Size { width, height }
45 }
46}
47
48#[allow(clippy::large_enum_variant)]
49enum NodeContext {
50 Text(CosmicTextContext),
51 Image(ImageContext),
52}
53
54impl NodeContext {
55 /// Constructor for a text node context
56 fn text(metrics: Metrics, text: &str, attrs: Attrs, font_system: &mut FontSystem) -> Self {
57 NodeContext::Text(CosmicTextContext::new(metrics, text, attrs, font_system))
58 }
59
60 /// Constructor for an image node context
61 fn image(width: f32, height: f32) -> Self {
62 NodeContext::Image(ImageContext { width, height })
63 }
64}
65
66fn measure_function(
67 known_dimensions: taffy::Size<Option<f32>>,
68 available_space: taffy::Size<taffy::AvailableSpace>,
69 node_context: Option<&mut NodeContext>,
70 font_system: &mut FontSystem,
71) -> Size<f32> {
72 if let Size { width: Some(width), height: Some(height) } = known_dimensions {
73 return Size { width, height };
74 }
75
76 match node_context {
77 None => Size::ZERO,
78 Some(NodeContext::Text(text_context)) => text_context.measure(known_dimensions, available_space, font_system),
79 Some(NodeContext::Image(image_context)) => image_measure_function(known_dimensions, image_context),
80 }
81}
82
83fn main() -> Result<(), taffy::TaffyError> {
84 let metrics = Metrics { font_size: 14.0, line_height: 16.0 };
85 let mut font_system = FontSystem::new();
86 let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
87
88 let text_node = taffy.new_leaf_with_context(
89 Style::default(),
90 NodeContext::text(metrics, LOREM_IPSUM, Attrs::new(), &mut font_system),
91 )?;
92
93 let image_node = taffy.new_leaf_with_context(Style::default(), NodeContext::image(400.0, 300.0))?;
94
95 let root = taffy.new_with_children(
96 Style {
97 display: Display::Flex,
98 flex_direction: FlexDirection::Column,
99 size: Size { width: length(200.0), height: auto() },
100 ..Default::default()
101 },
102 &[text_node, image_node],
103 )?;
104
105 // Compute layout and print result
106 taffy.compute_layout_with_measure(
107 root,
108 Size::MAX_CONTENT,
109 // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
110 // For example, you may wish to borrow a global font registry and pass it into your text measuring function
111 |known_dimensions, available_space, _node_id, node_context, _style| {
112 measure_function(known_dimensions, available_space, node_context, &mut font_system)
113 },
114 )?;
115 taffy.print_tree(root);
116
117 Ok(())
118}