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
//! Text layout for [rusttype](https://gitlab.redox-os.org/redox-os/rusttype).
//!
//! # Example
//!
//! ```
//! extern crate glyph_brush_layout;
//! use glyph_brush_layout::{rusttype::*, *};
//! # fn main() -> Result<(), rusttype::Error> {
//!
//! let dejavu = Font::from_bytes(&include_bytes!("../../fonts/DejaVuSans.ttf")[..])?;
//! let garamond = Font::from_bytes(&include_bytes!("../../fonts/GaramondNo8-Reg.ttf")[..])?;
//!
//! // Simple vec font mapping: FontId(0) -> deja vu sans, FontId(1) -> garamond
//! let fonts = vec![dejavu, garamond];
//!
//! // Layout "hello glyph_brush_layout" on an unbounded line with the second
//! // word suitably bigger, greener and serif-ier.
//! let glyphs = Layout::default().calculate_glyphs(
//!     &fonts,
//!     &SectionGeometry {
//!         screen_position: (150.0, 50.0),
//!         ..SectionGeometry::default()
//!     },
//!     &[
//!         SectionText {
//!             text: "hello ",
//!             scale: Scale::uniform(20.0),
//!             ..SectionText::default()
//!         },
//!         SectionText {
//!             text: "glyph_brush_layout",
//!             scale: Scale::uniform(25.0),
//!             font_id: FontId(1),
//!             color: [0.0, 1.0, 0.0, 1.0],
//!         },
//!     ],
//! );
//!
//! assert_eq!(glyphs.len(), 23);
//!
//! let (o_glyph, glyph_4_color, glyph_4_font) = &glyphs[4];
//! assert_eq!(o_glyph.id(), fonts[0].glyph('o').id());
//! assert_eq!(*glyph_4_color, [0.0, 0.0, 0.0, 1.0]);
//! assert_eq!(*glyph_4_font, FontId(0));
//!
//! let (s_glyph, glyph_14_color, glyph_14_font) = &glyphs[14];
//! assert_eq!(s_glyph.id(), fonts[1].glyph('s').id());
//! assert_eq!(*glyph_14_color, [0.0, 1.0, 0.0, 1.0]);
//! assert_eq!(*glyph_14_font, FontId(1));
//!
//! # Ok(())
//! # }
//! ```

extern crate rusttype as full_rusttype;
extern crate xi_unicode;

#[cfg(test)]
#[macro_use]
extern crate approx;
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
#[cfg(test)]
extern crate ordered_float;

mod builtin;
mod characters;
mod font;
mod linebreak;
mod lines;
mod section;
mod words;

pub use self::builtin::*;
pub use self::font::*;
pub use self::linebreak::*;
pub use self::section::*;

/// Re-exported rusttype types.
pub mod rusttype {
    pub use full_rusttype::{
        point, Error, Font, Glyph, GlyphId, HMetrics, Point, PositionedGlyph, Rect, Scale,
        ScaledGlyph, SharedBytes, VMetrics,
    };
}

use rusttype::*;
use std::hash::Hash;

/// Logic to calculate glyph positioning using [`Font`](struct.Font.html),
/// [`SectionGeometry`](struct.SectionGeometry.html) and
/// [`SectionText`](struct.SectionText.html).
pub trait GlyphPositioner: Hash {
    /// Calculate a sequence of positioned glyphs to render. Custom implementations should
    /// return the same result when called with the same arguments to allow layout caching.
    fn calculate_glyphs<'font, F: FontMap<'font>>(
        &self,
        &F,
        geometry: &SectionGeometry,
        sections: &[SectionText],
    ) -> Vec<(PositionedGlyph<'font>, Color, FontId)>;

    /// Return a screen rectangle according to the requested render position and bounds
    /// appropriate for the glyph layout.
    fn bounds_rect(&self, geometry: &SectionGeometry) -> Rect<f32>;
}