masonry_core 0.5.0

Traits and types of the Masonry toolkit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2026 the Xilem Authors and the Parley Authors
// SPDX-License-Identifier: Apache-2.0

//! An [`accesskit`] integration for Parley text layouts and editors.
//!
//! Adapted from the `editor` example of Parley 0.12, which replaced Parley's
//! former built-in `accesskit` feature.

use std::collections::HashMap;

use accesskit::{
    Node, NodeId, Rect, Role, TextAlign, TextDirection, TextPosition, TextSelection, TreeUpdate,
};
use parley::editing::{Cursor, PlainEditor, PlainEditorDriver, Selection};
use parley::layout::{Affinity, Alignment, Cluster, ClusterPath, Layout, LineMetrics, Run, Style};
use parley::style::{Brush, FontStyle};
use skrifa::FontRef;
use skrifa::raw::TableProvider;
use skrifa::raw::types::NameId;

/// The maximum number of characters in a single `TextRun` node.
///
/// AccessKit's `word_starts` are `u8`s, so a span can't describe more characters than that.
const MAX_CHARACTERS_PER_SPAN: usize = u8::MAX as usize + 1;

/// Maps a Parley [`Layout`] onto a tree of AccessKit [`Role::TextRun`] nodes.
///
/// The same instance should be reused across accessibility passes for a given layout, so
/// that unchanged parts of the text keep their node IDs.
#[derive(Clone, Default, Debug)]
pub struct LayoutAccessibility {
    // Each span is a sequence of clusters, in logical order, that all have an identical
    // style. The nth span is given the nth ID from this list, which is kept across passes.
    span_ids: Vec<NodeId>,
    // Map from node ID to the path of the span's first cluster.
    span_paths_by_access_id: HashMap<NodeId, ClusterPath>,
    // Map from cluster path to the cluster's position within its span.
    span_positions_by_cluster_path: HashMap<ClusterPath, SpanPosition>,
}

/// The position of a cluster within its accessibility span.
#[derive(Copy, Clone, Debug)]
struct SpanPosition {
    /// The index of the span in logical order.
    span_index: usize,
    /// The index of the cluster within the span's characters.
    character_index: usize,
}

impl LayoutAccessibility {
    fn span_id(&self, span_index: usize) -> Option<NodeId> {
        self.span_ids.get(span_index).copied()
    }

