fframes 1.0.0

A video generation framework
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
use crate::{
    FFramesContext, FontFace, FontSource, FontStretch, FontStyle, FontVariant, Svgr, svgr,
};
use lru::LruCache;
use std::{
    borrow::Cow,
    cell::RefCell,
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
    num::NonZeroUsize,
    rc::Rc,
};
use usvgr::svgtree::SvgAttributeValue;

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum TextAlign {
    Left,
    Center,
    Right,
}

#[derive(Debug, Clone, Copy, Hash)]
/// Define a query that will resolve a font from the provided media source.
/// The values from the struct could be passed directly to the <text> element attributes like:
///
/// ```no_run
/// let font = FontQuery {
///    family: "Arial",
///    size: 16,
///    weight: 700,
///    ..Default::default()
/// }
///
/// svgr!(
///  <text
///     font-size={font.size}
///     height={font.height}
///     font-family={font.family}
///     font-style={font.style}
///     font-weight={font.weight}
///     font-stretch={font.stretch}
///  > "Hello world!" </text>
/// )
/// ```
pub struct FontQuery<'a> {
    pub family: &'a str,
    pub size: usize,
    pub weight: u16,
    /// Font style (normal, italic, oblique) to use. If the font is variadic, applies the style.
    /// If not - uses the will try to resolve font from the family that satisfies this style.
    pub style: FontStyle,
    pub stretch: FontStretch,
}

impl Default for FontQuery<'_> {
    fn default() -> Self {
        Self {
            weight: 500,
            size: 16,
            family: "Arial",
            style: Default::default(),
            stretch: Default::default(),
        }
    }
}

impl FontQuery<'_> {
    pub fn hash_with_value(&self, value: &str) -> u64 {
        let mut s = DefaultHasher::new();
        value.hash(&mut s);
        self.hash(&mut s);

        s.finish()
    }
}

#[derive(Debug, Clone, Copy)]
pub struct BreakLinesOpts<
    'a,
    TX: Into<SvgAttributeValue<'a>> + Hash + Default,
    TY: Into<SvgAttributeValue<'a>> + Hash + Default,
> {
    /// The font family to use for the text.
    pub font: FontQuery<'a>,
    pub width: usize,
    /// Similar to css `line-height` property where 1.0 is the line height of the selected font
    /// size. 1.1 is 10% larger than the font size which adds 10% of the font size as a margin
    /// between lines after the break.
    ///
    /// @default 1.1
    pub line_height: f32,
    /// The font family to use for the text.
    /// **Pro tip**: check resolved font family name in the editor media panel.
    pub x: TX,
    pub y: TY,
    pub align: TextAlign,
    pub fill: &'a str,
    /// The [dominant-baseline](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dominant-baseline) svg attribute specifies the dominant baseline,
    /// which is the baseline used to align the box's text and inline-level contents.
    /// It also indicates the default alignment baseline of any boxes
    /// participating in baseline alignment in the box's alignment context.
    ///
    /// **In short: defines how to align text vertically based to the `y` position.**
    pub dominant_baseline: &'a str,
    /// The [text-anchor](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor) attribute is used to align (start-, middle- or end-alignment) a string of pre-formatted text or auto-wrapped text where the wrapping area is determined from the inline-size property relative to a given point.
    /// **In short: defines how to align text horizontally based to the `x` position.**
    pub text_anchor: &'a str,
    pub opacity: f32,
}

impl<'a, TX, TY> BreakLinesOpts<'a, TX, TY>
where
    TX: Into<SvgAttributeValue<'a>> + Hash + Default,
    TY: Into<SvgAttributeValue<'a>> + Hash + Default,
{
    pub(crate) fn hash_with_value(&self, value: &str) -> u64 {
        let mut s = DefaultHasher::new();
        value.hash(&mut s);
        self.hash(&mut s);

        s.finish()
    }

    /// When using `frame.text_break_lines_structure` which returns the text structure
    /// use this function to create top level `text` element that applies all the options from the
    /// `BreakLinesOpts` to the `text` svg element.
    pub fn create_text_svgr(self, children: Svgr<'a>) -> Svgr<'a> {
        let BreakLinesOpts::<'a, TX, TY> {
            font,
            x,
            y,
            fill,
            dominant_baseline,
            text_anchor,
            ..
        } = self;

        svgr!(
          <text
            x={x.into()}
            y={y.into()}
            fill={fill}
            font-size={font.size}
            font-family={font.family}
            font-weight={font.weight}
            font-stretch={font.stretch.to_string()}
            dominant-baseline={dominant_baseline}
            text-anchor={text_anchor}
          >
             {children}
          </text>
        )
    }
}

