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
// SPDX-License-Identifier: MIT OR Apache-2.0

use unicode_script::{Script, UnicodeScript};

use super::{Font, FontShapeGlyph, FontShapeLine, FontShapeSpan, FontShapeWord};
use super::fallback::{FontFallbackIter};

/// Fonts that match a pattern
pub struct FontMatches<'a> {
    pub locale: &'a str,
    pub fonts: Vec<Font<'a>>,
}

impl<'a> FontMatches<'a> {
    fn shape_fallback(
        &self,
        font: &'a Font<'a>,
        line: &str,
        start_word: usize,
        end_word: usize,
        span_rtl: bool,
    ) -> (Vec<FontShapeGlyph>, Vec<usize>) {
        let word = &line[start_word..end_word];

        let font_scale = font.rustybuzz.units_per_em() as f32;

        let mut buffer = rustybuzz::UnicodeBuffer::new();
        buffer.set_direction(if span_rtl {
            rustybuzz::Direction::RightToLeft
        } else {
            rustybuzz::Direction::LeftToRight
        });
        buffer.push_str(word);
        buffer.guess_segment_properties();

        let rtl = match buffer.direction() {
            rustybuzz::Direction::RightToLeft => true,
            //TODO: other directions?
            _ => false,
        };
        assert_eq!(rtl, span_rtl);

        let glyph_buffer = rustybuzz::shape(&font.rustybuzz, &[], buffer);
        let glyph_infos = glyph_buffer.glyph_infos();
        let glyph_positions = glyph_buffer.glyph_positions();

        let mut missing = Vec::new();
        let mut glyphs = Vec::with_capacity(glyph_infos.len());
        for (info, pos) in glyph_infos.iter().zip(glyph_positions.iter()) {
            let x_advance = pos.x_advance as f32 / font_scale;
            let y_advance = pos.y_advance as f32 / font_scale;
            let x_offset = pos.x_offset as f32 / font_scale;
            let y_offset = pos.y_offset as f32 / font_scale;

            //println!("  {:?} {:?}", info, pos);
            if info.glyph_id == 0 {
                missing.push(start_word + info.cluster as usize);
            }

            glyphs.push(FontShapeGlyph {
                start: start_word + info.cluster as usize,
                end: end_word, // Set later
                x_advance,
                y_advance,
                x_offset,
                y_offset,
                font_id: font.info.id,
                glyph_id: info.glyph_id.try_into().unwrap(),
            });
        }

        // Adjust end of glyphs
        if rtl {
            for i in 1..glyphs.len() {
                let next_start = glyphs[i - 1].start;
                let next_end = glyphs[i - 1].end;
                let prev = &mut glyphs[i];
                if prev.start == next_start {
                    prev.end = next_end;
                } else {
                    prev.end = next_start;
                }
            }
        } else {
            for i in (1..glyphs.len()).rev() {
                let next_start = glyphs[i].start;
                let next_end = glyphs[i].end;
                let prev = &mut glyphs[i - 1];
                if prev.start == next_start {
                    prev.end = next_end;
                } else {
                    prev.end = next_start;
                }
            }
        }

        (glyphs, missing)
    }

