Skip to main content

cotis_layout/layout_struct/
text_draw_payload.rs

1use cotis_defaults::element_configs::text_config::{TextConfig, TextStyle};
2use std::ops::Range;
3use std::sync::Arc;
4
5/// Text fragment attached to a [`RenderCommandOutputElement`](super::RenderCommandOutputElement).
6///
7/// Holds a byte range into shared source text plus the [`TextConfig`] and [`TextStyle`] used
8/// when the pipe emits a draw call. The range must lie on UTF-8 character boundaries.
9///
10/// With the `serialization` feature enabled, the source is stored as an owned [`String`]
11/// instead of [`Arc<str>`] so the payload can be serialized.
12#[derive(Clone, Debug)]
13#[cfg_attr(
14    feature = "serialization",
15    derive(serde::Serialize, serde::Deserialize)
16)]
17pub struct TextDrawPayload {
18    #[cfg(not(feature = "serialization"))]
19    source: Arc<str>,
20    #[cfg(feature = "serialization")]
21    source: String,
22    range: Range<usize>,
23    /// Typography settings from `cotis-defaults` (font, size, wrap mode, alignment).
24    pub config: TextConfig,
25    /// Color and other draw-time style.
26    pub style: TextStyle,
27}
28
29#[cfg(not(feature = "serialization"))]
30impl TextDrawPayload {
31    /// Creates a payload referencing shared source text without copying the string.
32    pub fn new_from_arc(
33        source: &Arc<str>,
34        range: Range<usize>,
35        config: TextConfig,
36        style: TextStyle,
37    ) -> Self {
38        Self {
39            source: source.clone(),
40            range,
41            config,
42            style,
43        }
44    }
45
46    /// Returns the UTF-8 substring described by `range`.
47    pub fn get_str(&self) -> &str {
48        &self.source[self.range.clone()]
49    }
50}
51
52#[cfg(feature = "serialization")]
53impl TextDrawPayload {
54    /// Creates a payload, copying source text into an owned [`String`] for serialization.
55    pub fn new_from_arc(
56        source: &Arc<str>,
57        range: Range<usize>,
58        config: TextConfig,
59        style: TextStyle,
60    ) -> Self {
61        Self {
62            source: source.to_string(),
63            range,
64            config,
65            style,
66        }
67    }
68
69    /// Returns the UTF-8 substring described by `range`.
70    pub fn get_str(&self) -> &str {
71        &self.source[self.range.clone()]
72    }
73}