Skip to main content

harfrust/
lib.rs

1/*!
2Text shaping with a Rust port of [HarfBuzz](https://harfbuzz.github.io/).
3
4Create a [`ShaperFont`] from a [`Font`], add Unicode text to a [`Buffer`],
5and call [`shape`] to replace the text with positioned glyphs.
6*/
7
8#![cfg_attr(not(feature = "std"), no_std)]
9// Forbidding unsafe code only applies to the lib
10// examples continue to use it, so this cannot be placed into Cargo.toml
11#![forbid(unsafe_code)]
12#![warn(missing_docs)]
13#![allow(non_camel_case_types)]
14#![allow(non_upper_case_globals)]
15#![allow(non_snake_case)]
16#![allow(clippy::collapsible_if)]
17#![allow(clippy::collapsible_else_if)]
18#![allow(clippy::comparison_chain)]
19#![allow(clippy::needless_range_loop)]
20#![allow(clippy::non_canonical_partial_ord_impl)]
21#![allow(clippy::upper_case_acronyms)]
22#![allow(clippy::too_many_arguments)]
23#![allow(clippy::wildcard_in_or_patterns)]
24#![allow(clippy::identity_op)]
25#![allow(clippy::inline_always)]
26#![allow(clippy::mut_range_bound)]
27#![allow(clippy::enum_variant_names)]
28#![allow(clippy::manual_range_patterns)]
29#![allow(clippy::type_complexity)]
30#![allow(clippy::wrong_self_convention)]
31#![allow(clippy::match_like_matches_macro)]
32#![allow(clippy::manual_range_contains)]
33
34extern crate alloc;
35
36mod algs;
37mod budget;
38pub mod unicode;
39#[macro_use]
40mod buffer;
41mod aat;
42mod cache;
43mod direction;
44mod error;
45pub(crate) mod fallback;
46mod feature;
47mod hint;
48mod language;
49pub(crate) mod normalize;
50mod once;
51mod options;
52pub(crate) mod ot;
53mod parse_setting;
54mod plan;
55pub(crate) mod planner;
56mod scale;
57mod script;
58pub(crate) mod set_digest;
59mod shape;
60mod shaper_font;
61mod tag;
62mod tag_reverse;
63#[allow(clippy::collapsible_match)]
64mod tag_table;
65mod text_parser;
66
67type Mask = u32;
68
69#[inline(always)]
70fn clamp_i64_to_i32(value: i64) -> i32 {
71    value.clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32
72}
73
74pub(crate) type U32Set = read_fonts::collections::int_set::U32Set;
75
76pub use error::ShapeError;
77pub use options::ShapeOptions;
78#[doc(hidden)]
79pub use parse_setting::ParseSetting;
80pub use plan::{ShapePlan, ShapePlanKey};
81pub(crate) use planner::ShapePlanner;
82pub use scale::Scale;
83pub use shape::shape;
84pub(crate) use shaper_font::LayoutData;
85
86#[cfg(feature = "tracing")]
87pub use buffer::MessageFunction;
88pub use buffer::{Buffer, ContentType, GlyphFlags, GlyphInfo, GlyphPosition};
89pub use direction::Direction;
90pub use feature::Feature;
91pub use language::Language;
92pub use script::Script;
93pub use shaper_font::{
94    Advances, FontFuncs, GlyphExtents, NominalGlyphs, RawAdvances, RawNominalGlyphs, ShaperFont,
95};
96pub use tag::{LanguageTags, ScriptTags};
97
98/// Font types supplied by `read-fonts`.
99pub mod font {
100    pub use read_fonts::model::*;
101}
102
103#[doc(inline)]
104pub use font::Font;
105
106pub use read_fonts::types::{GlyphId, Tag};
107
108// /// An OpenType tag.
109// pub type Tag = read_fonts::types::Tag;
110
111// /// A 32-bit glyph identifier.
112// pub type GlyphId = read_fonts::types::GlyphId;
113
114bitflags::bitflags! {
115    /// Flags that control how a buffer is shaped.
116    #[derive(Default, Debug, Clone, Copy)]
117    pub struct BufferFlags: u32 {
118        /// Treat the buffer as the beginning of a paragraph.
119        ///
120        /// Set this when the buffer includes the beginning of the text, rather
121        /// than a segment shaped with surrounding context.
122        const BEGINNING_OF_TEXT             = 0x0000_0001;
123        /// Treat the buffer as the end of a paragraph.
124        ///
125        /// See [`BufferFlags::BEGINNING_OF_TEXT`].
126        const END_OF_TEXT                   = 0x0000_0002;
127        /// Keep glyphs for default-ignorable Unicode characters.
128        ///
129        /// Without this flag, shaping hides them by substituting a space glyph
130        /// with zero advance. This takes precedence over
131        /// [`BufferFlags::REMOVE_DEFAULT_IGNORABLES`].
132        const PRESERVE_DEFAULT_IGNORABLES   = 0x0000_0004;
133        /// Remove default-ignorable Unicode characters from the glyph output.
134        ///
135        /// [`BufferFlags::PRESERVE_DEFAULT_IGNORABLES`] takes precedence.
136        const REMOVE_DEFAULT_IGNORABLES     = 0x0000_0008;
137        /// Suppress dotted circles for broken character sequences.
138        const DO_NOT_INSERT_DOTTED_CIRCLE   = 0x0000_0010;
139        /// Reserved for HarfBuzz-compatible shaping verification.
140        ///
141        /// Verification is not currently implemented by this crate.
142        const VERIFY                        = 0x0000_0020;
143        /// Produce [`GlyphFlags::UNSAFE_TO_CONCAT`] on output glyphs.
144        ///
145        /// Disabled by default because it adds shaping work.
146        const PRODUCE_UNSAFE_TO_CONCAT      = 0x0000_0040;
147        /// Produce [`GlyphFlags::SAFE_TO_INSERT_TATWEEL`] on output glyphs.
148        const PRODUCE_SAFE_TO_INSERT_TATWEEL      = 0x0000_0080;
149        /// All currently defined flags.
150        const DEFINED = 0x0000_00FF;
151    }
152}
153
154/// How input text clusters are assigned to output glyphs.
155///
156/// Cluster values relate glyphs to positions in the input. Monotone levels
157/// preserve cluster order, which is useful for finding line break positions.
158#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
159pub enum ClusterLevel {
160    /// Keep grapheme clusters together and preserve cluster order.
161    MonotoneGraphemes,
162    /// Preserve cluster order, allowing characters to keep separate clusters.
163    MonotoneCharacters,
164    /// Allow separate character clusters without preserving cluster order.
165    Characters,
166    /// Keep grapheme clusters together without preserving cluster order.
167    Graphemes,
168}
169
170impl ClusterLevel {
171    #[inline]
172    fn new(level: u32) -> Self {
173        match level {
174            0 => Self::MonotoneGraphemes,
175            1 => Self::MonotoneCharacters,
176            2 => Self::Characters,
177            3 => Self::Graphemes,
178            _ => Self::MonotoneGraphemes,
179        }
180    }
181    #[inline]
182    fn is_monotone(self) -> bool {
183        matches!(self, Self::MonotoneGraphemes | Self::MonotoneCharacters)
184    }
185    #[inline]
186    fn is_graphemes(self) -> bool {
187        matches!(self, Self::MonotoneGraphemes | Self::Graphemes)
188    }
189    #[inline]
190    fn _is_characters(self) -> bool {
191        matches!(self, Self::MonotoneCharacters | Self::Characters)
192    }
193}
194
195impl Default for ClusterLevel {
196    #[inline]
197    fn default() -> Self {
198        ClusterLevel::MonotoneGraphemes
199    }
200}
201
202bitflags::bitflags! {
203    /// Flags used for serializing a buffer.
204    #[derive(Default)]
205    pub struct SerializeFlags: u8 {
206        /// Omit glyph cluster values.
207        const NO_CLUSTERS       = 0b0000_0001;
208        /// Omit glyph position information.
209        const NO_POSITIONS      = 0b0000_0010;
210        /// Serialize glyph IDs instead of glyph names.
211        const NO_GLYPH_NAMES    = 0b0000_0100;
212        /// Serialize glyph extents.
213        const GLYPH_EXTENTS     = 0b0000_1000;
214        /// Serialize glyph flags.
215        const GLYPH_FLAGS       = 0b0001_0000;
216        /// Omit advances and report offsets as absolute glyph positions.
217        const NO_ADVANCES       = 0b0010_0000;
218        /// All currently defined flags.
219        const DEFINED = 0b0011_1111;
220    }
221}