    fn shape_word(
        &self,
        line: &str,
        start_word: usize,
        end_word: usize,
        span_rtl: bool,
        blank: bool,
    ) -> FontShapeWord {
        //TODO: use smallvec?
        let mut scripts = Vec::new();
        for c in line[start_word..end_word].chars() {
            match c.script() {
                Script::Common |
                Script::Inherited |
                Script::Latin |
                Script::Unknown => (),
                script => if ! scripts.contains(&script) {
                    scripts.push(script);
                },
            }
        }

        log::trace!(
            "    Word {:?}{}: '{}'",
            scripts,
            if blank { " BLANK" } else { "" },
            &line[start_word..end_word],
        );

        //TODO: configure default family
        let mut font_iter = FontFallbackIter::new(
            &self.fonts,
            &["Fira Sans", "Fira Mono"],
            scripts,
            self.locale
        );

        let (mut glyphs, mut missing) = self.shape_fallback(
            font_iter.next().unwrap(),
            line,
            start_word,
            end_word,
            span_rtl
        );

        //TODO: improve performance!
        while !missing.is_empty() {
            let font = match font_iter.next() {
                Some(some) => some,
                None => break,
            };

            log::trace!("Evaluating fallback with font '{}'", font.info.family);
            let (mut fb_glyphs, fb_missing) = self.shape_fallback(
                font,
                line,
                start_word,
                end_word,
                span_rtl
            );

            // Insert all matching glyphs
            let mut fb_i = 0;
            while fb_i < fb_glyphs.len() {
                let start = fb_glyphs[fb_i].start;
                let end = fb_glyphs[fb_i].end;

                // Skip clusters that are not missing, or where the fallback font is missing
                if !missing.contains(&start) || fb_missing.contains(&start) {
                    fb_i += 1;
                    continue;
                }

                let mut missing_i = 0;
                while missing_i < missing.len() {
                    if missing[missing_i] >= start && missing[missing_i] < end {
                        // println!("No longer missing {}", missing[missing_i]);
                        missing.remove(missing_i);
                    } else {
                        missing_i += 1;
                    }
                }

                // Find prior glyphs
                let mut i = 0;
                while i < glyphs.len() {
                    if glyphs[i].start >= start && glyphs[i].end <= end {
                        break;
                    } else {
                        i += 1;
                    }
                }

                // Remove prior glyphs
                while i < glyphs.len() {
                    if glyphs[i].start >= start && glyphs[i].end <= end {
                        let _glyph = glyphs.remove(i);
                        // log::trace!("Removed {},{} from {}", _glyph.start, _glyph.end, i);
                    } else {
                        break;
                    }
                }

                while fb_i < fb_glyphs.len() {
                    if fb_glyphs[fb_i].start >= start && fb_glyphs[fb_i].end <= end {
                        let fb_glyph = fb_glyphs.remove(fb_i);
                        // log::trace!("Insert {},{} from font {} at {}", fb_glyph.start, fb_glyph.end, font_i, i);
                        glyphs.insert(i, fb_glyph);
                        i += 1;
                    } else {
                        break;
                    }
                }
            }
        }

        // Debug missing font fallbacks
        font_iter.check_missing(&line[start_word..end_word]);

        /*
        for glyph in glyphs.iter() {
            log::trace!("'{}': {}, {}, {}, {}", &line[glyph.start..glyph.end], glyph.x_advance, glyph.y_advance, glyph.x_offset, glyph.y_offset);
        }
        */

        FontShapeWord { blank, glyphs }
    }

    fn shape_span(
        &self,
        line: &str,
        start_span: usize,
        end_span: usize,
        line_rtl: bool,
        span_rtl: bool,
    ) -> FontShapeSpan {
        let span = &line[start_span..end_span];

        log::trace!(
            "  Span {}: '{}'",
            if span_rtl { "RTL" } else { "LTR" },
            span
        );

        let mut words = Vec::new();

        let mut start_word = 0;
        for (end_lb, _) in unicode_linebreak::linebreaks(span) {
            let mut start_lb = end_lb;
            for (i, c) in span[start_word..end_lb].char_indices() {
                if start_word + i == end_lb {
                    break;
                } else if c.is_whitespace() {
                    start_lb = start_word + i;
                }
            }
            if start_word < start_lb {
                words.push(self.shape_word(
                    line,
                    start_span + start_word,
                    start_span + start_lb,
                    span_rtl,
                    false,
                ));
            }
            if start_lb < end_lb {
                words.push(self.shape_word(
                    line,
                    start_span + start_lb,
                    start_span + end_lb,
                    span_rtl,
                    true,
                ));
            }
            start_word = end_lb;
        }

        // Reverse glyphs in RTL lines
        if line_rtl {
            for word in words.iter_mut() {
                word.glyphs.reverse();
            }
        }

        // Reverse words in spans that do not match line direction
        if line_rtl != span_rtl {
            words.reverse();
        }

        FontShapeSpan {
            rtl: span_rtl,
            words,
        }
    }

    pub fn shape_line(&self, line: &str) -> FontShapeLine {
        let mut spans = Vec::new();

        let bidi = unicode_bidi::BidiInfo::new(line, None);
        let rtl = if bidi.paragraphs.is_empty() {
            false
        } else {
            assert_eq!(bidi.paragraphs.len(), 1);
            let para_info = &bidi.paragraphs[0];
            let line_rtl = para_info.level.is_rtl();

            log::trace!("Line {}: '{}'", if line_rtl { "RTL" } else { "LTR" }, line);

            let paragraph = unicode_bidi::Paragraph::new(&bidi, para_info);

            let mut start = 0;
            let mut span_rtl = line_rtl;
            for i in paragraph.para.range.clone() {
                let next_rtl = paragraph.info.levels[i].is_rtl();
                if span_rtl != next_rtl {
                    spans.push(self.shape_span(line, start, i, line_rtl, span_rtl));
                    span_rtl = next_rtl;
                    start = i;
                }
            }
            spans.push(self.shape_span(line, start, line.len(), line_rtl, span_rtl));

            line_rtl
        };

        FontShapeLine { rtl, spans }
    }

    pub fn get_font(&self, id: &fontdb::ID) -> Option<&Font> {
        for font in self.fonts.iter() {
            if &font.info.id == id {
                return Some(font);
            }
        }
        None
    }
}