    /// Push a `TextRun` node for every style span in `layout` onto `update`, as children
    /// of `parent_node`.
    ///
    /// `text` must be the source text that `layout` was built from. `x_offset` and
    /// `y_offset` are added to the layout-relative coordinates of every node.
    ///
    /// `next_node_id` is called to allocate IDs for spans that weren't present on the
    /// previous pass. `set_brush_properties` is called once per span, and should map the
    /// span's brush onto AccessKit's color and text decoration properties.
    pub fn build_nodes<B: Brush>(
        &mut self,
        text: &str,
        layout: &Layout<B>,
        update: &mut TreeUpdate,
        parent_node: &mut Node,
        mut next_node_id: impl FnMut() -> NodeId,
        x_offset: f64,
        y_offset: f64,
        set_brush_properties: impl Fn(&mut Node, &Style<B>),
    ) {
        self.span_paths_by_access_id.clear();
        self.span_positions_by_cluster_path.clear();
        // The number of spans started so far, which is also the logical index
        // of the next span to start.
        let mut spans_started = 0;
        let mut runs = Vec::new();
        let alignment = layout.alignment();

        for (line_index, line) in layout.lines().enumerate() {
            let metrics = line.metrics();
            // Defer adding each run node until we reach either the next run
            // or the end of the line, so that we can link runs within a line.
            let mut last_node: Option<(NodeId, Node)> = None;

            // Compute run offsets from left to right, then sort them into text order.
            runs.clear();
            runs.reserve(line.len());
            {
                let mut run_offset = metrics.offset;
                for run in line.runs() {
                    let advance = run.advance();
                    runs.push((run, run_offset));
                    run_offset += advance;
                }
            }
            runs.sort_by_key(|(r, _)| r.text_range().start);

            for (run, run_offset) in runs.drain(..) {
                let mut span_path = run_start_path(line_index, &run);
                let (mut id, mut node) = self.span_id_and_node(
                    &mut next_node_id,
                    spans_started,
                    &run,
                    alignment,
                    span_path,
                );
                spans_started += 1;
                if run.is_empty() {
                    // Record the start of empty runs so that a cursor in them can still be addressed.
                    self.span_positions_by_cluster_path.insert(
                        span_path,
                        SpanPosition {
                            span_index: spans_started - 1,
                            character_index: 0,
                        },
                    );
                }

                if let Some((last_id, mut last_node)) = last_node.take() {
                    link_spans(last_id, &mut last_node, id, &mut node);
                    add_span(update, parent_node, last_id, last_node);
                }

                let mut prev_style_index: Option<u16> = None;
                let mut span = SpanData::default();
                let mut span_offset = 0.0;

                for cluster in run.clusters() {
                    let style_index = cluster.style_index();
                    if let Some(prev_index) = prev_style_index
                        && (prev_index != style_index
                            || span.character_lengths.len() >= MAX_CHARACTERS_PER_SPAN)
                    {
                        prev_style_index = None;
                        let span_advance = span.advance;
                        std::mem::take(&mut span).finish(
                            &mut node,
                            x_offset,
                            y_offset,
                            metrics,
                            run_offset + span_offset,
                        );
                        span_offset += span_advance;
                        (id, node) = {
                            let (old_id, mut old_node) = (id, node);
                            span_path = cluster.path();
                            let (new_id, mut new_node) = self.span_id_and_node(
                                &mut next_node_id,
                                spans_started,
                                &run,
                                alignment,
                                span_path,
                            );
                            spans_started += 1;
                            link_spans(old_id, &mut old_node, new_id, &mut new_node);
                            add_span(update, parent_node, old_id, old_node);
                            (new_id, new_node)
                        };
                    }

                    if prev_style_index.is_none() {
                        prev_style_index = Some(style_index);
                        let style = cluster.style();
                        set_brush_properties(&mut node, style);
                        if let Some(locale) = &style.locale {
                            node.set_language(locale.as_str());
                        }
                    }

                    let cluster_text = &text[cluster.text_range()];
                    span.text.push_str(cluster_text);
                    if cluster.is_word_boundary() && !cluster.is_space_or_nbsp() {
                        span.word_starts.push(span.character_lengths.len() as _);
                    }
                    let character_index = span.character_lengths.len();
                    span.character_lengths.push(cluster_text.len() as _);
                    span.character_positions.push(span.advance);
                    span.character_widths.push(cluster.advance());
                    span.advance += cluster.advance();
                    self.span_positions_by_cluster_path.insert(
                        cluster.path(),
                        SpanPosition {
                            span_index: spans_started - 1,
                            character_index,
                        },
                    );
                }

                span.finish(
                    &mut node,
                    x_offset,
                    y_offset,
                    metrics,
                    run_offset + span_offset,
                );
                last_node = Some((id, node));
            }

            if let Some((id, node)) = last_node {
                add_span(update, parent_node, id, node);
            }
        }
    }

    /// Convert an AccessKit position within the nodes built by [`Self::build_nodes`] into
    /// a Parley [`Cursor`].
    ///
    /// Returns `None` if the position doesn't refer to one of those nodes.
    pub fn cursor_from_access_position<B: Brush>(
        &self,
        pos: &TextPosition,
        layout: &Layout<B>,
    ) -> Option<Cursor> {
        let span_path = self.span_paths_by_access_id.get(&pos.node)?;
        let run = span_path.run(layout)?;
        let index = run
            .clusters()
            .skip_while(|cluster| cluster.path() != *span_path)
            .nth(pos.character_index)
            .map(|cluster| cluster.text_range().start)
            .unwrap_or(layout.text_len());
        Some(Cursor::from_byte_index(layout, index, Affinity::Downstream))
    }

