Skip to main content

whisker_engine/text/
paragraph.rs

1use super::*;
2
3/// One logical inline Text with fully resolved style.
4#[derive(Clone, Debug)]
5pub struct ResolvedTextRun {
6    /// UTF-8 byte range in the complete paragraph.
7    pub range: whisker_protocol::TextByteRange,
8    /// Runtime identity used for logical event propagation.
9    pub span: whisker_protocol::TextSpanId,
10    /// Logical inline ancestor with a tap action.
11    pub action: Option<whisker_protocol::TextSpanId>,
12    /// Resolved style of the inline Text.
13    pub style: ComputedStyle,
14    /// Whether box paint belongs to a logical inline span.
15    pub inline_box_paint: bool,
16}
17
18/// An inline subtree whose dimensions are resolved by the layout adapter.
19#[derive(Clone, Debug, PartialEq)]
20pub struct InlineAttachmentInput {
21    /// Whether this subtree replaces the hidden suffix.
22    pub truncation: bool,
23    /// Explicit alternative used by selection copy.
24    pub label: Option<String>,
25    /// Retained subtree root.
26    pub node: whisker_protocol::NodeId,
27    /// Object replacement character in the paragraph.
28    pub range: whisker_protocol::TextByteRange,
29    /// Position relative to the surrounding line.
30    pub alignment: whisker_protocol::InlineAlignment,
31}
32
33/// Lowers a paragraph while keeping metric and paint-only changes separate.
34pub fn lower_rich_text(
35    input: &PlainTextInput,
36    style: &ComputedStyle,
37    runs: &[ResolvedTextRun],
38    attachments: &[InlineAttachmentInput],
39) -> LoweredPlainText {
40    let mut lowered = lower_plain_text(input, style);
41    if runs.is_empty() && attachments.is_empty() {
42        return lowered;
43    }
44    let mut metric_hash = DefaultHasher::new();
45    lowered.measurement.style_hash.hash(&mut metric_hash);
46    for run in runs {
47        let resolved = lower_plain_text(&PlainTextInput::new(""), &run.style);
48        run.style.vertical_align().hash(&mut metric_hash);
49        run.range.hash(&mut metric_hash);
50        hash_font_metrics(run.style.inherited_text(), &mut metric_hash);
51        let mut metrics = resolved.content.payload.style;
52        metrics.line_height = lowered.content.payload.style.line_height;
53        lowered
54            .content
55            .payload
56            .runs
57            .push(whisker_protocol::TextMeasureRun {
58                alignment: inline_alignment(run.style.vertical_align()),
59                range: run.range,
60                style: metrics,
61            });
62        lowered.content.runs.push(whisker_protocol::TextPaintRun {
63            range: run.range,
64            span: run.span,
65            action: run.action,
66            paint: resolved.content.paint,
67            background: run
68                .inline_box_paint
69                .then(|| lower_color(&run.style.paint().background_color)),
70            background_radii: whisker_protocol::PaintCorners {
71                top_left: crate::paint::corner_radius(&run.style.paint().border_radii.top_left),
72                top_right: crate::paint::corner_radius(&run.style.paint().border_radii.top_right),
73                bottom_right: crate::paint::corner_radius(
74                    &run.style.paint().border_radii.bottom_right,
75                ),
76                bottom_left: crate::paint::corner_radius(
77                    &run.style.paint().border_radii.bottom_left,
78                ),
79            },
80        });
81    }
82    for attachment in attachments {
83        attachment.truncation.hash(&mut metric_hash);
84        attachment.range.hash(&mut metric_hash);
85        attachment.node.hash(&mut metric_hash);
86        std::mem::discriminant(&attachment.alignment).hash(&mut metric_hash);
87        if let whisker_protocol::InlineAlignment::Offset(value) = attachment.alignment {
88            value.to_bits().hash(&mut metric_hash);
89        }
90    }
91    lowered.measurement.style_hash = metric_hash.finish();
92    lowered.measurement.payload = MeasurementPayload::Text(lowered.content.payload.clone());
93    lowered
94}
95
96fn inline_alignment(
97    value: whisker_style::ComputedVerticalAlign,
98) -> whisker_protocol::InlineAlignment {
99    use whisker_protocol::InlineAlignment as Inline;
100    use whisker_style::ComputedVerticalAlign as Computed;
101    match value {
102        Computed::Baseline => Inline::Baseline,
103        Computed::Top => Inline::Top,
104        Computed::Middle => Inline::Middle,
105        Computed::Bottom => Inline::Bottom,
106        Computed::Offset(value) => Inline::Offset(value.get()),
107    }
108}