fonts/shapers/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5mod harfbuzz;
6
7use app_units::Au;
8use euclid::default::Point2D;
9pub(crate) use harfbuzz::Shaper;
10
11use crate::GlyphId;
12
13/// Utility function to convert a `unicode_script::Script` enum into the corresponding `c_uint` tag that
14/// harfbuzz uses to represent unicode scipts.
15fn unicode_script_to_iso15924_tag(script: unicode_script::Script) -> u32 {
16    let bytes: [u8; 4] = match script {
17        unicode_script::Script::Unknown => *b"Zzzz",
18        _ => {
19            let short_name = script.short_name();
20            short_name.as_bytes().try_into().unwrap()
21        },
22    };
23
24    u32::from_be_bytes(bytes)
25}
26
27#[derive(Debug)]
28pub(crate) struct ShapedGlyph {
29    /// The actual glyph to render for this [`ShapedGlyph`].
30    pub glyph_id: GlyphId,
31    /// The original byte offset in the input buffer of the character that this
32    /// glyph belongs to. More than one glyph can share the same character and
33    /// one character can produce multiple glyphs.
34    pub string_byte_offset: usize,
35    /// The advance the direction of the writing mode that this glyph needs.
36    pub advance: Au,
37    /// An offset that should be applied when rendering this glyph.
38    pub offset: Option<Point2D<Au>>,
39}
40
41/// Holds the results of shaping. Abstracts over HarfBuzz and HarfRust which return data in very similar
42/// form but with different types
43pub(crate) trait GlyphShapingResult {
44    /// The number of shaped glyphs
45    fn len(&self) -> usize;
46    /// Whether or not the result is right-to-left.
47    fn is_rtl(&self) -> bool;
48    /// An iterator of the shaped glyphs of this data.
49    fn iter(&self) -> impl Iterator<Item = ShapedGlyph>;
50}