    /// Convert a Parley [`Cursor`] into a position within the nodes built by
    /// [`Self::build_nodes`].
    ///
    /// Returns `None` if [`Self::build_nodes`] hasn't been called for this layout yet.
    pub fn cursor_to_access_position<B: Brush>(
        &self,
        cursor: Cursor,
        layout: &Layout<B>,
    ) -> Option<TextPosition> {
        if layout.text_len() == 0 {
            // If the text is empty, return the first node with a character index of 0.
            let run = layout.get(0)?.runs().next()?;
            let position = self
                .span_positions_by_cluster_path
                .get(&run_start_path(0, &run))?;
            return Some(TextPosition {
                node: self.span_id(position.span_index)?,
                character_index: 0,
            });
        }
        // Prefer the downstream cluster except at the end of the text, where we
        // choose the upstream cluster and add 1 to the character index.
        let (offset, path) = cursor
            .downstream_cluster(layout)
            .map(|cluster| (0, cluster.path()))
            .or_else(|| {
                cursor
                    .upstream_cluster(layout)
                    .map(|cluster| (1, cluster.path()))
            })?;
        // At the end of a layout ending with a newline, use the "phantom" run of the
        // trailing line so that AccessKit has correct visual geometry for the cursor.
        let (span_index, character_index) =
            if cursor.index() == layout.text_len() && ends_with_hard_line_break(layout) {
                let line_index = path.line_index() + 1;
                let run = layout.get(line_index)?.runs().next()?;
                let position = self
                    .span_positions_by_cluster_path
                    .get(&run_start_path(line_index, &run))?;
                (position.span_index, 0)
            } else {
                let position = self.span_positions_by_cluster_path.get(&path)?;
                (position.span_index, position.character_index + offset)
            };
        Some(TextPosition {
            node: self.span_id(span_index)?,
            character_index,
        })
    }

    /// Convert an AccessKit selection over the nodes built by [`Self::build_nodes`] into
    /// a Parley [`Selection`].
    ///
    /// Returns `None` if either endpoint doesn't refer to one of those nodes.
    pub fn selection_from_access_selection<B: Brush>(
        &self,
        selection: &TextSelection,
        layout: &Layout<B>,
    ) -> Option<Selection> {
        let anchor = self.cursor_from_access_position(&selection.anchor, layout)?;
        let focus = self.cursor_from_access_position(&selection.focus, layout)?;
        Some(Selection::new(anchor, focus))
    }

    /// Convert a Parley [`Selection`] into a selection over the nodes built by
    /// [`Self::build_nodes`].
    ///
    /// Returns `None` if [`Self::build_nodes`] hasn't been called for this layout yet.
    pub fn selection_to_access_selection<B: Brush>(
        &self,
        selection: &Selection,
        layout: &Layout<B>,
    ) -> Option<TextSelection> {
        let anchor = self.cursor_to_access_position(selection.anchor(), layout)?;
        let focus = self.cursor_to_access_position(selection.focus(), layout)?;
        Some(TextSelection { anchor, focus })
    }

    fn span_id_and_node<B: Brush>(
        &mut self,
        next_node_id: &mut impl FnMut() -> NodeId,
        span_index: usize,
        run: &Run<'_, B>,
        alignment: Option<Alignment>,
        span_path: ClusterPath,
    ) -> (NodeId, Node) {
        // Reuse the ID from the previous pass for the same span index, so that
        // merely updating the content of existing spans keeps node IDs stable.
        let id = match self.span_ids.get(span_index) {
            Some(id) => *id,
            None => {
                let id = next_node_id();
                self.span_ids.push(id);
                id
            }
        };
        self.span_paths_by_access_id.insert(id, span_path);
        let mut node = Node::new(Role::TextRun);
        node.set_text_direction(if run.is_rtl() {
            TextDirection::RightToLeft
        } else {
            TextDirection::LeftToRight
        });

        let font = run.font();
        if let Ok(font_ref) = FontRef::from_index(font.data.as_ref(), font.index)
            && let Ok(name) = font_ref.name()
        {
            for n in name.name_record().iter() {
                if n.name_id.get() == NameId::FAMILY_NAME {
                    if let Ok(string) = n.string(name.string_data()) {
                        node.set_font_family(string.to_string());
                    }
                    break;
                }
            }
        }
        node.set_font_size(run.font_size());
        let attrs = run.font_attrs();
        node.set_font_weight(attrs.weight.value());
        if matches!(attrs.style, FontStyle::Italic) {
            node.set_italic();
        }
        if let Some(align) = alignment {
            node.set_text_align(match align {
                Alignment::Start if run.is_rtl() => TextAlign::Right,
                Alignment::Start => TextAlign::Left,
                Alignment::End if run.is_rtl() => TextAlign::Left,
                Alignment::End => TextAlign::Right,
                Alignment::Left => TextAlign::Left,
                Alignment::Center => TextAlign::Center,
                Alignment::Right => TextAlign::Right,
                Alignment::Justify => TextAlign::Justify,
            });
        }

        (id, node)
    }
}