impl<'a, TX, TY> std::hash::Hash for BreakLinesOpts<'a, TX, TY>
where
    TX: Into<SvgAttributeValue<'a>> + Hash + Default,
    TY: Into<SvgAttributeValue<'a>> + Hash + Default,
{
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.width.hash(state);
        self.line_height.to_bits().hash(state);
        self.x.hash(state);
        self.y.hash(state);
        self.align.hash(state);
        self.fill.hash(state);
        self.dominant_baseline.hash(state);
        self.text_anchor.hash(state);
        self.font.hash(state);
    }
}

impl<'a, TX, TY> Default for BreakLinesOpts<'a, TX, TY>
where
    TX: Into<SvgAttributeValue<'a>> + Hash + Default,
    TY: Into<SvgAttributeValue<'a>> + Hash + Default,
{
    fn default() -> Self {
        Self {
            width: 0,
            line_height: 1.1,
            x: TX::default(),
            y: TY::default(),
            fill: "",
            font: FontQuery::default(),
            align: TextAlign::Left,
            dominant_baseline: "auto",
            text_anchor: "start",
            opacity: 1.0,
        }
    }
}

/// This is only fframes cache for text related processing, the actual
/// glyph aliasing, text outlining happens in the rendering backend and
/// can have its own cache
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct TextCache {
    pub(crate) breaks_cache: Rc<RefCell<LruCache<u64, WrappedTextStructure>>>,
    pub(crate) text_width_cache: Rc<RefCell<LruCache<u64, usize>>>,
}

impl TextCache {
    pub fn new(size: usize) -> Option<Self> {
        if size > 0 {
            Some(Self {
                breaks_cache: Rc::new(RefCell::new(LruCache::new(
                    NonZeroUsize::new(size).unwrap(),
                ))),
                text_width_cache: Rc::new(RefCell::new(LruCache::new(
                    NonZeroUsize::new(size).unwrap(),
                ))),
            })
        } else {
            None
        }
    }
}

#[derive(Debug, Clone)]
pub struct WrappedTextLine {
    pub words: Vec<String>,
    pub width: usize,
    pub dx: usize,
    pub dy: usize,
}

#[derive(Debug, Clone, Default)]
pub struct WrappedTextStructure {
    pub lines: Vec<WrappedTextLine>,
    pub(crate) line_height_in_px: f32,
    pub(crate) hash: u64,
}

impl WrappedTextStructure {
    pub fn as_svgr<
        'a,
        X: Into<SvgAttributeValue<'a>> + Hash + Default,
        Y: Into<SvgAttributeValue<'a>> + Hash + Default,
    >(
        &self,
        BreakLinesOpts {
            x,
            y,
            fill,
            font,
            dominant_baseline,
            text_anchor,
            opacity,
            ..
        }: BreakLinesOpts<'a, X, Y>,
    ) -> Svgr<'a> {
        let svgr_x: SvgAttributeValue = x.into();
        let svgr_y: SvgAttributeValue = y.into();

        svgr_macro::svgr! {
            <text
                id={self.hash}
                x={svgr_x.clone()}
                y={svgr_y.clone()}
                fill={fill}
                font-size={font.size}
                font-family={font.family}
                font-weight={font.weight}
                dominant-baseline={dominant_baseline}
                text-anchor={text_anchor}
                opacity={opacity}
            >
                {Svgr::from_iter(
                    self.lines.iter().map(|line| {
                        svgr_macro::svgr! {
                            <tspan
                                x={svgr_x.clone()}
                                y={svgr_y.clone()}
                                dx={line.dx}
                                dy={line.dy}
                            >
                                {line.words.join(" ")}
                            </tspan>
                        }
                    })
                )}
            </text>
        }
    }

    pub fn occupied_height(&self) -> usize {
        let lines = &self.lines;

        if lines.is_empty() {
            return 0;
        }

        if lines.len() == 1 {
            return self.line_height_in_px as usize;
        }

        lines.len() * self.line_height_in_px as usize
    }
}

