cranpose-render-wgpu 0.28.0

GPU renderer for Cranpose scenes through wgpu
Documentation
use cranpose_render_common::{
    graph::{LayerNode, ProjectiveTransform, RenderNode},
    text_fonts::TextFonts,
};
use cranpose_ui_graphics::{GraphicsLayer, Point, Rect};

pub(crate) fn layer_node(
    rect: Rect,
    transform_to_parent: ProjectiveTransform,
    graphics_layer: GraphicsLayer,
    children: Vec<RenderNode>,
) -> LayerNode {
    moved_to(
        Point::new(rect.x, rect.y),
        LayerNode {
            size: rect.size(),
            transform_to_parent,
            graphics_layer: graphics_layer.into(),
            children,
            ..Default::default()
        },
    )
}

// Moves `layer`'s rect and draws to `origin` of its space, where its child
// layers and inline nodes keep their place.
pub(crate) fn moved_to(origin: Point, mut layer: LayerNode) -> LayerNode {
    layer.transform_to_parent =
        ProjectiveTransform::translation(origin.x, origin.y).then(layer.transform_to_parent);
    let back = ProjectiveTransform::translation(-origin.x, -origin.y);
    for child in &mut layer.children {
        match child {
            RenderNode::Layer(child) => {
                child.transform_to_parent = child.transform_to_parent.then(back);
            }
            RenderNode::Inline(inline) => {
                inline.origin = Point::new(inline.origin.x - origin.x, inline.origin.y - origin.y);
            }
            RenderNode::Primitive(_) | RenderNode::DrawRun(_) => {}
        }
    }
    layer
}

// Runs `block` in an app context whose text lays out with the font a
// renderer without app fonts draws with.
fn in_layout_context<R>(block: impl FnOnce() -> R) -> R {
    let context = cranpose_ui::AppContext::new();
    context.set_text_fonts(TextFonts::from_fonts_or_default(&[]).resolver());
    context.enter(block)
}

// `node` with its text laid out in its style as wide as its rect.
pub(crate) fn with_layout(
    mut node: cranpose_render_common::graph::TextPrimitiveNode,
) -> cranpose_render_common::graph::TextPrimitiveNode {
    node.layout = in_layout_context(|| {
        cranpose_ui::text::prepare_text_layout(
            &std::sync::Arc::clone(&node.render_text).into(),
            &node.text_style,
            cranpose_ui::TextLayoutOptions {
                overflow: node.overflow,
                ..cranpose_ui::TextLayoutOptions::default()
            },
            Some(node.rect.width),
        )
        .layout
        .clone()
    });
    node
}

// `text` in the default style laid out on one line.
pub(crate) fn prepared_text(text: &str) -> std::rc::Rc<cranpose_ui::PreparedTextLayout> {
    in_layout_context(|| {
        cranpose_ui::text::prepare_text_layout(
            &cranpose_ui::text::AnnotatedString::from(text),
            &cranpose_ui::text::TextStyle::default(),
            cranpose_ui::TextLayoutOptions::default(),
            None,
        )
    })
}

// The layout a draw scope gives `text` in `style`.
pub(crate) fn draw_text_layout(
    text: &str,
    style: &cranpose_ui_graphics::DrawTextStyle,
) -> Option<cranpose_ui_graphics::TextLayoutHandle> {
    use cranpose_ui_graphics::DrawTextMeasurer as _;
    in_layout_context(|| {
        cranpose_ui::text::AppContextTextMeasurer
            .measure_text(text, style)
            .layout
    })
}