parley 0.14.0

Parley provides an API for implementing rich text layout.
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
// Copyright 2021 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::BaselineShift;
use crate::inline_box::inline_object_box_index;
use crate::layout::data::BreakReason;
use crate::layout::data::LineData;
use crate::layout::layout::Layout;
use crate::layout::run::Run;
use crate::layout::spacing::Gaps;
use crate::layout::{SpanMetrics, Style};
use crate::style::Brush;

use core::ops::Range;
use parley_engine::Glyph;
use parley_engine::shape::ShapedCluster;

/// Line in a text layout.
#[derive(Copy, Clone)]
pub struct Line<'a, B: Brush> {
    pub(crate) layout: &'a Layout<B>,
    pub(crate) index: u32,
    pub(crate) data: &'a LineData,
}

impl<'a, B: Brush> Line<'a, B> {
    /// Returns the metrics for the line.
    pub fn metrics(&self) -> &'a LineMetrics {
        &self.data.metrics
    }

    pub fn break_reason(&self) -> BreakReason {
        self.data.break_reason
    }

    /// Returns the range of text for the line.
    pub fn text_range(&self) -> Range<usize> {
        self.data.text_range.into()
    }

    /// Returns the number of items in the line.
    pub fn len(&self) -> usize {
        self.data.item_range.end - self.data.item_range.start
    }

    /// Returns `true` if the line is empty.
    pub fn is_empty(&self) -> bool {
        self.data.item_range.is_empty()
    }

    /// Returns the run at the specified index.
    pub(crate) fn item(&self, index: usize) -> Option<Run<'a, B>> {
        let original_index = index;
        let index = self.data.item_range.start + index;
        if index >= self.data.item_range.end {
            return None;
        }
        let item = self.layout.data.line_items.get(index)?;

        Some(Run {
            layout: self.layout,
            line_index: self.index,
            index: original_index as u32,
            shaped_text_run_index: item.index as u32,
            shaped: self.layout.data.shaped_text.runs().get(item.index)?,
            data: self.layout.data.runs.get(item.index)?,
            line_data: Some(item),
        })
    }

    /// Block-axis coordinate of the baseline of the span box generated by `style_index`, taking
    /// its (and its ancestors') `vertical-align` into account.
    pub(crate) fn style_baseline(&self, style_index: u16) -> f32 {
        match self.layout.data.style_metrics.get(usize::from(style_index)) {
            Some(metrics) => self
                .data
                .style_baseline(&self.layout.data.aligned_subtree_offsets, metrics),
            None => self.data.metrics.baseline,
        }
    }

    /// Block-axis coordinate of the top edge of the layout's inline box `index`.
    pub(crate) fn inline_box_top(&self, index: usize) -> f32 {
        let layout_box = &self.layout.data.inline_boxes[index];
        let inline_box = &layout_box.inline_box;
        match inline_box.vertical_align.shift {
            BaselineShift::Top => self.data.metrics.block_min_coord,
            BaselineShift::Bottom => self.data.metrics.block_max_coord - inline_box.height,
            _ => {
                let aligned_subtree_root = self
                    .layout
                    .data
                    .style_metrics
                    .get(usize::from(layout_box.parent_style_index))
                    .map_or(0, |m| m.aligned_subtree_root);
                let ascent = inline_box.baseline.unwrap_or(inline_box.height);
                self.data.metrics.baseline
                    - self.data.aligned_subtree_offset(
                        &self.layout.data.aligned_subtree_offsets,
                        aligned_subtree_root,
                    )
                    - layout_box.baseline_offset
                    - ascent
            }
        }
    }

    /// Returns an iterator over the runs for the line.
    pub fn runs(&self) -> impl Iterator<Item = Run<'a, B>> + 'a + Clone + use<'a, B> {
        let copy = self.clone();
        let line_items = &copy.layout.data.line_items[self.data.item_range];
        line_items
            .iter()
            .enumerate()
            .map(move |(item_index, line_data)| Run {
                layout: copy.layout,
                line_index: copy.index,
                index: item_index as u32,
                shaped_text_run_index: line_data.index as u32,
                shaped: &copy.layout.data.shaped_text.runs()[line_data.index],
                data: &copy.layout.data.runs[line_data.index],
                line_data: Some(line_data),
            })
    }

    /// Returns an iterator over the glyph runs for the line.
    pub fn items(
        &self,
    ) -> impl Iterator<Item = PositionedLayoutItem<'a, B>> + 'a + Clone + use<'a, B> {
        GlyphRunIter {
            line: self.clone(),
            item_index: 0,
            clusters_consumed: 0,
            offset: 0.,
        }
    }
}

