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