/// How text that does not fit into its box is shortened, the equivalent of
/// the css `text-overflow` property.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum TextOverflow<'a> {
    /// Drop the characters that do not fit; the result borrows from the input.
    Clip,
    /// Drop the characters that do not fit and end the text with `…`.
    #[default]
    Ellipsis,
    /// Like [`TextOverflow::Ellipsis`] with a custom marker, e.g. `"..."`.
    Marker(&'a str),
}

impl TextOverflow<'_> {
    fn marker(&self) -> &str {
        match self {
            TextOverflow::Clip => "",
            TextOverflow::Ellipsis => "…",
            TextOverflow::Marker(marker) => marker,
        }
    }
}

fn char_width(
    char: char,
    font_face: &dyn FontFace,
    font_size: usize,
    font_variant: FontVariant,
) -> usize {
    match font_variant {
        FontVariant::Monospaced(mono_width) => mono_width,
        FontVariant::Variable => font_face
            .resolve_char_width(font_size, char)
            .unwrap_or_default(),
    }
}

fn calc_text_width(
    text: &str,
    font_face: &dyn FontFace,
    font_size: usize,
    font_variant: FontVariant,
) -> usize {
    match text {
        "" => 0,
        "\n" | "\r" => 0,
        text => text
            .chars()
            .map(|char| char_width(char, font_face, font_size, font_variant))
            .sum(),
    }
}

/// Shortens `value` so it fits into `max_width` pixels, see
/// [`crate::Frame::text_fit`].
pub(crate) fn text_fit_impl<'a>(
    font_query: FontQuery,
    value: &'a str,
    max_width: usize,
    overflow: TextOverflow,
    font_source: &'a (dyn FontSource<'a> + 'a),
) -> Option<Cow<'a, str>> {
    let font_face = font_source.resolve_font(
        font_query.family,
        font_query.weight,
        font_query.style,
        font_query.stretch,
    )?;
    let font_variant = font_face.font_variant(font_query.size)?;
    let width =
        |text: &str| calc_text_width(text, font_face.as_ref(), font_query.size, font_variant);

    let marker = overflow.marker();
    let marker_width = width(marker);

    // One pass over the glyphs: `fits_end` is the end of the longest prefix
    // that leaves room for the marker, `total` the width of the whole text.
    let mut total = 0;
    let mut fits_end = 0;
    for (index, char) in value.char_indices() {
        total += char_width(char, font_face.as_ref(), font_query.size, font_variant);
        if total + marker_width <= max_width {
            fits_end = index + char.len_utf8();
        }
    }

    if total <= max_width {
        return Some(Cow::Borrowed(value));
    }
    if marker_width > max_width {
        // Not even the marker fits.
        return Some(Cow::Borrowed(""));
    }

    let kept = &value[..fits_end];
    if marker.is_empty() {
        return Some(Cow::Borrowed(kept));
    }

    // A marker right after a space reads as a typo ("and …"), so the kept
    // part ends on a visible character.
    Some(Cow::Owned(format!("{}{marker}", kept.trim_end())))
}

pub(crate) fn text_get_width_impl<'a>(
    font_query: FontQuery,
    text: &'a str,
    font_source: &'a (dyn FontSource<'a> + 'a),
) -> Option<usize> {
    let font_face = font_source.resolve_font(
        font_query.family,
        font_query.weight,
        font_query.style,
        font_query.stretch,
    )?;

    let font_variant = font_face.font_variant(font_query.size)?;
    Some(calc_text_width(
        text,
        font_face.as_ref(),
        font_query.size,
        font_variant,
    ))
}

