waterui-text 0.4.0

Text and typography components for WaterUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
use core::fmt;
use core::{fmt::Display, mem::take, ops::Add};

use crate::{
    font::{Font, FontWeight},
    text,
};
use alloc::{string::String, vec::Vec};
use core::ops::AddAssign;
use nami::impl_constant;
#[cfg(feature = "markdown")]
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag};
use waterui_core::{Str, View};
use waterui_graphics::color::{Color, SurfaceVariantColor};

/// A set of text attributes for rich text formatting.
#[derive(Debug, Clone, Default)]
pub struct Style {
    /// The font to use.
    pub font: Font,
    /// The foreground (text) color.
    pub foreground: Option<Color>,
    /// The background color.
    pub background: Option<Color>,
    /// Whether the text is italic.
    pub italic: bool,
    /// Whether the text has an underline.
    pub underline: bool,
    /// Whether the text has a strikethrough.
    pub strikethrough: bool,
}

impl Style {
    /// Creates a new default `Style`.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the font.
    #[must_use]
    pub fn font(mut self, font: impl Into<Font>) -> Self {
        self.font = font.into();
        self
    }

    /// Sets the text color.
    #[must_use]
    pub fn foreground(mut self, color: impl Into<Color>) -> Self {
        self.foreground = Some(color.into());
        self
    }

    /// Sets the background color.
    #[must_use]
    pub fn background(mut self, color: impl Into<Color>) -> Self {
        self.background = Some(color.into());
        self
    }

    /// Sets the font weight.
    #[must_use]
    pub fn weight(mut self, weight: FontWeight) -> Self {
        self.font = self.font.weight(weight);
        self
    }

    /// Sets the bold style.
    /// Equal to calling `self.weight(FontWeight::Bold)`.
    #[must_use]
    pub fn bold(mut self) -> Self {
        self.font = self.font.bold();
        self
    }

    /// Sets the font size in points.
    #[must_use]
    pub fn size(mut self, size: f32) -> Self {
        self.font = self.font.size(size);
        self
    }

    /// Sets the italic style.
    #[must_use]
    pub const fn italic(mut self) -> Self {
        self.italic = true;
        self
    }

    /// Disables the italic style.
    #[must_use]
    pub const fn not_italic(mut self) -> Self {
        self.italic = false;
        self
    }

    /// Sets the underline style.
    #[must_use]
    pub const fn underline(mut self) -> Self {
        self.underline = true;
        self
    }

    /// Disables the underline style.
    #[must_use]
    pub const fn not_underline(mut self) -> Self {
        self.underline = false;
        self
    }

    /// Sets the strikethrough style.
    #[must_use]
    pub const fn strikethrough(mut self) -> Self {
        self.strikethrough = true;
        self
    }

    /// Disables the strikethrough style.
    #[must_use]
    pub const fn not_strikethrough(mut self) -> Self {
        self.strikethrough = false;
        self
    }

    /// Returns `true` if this style has no text decorations or colors applied.
    ///
    /// Note: font customizations are not currently considered here.
    #[must_use]
    pub const fn is_plain(&self) -> bool {
        !self.italic
            && !self.underline
            && !self.strikethrough
            && self.foreground.is_none()
            && self.background.is_none()
    }
}

/// A string with associated text attributes for rich text formatting.
#[derive(Debug, Clone, Default)]
pub struct StyledStr {
    chunks: Vec<(Str, Style)>,
}

impl StyledStr {
    /// Creates a new empty `StyledStr`.
    #[must_use]
    pub const fn empty() -> Self {
        Self { chunks: Vec::new() }
    }

