textflow-rs 0.2.3

Bounded no_std Unicode text layout, bidi shaping, line breaking, and caret positioning for Rust
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
use super::{
    validate_request, FontAccessError, FontId, FontMetrics, GlyphId, GlyphSource, ShapeError,
    ShapeRequest, ShapedGlyph, SimpleTypeface, Typeface,
};
use crate::bidi::Direction;
use crate::unicode::Script;
use core::ops::Range;

const OUTPUT_FLAGS: u16 = ShapedGlyph::UNSAFE_TO_BREAK;
const MASK_SHIFT: u32 = 1;

#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct GlyphMask(u16);

impl GlyphMask {
    pub const NONE: Self = Self(0);
    pub const ALL: Self = Self(u16::MAX >> MASK_SHIFT);

    pub const fn new(bits: u16) -> Self {
        Self(bits & Self::ALL.0)
    }

    pub const fn bits(self) -> u16 {
        self.0
    }

    pub const fn intersects(self, other: Self) -> bool {
        self.0 & other.0 != 0
    }
}

pub struct GlyphBuffer<'a> {
    storage: &'a mut [ShapedGlyph],
    len: usize,
}

impl<'a> GlyphBuffer<'a> {
    pub fn new(storage: &'a mut [ShapedGlyph], len: usize) -> Result<Self, ShapeError> {
        if len > storage.len() {
            return Err(ShapeError::InsufficientCapacity { required: len });
        }
        Ok(Self { storage, len })
    }

    pub const fn len(&self) -> usize {
        self.len
    }

    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub const fn capacity(&self) -> usize {
        self.storage.len()
    }

    pub fn get(&self, index: usize) -> Option<&ShapedGlyph> {
        self.storage.get(index).filter(|_| index < self.len)
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&mut ShapedGlyph> {
        if index >= self.len {
            return None;
        }
        self.storage.get_mut(index)
    }

    pub fn glyphs(&self) -> &[ShapedGlyph] {
        &self.storage[..self.len]
    }

    pub fn glyphs_mut(&mut self) -> &mut [ShapedGlyph] {
        &mut self.storage[..self.len]
    }

    pub fn mask(&self, index: usize) -> Option<GlyphMask> {
        self.get(index)
            .map(|glyph| GlyphMask::new(glyph.flags >> MASK_SHIFT))
    }

    pub fn set_mask(&mut self, index: usize, mask: GlyphMask) -> Result<(), ShapeError> {
        let glyph = self.get_mut(index).ok_or(ShapeError::InvalidGlyphRange)?;
        glyph.flags = (glyph.flags & OUTPUT_FLAGS) | (mask.bits() << MASK_SHIFT);
        Ok(())
    }

    pub fn set_glyph(&mut self, index: usize, glyph_id: GlyphId) -> Result<(), ShapeError> {
        let glyph = self.get_mut(index).ok_or(ShapeError::InvalidGlyphRange)?;
        glyph.glyph_id = glyph_id;
        Ok(())
    }

    pub fn replace(
        &mut self,
        range: Range<usize>,
        replacements: &[ShapedGlyph],
    ) -> Result<(), ShapeError> {
        self.replace_with(range, replacements.len(), |index| replacements[index])
    }

    pub fn replace_with(
        &mut self,
        range: Range<usize>,
        replacement_len: usize,
        mut replacement: impl FnMut(usize) -> ShapedGlyph,
    ) -> Result<(), ShapeError> {
        if range.start > range.end || range.end > self.len {
            return Err(ShapeError::InvalidGlyphRange);
        }
        let removed = range.end - range.start;
        let required = self
            .len
            .checked_sub(removed)
            .and_then(|len| len.checked_add(replacement_len))
            .ok_or(ShapeError::InsufficientCapacity {
                required: usize::MAX,
            })?;
        if required > self.storage.len() {
            return Err(ShapeError::InsufficientCapacity { required });
        }
        if replacement_len != removed {
            self.storage
                .copy_within(range.end..self.len, range.start + replacement_len);
        }
        for index in 0..replacement_len {
            self.storage[range.start + index] = replacement(index);
        }
        self.len = required;
        Ok(())
    }

    fn finish(&mut self) -> usize {
        for glyph in &mut self.storage[..self.len] {
            glyph.flags &= OUTPUT_FLAGS;
        }
        self.len
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LookupStatus {
    NotFound,
    Applied,
}

#[derive(Clone, Copy)]
pub struct LookupRequest<'a, 'text> {
    shape: &'a ShapeRequest<'text>,
    feature: [u8; 4],
    mask: GlyphMask,
}

impl<'a, 'text> LookupRequest<'a, 'text> {
    pub const fn new(shape: &'a ShapeRequest<'text>, feature: [u8; 4], mask: GlyphMask) -> Self {
        Self {
            shape,
            feature,
            mask,
        }
    }

    pub const fn shape(self) -> &'a ShapeRequest<'text> {
        self.shape
    }

    pub const fn feature(self) -> [u8; 4] {
        self.feature
    }

    pub const fn mask(self) -> GlyphMask {
        self.mask
    }

    pub const fn selects(self, glyph_mask: GlyphMask) -> bool {
        self.mask.bits() == GlyphMask::ALL.bits() || self.mask.intersects(glyph_mask)
    }
}

pub trait ShapingData {
    fn substitute(
        &self,
        request: LookupRequest<'_, '_>,
        glyphs: &mut GlyphBuffer<'_>,
    ) -> Result<LookupStatus, ShapeError>;

    fn position(
        &self,
        request: LookupRequest<'_, '_>,
        glyphs: &mut GlyphBuffer<'_>,
    ) -> Result<LookupStatus, ShapeError>;
}

pub trait ScriptProvider {
    fn script(&self) -> Script;

    fn substitute(
        &self,
        request: &ShapeRequest<'_>,
        font: &dyn ShapingData,
        glyphs: &mut GlyphBuffer<'_>,
    ) -> Result<(), ShapeError>;

    fn position(
        &self,
        request: &ShapeRequest<'_>,
        font: &dyn ShapingData,
        glyphs: &mut GlyphBuffer<'_>,
    ) -> Result<(), ShapeError>;
}

pub struct ScriptTypeface<'a> {
    glyphs: &'a dyn GlyphSource,
    shaping: &'a dyn ShapingData,
    scripts: &'a [&'a dyn ScriptProvider],
}

impl<'a> ScriptTypeface<'a> {
    pub const fn new(glyphs: &'a dyn GlyphSource, shaping: &'a dyn ShapingData) -> Self {
        Self {
            glyphs,
            shaping,
            scripts: &[],
        }
    }

