Skip to main content

waft_embedder/
text.rs

1//! Flutter counterpart: `engine/src/flutter/lib/ui/text.dart` value types
2//! used by painting (`FontWeight`, `TextDecoration`, `TextDirection`, …).
3//! `TextStyle` / `ParagraphStyle` / `Paragraph` are in `paragraph.rs`.
4
5use std::fmt::{self, Debug, Display};
6
7use crate::{clamp_double, lerp_double, lerp_int};
8
9/// A [`TextStyle.height`](https://api.flutter.dev/flutter/dart-ui/TextStyle/height.html)
10/// value that indicates the text span should take the height defined by the
11/// font, which may not be exactly the height of `fontSize`.
12pub const K_TEXT_HEIGHT_NONE: f64 = 0.0;
13
14/// Whether to use the italic type variation of glyphs in the font.
15///
16/// Some modern fonts allow this to be selected in a more fine-grained manner.
17/// See `FontVariation.italic` for details.
18///
19/// Italic type is distinct from slanted glyphs. To control the slant of a
20/// glyph, consider the `FontVariation.slant` font feature.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub enum FontStyle {
23    /// Use the upright ("Roman") glyphs.
24    Normal,
25
26    /// Use glyphs that have a more pronounced angle and typically a cursive style
27    /// ("italic type").
28    Italic,
29}
30
31/// The thickness of the glyphs used to draw the text.
32///
33/// Values must be in the range 1..1000.
34///
35/// Fonts are typically weighted on a 9-point scale, which, for historical
36/// reasons, uses the names 100 to 900. In Flutter, these are named `w100` to
37/// `w900` and have the following conventional meanings:
38///
39///  * [`w100`](FontWeight::W100): Thin, the thinnest font weight.
40///
41///  * [`w200`](FontWeight::W200): Extra light.
42///
43///  * [`w300`](FontWeight::W300): Light.
44///
45///  * [`w400`](FontWeight::W400): Normal. The constant [`NORMAL`](FontWeight::NORMAL) is an alias for this value.
46///
47///  * [`w500`](FontWeight::W500): Medium.
48///
49///  * [`w600`](FontWeight::W600): Semi-bold.
50///
51///  * [`w700`](FontWeight::W700): Bold. The constant [`BOLD`](FontWeight::BOLD) is an alias for this value.
52///
53///  * [`w800`](FontWeight::W800): Extra-bold.
54///
55///  * [`w900`](FontWeight::W900): Black, the thickest font weight.
56///
57/// For example, the font named "Roboto Medium" is typically exposed as a font
58/// with the name "Roboto" and the weight [`W500`](FontWeight::W500).
59///
60/// Some modern fonts allow the weight to be adjusted in arbitrary increments.
61/// When using these fonts, applications can specify [`FontWeight`] instances
62/// constructed using values other than the predefined values.
63#[derive(Clone, Copy, PartialEq, Eq, Hash)]
64pub struct FontWeight {
65    /// The thickness value of this font weight.
66    pub value: i32,
67}
68
69impl FontWeight {
70    /// Creates a [`FontWeight`] object, which can be added to a text style to
71    /// select the thickness of a font's glyphs.
72    pub fn new(value: i32) -> FontWeight {
73        debug_assert!(
74            (1..=1000).contains(&value),
75            "Font weight must be between 1 and 1000"
76        );
77        FontWeight { value }
78    }
79
80    /// The encoded integer value of this font weight.
81    ///
82    /// Deprecated in Dart in favor of [`value`](Self::value).
83    pub fn index(self) -> i32 {
84        ((self.value / 100) - 1).clamp(0, 8)
85    }
86
87    /// Thin, the least thick.
88    pub const W100: FontWeight = FontWeight { value: 100 };
89
90    /// Extra-light.
91    pub const W200: FontWeight = FontWeight { value: 200 };
92
93    /// Light.
94    pub const W300: FontWeight = FontWeight { value: 300 };
95
96    /// Normal / regular / plain.
97    pub const W400: FontWeight = FontWeight { value: 400 };
98
99    /// Medium.
100    pub const W500: FontWeight = FontWeight { value: 500 };
101
102    /// Semi-bold.
103    pub const W600: FontWeight = FontWeight { value: 600 };
104
105    /// Bold.
106    pub const W700: FontWeight = FontWeight { value: 700 };
107
108    /// Extra-bold.
109    pub const W800: FontWeight = FontWeight { value: 800 };
110
111    /// Black, the most thick.
112    pub const W900: FontWeight = FontWeight { value: 900 };
113
114    /// The default font weight.
115    pub const NORMAL: FontWeight = FontWeight::W400;
116
117    /// A commonly used font weight that is heavier than normal.
118    pub const BOLD: FontWeight = FontWeight::W700;
119
120    /// A list of all the named font weights.
121    pub const VALUES: [FontWeight; 9] = [
122        FontWeight::W100,
123        FontWeight::W200,
124        FontWeight::W300,
125        FontWeight::W400,
126        FontWeight::W500,
127        FontWeight::W600,
128        FontWeight::W700,
129        FontWeight::W800,
130        FontWeight::W900,
131    ];
132
133    /// Linearly interpolates between two font weights.
134    ///
135    /// If both `a` and `b` are null, then this method will return null. Otherwise,
136    /// any null values for `a` or `b` are interpreted as equivalent to [`NORMAL`](FontWeight::NORMAL)
137    /// (also known as [`W400`](FontWeight::W400)).
138    ///
139    /// The `t` argument represents position on the timeline, with 0.0 meaning
140    /// that the interpolation has not started, returning `a` (or something
141    /// equivalent to `a`), 1.0 meaning that the interpolation has finished,
142    /// returning `b` (or something equivalent to `b`), and values in between
143    /// meaning that the interpolation is at the relevant point on the timeline
144    /// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
145    /// 1.0, so negative values and values greater than 1.0 are valid. The result
146    /// is clamped to the range [`W100`](FontWeight::W100)–[`W900`](FontWeight::W900).
147    pub fn lerp(a: Option<FontWeight>, b: Option<FontWeight>, t: f64) -> Option<FontWeight> {
148        if a.is_none() && b.is_none() {
149            return None;
150        }
151        let a = a.unwrap_or(FontWeight::NORMAL).value;
152        let b = b.unwrap_or(FontWeight::NORMAL).value;
153        Some(FontWeight::new(
154            lerp_int(a, b, t).round().clamp(100.0, 900.0) as i32,
155        ))
156    }
157}
158
159impl Debug for FontWeight {
160    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161        Display::fmt(self, f)
162    }
163}
164
165impl Display for FontWeight {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        if self.value % 100 != 0 {
168            return write!(f, "FontWeight({})", self.value);
169        }
170        let name = match self.index() {
171            0 => "w100",
172            1 => "w200",
173            2 => "w300",
174            3 => "w400",
175            4 => "w500",
176            5 => "w600",
177            6 => "w700",
178            7 => "w800",
179            8 => "w900",
180            _ => unreachable!(),
181        };
182        write!(f, "FontWeight.{name}")
183    }
184}
185
186/// Whether and how to align text horizontally.
187///
188/// The order of this enum must match the order of the values in
189/// RenderStyleConstants.h's ETextAlign.
190#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
191pub enum TextAlign {
192    /// Align the text on the left edge of the container.
193    Left,
194
195    /// Align the text on the right edge of the container.
196    Right,
197
198    /// Align the text in the center of the container.
199    Center,
200
201    /// Stretch lines of text that end with a soft line break to fill the width of
202    /// the container.
203    ///
204    /// Lines that end with hard line breaks are aligned towards the [`Start`](TextAlign::Start) edge.
205    Justify,
206
207    /// Align the text on the leading edge of the container.
208    ///
209    /// For left-to-right text ([`TextDirection::Ltr`]), this is the left edge.
210    ///
211    /// For right-to-left text ([`TextDirection::Rtl`]), this is the right edge.
212    Start,
213
214    /// Align the text on the trailing edge of the container.
215    ///
216    /// For left-to-right text ([`TextDirection::Ltr`]), this is the right edge.
217    ///
218    /// For right-to-left text ([`TextDirection::Rtl`]), this is the left edge.
219    End,
220}
221
222/// A horizontal line used for aligning text.
223#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
224pub enum TextBaseline {
225    /// The horizontal line used to align the bottom of glyphs for alphabetic characters.
226    ///
227    /// This baseline is often used for alphabetical scripts like Latin, Greek,
228    /// Cyrillic, etc.
229    ///
230    /// Characters with descenders (like 'p', 'g', or 'y') extend below this line.
231    Alphabetic,
232
233    /// The horizontal line used to align ideographic characters.
234    ///
235    /// This baseline is often used for scripts with uniform square heights,
236    /// like Chinese, Japanese, Korean, etc.
237    Ideographic,
238}
239
240/// A linear decoration to draw near the text.
241#[derive(Clone, Copy, PartialEq, Eq, Hash)]
242pub struct TextDecoration {
243    mask: i32,
244}
245
246impl TextDecoration {
247    /// Do not draw a decoration
248    pub const NONE: TextDecoration = TextDecoration { mask: 0x0 };
249
250    /// Draw a line underneath each line of text
251    pub const UNDERLINE: TextDecoration = TextDecoration { mask: 0x1 };
252
253    /// Draw a line above each line of text
254    pub const OVERLINE: TextDecoration = TextDecoration { mask: 0x2 };
255
256    /// Draw a line through each line of text
257    pub const LINE_THROUGH: TextDecoration = TextDecoration { mask: 0x4 };
258
259    /// Creates a decoration that paints the union of all the given decorations.
260    pub fn combine(decorations: &[TextDecoration]) -> TextDecoration {
261        let mut mask = 0;
262        for decoration in decorations {
263            mask |= decoration.mask;
264        }
265        TextDecoration { mask }
266    }
267
268    /// The bit mask value that represents this decoration.
269    pub fn mask_value(self) -> i32 {
270        self.mask
271    }
272
273    /// Whether this decoration will paint at least as much decoration as the given decoration.
274    pub fn contains(self, other: TextDecoration) -> bool {
275        (self.mask | other.mask) == self.mask
276    }
277}
278
279impl Debug for TextDecoration {
280    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
281        Display::fmt(self, f)
282    }
283}
284
285impl Display for TextDecoration {
286    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287        if self.mask == 0 {
288            return write!(f, "TextDecoration.none");
289        }
290        let mut values = Vec::new();
291        if self.mask & TextDecoration::UNDERLINE.mask != 0 {
292            values.push("underline");
293        }
294        if self.mask & TextDecoration::OVERLINE.mask != 0 {
295            values.push("overline");
296        }
297        if self.mask & TextDecoration::LINE_THROUGH.mask != 0 {
298            values.push("lineThrough");
299        }
300        if values.len() == 1 {
301            write!(f, "TextDecoration.{}", values[0])
302        } else {
303            write!(f, "TextDecoration.combine([{}])", values.join(", "))
304        }
305    }
306}
307
308/// The style in which to draw a text decoration
309#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
310pub enum TextDecorationStyle {
311    /// Draw a solid line
312    Solid,
313
314    /// Draw two lines
315    Double,
316
317    /// Draw a dotted line
318    Dotted,
319
320    /// Draw a dashed line
321    Dashed,
322
323    /// Draw a sinusoidal line
324    Wavy,
325}
326
327/// How the leading of the text is distributed over and under the text.
328#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
329pub enum TextLeadingDistribution {
330    /// Distributes the leading of the text proportionally above and below the
331    /// text, to the font's ascent/descent ratio.
332    Proportional,
333
334    /// Distributes the leading of the text evenly above and below the text
335    /// (i.e. evenly above the font's ascender and below the descender).
336    ///
337    /// The leading can become negative when `TextStyle.height` is smaller than
338    /// 1.0.
339    ///
340    /// This is the default strategy used by CSS, known as "half-leading".
341    Even,
342}
343
344/// Defines how to apply `TextStyle.height` over and under text.
345#[derive(Clone, Copy, Debug, PartialEq)]
346pub struct TextHeightBehavior {
347    /// Whether to apply the `TextStyle.height` modifier to the ascent of the first
348    /// line in the paragraph.
349    pub apply_height_to_first_ascent: bool,
350
351    /// Whether to apply the `TextStyle.height` modifier to the descent of the last
352    /// line in the paragraph.
353    pub apply_height_to_last_descent: bool,
354
355    /// How the leading is distributed over and under the text.
356    pub leading_distribution: TextLeadingDistribution,
357}
358
359/// Dart's `TextHeightBehavior.toString`.
360impl Display for TextHeightBehavior {
361    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
362        let leading_distribution = match self.leading_distribution {
363            TextLeadingDistribution::Proportional => "proportional",
364            TextLeadingDistribution::Even => "even",
365        };
366        write!(
367            f,
368            "TextHeightBehavior(applyHeightToFirstAscent: {}, applyHeightToLastDescent: {}, \
369             leadingDistribution: TextLeadingDistribution.{leading_distribution})",
370            self.apply_height_to_first_ascent, self.apply_height_to_last_descent,
371        )
372    }
373}
374
375impl Default for TextHeightBehavior {
376    fn default() -> TextHeightBehavior {
377        TextHeightBehavior {
378            apply_height_to_first_ascent: true,
379            apply_height_to_last_descent: true,
380            leading_distribution: TextLeadingDistribution::Proportional,
381        }
382    }
383}
384
385/// A direction in which text flows.
386///
387/// Some languages are written from the left to the right (for example, English,
388/// Tamil, or Chinese), while others are written from the right to the left (for
389/// example Aramaic, Hebrew, or Urdu). Some are also written in a mixture, for
390/// example Arabic is mostly written right-to-left, with numerals written
391/// left-to-right.
392///
393/// The text direction must be provided to APIs that render text or lay out
394/// boxes horizontally, so that they can determine which direction to start in:
395/// either right-to-left, [`TextDirection::Rtl`]; or left-to-right,
396/// [`TextDirection::Ltr`].
397///
398/// ## Design discussion
399///
400/// Flutter is designed to address the needs of applications written in any of
401/// the world's currently-used languages, whether they use a right-to-left or
402/// left-to-right writing direction. Flutter does not support other writing
403/// modes, such as vertical text or boustrophedon text, as these are rarely used
404/// in computer programs.
405///
406/// It is common when developing user interface frameworks to pick a default
407/// text direction — typically left-to-right, the direction most familiar to the
408/// engineers working on the framework — because this simplifies the development
409/// of applications on the platform. Unfortunately, this frequently results in
410/// the platform having unexpected left-to-right biases or assumptions, as
411/// engineers will typically miss places where they need to support
412/// right-to-left text. This then results in bugs that only manifest in
413/// right-to-left environments.
414///
415/// In an effort to minimize the extent to which Flutter experiences this
416/// category of issues, the lowest levels of the Flutter framework do not have a
417/// default text reading direction. Any time a reading direction is necessary,
418/// for example when text is to be displayed, or when a
419/// writing-direction-dependent value is to be interpreted, the reading
420/// direction must be explicitly specified. Where possible, such as in `match`
421/// statements, the right-to-left case is listed first, to avoid the impression
422/// that it is an afterthought.
423///
424/// At the higher levels (specifically starting at the widgets library), an
425/// ambient `Directionality` is introduced, which provides a default. Thus, for
426/// instance, a `Text` widget in the scope of a `MaterialApp` widget does not
427/// need to be given an explicit writing direction. The `Directionality.of`
428/// static method can be used to obtain the ambient text direction for a
429/// particular `BuildContext`.
430///
431/// ### Known left-to-right biases in Flutter
432///
433/// Despite the design intent described above, certain left-to-right biases have
434/// nonetheless crept into Flutter's design. These include:
435///
436///  * The `Canvas` origin is at the top left, and the x-axis increases in a
437///    left-to-right direction.
438///
439///  * The default localization in the widgets and material libraries is
440///    American English, which is left-to-right.
441///
442/// ### Visual properties vs directional properties
443///
444/// Many classes in the Flutter framework are offered in two versions, a
445/// visually-oriented variant, and a text-direction-dependent variant. For
446/// example, `EdgeInsets` is described in terms of top, left, right, and
447/// bottom, while `EdgeInsetsDirectional` is described in terms of top, start,
448/// end, and bottom, where start and end correspond to right and left in
449/// right-to-left text and left and right in left-to-right text.
450///
451/// There are distinct use cases for each of these variants.
452///
453/// Text-direction-dependent variants are useful when developing user interfaces
454/// that should "flip" with the text direction. For example, a paragraph of text
455/// in English will typically be left-aligned and a quote will be indented from
456/// the left, while in Arabic it will be right-aligned and indented from the
457/// right. Both of these cases are described by the direction-dependent
458/// [`TextAlign::Start`] and `EdgeInsetsDirectional` start.
459///
460/// In contrast, the visual variants are useful when the text direction is known
461/// and not affected by the reading direction. For example, an application
462/// giving driving directions might show a "turn left" arrow on the left and a
463/// "turn right" arrow on the right — and would do so whether the application
464/// was localized to French (left-to-right) or Hebrew (right-to-left).
465///
466/// In practice, it is also expected that many developers will only be targeting
467/// one language, and in that case it may be simpler to think in visual terms.
468///
469/// The order of this enum must match the order of the values in
470/// `TextDirection.h`'s `TextDirection`.
471#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
472pub enum TextDirection {
473    /// The text flows from right to left (e.g. Arabic, Hebrew).
474    Rtl,
475
476    /// The text flows from left to right (e.g., English, French).
477    Ltr,
478}
479
480/// A feature tag and value that affect the selection of glyphs in a font.
481///
482/// Named constructors for individual OpenType tags (`alternative`, `fractions`,
483/// …) are deferred until a caller needs them. [`new`](FontFeature::new),
484/// [`enable`](FontFeature::enable), and [`disable`](FontFeature::disable) are
485/// the constructors Flutter's painting `TextStyle` uses.
486#[derive(Clone, Debug, PartialEq, Eq, Hash)]
487pub struct FontFeature {
488    /// The tag that identifies the effect of this feature. Must consist of 4
489    /// ASCII characters.
490    pub feature: String,
491    /// The value assigned to this feature.
492    ///
493    /// Must be a positive integer. Many features are Boolean values using 1
494    /// (on) and 0 (off).
495    pub value: i32,
496}
497
498impl FontFeature {
499    /// Creates a [`FontFeature`] object, which can be added to a text style to
500    /// change how the engine selects glyphs when rendering text.
501    pub fn new(feature: impl Into<String>, value: i32) -> FontFeature {
502        let feature = feature.into();
503        debug_assert!(
504            feature.len() == 4,
505            "Feature tag must be exactly four characters long."
506        );
507        debug_assert!(
508            value >= 0,
509            "Feature value must be zero or a positive integer."
510        );
511        FontFeature { feature, value }
512    }
513
514    /// Create a [`FontFeature`] object that enables the feature with the given
515    /// tag.
516    pub fn enable(feature: impl Into<String>) -> FontFeature {
517        FontFeature::new(feature, 1)
518    }
519
520    /// Create a [`FontFeature`] object that disables the feature with the given
521    /// tag.
522    pub fn disable(feature: impl Into<String>) -> FontFeature {
523        FontFeature::new(feature, 0)
524    }
525}
526
527impl Display for FontFeature {
528    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
529        write!(f, "FontFeature('{}', {})", self.feature, self.value)
530    }
531}
532
533/// An axis tag and value that can be used to customize variable fonts.
534///
535/// Named constructors besides [`italic`](FontVariation::italic),
536/// [`optical_size`](FontVariation::optical_size), [`slant`](FontVariation::slant),
537/// [`width`](FontVariation::width), and [`weight`](FontVariation::weight) are
538/// not added until a caller needs them.
539#[derive(Clone, Debug, PartialEq)]
540pub struct FontVariation {
541    /// The tag that identifies the design axis.
542    ///
543    /// An axis tag must consist of 4 ASCII characters.
544    pub axis: String,
545    /// The value assigned to this design axis.
546    pub value: f64,
547}
548
549impl FontVariation {
550    /// Creates a [`FontVariation`] object, which can be added to a text style to
551    /// change the variable attributes of a font.
552    pub fn new(axis: impl Into<String>, value: f64) -> FontVariation {
553        let axis = axis.into();
554        debug_assert!(
555            axis.len() == 4,
556            "Axis tag must be exactly four characters long."
557        );
558        debug_assert!(
559            (-32768.0..32768.0).contains(&value),
560            "Value must be representable as a signed 16.16 fixed-point number, i.e. it must be in this range: -32768.0 ≤ value < 32768.0"
561        );
562        FontVariation { axis, value }
563    }
564
565    /// Variable font style. (`ital`)
566    pub fn italic(value: f64) -> FontVariation {
567        debug_assert!((0.0..=1.0).contains(&value));
568        FontVariation::new("ital", value)
569    }
570
571    /// Optical size optimization. (`opsz`)
572    pub fn optical_size(value: f64) -> FontVariation {
573        debug_assert!(value > 0.0);
574        FontVariation::new("opsz", value)
575    }
576
577    /// Variable font slant. (`slnt`)
578    pub fn slant(value: f64) -> FontVariation {
579        debug_assert!(value > -90.0 && value < 90.0);
580        FontVariation::new("slnt", value)
581    }
582
583    /// Variable font width. (`wdth`)
584    pub fn width(value: f64) -> FontVariation {
585        debug_assert!(value >= 0.0);
586        FontVariation::new("wdth", value)
587    }
588
589    /// Variable font weight. (`wght`)
590    pub fn weight(value: f64) -> FontVariation {
591        debug_assert!((1.0..=1000.0).contains(&value));
592        FontVariation::new("wght", value)
593    }
594
595    /// Linearly interpolates between two font variations.
596    ///
597    /// If the two variations have different axis tags, the interpolation
598    /// switches abruptly from one to the other at t=0.5. Otherwise, the value
599    /// is interpolated.
600    pub fn lerp(
601        a: Option<&FontVariation>,
602        b: Option<&FontVariation>,
603        t: f64,
604    ) -> Option<FontVariation> {
605        if a.map(|v| v.axis.as_str()) != b.map(|v| v.axis.as_str()) || (a.is_none() && b.is_none())
606        {
607            return if t < 0.5 { a.cloned() } else { b.cloned() };
608        }
609        Some(FontVariation::new(
610            a.unwrap().axis.clone(),
611            clamp_double(
612                lerp_double(Some(a.unwrap().value), Some(b.unwrap().value), t).unwrap(),
613                -32768.0,
614                32768.0 - 1.0 / 65536.0,
615            ),
616        ))
617    }
618}
619
620impl Display for FontVariation {
621    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
622        write!(f, "FontVariation('{}', {})", self.axis, self.value)
623    }
624}
625
626#[cfg(test)]
627mod tests {
628    use super::*;
629
630    #[test]
631    fn font_weight_lerp_treats_null_as_normal() {
632        assert!(FontWeight::lerp(None, None, 0.5).is_none());
633        assert_eq!(
634            FontWeight::lerp(None, Some(FontWeight::W700), 0.0),
635            Some(FontWeight::NORMAL)
636        );
637        assert_eq!(
638            FontWeight::lerp(Some(FontWeight::W100), Some(FontWeight::W900), 0.5),
639            Some(FontWeight::W500)
640        );
641    }
642
643    #[test]
644    fn text_decoration_combine_unions_masks() {
645        let combined =
646            TextDecoration::combine(&[TextDecoration::UNDERLINE, TextDecoration::LINE_THROUGH]);
647        assert!(combined.contains(TextDecoration::UNDERLINE));
648        assert!(combined.contains(TextDecoration::LINE_THROUGH));
649        assert!(!combined.contains(TextDecoration::OVERLINE));
650        assert_eq!(
651            format!("{combined}"),
652            "TextDecoration.combine([underline, lineThrough])"
653        );
654    }
655
656    #[test]
657    fn text_direction_lists_rtl_first() {
658        assert_eq!(TextDirection::Rtl as u8, 0);
659        assert_eq!(TextDirection::Ltr as u8, 1);
660    }
661
662    #[test]
663    fn font_variation_lerp_switches_axis_at_half() {
664        let a = FontVariation::weight(400.0);
665        let b = FontVariation::width(100.0);
666        assert_eq!(
667            FontVariation::lerp(Some(&a), Some(&b), 0.4).unwrap().axis,
668            "wght"
669        );
670        assert_eq!(
671            FontVariation::lerp(Some(&a), Some(&b), 0.6).unwrap().axis,
672            "wdth"
673        );
674    }
675
676    #[test]
677    fn font_variation_lerp_interpolates_same_axis() {
678        let a = FontVariation::weight(100.0);
679        let b = FontVariation::weight(900.0);
680        let mid = FontVariation::lerp(Some(&a), Some(&b), 0.5).unwrap();
681        assert_eq!(mid.axis, "wght");
682        assert!((mid.value - 500.0).abs() < 1e-9);
683    }
684}