piet-cosmic-text 0.3.4

A text layout engine for piet based on cosmic-text
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
// SPDX-License-Identifier: LGPL-3.0-or-later OR MPL-2.0
// This file is a part of `piet-cosmic-text`.
//
// `piet-cosmic-text` is free software: you can redistribute it and/or modify it under the
// terms of either:
//
// * GNU Lesser General Public License as published by the Free Software Foundation, either
//   version 3 of the License, or (at your option) any later version.
// * Mozilla Public License as published by the Mozilla Foundation, version 2.
//
// `piet-cosmic-text` is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License or the Mozilla Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License and the Mozilla
// Public License along with `piet-cosmic-text`. If not, see <https://www.gnu.org/licenses/>.

use crate::attributes::Attributes;
use crate::metadata::Metadata;
use crate::text::{FontSystemAndDefaults, Text};
use crate::text_layout::TextLayout;
use crate::{cvt_color, cvt_family, cvt_style, cvt_weight, FontError, POINTS_PER_INCH};

use cosmic_text as ct;
use ct::{Attrs, Buffer, BufferLine, Metrics};

use piet::{util, Error, TextAlignment, TextAttribute, TextStorage};

use std::cmp;
use std::fmt;
use std::mem;
use std::ops::{Range, RangeBounds};

use tinyvec::TinyVec;

/// The text layout builder used by the [`Text`].
pub struct TextLayoutBuilder {
    /// Handle to the original `Text` object.
    handle: Text,

    /// The string we're laying out.
    string: Box<dyn TextStorage>,

    /// The default text attributes.
    defaults: util::LayoutDefaults,

    /// The width constraint.
    max_width: f64,

    /// Alignment for the text.
    alignment: Option<TextAlignment>,

    /// The range attributes.
    range_attributes: Attributes,

    /// The starting point for the last range.
    ///
    /// Used for error checking.
    last_range_start_pos: usize,

    /// The last error that occurred.
    error: Option<Error>,
}

impl fmt::Debug for TextLayoutBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TextLayoutBuilder")
            .field("string", &self.string.as_str())
            .field("max_width", &self.max_width)
            .field("range_attributes", &self.range_attributes)
            .finish_non_exhaustive()
    }
}

impl TextLayoutBuilder {
    pub(crate) fn new(text: Text, string: impl TextStorage) -> Self {
        Self {
            handle: text,
            string: Box::new(string),
            defaults: util::LayoutDefaults::default(),
            max_width: f64::INFINITY,
            alignment: None,
            last_range_start_pos: 0,
            range_attributes: Attributes::default(),
            error: None,
        }
    }

    fn shaping(&self) -> ct::Shaping {
        // TODO: Use a better strategy to find this!
        ct::Shaping::Advanced
    }
}

impl piet::TextLayoutBuilder for TextLayoutBuilder {
    type Out = TextLayout;

    fn alignment(mut self, alignment: TextAlignment) -> Self {
        self.alignment = Some(alignment);
        self
    }

    fn max_width(mut self, width: f64) -> Self {
        self.max_width = width;
        self
    }

    fn default_attribute(mut self, attribute: impl Into<TextAttribute>) -> Self {
        self.defaults.set(attribute);
        self
    }

    fn range_attribute(
        mut self,
        range: impl RangeBounds<usize>,
        attribute: impl Into<TextAttribute>,
    ) -> Self {
        let range = util::resolve_range(range, self.string.len());
        let attribute = attribute.into();

        debug_assert!(
            range.start >= self.last_range_start_pos,
            "attributes must be added in non-decreasing start order"
        );
        self.last_range_start_pos = range.start;

        self.range_attributes.push(range, attribute);

        self
    }

