1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
//! `harfbuzz_rs` is a high-level interface to HarfBuzz, exposing its most important functionality //! in a safe manner using Rust. //! //! # What is HarfBuzz? //! HarfBuzz is a library for performing complex text layout. It does not perform any drawing. This //! is quite a low-level operation. If you want to simply draw some text on the screen choose //! another library. However if you want to build a library for drawing text on some canvas or //! need a lot of control on advanced text layout then this is the right library to use. //! //! # Getting Started //! //! To shape a simple string of text you just create a `Font` from a font file, fill a `Buffer` //! with some text and call the `shape` function. //! //! ``` //! # extern crate harfbuzz_rs; //! use harfbuzz_rs::*; //! //! # fn try_main() -> Result<(), std::io::Error> { //! //! let path = "path/to/some/font_file.otf"; //! let index = 0; //< face index in the font file //! # let path = "testfiles/SourceSansVariable-Roman.ttf"; //! let face = Face::from_file(path, index)?; //! let mut font = Font::new(face); //! //! let buffer = UnicodeBuffer::new().add_str("Hello World!"); //! let output = shape(&font, buffer, &[]); //! //! // The results of the shaping operation are stored in the `output` buffer. //! //! let positions = output.get_glyph_positions(); //! let infos = output.get_glyph_infos(); //! //! # assert_eq!(positions.len(), 12); //! assert_eq!(positions.len(), infos.len()); //! //! // iterate over the shaped glyphs //! for (position, info) in positions.iter().zip(infos) { //! let gid = info.codepoint; //! let cluster = info.cluster; //! let x_advance = position.x_advance; //! let x_offset = position.x_offset; //! let y_offset = position.y_offset; //! //! // Here you would usually draw the glyphs. //! println!("gid{:?}={:?}@{:?},{:?}+{:?}", gid, cluster, x_advance, x_offset, y_offset); //! } //! //! # Ok(()) //! # } //! # //! # fn main() { //! # try_main().unwrap(); //! # } //! ``` //! This should print out something similar to the following: //! //! ```text //! gid41=0@741,0+0 //! gid70=1@421,0+0 //! gid77=2@258,0+0 //! gid77=3@253,0+0 //! gid80=4@510,0+0 //! gid1=5@227,0+0 //! gid56=6@874,0+0 //! gid80=7@498,0+0 //! gid83=8@367,0+0 //! gid77=9@253,0+0 //! gid69=10@528,0+0 //! gid2=11@276,0+0 //! ``` #![deny(missing_debug_implementations)] /// Reexported `harfbuzz_sys` crate to directly access the C API whenever no /// adequate wrapper is provided. // This will hopefully not cause backwards compability concerns since harfbuzz // tries to be backwards compatible. pub use harfbuzz_sys as hb; #[macro_use] extern crate bitflags; mod blob; mod buffer; mod common; mod face; mod font; pub mod font_funcs; #[cfg(feature = "rusttype")] pub mod rusttype; pub use crate::blob::*; pub use crate::buffer::*; pub use crate::common::*; pub use crate::face::*; pub use crate::font::*; use std::ops::{Bound, RangeBounds}; use std::os::raw::c_uint; /// A feature tag with an accompanying range specifying on which subslice of /// `shape`s input it should be applied. /// /// You can pass a slice of `Feature`s to `shape` that will be activated for the /// corresponding slices of input. /// /// # Examples /// /// Shape some text using the `calt` (Contextual Alternatives) feature. /// /// ``` /// use harfbuzz_rs::{Face, Font, UnicodeBuffer, shape, Feature, Tag}; /// /// let path = "testfiles/SourceSansVariable-Roman.ttf"; /// let face = Face::from_file(path, 0).expect("could not load face"); /// let font = Font::new(face); /// /// let buffer = UnicodeBuffer::new().add_str("Hello World!"); /// /// // contextual alternatives feature /// let feature_tag = Tag::new('c', 'a', 'l', 't'); /// /// // use the feature on the entire input /// let feature_range = 0..; /// let feature = Feature::new(feature_tag, 0, feature_range); /// /// let output = shape(&font, buffer, &[feature]); /// ``` #[derive(Debug, Copy, Clone)] #[repr(transparent)] pub struct Feature(hb::hb_feature_t); impl Feature { /// Create a new `Feature` struct. /// /// # Arguments /// /// - `tag`: The OpenType feature tag to use. /// - `value`: Some OpenType features accept different values to change /// their behaviour. /// - `range`: The character range that should be affected by this feature. pub fn new(tag: Tag, value: u32, range: impl RangeBounds<usize>) -> Feature { // We have to do careful bounds checking since c_uint may be of // different sizes on different platforms. We do assume that // sizeof(usize) >= sizeof(c_uint). const MAX_UINT: usize = c_uint::max_value() as usize; let start = match range.start_bound() { Bound::Included(&included) => included.min(MAX_UINT) as c_uint, Bound::Excluded(&excluded) => excluded.min(MAX_UINT - 1) as c_uint + 1, Bound::Unbounded => 0, }; let end = match range.end_bound() { Bound::Included(&included) => included.min(MAX_UINT) as c_uint, Bound::Excluded(&excluded) => excluded.saturating_sub(1).min(MAX_UINT) as c_uint, Bound::Unbounded => c_uint::max_value(), }; Feature(hb::hb_feature_t { tag: tag.0, value, start, end, }) } pub fn tag(&self) -> Tag { Tag(self.0.tag) } pub fn value(&self) -> u32 { self.0.value } pub fn start(&self) -> usize { self.0.start as usize } pub fn end(&self) -> usize { self.0.end as usize } } /// Shape the contents of the buffer using the provided font and activating all /// OpenType features given in `features`. /// /// This function consumes the `buffer` and returns a `GlyphBuffer` containing /// the resulting glyph indices and the corresponding positioning information. /// Once all the information from the `GlyphBuffer` has been processed as /// necessary you can reuse the `GlyphBuffer` as an `UnicodeBuffer` (using /// `GlyphBuffer::clear_contents`) and use that to call `shape` again with new /// data. /// /// By default some basic OpenType features are enabled according to the /// language and the script set in the buffer. /// /// # Arguments /// - `font` – a reference to the harfbuzz font used to shape the text. /// - `buffer` – a `UnicodeBuffer` that is filled with the text to be shaped and /// also contains metadata about the text in the form of segment properties. /// - `features` – a slice of additional features to activate pub fn shape(font: &Font<'_>, buffer: UnicodeBuffer, features: &[Feature]) -> GlyphBuffer { let buffer = buffer.guess_segment_properties(); unsafe { hb::hb_shape( font.as_raw(), buffer.0.as_raw(), features.as_ptr() as *mut _, features.len() as u32, ) }; GlyphBuffer(buffer.0) } #[cfg(test)] mod tests { use std::mem::{align_of, size_of}; pub(crate) fn assert_memory_layout_equal<T, U>() { assert_eq!(size_of::<T>(), size_of::<U>()); assert_eq!(align_of::<T>(), align_of::<U>()); } #[test] fn it_works() {} fn assert_feature(feat: Feature, tag: Tag, value: u32, start: usize, end: usize) { assert_eq!(feat.tag(), tag); assert_eq!(feat.value(), value); assert_eq!(feat.start(), start); assert_eq!(feat.end(), end); } use super::{Feature, Tag}; #[test] fn feature_new() { let tag = Tag::new('a', 'b', 'c', 'd'); const UINT_MAX: usize = std::os::raw::c_uint::max_value() as usize; let feature = Feature::new(tag, 100, 2..100); assert_feature(feature, tag, 100, 2, 99); let feature = Feature::new(tag, 100, 2..=100); assert_feature(feature, tag, 100, 2, 100); let feature = Feature::new(tag, 100, 2..); assert_feature(feature, tag, 100, 2, UINT_MAX); let feature = Feature::new(tag, 100, ..100); assert_feature(feature, tag, 100, 0, 99); let feature = Feature::new(tag, 100, ..=100); assert_feature(feature, tag, 100, 0, 100); let feature = Feature::new(tag, 100, ..); assert_feature(feature, tag, 100, 0, UINT_MAX); } }