pub(crate) fn text_wrap_impl<
    'a,
    'b,
    TX: Into<SvgAttributeValue<'a>> + Hash + Default,
    TY: Into<SvgAttributeValue<'a>> + Hash + Default,
>(
    hash: u64,
    value: &'b str,
    font_source: &'a (dyn FontSource<'a> + 'a),
    options: BreakLinesOpts<'a, TX, TY>,
) -> Option<WrappedTextStructure> {
    let BreakLinesOpts {
        width,
        align,
        font,
        line_height,
        ..
    } = options;

    let font_face = if let Some(font_face) =
        font_source.resolve_font(font.family, font.weight, font.style, font.stretch)
    {
        font_face
    } else {
        crate::log!(
            "ERROR breaking lines: font is not resolved. Make sure that system fonts are not available for break_lines feature in editor."
        );
        return None;
    };

    let font_variant = font_face.font_variant(font.size)?;
    let space_width = match font_variant {
        crate::FontVariant::Monospaced(mono_width) => mono_width,
        crate::FontVariant::Variable => font_face.resolve_char_width(font.size, ' ')?,
    };

    let mut structure: Vec<(Vec<String>, usize)> = vec![(vec![], 0usize)];
    let line_height_in_px = line_height * font.size as f32;

    for line in value.split('\n') {
        let mut is_first_word_in_line = true;

        for word in line.split_whitespace() {
            let word_width = calc_text_width(word, font_face.as_ref(), font.size, font_variant);
            let (last_line, last_line_width) = structure.last_mut()?;

            // The first word of a line is never preceded by a space.
            let separator_width = if *last_line_width == 0 {
                0
            } else {
                space_width
            };

            if *last_line_width + separator_width + word_width > width && !last_line.is_empty() {
                structure.push((vec![word.to_owned()], word_width));
            } else {
                *last_line_width += separator_width + word_width;
                last_line.push(word.to_owned());
            }
            is_first_word_in_line = false;
        }

        if !is_first_word_in_line {
            structure.push((vec![], 0usize));
        }
    }

    if let Some((last_words, last_width)) = structure.last()
        && last_words.is_empty()
        && *last_width == 0
    {
        structure.pop();
    }

    let lines = structure
        .into_iter()
        .enumerate()
        .map(|(index, (words, line_width))| {
            let dx = match align {
                TextAlign::Left => 0,
                TextAlign::Center => width.saturating_sub(line_width) / 2,
                TextAlign::Right => width.saturating_sub(line_width),
            };

            WrappedTextLine {
                words,
                width: line_width,
                dx,
                dy: (index as f32 * line_height_in_px) as usize,
            }
        })
        .collect();

    Some(WrappedTextStructure {
        lines,
        line_height_in_px,
        hash,
    })
}

#[derive(Debug, Clone)]
pub struct EstimateTextWidthOptions<'a> {
    pub font_family: &'a str,
    pub font_size: usize,
    pub font_weight: u16,
    pub font_style: FontStyle,
    pub font_stretch: FontStretch,
}