    fn build(self) -> Result<Self::Out, Error> {
        let shaping = self.shaping();
        let Self {
            handle,
            string,
            defaults,
            max_width,
            mut range_attributes,
            error,
            ..
        } = self;

        // If an error occurred, return it.
        if let Some(error) = error {
            return Err(error);
        }

        // Get a handle to the font system.
        let mut font_system_guard = handle
            .borrow_font_system()
            .ok_or(Error::BackendError(FontError::AlreadyBorrowed.into()))?;
        let font_system = match font_system_guard.get() {
            Some(font_system) => font_system,
            None => {
                warn!("Still waiting for font system to be loaded, returning error");
                return Err(Error::BackendError(FontError::NotLoaded.into()));
            }
        };

        // Get the font size and line height.
        let font_size = defaults.font_size * handle.dpi() / POINTS_PER_INCH;

        // NOTE: Pango uses a default line height of 0, and piet-cairo doesn't appear to
        // change this.
        let metrics = Metrics::new(font_size as _, font_size as _);

        // Get the default attributes for the layout.
        let default_attrs = {
            let mut metadata = Metadata::new();

            metadata.set_underline(defaults.underline);
            metadata.set_strikethrough(defaults.strikethrough);
            metadata.set_boldness(defaults.weight);

            let mut attrs = Attrs::new()
                .family(cvt_family(&defaults.font))
                .weight(cvt_weight(defaults.weight))
                .style(cvt_style(defaults.style))
                .metadata(metadata.into_raw());

            if defaults.fg_color != util::DEFAULT_TEXT_COLOR {
                attrs = attrs.color(cvt_color(defaults.fg_color));
            }

            font_system.fix_attrs(attrs)
        };

        // Re-use memory from a previous layout.
        let mut buffer_lines = handle.take_buffer();
        let mut offset = 0;

        for line in ct::BidiParagraphs::new(&string) {
            let start = offset;
            let end = start + line.len() + 1;

            // Get the attributes for this line.
            let attrs_list = range_attributes.text_attributes(
                font_system,
                start..end,
                default_attrs.as_attrs(),
            )?;

            let mut line = BufferLine::new(line, attrs_list, shaping);
            line.set_align(self.alignment.map(|a| match a {
                TextAlignment::Start => ct::Align::Left,
                TextAlignment::Center => ct::Align::Center,
                TextAlignment::End => ct::Align::Right,
                TextAlignment::Justified => ct::Align::Justified,
            }));

            buffer_lines.push(line);

            offset = end;
        }

        let mut buffer = {
            let FontSystemAndDefaults { system, .. } = font_system;
            let mut buffer = Buffer::new(system, metrics);

            buffer.lines = buffer_lines;
            buffer.set_size(system, max_width as f32, f32::INFINITY);
            buffer.set_wrap(system, ct::Wrap::Word);

            // Shape the buffer.
            buffer.shape_until_scroll(system);

            buffer
        };

        // Fix any shaping holes.
        fix_shaping_holes(
            &mut buffer,
            &mut range_attributes,
            default_attrs.as_attrs(),
            font_system,
        )?;

        Ok(TextLayout::new(
            handle.clone(),
            buffer,
            string,
            font_size as i32,
            &mut font_system.system,
        ))
    }
}

/// Attempt to fill the holes in a buffer.
fn fix_shaping_holes(
    buffer: &mut Buffer,
    attributes: &mut Attributes,
    attrs: Attrs<'_>,
    system: &mut FontSystemAndDefaults,
) -> Result<(), Error> {
    // First, try clearing the font.
    if fill_holes(buffer, system, attrs, attributes, FillType::ClearFont)? {
        buffer.shape_until_scroll(&mut system.system);
    } else {
        return Ok(());
    }

    // Then, try clearing the style.
    if fill_holes(buffer, system, attrs, attributes, FillType::ClearStyle)? {
        buffer.shape_until_scroll(&mut system.system);
    } else {
        return Ok(());
    }

    // If we still have holes, give up.
    #[cfg(feature = "tracing")]
    {
        if !find_holes(&buffer.lines[0]).is_empty() {
            trace!("Failed to fill holes in text");
        }
    }

    Ok(())
}