/// Metrics information for a line.
///
/// A line has an inline axis and a block axis. The inline axis runs along a line of a text; the
/// block axis is perpendicular to it. Lines are stacked along the block axis.
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct LineMetrics {
    /// The absolute line height (in layout units).
    pub line_height: f32,

    /// The block-axis coordinate of the line's baseline.
    ///
    /// For horizontal text, this is the baseline's y-coordinate within the layout.
    pub baseline: f32,

    /// Offset for alignment.
    pub offset: f32,

    /// The text indent applied to this line (see [`Layout::set_text_indent`]).
    ///
    /// This isn't part of [`Self::advance`]. After alignment, [`Self::offset`] accounts for it.
    pub indent: f32,

    /// The inline advance of the line's content.
    ///
    /// This includes [`Self::hanging_advance`], but not [`Self::indent`]. This also doesn't include
    /// additional spacing from [justified alignment](crate::Alignment::Justify).
    pub advance: f32,

    /// The portion of [`Self::advance`] that hangs past the line's end edge.
    ///
    /// For LTR paragraphs, this is the right edge, and in RTL paragraphs, it's the left edge.
    //
    // Note: only whitespace hangs at the moment. With something like hanging punctuation, inked
    // content could hang past both the start and end edge. That would likely require splitting this
    // into two fields.
    pub hanging_advance: f32,

    /// The minimum inline-axis coordinate of the line.
    ///
    /// For horizontal text, this would be the left of the line.
    pub inline_min_coord: f32,

    /// The maximum inline-axis coordinate of the line.
    ///
    /// For horizontal text, this would be the right of the line.
    pub inline_max_coord: f32,

    /// The minimum block-axis coordinate of the line box.
    ///
    /// "Line box" is used as in
    /// [CSS 2.2 § 9.4.2](https://www.w3.org/TR/CSS22/visuren.html#line-box). The line box is tall
    /// enough to cover all its inline boxes, but note glyphs may overflow their box, especially
    /// when the typographic line height is small. See also [`Self::content_block_min_coord`].
    ///
    /// For horizontal text, this is the top of the line box.
    pub block_min_coord: f32,

    /// The maximum block-axis coordinate of the line box.
    ///
    /// For horizontal text, this is the bottom of the line box.
    pub block_max_coord: f32,

    /// The minimum block-axis coordinate of the line's content.
    ///
    /// This covers, roughly, the line's glyphs and inline boxes. This does not take into account
    /// typographic leading, but only the typographic ascent and descent. In case of negative
    /// leading, the content block can be larger than the line's block. Glyphs can overflow the
    /// content area: tall diacritics can exceed the area, stacking combining marks can easily
    /// exceed the area, and some fonts will simply have glyphs that exceed this.
    ///
    /// For horizontal text, this would be the top of the line's content.
    pub content_block_min_coord: f32,

    /// The maximum block-axis coordinate of the line's content.
    ///
    /// See [`LineMetrics::content_block_min_coord`] for a caveat about glyphs overflowing the
    /// content area.
    ///
    /// For horizontal text, this would be the bottom of the line's content.
    pub content_block_max_coord: f32,
}

impl LineMetrics {
    /// Returns the size of the line
    pub fn size(&self) -> f32 {
        self.line_height
    }
}

/// The computed result of an item (glyph run or inline box) within a layout
#[derive(Clone)]
pub enum PositionedLayoutItem<'a, B: Brush> {
    GlyphRun(GlyphRun<'a, B>),
    InlineBox(PositionedInlineBox),
}

