harfrust/lib.rs
1/*!
2A complete [harfbuzz](https://github.com/harfbuzz/harfbuzz) shaping algorithm port to Rust.
3*/
4
5#![cfg_attr(not(feature = "std"), no_std)]
6// Forbidding unsafe code only applies to the lib
7// examples continue to use it, so this cannot be placed into Cargo.toml
8#![forbid(unsafe_code)]
9#![warn(missing_docs)]
10
11extern crate alloc;
12
13mod hb;
14
15#[cfg(feature = "std")]
16pub(crate) type U32Set = read_fonts::collections::int_set::U32Set;
17#[cfg(not(feature = "std"))]
18mod digest_u32_set;
19#[cfg(not(feature = "std"))]
20pub(crate) type U32Set = digest_u32_set::DigestU32Set;
21
22pub use read_fonts::{
23 types::{GlyphId, Tag},
24 FontRef,
25};
26
27#[cfg(feature = "experimental_font_api")]
28pub use hb::face::shape;
29
30/// Font related types.
31pub mod font {
32 pub use crate::hb::face::{
33 AdvanceWidthBatch, BuiltinFontFuncs, FontFuncs, RawAdvanceWidthBatch,
34 };
35
36 // Import the whole read-fonts "model" module as our font representation.
37
38 #[cfg(feature = "experimental_font_api")]
39 pub use read_fonts::model::*;
40
41 #[cfg(not(feature = "experimental_font_api"))]
42 pub(crate) use read_fonts::model::*;
43}
44
45pub use hb::buffer::{GlyphBuffer, GlyphFlags, GlyphInfo, GlyphPosition, UnicodeBuffer};
46pub use hb::common::{script, Direction, Feature, Language, Script, Variation};
47pub use hb::face::{
48 hb_font_t as Shaper, GlyphExtents, ShapeOptions, ShaperBuilder, ShaperData, ShaperInstance,
49};
50
51pub use hb::ot_shape_plan::{hb_ot_shape_plan_t as ShapePlan, ShapePlanKey};
52
53/// Type alias for a normalized variation coordinate.
54pub type NormalizedCoord = read_fonts::types::F2Dot14;
55
56bitflags::bitflags! {
57 /// Flags for buffers.
58 #[derive(Default, Debug, Clone, Copy)]
59 pub struct BufferFlags: u32 {
60 /// Indicates that special handling of the beginning of text paragraph can be applied to this buffer. Should usually be set, unless you are passing to the buffer only part of the text without the full context.
61 const BEGINNING_OF_TEXT = 0x0000_0001;
62 /// Indicates that special handling of the end of text paragraph can be applied to this buffer, similar to [`BufferFlags::BEGINNING_OF_TEXT`].
63 const END_OF_TEXT = 0x0000_0002;
64 /// Indicates that characters with `Default_Ignorable` Unicode property should use the corresponding glyph from the font, instead of hiding them (done by replacing them with the space glyph and zeroing the advance width.) This flag takes precedence over [`BufferFlags::REMOVE_DEFAULT_IGNORABLES`].
65 const PRESERVE_DEFAULT_IGNORABLES = 0x0000_0004;
66 /// Indicates that characters with `Default_Ignorable` Unicode property should be removed from glyph string instead of hiding them (done by replacing them with the space glyph and zeroing the advance width.) [`BufferFlags::PRESERVE_DEFAULT_IGNORABLES`] takes precedence over this flag.
67 const REMOVE_DEFAULT_IGNORABLES = 0x0000_0008;
68 /// Indicates that a dotted circle should not be inserted in the rendering of incorrect character sequences (such as `<0905 093E>`).
69 const DO_NOT_INSERT_DOTTED_CIRCLE = 0x0000_0010;
70 /// Indicates that the shape() call and its variants should perform various verification processes on the results of the shaping operation on the buffer. If the verification fails, then either a buffer message is sent, if a message handler is installed on the buffer, or a message is written to standard error. In either case, the shaping result might be modified to show the failed output.
71 const VERIFY = 0x0000_0020;
72 /// Indicates that the `UNSAFE_TO_CONCAT` glyph-flag should be produced by the shaper. By default it will not be produced since it incurs a cost.
73 const PRODUCE_UNSAFE_TO_CONCAT = 0x0000_0040;
74 /// Indicates that the `SAFE_TO_INSERT_TATWEEL` glyph-flag should be produced by the shaper. By default it will not be produced.
75 const PRODUCE_SAFE_TO_INSERT_TATWEEL = 0x0000_0080;
76 /// All currently defined flags
77 const DEFINED = 0x0000_00FF;
78 }
79}
80
81/// A cluster level.
82#[allow(missing_docs)]
83#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
84pub enum BufferClusterLevel {
85 MonotoneGraphemes,
86 MonotoneCharacters,
87 Characters,
88 Graphemes,
89}
90
91impl BufferClusterLevel {
92 #[inline]
93 fn new(level: u32) -> Self {
94 match level {
95 0 => Self::MonotoneGraphemes,
96 1 => Self::MonotoneCharacters,
97 2 => Self::Characters,
98 3 => Self::Graphemes,
99 _ => Self::MonotoneGraphemes,
100 }
101 }
102 #[inline]
103 fn is_monotone(self) -> bool {
104 matches!(self, Self::MonotoneGraphemes | Self::MonotoneCharacters)
105 }
106 #[inline]
107 fn is_graphemes(self) -> bool {
108 matches!(self, Self::MonotoneGraphemes | Self::Graphemes)
109 }
110 #[inline]
111 fn _is_characters(self) -> bool {
112 matches!(self, Self::MonotoneCharacters | Self::Characters)
113 }
114}
115
116impl Default for BufferClusterLevel {
117 #[inline]
118 fn default() -> Self {
119 BufferClusterLevel::MonotoneGraphemes
120 }
121}
122
123bitflags::bitflags! {
124 /// Flags used for serialization with a `BufferSerializer`.
125 #[derive(Default)]
126 pub struct SerializeFlags: u8 {
127 /// Do not serialize glyph cluster.
128 const NO_CLUSTERS = 0b0000_0001;
129 /// Do not serialize glyph position information.
130 const NO_POSITIONS = 0b0000_0010;
131 /// Do no serialize glyph name.
132 const NO_GLYPH_NAMES = 0b0000_0100;
133 /// Serialize glyph extents.
134 const GLYPH_EXTENTS = 0b0000_1000;
135 /// Serialize glyph flags.
136 const GLYPH_FLAGS = 0b0001_0000;
137 /// Do not serialize glyph advances, glyph offsets will reflect absolute
138 /// glyph positions.
139 const NO_ADVANCES = 0b0010_0000;
140 /// All currently defined flags.
141 const DEFINED = 0b0011_1111;
142 }
143}