Skip to main content

text_typeset/shaping/
shaper.rs

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
16/// Convert public [`FontFeature`] toggles into harfrust [`Feature`]s,
17/// applied across the whole shaped string (global range). Script-mandated
18/// features apply regardless; these are the discretionary toggles.
19pub 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
26/// Read units-per-em for a font face.
27///
28/// `harfrust::FontRef` is a thin wrapper over read-fonts and exposes
29/// the `head` table only through the `read_fonts::TableProvider` trait,
30/// which harfrust doesn't re-export. Since we already depend on swash
31/// for `font_metrics_px` further down, we reuse swash's `Metrics` to
32/// pull UPEM — one less dependency surface to maintain.
33fn 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/// Text direction for shaping.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
41pub enum TextDirection {
42    /// Auto-detect from text content (default).
43    #[default]
44    Auto,
45    LeftToRight,
46    RightToLeft,
47}
48
49/// Shape a text string with the given resolved font.
50///
51/// Returns a ShapedRun with glyph IDs and pixel-space positions.
52/// The `text_offset` is the byte offset of this text within the block
53/// (used for cluster mapping back to document positions).
54/// Shape a text string with automatic glyph fallback.
55///
56/// After shaping with the primary font, any .notdef glyphs (glyph_id==0)
57/// are detected and re-shaped with fallback fonts. If no fallback font
58/// covers a character, it remains as .notdef (renders as blank space
59/// with correct advance).
60pub 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
76/// Shape text with an explicit direction and glyph fallback.
77///
78/// Like `shape_text`, but caller supplies the direction instead of letting
79/// rustybuzz guess. Used by the bidi-aware layout path, which splits text
80/// into directional runs before shaping.
81pub 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/// [`shape_text_with_fallback`] for text that is part of a paragraph: the
102/// paragraph and the language its block is in (`context`), and the byte
103/// where `text` starts in the paragraph (`para_pos`), decide which faces
104/// fallback prefers for Han (see [`crate::font::script_fallback`]).
105#[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    // Look for another face for every glyph the primary font lacks (a hard
128    // break is glyph 0 on purpose and needs none) and every emoji it does not
129    // draw in colour.
130    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        // A break beside a character the primary font lacks is re-shaped with
153        // it, in a font that may well draw it.
154        blank_hard_breaks(text, &mut run);
155    }
156
157    Some(run)
158}
159
160/// Whether `glyph` stands for a hard line break of `text`, the text its run was shaped
161/// from.
162fn 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
168/// Lay out every hard line break in `run` as nothing: glyph 0, which is never
169/// drawn, with no advance and no offset (see [`crate::types::is_hard_break`]).
170/// Returns how many there are.
171///
172/// A font draws a line separator as it likes (a box, a pilcrow) or not at all,
173/// and gives it whatever advance it likes, which pushed a right-aligned or
174/// centred line off its place by that much.
175fn 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
192/// How deep fallback goes: the part of a span its fallback face does not
193/// draw is handed to another face, and what that one cannot draw to another,
194/// each time with every face tried so far left out. A span of one script
195/// takes one face and a mixed one (Han then Hangul, say) a second; the cap
196/// only bounds a pathological font set.
197const MAX_FALLBACK_DEPTH: usize = 6;
198
199/// What fallback makes of one glyph of a shaped run.
200#[derive(Debug, Clone, Copy)]
201struct FallbackNeed {
202    /// Whether another face is looked for.
203    needed: bool,
204    /// Whether the glyph has no glyph at all (`.notdef`), as against one that
205    /// is drawn but not as it should be (an emoji drawn in monochrome).
206    missing: bool,
207    /// Whether the glyph's cluster asks for emoji presentation.
208    emoji: bool,
209}
210
211/// Decide, glyph by glyph, which glyphs of `run` (shaped from `text`) want
212/// another face: those with no glyph, and those of an emoji cluster drawn
213/// in anything but colour. A hard break needs none.
214fn 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
242/// For each glyph of `glyphs` (shaped from `text`), whether its cluster asks
243/// for emoji presentation (see [`crate::shaping::emoji`]).
244fn emoji_clusters(text: &str, glyphs: &[ShapedGlyph]) -> Vec<bool> {
245    if !emoji::may_contain_emoji(text) {
246        return vec![false; glyphs.len()];
247    }
248    // A cluster runs from its own start to the next start in the text, which
249    // works for an RTL run too, whose glyphs carry descending clusters.
250    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/// Re-shape the glyphs of `run` that need another face in fallback fonts.
267///
268/// Works on **spans** of consecutive glyphs that need one, not on glyphs one
269/// at a time: each span is mapped back to the character range that produced
270/// it and that whole range is re-shaped in the fallback font, then spliced
271/// in.
272///
273/// Shaping a span as a unit is what makes complex scripts survive fallback.
274/// Re-shaping character by character, which this used to do, cannot join
275/// Arabic, because a letter shaped alone has no neighbours and can only take
276/// its isolated form. It also kept only the *first* glyph of each result, so
277/// any letter whose isolated form is a base plus a mark lost the mark. An
278/// Arabic word set in a font without Arabic came out as disconnected,
279/// dotless stumps.
280///
281/// A span is cut where emoji presentation starts or stops, so that an emoji
282/// after CJK text gets a face of its own rather than the CJK font's
283/// `.notdef`. The span is re-shaped in the face found for its first
284/// character, and whatever that face does not draw goes through the same
285/// steps again, with every face tried so far left out: before, a span was
286/// shaped in one face and what that face lacked stayed blank. When the face
287/// found for the first character a span's face lacks draws the whole span,
288/// the span is set in that face instead, so that a word is not split
289/// between two CJK fonts of different designs.
290///
291/// Every face is chosen to match `style`, the weight, style and width of the
292/// text (see [`find_fallback_face`]), and for kana, hangul and Han among the
293/// faces the system ships for their language first, which for Han is found
294/// in `context`, the paragraph `text` starts at byte `para_pos` of (see
295/// [`crate::font::script_fallback`]).
296///
297/// A span is re-shaped with the run's own direction rather than `Auto`, so
298/// the substituted glyphs come back in the same visual order as the ones
299/// around them.
300#[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    // The faces the fallback table prefers for the character at byte `at` of
316    // `text`.
317    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    // Maximal spans of consecutive glyphs that need a face, cut where emoji
327    // presentation changes. A hard break needs none and so ends a span:
328    // inside one it would choose the span's font, since a span is re-shaped
329    // in the font that draws its first character, and a character beside it
330    // would lose its own.
331    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    // Splice from the back so earlier ranges stay valid as lengths change.
350    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        // An emoji drawn in monochrome is left out with the faces tried
363        // already, so the search is for a face that draws it otherwise.
364        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; // no fallback available: leave the span as it is
384        };
385        // An emoji already drawn, if in monochrome, is only worth replacing
386        // by one drawn in colour.
387        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            // Clusters come back local to `slice`.
403            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        // Lift the clusters from `slice` into `text` space, which is what the
468        // rest of the pipeline expects of this run. Each glyph keeps the face
469        // it was shaped in, which deeper fallback may have changed.
470        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
485/// The byte range of `text` that a span of consecutive glyphs covers.
486///
487/// Glyphs sit in visual order, so clusters ascend across an LTR run and
488/// descend across an RTL one. Taking the min and max over the span rather
489/// than its first and last glyph keeps this direction-agnostic.
490///
491/// The span's end is the nearest cluster *after* it among the glyphs outside
492/// it (the start of whatever follows it in the text), or the end of the text
493/// when the span runs to the edge.
494fn 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
518/// Shape text with an explicit direction.
519pub 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
538/// The language the shaper is told `tag` (a BCP 47 tag) names, `None` for
539/// no tag. The shaper picks a font's language-specific forms with it
540/// (OpenType `locl`: Serbian and Bulgarian Cyrillic, Romanian comma-below
541/// letters, the Persian and Urdu forms of Arabic letters and digits, a
542/// pan-CJK face's regional glyphs).
543fn 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/// [`shape_text_directed`] for text in `language`, a BCP 47 tag.
550#[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    // Shape at physical ppem, then divide results by scale_factor so
568    // downstream layout stays in logical pixels. See ResolvedFont.
569    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    // Set before the guess below, which leaves a language it finds set.
582    buffer.set_language(shaping_language(language));
583    // Always guess, including after an explicit set_direction. The guess
584    // only fills in what is still unset — it assigns `script` when it is
585    // `None` and `direction` when it is `Invalid` — so the caller's
586    // direction survives untouched and the script gets populated.
587    //
588    // That script is what picks the shaper: with `script: None` harfrust
589    // falls back to DEFAULT_SHAPER, which never requests init/medi/fina/
590    // isol, and Arabic comes out in disconnected isolated forms. Setting
591    // it selects ARABIC_SHAPER and the letters join. It also keeps
592    // harfrust from panicking on a still-Invalid direction in the Auto
593    // case, which is why this call used to live in that branch alone.
594    buffer.guess_segment_properties();
595
596    // Resolve the concrete direction (Auto is now decided by the buffer).
597    // Stored on the run so hit-testing knows RTL glyph order.
598    let resolved_direction = if buffer.direction() == Direction::RightToLeft {
599        TextDirection::RightToLeft
600    } else {
601        TextDirection::LeftToRight
602    };
603
604    // The font is opened once per face and cached on the FontEntry, and
605    // it keeps its preprocessed tables, so repeated shape calls (every
606    // relayout/keystroke) reuse them; the ShaperFont over it is cheap.
607    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
674/// Shape a text string, reusing a harfrust `Buffer` to avoid allocations.
675pub 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    // Recycled buffers come back without segment properties; explicitly
698    // guess them so harfrust doesn't panic on Direction::Invalid.
699    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    // Reclaim the buffer for reuse: clearing keeps the allocation and
776    // drops the contents and segment properties.
777    buffer.clear();
778    Some((run, buffer))
779}
780
781/// [`GlyphRendering::Crisp`]: give every glyph its hinted advance in whole
782/// device pixels, keeping what the shaper added to the design advance
783/// (kerning, synthetic bold) rounded to a pixel, and round its offsets to a
784/// pixel too, so the layout measures the text as it is drawn. A glyph the
785/// hinter cannot fit keeps its advance rounded. Returns the run's advance.
786fn 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
815/// Widen every advancing glyph by what synthetic bold adds to its outline
816/// (see [`crate::font::synthesis`]), so the layout, the caret and hit testing
817/// measure the glyph as the rasterizer draws it. Returns the run's new
818/// advance.
819fn 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
837/// Get font metrics (ascent, descent, leading) scaled to logical pixels.
838///
839/// Scales at `size_px * scale_factor` (physical) and divides by
840/// `scale_factor`, so callers always see logical-pixel metrics.
841pub 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/// A bidi run: a contiguous range of text with the same direction.
860#[non_exhaustive]
861pub struct BidiRun {
862    pub byte_range: std::ops::Range<usize>,
863    pub direction: TextDirection,
864    /// Visual order index (for reordering after line breaking).
865    pub visual_order: usize,
866    /// UAX #9 embedding level: even is LTR, odd is RTL.
867    ///
868    /// Rule L2 reorders by level, not by direction, and the two are not
869    /// interchangeable: a Latin phrase quoted inside Arabic inside an
870    /// English paragraph sits at level 2, and reordering it as though it
871    /// were level 0 puts it on the wrong side of the Arabic. `direction`
872    /// cannot tell those apart — `level` can.
873    pub level: u8,
874}
875
876/// A paragraph's bidi structure in **logical** order.
877///
878/// [`bidi_runs`] returns runs already reordered for display, which suits
879/// a single-line label that is shaped and painted in one go. The
880/// multi-line editor cannot use that: line breaking is a logical
881/// operation, so runs have to stay in logical order until the breaker has
882/// decided where the lines fall, and only then get reordered *per line*.
883/// Reordering the paragraph up front and breaking afterwards would
884/// scramble any paragraph that wraps.
885#[non_exhaustive]
886pub struct BidiParagraph {
887    /// Runs of uniform embedding level, in logical order.
888    pub runs: Vec<BidiRun>,
889    /// The paragraph embedding level actually used — the explicit base
890    /// direction when one was given, else the rule P2/P3 auto-detection.
891    pub para_level: u8,
892}
893
894impl BidiParagraph {
895    /// The paragraph's base direction, as resolved.
896    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
905/// The unicode-bidi paragraph level that expresses `base`.
906///
907/// `None` asks unicode-bidi to auto-detect (rules P2/P3: the first strong
908/// character wins, defaulting to LTR when the text has none). An explicit
909/// level overrides that — which is the point of honouring a stored
910/// paragraph direction, since P2/P3 incorrectly classifies any RTL
911/// paragraph that happens to open with a digit, a Latin acronym or an
912/// opening quote.
913fn 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
921/// The base direction a paragraph of `text` takes.
922///
923/// An explicit `base` is the answer. `Auto` applies UAX #9 rules P2/P3: the
924/// first strongly directional character outside an isolate decides. When
925/// the text has none (it is empty, or only digits, spaces and punctuation),
926/// `neutral` decides, which is how a host gives an empty field the
927/// direction of the language it is for; the algorithm's own answer there is
928/// left to right, which is what `Auto` and `LeftToRight` give.
929///
930/// Never returns `Auto`.
931pub 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
949/// [`analyze_paragraph`] for a paragraph whose `Auto` direction falls back
950/// on `neutral` when the text has no strongly directional character (see
951/// [`resolve_paragraph_direction`]).
952pub 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
966/// Resolve `text`'s bidi structure under an explicit or auto base direction.
967///
968/// Runs come back in logical order, each tagged with its embedding level.
969/// Callers shape each run with its own direction and later reorder the
970/// runs of each line with [`visual_order`].
971pub 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    // A block is one paragraph as far as layout is concerned; if the text
984    // somehow carries a hard break, the first paragraph's level is the
985    // one that governs alignment.
986    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    // Group consecutive characters of equal level. Iterating by
994    // `char_indices` rather than by byte keeps a run boundary from ever
995    // landing inside a multi-byte character.
996    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    // Each run ends where the next begins; the last ends with the text.
1005    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            // Always 0 here: these runs are in *logical* order and the
1022            // block path reorders per line after breaking, so a
1023            // paragraph-wide visual index would be both unused and
1024            // misleading. `bidi_runs` fills it in for the single-line
1025            // path, which does lay a paragraph out in one go.
1026            visual_order: 0,
1027            level,
1028        })
1029        .collect();
1030
1031    BidiParagraph { runs, para_level }
1032}
1033
1034/// Reorder items from logical into visual order per UAX #9 rule L2.
1035///
1036/// Returns indices into `levels`: `result[0]` is the item to paint
1037/// leftmost. L2 reads "from the highest level down to the lowest odd
1038/// level, reverse any contiguous sequence of items at that level or
1039/// higher", which is what the loop below does literally.
1040pub 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    // With no odd level nothing is RTL, so logical order is already visual.
1046    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        // min_odd is odd, hence >= 1, so this cannot wrap past zero.
1065        level -= 1;
1066    }
1067    order
1068}
1069
1070/// Analyze text for bidirectional content and return directional runs
1071/// in **visual order** per UAX #9 (Unicode Bidirectional Algorithm, rule L2).
1072///
1073/// The returned runs can be shaped independently and concatenated left-to-right
1074/// to produce correctly-ordered mixed-script text (e.g. Latin embedded in
1075/// Arabic). For pure-LTR text, returns a single LTR run. For pure-RTL text,
1076/// returns a single RTL run.
1077pub 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}"; // كتب
1126    const HEBREW: &str = "\u{05E9}\u{05DC}\u{05D5}\u{05DD}"; // שלום
1127
1128    #[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        // level 0 "a", level 1 RTL, level 0 "b" -> the RTL span reverses
1137        // in place but stays between its neighbours.
1138        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        // An English phrase (level 2) quoted inside Arabic (level 1)
1144        // inside an English paragraph (level 0). The level-2 island must
1145        // keep its own left-to-right order while the level-1 material
1146        // around it reverses — the case `direction` alone cannot express.
1147        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        // Level 1 throughout with a level-2 Latin word: the paragraph
1154        // reads right-to-left, so logical item 0 paints rightmost.
1155        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        // Worth pinning, because it is easy to assume otherwise: digits
1161        // are type EN, not strong, so rule P2 skips them and finds the
1162        // Arabic. An Arabic paragraph opening with a number needs no
1163        // explicit direction.
1164        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        // A Latin acronym *is* strong (type L), so rule P2 stops at the
1171        // "NASA" and calls this Arabic paragraph left-to-right — the
1172        // real false detection, and the reason a stored paragraph
1173        // direction has to win over guessing.
1174        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        // And the override genuinely changes the layout, not just the
1186        // recorded level: the Arabic now leads visually. Runs come back
1187        // in logical order, so ask rule L2 which one paints leftmost —
1188        // the same thing a caller laying the paragraph out would do.
1189        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        // "hello <hebrew> world" — three runs, contiguous, logical order.
1210        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(&registry, None, None, None, None, None, 1.0, 1.0).unwrap();
1282        let run = shape_text_in_paragraph(
1283            &registry,
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        // Noto Sans' `locl` for Romanian draws ş and ţ (cedilla) with the
1299        // comma below Romanian writes; no language, or English, keeps the
1300        // cedilla.
1301        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}