-
Notifications
You must be signed in to change notification settings - Fork 161
Description
I'm new to Rust so this might be a basic problem. I couldn't find an example of MeasureFuncs in stretch.
I'm trying to create a text leaf node. I wrote a font loading mechanism which returns Font, and I wrote a basic text layout algorithm which takes a borrow to Font (lets call this function layout(in_bounds: Size<Number>, f: &Font) -> Size<f32>, where the returned vector is the bounds of the text. It also takes the bounds where we should layout text into). I'd like to be able to call this layout function inside the MeasureFunc, but no matter what I try it says font does not live long enough. borrowed value does not live long enough.
let mut stretch = stretch2::node::Stretch::new();
let font = load_font(...);
let text_node = stretch.new_leaf(
Style { ..Default::default() },
MeasureFunc::Boxed(Box::new(|size|{
layout(size, &f)
}))).unwrap();
stretch.compute_layout(text_node, Size{ width: Number::Defined(500), height: Number::Undefined }).unwrap();I tried boxing and Rc-ing the font with no luck.
let mut stretch = stretch::node::Stretch::new();
let font = load_font(...);
let rc_font = Rc::new(font);
let text_node = stretch.new_leaf(
Style { ..Default::default() },
MeasureFunc::BoxedBox::new(|size|{
layout(size, rc_font.as_ref())
}))).unwrap();
stretch.compute_layout(text_node, Size{ width: Number::Defined(500), height: Number::Undefined }).unwrap();The reason I want this is because the text layout should change based on the size of the container. What's the proper way to do this? An example of this in the future would be immensely useful!