1use harfrust::{Buffer, Direction, Feature, ShapeOptions, ShaperFont, Tag};
2
3use crate::font::color::{GlyphPaint, glyph_paint};
4use crate::font::registry::FontRegistry;
5use crate::font::resolve::{
6 FaceStyle, Presentation, ResolvedFont, face_covers, find_fallback_face,
7};
8use crate::font::script_fallback::{ScriptContext, preferred_families};
9use crate::font::synthesis;
10use crate::shaping::emoji;
11use crate::shaping::run::{ShapedGlyph, ShapedRun};
12use crate::types::FontFaceId;
13use crate::types::FontFeature;
14
15pub fn to_harfrust_features(features: &[FontFeature]) -> Vec<Feature> {
19 features
20 .iter()
21 .map(|f| Feature::new(Tag::new(&f.tag), f.value, ..))
22 .collect()
23}
24
25fn units_per_em(bytes: &[u8], face_index: u32) -> Option<u16> {
33 let font_ref = swash::FontRef::from_index(bytes, face_index as usize)?;
34 let upem = font_ref.metrics(&[]).units_per_em;
35 if upem == 0 { None } else { Some(upem) }
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
40pub enum TextDirection {
41 #[default]
43 Auto,
44 LeftToRight,
45 RightToLeft,
46}
47
48pub fn shape_text(
60 registry: &FontRegistry,
61 resolved: &ResolvedFont,
62 text: &str,
63 text_offset: usize,
64) -> Option<ShapedRun> {
65 shape_text_with_fallback(
66 registry,
67 resolved,
68 text,
69 text_offset,
70 TextDirection::Auto,
71 &[],
72 )
73}
74
75pub fn shape_text_with_fallback(
81 registry: &FontRegistry,
82 resolved: &ResolvedFont,
83 text: &str,
84 text_offset: usize,
85 direction: TextDirection,
86 features: &[Feature],
87) -> Option<ShapedRun> {
88 shape_text_in_paragraph(
89 registry,
90 resolved,
91 text,
92 text_offset,
93 direction,
94 features,
95 &ScriptContext::new(text, None),
96 0,
97 )
98}
99
100#[allow(clippy::too_many_arguments)]
105pub(crate) fn shape_text_in_paragraph(
106 registry: &FontRegistry,
107 resolved: &ResolvedFont,
108 text: &str,
109 text_offset: usize,
110 direction: TextDirection,
111 features: &[Feature],
112 context: &ScriptContext<'_>,
113 para_pos: usize,
114) -> Option<ShapedRun> {
115 let mut run = shape_text_directed_in(
116 registry,
117 resolved,
118 text,
119 text_offset,
120 direction,
121 features,
122 context.language_tag(),
123 )?;
124 blank_hard_breaks(text, &mut run);
125
126 let may_need = run.glyphs.iter().any(|g| g.glyph_id == 0) || emoji::may_contain_emoji(text);
130 let needs = if may_need {
131 fallback_needs(registry, text, &run)
132 } else {
133 Vec::new()
134 };
135 if needs.iter().any(|n| n.needed) {
136 let style = FaceStyle::of_face(registry, resolved.font_face_id, resolved.weight);
137 apply_glyph_fallback(
138 registry,
139 resolved,
140 style,
141 text,
142 text_offset,
143 features,
144 &mut run,
145 &needs,
146 &[resolved.font_face_id],
147 context,
148 para_pos,
149 0,
150 );
151 blank_hard_breaks(text, &mut run);
154 }
155
156 Some(run)
157}
158
159fn is_hard_break_glyph(text: &str, glyph: &ShapedGlyph) -> bool {
162 text.get(glyph.cluster as usize..)
163 .and_then(|rest| rest.chars().next())
164 .is_some_and(crate::types::is_hard_break)
165}
166
167fn blank_hard_breaks(text: &str, run: &mut ShapedRun) -> usize {
175 let mut count = 0;
176 for glyph in &mut run.glyphs {
177 if is_hard_break_glyph(text, glyph) {
178 glyph.glyph_id = 0;
179 glyph.x_advance = 0.0;
180 glyph.x_offset = 0.0;
181 glyph.y_offset = 0.0;
182 count += 1;
183 }
184 }
185 if count > 0 {
186 run.advance_width = run.glyphs.iter().map(|g| g.x_advance).sum();
187 }
188 count
189}
190
191const MAX_FALLBACK_DEPTH: usize = 6;
197
198#[derive(Debug, Clone, Copy)]
200struct FallbackNeed {
201 needed: bool,
203 missing: bool,
206 emoji: bool,
208}
209
210fn fallback_needs(registry: &FontRegistry, text: &str, run: &ShapedRun) -> Vec<FallbackNeed> {
214 let emoji = emoji_clusters(text, &run.glyphs);
215 run.glyphs
216 .iter()
217 .zip(emoji)
218 .map(|(glyph, emoji)| {
219 if is_hard_break_glyph(text, glyph) {
220 return FallbackNeed {
221 needed: false,
222 missing: false,
223 emoji,
224 };
225 }
226 let missing = glyph.glyph_id == 0;
227 let not_in_color = emoji
228 && !missing
229 && registry
230 .get(glyph.font_face_id)
231 .is_some_and(|entry| glyph_paint(entry, glyph.glyph_id) != GlyphPaint::Color);
232 FallbackNeed {
233 needed: missing || not_in_color,
234 missing,
235 emoji,
236 }
237 })
238 .collect()
239}
240
241fn emoji_clusters(text: &str, glyphs: &[ShapedGlyph]) -> Vec<bool> {
244 if !emoji::may_contain_emoji(text) {
245 return vec![false; glyphs.len()];
246 }
247 let mut starts: Vec<usize> = glyphs.iter().map(|g| g.cluster as usize).collect();
250 starts.sort_unstable();
251 starts.dedup();
252 glyphs
253 .iter()
254 .map(|g| {
255 let start = g.cluster as usize;
256 let end = match starts.binary_search(&start) {
257 Ok(i) => starts.get(i + 1).copied().unwrap_or(text.len()),
258 Err(_) => text.len(),
259 };
260 text.get(start..end).is_some_and(emoji::cluster_wants_emoji)
261 })
262 .collect()
263}
264
265#[allow(clippy::too_many_arguments)]
300fn apply_glyph_fallback(
301 registry: &FontRegistry,
302 primary: &ResolvedFont,
303 style: FaceStyle,
304 text: &str,
305 text_offset: usize,
306 features: &[Feature],
307 run: &mut ShapedRun,
308 needs: &[FallbackNeed],
309 tried: &[FontFaceId],
310 context: &ScriptContext<'_>,
311 para_pos: usize,
312 depth: usize,
313) {
314 let preferred = |ch: char, at: usize| {
317 preferred_families(
318 ch,
319 context,
320 para_pos + at,
321 registry.default_language(),
322 registry.fallback_platform(),
323 )
324 };
325 let mut spans: Vec<(std::ops::Range<usize>, bool)> = Vec::new();
331 let mut i = 0;
332 while i < needs.len() {
333 if !needs[i].needed {
334 i += 1;
335 continue;
336 }
337 let start = i;
338 let emoji = needs[i].emoji;
339 while i < needs.len() && needs[i].needed && needs[i].emoji == emoji {
340 i += 1;
341 }
342 spans.push((start..i, emoji));
343 }
344 if spans.is_empty() {
345 return;
346 }
347
348 for (span, emoji) in spans.into_iter().rev() {
350 let any_missing = needs[span.clone()].iter().any(|n| n.missing);
351 let Some((byte_start, byte_end)) = span_char_range(&run.glyphs, &span, text) else {
352 continue;
353 };
354 let Some(slice) = text.get(byte_start..byte_end) else {
355 continue;
356 };
357 let Some(first_char) = slice.chars().next() else {
358 continue;
359 };
360
361 let mut excluded: Vec<FontFaceId> = tried.to_vec();
364 for glyph in &run.glyphs[span.clone()] {
365 if !excluded.contains(&glyph.font_face_id) {
366 excluded.push(glyph.font_face_id);
367 }
368 }
369 let presentation = if emoji {
370 Presentation::Emoji
371 } else {
372 Presentation::Text
373 };
374 let Some((mut fallback_id, paint)) = find_fallback_face(
375 registry,
376 first_char,
377 presentation,
378 style,
379 &excluded,
380 preferred(first_char, byte_start),
381 ) else {
382 continue; };
384 if emoji && !any_missing && paint != GlyphPaint::Color {
387 continue;
388 }
389
390 let reshape = |face: FontFaceId| -> Option<(ResolvedFont, ShapedRun)> {
391 let entry = registry.get(face)?;
392 let resolved = ResolvedFont {
393 font_face_id: face,
394 size_px: primary.size_px,
395 face_index: entry.face_index,
396 swash_cache_key: entry.swash_cache_key,
397 scale_factor: primary.scale_factor,
398 weight: primary.weight,
399 italic: primary.italic,
400 };
401 let run = shape_text_directed_in(
403 registry,
404 &resolved,
405 slice,
406 text_offset + byte_start,
407 run.direction,
408 features,
409 context.language_tag(),
410 )?;
411 (!run.glyphs.is_empty()).then_some((resolved, run))
412 };
413 let Some((mut fallback_resolved, mut fallback_run)) = reshape(fallback_id) else {
414 continue;
415 };
416 blank_hard_breaks(slice, &mut fallback_run);
417
418 excluded.push(fallback_id);
419 let mut still = fallback_needs(registry, slice, &fallback_run);
420 if !emoji
421 && let Some((at, lacking)) = fallback_run
422 .glyphs
423 .iter()
424 .zip(&still)
425 .find(|(_, n)| n.missing)
426 .and_then(|(g, _)| {
427 let at = g.cluster as usize;
428 Some((at, slice.get(at..)?.chars().next()?))
429 })
430 && let Some((whole_id, _)) = find_fallback_face(
431 registry,
432 lacking,
433 Presentation::Text,
434 style,
435 &excluded,
436 preferred(lacking, byte_start + at),
437 )
438 && face_covers(registry, whole_id, slice)
439 && let Some((resolved, mut whole_run)) = reshape(whole_id)
440 {
441 blank_hard_breaks(slice, &mut whole_run);
442 fallback_id = whole_id;
443 fallback_resolved = resolved;
444 fallback_run = whole_run;
445 excluded.push(fallback_id);
446 still = fallback_needs(registry, slice, &fallback_run);
447 }
448
449 if depth < MAX_FALLBACK_DEPTH && still.iter().any(|n| n.needed) {
450 apply_glyph_fallback(
451 registry,
452 &fallback_resolved,
453 style,
454 slice,
455 text_offset + byte_start,
456 features,
457 &mut fallback_run,
458 &still,
459 &excluded,
460 context,
461 para_pos + byte_start,
462 depth + 1,
463 );
464 }
465
466 let replacement: Vec<ShapedGlyph> = fallback_run
470 .glyphs
471 .into_iter()
472 .map(|mut g| {
473 g.cluster += byte_start as u32;
474 g
475 })
476 .collect();
477
478 run.glyphs.splice(span, replacement);
479 }
480
481 run.advance_width = run.glyphs.iter().map(|g| g.x_advance).sum();
482}
483
484fn span_char_range(
494 glyphs: &[ShapedGlyph],
495 span: &std::ops::Range<usize>,
496 text: &str,
497) -> Option<(usize, usize)> {
498 let inside = glyphs.get(span.clone())?;
499 let start = inside.iter().map(|g| g.cluster as usize).min()?;
500 let last = inside.iter().map(|g| g.cluster as usize).max()?;
501
502 let end = glyphs
503 .iter()
504 .enumerate()
505 .filter(|(i, _)| !span.contains(i))
506 .map(|(_, g)| g.cluster as usize)
507 .filter(|&c| c > last)
508 .min()
509 .unwrap_or(text.len());
510
511 if start >= end || !text.is_char_boundary(start) || !text.is_char_boundary(end) {
512 return None;
513 }
514 Some((start, end))
515}
516
517pub fn shape_text_directed(
519 registry: &FontRegistry,
520 resolved: &ResolvedFont,
521 text: &str,
522 text_offset: usize,
523 direction: TextDirection,
524 features: &[Feature],
525) -> Option<ShapedRun> {
526 shape_text_directed_in(
527 registry,
528 resolved,
529 text,
530 text_offset,
531 direction,
532 features,
533 None,
534 )
535}
536
537fn shaping_language(tag: Option<&str>) -> Option<harfrust::Language> {
543 tag.map(str::trim)
544 .filter(|t| !t.is_empty())
545 .and_then(|t| t.parse::<harfrust::Language>().ok())
546}
547
548#[allow(clippy::too_many_arguments)]
550pub(crate) fn shape_text_directed_in(
551 registry: &FontRegistry,
552 resolved: &ResolvedFont,
553 text: &str,
554 text_offset: usize,
555 direction: TextDirection,
556 features: &[Feature],
557 language: Option<&str>,
558) -> Option<ShapedRun> {
559 let entry = registry.get(resolved.font_face_id)?;
560 let font = entry.shaping_font()?;
561
562 let upem = units_per_em(entry.bytes(), entry.face_index).unwrap_or(0) as f32;
563 if upem == 0.0 {
564 return None;
565 }
566 let sf = resolved.scale_factor.max(f32::MIN_POSITIVE);
569 let physical_size = resolved.size_px * sf;
570 let physical_scale = physical_size / upem;
571 let inv_sf = 1.0 / sf;
572
573 let mut buffer = Buffer::new();
574 buffer.push_str(text);
575 match direction {
576 TextDirection::LeftToRight => buffer.set_direction(Direction::LeftToRight),
577 TextDirection::RightToLeft => buffer.set_direction(Direction::RightToLeft),
578 TextDirection::Auto => {}
579 }
580 buffer.set_language(shaping_language(language));
582 buffer.guess_segment_properties();
594
595 let resolved_direction = if buffer.direction() == Direction::RightToLeft {
598 TextDirection::RightToLeft
599 } else {
600 TextDirection::LeftToRight
601 };
602
603 harfrust::shape(
607 &ShaperFont::new(font),
608 &mut buffer,
609 ShapeOptions::new().features(features),
610 )
611 .ok()?;
612
613 let infos = buffer.glyph_infos();
614 let positions = buffer.glyph_positions();
615
616 let mut glyphs = Vec::with_capacity(infos.len());
617 let mut total_advance = 0.0f32;
618
619 for (info, pos) in infos.iter().zip(positions.iter()) {
620 let x_advance = pos.x_advance as f32 * physical_scale * inv_sf;
621 let y_advance = pos.y_advance as f32 * physical_scale * inv_sf;
622 let x_offset = pos.x_offset as f32 * physical_scale * inv_sf;
623 let y_offset = pos.y_offset as f32 * physical_scale * inv_sf;
624
625 glyphs.push(ShapedGlyph {
626 glyph_id: info.glyph_id as u16,
627 cluster: info.cluster,
628 x_advance,
629 y_advance,
630 x_offset,
631 y_offset,
632 font_face_id: resolved.font_face_id,
633 });
634
635 total_advance += x_advance;
636 }
637 if entry.synthesis().synthesizes_bold(resolved.weight) {
638 total_advance = widen_for_synthetic_bold(&mut glyphs, resolved.size_px);
639 }
640
641 Some(ShapedRun {
642 font_face_id: resolved.font_face_id,
643 size_px: resolved.size_px,
644 weight: resolved.weight,
645 italic: resolved.italic,
646 glyphs,
647 advance_width: total_advance,
648 text_range: text_offset..text_offset + text.len(),
649 direction: resolved_direction,
650 bidi_level: if resolved_direction == TextDirection::RightToLeft {
651 1
652 } else {
653 0
654 },
655 underline_style: crate::types::UnderlineStyle::None,
656 overline: false,
657 strikeout: false,
658 is_link: false,
659 foreground_color: None,
660 underline_color: None,
661 background_color: None,
662 anchor_href: None,
663 tooltip: None,
664 vertical_alignment: crate::types::VerticalAlignment::Normal,
665 image_name: None,
666 image_height: 0.0,
667 })
668}
669
670pub fn shape_text_with_buffer(
672 registry: &FontRegistry,
673 resolved: &ResolvedFont,
674 text: &str,
675 text_offset: usize,
676 buffer: Buffer,
677 features: &[Feature],
678) -> Option<(ShapedRun, Buffer)> {
679 let entry = registry.get(resolved.font_face_id)?;
680 let font = entry.shaping_font()?;
681
682 let upem = units_per_em(entry.bytes(), entry.face_index).unwrap_or(0) as f32;
683 if upem == 0.0 {
684 return None;
685 }
686 let sf = resolved.scale_factor.max(f32::MIN_POSITIVE);
687 let physical_size = resolved.size_px * sf;
688 let physical_scale = physical_size / upem;
689 let inv_sf = 1.0 / sf;
690
691 let mut buffer = buffer;
692 buffer.push_str(text);
693 buffer.guess_segment_properties();
696
697 let resolved_direction = if buffer.direction() == Direction::RightToLeft {
698 TextDirection::RightToLeft
699 } else {
700 TextDirection::LeftToRight
701 };
702
703 harfrust::shape(
704 &ShaperFont::new(font),
705 &mut buffer,
706 ShapeOptions::new().features(features),
707 )
708 .ok()?;
709
710 let infos = buffer.glyph_infos();
711 let positions = buffer.glyph_positions();
712
713 let mut glyphs = Vec::with_capacity(infos.len());
714 let mut total_advance = 0.0f32;
715
716 for (info, pos) in infos.iter().zip(positions.iter()) {
717 let x_advance = pos.x_advance as f32 * physical_scale * inv_sf;
718 let y_advance = pos.y_advance as f32 * physical_scale * inv_sf;
719 let x_offset = pos.x_offset as f32 * physical_scale * inv_sf;
720 let y_offset = pos.y_offset as f32 * physical_scale * inv_sf;
721
722 glyphs.push(ShapedGlyph {
723 glyph_id: info.glyph_id as u16,
724 cluster: info.cluster,
725 x_advance,
726 y_advance,
727 x_offset,
728 y_offset,
729 font_face_id: resolved.font_face_id,
730 });
731
732 total_advance += x_advance;
733 }
734 if entry.synthesis().synthesizes_bold(resolved.weight) {
735 total_advance = widen_for_synthetic_bold(&mut glyphs, resolved.size_px);
736 }
737
738 let run = ShapedRun {
739 font_face_id: resolved.font_face_id,
740 size_px: resolved.size_px,
741 weight: resolved.weight,
742 italic: resolved.italic,
743 glyphs,
744 advance_width: total_advance,
745 text_range: text_offset..text_offset + text.len(),
746 direction: resolved_direction,
747 bidi_level: if resolved_direction == TextDirection::RightToLeft {
748 1
749 } else {
750 0
751 },
752 underline_style: crate::types::UnderlineStyle::None,
753 overline: false,
754 strikeout: false,
755 is_link: false,
756 foreground_color: None,
757 underline_color: None,
758 background_color: None,
759 anchor_href: None,
760 tooltip: None,
761 vertical_alignment: crate::types::VerticalAlignment::Normal,
762 image_name: None,
763 image_height: 0.0,
764 };
765 let mut run = run;
766 blank_hard_breaks(text, &mut run);
767
768 buffer.clear();
771 Some((run, buffer))
772}
773
774fn widen_for_synthetic_bold(glyphs: &mut [ShapedGlyph], size_px: f32) -> f32 {
779 let extra = synthesis::embolden_strength(size_px);
780 for glyph in glyphs.iter_mut().filter(|g| g.x_advance > 0.0) {
781 glyph.x_advance += extra;
782 }
783 glyphs.iter().map(|g| g.x_advance).sum()
784}
785
786#[non_exhaustive]
787pub struct FontMetricsPx {
788 pub ascent: f32,
789 pub descent: f32,
790 pub leading: f32,
791 pub underline_offset: f32,
792 pub strikeout_offset: f32,
793 pub stroke_size: f32,
794}
795
796pub fn font_metrics_px(registry: &FontRegistry, resolved: &ResolvedFont) -> Option<FontMetricsPx> {
801 let entry = registry.get(resolved.font_face_id)?;
802 let font_ref = swash::FontRef::from_index(entry.bytes(), entry.face_index as usize)?;
803 let sf = resolved.scale_factor.max(f32::MIN_POSITIVE);
804 let physical_size = resolved.size_px * sf;
805 let metrics = font_ref.metrics(&[]).scale(physical_size);
806 let inv_sf = 1.0 / sf;
807
808 Some(FontMetricsPx {
809 ascent: metrics.ascent * inv_sf,
810 descent: metrics.descent * inv_sf,
811 leading: metrics.leading * inv_sf,
812 underline_offset: metrics.underline_offset * inv_sf,
813 strikeout_offset: metrics.strikeout_offset * inv_sf,
814 stroke_size: metrics.stroke_size * inv_sf,
815 })
816}
817
818#[non_exhaustive]
820pub struct BidiRun {
821 pub byte_range: std::ops::Range<usize>,
822 pub direction: TextDirection,
823 pub visual_order: usize,
825 pub level: u8,
833}
834
835#[non_exhaustive]
845pub struct BidiParagraph {
846 pub runs: Vec<BidiRun>,
848 pub para_level: u8,
851}
852
853impl BidiParagraph {
854 pub fn base_direction(&self) -> TextDirection {
856 if self.para_level % 2 == 1 {
857 TextDirection::RightToLeft
858 } else {
859 TextDirection::LeftToRight
860 }
861 }
862}
863
864fn base_para_level(base: TextDirection) -> Option<unicode_bidi::Level> {
873 match base {
874 TextDirection::Auto => None,
875 TextDirection::LeftToRight => Some(unicode_bidi::Level::ltr()),
876 TextDirection::RightToLeft => Some(unicode_bidi::Level::rtl()),
877 }
878}
879
880pub fn resolve_paragraph_direction(
891 text: &str,
892 base: TextDirection,
893 neutral: TextDirection,
894) -> TextDirection {
895 match base {
896 TextDirection::LeftToRight | TextDirection::RightToLeft => base,
897 TextDirection::Auto => match unicode_bidi::get_base_direction(text) {
898 unicode_bidi::Direction::Ltr => TextDirection::LeftToRight,
899 unicode_bidi::Direction::Rtl => TextDirection::RightToLeft,
900 unicode_bidi::Direction::Mixed => match neutral {
901 TextDirection::RightToLeft => TextDirection::RightToLeft,
902 _ => TextDirection::LeftToRight,
903 },
904 },
905 }
906}
907
908pub fn analyze_paragraph_with_neutral(
912 text: &str,
913 base: TextDirection,
914 neutral: TextDirection,
915) -> BidiParagraph {
916 let base = match (base, neutral) {
917 (TextDirection::Auto, TextDirection::RightToLeft) => {
918 resolve_paragraph_direction(text, base, neutral)
919 }
920 _ => base,
921 };
922 analyze_paragraph(text, base)
923}
924
925pub fn analyze_paragraph(text: &str, base: TextDirection) -> BidiParagraph {
931 use unicode_bidi::BidiInfo;
932
933 if text.is_empty() {
934 return BidiParagraph {
935 runs: Vec::new(),
936 para_level: base_para_level(base).map_or(0, |l| l.number()),
937 };
938 }
939
940 let bidi_info = BidiInfo::new(text, base_para_level(base));
941
942 let para_level = bidi_info
946 .paragraphs
947 .first()
948 .map(|p| p.level.number())
949 .or_else(|| base_para_level(base).map(|l| l.number()))
950 .unwrap_or(0);
951
952 let mut starts: Vec<(usize, u8)> = Vec::new();
956 for (idx, _) in text.char_indices() {
957 let level = bidi_info.levels[idx].number();
958 if starts.last().map(|&(_, l)| l) != Some(level) {
959 starts.push((idx, level));
960 }
961 }
962
963 let ends = starts
965 .iter()
966 .skip(1)
967 .map(|&(start, _)| start)
968 .chain(std::iter::once(text.len()));
969
970 let runs: Vec<BidiRun> = starts
971 .iter()
972 .zip(ends)
973 .map(|(&(start, level), end)| BidiRun {
974 byte_range: start..end,
975 direction: if level % 2 == 1 {
976 TextDirection::RightToLeft
977 } else {
978 TextDirection::LeftToRight
979 },
980 visual_order: 0,
986 level,
987 })
988 .collect();
989
990 BidiParagraph { runs, para_level }
991}
992
993pub fn visual_order(levels: &[u8]) -> Vec<usize> {
1000 let mut order: Vec<usize> = (0..levels.len()).collect();
1001 let Some(&max) = levels.iter().max() else {
1002 return order;
1003 };
1004 let Some(min_odd) = levels.iter().copied().filter(|l| l % 2 == 1).min() else {
1006 return order;
1007 };
1008
1009 let mut level = max;
1010 while level >= min_odd {
1011 let mut i = 0;
1012 while i < order.len() {
1013 if levels[order[i]] >= level {
1014 let start = i;
1015 while i < order.len() && levels[order[i]] >= level {
1016 i += 1;
1017 }
1018 order[start..i].reverse();
1019 } else {
1020 i += 1;
1021 }
1022 }
1023 level -= 1;
1025 }
1026 order
1027}
1028
1029pub fn bidi_runs(text: &str) -> Vec<BidiRun> {
1037 use unicode_bidi::BidiInfo;
1038
1039 if text.is_empty() {
1040 return Vec::new();
1041 }
1042
1043 let bidi_info = BidiInfo::new(text, None);
1044 let mut runs = Vec::new();
1045
1046 for para in &bidi_info.paragraphs {
1047 let (levels, level_runs) = bidi_info.visual_runs(para, para.range.clone());
1048 for level_run in level_runs {
1049 if level_run.is_empty() {
1050 continue;
1051 }
1052 let level = levels[level_run.start];
1053 let direction = if level.is_rtl() {
1054 TextDirection::RightToLeft
1055 } else {
1056 TextDirection::LeftToRight
1057 };
1058 let visual_order = runs.len();
1059 runs.push(BidiRun {
1060 byte_range: level_run,
1061 direction,
1062 visual_order,
1063 level: level.number(),
1064 });
1065 }
1066 }
1067
1068 if runs.is_empty() {
1069 runs.push(BidiRun {
1070 byte_range: 0..text.len(),
1071 direction: TextDirection::LeftToRight,
1072 visual_order: 0,
1073 level: 0,
1074 });
1075 }
1076
1077 runs
1078}
1079
1080#[cfg(test)]
1081mod bidi_tests {
1082 use super::*;
1083
1084 const ARABIC: &str = "\u{0643}\u{062A}\u{0628}"; const HEBREW: &str = "\u{05E9}\u{05DC}\u{05D5}\u{05DD}"; #[test]
1088 fn rule_l2_leaves_all_ltr_text_alone() {
1089 assert_eq!(visual_order(&[0, 0, 0]), vec![0, 1, 2]);
1090 assert_eq!(visual_order(&[]), Vec::<usize>::new());
1091 }
1092
1093 #[test]
1094 fn rule_l2_reverses_a_run_of_rtl() {
1095 assert_eq!(visual_order(&[0, 1, 1, 0]), vec![0, 2, 1, 3]);
1098 }
1099
1100 #[test]
1101 fn rule_l2_nests_an_ltr_island_inside_rtl() {
1102 let levels = [0, 1, 2, 2, 1, 0];
1107 assert_eq!(visual_order(&levels), vec![0, 4, 2, 3, 1, 5]);
1108 }
1109
1110 #[test]
1111 fn rule_l2_reverses_the_whole_line_in_an_rtl_paragraph() {
1112 assert_eq!(visual_order(&[1, 2, 1]), vec![2, 1, 0]);
1115 }
1116
1117 #[test]
1118 fn a_leading_digit_does_not_fool_auto_detection() {
1119 let text = "123 \u{0643}\u{062A}\u{0628}";
1124 assert_eq!(analyze_paragraph(text, TextDirection::Auto).para_level, 1);
1125 }
1126
1127 #[test]
1128 fn an_explicit_base_direction_overrides_first_strong_detection() {
1129 let text = "NASA \u{0623}\u{0639}\u{0644}\u{0646}\u{062A}";
1134 assert_eq!(
1135 analyze_paragraph(text, TextDirection::Auto).para_level,
1136 0,
1137 "auto-detection is expected to get this one wrong"
1138 );
1139
1140 let forced = analyze_paragraph(text, TextDirection::RightToLeft);
1141 assert_eq!(forced.para_level, 1);
1142 assert_eq!(forced.base_direction(), TextDirection::RightToLeft);
1143
1144 let auto = analyze_paragraph(text, TextDirection::Auto);
1149 let first_visual = |p: &BidiParagraph| {
1150 let levels: Vec<u8> = p.runs.iter().map(|r| r.level).collect();
1151 visual_order(&levels).first().map(|&i| p.runs[i].direction)
1152 };
1153 assert_eq!(first_visual(&auto), Some(TextDirection::LeftToRight));
1154 assert_eq!(first_visual(&forced), Some(TextDirection::RightToLeft));
1155 }
1156
1157 #[test]
1158 fn pure_arabic_auto_detects_as_rtl() {
1159 let para = analyze_paragraph(ARABIC, TextDirection::Auto);
1160 assert_eq!(para.base_direction(), TextDirection::RightToLeft);
1161 assert_eq!(para.runs.len(), 1);
1162 assert_eq!(para.runs[0].direction, TextDirection::RightToLeft);
1163 assert_eq!(para.runs[0].byte_range, 0..ARABIC.len());
1164 }
1165
1166 #[test]
1167 fn runs_come_back_in_logical_order_and_cover_the_text() {
1168 let text = format!("hello {HEBREW} world");
1170 let para = analyze_paragraph(&text, TextDirection::Auto);
1171
1172 assert!(para.runs.len() >= 2, "expected a directional split");
1173 assert_eq!(para.runs[0].byte_range.start, 0);
1174 assert_eq!(para.runs.last().unwrap().byte_range.end, text.len());
1175 for pair in para.runs.windows(2) {
1176 assert_eq!(
1177 pair[0].byte_range.end, pair[1].byte_range.start,
1178 "runs must tile the text with no gap or overlap"
1179 );
1180 assert!(
1181 pair[0].byte_range.start < pair[1].byte_range.start,
1182 "runs must be in logical order"
1183 );
1184 }
1185 assert!(
1186 para.runs
1187 .iter()
1188 .any(|r| r.direction == TextDirection::RightToLeft),
1189 "the Hebrew span should have produced an RTL run"
1190 );
1191 }
1192
1193 #[test]
1194 fn run_boundaries_never_split_a_multibyte_character() {
1195 let text = format!("a{ARABIC}b{HEBREW}c");
1196 for run in analyze_paragraph(&text, TextDirection::Auto).runs {
1197 assert!(
1198 text.is_char_boundary(run.byte_range.start)
1199 && text.is_char_boundary(run.byte_range.end),
1200 "run {:?} splits a character in {text:?}",
1201 run.byte_range
1202 );
1203 }
1204 }
1205
1206 #[test]
1207 fn rule_l2_over_a_real_paragraph_is_a_permutation() {
1208 let text = format!("hello {HEBREW} world {ARABIC} end");
1209 let para = analyze_paragraph(&text, TextDirection::Auto);
1210 let levels: Vec<u8> = para.runs.iter().map(|r| r.level).collect();
1211
1212 let mut seen = visual_order(&levels);
1213 seen.sort_unstable();
1214 assert_eq!(
1215 seen,
1216 (0..para.runs.len()).collect::<Vec<_>>(),
1217 "every run must appear exactly once in the visual order"
1218 );
1219 }
1220
1221 #[test]
1222 fn empty_text_analyzes_without_panicking() {
1223 let para = analyze_paragraph("", TextDirection::RightToLeft);
1224 assert!(para.runs.is_empty());
1225 assert_eq!(para.para_level, 1);
1226 }
1227}
1228
1229#[cfg(test)]
1230mod language_tests {
1231 use super::*;
1232 use crate::font::resolve::resolve_font;
1233
1234 const NOTO_SANS: &[u8] = include_bytes!("../../test-fonts/NotoSans-Variable.ttf");
1235
1236 fn glyphs(text: &str, language: Option<&str>) -> Vec<u16> {
1237 let mut registry = FontRegistry::new_without_system_fonts();
1238 let face = registry.register_font(NOTO_SANS)[0];
1239 registry.set_default_font(face, 16.0);
1240 let resolved = resolve_font(®istry, None, None, None, None, None, 1.0, 1.0).unwrap();
1241 let run = shape_text_in_paragraph(
1242 ®istry,
1243 &resolved,
1244 text,
1245 0,
1246 TextDirection::Auto,
1247 &[],
1248 &ScriptContext::in_language(text, language),
1249 0,
1250 )
1251 .unwrap();
1252 run.glyphs.iter().map(|g| g.glyph_id).collect()
1253 }
1254
1255 #[test]
1256 fn the_shaper_is_told_the_language() {
1257 let text = "\u{015F}\u{0163}";
1261 let none = glyphs(text, None);
1262 assert_eq!(glyphs(text, Some("en")), none);
1263 assert_ne!(glyphs(text, Some("ro")), none, "Romanian forms");
1264 assert_eq!(glyphs(text, Some("ro-RO")), glyphs(text, Some("ro")));
1265 }
1266
1267 #[test]
1268 fn a_tag_the_shaper_cannot_read_is_no_language() {
1269 assert_eq!(shaping_language(None), None);
1270 assert_eq!(shaping_language(Some(" ")), None);
1271 assert!(shaping_language(Some("sr-Cyrl")).is_some());
1272 }
1273}