cotis-layout 0.1.0-alpha.1

Flexbox-style layout engine for Cotis
Documentation
use cotis_defaults::element_configs::text_config::{TextConfig, TextStyle};
use std::ops::Range;
use std::sync::Arc;

/// Text fragment attached to a [`RenderCommandOutputElement`](super::RenderCommandOutputElement).
///
/// Holds a byte range into shared source text plus the [`TextConfig`] and [`TextStyle`] used
/// when the pipe emits a draw call. The range must lie on UTF-8 character boundaries.
///
/// With the `serialization` feature enabled, the source is stored as an owned [`String`]
/// instead of [`Arc<str>`] so the payload can be serialized.
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct TextDrawPayload {
    #[cfg(not(feature = "serialization"))]
    source: Arc<str>,
    #[cfg(feature = "serialization")]
    source: String,
    range: Range<usize>,
    /// Typography settings from `cotis-defaults` (font, size, wrap mode, alignment).
    pub config: TextConfig,
    /// Color and other draw-time style.
    pub style: TextStyle,
}

#[cfg(not(feature = "serialization"))]
impl TextDrawPayload {
    /// Creates a payload referencing shared source text without copying the string.
    pub fn new_from_arc(
        source: &Arc<str>,
        range: Range<usize>,
        config: TextConfig,
        style: TextStyle,
    ) -> Self {
        Self {
            source: source.clone(),
            range,
            config,
            style,
        }
    }

    /// Returns the UTF-8 substring described by `range`.
    pub fn get_str(&self) -> &str {
        &self.source[self.range.clone()]
    }
}

#[cfg(feature = "serialization")]
impl TextDrawPayload {
    /// Creates a payload, copying source text into an owned [`String`] for serialization.
    pub fn new_from_arc(
        source: &Arc<str>,
        range: Range<usize>,
        config: TextConfig,
        style: TextStyle,
    ) -> Self {
        Self {
            source: source.to_string(),
            range,
            config,
            style,
        }
    }

    /// Returns the UTF-8 substring described by `range`.
    pub fn get_str(&self) -> &str {
        &self.source[self.range.clone()]
    }
}