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 pub var_weight_x10: u16,
183 pub var_width_x10: u16,
185}
186
187impl<'a> TextSpan<'a> {
188 #[inline(always)]
190 pub fn new(text: impl Into<StrRef<'a>>) -> Self {
191 Self {
192 text: text.into(),
193 color: None,
194 bg_color: None,
195 font_size: None,
196 font: FontSpec::SANS,
197 decoration: TextDecoration::NONE,
198 letter_spacing_q4: 0,
199 ligatures: false,
200 link_id: None,
201 var_weight_x10: 0,
202 var_width_x10: 0,
203 }
204 }
205
206 #[inline(always)]
207 pub const fn color(mut self, c: Color) -> Self {
208 self.color = Some(c);
209 self
210 }
211
212 #[inline(always)]
213 pub const fn bg(mut self, c: Color) -> Self {
214 self.bg_color = Some(c);
215 self
216 }
217
218 #[inline(always)]
219 pub const fn size(mut self, px: u8) -> Self {
220 self.font_size = Some(px);
221 self
222 }
223
224 #[inline(always)]
225 pub const fn font(mut self, font: FontSpec) -> Self {
226 self.font = font;
227 self
228 }
229
230 #[inline(always)]
231 pub const fn bold(mut self) -> Self {
232 self.font.weight = FontWeight::Bold;
233 self
234 }
235
236 #[inline(always)]
237 pub const fn italic(mut self) -> Self {
238 self.font.style = FontStyle::Italic;
239 self
240 }
241
242 #[inline(always)]
243 pub const fn mono(mut self) -> Self {
244 self.font.family = FontFamily::Mono;
245 self
246 }
247
248 #[inline(always)]
249 pub const fn underline(mut self) -> Self {
250 self.decoration = self.decoration.union(TextDecoration::UNDERLINE);
251 self
252 }
253
254 #[inline(always)]
255 pub const fn strikethrough(mut self) -> Self {
256 self.decoration = self.decoration.union(TextDecoration::STRIKETHROUGH);
257 self
258 }
259
260 #[inline(always)]
261 pub const fn letter_spacing_px(mut self, px: f32) -> Self {
262 let q = (px * 4.0) as i32;
263 self.letter_spacing_q4 = if q < -127 {
264 -127
265 } else if q > 127 {
266 127
267 } else {
268 q as i8
269 };
270 self
271 }
272
273 #[inline(always)]
274 pub const fn ligatures(mut self, enable: bool) -> Self {
275 self.ligatures = enable;
276 self
277 }
278
279 #[inline(always)]
280 pub const fn link_id(mut self, id: u32) -> Self {
281 self.link_id = Some(id);
282 self
283 }
284
285 #[inline(always)]
287 pub const fn var_weight(mut self, wght: f32) -> Self {
288 let clamped = if wght < 1.0 {
289 1.0
290 } else if wght > 1000.0 {
291 1000.0
292 } else {
293 wght
294 };
295 self.var_weight_x10 = (clamped * 10.0 + 0.5) as u16;
296 self
297 }
298
299 #[inline(always)]
301 pub const fn var_width(mut self, wdth: f32) -> Self {
302 let clamped = if wdth < 25.0 {
303 25.0
304 } else if wdth > 400.0 {
305 400.0
306 } else {
307 wdth
308 };
309 self.var_width_x10 = (clamped * 10.0 + 0.5) as u16;
310 self
311 }
312
313 #[inline(always)]
315 pub const fn has_variable_axes(&self) -> bool {
316 self.var_weight_x10 != 0 || self.var_width_x10 != 0
317 }
318}
319
320#[derive(Copy, Clone, PartialEq, Eq, Debug)]
322pub struct RichTextSpec<'a> {
323 pub spans: &'a [TextSpan<'a>],
324 pub default_color: Color,
325 pub default_font_size: u8,
326 pub line_height_pct: u16,
328 pub align: TextAlign,
329 pub rtl_paragraph: bool,
331 pub max_lines: u8,
333 pub nowrap: bool,
335}
336
337impl<'a> RichTextSpec<'a> {
338 #[inline]
339 pub fn mix_hash(&self, h: &mut u32) {
340 let mut mix = |b: u8| {
341 *h ^= b as u32;
342 *h = h.wrapping_mul(0x0100_0193);
343 };
344 for b in self.default_color.0.to_le_bytes() {
345 mix(b);
346 }
347 mix(self.default_font_size);
348 for b in self.line_height_pct.to_le_bytes() {
349 mix(b);
350 }
351 mix(self.align as u8);
352 mix(if self.rtl_paragraph { 1 } else { 0 });
353 mix(self.max_lines);
354 mix(if self.nowrap { 1 } else { 0 });
355 for sp in self.spans {
356 for b in sp.font.packed_key().to_le_bytes() {
357 mix(b);
358 }
359 mix(sp.decoration.0);
360 mix(sp.letter_spacing_q4 as u8);
361 mix(if sp.ligatures { 1 } else { 0 });
362 for b in sp.var_weight_x10.to_le_bytes() {
363 mix(b);
364 }
365 for b in sp.var_width_x10.to_le_bytes() {
366 mix(b);
367 }
368 if let Some(c) = sp.color {
369 mix(1);
370 for b in c.0.to_le_bytes() {
371 mix(b);
372 }
373 } else {
374 mix(0);
375 }
376 if let Some(c) = sp.bg_color {
377 mix(1);
378 for b in c.0.to_le_bytes() {
379 mix(b);
380 }
381 } else {
382 mix(0);
383 }
384 mix(sp.font_size.unwrap_or(0));
385 for &b in sp.text.as_str().as_bytes() {
386 mix(b);
387 }
388 }
389 }
390
391 #[inline(always)]
393 pub fn line_height_px(&self, max_span_fs: u8) -> i16 {
394 let fs = max_span_fs.max(self.default_font_size).max(8) as i32;
395 let pct = self.line_height_pct.max(100) as i32;
396 ((fs * pct + 50) / 100).max(fs) as i16
397 }
398}
399
400#[derive(Copy, Clone, Debug)]
402pub struct RichText<'a> {
403 pub spec: RichTextSpec<'a>,
404}
405
406#[inline]
408pub fn rich_text<'a>(cx: &'a FrameScope, spans: &[TextSpan<'a>]) -> RichText<'a> {
409 let alloc_spans = cx.arena().alloc_iter(spans.iter().copied());
410 RichText {
411 spec: RichTextSpec {
412 spans: alloc_spans,
413 default_color: Color::WHITE,
414 default_font_size: 14,
415 line_height_pct: 135,
416 align: TextAlign::Left,
417 rtl_paragraph: cx.is_rtl(),
418 max_lines: 0,
419 nowrap: false,
420 },
421 }
422}
423
424impl<'a> RichText<'a> {
425 #[inline(always)]
426 pub const fn color(mut self, c: Color) -> Self {
427 self.spec.default_color = c;
428 self
429 }
430
431 #[inline(always)]
432 pub const fn size(mut self, px: u8) -> Self {
433 self.spec.default_font_size = px;
434 self
435 }
436
437 #[inline(always)]
438 pub const fn line_height_pct(mut self, pct: u16) -> Self {
439 self.spec.line_height_pct = pct;
440 self
441 }
442
443 #[inline(always)]
444 pub const fn align(mut self, align: TextAlign) -> Self {
445 self.spec.align = align;
446 self
447 }
448
449 #[inline(always)]
450 pub const fn rtl(mut self, rtl: bool) -> Self {
451 self.spec.rtl_paragraph = rtl;
452 self
453 }
454
455 #[inline(always)]
456 pub const fn max_lines(mut self, n: u8) -> Self {
457 self.spec.max_lines = n;
458 self
459 }
460
461 #[inline(always)]
462 pub const fn nowrap(mut self) -> Self {
463 self.spec.nowrap = true;
464 self
465 }
466}
467
468impl<'a> IntoElement<'a> for RichText<'a> {
469 #[inline]
470 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
471 let spec_ref = cx.arena().alloc_temp(self.spec);
472 Element::RichText(spec_ref)
473 }
474}
475
476#[derive(Copy, Clone, PartialEq, Eq, Debug)]
482pub enum BidiClass {
483 Ltr,
484 Rtl,
485 Number,
486 Neutral,
487}
488
489#[inline]
491pub const fn is_rtl_char(ch: char) -> bool {
492 let cp = ch as u32;
493 (cp >= 0x0590 && cp <= 0x05FF)
494 || (cp >= 0x0600 && cp <= 0x06FF)
495 || (cp >= 0x0750 && cp <= 0x077F)
496 || (cp >= 0x08A0 && cp <= 0x08FF)
497 || (cp >= 0xFB1D && cp <= 0xFDFF)
498 || (cp >= 0xFE70 && cp <= 0xFEFF)
499}
500
501#[inline]
503pub const fn is_combining_mark(ch: char) -> bool {
504 let cp = ch as u32;
505 (cp >= 0x0300 && cp <= 0x036F)
506 || (cp >= 0x1AB0 && cp <= 0x1AFF)
507 || (cp >= 0x20D0 && cp <= 0x20FF)
508 || (cp >= 0xFE20 && cp <= 0xFE2F)
509}
510
511#[inline]
513pub const fn is_emoji_char(ch: char) -> bool {
514 let cp = ch as u32;
515 (cp >= 0x1F300 && cp <= 0x1FAFF)
516 || (cp >= 0x2600 && cp <= 0x27BF)
517 || (cp >= 0x2300 && cp <= 0x23FF)
518}
519
520#[inline]
522pub const fn mirror_bidi_char(ch: char) -> char {
523 match ch {
524 '(' => ')',
525 ')' => '(',
526 '[' => ']',
527 ']' => '[',
528 '{' => '}',
529 '}' => '{',
530 '<' => '>',
531 '>' => '<',
532 '«' => '»',
533 '»' => '«',
534 _ => ch,
535 }
536}
537
538#[inline]
539pub const fn classify_bidi_char(ch: char) -> BidiClass {
540 if is_rtl_char(ch) {
541 BidiClass::Rtl
542 } else if ch.is_ascii_digit() {
543 BidiClass::Number
544 } else if ch.is_ascii_alphabetic() || (ch as u32 >= 0x00C0 && !ch.is_whitespace() && !ch.is_ascii_punctuation()) {
545 if is_rtl_char(ch) {
547 BidiClass::Rtl
548 } else {
549 BidiClass::Ltr
550 }
551 } else {
552 BidiClass::Neutral
553 }
554}
555
556#[inline]
558pub fn contains_rtl(s: &str) -> bool {
559 s.chars().any(is_rtl_char)
560}
561
562#[derive(Copy, Clone, PartialEq, Eq, Debug)]
564pub struct BidiVisualRun {
565 pub start: usize,
566 pub end: usize,
567 pub is_rtl: bool,
568}
569
570pub fn reorder_bidi_visual(line: &str, base_rtl: bool) -> SmallVec<BidiVisualRun, 32> {
572 let mut runs: SmallVec<BidiVisualRun, 32> = SmallVec::new();
573 if line.is_empty() {
574 return runs;
575 }
576 if !base_rtl && !contains_rtl(line) {
577 let _ = runs.push(BidiVisualRun {
578 start: 0,
579 end: line.len(),
580 is_rtl: false,
581 });
582 return runs;
583 }
584
585 let mut cur_start = 0usize;
587 let mut cur_rtl = base_rtl;
588 let mut cur_is_num = false;
589 let mut initialized = false;
590
591 for (idx, ch) in line.char_indices() {
592 let cls = classify_bidi_char(ch);
593 let (want_rtl, want_num) = match cls {
594 BidiClass::Rtl => (true, false),
595 BidiClass::Ltr => (false, false),
596 BidiClass::Number => (false, true),
597 BidiClass::Neutral => {
598 if initialized {
599 if cur_is_num {
600 (base_rtl, false)
601 } else {
602 (cur_rtl, false)
603 }
604 } else {
605 (base_rtl, false)
606 }
607 }
608 };
609 if !initialized {
610 cur_start = idx;
611 cur_rtl = want_rtl;
612 cur_is_num = want_num;
613 initialized = true;
614 } else if want_rtl != cur_rtl || want_num != cur_is_num {
615 let _ = runs.push(BidiVisualRun {
616 start: cur_start,
617 end: idx,
618 is_rtl: cur_rtl,
619 });
620 cur_start = idx;
621 cur_rtl = want_rtl;
622 cur_is_num = want_num;
623 }
624 }
625 if initialized && cur_start < line.len() {
626 let _ = runs.push(BidiVisualRun {
627 start: cur_start,
628 end: line.len(),
629 is_rtl: cur_rtl,
630 });
631 }
632
633 if base_rtl && runs.len() > 1 {
635 let slice = runs.as_mut_slice();
636 slice.reverse();
637 }
638 runs
639}
640
641#[derive(Copy, Clone, PartialEq, Eq, Debug)]
647pub struct ShapedGlyph {
648 pub ch: char,
649 pub is_combining: bool,
650 pub byte_offset: usize,
651}
652
653pub fn shape_text_run(text: &str, ligatures: bool) -> SmallVec<ShapedGlyph, 128> {
655 let mut out: SmallVec<ShapedGlyph, 128> = SmallVec::new();
656 let bytes = text.as_bytes();
657 let mut i = 0usize;
658 while i < bytes.len() {
659 if ligatures && i + 1 < bytes.len() {
660 let lig = match (bytes[i], bytes[i + 1]) {
661 (b'-', b'>') => Some('→'),
662 (b'<', b'-') => Some('←'),
663 (b'!', b'=') => Some('≠'),
664 (b'<', b'=') => Some('≤'),
665 (b'>', b'=') => Some('≥'),
666 (b'=', b'>') => Some('⇒'),
667 _ => None,
668 };
669 if let Some(ch) = lig {
670 if !out.push(ShapedGlyph {
671 ch,
672 is_combining: false,
673 byte_offset: i,
674 }) {
675 break;
676 }
677 i += 2;
678 continue;
679 }
680 }
681 let Some(ch) = text[i..].chars().next() else {
682 break;
683 };
684 if !out.push(ShapedGlyph {
685 ch,
686 is_combining: is_combining_mark(ch),
687 byte_offset: i,
688 }) {
689 break;
690 }
691 i += ch.len_utf8();
692 }
693 out
694}
695
696pub fn measure_rich_text_span_width_f32(span: &TextSpan<'_>, default_fs: u8) -> f32 {
698 let fs = span.font_size.unwrap_or(default_fs).max(8);
699 let ls = (span.letter_spacing_q4 as f32) * 0.25;
700 let shaped = shape_text_run(span.text.as_str(), span.ligatures);
701 let mut total = 0.0f32;
702 let mut prev: Option<char> = None;
703 for g in shaped.as_slice() {
704 if g.is_combining {
705 continue;
706 }
707 if let Some(p) = prev {
708 total += crate::display::measure_char_kerning_f32(p, g.ch, fs);
709 total += ls;
710 }
711 total += crate::display::measure_char_advance_styled_f32(g.ch, fs, span.font);
712 prev = Some(g.ch);
713 }
714 total.max(0.0)
715}
716
717#[inline(always)]
718fn ceil_i16(v: f32) -> i16 {
719 let i = v as i32;
720 let c = if (i as f32) < v { i + 1 } else { i };
721 c.clamp(0, i16::MAX as i32) as i16
722}
723
724pub fn measure_rich_text_box(spec: &RichTextSpec<'_>, max_width: i16) -> (i16, i16) {
726 if spec.spans.is_empty() {
727 return (0, 0);
728 }
729 let avail_w = if spec.nowrap || max_width <= 0 {
730 f32::INFINITY
731 } else {
732 max_width as f32
733 };
734 let mut cur_line_w = 0.0f32;
735 let mut cur_line_max_fs = spec.default_font_size.max(8);
736 let mut max_line_w = 0.0f32;
737 let mut total_h = 0i32;
738 let mut line_count = 1u8;
739
740 for sp in spec.spans {
741 let fs = sp.font_size.unwrap_or(spec.default_font_size).max(8);
742 let ls = (sp.letter_spacing_q4 as f32) * 0.25;
743 let shaped = shape_text_run(sp.text.as_str(), sp.ligatures);
744 let mut prev: Option<char> = None;
745 for g in shaped.as_slice() {
746 if g.ch == '\n' {
747 max_line_w = max_line_w.max(cur_line_w);
748 total_h += spec.line_height_px(cur_line_max_fs) as i32;
749 cur_line_w = 0.0;
750 cur_line_max_fs = spec.default_font_size.max(8);
751 prev = None;
752 line_count = line_count.saturating_add(1);
753 if spec.max_lines > 0 && line_count > spec.max_lines {
754 return (ceil_i16(max_line_w), total_h.min(i16::MAX as i32) as i16);
755 }
756 continue;
757 }
758 if g.is_combining {
759 continue;
760 }
761 let adv = crate::display::measure_char_advance_styled_f32(g.ch, fs, sp.font)
762 + if prev.is_some() { ls } else { 0.0 };
763 if cur_line_w > 0.0 && cur_line_w + adv > avail_w {
764 max_line_w = max_line_w.max(cur_line_w);
765 total_h += spec.line_height_px(cur_line_max_fs) as i32;
766 cur_line_w = adv;
767 cur_line_max_fs = fs;
768 prev = Some(g.ch);
769 line_count = line_count.saturating_add(1);
770 if spec.max_lines > 0 && line_count > spec.max_lines {
771 return (ceil_i16(max_line_w), total_h.min(i16::MAX as i32) as i16);
772 }
773 } else {
774 cur_line_w += adv;
775 cur_line_max_fs = cur_line_max_fs.max(fs);
776 prev = Some(g.ch);
777 }
778 }
779 }
780 max_line_w = max_line_w.max(cur_line_w);
781 total_h += spec.line_height_px(cur_line_max_fs) as i32;
782 (
783 ceil_i16(max_line_w),
784 total_h.min(i16::MAX as i32) as i16,
785 )
786}
787
788#[repr(u8)]
794#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
795pub enum ScriptKind {
796 #[default]
797 Latin = 0,
798 Cjk = 1,
799 Arabic = 2,
800 Hebrew = 3,
801 Devanagari = 4,
802 Cyrillic = 5,
803 Emoji = 6,
804 Symbol = 7,
805}
806
807#[inline]
809pub const fn classify_script(ch: char) -> ScriptKind {
810 let cp = ch as u32;
811 if is_emoji_char(ch)
812 || cp == 0x200D
813 || cp == 0xFE0F
814 || (cp >= 0x1F3FB && cp <= 0x1F3FF)
815 || (cp >= 0x1F1E6 && cp <= 0x1F1FF)
816 {
817 return ScriptKind::Emoji;
818 }
819 if (cp >= 0x4E00 && cp <= 0x9FFF)
820 || (cp >= 0x3400 && cp <= 0x4DBF)
821 || (cp >= 0x3000 && cp <= 0x30FF)
822 || (cp >= 0xAC00 && cp <= 0xD7AF)
823 || (cp >= 0xFF00 && cp <= 0xFFEF)
824 {
825 return ScriptKind::Cjk;
826 }
827 if (cp >= 0x0600 && cp <= 0x06FF)
828 || (cp >= 0x0750 && cp <= 0x077F)
829 || (cp >= 0x08A0 && cp <= 0x08FF)
830 {
831 return ScriptKind::Arabic;
832 }
833 if cp >= 0x0590 && cp <= 0x05FF {
834 return ScriptKind::Hebrew;
835 }
836 if cp >= 0x0900 && cp <= 0x097F {
837 return ScriptKind::Devanagari;
838 }
839 if cp >= 0x0400 && cp <= 0x052F {
840 return ScriptKind::Cyrillic;
841 }
842 if cp < 0x0250 || (cp >= 0x1E00 && cp <= 0x1EFF) {
843 return ScriptKind::Latin;
844 }
845 ScriptKind::Symbol
846}
847
848#[derive(Copy, Clone, PartialEq, Eq, Debug)]
850pub struct ScriptRun<'a> {
851 pub script: ScriptKind,
852 pub text: &'a str,
853}
854
855pub fn split_script_runs<'a>(text: &'a str) -> SmallVec<ScriptRun<'a>, 32> {
857 let mut runs: SmallVec<ScriptRun<'a>, 32> = SmallVec::new();
858 let mut iter = text.char_indices().peekable();
859 let mut cur_start: Option<usize> = None;
860 let mut cur_script = ScriptKind::Latin;
861
862 while let Some((idx, ch)) = iter.next() {
863 let sc = if ch.is_whitespace() && cur_start.is_some() {
864 cur_script
865 } else {
866 classify_script(ch)
867 };
868 match cur_start {
869 None => {
870 cur_start = Some(idx);
871 cur_script = sc;
872 }
873 Some(start) if sc != cur_script => {
874 let _ = runs.push(ScriptRun {
875 script: cur_script,
876 text: &text[start..idx],
877 });
878 cur_start = Some(idx);
879 cur_script = sc;
880 }
881 Some(_) => {}
882 }
883 }
884 if let Some(start) = cur_start {
885 let _ = runs.push(ScriptRun {
886 script: cur_script,
887 text: &text[start..],
888 });
889 }
890 runs
891}
892
893#[derive(Copy, Clone, PartialEq, Eq, Debug)]
895pub struct FontFallbackChain {
896 slots: [Option<FontFamily>; 8],
897}
898
899impl Default for FontFallbackChain {
900 #[inline(always)]
901 fn default() -> Self {
902 Self::new()
903 }
904}
905
906impl FontFallbackChain {
907 #[inline(always)]
908 pub const fn new() -> Self {
909 Self { slots: [None; 8] }
910 }
911
912 #[inline(always)]
914 pub const fn with_script(mut self, script: ScriptKind, family: FontFamily) -> Self {
915 self.slots[script as usize] = Some(family);
916 self
917 }
918
919 #[inline]
921 pub const fn resolve_for_char(&self, ch: char, base: FontSpec) -> FontSpec {
922 let script = classify_script(ch);
923 if let Some(fam) = self.slots[script as usize] {
924 FontSpec {
925 family: fam,
926 weight: base.weight,
927 style: base.style,
928 }
929 } else {
930 base
931 }
932 }
933}
934
935pub struct InlineFlow<'a> {
938 root: crate::element::Div<'a>,
939 default_size: u8,
940 default_color: Color,
941}
942
943#[inline]
945pub fn inline_flow<'a>() -> InlineFlow<'a> {
946 use crate::style::Styled;
947 InlineFlow {
948 root: crate::element::div()
949 .flex_row()
950 .flex_wrap()
951 .items_center()
952 .gap(4),
953 default_size: 13,
954 default_color: Color::WHITE,
955 }
956}
957
958impl<'a> InlineFlow<'a> {
959 #[inline]
960 pub fn w(mut self, w: i16) -> Self {
961 use crate::style::Styled;
962 self.root = self.root.w(w);
963 self
964 }
965
966 #[inline]
967 pub fn w_full(mut self) -> Self {
968 use crate::style::Styled;
969 self.root = self.root.w_full();
970 self
971 }
972
973 #[inline]
974 pub fn gap(mut self, px: i8) -> Self {
975 use crate::style::Styled;
976 self.root = self.root.gap(px);
977 self
978 }
979
980 #[inline]
981 pub const fn default_size(mut self, px: u8) -> Self {
982 self.default_size = px;
983 self
984 }
985
986 #[inline]
987 pub const fn default_color(mut self, color: Color) -> Self {
988 self.default_color = color;
989 self
990 }
991
992 pub fn text(self, cx: &'a FrameScope, content: &'a str) -> Self {
994 let sz = self.default_size;
995 let col = self.default_color;
996 self.styled_text(cx, content, col, sz, false)
997 }
998
999 pub fn styled_text(
1001 mut self,
1002 cx: &'a FrameScope,
1003 content: &'a str,
1004 color: Color,
1005 font_size: u8,
1006 bold: bool,
1007 ) -> Self {
1008 let mut start = 0usize;
1009 let mut iter = content.char_indices().peekable();
1010 while let Some((idx, ch)) = iter.next() {
1011 let next_idx = iter.peek().map(|&(ni, _)| ni).unwrap_or(content.len());
1012 let is_cjk = matches!(
1013 classify_script(ch),
1014 ScriptKind::Cjk | ScriptKind::Emoji
1015 );
1016 if ch.is_whitespace() {
1017 if idx > start {
1018 let word = &content[start..idx];
1019 self = self.push_atom_text(cx, word, color, font_size, bold);
1020 }
1021 start = next_idx;
1022 } else if is_cjk {
1023 if idx > start {
1024 self = self.push_atom_text(cx, &content[start..idx], color, font_size, bold);
1025 }
1026 self = self.push_atom_text(cx, &content[idx..next_idx], color, font_size, bold);
1027 start = next_idx;
1028 }
1029 }
1030 if start < content.len() {
1031 self = self.push_atom_text(cx, &content[start..], color, font_size, bold);
1032 }
1033 self
1034 }
1035
1036 fn push_atom_text(
1037 mut self,
1038 cx: &'a FrameScope,
1039 word: &'a str,
1040 color: Color,
1041 font_size: u8,
1042 bold: bool,
1043 ) -> Self {
1044 if word.is_empty() {
1045 return self;
1046 }
1047 let sz = if bold { font_size.saturating_add(1) } else { font_size };
1048 let t = crate::element::text(word).size(sz).color(color).nowrap();
1049 self.root = self.root.child(cx, t);
1050 self
1051 }
1052
1053 #[inline]
1055 pub fn inline_widget(mut self, cx: &'a FrameScope, widget: impl IntoElement<'a>) -> Self {
1056 self.root = self.root.child(cx, widget);
1057 self
1058 }
1059
1060 #[inline]
1062 pub fn build(self) -> crate::element::Div<'a> {
1063 self.root
1064 }
1065}
1066
1067impl<'a> IntoElement<'a> for InlineFlow<'a> {
1068 #[inline]
1069 fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
1070 self.root.into_element(cx)
1071 }
1072}