whisker_engine/text/
paragraph.rs1use super::*;
2
3#[derive(Clone, Debug)]
5pub struct ResolvedTextRun {
6 pub range: whisker_protocol::TextByteRange,
8 pub span: whisker_protocol::TextSpanId,
10 pub action: Option<whisker_protocol::TextSpanId>,
12 pub style: ComputedStyle,
14 pub inline_box_paint: bool,
16}
17
18#[derive(Clone, Debug, PartialEq)]
20pub struct InlineAttachmentInput {
21 pub truncation: bool,
23 pub label: Option<String>,
25 pub node: whisker_protocol::NodeId,
27 pub range: whisker_protocol::TextByteRange,
29 pub alignment: whisker_protocol::InlineAlignment,
31}
32
33pub 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}