#[derive(Clone, Copy)]
enum FillType {
    ClearStyle,
    ClearFont,
}

/// Fill the holes of the text.
fn fill_holes(
    buffer: &mut Buffer,
    system: &mut FontSystemAndDefaults,
    defaults: Attrs<'_>,
    attributes: &mut Attributes,
    ty: FillType,
) -> Result<bool, Error> {
    let mut found_holes = false;
    let mut offset = 0;

    for line in &mut buffer.lines {
        let holes = find_holes(line);

        if holes.is_empty() {
            continue;
        }

        found_holes = true;

        // Try to fill the holes.
        let original = line.attrs_list();
        for range in holes {
            // Figure out the replacement attribute.
            match ty {
                FillType::ClearFont => {
                    // Figure out the font type to use.
                    let family = match original.get_span(range.start).family {
                        ct::Family::Cursive => piet::FontFamily::SERIF,
                        ct::Family::Monospace => piet::FontFamily::MONOSPACE,
                        ct::Family::SansSerif => piet::FontFamily::SANS_SERIF,
                        ct::Family::Serif => piet::FontFamily::SERIF,
                        ct::Family::Fantasy => piet::FontFamily::SANS_SERIF,
                        ct::Family::Name(name) => {
                            // Figure out the best kind of font to use.
                            let mut family = piet::FontFamily::SANS_SERIF;
                            let name = name.to_ascii_lowercase();

                            if name.contains("serif") {
                                family = piet::FontFamily::SERIF;
                            } else if name.contains("mono") {
                                family = piet::FontFamily::MONOSPACE;
                            } // Sans-Serif case is implicitly handled.

                            family
                        }
                    };

                    attributes.push(range, TextAttribute::FontFamily(family));
                }

                FillType::ClearStyle => {
                    attributes.push(
                        range.clone(),
                        TextAttribute::Style(piet::FontStyle::Regular),
                    );
                    attributes.push(range, TextAttribute::Weight(piet::FontWeight::NORMAL));
                }
            };
        }

        // Set the new attributes.
        let end = offset + line.text().len() + 1;
        let attrs_list = attributes.text_attributes(system, offset..end, defaults)?;
        line.set_attrs_list(attrs_list);
        offset = end;
    }

    Ok(found_holes)
}

/// Find holes where the text is not rendered.
fn find_holes(line: &BufferLine) -> TinyVec<[Range<usize>; 1]> {
    let mut holes = TinyVec::new();

    let shape = match line.shape_opt().as_ref() {
        Some(shape) => shape,
        None => return holes,
    };

    let mut current_range = 0..0;
    let mut in_hole = false;

    for word in shape.spans.iter().flat_map(|span| &span.words) {
        // Make sure blank words fall into the same hole.
        if word.blank {
            if in_hole {
                let end = word
                    .glyphs
                    .iter()
                    .map(|glyph| glyph.end)
                    .chain(Some(current_range.end))
                    .max()
                    .unwrap();
                let start = word
                    .glyphs
                    .iter()
                    .map(|glyph| glyph.start)
                    .chain(Some(current_range.start))
                    .min()
                    .unwrap();
                current_range = start..end;
            }
            continue;
        }

        // Find holes in non-blank words.
        for glyph in &word.glyphs {
            if glyph.glyph_id == 0 {
                // Unshaped glyph, this is a hole.
                if !in_hole {
                    in_hole = true;
                    current_range = glyph.start..glyph.end;
                } else {
                    // Extend the hole.
                    current_range.start = cmp::min(current_range.start, glyph.start);
                    current_range.end = cmp::max(current_range.end, glyph.end);
                }
            } else if mem::replace(&mut in_hole, false) {
                holes.push(current_range);
                current_range = 0..0;
            }
        }
    }

    if in_hole && !current_range.is_empty() {
        holes.push(current_range);
    }

    holes
}