    pub const fn with_scripts(mut self, scripts: &'a [&'a dyn ScriptProvider]) -> Self {
        self.scripts = scripts;
        self
    }

    fn provider(&self, script: Script) -> Option<&dyn ScriptProvider> {
        self.scripts
            .iter()
            .copied()
            .find(|provider| provider.script() == script)
    }

    fn map_into(
        &self,
        request: &ShapeRequest<'_>,
        output: &mut [ShapedGlyph],
    ) -> Result<usize, ShapeError> {
        let text = &request.text[request.range.clone()];
        let required = text.chars().count();
        if output.len() < required {
            return Err(ShapeError::InsufficientCapacity { required });
        }
        for (slot, (offset, character)) in output.iter_mut().zip(text.char_indices()) {
            let start = request.range.start + offset;
            let end = start + character.len_utf8();
            let glyph = self
                .glyphs
                .glyph_for(character)?
                .or(self.glyphs.notdef_glyph()?)
                .ok_or(ShapeError::MissingGlyph { offset: start })?;
            *slot = ShapedGlyph::new(glyph, super::TextRange::new(start as u32, end as u32));
        }
        Ok(required)
    }
}

impl Typeface for ScriptTypeface<'_> {
    fn id(&self) -> FontId {
        self.glyphs.id()
    }

    fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
        self.glyphs.metrics()
    }

    fn covers(&self, grapheme: &str) -> Result<bool, FontAccessError> {
        for character in grapheme.chars() {
            if self.glyphs.glyph_for(character)?.is_none() {
                return Ok(false);
            }
        }
        Ok(!grapheme.is_empty())
    }

    fn supports_complex_shaping(&self) -> bool {
        true
    }

    fn shape_into(
        &self,
        request: &ShapeRequest<'_>,
        output: &mut [ShapedGlyph],
    ) -> Result<usize, ShapeError> {
        validate_request(request)?;
        let Some(provider) = self.provider(request.script) else {
            return SimpleTypeface::new(self.glyphs).shape_into(request, output);
        };
        let count = self.map_into(request, output)?;
        let mut glyphs = GlyphBuffer::new(output, count)?;
        provider.substitute(request, self.shaping, &mut glyphs)?;
        for glyph in glyphs.glyphs_mut() {
            glyph.advance = self.glyphs.glyph_advance(glyph.glyph_id())?;
        }
        provider.position(request, self.shaping, &mut glyphs)?;
        let count = glyphs.finish();
        if request.direction == Direction::RightToLeft {
            output[..count].reverse();
        }
        Ok(count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shaping::{FlowPoint, TextRange};

    struct Font;

    impl GlyphSource for Font {
        fn id(&self) -> FontId {
            FontId::new(7)
        }

        fn metrics(&self) -> Result<FontMetrics, FontAccessError> {
            Ok(FontMetrics::default())
        }

        fn glyph_for(&self, character: char) -> Result<Option<GlyphId>, FontAccessError> {
            Ok(Some(GlyphId::new(character as u16)))
        }

        fn glyph_advance(&self, glyph: GlyphId) -> Result<FlowPoint, FontAccessError> {
            Ok(FlowPoint {
                x: i32::from(glyph.value()),
                y: 0,
            })
        }
    }

    struct Data;

    impl ShapingData for Data {
        fn substitute(
            &self,
            request: LookupRequest<'_, '_>,
            glyphs: &mut GlyphBuffer<'_>,
        ) -> Result<LookupStatus, ShapeError> {
            if request.feature() != *b"test" {
                return Ok(LookupStatus::NotFound);
            }
            glyphs.set_glyph(0, GlyphId::new(9)).unwrap();
            Ok(LookupStatus::Applied)
        }

        fn position(
            &self,
            request: LookupRequest<'_, '_>,
            glyphs: &mut GlyphBuffer<'_>,
        ) -> Result<LookupStatus, ShapeError> {
            if request.feature() != *b"mark" {
                return Ok(LookupStatus::NotFound);
            }
            glyphs.get_mut(1).unwrap().offset.y = -3;
            Ok(LookupStatus::Applied)
        }
    }

    struct Provider;

    impl ScriptProvider for Provider {
        fn script(&self) -> Script {
            Script::Thai
        }

        fn substitute(
            &self,
            request: &ShapeRequest<'_>,
            font: &dyn ShapingData,
            glyphs: &mut GlyphBuffer<'_>,
        ) -> Result<(), ShapeError> {
            glyphs.set_mask(0, GlyphMask::new(1))?;
            font.substitute(
                LookupRequest::new(request, *b"test", GlyphMask::new(1)),
                glyphs,
            )?;
            Ok(())
        }

        fn position(
            &self,
            request: &ShapeRequest<'_>,
            font: &dyn ShapingData,
            glyphs: &mut GlyphBuffer<'_>,
        ) -> Result<(), ShapeError> {
            font.position(
                LookupRequest::new(request, *b"mark", GlyphMask::ALL),
                glyphs,
            )?;
            Ok(())
        }
    }

    #[test]
    fn glyph_buffer_replaces_in_place_and_reports_capacity() {
        let mut storage = [ShapedGlyph::default(); 3];
        storage[0] = ShapedGlyph::new(GlyphId::new(1), TextRange::new(0, 1));
        storage[1] = ShapedGlyph::new(GlyphId::new(2), TextRange::new(1, 2));
        let mut buffer = GlyphBuffer::new(&mut storage, 2).unwrap();
        let replacements = [
            ShapedGlyph::new(GlyphId::new(3), TextRange::new(0, 1)),
            ShapedGlyph::new(GlyphId::new(4), TextRange::new(0, 1)),
        ];
        buffer.replace(0..1, &replacements).unwrap();
        assert_eq!(buffer.len(), 3);
        assert_eq!(buffer.get(2).unwrap().glyph_id(), GlyphId::new(2));

        let before = *buffer.get(0).unwrap();
        assert_eq!(
            buffer.replace(0..1, &replacements),
            Err(ShapeError::InsufficientCapacity { required: 4 })
        );
        assert_eq!(*buffer.get(0).unwrap(), before);
    }

    #[test]
    fn glyph_buffer_builds_variable_replacements_in_caller_storage() {
        let mut storage = [ShapedGlyph::default(); 4];
        storage[0] = ShapedGlyph::new(GlyphId::new(1), TextRange::new(0, 1));
        storage[1] = ShapedGlyph::new(GlyphId::new(2), TextRange::new(1, 2));
        let mut buffer = GlyphBuffer::new(&mut storage, 2).unwrap();

        buffer
            .replace_with(0..1, 3, |index| {
                ShapedGlyph::new(GlyphId::new(10 + index as u16), TextRange::new(0, 1))
            })
            .unwrap();

        assert_eq!(buffer.len(), 4);
        assert_eq!(buffer.get(0).unwrap().glyph_id(), GlyphId::new(10));
        assert_eq!(buffer.get(1).unwrap().glyph_id(), GlyphId::new(11));
        assert_eq!(buffer.get(2).unwrap().glyph_id(), GlyphId::new(12));
        assert_eq!(buffer.get(3).unwrap().glyph_id(), GlyphId::new(2));
    }

    #[test]
    fn all_lookup_mask_selects_unmarked_glyphs() {
        let shape = ShapeRequest::new("a", 0..1, Direction::LeftToRight, Script::Latin);
        let all = LookupRequest::new(&shape, *b"ccmp", GlyphMask::ALL);
        let form = LookupRequest::new(&shape, *b"init", GlyphMask::new(1));

        assert!(all.selects(GlyphMask::NONE));
        assert!(form.selects(GlyphMask::new(1)));
        assert!(!form.selects(GlyphMask::NONE));
    }

    #[test]
    fn script_typeface_runs_provider_without_heap_storage() {
        let providers: [&dyn ScriptProvider; 1] = [&Provider];
        let face = ScriptTypeface::new(&Font, &Data).with_scripts(&providers);
        let text = "กิ";
        let request = ShapeRequest::new(text, 0..text.len(), Direction::LeftToRight, Script::Thai);
        let mut output = [ShapedGlyph::default(); 2];

        let count = face.shape_into(&request, &mut output).unwrap();

        assert_eq!(count, 2);
        assert_eq!(output[0].glyph_id(), GlyphId::new(9));
        assert_eq!(output[0].advance.x, 9);
        assert_eq!(output[1].offset.y, -3);
    }
}