frus_core/text_style.rs
1//! [`TextStyle`]: the typographic attributes of a piece of text (size, weight,
2//! italic, colour), independent of any widget or theme.
3//!
4//! A `TextStyle` is a pure `Copy` value. Its `color` is optional, and `None`
5//! means "inherit" — the widget resolves it to the theme colour at paint time.
6//! Named typographic scales, in the style of a Material `TextTheme`, are built
7//! out of this type.
8
9use crate::Color;
10
11/// How the lines of a piece of text are **aligned inside the box** it was given.
12///
13/// It is not the same question as where the box goes. A centred paragraph and a centred
14/// box look identical while the text is one line and stop looking identical the moment it
15/// wraps, which is when the setting starts to matter.
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
17pub enum TextAlign {
18 /// The start of the reading direction: the left in a left-to-right script, the right
19 /// in a right-to-left one. The default, and the only one that follows the text.
20 #[default]
21 Start,
22 /// The end of the reading direction.
23 End,
24 /// Centred in the box.
25 Center,
26 /// Both edges flush, the space stretched between the words. Every line but the last.
27 Justify,
28 /// The left edge, whatever the script. A column of figures wants this and not
29 /// [`TextAlign::Start`], because the figures do not change direction with the prose
30 /// around them.
31 Left,
32 /// The right edge, whatever the script.
33 Right,
34}
35
36/// What becomes of text that does not fit the box it was given.
37#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
38pub enum TextOverflow {
39 /// Cut at the edge of the box. The default, and the quiet one: nothing marks the
40 /// place where the words stopped.
41 #[default]
42 Clip,
43 /// Cut, with the last line ending in an ellipsis. The only mode that tells the reader
44 /// something is missing, which is usually the one wanted.
45 Ellipsis,
46 /// Cut, with the last line fading out into the background rather than stopping at a
47 /// hard edge.
48 Fade,
49 /// Draw past the box. Not an accident: a badge or a decoration that is *meant* to
50 /// spill says so this way, rather than by being given a box it does not fit.
51 Visible,
52}
53
54/// Font weight — a useful subset, mapped onto the CSS/OpenType weights.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
56pub enum FontWeight {
57 /// 400 — regular.
58 #[default]
59 Regular,
60 /// 500.
61 Medium,
62 /// 600.
63 SemiBold,
64 /// 700 — bold.
65 Bold,
66}
67
68impl FontWeight {
69 /// The numeric OpenType weight (400/500/600/700).
70 pub fn to_u16(self) -> u16 {
71 match self {
72 FontWeight::Regular => 400,
73 FontWeight::Medium => 500,
74 FontWeight::SemiBold => 600,
75 FontWeight::Bold => 700,
76 }
77 }
78
79 /// The weight `t` of the way from `self` to `other` — **the nearest of the four**,
80 /// not a number between them.
81 ///
82 /// A weight is drawn with a face, and there are four faces. Interpolating the
83 /// *number* would ask for a 437 that no font has, so what moves is the choice: the
84 /// numeric weights are interpolated and the result snaps to whichever of the four is
85 /// closest. Halfway from regular to bold is semi-bold, and a run of words thickens in
86 /// steps rather than gliding.
87 ///
88 /// The reference does exactly this over its nine weights, and this is its method
89 /// rather than a lookalike: the **position in the list** is interpolated and rounded,
90 /// not the OpenType number. The two agree here because the four are evenly spaced,
91 /// and the list is what stays right if a fifth face is ever added between two of them.
92 ///
93 /// **A tie goes to the heavier face.** Exactly halfway from regular to bold is 550,
94 /// which has no nearest — rounding a half up the list answers semi-bold, going in
95 /// either direction, so a run of words thickening and one thinning pass through the
96 /// same face at the same moment instead of missing each other by one step.
97 ///
98 /// Four steps make the stepping easier to see than nine would. That is a reason to
99 /// say so here, not a reason to do something else: a variable-font axis is the thing
100 /// that would make a weight glide, and this framework does not have one yet.
101 #[must_use]
102 pub fn lerp(self, other: FontWeight, t: f32) -> FontWeight {
103 const FACES: [FontWeight; 4] = [
104 FontWeight::Regular,
105 FontWeight::Medium,
106 FontWeight::SemiBold,
107 FontWeight::Bold,
108 ];
109 fn place(weight: FontWeight) -> f32 {
110 match weight {
111 FontWeight::Regular => 0.0,
112 FontWeight::Medium => 1.0,
113 FontWeight::SemiBold => 2.0,
114 FontWeight::Bold => 3.0,
115 }
116 }
117 let a = place(self);
118 let b = place(other);
119 // `round` takes a half away from zero and every place is positive, so a tie goes
120 // up the list — to the heavier face, as documented above.
121 let step = (a + (b - a) * t.clamp(0.0, 1.0)).round().clamp(0.0, 3.0);
122 FACES[step as usize]
123 }
124}
125
126/// A text's **decoration** lines, which combine with one another. They have no
127/// effect on measurement: the text's geometry does not change, and the lines are
128/// drawn by the backend from the baseline metrics.
129#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
130pub struct TextDecoration {
131 /// Underline, below the baseline.
132 pub underline: bool,
133 /// A line above the text.
134 pub overline: bool,
135 /// Struck through, at the middle of the x-height.
136 pub strikethrough: bool,
137}
138
139impl TextDecoration {
140 /// No decoration at all.
141 pub const NONE: Self = Self {
142 underline: false,
143 overline: false,
144 strikethrough: false,
145 };
146 /// Underline only.
147 pub const UNDERLINE: Self = Self {
148 underline: true,
149 ..Self::NONE
150 };
151 /// Overline only.
152 pub const OVERLINE: Self = Self {
153 overline: true,
154 ..Self::NONE
155 };
156 /// Strikethrough only.
157 pub const STRIKETHROUGH: Self = Self {
158 strikethrough: true,
159 ..Self::NONE
160 };
161
162 /// `true` when no line at all is asked for.
163 pub const fn is_none(self) -> bool {
164 !self.underline && !self.overline && !self.strikethrough
165 }
166
167 /// Combines two decorations — the union of their lines.
168 pub const fn combine(self, other: Self) -> Self {
169 Self {
170 underline: self.underline || other.underline,
171 overline: self.overline || other.overline,
172 strikethrough: self.strikethrough || other.strikethrough,
173 }
174 }
175}
176
177/// The font size a text ends up at when nothing anywhere in the chain named one.
178pub const DEFAULT_TEXT_SIZE: f32 = 16.0;
179
180/// A **font family**, as a style names it.
181///
182/// `Copy`, and `Named` holds a `&'static str`, so that a [`TextStyle`] stays `Copy` and can
183/// be handed down a subtree by value like every other field. Family names come from
184/// `frus_text::add_font`, which registers them for the life of the program, so a borrowed
185/// name outliving the style is the normal case rather than a restriction.
186#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
187pub enum FontFamily {
188 /// The application's default sans-serif — what text gets when it says nothing.
189 SansSerif,
190 /// A serif face, if the platform or the application has one.
191 Serif,
192 /// The registered monospaced family: code, keys, anything whose columns must line up.
193 Monospace,
194 /// A family **by name**, as registered with `frus_text::add_font`.
195 Named(&'static str),
196}
197
198/// The line height a style that says nothing gets, as a **multiple of the font size**.
199///
200/// 1.2 is what the bundled faces want, and what every part of the framework used before it
201/// was expressible at all — see [`TextStyle::height`].
202pub const DEFAULT_LINE_HEIGHT: f32 = 1.2;
203
204/// The typographic attributes of a single line of text — **every field optional**.
205///
206/// `None` is not a missing value but an answer: *this style does not say*. That is what
207/// makes a style inheritable field by field, and it is the reference's shape — every field
208/// of its `TextStyle` is nullable for exactly this reason.
209///
210/// It matters more than it looks. `TextStyle::new(20.0)` says a size and **nothing else**,
211/// so a text wearing it still takes its weight from whatever subtree it is in. Before this
212/// was optional, a style named all three at once and *size 20, inherit the weight* was
213/// simply not expressible.
214///
215/// Ask [`TextStyle::resolved`] for the concrete numbers to measure and draw with.
216#[derive(Clone, Copy, Debug, Default, PartialEq)]
217pub struct TextStyle {
218 /// Font size, in logical pixels. Unset, [`DEFAULT_TEXT_SIZE`].
219 pub size: Option<f32>,
220 /// Weight. Unset, [`FontWeight::Regular`].
221 pub weight: Option<FontWeight>,
222 /// Italic. Unset, upright.
223 pub italic: Option<bool>,
224 /// Colour. Unset, the widget resolves it against the theme at paint.
225 pub color: Option<Color>,
226 /// Decoration lines (underline, strikethrough, and so on). Unset, none — and set to
227 /// [`TextDecoration::NONE`] is a **different** answer, being how a caller takes an
228 /// underline back off a run of words a subtree underlined.
229 pub decoration: Option<TextDecoration>,
230 /// Decoration colour; unset, the text's own.
231 pub decoration_color: Option<Color>,
232 /// The line's height, as a **multiple of the font size** — the reference's `height`.
233 ///
234 /// `1.0` sets each line box to exactly the font size, `1.5` to half again; unset says
235 /// nothing, and a style that says nothing inherits whatever a subtree handed down, or
236 /// [`DEFAULT_LINE_HEIGHT`] at the end of that chain.
237 ///
238 /// It is a **ratio, not a length**, for the reason the reference gives: a paragraph
239 /// keeps its rhythm when the reader turns the type up, because the leading grows with
240 /// the letters instead of staying where a designer left it.
241 pub height: Option<f32>,
242 /// The font family — the reference's `fontFamily`. Unset, the application's default.
243 ///
244 /// **It does not always get the last word, and that is deliberate.** A run containing
245 /// Arabic is drawn with the registered Arabic face even when a family was named,
246 /// because cosmic-text does not fall back across families on Android, where the
247 /// platform fallback lists are empty: a named family without Arabic coverage renders
248 /// *nothing at all*. Text in an unexpected face is a smaller failure than no text, and
249 /// naming the Arabic family itself still works.
250 pub family: Option<FontFamily>,
251 /// The room added after every character, in **logical pixels** — the reference's
252 /// `letterSpacing`; negative draws them closer (milestone 623). Unset, none.
253 ///
254 /// A length, not a ratio, as the reference has it, and **not** grown by the reader's
255 /// font setting: the reference scales a style's font size and leaves its letter spacing
256 /// as it was written.
257 pub letter_spacing: Option<f32>,
258}
259
260/// A [`TextStyle`] with every question answered: what to measure with, and what to draw.
261///
262/// The colour stays optional, and deliberately. Size, weight and slant have a *framework*
263/// default that is right everywhere; a colour's last word belongs to the theme, which this
264/// type cannot see. A widget resolves it at paint against `on_surface`.
265#[derive(Clone, Copy, Debug, PartialEq)]
266pub struct ResolvedTextStyle {
267 /// Font size, in logical pixels.
268 pub size: f32,
269 /// Weight.
270 pub weight: FontWeight,
271 /// Italic.
272 pub italic: bool,
273 /// Explicit colour; `None` means the widget resolves it against the theme.
274 pub color: Option<Color>,
275 /// Decoration lines.
276 pub decoration: TextDecoration,
277 /// Decoration colour; `None` means the text's own colour.
278 pub decoration_color: Option<Color>,
279 /// The line's height as a **multiple of the font size**; `None` means
280 /// [`DEFAULT_LINE_HEIGHT`]. Read it through [`line_height`](Self::line_height).
281 pub height: Option<f32>,
282 /// The font family; `None` means the application's default. See
283 /// [`TextStyle::family`] for why it does not always win.
284 pub family: Option<FontFamily>,
285 /// The room added after every character, in logical pixels; `0` for none.
286 pub letter_spacing: f32,
287}
288
289impl ResolvedTextStyle {
290 /// The height of one line, in logical pixels.
291 ///
292 /// **The one place this number is decided.** It used to be a `LINE_HEIGHT_FACTOR`
293 /// constant in `frus-text` and *another* in `frus-gpu`, which is two constants that
294 /// had to agree: a measure and a paint disagreeing about how tall a line is puts the
295 /// second line of every paragraph somewhere the layout did not reserve.
296 pub fn line_height(&self) -> f32 {
297 self.size * self.height.unwrap_or(DEFAULT_LINE_HEIGHT)
298 }
299 /// A style of **exactly** this size — regular, upright, undecorated, and *not* put
300 /// through the reader's font setting.
301 ///
302 /// For glyphs whose size is geometry rather than type: an icon on its 24 px grid, a
303 /// marker that has to stay inside a circle drawn beside it. The reference's `Icon` is
304 /// sized by `IconTheme` and ignores the text scaler for the same reason — an icon that
305 /// grew with the type would leave its own box.
306 ///
307 /// Everything a reader *reads* wants
308 /// [`TextStyle::new(size).resolved()`](TextStyle::resolved) instead. The two are one
309 /// keyword apart on purpose: the choice is worth stating at every call, which is why
310 /// there is no third form that guesses.
311 pub const fn exact(size: f32) -> Self {
312 Self {
313 size,
314 weight: FontWeight::Regular,
315 italic: false,
316 color: None,
317 decoration: TextDecoration::NONE,
318 decoration_color: None,
319 height: None,
320 family: None,
321 letter_spacing: 0.0,
322 }
323 }
324}
325
326impl TextStyle {
327 /// A style that says **nothing at all** — every field inherited.
328 pub const NONE: Self = Self {
329 size: None,
330 weight: None,
331 italic: None,
332 color: None,
333 decoration: None,
334 decoration_color: None,
335 height: None,
336 family: None,
337 letter_spacing: None,
338 };
339
340 /// A style that names a `size` and nothing else.
341 pub const fn new(size: f32) -> Self {
342 Self {
343 size: Some(size),
344 ..Self::NONE
345 }
346 }
347
348 /// Sets the weight.
349 pub const fn weight(mut self, weight: FontWeight) -> Self {
350 self.weight = Some(weight);
351 self
352 }
353
354 /// Switches to italic.
355 pub const fn italic(mut self) -> Self {
356 self.italic = Some(true);
357 self
358 }
359
360 /// Sets the size.
361 pub const fn size(mut self, size: f32) -> Self {
362 self.size = Some(size);
363 self
364 }
365
366 /// Sets the colour.
367 pub const fn color(mut self, color: Color) -> Self {
368 self.color = Some(color);
369 self
370 }
371
372 /// Sets the decoration lines.
373 pub const fn decoration(mut self, decoration: TextDecoration) -> Self {
374 self.decoration = Some(decoration);
375 self
376 }
377
378 /// Adds an underline, combining with any other decoration **this style** already
379 /// carries — never with an inherited one, which this style cannot see.
380 pub const fn underline(mut self) -> Self {
381 let current = match self.decoration {
382 Some(d) => d,
383 None => TextDecoration::NONE,
384 };
385 self.decoration = Some(current.combine(TextDecoration::UNDERLINE));
386 self
387 }
388
389 /// Adds a strikethrough, combining with any other decoration this style carries.
390 pub const fn strikethrough(mut self) -> Self {
391 let current = match self.decoration {
392 Some(d) => d,
393 None => TextDecoration::NONE,
394 };
395 self.decoration = Some(current.combine(TextDecoration::STRIKETHROUGH));
396 self
397 }
398
399 /// Sets the decoration colour; otherwise the text's colour is used.
400 pub const fn decoration_color(mut self, color: Color) -> Self {
401 self.decoration_color = Some(color);
402 self
403 }
404
405 /// Sets the line's height, as a **multiple of the font size** — `2.0` is double
406 /// leading, not two pixels. See the [field](Self::height) for why it is a ratio.
407 pub const fn height(mut self, height: f32) -> Self {
408 self.height = Some(height);
409 self
410 }
411
412 /// Names the font family. See the [field](Self::family) for the one case where a
413 /// named family does not get the last word.
414 pub const fn family(mut self, family: FontFamily) -> Self {
415 self.family = Some(family);
416 self
417 }
418
419 /// Sets the room added after every character, in logical pixels; negative draws them
420 /// closer. See the [field](Self::letter_spacing).
421 pub const fn letter_spacing(mut self, spacing: f32) -> Self {
422 self.letter_spacing = Some(spacing);
423 self
424 }
425
426 /// **Merges** `over` on top of `self`, **field by field**: where `over` said nothing,
427 /// this one's answer survives.
428 ///
429 /// The cascade, and it is one operation now rather than three. Until every field could
430 /// say "unset" this codebase carried the same idea three times — a private `Overrides`
431 /// struct for rich-text spans, a `Chosen` record of booleans beside a `Text`'s style,
432 /// and half of the widgets' `DefaultTextStyle` — because a whole-style merge could only
433 /// replace the typography wholesale and each caller needed the other behaviour.
434 #[must_use]
435 pub fn merge(self, over: TextStyle) -> TextStyle {
436 TextStyle {
437 size: over.size.or(self.size),
438 weight: over.weight.or(self.weight),
439 italic: over.italic.or(self.italic),
440 color: over.color.or(self.color),
441 decoration: over.decoration.or(self.decoration),
442 decoration_color: over.decoration_color.or(self.decoration_color),
443 height: over.height.or(self.height),
444 family: over.family.or(self.family),
445 letter_spacing: over.letter_spacing.or(self.letter_spacing),
446 }
447 }
448
449 /// The style `t` of the way from `self` to `other` — what an animated default text
450 /// style hands its subtree mid-flight.
451 ///
452 /// Three rules, one per kind of field, and the kinds are not a taxonomy — each is a
453 /// different answer to *what is halfway between these two*:
454 ///
455 /// - **Sizes and ratios travel** (`size`, `height`, `letter_spacing`): a number between the two.
456 /// - **A field set at only one end does not travel.** It holds the one value it has,
457 /// from the first frame to the last. `None` here means *unset* — a question passed
458 /// further up the cascade — and there is no number between "18 pixels" and "ask
459 /// somebody else". Interpolating towards a value nobody has stated would be
460 /// interpolating towards whatever the theme happens to say, which changes what the
461 /// movement looks like when the theme changes and is not what either end asked for.
462 /// The reference lands on the same rule from the other end, taking the stated value
463 /// for *both* of its own, and it is the rule an animated pin follows for the same
464 /// reason: **a movement needs both ends to say where they are.**
465 /// - **Faces and lines swap at the halfway point** (`italic`, `decoration`, `family`):
466 /// there is no half-italic face and no two-thirds of an underline, so the change
467 /// happens once, in the middle, where it is least noticeable. The reference's rule
468 /// again, and `weight` is the near miss — see [`FontWeight::lerp`], which steps
469 /// through the four faces rather than swapping once.
470 ///
471 /// **The colours diverge from the reference deliberately.** It fades an unset colour
472 /// to transparent, because there `null` means *no colour was painted*. Here `None`
473 /// means *the widget resolves it against the theme at paint* — `on_surface`, a
474 /// perfectly visible colour — so fading it out would make a run of words vanish
475 /// halfway through a movement that was only ever about its size. An unset colour
476 /// therefore follows the rule above: it does not travel.
477 #[must_use]
478 pub fn lerp(self, other: TextStyle, t: f32) -> TextStyle {
479 let t = t.clamp(0.0, 1.0);
480 /// A number, where both ends stated one. Where only one did, that one — held.
481 fn number(a: Option<f32>, b: Option<f32>, t: f32) -> Option<f32> {
482 match (a, b) {
483 (Some(a), Some(b)) => Some(a + (b - a) * t),
484 (a, b) => a.or(b),
485 }
486 }
487 /// The same rule for a colour, and for the same reason.
488 fn color(a: Option<Color>, b: Option<Color>, t: f32) -> Option<Color> {
489 match (a, b) {
490 (Some(a), Some(b)) => Some(a.lerp(b, t)),
491 (a, b) => a.or(b),
492 }
493 }
494 /// A choice, not a quantity: the near side until halfway, the far side after.
495 fn swap<T>(a: Option<T>, b: Option<T>, t: f32) -> Option<T> {
496 if t < 0.5 {
497 a
498 } else {
499 b
500 }
501 }
502 TextStyle {
503 size: number(self.size, other.size, t),
504 weight: match (self.weight, other.weight) {
505 (Some(a), Some(b)) => Some(a.lerp(b, t)),
506 (a, b) => a.or(b),
507 },
508 italic: swap(self.italic, other.italic, t),
509 color: color(self.color, other.color, t),
510 decoration: swap(self.decoration, other.decoration, t),
511 decoration_color: color(self.decoration_color, other.decoration_color, t),
512 height: number(self.height, other.height, t),
513 family: swap(self.family, other.family, t),
514 letter_spacing: number(self.letter_spacing, other.letter_spacing, t),
515 }
516 }
517
518 /// Every question answered: the framework's own default wherever nothing in the chain
519 /// said anything, and **the reader's font size applied**.
520 ///
521 /// This is where the chain **stops**. Above it a style may leave a field open for a
522 /// subtree or a theme to answer; below it a shaper needs a number, and there is nobody
523 /// left to ask.
524 ///
525 /// Which is exactly why the user's *Font size* setting is applied here and nowhere
526 /// else. A phone's slider goes to 1.3 on Android and past 3 with iOS's larger
527 /// accessibility sizes, and an interface that ignores it is one a lot of people cannot
528 /// read. Scaling it at the one place a size becomes a number is what keeps the
529 /// **measurement and the paint agreeing**: text measured at one size and drawn at
530 /// another is a layout that is wrong everywhere at once, with nothing in the picture to
531 /// say which of the two numbers was the mistake.
532 ///
533 /// See [`with_text_scale`]. Outside any scope the scale is 1 and this is the identity.
534 /// Caps how far the **reader's font setting** may enlarge this style.
535 ///
536 /// The size is declared smaller in exact proportion, so that resolving it lands at
537 /// `max_scale` times the size asked for and no more. Below `max_scale` nothing changes.
538 ///
539 /// This is the reference's second answer to a reader who turns the system type up, and
540 /// it belongs to **chrome**. A component grows: its default height is a floor and the
541 /// content wins. A toolbar cannot — it would push the whole screen down — so the
542 /// reference keeps its bar at `kToolbarHeight` and clamps the title's scaler to 1.34
543 /// instead, "to keep the visual hierarchy the same even with larger font sizes".
544 ///
545 /// It reads the ambient scale, so it must be called where that scale is in force —
546 /// which for a widget means while it is being built, the same place it reads a theme.
547 #[must_use]
548 pub fn clamp_scale(mut self, max_scale: f32) -> Self {
549 let scale = text_scale();
550 if scale > max_scale && scale > 0.0 {
551 if let Some(size) = self.size {
552 self.size = Some(size * max_scale / scale);
553 }
554 }
555 self
556 }
557
558 /// Fills every unset field with its default, applying the ambient text scale to
559 /// the size on the way.
560 pub fn resolved(self) -> ResolvedTextStyle {
561 ResolvedTextStyle {
562 size: self.size.unwrap_or(DEFAULT_TEXT_SIZE) * text_scale(),
563 weight: self.weight.unwrap_or(FontWeight::Regular),
564 italic: self.italic.unwrap_or(false),
565 color: self.color,
566 decoration: self.decoration.unwrap_or(TextDecoration::NONE),
567 decoration_color: self.decoration_color,
568 height: self.height,
569 family: self.family,
570 letter_spacing: self.letter_spacing.unwrap_or(0.0),
571 }
572 }
573}
574
575/// The reader's font-size setting in force on this thread, or `1.0` outside any
576/// [`with_text_scale`].
577///
578/// Read once, by [`TextStyle::resolved`]. Nothing else should consult it: a second reader
579/// is a second chance to scale a size twice, or to scale one a sibling did not.
580pub fn text_scale() -> f32 {
581 TEXT_SCALE.with(|s| s.get())
582}
583
584/// Runs `f` with the reader's font-size setting in force, restoring whatever was there
585/// before — including while a panic unwinds, so one bad frame cannot leave a stale scale
586/// installed for every frame after it.
587///
588/// **Ambient rather than threaded**, and that is the decision worth stating. Passing a
589/// scaler down would mean every widget that measures a label remembering to apply it, and
590/// the one that forgot would draw text the layout never measured. There is no diagnostic
591/// for that — only a screen that is subtly wrong. Ambient makes forgetting impossible: the
592/// only place a size becomes a number already reads it.
593///
594/// The framework installs this around `view` from `MediaQuery::text_scaler`; an
595/// application does not normally call it. Scales at or below zero are ignored, a font of
596/// no size being a screen with no words on it.
597///
598/// **A closure is not always a long enough life.** A size becomes a number during layout
599/// and again during paint, which happen after the widgets have been built — see
600/// [`install_text_scale`], which the shell uses to cover a whole frame.
601pub fn with_text_scale<R>(scale: f32, f: impl FnOnce() -> R) -> R {
602 let guard = install_text_scale(scale);
603 let out = f();
604 drop(guard);
605 out
606}
607
608/// Installs the reader's font-size setting **until the returned guard is dropped**.
609///
610/// [`with_text_scale`] covers a closure, which is right for a subtree and wrong for a
611/// frame: a widget is *built* first, then measured, laid out and painted, and every one of
612/// those later steps resolves sizes too. Scoping only the build leaves the scale at 1 for
613/// the two steps that decide how big the text actually is — so the layout measures one
614/// size and the renderer draws another, which is the exact failure
615/// [`TextStyle::resolved`] exists to make impossible.
616///
617/// That is not a hypothetical either: the shell wrapped `view` alone, and the setting
618/// reached a device without changing a single pixel (milestone 407).
619///
620/// Scales at or below zero are ignored, as in [`with_text_scale`].
621#[must_use = "the scale is restored the moment the guard is dropped"]
622pub fn install_text_scale(scale: f32) -> TextScaleGuard {
623 let scale = if scale > 0.0 { scale } else { 1.0 };
624 TextScaleGuard(TEXT_SCALE.with(|s| s.replace(scale)))
625}
626
627/// Puts back the scale that was in force before [`install_text_scale`], when dropped —
628/// including while a panic unwinds, so one bad frame cannot leave a stale scale installed
629/// for every frame after it.
630pub struct TextScaleGuard(f32);
631
632impl Drop for TextScaleGuard {
633 fn drop(&mut self) {
634 TEXT_SCALE.with(|s| s.set(self.0));
635 }
636}
637
638thread_local! {
639 /// The reader's font-size setting on this thread. `Cell`, not `RefCell`: an `f32` is
640 /// `Copy` and every access is a whole get or a whole set.
641 static TEXT_SCALE: std::cell::Cell<f32> = const { std::cell::Cell::new(1.0) };
642}
643
644/// A **rich-text tree**: each node carries a fragment of text, *partial* style
645/// overrides — whatever is unspecified **inherits** from the parent, so a
646/// `.bold()` child keeps its parent's size — and children of its own.
647///
648/// ```
649/// # use frus_core::{TextSpan, TextStyle};
650/// let span = TextSpan::new("Hello ")
651/// .child(TextSpan::new("bold").bold())
652/// .child(TextSpan::new(" world"));
653/// let runs = span.flatten(TextStyle::new(20.0));
654/// assert_eq!(runs.len(), 3);
655/// assert_eq!(runs[1].1.size, 20.0); // the bold child inherits the size
656/// ```
657#[derive(Clone, Debug, Default, PartialEq)]
658pub struct TextSpan {
659 text: String,
660 overrides: TextStyle,
661 children: Vec<TextSpan>,
662}
663
664impl TextSpan {
665 /// A text fragment with no overrides — it inherits everything.
666 pub fn new(text: impl Into<String>) -> Self {
667 Self {
668 text: text.into(),
669 overrides: TextStyle::NONE,
670 children: Vec::new(),
671 }
672 }
673
674 /// Makes this subtree bold.
675 pub fn bold(mut self) -> Self {
676 self.overrides.weight = Some(FontWeight::Bold);
677 self
678 }
679
680 /// Sets this subtree's weight.
681 pub fn weight(mut self, weight: FontWeight) -> Self {
682 self.overrides.weight = Some(weight);
683 self
684 }
685
686 /// Makes this subtree italic.
687 pub fn italic(mut self) -> Self {
688 self.overrides.italic = Some(true);
689 self
690 }
691
692 /// Sets this subtree's size.
693 pub fn size(mut self, size: f32) -> Self {
694 self.overrides.size = Some(size);
695 self
696 }
697
698 /// Sets this subtree's colour.
699 pub fn color(mut self, color: Color) -> Self {
700 self.overrides.color = Some(color);
701 self
702 }
703
704 /// Underlines this subtree. It combines with the decoration already set on
705 /// this node, but not with the parent's.
706 pub fn underline(mut self) -> Self {
707 self.overrides = self.overrides.underline();
708 self
709 }
710
711 /// Strikes through this subtree.
712 pub fn strikethrough(mut self) -> Self {
713 self.overrides = self.overrides.strikethrough();
714 self
715 }
716
717 /// Sets this subtree's decoration lines.
718 pub fn decoration(mut self, decoration: TextDecoration) -> Self {
719 self.overrides.decoration = Some(decoration);
720 self
721 }
722
723 /// Sets this subtree's decoration colour.
724 pub fn decoration_color(mut self, color: Color) -> Self {
725 self.overrides.decoration_color = Some(color);
726 self
727 }
728
729 /// Applies a whole [`TextStyle`] as this subtree's override, **replacing** whatever
730 /// this span had said before.
731 ///
732 /// It used to force every typographic field to *answered*, because a `TextStyle` could
733 /// not say otherwise. It no longer has to: a style naming only a size now overrides
734 /// only the size, and the weight goes on being inherited from the parent span.
735 pub fn style(mut self, style: TextStyle) -> Self {
736 self.overrides = style;
737 self
738 }
739
740 /// Adds a child, rendered after this node's own text.
741 pub fn child(mut self, child: TextSpan) -> Self {
742 self.children.push(child);
743 self
744 }
745
746 /// Flattens the tree into **resolved runs** `(text, style)`, in reading order,
747 /// cascading the overrides down from `base`, the paragraph's default style.
748 /// Nodes with no text of their own produce no empty run.
749 pub fn flatten(&self, base: TextStyle) -> Vec<(String, ResolvedTextStyle)> {
750 let mut runs = Vec::new();
751 self.collect(base, &mut runs);
752 runs
753 }
754
755 /// Mixes into `hasher` everything that affects the tree's **measurement**:
756 /// text, sizes, weights and italics. Colours and **decorations** are excluded,
757 /// having no effect on geometry. This serves as the measurement fingerprint
758 /// (`measure_key`) without having to flatten the tree.
759 pub fn measure_hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
760 use std::hash::Hash;
761 self.text.hash(hasher);
762 self.overrides.size.map(f32::to_bits).hash(hasher);
763 self.overrides.weight.map(FontWeight::to_u16).hash(hasher);
764 self.overrides.italic.hash(hasher);
765 self.children.len().hash(hasher);
766 for child in &self.children {
767 child.measure_hash(hasher);
768 }
769 }
770
771 fn collect(&self, inherited: TextStyle, out: &mut Vec<(String, ResolvedTextStyle)>) {
772 let effective = inherited.merge(self.overrides);
773 if !self.text.is_empty() {
774 out.push((self.text.clone(), effective.resolved()));
775 }
776 // The children inherit the **unresolved** style, not the run's numbers. Handing
777 // them the resolved one would turn every framework default into an answer, and a
778 // child that wanted to inherit a weight nobody had named would find one waiting.
779 for child in &self.children {
780 child.collect(effective, out);
781 }
782 }
783}
784
785/// A text run **ready to render**: a fragment plus its typographic attributes and
786/// colour, all **resolved** — nothing left to inherit.
787#[derive(Clone, Debug, PartialEq)]
788pub struct TextRun {
789 /// The run's text.
790 pub text: String,
791 /// The font size, in logical pixels.
792 pub size: f32,
793 /// The font weight.
794 pub weight: FontWeight,
795 /// Whether the run is italic.
796 pub italic: bool,
797 /// The run's colour.
798 pub color: Color,
799 /// The run's decoration lines (no effect on measurement).
800 pub decoration: TextDecoration,
801 /// Decoration colour; `None` means the run's own colour.
802 pub decoration_color: Option<Color>,
803 /// The room added after every character, in logical pixels; `0` for none.
804 pub letter_spacing: f32,
805}
806
807#[cfg(test)]
808mod tests {
809 use super::*;
810
811 /// **Letter spacing cascades, travels, and is not grown by the reader's setting**
812 /// (milestone 623): a style that says nothing inherits it, two that say something
813 /// meet halfway, and a larger text scale grows the size and leaves the spacing as
814 /// it was written.
815 #[test]
816 fn letter_spacing_cascades_travels_and_stays_written() {
817 let spaced = TextStyle::new(14.0).letter_spacing(0.5);
818 assert_eq!(spaced.merge(TextStyle::new(20.0)).letter_spacing, Some(0.5));
819 assert_eq!(
820 spaced
821 .merge(TextStyle::NONE.letter_spacing(-1.0))
822 .letter_spacing,
823 Some(-1.0)
824 );
825 let half = spaced.lerp(TextStyle::new(14.0).letter_spacing(1.5), 0.5);
826 assert_eq!(half.letter_spacing, Some(1.0));
827 assert_eq!(
828 TextStyle::new(14.0).resolved().letter_spacing,
829 0.0,
830 "none by default"
831 );
832 let scaled = with_text_scale(2.0, || spaced.resolved());
833 assert_eq!(scaled.size, 28.0, "the size grows");
834 assert_eq!(scaled.letter_spacing, 0.5, "the spacing does not");
835 }
836
837 #[test]
838 fn weights_map_to_opentype() {
839 assert_eq!(FontWeight::Regular.to_u16(), 400);
840 assert_eq!(FontWeight::Bold.to_u16(), 700);
841 }
842
843 #[test]
844 fn builders_compose() {
845 let s = TextStyle::new(20.0).weight(FontWeight::Bold).italic();
846 assert_eq!(s.size, Some(20.0));
847 assert_eq!(s.weight, Some(FontWeight::Bold));
848 assert_eq!(s.italic, Some(true));
849 assert_eq!(s.color, None);
850 }
851
852 /// **A style can name one thing and leave the rest open**, which is the whole reason
853 /// the fields are optional.
854 ///
855 /// `TextStyle::new(20.0)` used to answer a size, a weight *and* a slant, because the
856 /// type had nowhere to put "unset" — so *size 20, inherit the weight* was not
857 /// expressible, however the caller wrote it. The reference writes it
858 /// `TextStyle(fontSize: 20)`, and now so do we.
859 #[test]
860 fn a_style_may_name_one_thing_and_leave_the_rest_open() {
861 let only_size = TextStyle::new(20.0);
862 assert_eq!(only_size.size, Some(20.0));
863 assert_eq!(only_size.weight, None, "and says nothing about the weight");
864
865 let inherited = TextStyle::NONE.weight(FontWeight::Bold).merge(only_size);
866 assert_eq!(inherited.size, Some(20.0), "the size it named");
867 assert_eq!(
868 inherited.weight,
869 Some(FontWeight::Bold),
870 "the weight it did not"
871 );
872 }
873
874 /// The chain **stops** somewhere: a shaper needs a number and there is nobody left to
875 /// ask. Everything unanswered lands on the framework's own default.
876 #[test]
877 fn a_style_that_says_nothing_resolves_to_the_frameworks_own() {
878 let r = TextStyle::NONE.resolved();
879 assert_eq!(r.size, DEFAULT_TEXT_SIZE);
880 assert_eq!(r.weight, FontWeight::Regular);
881 assert!(!r.italic);
882 assert_eq!(r.decoration, TextDecoration::NONE);
883 // The colour is the one thing left open, its last word belonging to a theme this
884 // type cannot see.
885 assert_eq!(r.color, None);
886 }
887
888 /// The reader's font size lands on **the one place a size becomes a number**.
889 #[test]
890 fn the_readers_font_size_reaches_a_resolved_style() {
891 assert_eq!(
892 TextStyle::new(10.0).resolved().size,
893 10.0,
894 "unscaled by default"
895 );
896 with_text_scale(1.5, || {
897 assert_eq!(TextStyle::new(10.0).resolved().size, 15.0);
898 // A style that named no size scales the framework's own, not nothing.
899 assert_eq!(
900 TextStyle::NONE.resolved().size,
901 DEFAULT_TEXT_SIZE * 1.5,
902 "the default is a size like any other"
903 );
904 });
905 assert_eq!(
906 TextStyle::new(10.0).resolved().size,
907 10.0,
908 "and the scope put it back"
909 );
910 }
911
912 /// Nothing but the size moves. A reader asking for larger text has not asked for a
913 /// different typeface, and scaling a weight or a slant is not a thing to do.
914 #[test]
915 fn only_the_size_is_scaled() {
916 with_text_scale(2.0, || {
917 let r = TextStyle::new(10.0)
918 .weight(FontWeight::Bold)
919 .italic()
920 .resolved();
921 assert_eq!(r.size, 20.0);
922 assert_eq!(r.weight, FontWeight::Bold);
923 assert!(r.italic);
924 });
925 }
926
927 /// A scale of zero is **ignored**, not obeyed. A font of no size is a screen with no
928 /// words on it, and a platform reporting one is a platform to disbelieve rather than a
929 /// user to accommodate.
930 #[test]
931 fn a_scale_of_nothing_is_disbelieved() {
932 with_text_scale(0.0, || {
933 assert_eq!(TextStyle::new(10.0).resolved().size, 10.0);
934 });
935 with_text_scale(-2.0, || {
936 assert_eq!(TextStyle::new(10.0).resolved().size, 10.0);
937 });
938 }
939
940 #[test]
941 fn merge_overrides_type_but_inherits_missing_colour() {
942 let base = TextStyle::new(16.0).color(Color::WHITE);
943 // `over` changes size and weight but specifies no colour.
944 let over = TextStyle::new(24.0).weight(FontWeight::Bold);
945 let merged = base.merge(over);
946 assert_eq!(merged.size, Some(24.0));
947 assert_eq!(merged.weight, Some(FontWeight::Bold));
948 assert_eq!(merged.color, Some(Color::WHITE), "colour inherited");
949
950 // When `over` does specify a colour, it wins.
951 let over2 = TextStyle::new(24.0).color(Color::BLACK);
952 assert_eq!(base.merge(over2).color, Some(Color::BLACK));
953 }
954
955 #[test]
956 fn span_children_inherit_unspecified_attributes() {
957 // "Hello **bold** _red italic_": the bold part inherits size and colour, the
958 // red italic inherits size and weight.
959 let span = TextSpan::new("Hello ")
960 .child(TextSpan::new("bold").bold())
961 .child(
962 TextSpan::new("red italic")
963 .italic()
964 .color(Color::rgb(1.0, 0.0, 0.0)),
965 );
966 let base = TextStyle::new(20.0).color(Color::WHITE);
967 let runs = span.flatten(base);
968
969 assert_eq!(runs.len(), 3);
970 let (t0, s0) = &runs[0];
971 assert_eq!(
972 (t0.as_str(), s0.size, s0.weight),
973 ("Hello ", 20.0, FontWeight::Regular)
974 );
975 let (t1, s1) = &runs[1];
976 assert_eq!((t1.as_str(), s1.weight), ("bold", FontWeight::Bold));
977 assert_eq!(s1.size, 20.0, "bold inherits the size");
978 assert_eq!(s1.color, Some(Color::WHITE), "bold inherits the colour");
979 let (t2, s2) = &runs[2];
980 assert_eq!(t2, "red italic");
981 assert!(s2.italic);
982 assert_eq!(s2.weight, FontWeight::Regular, "italic inherits the weight");
983 assert_eq!(s2.color, Some(Color::rgb(1.0, 0.0, 0.0)));
984 }
985
986 #[test]
987 fn nested_spans_cascade_depth_first() {
988 // A bold subtree in which a grandchild puts the size back to 12.
989 let span = TextSpan::new("a").child(
990 TextSpan::new("b")
991 .bold()
992 .child(TextSpan::new("c").size(12.0)),
993 );
994 let runs = span.flatten(TextStyle::new(16.0));
995 assert_eq!(runs.len(), 3);
996 assert_eq!(runs[2].0, "c");
997 assert_eq!(runs[2].1.size, 12.0);
998 assert_eq!(
999 runs[2].1.weight,
1000 FontWeight::Bold,
1001 "inherits bold from the parent"
1002 );
1003 }
1004
1005 #[test]
1006 fn decorations_combine_and_cascade() {
1007 // Combining: underline plus strikethrough on the same style.
1008 let s = TextStyle::new(16.0).underline().strikethrough().resolved();
1009 assert!(s.decoration.underline && s.decoration.strikethrough);
1010 assert!(!s.decoration.overline);
1011 assert!(!TextDecoration::UNDERLINE.is_none());
1012 assert!(TextDecoration::NONE.is_none());
1013
1014 // Cascade: the child inherits the parent's decoration when it specifies
1015 // nothing, and an explicit `decoration(NONE)` cancels it (Some(NONE) is not
1016 // the same as absent).
1017 let span = TextSpan::new("a")
1018 .underline()
1019 .decoration_color(Color::rgb(1.0, 0.0, 0.0))
1020 .child(TextSpan::new("b"))
1021 .child(TextSpan::new("c").decoration(TextDecoration::NONE));
1022 let runs = span.flatten(TextStyle::new(16.0));
1023 assert!(runs[0].1.decoration.underline);
1024 assert!(
1025 runs[1].1.decoration.underline,
1026 "the child inherits the underline"
1027 );
1028 assert_eq!(
1029 runs[1].1.decoration_color,
1030 Some(Color::rgb(1.0, 0.0, 0.0)),
1031 "the decoration colour cascades"
1032 );
1033 assert!(runs[2].1.decoration.is_none(), "explicitly cancelled");
1034
1035 // merge: decoration is a typographic attribute, so `over`'s wins, while its
1036 // colour inherits the way the text colour does.
1037 let base = TextStyle::new(16.0)
1038 .underline()
1039 .decoration_color(Color::BLACK);
1040 let merged = base.merge(TextStyle::new(20.0).strikethrough()).resolved();
1041 assert!(merged.decoration.strikethrough && !merged.decoration.underline);
1042 assert_eq!(merged.decoration_color, Some(Color::BLACK));
1043
1044 // And a style that names **no** decoration leaves the parent's alone, where it
1045 // used to erase it: `over.decoration` was a value the type could not withhold.
1046 let quiet = base.merge(TextStyle::new(20.0)).resolved();
1047 assert!(quiet.decoration.underline, "the underline survived");
1048 }
1049
1050 #[test]
1051 fn empty_nodes_produce_no_run() {
1052 // A "group" node with no text of its own only exists to style its children.
1053 let span = TextSpan::new("").bold().child(TextSpan::new("x"));
1054 let runs = span.flatten(TextStyle::new(16.0));
1055 assert_eq!(runs.len(), 1);
1056 assert_eq!(runs[0].1.weight, FontWeight::Bold);
1057 }
1058
1059 /// A style that says nothing about its line height gets the framework's default, and
1060 /// one that names a ratio gets that ratio **of its own size**.
1061 #[test]
1062 fn a_line_height_is_a_ratio_of_the_size_that_asked_for_it() {
1063 assert_eq!(TextStyle::new(20.0).resolved().line_height(), 24.0);
1064 let open = TextStyle {
1065 height: Some(1.5),
1066 ..TextStyle::new(20.0)
1067 };
1068 assert_eq!(open.resolved().line_height(), 30.0);
1069 let packed = TextStyle {
1070 height: Some(1.0),
1071 ..TextStyle::new(20.0)
1072 };
1073 assert_eq!(packed.resolved().line_height(), 20.0);
1074 }
1075
1076 /// **The leading grows with the letters.** A ratio rather than a length is what makes
1077 /// a paragraph keep its rhythm when the reader turns the type up: a `height` of 1.5 is
1078 /// 30 px at a size of 20 and 60 px when that 20 has been doubled for a reader who
1079 /// asked for larger text. A length would have stayed at 30 and closed the paragraph up
1080 /// exactly when it needed opening.
1081 #[test]
1082 fn the_leading_grows_with_the_reader() {
1083 let style = TextStyle {
1084 height: Some(1.5),
1085 ..TextStyle::new(20.0)
1086 };
1087 assert_eq!(style.resolved().line_height(), 30.0);
1088 with_text_scale(2.0, || {
1089 let resolved = style.resolved();
1090 assert_eq!(resolved.size, 40.0);
1091 assert_eq!(resolved.line_height(), 60.0);
1092 });
1093 }
1094
1095 /// It inherits like every other field, and a nearer style still wins.
1096 #[test]
1097 fn a_line_height_is_inherited_and_overridable() {
1098 let handed_down = TextStyle {
1099 height: Some(1.8),
1100 ..TextStyle::NONE
1101 };
1102 let asks_only_a_size = TextStyle::new(10.0);
1103 assert_eq!(
1104 handed_down.merge(asks_only_a_size).resolved().line_height(),
1105 18.0,
1106 "the size is the near style's and the height the inherited one"
1107 );
1108 let asks_for_both = TextStyle {
1109 height: Some(1.0),
1110 ..TextStyle::new(10.0)
1111 };
1112 assert_eq!(
1113 handed_down.merge(asks_for_both).resolved().line_height(),
1114 10.0
1115 );
1116 }
1117
1118 // --- Interpolation (milestone 498) ---
1119
1120 /// **A size travels.** The plain case, and the only field of a style for which
1121 /// "halfway between" is an obvious answer.
1122 #[test]
1123 fn a_size_travels() {
1124 let a = TextStyle::NONE.size(12.0);
1125 let b = TextStyle::NONE.size(24.0);
1126 assert_eq!(a.lerp(b, 0.0).size, Some(12.0));
1127 assert_eq!(a.lerp(b, 0.5).size, Some(18.0));
1128 assert_eq!(a.lerp(b, 1.0).size, Some(24.0));
1129 }
1130
1131 /// **A field only one end states does not travel** — it holds the one value it has,
1132 /// from the first frame to the last.
1133 ///
1134 /// `None` is not nought and not transparent: it means *this style does not say*, and
1135 /// the question passes further up the cascade. There is no number between "24 pixels"
1136 /// and "ask somebody else", and interpolating towards whatever the theme happens to
1137 /// answer would make the movement depend on the theme, which neither end asked for.
1138 #[test]
1139 fn a_field_only_one_end_states_holds_still() {
1140 let unset = TextStyle::NONE;
1141 let stated = TextStyle::NONE.size(24.0);
1142 for t in [0.0, 0.25, 0.5, 0.75, 1.0] {
1143 assert_eq!(unset.lerp(stated, t).size, Some(24.0), "arriving, at {t}");
1144 assert_eq!(stated.lerp(unset, t).size, Some(24.0), "leaving, at {t}");
1145 }
1146 assert_eq!(
1147 unset.lerp(unset, 0.5).size,
1148 None,
1149 "and unset at both ends stays so"
1150 );
1151 }
1152
1153 /// **An unset colour does not fade out**, which is where this diverges from the
1154 /// reference on purpose.
1155 ///
1156 /// There, a null colour means *nothing was painted*, so fading towards it is right.
1157 /// Here it means *the widget resolves it against the theme* — `on_surface`, a
1158 /// perfectly visible colour — so fading it out would make a run of words disappear
1159 /// halfway through a movement that was only ever about its size.
1160 #[test]
1161 fn an_unset_colour_does_not_fade_out() {
1162 let red = Color::rgb(1.0, 0.0, 0.0);
1163 let stated = TextStyle::NONE.color(red);
1164 let mid = TextStyle::NONE.lerp(stated, 0.5).color.expect("a colour");
1165 assert_eq!(mid, red, "held whole, not at half alpha");
1166 assert_eq!(TextStyle::NONE.lerp(TextStyle::NONE, 0.5).color, None);
1167 }
1168
1169 /// **A colour stated at both ends travels**, on the same path an animated background
1170 /// takes, so two things changing colour together stay together.
1171 #[test]
1172 fn a_colour_stated_at_both_ends_travels() {
1173 let a = TextStyle::NONE.color(Color::rgb(0.0, 0.0, 0.0));
1174 let b = TextStyle::NONE.color(Color::rgb(1.0, 1.0, 1.0));
1175 let mid = a.lerp(b, 0.5).color.expect("a colour");
1176 assert_eq!(
1177 mid,
1178 Color::rgb(0.0, 0.0, 0.0).lerp(Color::rgb(1.0, 1.0, 1.0), 0.5)
1179 );
1180 }
1181
1182 /// **Faces and lines swap at the halfway point.** There is no half-italic face and no
1183 /// two-thirds of an underline, so the change happens once, in the middle.
1184 #[test]
1185 fn faces_and_lines_swap_in_the_middle() {
1186 let upright = TextStyle {
1187 italic: Some(false),
1188 ..TextStyle::NONE.decoration(TextDecoration::NONE)
1189 };
1190 let slanted = TextStyle::NONE
1191 .italic()
1192 .decoration(TextDecoration::UNDERLINE);
1193 assert_eq!(upright.lerp(slanted, 0.49).italic, Some(false));
1194 assert_eq!(upright.lerp(slanted, 0.51).italic, Some(true));
1195 assert_eq!(
1196 upright.lerp(slanted, 0.49).decoration,
1197 Some(TextDecoration::NONE)
1198 );
1199 assert_eq!(
1200 upright.lerp(slanted, 0.51).decoration,
1201 Some(TextDecoration::UNDERLINE)
1202 );
1203 }
1204
1205 /// **A weight steps through the faces it has.** Interpolating the number would ask
1206 /// for a 437 no font can draw, so what moves is the choice of face: regular to bold
1207 /// passes through semi-bold in the middle rather than gliding.
1208 #[test]
1209 fn a_weight_steps_through_the_faces() {
1210 use FontWeight::{Bold, Medium, Regular, SemiBold};
1211 assert_eq!(Regular.lerp(Bold, 0.0), Regular);
1212 assert_eq!(Regular.lerp(Bold, 0.5), SemiBold, "550 is nearest 600");
1213 assert_eq!(Regular.lerp(Bold, 1.0), Bold);
1214 assert_eq!(Regular.lerp(Bold, 0.2), Medium, "460 is nearest 500");
1215 assert_eq!(Bold.lerp(Regular, 0.5), SemiBold, "and the same going back");
1216 }
1217}