/// Exposes a [`PlainEditor`] to assistive technologies.
///
/// This wraps [`LayoutAccessibility`] with the editor-specific text selection.
#[derive(Clone, Default, Debug)]
pub struct PlainEditorAccessibility {
    layout_access: LayoutAccessibility,
}

impl PlainEditorAccessibility {
    /// Perform an accessibility update for `editor`.
    ///
    /// `node` is the node for the text input itself; the spans are pushed onto `update`
    /// as its children. See [`LayoutAccessibility::build_nodes`] for the remaining
    /// arguments.
    ///
    /// Returns `None` if the editor's layout is not up to date.
    pub fn build_nodes<B: Brush>(
        &mut self,
        editor: &PlainEditor<B>,
        update: &mut TreeUpdate,
        node: &mut Node,
        next_node_id: impl FnMut() -> NodeId,
        x_offset: f64,
        y_offset: f64,
        set_brush_properties: impl Fn(&mut Node, &Style<B>),
    ) -> Option<()> {
        let layout = editor.try_layout()?;
        self.layout_access.build_nodes(
            editor.raw_text(),
            layout,
            update,
            node,
            next_node_id,
            x_offset,
            y_offset,
            set_brush_properties,
        );
        // The IME can ask for the caret to be hidden, in which case there is no
        // selection to report.
        if editor.is_cursor_visible() {
            if let Some(selection) = self
                .layout_access
                .selection_to_access_selection(editor.raw_selection(), layout)
            {
                node.set_text_selection(selection);
            }
        } else {
            node.clear_text_selection();
        }
        node.add_action(accesskit::Action::SetTextSelection);
        Some(())
    }

    /// Set the editor's selection from a selection over the nodes built by
    /// [`Self::build_nodes`].
    pub fn select_from_access_selection<B: Brush>(
        &self,
        driver: &mut PlainEditorDriver<'_, B>,
        selection: &TextSelection,
    ) {
        driver.refresh_layout();
        let selection = driver.editor.try_layout().and_then(|layout| {
            self.layout_access
                .selection_from_access_selection(selection, layout)
        });
        if let Some(selection) = selection {
            driver.set_selection(selection);
        }
    }
}

/// Returns `true` if the layout's text ends with a hard line break, meaning the layout has
/// a trailing "phantom" line that a cursor at the end of the text belongs to.
fn ends_with_hard_line_break<B: Brush>(layout: &Layout<B>) -> bool {
    layout
        .text_len()
        .checked_sub(1)
        .and_then(|index| Cluster::from_byte_index(layout, index))
        .is_some_and(|cluster| cluster.is_hard_line_break())
}

/// The path of `run`'s first cluster, used as the path of its first accessibility span.
///
/// Empty runs have no clusters, so they get a path that no cluster can have.
fn run_start_path<B: Brush>(line_index: usize, run: &Run<'_, B>) -> ClusterPath {
    run.clusters()
        .next()
        .map(|cluster| cluster.path())
        .unwrap_or_else(|| ClusterPath::new(line_index as u32, run.index() as u32, 0))
}

fn link_spans(prev_id: NodeId, prev: &mut Node, next_id: NodeId, next: &mut Node) {
    prev.set_next_on_line(next_id);
    next.set_previous_on_line(prev_id);
}

fn add_span(update: &mut TreeUpdate, parent_node: &mut Node, id: NodeId, node: Node) {
    update.nodes.push((id, node));
    parent_node.push_child(id);
}

/// The accumulated contents of a `TextRun` node.
#[derive(Default)]
struct SpanData {
    text: String,
    advance: f32,
    character_lengths: Vec<u8>,
    character_positions: Vec<f32>,
    character_widths: Vec<f32>,
    word_starts: Vec<u8>,
}

impl SpanData {
    fn finish(self, node: &mut Node, x_offset: f64, y_offset: f64, metrics: &LineMetrics, x: f32) {
        node.set_bounds(Rect {
            x0: x_offset + x as f64,
            y0: y_offset + metrics.content_block_min_coord as f64,
            x1: x_offset + (x + self.advance) as f64,
            y1: y_offset + metrics.content_block_max_coord as f64,
        });
        node.set_value(self.text);
        node.set_character_lengths(self.character_lengths);
        node.set_character_positions(self.character_positions);
        node.set_character_widths(self.character_widths);
        node.set_word_starts(self.word_starts);
    }
}