// SPDX-License-Identifier: MIT OR Apache-2.0 #![allow(clippy::too_many_arguments)] use crate::fallback::FontFallbackIter; use crate::{ math, Align, Attrs, AttrsList, CacheKeyFlags, Color, DecorationMetrics, Ellipsize, EllipsizeHeightLimit, Font, FontSystem, GlyphDecorationData, Hinting, LayoutGlyph, LayoutLine, Metrics, Wrap, }; #[cfg(not(feature = "std"))] use alloc::{boxed::Box, format, vec, vec::Vec}; use alloc::collections::VecDeque; use core::cmp::{max, min}; use core::fmt; use core::mem; use core::ops::Range; #[cfg(not(feature = "std"))] use core_maths::CoreFloat; use fontdb::Style; use unicode_script::{Script, UnicodeScript}; use unicode_segmentation::UnicodeSegmentation; /// The shaping strategy of some text. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Shaping { /// Basic shaping with no font fallback. /// /// This shaping strategy is very cheap, but it will not display complex /// scripts properly nor try to find missing glyphs in your system fonts. /// /// You should use this strategy when you have complete control of the text /// and the font you are displaying in your application. #[cfg(feature = "swash")] Basic, /// Advanced text shaping and font fallback. /// /// You will need to enable this strategy if the text contains a complex /// script, the font used needs it, and/or multiple fonts in your system /// may be needed to display all of the glyphs. Advanced, } impl Shaping { fn run( self, glyphs: &mut Vec, font_system: &mut FontSystem, line: &str, attrs_list: &AttrsList, start_run: usize, end_run: usize, span_rtl: bool, ) { match self { #[cfg(feature = "swash")] Self::Basic => shape_skip(font_system, glyphs, line, attrs_list, start_run, end_run), #[cfg(not(feature = "shape-run-cache"))] Self::Advanced => shape_run( glyphs, font_system, line, attrs_list, start_run, end_run, span_rtl, ), #[cfg(feature = "shape-run-cache")] Self::Advanced => shape_run_cached( glyphs, font_system, line, attrs_list, start_run, end_run, span_rtl, ), } } } const NUM_SHAPE_PLANS: usize = 6; /// A set of buffers containing allocations for shaped text. #[derive(Default)] pub struct ShapeBuffer { /// Cache for harfrust shape plans. Stores up to [`NUM_SHAPE_PLANS`] plans at once. Inserting a new one past that /// will remove the one that was least recently added (not least recently used). shape_plan_cache: VecDeque<(fontdb::ID, harfrust::ShapePlan)>, /// Buffer for holding unicode text. harfrust_buffer: Option, /// Temporary buffers for scripts. scripts: Vec