1use crate::arena::{SmallVec, StrRef};
11use crate::element::{Element, IntoElement};
12use crate::pod::FrameScope;
13use crate::style::{Color, TextAlign};
14
15#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
17pub enum FontFamily {
18 #[default]
20 Sans,
21 Mono,
23 Serif,
25 Custom(u16),
27}
28
29impl FontFamily {
30 #[inline(always)]
31 pub const fn tag(self) -> u32 {
32 match self {
33 Self::Sans => 0,
34 Self::Mono => 1,
35 Self::Serif => 2,
36 Self::Custom(id) => 0x1000 | (id as u32),
37 }
38 }
39}
40
41#[repr(u8)]
43#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
44pub enum FontWeight {
45 Light = 0,
46 #[default]
47 Normal = 1,
48 Medium = 2,
49 SemiBold = 3,
50 Bold = 4,
51}
52
53impl FontWeight {
54 #[inline(always)]
55 pub const fn is_bold(self) -> bool {
56 matches!(self, Self::SemiBold | Self::Bold)
57 }
58}
59
60#[repr(u8)]
62#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
63pub enum FontStyle {
64 #[default]
65 Normal = 0,
66 Italic = 1,
67}
68
69#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
71pub struct TextDecoration(pub u8);
72
73impl TextDecoration {
74 pub const NONE: Self = Self(0);
75 pub const UNDERLINE: Self = Self(1 << 0);
76 pub const STRIKETHROUGH: Self = Self(1 << 1);
77
78 #[inline(always)]
79 pub const fn union(self, other: Self) -> Self {
80 Self(self.0 | other.0)
81 }
82
83 #[inline(always)]
84 pub const fn has_underline(self) -> bool {
85 (self.0 & Self::UNDERLINE.0) != 0
86 }
87
88 #[inline(always)]
89 pub const fn has_strikethrough(self) -> bool {
90 (self.0 & Self::STRIKETHROUGH.0) != 0
91 }
92}
93
94#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
96pub struct FontSpec {
97 pub family: FontFamily,
98 pub weight: FontWeight,
99 pub style: FontStyle,
100}
101
102impl FontSpec {
103 pub const SANS: Self = Self {
104 family: FontFamily::Sans,
105 weight: FontWeight::Normal,
106 style: FontStyle::Normal,
107 };
108
109 pub const SANS_BOLD: Self = Self {
110 family: FontFamily::Sans,
111 weight: FontWeight::Bold,
112 style: FontStyle::Normal,
113 };
114
115 pub const SANS_ITALIC: Self = Self {
116 family: FontFamily::Sans,
117 weight: FontWeight::Normal,
118 style: FontStyle::Italic,
119 };
120
121 pub const MONO: Self = Self {
122 family: FontFamily::Mono,
123 weight: FontWeight::Normal,
124 style: FontStyle::Normal,
125 };
126
127 pub const MONO_BOLD: Self = Self {
128 family: FontFamily::Mono,
129 weight: FontWeight::Bold,
130 style: FontStyle::Normal,
131 };
132
133 #[inline(always)]
134 pub const fn custom(id: u16) -> Self {
135 Self {
136 family: FontFamily::Custom(id),
137 weight: FontWeight::Normal,
138 style: FontStyle::Normal,
139 }
140 }
141
142 #[inline(always)]
143 pub const fn weight(mut self, w: FontWeight) -> Self {
144 self.weight = w;
145 self
146 }
147
148 #[inline(always)]
149 pub const fn bold(mut self) -> Self {
150 self.weight = FontWeight::Bold;
151 self
152 }
153
154 #[inline(always)]
155 pub const fn italic(mut self) -> Self {
156 self.style = FontStyle::Italic;
157 self
158 }
159
160 #[inline(always)]
161 pub const fn packed_key(self) -> u32 {
162 (self.family.tag() << 4) | ((self.weight as u32) << 1) | (self.style as u32)
163 }
164}
165
166#[derive(Copy, Clone, PartialEq, Eq, Debug)]
168pub struct TextSpan<'a> {
169 pub text: StrRef<'a>,
170 pub color: Option<Color>,
171 pub bg_color: Option<Color>,
172 pub font_size: Option<u8>,
173 pub font: FontSpec,
174 pub decoration: TextDecoration,
175 pub letter_spacing_q4: i8,
177 pub ligatures: bool,
179 pub link_id: Option<u32>,
181}
182
183impl<'a> TextSpan<'a> {
184 #[inline(always)]
186 pub fn new(text: impl Into<StrRef<'a>>) -> Self {
187 Self {
188 text: text.into(),
189 color: None,
190 bg_color: None,
191 font_size: None,
192 font: FontSpec::SANS,
193 decoration: TextDecoration::NONE,
194 letter_spacing_q4: 0,
195 ligatures: false,
196 link_id: None,
197 }
198 }
199
200 #[inline(always)]
201 pub const fn color(mut self, c: Color) -> Self {
202 self.color = Some(c);
203 self
204 }
205
206 #[inline(always)]
207 pub const fn bg(mut self, c: Color) -> Self {
208 self.bg_color = Some(c);
209 self
210 }
211
212 #[inline(always)]
213 pub const fn size(mut self, px: u8) -> Self {
214 self.font_size = Some(px);
215 self
216 }
217
218 #[inline(always)]
219 pub const fn font(mut self, font: FontSpec) -> Self {
220 self.font = font;
221 self
222 }
223
224 #[inline(always)]
225 pub const fn bold(mut self) -> Self {
226 self.font.weight = FontWeight::Bold;
227 self
228 }
229
230 #[inline(always)]
231 pub const fn italic(mut self) -> Self {
232 self.font.style = FontStyle::Italic;
233 self
234 }
235
236 #[inline(always)]
237 pub const fn mono(mut self) -> Self {
238 self.font.family = FontFamily::Mono;
239 self
240 }
241
242 #[inline(always)]
243 pub const fn underline(mut self) -> Self {
244 self.decoration = self.decoration.union(TextDecoration::UNDERLINE);
245 self
246 }
247
248 #[inline(always)]
249 pub const fn strikethrough(mut self) -> Self {
250 self.decoration = self.decoration.union(TextDecoration::STRIKETHROUGH);
251 self
252 }
253
254 #[inline(always)]
255 pub const fn letter_spacing_px(mut self, px: f32) -> Self {
256 let q = (px * 4.0) as i32;
257 self.letter_spacing_q4 = if q < -127 {
258 -127
259 } else if q > 127 {
260 127
261 } else {
262 q as i8
263 };
264 self
265 }
266
267 #[inline(always)]
268 pub const fn ligatures(mut self, enable: bool) -> Self {
269 self.ligatures = enable;
270 self
271 }
272
273 #[inline(always)]
274 pub const fn link_id(mut self, id: u32) -> Self {
275 self.link_id = Some(id);
276 self
277 }
278}
279
280#[derive(Copy, Clone, PartialEq, Eq, Debug)]
282pub struct RichTextSpec<'a> {
283 pub spans: &'a [TextSpan<'a>],
284 pub default_color: Color,
285 pub default_font_size: u8,
286 pub line_height_pct: u16,
288 pub align: TextAlign,
289 pub rtl_paragraph: bool,
291 pub max_lines: u8,
293 pub nowrap: bool,
295}
296
297impl<'a> RichTextSpec<'a> {
298 #[inline]
299 pub fn mix_hash(&self, h: &mut u32) {
300 let mut mix = |b: u8| {
301 *h ^= b as u32;
302 *h = h.wrapping_mul(0x0100_0193);
303 };
304 for b in self.default_color.0.to_le_bytes() {
305 mix(b);
306 }
307 mix(self.default_font_size);
308 for b in self.line_height_pct.to_le_bytes() {
309 mix(b);
310 }
311 mix(self.align as u8);
312 mix(if self.rtl_paragraph { 1 } else { 0 });
313 mix(self.max_lines);
314 mix(if self.nowrap { 1 } else { 0 });
315 for sp in self.spans {
316 for b in sp.font.packed_key().to_le_bytes() {
317 mix(b);
318 }
319 mix(sp.decoration.0);
320 mix(sp.letter_spacing_q4 as u8);
321 mix(if sp.ligatures { 1 } else { 0 });
322 if let Some(c) = sp.color {
323 mix(1);
324 for b in c.0.to_le_bytes() {
325 mix(b);
326 }
327 } else {
328 mix(0);
329 }
330 if let Some(c) = sp.bg_color {
331 mix(1);
332 for b in c.0.to_le_bytes() {
333 mix(b);
334 }
335 } else {
336 mix(0);
337 }
338 mix(sp.font_size.unwrap_or(0));
339 for &b in sp.text.as_str().as_bytes() {
340 mix(b);
341 }
342 }
343 }
344
345 #[inline(always)]
347 pub fn line_height_px(&self, max_span_fs: u8) -> i16 {
348 let fs = max_span_fs.max(self.default_font_size).max(8) as i32;
349 let pct = self.line_height_pct.max(100) as i32;
350 ((fs * pct + 50) / 100).max(fs) as i16
351 }
352}
353
354#[derive(Copy, Clone, Debug)]
356pub struct RichText<'a> {
357 pub spec: RichTextSpec<'a>,
358}
359
360#[inline]
362pub fn rich_text<'a>(cx: &'a FrameScope, spans: &[TextSpan<'a>]) -> RichText<'a> {
363 let alloc_spans = cx.arena().alloc_iter(spans.iter().copied());
364 RichText {
365 spec: RichTextSpec {
366 spans: alloc_spans,
367 default_color: Color::WHITE,
368 default_font_size: 14,
369 line_height_pct: 135,
370 align: TextAlign::Left,
371 rtl_paragraph: cx.is_rtl(),
372 max_lines: 0,
373 nowrap: false,
374 },
375 }
376}
377
378impl<'a> RichText<'a> {
379 #[inline(always)]
380 pub const fn color(mut self, c: Color) -> Self {
381 self.spec.default_color = c;
382 self
383 }
384
385 #[inline(always)]
386 pub const fn size(mut self, px: u8) -> Self {
387 self.spec.default_font_size = px;
388 self
389 }
390
391 #[inline(always)]
392 pub const fn line_height_pct(mut self, pct: u16) -> Self {
393 self.spec.line_height_pct = pct;
394 self
395 }
396
397 #[inline(always)]
398 pub const fn align(mut self, align: TextAlign) -> Self {
399 self.spec.align = align;
400 self
401 }
402
403 #[inline(always)]
404 pub const fn rtl(mut self, rtl: bool) -> Self {
405 self.spec.rtl_paragraph = rtl;
406 self
407 }
408
409 #[inline(always)]
410 pub const fn max_lines(mut self, n: u8) -> Self {
411 self.spec.max_lines = n;
412 self
413 }
414
415 #[inline(always)]
416 pub const fn nowrap(mut self) -> Self {
417 self.spec.nowrap = true;
418 self
419 }
420}
421
422impl<'a> IntoElement<'a> for RichText<'a> {
423 #[inline]
424 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
425 let spec_ref = cx.arena().alloc_temp(self.spec);
426 Element::RichText(spec_ref)
427 }
428}
429
430#[derive(Copy, Clone, PartialEq, Eq, Debug)]
436pub enum BidiClass {
437 Ltr,
438 Rtl,
439 Number,
440 Neutral,
441}
442
443#[inline]
445pub const fn is_rtl_char(ch: char) -> bool {
446 let cp = ch as u32;
447 (cp >= 0x0590 && cp <= 0x05FF)
448 || (cp >= 0x0600 && cp <= 0x06FF)
449 || (cp >= 0x0750 && cp <= 0x077F)
450 || (cp >= 0x08A0 && cp <= 0x08FF)
451 || (cp >= 0xFB1D && cp <= 0xFDFF)
452 || (cp >= 0xFE70 && cp <= 0xFEFF)
453}
454
455#[inline]
457pub const fn is_combining_mark(ch: char) -> bool {
458 let cp = ch as u32;
459 (cp >= 0x0300 && cp <= 0x036F)
460 || (cp >= 0x1AB0 && cp <= 0x1AFF)
461 || (cp >= 0x20D0 && cp <= 0x20FF)
462 || (cp >= 0xFE20 && cp <= 0xFE2F)
463}
464
465#[inline]
467pub const fn is_emoji_char(ch: char) -> bool {
468 let cp = ch as u32;
469 (cp >= 0x1F300 && cp <= 0x1FAFF)
470 || (cp >= 0x2600 && cp <= 0x27BF)
471 || (cp >= 0x2300 && cp <= 0x23FF)
472}
473
474#[inline]
476pub const fn mirror_bidi_char(ch: char) -> char {
477 match ch {
478 '(' => ')',
479 ')' => '(',
480 '[' => ']',
481 ']' => '[',
482 '{' => '}',
483 '}' => '{',
484 '<' => '>',
485 '>' => '<',
486 '«' => '»',
487 '»' => '«',
488 _ => ch,
489 }
490}
491
492#[inline]
493pub const fn classify_bidi_char(ch: char) -> BidiClass {
494 if is_rtl_char(ch) {
495 BidiClass::Rtl
496 } else if ch.is_ascii_digit() {
497 BidiClass::Number
498 } else if ch.is_ascii_alphabetic() || (ch as u32 >= 0x00C0 && !ch.is_whitespace() && !ch.is_ascii_punctuation()) {
499 if is_rtl_char(ch) {
501 BidiClass::Rtl
502 } else {
503 BidiClass::Ltr
504 }
505 } else {
506 BidiClass::Neutral
507 }
508}
509
510#[inline]
512pub fn contains_rtl(s: &str) -> bool {
513 s.chars().any(is_rtl_char)
514}
515
516#[derive(Copy, Clone, PartialEq, Eq, Debug)]
518pub struct BidiVisualRun {
519 pub start: usize,
520 pub end: usize,
521 pub is_rtl: bool,
522}
523
524pub fn reorder_bidi_visual(line: &str, base_rtl: bool) -> SmallVec<BidiVisualRun, 32> {
526 let mut runs: SmallVec<BidiVisualRun, 32> = SmallVec::new();
527 if line.is_empty() {
528 return runs;
529 }
530 if !base_rtl && !contains_rtl(line) {
531 let _ = runs.push(BidiVisualRun {
532 start: 0,
533 end: line.len(),
534 is_rtl: false,
535 });
536 return runs;
537 }
538
539 let mut cur_start = 0usize;
541 let mut cur_rtl = base_rtl;
542 let mut cur_is_num = false;
543 let mut initialized = false;
544
545 for (idx, ch) in line.char_indices() {
546 let cls = classify_bidi_char(ch);
547 let (want_rtl, want_num) = match cls {
548 BidiClass::Rtl => (true, false),
549 BidiClass::Ltr => (false, false),
550 BidiClass::Number => (false, true),
551 BidiClass::Neutral => {
552 if initialized {
553 if cur_is_num {
554 (base_rtl, false)
555 } else {
556 (cur_rtl, false)
557 }
558 } else {
559 (base_rtl, false)
560 }
561 }
562 };
563 if !initialized {
564 cur_start = idx;
565 cur_rtl = want_rtl;
566 cur_is_num = want_num;
567 initialized = true;
568 } else if want_rtl != cur_rtl || want_num != cur_is_num {
569 let _ = runs.push(BidiVisualRun {
570 start: cur_start,
571 end: idx,
572 is_rtl: cur_rtl,
573 });
574 cur_start = idx;
575 cur_rtl = want_rtl;
576 cur_is_num = want_num;
577 }
578 }
579 if initialized && cur_start < line.len() {
580 let _ = runs.push(BidiVisualRun {
581 start: cur_start,
582 end: line.len(),
583 is_rtl: cur_rtl,
584 });
585 }
586
587 if base_rtl && runs.len() > 1 {
589 let slice = runs.as_mut_slice();
590 slice.reverse();
591 }
592 runs
593}
594
595#[derive(Copy, Clone, PartialEq, Eq, Debug)]
601pub struct ShapedGlyph {
602 pub ch: char,
603 pub is_combining: bool,
604 pub byte_offset: usize,
605}
606
607pub fn shape_text_run(text: &str, ligatures: bool) -> SmallVec<ShapedGlyph, 128> {
609 let mut out: SmallVec<ShapedGlyph, 128> = SmallVec::new();
610 let bytes = text.as_bytes();
611 let mut i = 0usize;
612 while i < bytes.len() {
613 if ligatures && i + 1 < bytes.len() {
614 let lig = match (bytes[i], bytes[i + 1]) {
615 (b'-', b'>') => Some('→'),
616 (b'<', b'-') => Some('←'),
617 (b'!', b'=') => Some('≠'),
618 (b'<', b'=') => Some('≤'),
619 (b'>', b'=') => Some('≥'),
620 (b'=', b'>') => Some('⇒'),
621 _ => None,
622 };
623 if let Some(ch) = lig {
624 if !out.push(ShapedGlyph {
625 ch,
626 is_combining: false,
627 byte_offset: i,
628 }) {
629 break;
630 }
631 i += 2;
632 continue;
633 }
634 }
635 let Some(ch) = text[i..].chars().next() else {
636 break;
637 };
638 if !out.push(ShapedGlyph {
639 ch,
640 is_combining: is_combining_mark(ch),
641 byte_offset: i,
642 }) {
643 break;
644 }
645 i += ch.len_utf8();
646 }
647 out
648}
649
650pub fn measure_rich_text_span_width_f32(span: &TextSpan<'_>, default_fs: u8) -> f32 {
652 let fs = span.font_size.unwrap_or(default_fs).max(8);
653 let ls = (span.letter_spacing_q4 as f32) * 0.25;
654 let shaped = shape_text_run(span.text.as_str(), span.ligatures);
655 let mut total = 0.0f32;
656 let mut prev: Option<char> = None;
657 for g in shaped.as_slice() {
658 if g.is_combining {
659 continue;
660 }
661 if let Some(p) = prev {
662 total += crate::display::measure_char_kerning_f32(p, g.ch, fs);
663 total += ls;
664 }
665 total += crate::display::measure_char_advance_styled_f32(g.ch, fs, span.font);
666 prev = Some(g.ch);
667 }
668 total.max(0.0)
669}
670
671#[inline(always)]
672fn ceil_i16(v: f32) -> i16 {
673 let i = v as i32;
674 let c = if (i as f32) < v { i + 1 } else { i };
675 c.clamp(0, i16::MAX as i32) as i16
676}
677
678pub fn measure_rich_text_box(spec: &RichTextSpec<'_>, max_width: i16) -> (i16, i16) {
680 if spec.spans.is_empty() {
681 return (0, 0);
682 }
683 let avail_w = if spec.nowrap || max_width <= 0 {
684 f32::INFINITY
685 } else {
686 max_width as f32
687 };
688 let mut cur_line_w = 0.0f32;
689 let mut cur_line_max_fs = spec.default_font_size.max(8);
690 let mut max_line_w = 0.0f32;
691 let mut total_h = 0i32;
692 let mut line_count = 1u8;
693
694 for sp in spec.spans {
695 let fs = sp.font_size.unwrap_or(spec.default_font_size).max(8);
696 let ls = (sp.letter_spacing_q4 as f32) * 0.25;
697 let shaped = shape_text_run(sp.text.as_str(), sp.ligatures);
698 let mut prev: Option<char> = None;
699 for g in shaped.as_slice() {
700 if g.ch == '\n' {
701 max_line_w = max_line_w.max(cur_line_w);
702 total_h += spec.line_height_px(cur_line_max_fs) as i32;
703 cur_line_w = 0.0;
704 cur_line_max_fs = spec.default_font_size.max(8);
705 prev = None;
706 line_count = line_count.saturating_add(1);
707 if spec.max_lines > 0 && line_count > spec.max_lines {
708 return (ceil_i16(max_line_w), total_h.min(i16::MAX as i32) as i16);
709 }
710 continue;
711 }
712 if g.is_combining {
713 continue;
714 }
715 let adv = crate::display::measure_char_advance_styled_f32(g.ch, fs, sp.font)
716 + if prev.is_some() { ls } else { 0.0 };
717 if cur_line_w > 0.0 && cur_line_w + adv > avail_w {
718 max_line_w = max_line_w.max(cur_line_w);
719 total_h += spec.line_height_px(cur_line_max_fs) as i32;
720 cur_line_w = adv;
721 cur_line_max_fs = fs;
722 prev = Some(g.ch);
723 line_count = line_count.saturating_add(1);
724 if spec.max_lines > 0 && line_count > spec.max_lines {
725 return (ceil_i16(max_line_w), total_h.min(i16::MAX as i32) as i16);
726 }
727 } else {
728 cur_line_w += adv;
729 cur_line_max_fs = cur_line_max_fs.max(fs);
730 prev = Some(g.ch);
731 }
732 }
733 }
734 max_line_w = max_line_w.max(cur_line_w);
735 total_h += spec.line_height_px(cur_line_max_fs) as i32;
736 (
737 ceil_i16(max_line_w),
738 total_h.min(i16::MAX as i32) as i16,
739 )
740}
741
742#[repr(u8)]
748#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
749pub enum ScriptKind {
750 #[default]
751 Latin = 0,
752 Cjk = 1,
753 Arabic = 2,
754 Hebrew = 3,
755 Devanagari = 4,
756 Cyrillic = 5,
757 Emoji = 6,
758 Symbol = 7,
759}
760
761#[inline]
763pub const fn classify_script(ch: char) -> ScriptKind {
764 let cp = ch as u32;
765 if is_emoji_char(ch)
766 || cp == 0x200D
767 || cp == 0xFE0F
768 || (cp >= 0x1F3FB && cp <= 0x1F3FF)
769 || (cp >= 0x1F1E6 && cp <= 0x1F1FF)
770 {
771 return ScriptKind::Emoji;
772 }
773 if (cp >= 0x4E00 && cp <= 0x9FFF)
774 || (cp >= 0x3400 && cp <= 0x4DBF)
775 || (cp >= 0x3000 && cp <= 0x30FF)
776 || (cp >= 0xAC00 && cp <= 0xD7AF)
777 || (cp >= 0xFF00 && cp <= 0xFFEF)
778 {
779 return ScriptKind::Cjk;
780 }
781 if (cp >= 0x0600 && cp <= 0x06FF)
782 || (cp >= 0x0750 && cp <= 0x077F)
783 || (cp >= 0x08A0 && cp <= 0x08FF)
784 {
785 return ScriptKind::Arabic;
786 }
787 if cp >= 0x0590 && cp <= 0x05FF {
788 return ScriptKind::Hebrew;
789 }
790 if cp >= 0x0900 && cp <= 0x097F {
791 return ScriptKind::Devanagari;
792 }
793 if cp >= 0x0400 && cp <= 0x052F {
794 return ScriptKind::Cyrillic;
795 }
796 if cp < 0x0250 || (cp >= 0x1E00 && cp <= 0x1EFF) {
797 return ScriptKind::Latin;
798 }
799 ScriptKind::Symbol
800}
801
802#[derive(Copy, Clone, PartialEq, Eq, Debug)]
804pub struct ScriptRun<'a> {
805 pub script: ScriptKind,
806 pub text: &'a str,
807}
808
809pub fn split_script_runs<'a>(text: &'a str) -> SmallVec<ScriptRun<'a>, 32> {
811 let mut runs: SmallVec<ScriptRun<'a>, 32> = SmallVec::new();
812 let mut iter = text.char_indices().peekable();
813 let mut cur_start: Option<usize> = None;
814 let mut cur_script = ScriptKind::Latin;
815
816 while let Some((idx, ch)) = iter.next() {
817 let sc = if ch.is_whitespace() && cur_start.is_some() {
818 cur_script
819 } else {
820 classify_script(ch)
821 };
822 match cur_start {
823 None => {
824 cur_start = Some(idx);
825 cur_script = sc;
826 }
827 Some(start) if sc != cur_script => {
828 let _ = runs.push(ScriptRun {
829 script: cur_script,
830 text: &text[start..idx],
831 });
832 cur_start = Some(idx);
833 cur_script = sc;
834 }
835 Some(_) => {}
836 }
837 }
838 if let Some(start) = cur_start {
839 let _ = runs.push(ScriptRun {
840 script: cur_script,
841 text: &text[start..],
842 });
843 }
844 runs
845}
846
847#[derive(Copy, Clone, PartialEq, Eq, Debug)]
849pub struct FontFallbackChain {
850 slots: [Option<FontFamily>; 8],
851}
852
853impl Default for FontFallbackChain {
854 #[inline(always)]
855 fn default() -> Self {
856 Self::new()
857 }
858}
859
860impl FontFallbackChain {
861 #[inline(always)]
862 pub const fn new() -> Self {
863 Self { slots: [None; 8] }
864 }
865
866 #[inline(always)]
868 pub const fn with_script(mut self, script: ScriptKind, family: FontFamily) -> Self {
869 self.slots[script as usize] = Some(family);
870 self
871 }
872
873 #[inline]
875 pub const fn resolve_for_char(&self, ch: char, base: FontSpec) -> FontSpec {
876 let script = classify_script(ch);
877 if let Some(fam) = self.slots[script as usize] {
878 FontSpec {
879 family: fam,
880 weight: base.weight,
881 style: base.style,
882 }
883 } else {
884 base
885 }
886 }
887}
888
889pub struct InlineFlow<'a> {
892 root: crate::element::Div<'a>,
893 default_size: u8,
894 default_color: Color,
895}
896
897#[inline]
899pub fn inline_flow<'a>() -> InlineFlow<'a> {
900 use crate::style::Styled;
901 InlineFlow {
902 root: crate::element::div()
903 .flex_row()
904 .flex_wrap()
905 .items_center()
906 .gap(4),
907 default_size: 13,
908 default_color: Color::WHITE,
909 }
910}
911
912impl<'a> InlineFlow<'a> {
913 #[inline]
914 pub fn w(mut self, w: i16) -> Self {
915 use crate::style::Styled;
916 self.root = self.root.w(w);
917 self
918 }
919
920 #[inline]
921 pub fn w_full(mut self) -> Self {
922 use crate::style::Styled;
923 self.root = self.root.w_full();
924 self
925 }
926
927 #[inline]
928 pub fn gap(mut self, px: i8) -> Self {
929 use crate::style::Styled;
930 self.root = self.root.gap(px);
931 self
932 }
933
934 #[inline]
935 pub const fn default_size(mut self, px: u8) -> Self {
936 self.default_size = px;
937 self
938 }
939
940 #[inline]
941 pub const fn default_color(mut self, color: Color) -> Self {
942 self.default_color = color;
943 self
944 }
945
946 pub fn text(self, cx: &'a FrameScope, content: &'a str) -> Self {
948 let sz = self.default_size;
949 let col = self.default_color;
950 self.styled_text(cx, content, col, sz, false)
951 }
952
953 pub fn styled_text(
955 mut self,
956 cx: &'a FrameScope,
957 content: &'a str,
958 color: Color,
959 font_size: u8,
960 bold: bool,
961 ) -> Self {
962 let mut start = 0usize;
963 let mut iter = content.char_indices().peekable();
964 while let Some((idx, ch)) = iter.next() {
965 let next_idx = iter.peek().map(|&(ni, _)| ni).unwrap_or(content.len());
966 let is_cjk = matches!(
967 classify_script(ch),
968 ScriptKind::Cjk | ScriptKind::Emoji
969 );
970 if ch.is_whitespace() {
971 if idx > start {
972 let word = &content[start..idx];
973 self = self.push_atom_text(cx, word, color, font_size, bold);
974 }
975 start = next_idx;
976 } else if is_cjk {
977 if idx > start {
978 self = self.push_atom_text(cx, &content[start..idx], color, font_size, bold);
979 }
980 self = self.push_atom_text(cx, &content[idx..next_idx], color, font_size, bold);
981 start = next_idx;
982 }
983 }
984 if start < content.len() {
985 self = self.push_atom_text(cx, &content[start..], color, font_size, bold);
986 }
987 self
988 }
989
990 fn push_atom_text(
991 mut self,
992 cx: &'a FrameScope,
993 word: &'a str,
994 color: Color,
995 font_size: u8,
996 bold: bool,
997 ) -> Self {
998 if word.is_empty() {
999 return self;
1000 }
1001 let sz = if bold { font_size.saturating_add(1) } else { font_size };
1002 let t = crate::element::text(word).size(sz).color(color).nowrap();
1003 self.root = self.root.child(cx, t);
1004 self
1005 }
1006
1007 #[inline]
1009 pub fn inline_widget(mut self, cx: &'a FrameScope, widget: impl IntoElement<'a>) -> Self {
1010 self.root = self.root.child(cx, widget);
1011 self
1012 }
1013
1014 #[inline]
1016 pub fn build(self) -> crate::element::Div<'a> {
1017 self.root
1018 }
1019}
1020
1021impl<'a> IntoElement<'a> for InlineFlow<'a> {
1022 #[inline]
1023 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
1024 self.root.into_element(cx)
1025 }
1026}