    /// Creates a styled string from Markdown inline semantics.
    ///
    /// Supported features include headings, emphasis, strong text,
    /// strikethrough, and inline code styling.
    #[cfg(feature = "markdown")]
    #[must_use]
    pub fn from_markdown(markdown: &str) -> Self {
        let options =
            Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES | Options::ENABLE_STRIKETHROUGH;
        let parser = Parser::new_ext(markdown, options);

        let mut builder = MarkdownInlineBuilder::new();
        let mut pending_block_break = false;

        for event in parser {
            match event {
                Event::Start(tag) => match tag {
                    Tag::Heading { level, .. } => {
                        if pending_block_break || !builder.is_empty() {
                            builder.push_text("\n\n");
                        }
                        pending_block_break = false;
                        builder.enter_with(move |_| heading_style(level));
                    }
                    Tag::Paragraph => {
                        if pending_block_break || !builder.is_empty() {
                            builder.push_text("\n\n");
                        }
                        pending_block_break = false;
                    }
                    Tag::Emphasis => builder.enter_emphasis(),
                    Tag::Strong => builder.enter_strong(),
                    Tag::Strikethrough => builder.enter_strikethrough(),
                    Tag::CodeBlock(kind) => {
                        if pending_block_break || !builder.is_empty() {
                            builder.push_text("\n\n");
                        }
                        pending_block_break = false;
                        if let CodeBlockKind::Fenced(info) = kind
                            && !info.is_empty()
                        {
                            builder.push_text(info.as_ref());
                            builder.push_text(":\n");
                        }
                    }
                    Tag::List(_) | Tag::Item => {
                        if pending_block_break || !builder.is_empty() {
                            builder.push_text("\n");
                        }
                        pending_block_break = false;
                    }
                    _ => {}
                },
                Event::End(tag) => match tag {
                    pulldown_cmark::TagEnd::Heading(_) => {
                        builder.exit();
                        pending_block_break = true;
                    }
                    pulldown_cmark::TagEnd::Paragraph
                    | pulldown_cmark::TagEnd::CodeBlock
                    | pulldown_cmark::TagEnd::List(_) => {
                        pending_block_break = true;
                    }
                    pulldown_cmark::TagEnd::Emphasis
                    | pulldown_cmark::TagEnd::Strong
                    | pulldown_cmark::TagEnd::Strikethrough => {
                        builder.exit();
                    }
                    _ => {}
                },
                Event::Text(text)
                | Event::Html(text)
                | Event::FootnoteReference(text)
                | Event::InlineMath(text)
                | Event::DisplayMath(text)
                | Event::InlineHtml(text) => {
                    if pending_block_break && !builder.is_empty() {
                        builder.push_text("\n\n");
                        pending_block_break = false;
                    }
                    builder.push_text(text.as_ref());
                }
                Event::Code(text) => {
                    if pending_block_break && !builder.is_empty() {
                        builder.push_text("\n\n");
                        pending_block_break = false;
                    }
                    builder.push_inline_code(text.as_ref());
                }
                Event::SoftBreak => builder.push_soft_break(),
                Event::HardBreak => builder.push_hard_break(),
                Event::Rule => {
                    builder.push_text("\n\n——\n\n");
                    pending_block_break = false;
                }
                Event::TaskListMarker(checked) => {
                    if pending_block_break && !builder.is_empty() {
                        builder.push_text("\n");
                        pending_block_break = false;
                    }
                    builder.push_text(if checked { "[x] " } else { "[ ] " });
                }
            }
        }

        builder.finish()
    }

    /// Creates a plain attributed string with a single unstyled chunk.
    #[must_use]
    pub fn plain(text: impl Into<Str>) -> Self {
        let mut s = Self::empty();
        s.push(text.into(), Style::default());
        s
    }

    /// Adds a new text chunk with the specified style.
    pub fn push(&mut self, text: impl Into<Str>, style: Style) {
        let text = text.into();
        self.chunks.push((text, style));
    }

    /// Appends text to the last chunk, or creates a new chunk if empty.
    pub fn push_str(&mut self, text: impl Into<Str>) {
        let text = text.into();
        if let Some(last) = self.chunks.last_mut() {
            let (last_text, _) = last;
            last_text.add_assign(text);
        } else {
            self.chunks.push((text, Style::default()));
        }
    }