/// The computed position of an inline box within a layout
#[derive(Debug, Clone)]
pub struct PositionedInlineBox {
    /// The [`InlineBox::id`](crate::InlineBox::id) of this box.
    pub id: u64,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    /// The baseline of the box in pixels, relative to the top of the box (i.e. to [`Self::y`]).
    ///
    /// This is `None` if the box does not specify a baseline, in which case its bottom edge is
    /// aligned with the text baseline.
    pub baseline: Option<f32>,
}

/// Sequence of fully positioned glyphs with the same style.
#[derive(Clone)]
pub struct GlyphRun<'a, B: Brush> {
    run: Run<'a, B>,
    style_index: u16,
    /// The glyph run's shaped clusters, indexing into the shaped run this [`Run`] belongs to.
    ///
    /// Both bounds are atom boundaries, as glyph runs must span full ligatures.
    shaped_clusters: core::range::Range<u32>,
    offset: f32,
    baseline: f32,
    advance: f32,
}

impl<'a, B: Brush> GlyphRun<'a, B> {
    /// Returns the underlying run.
    pub fn run(&self) -> &Run<'a, B> {
        &self.run
    }

    /// Returns the associated style.
    ///
    /// See also [`Self::style_index`].
    pub fn style(&self) -> &'a Style<B> {
        &self.run.layout.styles()[usize::from(self.style_index)]
    }

    /// Returns the associated style index.
    ///
    /// See also [`Self::style`].
    pub fn style_index(&self) -> u16 {
        self.style_index
    }

    /// Returns the inherent metrics of the run's associated span, from the first available font.
    ///
    /// The metrics are identical for every run in the span, including runs that fell back to
    /// another font. Blink positions `line-through` from this ascent (i.e. the primary font's),
    /// for example, so a strikethrough placed from it keeps one height across fallback fonts.
    pub fn span_metrics(&self) -> SpanMetrics {
        self.run.layout.data.style_metrics[usize::from(self.style_index)].span_metrics()
    }

    /// Returns the offset to the baseline.
    pub fn baseline(&self) -> f32 {
        self.baseline
    }

    /// Returns the offset to the first glyph along the baseline.
    pub fn offset(&self) -> f32 {
        self.offset
    }

    /// Returns the total advance of the run.
    pub fn advance(&self) -> f32 {
        self.advance
    }

    /// Returns an iterator over the glyphs in the run.
    pub fn glyphs(&self) -> impl Iterator<Item = Glyph> + 'a + Clone + use<'a, B> {
        self.run.glyphs_in(self.shaped_clusters.into())
    }

    /// Returns an iterator over the fully positioned glyphs in the run.
    pub fn positioned_glyphs(&self) -> impl Iterator<Item = Glyph> + 'a + Clone + use<'a, B> {
        let mut offset = self.offset;
        let baseline = self.baseline;
        self.glyphs().map(move |mut g| {
            g.x += offset;
            g.y += baseline;
            offset += g.advance;
            g
        })
    }
}

/// Iterator over the items in a line
// TODO: Rename?
#[derive(Clone)]
struct GlyphRunIter<'a, B: Brush> {
    line: Line<'a, B>,
    item_index: usize,
    /// Cursor within the shaped clusters of the run at `item_index` in visual order.
    clusters_consumed: u32,
    offset: f32,
}

impl<'a, B: Brush> GlyphRunIter<'a, B> {
    /// The layout's inline box `box_index`, positioned at `offset` along the line.
    fn positioned_inline_box(&self, box_index: usize, offset: f32) -> PositionedLayoutItem<'a, B> {
        let inline_box = &self.line.layout.data.inline_boxes[box_index].inline_box;
        PositionedLayoutItem::InlineBox(PositionedInlineBox {
            x: offset + self.line.data.metrics.inline_min_coord + self.line.data.metrics.offset,
            y: self.line.inline_box_top(box_index),
            width: inline_box.width,
            height: inline_box.height,
            baseline: inline_box.baseline,
            id: inline_box.id,
        })
    }
}

impl<'a, B: Brush> Iterator for GlyphRunIter<'a, B> {
    type Item = PositionedLayoutItem<'a, B>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let run = self.line.item(self.item_index)?;

