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::{types::Tag, FontRef};
23
24pub use hb::buffer::{GlyphBuffer, GlyphInfo, GlyphPosition, UnicodeBuffer};
25pub use hb::common::{script, Direction, Feature, Language, Script, Variation};
26pub use hb::face::{hb_font_t as Shaper, ShaperBuilder, ShaperData, ShaperInstance};
27pub use hb::ot_shape_plan::{hb_ot_shape_plan_t as ShapePlan, ShapePlanKey};
28
29/// Type alias for a normalized variation coordinate.
30pub type NormalizedCoord = read_fonts::types::F2Dot14;
31
32bitflags::bitflags! {
33 /// Flags for buffers.
34 #[derive(Default, Debug, Clone, Copy)]
35 pub struct BufferFlags: u32 {
36 /// 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.
37 const BEGINNING_OF_TEXT = 0x0000_0001;
38 /// Indicates that special handling of the end of text paragraph can be applied to this buffer, similar to [`BufferFlags::BEGINNING_OF_TEXT`].
39 const END_OF_TEXT = 0x0000_0002;
40 /// 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`].
41 const PRESERVE_DEFAULT_IGNORABLES = 0x0000_0004;
42 /// 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.
43 const REMOVE_DEFAULT_IGNORABLES = 0x0000_0008;
44 /// Indicates that a dotted circle should not be inserted in the rendering of incorrect character sequences (such as `<0905 093E>`).
45 const DO_NOT_INSERT_DOTTED_CIRCLE = 0x0000_0010;
46 /// 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.
47 const VERIFY = 0x0000_0020;
48 /// 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.
49 const PRODUCE_UNSAFE_TO_CONCAT = 0x0000_0040;
50 /// Indicates that the `SAFE_TO_INSERT_TATWEEL` glyph-flag should be produced by the shaper. By default it will not be produced.
51 const PRODUCE_SAFE_TO_INSERT_TATWEEL = 0x0000_0080;
52 /// All currently defined flags
53 const DEFINED = 0x0000_00FF;
54 }
55}
56
57/// A cluster level.
58#[allow(missing_docs)]
59#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
60pub enum BufferClusterLevel {
61 MonotoneGraphemes,
62 MonotoneCharacters,
63 Characters,
64 Graphemes,
65}
66
67impl BufferClusterLevel {
68 #[inline]
69 fn new(level: u32) -> Self {
70 match level {
71 0 => Self::MonotoneGraphemes,
72 1 => Self::MonotoneCharacters,
73 2 => Self::Characters,
74 3 => Self::Graphemes,
75 _ => Self::MonotoneGraphemes,
76 }
77 }
78 #[inline]
79 fn is_monotone(self) -> bool {
80 matches!(self, Self::MonotoneGraphemes | Self::MonotoneCharacters)
81 }
82 #[inline]
83 fn is_graphemes(self) -> bool {
84 matches!(self, Self::MonotoneGraphemes | Self::Graphemes)
85 }
86 #[inline]
87 fn _is_characters(self) -> bool {
88 matches!(self, Self::MonotoneCharacters | Self::Characters)
89 }
90}
91
92impl Default for BufferClusterLevel {
93 #[inline]
94 fn default() -> Self {
95 BufferClusterLevel::MonotoneGraphemes
96 }
97}
98
99bitflags::bitflags! {
100 /// Flags used for serialization with a `BufferSerializer`.
101 #[derive(Default)]
102 pub struct SerializeFlags: u8 {
103 /// Do not serialize glyph cluster.
104 const NO_CLUSTERS = 0b0000_0001;
105 /// Do not serialize glyph position information.
106 const NO_POSITIONS = 0b0000_0010;
107 /// Do no serialize glyph name.
108 const NO_GLYPH_NAMES = 0b0000_0100;
109 /// Serialize glyph extents.
110 const GLYPH_EXTENTS = 0b0000_1000;
111 /// Serialize glyph flags.
112 const GLYPH_FLAGS = 0b0001_0000;
113 /// Do not serialize glyph advances, glyph offsets will reflect absolute
114 /// glyph positions.
115 const NO_ADVANCES = 0b0010_0000;
116 /// All currently defined flags.
117 const DEFINED = 0b0011_1111;
118 }
119}