/// Estimates the width of the text in pixels.
/// Returns `None` if the font is not resolved.
///
/// **Important! font**
pub fn estimate_text_width<'a>(
    ctx: &FFramesContext<'a, '_>,
    text: &'a str,
    options: EstimateTextWidthOptions<'a>,
) -> Option<usize> {
    let EstimateTextWidthOptions {
        font_family,
        font_size,
        font_weight,
        font_style,
        font_stretch,
    } = options;

    let font_face =
        ctx.font_source?
            .resolve_font(font_family, font_weight, font_style, font_stretch)?;

    let font_variant = font_face.font_variant(font_size)?;
    Some(calc_text_width(
        text,
        font_face.as_ref(),
        font_size,
        font_variant,
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    /// Every glyph is `CHAR_WIDTH` pixels wide at any size, which keeps the
    /// expected values below readable.
    const CHAR_WIDTH: usize = 10;

    #[derive(Debug)]
    struct FixedWidthFace {
        monospaced: bool,
    }

    impl FontFace<'_> for FixedWidthFace {
        fn is_monospaced(&self) -> Option<bool> {
            Some(self.monospaced)
        }

        fn resolve_char_width(&self, _font_size: usize, _char: char) -> Option<usize> {
            Some(CHAR_WIDTH)
        }
    }

    #[derive(Debug)]
    struct FixedWidthFonts {
        monospaced: bool,
    }

    impl<'a> FontSource<'a> for FixedWidthFonts {
        fn add_font(&mut self, _: String, _: Arc<dyn AsRef<[u8]> + Sync + Send>) {}

        fn resolve_font(
            &'a self,
            _: &str,
            _: u16,
            _: FontStyle,
            _: FontStretch,
        ) -> Option<Box<dyn FontFace<'a> + 'a>> {
            Some(Box::new(FixedWidthFace {
                monospaced: self.monospaced,
            }))
        }
    }

    const VARIABLE: FixedWidthFonts = FixedWidthFonts { monospaced: false };
    const MONOSPACED: FixedWidthFonts = FixedWidthFonts { monospaced: true };

    fn fit<'a>(
        fonts: &'a FixedWidthFonts,
        value: &'a str,
        max_width: usize,
        overflow: TextOverflow,
    ) -> Cow<'a, str> {
        text_fit_impl(FontQuery::default(), value, max_width, overflow, fonts)
            .expect("the fixed width font always resolves")
    }

    #[test]
    fn text_that_fits_is_returned_untouched() {
        let fitted = fit(
            &VARIABLE,
            "Hello world",
            11 * CHAR_WIDTH,
            TextOverflow::Ellipsis,
        );
        assert!(matches!(fitted, Cow::Borrowed("Hello world")));
    }

    #[test]
    fn ellipsis_replaces_the_characters_that_do_not_fit() {
        // 9 cells: 8 characters + the ellipsis.
        assert_eq!(
            fit(
                &VARIABLE,
                "Hello world",
                9 * CHAR_WIDTH,
                TextOverflow::Ellipsis
            ),
            "Hello wo…"
        );
    }

    #[test]
    fn ellipsis_never_follows_whitespace() {
        // 7 cells would keep "Hello " and the trailing space is dropped.
        assert_eq!(
            fit(
                &VARIABLE,
                "Hello world",
                7 * CHAR_WIDTH,
                TextOverflow::Ellipsis
            ),
            "Hello…"
        );
    }

    #[test]
    fn clip_drops_the_overflow_without_a_marker() {
        let clipped = fit(&VARIABLE, "Hello world", 8 * CHAR_WIDTH, TextOverflow::Clip);
        assert!(matches!(clipped, Cow::Borrowed("Hello wo")));
        // Clip cuts exactly at the box edge, whitespace included.
        assert_eq!(
            fit(&VARIABLE, "Hello world", 6 * CHAR_WIDTH, TextOverflow::Clip),
            "Hello "
        );
    }

    #[test]
    fn custom_marker_width_is_accounted_for() {
        assert_eq!(
            fit(
                &VARIABLE,
                "Hello world",
                8 * CHAR_WIDTH,
                TextOverflow::Marker("...")
            ),
            "Hello..."
        );
    }

    #[test]
    fn marker_wider_than_the_box_yields_nothing() {
        assert_eq!(
            fit(
                &VARIABLE,
                "Hello world",
                2 * CHAR_WIDTH,
                TextOverflow::Marker("...")
            ),
            ""
        );
        assert_eq!(
            fit(&VARIABLE, "Hello world", CHAR_WIDTH, TextOverflow::Ellipsis),
            "…"
        );
    }

    #[test]
    fn monospaced_fonts_count_characters_not_bytes() {
        // "…" is three bytes but one cell.
        assert_eq!(
            fit(
                &MONOSPACED,
                "Ünïcödé text",
                6 * CHAR_WIDTH,
                TextOverflow::Ellipsis
            ),
            "Ünïcö…"
        );
        assert_eq!(
            text_get_width_impl(FontQuery::default(), "Ünïcödé", &MONOSPACED),
            Some(7 * CHAR_WIDTH)
        );
    }
}