    /// Returns the total length of the attributed string.
    #[must_use]
    pub fn len(&self) -> usize {
        self.chunks.iter().map(|(text, _)| text.len()).sum()
    }

    /// Checks if the attributed string is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.chunks.is_empty()
    }

    /// The text as a reader should hear it, without bidi formatting controls.
    ///
    /// Interpolated values are wrapped in directional isolates so that a
    /// right-to-left value cannot reorder the text around it. That is a
    /// rendering concern: the characters are invisible, but they are still
    /// characters, and an accessibility label carrying them is neither what a
    /// screen reader should announce nor what a caller comparing text expects.
    ///
    /// Use this wherever text becomes semantics; use [`Self::to_plain`] where
    /// the exact content matters, such as the value of a text field the user
    /// typed into.
    #[must_use]
    pub fn to_semantic(&self) -> Str {
        let plain = self.to_plain();
        if plain.chars().any(is_bidi_control) {
            return Str::from(
                plain
                    .chars()
                    .filter(|character| !is_bidi_control(*character))
                    .collect::<String>(),
            );
        }
        plain
    }

    /// Converts the attributed string into its plain representation.
    #[must_use]
    pub fn to_plain(&self) -> Str {
        if self.chunks.len() == 1 {
            return self.chunks[0].0.clone();
        }

        let mut result = String::new();
        for (text, _) in &self.chunks {
            result.push_str(text);
        }
        result.into()
    }

    /// Returns `true` if all chunks are plain text style.
    #[must_use]
    pub fn is_plain(&self) -> bool {
        self.chunks.iter().all(|(_, style)| style.is_plain())
    }

    /// Consumes the attributed string and returns its constituent chunks.
    #[must_use]
    pub fn into_chunks(self) -> Vec<(Str, Style)> {
        self.chunks
    }

    /// Returns the constituent chunks without consuming the styled text.
    #[must_use]
    pub fn chunks(&self) -> &[(Str, Style)] {
        &self.chunks
    }

    /// Sets the style for all text in this styled text.
    #[must_use]
    pub fn set_style(self, style: &Style) -> Self {
        self.apply_style(|s| *s = style.clone())
    }

    fn apply_style(mut self, f: impl Fn(&mut Style)) -> Self {
        if self.chunks.is_empty() {
            return self;
        }
        let old_chunks = core::mem::take(&mut self.chunks);
        for (text, mut style) in old_chunks {
            f(&mut style);
            self.push(text, style);
        }
        self
    }

    /// Sets the font size for all chunks.
    #[must_use]
    pub fn size(self, size: f32) -> Self {
        self.apply_style(|s| *s = take(s).size(size))
    }

    /// Sets the font for all chunks.
    #[must_use]
    pub fn font(self, font: impl Into<Font>) -> Self {
        let font = font.into();
        self.apply_style(|s| s.font = font.clone())
    }

    /// Sets the foreground color for all chunks.
    #[must_use]
    pub fn foreground(self, color: impl Into<Color>) -> Self {
        let color = color.into();
        self.apply_style(|s| s.foreground = Some(color.clone()))
    }

    /// Sets the background color for all chunks.
    #[must_use]
    pub fn background_color(self, color: impl Into<Color>) -> Self {
        let color = color.into();
        self.apply_style(|s| s.background = Some(color.clone()))
    }

    /// Sets the font weight for all chunks.
    #[must_use]
    pub fn weight(self, weight: FontWeight) -> Self {
        self.apply_style(|s| {
            *s = take(s).weight(weight);
        })
    }

    /// Sets the font to bold for all chunks.
    #[must_use]
    pub fn bold(self) -> Self {
        self.weight(FontWeight::Bold)
    }

    /// Sets the italic style for all chunks.
    #[must_use]
    pub fn italic(self, italic: bool) -> Self {
        self.apply_style(|s| s.italic = italic)
    }

    /// Sets the underline style for all chunks.
    #[must_use]
    pub fn underline(self, underline: bool) -> Self {
        self.apply_style(|s| s.underline = underline)
    }

    /// Sets the strikethrough style for all chunks.
    #[must_use]
    pub fn strikethrough(self, strikethrough: bool) -> Self {
        self.apply_style(|s| s.strikethrough = strikethrough)
    }
}