            let line_slice = run.line_slice();
            let run_cluster_range = line_slice.shaped_clusters_range();
            let shaped_clusters = line_slice.shaped_clusters();
            let cluster_gaps = run
                .gaps()
                .map(|run_gaps| run_gaps.for_cluster_range(run_cluster_range.clone()));
            let is_rtl = run.is_rtl();
            let cluster_count = shaped_clusters.len() as u32;
            let start = self.clusters_consumed;

            // Scan the shaped clusters of one glyph run, stopping before a style change or an
            // inline object.

            let mut advance = 0.0;
            let mut glyph_count = 0;
            let mut glyph_run_style_index: Option<u16> = None;
            let mut position = start;
            while position < cluster_count {
                let cluster_idx = if is_rtl {
                    cluster_count - 1 - position
                } else {
                    position
                };
                let cluster = &shaped_clusters[cluster_idx as usize];
                let cluster_glyph_count = usize::from(cluster.glyph_len());
                if cluster_glyph_count == 0 {
                    if cluster.is_inline_object() {
                        // If there is an in-progress glyph run, we yield that first; the object
                        // is then found again by the next call.
                        if glyph_run_style_index.is_some() {
                            break;
                        }
                        self.clusters_consumed = position + 1;
                        let gaps = cluster_gaps.map_or(Gaps::ZERO, |cluster_gaps| {
                            cluster_gaps[cluster_idx as usize]
                        });
                        let replacement_char_index = self.line.layout.data.shaped_text.characters()
                            [cluster.chars_range().start as usize]
                            .text_byte_start;
                        let box_index = inline_object_box_index(
                            &self.line.layout.data.inline_boxes,
                            replacement_char_index as usize,
                        );
                        let x = self.offset + gaps.before;
                        self.offset += cluster.advance + gaps.total();
                        return Some(self.positioned_inline_box(box_index, x));
                    }
                    // The invariant of glyph runs is that each glyph has the same style index.
                    // As such, a glyphless cluster doesn't need to break the run, even if it
                    // is a style change. TODO: Is that reasonable/correct?
                    position += 1;
                } else {
                    let atom_style_index = if cluster.is_grapheme_start() {
                        cluster.style_index
                    } else {
                        // TODO: We use a single style index across the whole atom to match `Cluster::style_index`.
                        // We should consider using `cluster.style_index` directly.
                        find_atom_style(shaped_clusters, cluster_idx)
                    };
                    match glyph_run_style_index {
                        Some(style_index) if style_index != atom_style_index => break,
                        _ => glyph_run_style_index = Some(atom_style_index),
                    }
                    glyph_count += cluster_glyph_count;
                    advance += cluster.advance
                        + cluster_gaps.map_or(0., |cluster_gaps| {
                            cluster_gaps[cluster_idx as usize].total()
                        });
                    position += 1;
                }
            }

            // Everything before `position` is consumed by whatever we yield below.
            self.clusters_consumed = position;

            if let Some(style_index) = glyph_run_style_index {
                let consumed = if is_rtl {
                    cluster_count - position..cluster_count - start
                } else {
                    start..position
                };
                let glyph_run_clusters = consumed.start + run_cluster_range.start
                    ..consumed.end + run_cluster_range.start;
                debug_assert_eq!(
                    run.glyphs_in(glyph_run_clusters.clone()).count(),
                    glyph_count,
                    "The calculated shaped cluster range of this glyph run must hold exactly the number of glyphs we counted above."
                );
                let offset = self.offset;
                self.offset += advance;
                return Some(PositionedLayoutItem::GlyphRun(GlyphRun {
                    run,
                    style_index,
                    shaped_clusters: glyph_run_clusters.into(),
                    offset: offset
                        + self.line.data.metrics.inline_min_coord
                        + self.line.data.metrics.offset,
                    baseline: self.line.style_baseline(style_index),
                    advance,
                }));
            }

            // Nothing left to yield in this run.
            self.item_index += 1;
            self.clusters_consumed = 0;
        }
    }
}

#[cold]
#[inline(never)]
fn find_atom_style(shaped_clusters: &[ShapedCluster], mut idx: u32) -> u16 {
    while idx > 0 && !shaped_clusters[idx as usize].is_grapheme_start() {
        idx -= 1;
    }
    shaped_clusters[idx as usize].style_index
}