@@ -16,9 +16,76 @@ use bevy_reflect::TypePath;
16
16
///
17
17
/// Bevy currently loads a single font face as a single `Font` asset.
18
18
#[ derive( Debug , TypePath , Clone , Asset ) ]
19
- pub struct Font {
19
+ pub enum Font {
20
20
/// Content of a font file as bytes
21
- pub data : Arc < Vec < u8 > > ,
21
+ Data ( Arc < Vec < u8 > > ) ,
22
+ /// Reference a system font by its name
23
+ System {
24
+ /// A list of font families that satisfy this font requirement
25
+ families : Vec < Family > ,
26
+ /// Specifies the weight of glyphs in the font, their degree of blackness or stroke thickness.
27
+ ///
28
+ /// See [`cosmic_text::Weight`] for details.
29
+ weight : cosmic_text:: Weight ,
30
+ /// A face [width](https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass).
31
+ ///
32
+ /// See [`cosmic_text::Stretch`] for details.
33
+ stretch : cosmic_text:: Stretch ,
34
+ /// Allows italic or oblique faces to be selected.
35
+ ///
36
+ /// See [`cosmic_text::Style`] for details.
37
+ style : cosmic_text:: Style ,
38
+ } ,
39
+ }
40
+
41
+ /// A [font family](https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#propdef-font-family).
42
+ ///
43
+ /// See [`cosmic_text::Family`] for details.
44
+ #[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
45
+ pub enum Family {
46
+ /// The name of a font family of choice.
47
+ ///
48
+ /// This must be a *Typographic Family* (ID 16) or a *Family Name* (ID 1) in terms of TrueType.
49
+ /// Meaning you have to pass a family without any additional suffixes like _Bold_, _Italic_,
50
+ /// _Regular_, etc.
51
+ ///
52
+ /// Localized names are allowed.
53
+ Name ( String ) ,
54
+
55
+ /// Serif fonts represent the formal text style for a script.
56
+ Serif ,
57
+
58
+ /// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low contrast
59
+ /// and have stroke endings that are plain — without any flaring, cross stroke,
60
+ /// or other ornamentation.
61
+ SansSerif ,
62
+
63
+ /// Glyphs in cursive fonts generally use a more informal script style,
64
+ /// and the result looks more like handwritten pen or brush writing than printed letterwork.
65
+ Cursive ,
66
+
67
+ /// Fantasy fonts are primarily decorative or expressive fonts that
68
+ /// contain decorative or expressive representations of characters.
69
+ Fantasy ,
70
+
71
+ /// The sole criterion of a monospace font is that all glyphs have the same fixed width.
72
+ MonoSpace ,
73
+ }
74
+
75
+ impl Family {
76
+ /// References variants to create a [`cosmic_text::Family`].
77
+ ///
78
+ /// This is required for querying the underlying [`cosmic_text::fontdb::Database`]
79
+ pub fn as_fontdb_family ( & self ) -> cosmic_text:: Family < ' _ > {
80
+ match self {
81
+ Family :: Name ( name) => cosmic_text:: Family :: Name ( name) ,
82
+ Family :: Serif => cosmic_text:: Family :: Serif ,
83
+ Family :: SansSerif => cosmic_text:: Family :: SansSerif ,
84
+ Family :: Cursive => cosmic_text:: Family :: Cursive ,
85
+ Family :: Fantasy => cosmic_text:: Family :: Fantasy ,
86
+ Family :: MonoSpace => cosmic_text:: Family :: Monospace ,
87
+ }
88
+ }
22
89
}
23
90
24
91
impl Font {
@@ -28,8 +95,6 @@ impl Font {
28
95
) -> Result < Self , cosmic_text:: ttf_parser:: FaceParsingError > {
29
96
use cosmic_text:: ttf_parser;
30
97
ttf_parser:: Face :: parse ( & font_data, 0 ) ?;
31
- Ok ( Self {
32
- data : Arc :: new ( font_data) ,
33
- } )
98
+ Ok ( Self :: Data ( Arc :: new ( font_data) ) )
34
99
}
35
100
}
0 commit comments