/// Utility builder that incrementally constructs a [`StyledStr`] from Markdown
/// events. The builder keeps track of the active style stack and merges
/// contiguous text runs that share the same styling.
#[derive(Debug, Clone)]
pub struct MarkdownInlineBuilder {
    base_style: Style,
    stack: smallvec::SmallVec<[Style; 4]>,
    buffer: String,
    result: StyledStr,
}

impl Default for MarkdownInlineBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl MarkdownInlineBuilder {
    /// Creates a new builder using the default style.
    #[must_use]
    pub fn new() -> Self {
        Self::with_base_style(Style::default())
    }

    /// Creates a new builder with a custom base style.
    #[must_use]
    pub fn with_base_style(style: Style) -> Self {
        Self {
            base_style: style.clone(),
            stack: smallvec::smallvec![style],
            buffer: String::new(),
            result: StyledStr::empty(),
        }
    }

    fn current_style(&self) -> Style {
        self.stack.last().cloned().unwrap_or_else(Style::default)
    }

    fn flush(&mut self) {
        if self.buffer.is_empty() {
            return;
        }

        let text = take(&mut self.buffer);
        self.result.push(text, self.current_style());
    }

    /// Appends raw text to the builder.
    pub fn push_text(&mut self, text: &str) {
        if !text.is_empty() {
            self.buffer.push_str(text);
        }
    }

    /// Appends a soft break (space) to the builder.
    pub fn push_soft_break(&mut self) {
        self.buffer.push(' ');
    }

    /// Appends a hard break (newline) to the builder.
    pub fn push_hard_break(&mut self) {
        self.buffer.push('\n');
    }

    /// Starts a new styled span using the closure to derive the child style.
    pub fn enter_with(&mut self, f: impl FnOnce(Style) -> Style) {
        self.flush();
        let style = self.current_style();
        self.stack.push(f(style));
    }

    /// Exits the most recently entered styled span.
    pub fn exit(&mut self) {
        self.flush();
        if self.stack.len() > 1 {
            self.stack.pop();
        }
    }

    /// Enters an italic styled span.
    pub fn enter_emphasis(&mut self) {
        self.enter_with(Style::italic);
    }

    /// Enters a bold styled span.
    pub fn enter_strong(&mut self) {
        self.enter_with(Style::bold);
    }

    /// Enters a strikethrough styled span.
    pub fn enter_strikethrough(&mut self) {
        self.enter_with(Style::strikethrough);
    }

    /// Enters an inline code styled span.
    pub fn enter_code(&mut self) {
        self.enter_with(inline_code_style);
    }

    /// Pushes inline code text with code styling applied.
    pub fn push_inline_code(&mut self, text: &str) {
        self.enter_code();
        self.push_text(text);
        self.exit();
    }

    /// Returns `true` if the builder has emitted no content yet.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.result.is_empty() && self.buffer.is_empty()
    }

    /// Returns the base style used by the builder.
    #[must_use]
    pub fn base_style(&self) -> Style {
        self.base_style.clone()
    }

    /// Takes the currently buffered content, resetting the builder to the base
    /// style. If no content has been emitted, `None` is returned.
    #[must_use]
    pub fn take(&mut self) -> Option<StyledStr> {
        self.flush();

        if self.result.is_empty() {
            return None;
        }

        let mut output = StyledStr::empty();
        core::mem::swap(&mut output, &mut self.result);
        self.stack.truncate(1);
        if let Some(first) = self.stack.first_mut() {
            *first = self.base_style.clone();
        }

        Some(output)
    }

    /// Consumes the builder and returns the final `StyledStr`.
    #[must_use]
    pub fn finish(mut self) -> StyledStr {
        self.flush();
        self.result
    }
}

