Skip to main content

laser_pdf/fonts/
mod.rs

1pub mod builtin;
2pub mod truetype;
3
4use std::ops::Range;
5
6pub struct GeneralMetrics {
7    pub height_above_baseline: f32,
8    pub height_below_baseline: f32,
9}
10
11#[derive(Debug, Clone)]
12pub struct ShapedGlyph {
13    pub unsafe_to_break: bool,
14    /// Zero is reserved for glyphs not found in the font.
15    pub glyph_id: u32,
16    pub text_range: Range<usize>,
17    /// without kerning
18    pub x_advance_font: f32,
19    pub x_advance: f32,
20    pub x_offset: f32,
21    pub y_advance: f32,
22    pub y_offset: f32,
23}
24
25pub enum EncodedGlyph {
26    OneByte(u8),
27    TwoBytes([u8; 2]),
28}
29
30pub trait Font {
31    type Shaped<'a>: Iterator<Item = ShapedGlyph> + Clone + 'a
32    where
33        Self: 'a;
34
35    fn shape<'a>(
36        &'a self,
37        text: &'a str,
38        character_spacing: f32,
39        word_spacing: f32,
40    ) -> Self::Shaped<'a>;
41
42    fn encode(&self, pdf: &mut crate::Pdf, glyph_id: u32, text: &str) -> EncodedGlyph;
43
44    fn index(&self) -> usize;
45
46    fn resource_name(&self) -> pdf_writer::Name<'_>;
47
48    fn general_metrics(&self) -> GeneralMetrics;
49
50    fn fallback_fonts(&self) -> &[Self]
51    where
52        Self: Sized;
53}