skrifa/
provider.rs

1use crate::{color::ColorPalettes, GlyphNames};
2
3use super::{
4    attribute::Attributes,
5    charmap::Charmap,
6    color::ColorGlyphCollection,
7    instance::{LocationRef, Size},
8    metrics::{GlyphMetrics, Metrics},
9    outline::OutlineGlyphCollection,
10    string::{LocalizedStrings, StringId},
11    variation::{AxisCollection, NamedInstanceCollection},
12    FontRef,
13};
14use crate::bitmap::BitmapStrikes;
15
16/// Interface for types that can provide font metadata.
17pub trait MetadataProvider<'a>: Sized {
18    /// Returns the primary attributes for font classification-- stretch,
19    /// style and weight.
20    fn attributes(&self) -> Attributes;
21
22    /// Returns the collection of variation axes.
23    fn axes(&self) -> AxisCollection<'a>;
24
25    /// Returns the collection of named variation instances.
26    fn named_instances(&self) -> NamedInstanceCollection<'a>;
27
28    /// Returns an iterator over the collection of localized strings for the
29    /// given informational string identifier.
30    fn localized_strings(&self, id: StringId) -> LocalizedStrings<'a>;
31
32    /// Returns the mapping from glyph identifiers to names.
33    fn glyph_names(&self) -> GlyphNames<'a>;
34
35    /// Returns the global font metrics for the specified size and location in
36    /// normalized variation space.
37    fn metrics(&self, size: Size, location: impl Into<LocationRef<'a>>) -> Metrics;
38
39    /// Returns the glyph specific metrics for the specified size and location
40    /// in normalized variation space.
41    fn glyph_metrics(&self, size: Size, location: impl Into<LocationRef<'a>>) -> GlyphMetrics<'a>;
42
43    /// Returns the character to nominal glyph identifier mapping.
44    fn charmap(&self) -> Charmap<'a>;
45
46    /// Returns the collection of scalable glyph outlines.
47    ///
48    /// If the font contains multiple outline sources, this method prioritizes
49    /// `glyf`, `CFF2` and `CFF` in that order. To select a specific outline
50    /// source, use the [`OutlineGlyphCollection::with_format`] method.
51    fn outline_glyphs(&self) -> OutlineGlyphCollection<'a>;
52
53    /// Returns a collection of paintable color glyphs.
54    fn color_glyphs(&self) -> ColorGlyphCollection<'a>;
55
56    /// Returns a collection of color palettes for color glyphs.
57    fn color_palettes(&self) -> ColorPalettes<'a>;
58
59    /// Returns a collection of bitmap strikes.
60    fn bitmap_strikes(&self) -> BitmapStrikes<'a>;
61}
62
63impl<'a> MetadataProvider<'a> for FontRef<'a> {
64    /// Returns the primary attributes for font classification-- stretch,
65    /// style and weight.
66    fn attributes(&self) -> Attributes {
67        Attributes::new(self)
68    }
69
70    /// Returns the collection of variation axes.
71    fn axes(&self) -> AxisCollection<'a> {
72        AxisCollection::new(self)
73    }
74
75    /// Returns the collection of named variation instances.
76    fn named_instances(&self) -> NamedInstanceCollection<'a> {
77        NamedInstanceCollection::new(self)
78    }
79
80    /// Returns an iterator over the collection of localized strings for the
81    /// given informational string identifier.
82    fn localized_strings(&self, id: StringId) -> LocalizedStrings<'a> {
83        LocalizedStrings::new(self, id)
84    }
85
86    /// Returns the mapping from glyph identifiers to names.
87    fn glyph_names(&self) -> GlyphNames<'a> {
88        GlyphNames::new(self)
89    }
90
91    /// Returns the global font metrics for the specified size and location in
92    /// normalized variation space.
93    fn metrics(&self, size: Size, location: impl Into<LocationRef<'a>>) -> Metrics {
94        Metrics::new(self, size, location)
95    }
96
97    /// Returns the glyph specific metrics for the specified size and location
98    /// in normalized variation space.
99    fn glyph_metrics(&self, size: Size, location: impl Into<LocationRef<'a>>) -> GlyphMetrics<'a> {
100        GlyphMetrics::new(self, size, location)
101    }
102
103    /// Returns the character to nominal glyph identifier mapping.
104    fn charmap(&self) -> Charmap<'a> {
105        Charmap::new(self)
106    }
107
108    /// Returns the collection of scalable glyph outlines.
109    ///
110    /// If the font contains multiple outline sources, this method prioritizes
111    /// `glyf`, `CFF2` and `CFF` in that order. To select a specific outline
112    /// source, use the [`OutlineGlyphCollection::with_format`] method.
113    fn outline_glyphs(&self) -> OutlineGlyphCollection<'a> {
114        OutlineGlyphCollection::new(self)
115    }
116
117    // Returns a collection of paintable color glyphs.
118    fn color_glyphs(&self) -> ColorGlyphCollection<'a> {
119        ColorGlyphCollection::new(self)
120    }
121
122    /// Returns a collection of color palettes for color glyphs.
123    fn color_palettes(&self) -> ColorPalettes<'a> {
124        ColorPalettes::new(self)
125    }
126
127    /// Returns a collection of bitmap strikes.
128    fn bitmap_strikes(&self) -> BitmapStrikes<'a> {
129        BitmapStrikes::new(self)
130    }
131}