use alloc::{string::String, vec::Vec}; use core::convert::TryFrom; use std::cmp::min; use ttf_parser::GlyphId; use super::buffer::glyph_flag::{UNSAFE_TO_BREAK, UNSAFE_TO_CONCAT}; use super::face::GlyphExtents; use super::unicode::{CharExt, GeneralCategoryExt}; use super::{hb_font_t, hb_mask_t}; use crate::{script, BufferClusterLevel, BufferFlags, Direction, Language, Script, SerializeFlags}; const CONTEXT_LENGTH: usize = 5; pub mod glyph_flag { /// Indicates that if input text is broken at the /// beginning of the cluster this glyph is part of, /// then both sides need to be re-shaped, as the /// result might be different. /// /// On the flip side, it means that when /// this flag is not present, then it is safe /// to break the glyph-run at the beginning of /// this cluster, and the two sides will represent /// the exact same result one would get if breaking /// input text at the beginning of this cluster and /// shaping the two sides separately. /// /// This can be used to optimize paragraph layout, /// by avoiding re-shaping of each line after line-breaking. pub const UNSAFE_TO_BREAK: u32 = 0x00000001; /// Indicates that if input text is changed on one side /// of the beginning of the cluster this glyph is part /// of, then the shaping results for the other side /// might change. /// /// Note that the absence of this flag will NOT by /// itself mean that it IS safe to concat text. Only /// two pieces of text both of which clear of this /// flag can be concatenated safely. /// /// This can be used to optimize paragraph layout, /// by avoiding re-shaping of each line after /// line-breaking, by limiting the reshaping to a /// small piece around the breaking position only, /// even if the breaking position carries the /// UNSAFE_TO_BREAK or when hyphenation or /// other text transformation happens at /// line-break position, in the following way: /// /// 1. Iterate back from the line-break /// position until the first cluster /// start position that is NOT unsafe-to-concat, /// 2. shape the segment from there till the /// end of line, 3. check whether the resulting /// glyph-run also is clear of the unsafe-to-concat /// at its start-of-text position; if it is, just /// splice it into place and the line is shaped; /// If not, move on to a position further back that /// is clear of unsafe-to-concat and retry from /// there, and repeat. /// /// At the start of next line a similar /// algorithm can be implemented. /// That is: 1. Iterate forward from /// the line-break position until the first cluster /// start position that is NOT unsafe-to-concat, 2. /// shape the segment from beginning of the line to /// that position, 3. check whether the resulting /// glyph-run also is clear of the unsafe-to-concat /// at its end-of-text position; if it is, just splice /// it into place and the beginning is shaped; If not, /// move on to a position further forward that is clear /// of unsafe-to-concat and retry up to there, and repeat. /// /// A slight complication will arise in the /// implementation of the algorithm above, /// because while /// our buffer API has a way to return flags /// for position corresponding to /// start-of-text, there is currently no /// position corresponding to end-of-text. /// This limitation can be alleviated by /// shaping more text than needed and /// looking for unsafe-to-concat flag /// within text clusters. /// /// The UNSAFE_TO_BREAK flag will always imply this flag. /// To use this flag, you must enable the buffer flag /// PRODUCE_UNSAFE_TO_CONCAT during shaping, otherwise /// the buffer flag will not be reliably produced. pub const UNSAFE_TO_CONCAT: u32 = 0x00000002; /// All the currently defined flags. pub const DEFINED: u32 = 0x00000003; // OR of all defined flags } /// Holds the positions of the glyph in both horizontal and vertical directions. /// /// All positions are relative to the current point. #[repr(C)] #[derive(Clone, Copy, Default, Debug)] pub struct GlyphPosition { /// How much the line advances after drawing this glyph when setting text in /// horizontal direction. pub x_advance: i32, /// How much the line advances after drawing this glyph when setting text in /// vertical direction. pub y_advance: i32, /// How much the glyph moves on the X-axis before drawing it, this should /// not affect how much the line advances. pub x_offset: i32, /// How much the glyph moves on the Y-axis before drawing it, this should /// not affect how much the line advances. pub y_offset: i32, var: u32, } unsafe impl bytemuck::Zeroable for GlyphPosition {} unsafe impl bytemuck::Pod for GlyphPosition {} impl GlyphPosition { #[inline] pub(crate) fn attach_chain(&self) -> i16 { // glyph to which this attaches to, relative to current glyphs; // negative for going back, positive for forward. let v: &[i16; 2] = bytemuck::cast_ref(&self.var); v[0] } #[inline] pub(crate) fn set_attach_chain(&mut self, n: i16) { let v: &mut [i16; 2] = bytemuck::cast_mut(&mut self.var); v[0] = n; } #[inline] pub(crate) fn attach_type(&self) -> u8 { // attachment type // Note! if attach_chain() is zero, the value of attach_type() is irrelevant. let v: &[u8; 4] = bytemuck::cast_ref(&self.var); v[2] } #[inline] pub(crate) fn set_attach_type(&mut self, n: u8) { let v: &mut [u8; 4] = bytemuck::cast_mut(&mut self.var); v[2] = n; } } /// A glyph info. #[repr(C)] #[derive(Clone, Copy, Default, Debug)] pub struct hb_glyph_info_t { // NOTE: Stores a Unicode codepoint before shaping and a glyph ID after. // Just like harfbuzz, we are using the same variable for two purposes. // Occupies u32 as a codepoint and u16 as a glyph id. /// A selected glyph. /// /// Guarantee to be <= `u16::MAX`. pub glyph_id: u32, pub(crate) mask: hb_mask_t, /// An index to the start of the grapheme cluster in the original string. /// /// [Read more on clusters](https://harfbuzz.github.io/clusters.html). pub cluster: u32, pub(crate) var1: u32, pub(crate) var2: u32, } unsafe impl bytemuck::Zeroable for hb_glyph_info_t {} unsafe impl bytemuck::Pod for hb_glyph_info_t {} impl hb_glyph_info_t { /// Indicates that if input text is broken at the beginning of the cluster this glyph /// is part of, then both sides need to be re-shaped, as the result might be different. /// /// On the flip side, it means that when this flag is not present, /// then it's safe to break the glyph-run at the beginning of this cluster, /// and the two sides represent the exact same result one would get if breaking input text /// at the beginning of this cluster and shaping the two sides separately. /// This can be used to optimize paragraph layout, by avoiding re-shaping of each line /// after line-breaking, or limiting the reshaping to a small piece around /// the breaking point only. pub fn unsafe_to_break(&self) -> bool { self.mask & glyph_flag::UNSAFE_TO_BREAK != 0 } #[inline] pub(crate) fn as_char(&self) -> char { char::try_from(self.glyph_id).unwrap() } #[inline] pub(crate) fn as_glyph(&self) -> GlyphId { debug_assert!(self.glyph_id <= u32::from(u16::MAX)); GlyphId(self.glyph_id as u16) } // Var allocation: unicode_props // Used during the entire shaping process to store unicode properties #[inline] pub(crate) fn unicode_props(&self) -> u16 { let v: &[u16; 2] = bytemuck::cast_ref(&self.var2); v[0] } #[inline] pub(crate) fn set_unicode_props(&mut self, n: u16) { let v: &mut [u16; 2] = bytemuck::cast_mut(&mut self.var2); v[0] = n; } pub(crate) fn init_unicode_props(&mut self, scratch_flags: &mut hb_buffer_scratch_flags_t) { let u = self.as_char(); let gc = u.general_category(); let mut props = gc.to_rb() as u16; if u as u32 >= 0x80 { *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII; if u.is_default_ignorable() { props |= UnicodeProps::IGNORABLE.bits(); *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES; match u as u32 { 0x200C => props |= UnicodeProps::CF_ZWNJ.bits(), 0x200D => props |= UnicodeProps::CF_ZWJ.bits(), // Mongolian Free Variation Selectors need to be remembered // because although we need to hide them like default-ignorables, // they need to non-ignorable during shaping. This is similar to // what we do for joiners in Indic-like shapers, but since the // FVSes are GC=Mn, we have use a separate bit to remember them. // Fixes: // https://github.com/harfbuzz/harfbuzz/issues/234 0x180B..=0x180D | 0x180F => props |= UnicodeProps::HIDDEN.bits(), // TAG characters need similar treatment. Fixes: // https://github.com/harfbuzz/harfbuzz/issues/463 0xE0020..=0xE007F => props |= UnicodeProps::HIDDEN.bits(), // COMBINING GRAPHEME JOINER should not be skipped; at least some times. // https://github.com/harfbuzz/harfbuzz/issues/554 0x034F => { props |= UnicodeProps::HIDDEN.bits(); *scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CGJ; } _ => {} } } if gc.is_mark() { props |= UnicodeProps::CONTINUATION.bits(); props |= (u.modified_combining_class() as u16) << 8; } } self.set_unicode_props(props); } #[inline] pub(crate) fn is_hidden(&self) -> bool { self.unicode_props() & UnicodeProps::HIDDEN.bits() != 0 } #[inline] pub(crate) fn unhide(&mut self) { let mut n = self.unicode_props(); n &= !UnicodeProps::HIDDEN.bits(); self.set_unicode_props(n); } #[inline] pub(crate) fn lig_props(&self) -> u8 { let v: &[u8; 4] = bytemuck::cast_ref(&self.var1); v[2] } #[inline] pub(crate) fn set_lig_props(&mut self, n: u8) { let v: &mut [u8; 4] = bytemuck::cast_mut(&mut self.var1); v[2] = n; } #[inline] pub(crate) fn glyph_props(&self) -> u16 { let v: &[u16; 2] = bytemuck::cast_ref(&self.var1); v[0] } #[inline] pub(crate) fn set_glyph_props(&mut self, n: u16) { let v: &mut [u16; 2] = bytemuck::cast_mut(&mut self.var1); v[0] = n; } #[inline] pub(crate) fn syllable(&self) -> u8 { let v: &[u8; 4] = bytemuck::cast_ref(&self.var1); v[3] } #[inline] pub(crate) fn set_syllable(&mut self, n: u8) { let v: &mut [u8; 4] = bytemuck::cast_mut(&mut self.var1); v[3] = n; } // Var allocation: glyph_index // Used during the normalization process to store glyph indices #[inline] pub(crate) fn glyph_index(&mut self) -> u32 { self.var1 } #[inline] pub(crate) fn set_glyph_index(&mut self, n: u32) { self.var1 = n; } } pub type hb_buffer_cluster_level_t = u32; pub const HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES: u32 = 0; pub const HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS: u32 = 1; pub const HB_BUFFER_CLUSTER_LEVEL_CHARACTERS: u32 = 2; pub const HB_BUFFER_CLUSTER_LEVEL_DEFAULT: u32 = HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES; pub struct hb_buffer_t { // Information about how the text in the buffer should be treated. pub flags: BufferFlags, pub cluster_level: hb_buffer_cluster_level_t, pub invisible: Option, // Buffer contents. pub direction: Direction, pub script: Option