/// Returns the default style applied to Markdown headings.
#[cfg(feature = "markdown")]
#[must_use]
pub fn heading_style(level: HeadingLevel) -> Style {
    use crate::font::{Body, Caption, Footnote, Headline, Subheadline, Title};

    let font: Font = match level {
        HeadingLevel::H1 => Headline.into(),
        HeadingLevel::H2 => Title.into(),
        HeadingLevel::H3 => Subheadline.into(),
        HeadingLevel::H4 => Body.into(),
        HeadingLevel::H5 => Caption.into(),
        HeadingLevel::H6 => Footnote.into(),
    };

    Style::default().font(font).bold()
}

fn inline_code_style(mut style: Style) -> Style {
    style.font = style.font.clone().family("monospace");
    style.background = Some(Color::new(SurfaceVariantColor));
    style
}

impl View for StyledStr {
    fn body(self, _env: &waterui_core::Environment) -> impl waterui_core::View {
        text(self)
    }
}

impl Add for StyledStr {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        for (text, style) in rhs.chunks {
            self.push(text, style);
        }
        self
    }
}

impl Add<&'static str> for StyledStr {
    type Output = Self;

    fn add(mut self, rhs: &'static str) -> Self::Output {
        self.push(rhs, Style::default());
        self
    }
}

impl Extend<(Str, Style)> for StyledStr {
    fn extend<T: IntoIterator<Item = (Str, Style)>>(&mut self, iter: T) {
        for (text, style) in iter {
            self.push(text, style);
        }
    }
}

impl From<Str> for StyledStr {
    fn from(value: Str) -> Self {
        Self::plain(value)
    }
}

impl From<&'static str> for StyledStr {
    fn from(value: &'static str) -> Self {
        Self::plain(value)
    }
}

impl From<String> for StyledStr {
    fn from(value: String) -> Self {
        Self::plain(value)
    }
}

impl Display for StyledStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_plain())
    }
}

impl_constant!(Style, StyledStr);

/// Whether `character` only tells the bidi algorithm what to do.
///
/// Covers the isolates and embeddings — the marks a formatter inserts around
/// interpolated values — and not the text itself.
const fn is_bidi_control(character: char) -> bool {
    matches!(character, '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}')
}

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

    #[cfg(feature = "markdown")]
    #[test]
    fn parses_emphasis_markdown() {
        let styled = StyledStr::from_markdown("Hello *world*!");
        let chunks = styled.into_chunks();
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0].0.as_str(), "Hello ");
        assert_eq!(chunks[1].0.as_str(), "world");
        assert!(chunks[1].1.italic);
        assert_eq!(chunks[2].0.as_str(), "!");
    }

    #[cfg(feature = "markdown")]
    #[test]
    fn parses_heading_markdown() {
        let styled = StyledStr::from_markdown("# Title");
        let chunks = styled.into_chunks();
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].0.as_str(), "Title");
    }

    #[cfg(feature = "markdown")]
    #[test]
    fn parses_strikethrough_markdown() {
        let styled = StyledStr::from_markdown("Normal ~~removed~~ text");
        let chunks = styled.into_chunks();
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[1].0.as_str(), "removed");
        assert!(chunks[1].1.strikethrough);
    }

    #[cfg(feature = "markdown")]
    #[test]
    fn parses_inline_code_markdown() {
        let styled = StyledStr::from_markdown("Run `cargo test` now");
        let chunks = styled.into_chunks();
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[1].0.as_str(), "cargo test");
        assert!(chunks[1].1.background.is_some());
    }

    #[test]
    fn plain_detection_works() {
        assert!(StyledStr::plain("abc").is_plain());
        assert!(!StyledStr::plain("abc").italic(true).is_plain());
    }
}