Struct FontIndex

Source
pub struct FontIndex {
    pub base: BaseIndex,
    pub families: Vec<FamilyData>,
    pub script_map: HashMap<Script, Fallbacks, BuildHasherDefault<FxHasher>>,
    pub script_tag_map: HashMap<Tag, Vec<FamilyId>, BuildHasherDefault<FxHasher>>,
    pub language_tag_map: HashMap<Tag, Vec<FamilyId>, BuildHasherDefault<FxHasher>>,
    pub emacs_charset_map: HashMap<SmallString, Vec<FamilyId>, BuildHasherDefault<FxHasher>>,
    pub emacs_script_map: HashMap<SmallString, Vec<FamilyId>, BuildHasherDefault<FxHasher>>,
    pub cjk: [Fallbacks; 5],
    pub generic: [Option<FamilyId>; 13],
}

Fields§

§base: BaseIndex§families: Vec<FamilyData>§script_map: HashMap<Script, Fallbacks, BuildHasherDefault<FxHasher>>§script_tag_map: HashMap<Tag, Vec<FamilyId>, BuildHasherDefault<FxHasher>>§language_tag_map: HashMap<Tag, Vec<FamilyId>, BuildHasherDefault<FxHasher>>§emacs_charset_map: HashMap<SmallString, Vec<FamilyId>, BuildHasherDefault<FxHasher>>§emacs_script_map: HashMap<SmallString, Vec<FamilyId>, BuildHasherDefault<FxHasher>>§cjk: [Fallbacks; 5]§generic: [Option<FamilyId>; 13]

Implementations§

Source§

impl StaticIndex

Source

pub fn setup_default_fallbacks(&mut self)

Source

pub fn setup_default_generic(&mut self)

Source

pub fn emoji_family(&self) -> Option<FamilyId>

Source

pub fn fallbacks(&self, script: Script, cjk: Cjk) -> &[FamilyId]

Source§

impl StaticIndex

Source

pub fn query<'a>( &'a self, family: impl Into<FamilyKey<'a>>, attributes: impl Into<Attributes>, ) -> Option<FontEntry<'a>>

Returns a font entry that matches the specified family and attributes.

Examples found in repository?
examples/list-fonts.rs (line 29)
13fn main() {
14    // List all family names
15    let family_names: Vec<String> = FontIndex::global()
16        .families
17        .clone()
18        .iter()
19        .map(|data| data.name.to_string())
20        .collect();
21    println!("family names {:?}", family_names);
22
23    let stretch = Stretch::NORMAL;
24    let style = Style::Normal;
25    let weight = Weight::NORMAL;
26
27    // Query fontconfig alias
28    if let Some(font) =
29        FontIndex::global().query("Monospace", Attributes::new(stretch, weight, style))
30    {
31        println!(
32            "monospace font {:?} {:?}",
33            font.family_name(),
34            font.attributes()
35        );
36    }
37
38    //Query localized family name
39    if let Some(font) =
40        FontIndex::global().query("文泉驛微米黑", Attributes::new(stretch, weight, style))
41    {
42        println!(
43            "Query localized family name 文泉驛微米黑: {:?} {:?}",
44            font.family_name(),
45            font.attributes()
46        );
47    }
48
49    // Match font according FontSpec
50    #[cfg(feature = "emacs")]
51    if let Some(font) = FontIndex::global().match_(FontSpec {
52        family: None,
53        foundry: None,
54        width: Some(Stretch::CONDENSED),
55        weight: Some(Weight::BOLD),
56        slant: None,
57        adstyle: None,
58        registry: Some("iso8859-1".to_string()),
59        size: None,
60        dpi: None,
61        spacing: None,
62        avgwidth: None,
63        name: None,
64        // script: Some("han".to_string()),
65        script: None,
66        lang: None,
67        otf: None,
68    }) {
69        println!("gb18030 {:?} {:?}", font.family_name(), font.attributes());
70    }
71
72    // List font according FontSpec
73    #[cfg(feature = "emacs")]
74    FontIndex::global()
75        .list(FontSpec {
76            // family: Some("Droid Sans".to_string()),
77            family: None,
78            foundry: None,
79            width: None,
80            weight: Some(Weight::BOLD),
81            slant: None,
82            adstyle: None,
83            registry: Some("iso8859-1".to_string()),
84            size: None,
85            dpi: None,
86            spacing: None,
87            avgwidth: None,
88            name: None,
89            // script: Some("han".to_string()),
90            script: None,
91            lang: None,
92            otf: None,
93        })
94        .iter()
95        .for_each(|font| {
96            println!(
97                "list gb18030 {:?} {:?}",
98                font.family_name(),
99                font.attributes()
100            );
101        });
102
103    // Query font meta using FontCache
104    let mut cache = FontCache::default();
105    if let Some(font) = cache.query("Noto Color Emoji", Attributes::new(stretch, weight, style)) {
106        let font = &cache.get(font.id()).unwrap();
107        println!("data {:?}", &font.as_ref().data[..50]);
108
109        for name in font
110            .localized_strings()
111            .clone()
112            .filter(|name| name.id() == StringId::Manufacturer && name.is_unicode())
113        {
114            println!("{}", name);
115        }
116        font.writing_systems().for_each(|w| {
117            println!("language {:?}", w.language());
118            println!("script {:?}", w.script())
119        })
120    }
121
122    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
123    // 	let font = &cache.get(font_data.id).unwrap();
124
125    // 	println!("--------{:?}-----", font.localized_strings().find_by_id(StringId::Family, None).unwrap().to_string());
126    // 	font.writing_systems().for_each(|w| {
127    // 	    println!("language {:?}", w.language());
128    // 	    println!("script {:?}", w.script());
129    // 	})
130    // });
131
132    println!("--------Query font for script using opentype tag-----");
133    let tag = "latn";
134    let script = Script::from_opentype(swash::tag_from_str_lossy(tag)).unwrap();
135    if let Some(fallbacks) = FontIndex::global().script_map.get(&script) {
136        println!("tag: {tag}, script {script:?}, fonts {:?}", fallbacks.len());
137    }
138    println!("End--------Query font for script using opentype tag-----\n");
139
140    println!("--------Platform default fonts for script-----");
141    FontIndex::global()
142        .script_map
143        .iter()
144        .for_each(|(script, fallbacks)| {
145            println!("{:?}: {:?}", script, fallbacks.len());
146        });
147    println!("End--------Platform default fonts for script-----\n");
148
149    println!("--------List all fonts for opentype script tag-----");
150    FontIndex::global()
151        .script_tag_map
152        .iter()
153        .for_each(|(tag, fonts)| {
154            let tag = tag.to_be_bytes();
155            if let Ok(s) = core::str::from_utf8(&tag) {
156                println!("{:?}: {:?}", s, fonts.len());
157            }
158        });
159    println!("End --------List all fonts for opentype script tag-----\n");
160
161    println!("--------List all fonts for opentype language tag-----");
162    FontIndex::global()
163        .language_tag_map
164        .iter()
165        .for_each(|(tag, fonts)| {
166            let tag = tag.to_be_bytes();
167            if let Ok(s) = core::str::from_utf8(&tag) {
168                println!("{:?}: {:?}", s, fonts.len());
169            }
170        });
171    println!("End--------List all fonts for opentype language tag-----\n\n");
172
173    #[cfg(feature = "emacs")]
174    {
175        println!("--------List all fonts for Emacs charset-----");
176        FontIndex::global()
177            .emacs_charset_map
178            .iter()
179            .for_each(|(charset, fonts)| {
180                println!("{:?}: {:?}", charset, fonts.len());
181            });
182        println!("End--------List all fonts for Emacs charset-----\n\n");
183    }
184
185    // Debug lang
186    if let Some(lang) = Language::parse("zh-Hans") {
187        debug_lang(lang);
188        println!("{:?}: {:?}", lang, lang.script());
189    }
190
191    if let Some(lang) = Language::parse("zh-Hant") {
192        println!("{:?}: {:?}", lang, lang.script());
193    }
194
195    if let Some(lang) = Language::parse("zh") {
196        if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
197            println!("{lang}");
198        }
199    }
200
201    // if let Some(lang) = Language::parse("ko") {
202    //     print_lang(lang);
203    //     if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
204    //         print_lang(lang);
205    //     }
206    // }
207
208    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
209    //     let font = &cache.get(font_data.id).unwrap();
210
211    //     println!(
212    //         "--------{:?}-----",
213    //         font.localized_strings()
214    //             .find_by_id(StringId::Family, None)
215    //             .unwrap()
216    //             .to_string()
217    //     );
218    //     font.writing_systems().for_each(|w| {
219    //         if let Some(s) = w.script() {
220    //             println!("language {:?}", w.language());
221    //             println!("script {:?}", w.script());
222    //         }
223    // 	    let features = w.features();
224    // 	    features.for_each(|feature| {
225    // 		let tag = feature.tag().to_be_bytes();
226    // 		if let Ok(tag) = core::str::from_utf8(&tag) {
227    // 		    println!("tag: {}, name: {:?}, action: {:?} ", tag, feature.name(), feature.action());
228    // 		}
229
230    // 	    });
231
232    //     })
233    // });
234
235    println!("--------Source kind-----");
236    FontIndex::global()
237        .base
238        .sources
239        .iter()
240        .for_each(|source_data| match &source_data.kind {
241            SourceKind::File(file) => {
242                println!("file {:?}", file.path.as_path());
243            }
244            SourceKind::Memory(_data) => {
245                println!("Shared data");
246            }
247        });
248    println!("End--------Source kind-----");
249}
Source

pub fn list<'a>(&'a self, spec: FontSpec) -> Vec<FontEntry<'a>>

Returns a list font entries that matches the specified family and attributes.

Examples found in repository?
examples/list-fonts.rs (lines 75-93)
13fn main() {
14    // List all family names
15    let family_names: Vec<String> = FontIndex::global()
16        .families
17        .clone()
18        .iter()
19        .map(|data| data.name.to_string())
20        .collect();
21    println!("family names {:?}", family_names);
22
23    let stretch = Stretch::NORMAL;
24    let style = Style::Normal;
25    let weight = Weight::NORMAL;
26
27    // Query fontconfig alias
28    if let Some(font) =
29        FontIndex::global().query("Monospace", Attributes::new(stretch, weight, style))
30    {
31        println!(
32            "monospace font {:?} {:?}",
33            font.family_name(),
34            font.attributes()
35        );
36    }
37
38    //Query localized family name
39    if let Some(font) =
40        FontIndex::global().query("文泉驛微米黑", Attributes::new(stretch, weight, style))
41    {
42        println!(
43            "Query localized family name 文泉驛微米黑: {:?} {:?}",
44            font.family_name(),
45            font.attributes()
46        );
47    }
48
49    // Match font according FontSpec
50    #[cfg(feature = "emacs")]
51    if let Some(font) = FontIndex::global().match_(FontSpec {
52        family: None,
53        foundry: None,
54        width: Some(Stretch::CONDENSED),
55        weight: Some(Weight::BOLD),
56        slant: None,
57        adstyle: None,
58        registry: Some("iso8859-1".to_string()),
59        size: None,
60        dpi: None,
61        spacing: None,
62        avgwidth: None,
63        name: None,
64        // script: Some("han".to_string()),
65        script: None,
66        lang: None,
67        otf: None,
68    }) {
69        println!("gb18030 {:?} {:?}", font.family_name(), font.attributes());
70    }
71
72    // List font according FontSpec
73    #[cfg(feature = "emacs")]
74    FontIndex::global()
75        .list(FontSpec {
76            // family: Some("Droid Sans".to_string()),
77            family: None,
78            foundry: None,
79            width: None,
80            weight: Some(Weight::BOLD),
81            slant: None,
82            adstyle: None,
83            registry: Some("iso8859-1".to_string()),
84            size: None,
85            dpi: None,
86            spacing: None,
87            avgwidth: None,
88            name: None,
89            // script: Some("han".to_string()),
90            script: None,
91            lang: None,
92            otf: None,
93        })
94        .iter()
95        .for_each(|font| {
96            println!(
97                "list gb18030 {:?} {:?}",
98                font.family_name(),
99                font.attributes()
100            );
101        });
102
103    // Query font meta using FontCache
104    let mut cache = FontCache::default();
105    if let Some(font) = cache.query("Noto Color Emoji", Attributes::new(stretch, weight, style)) {
106        let font = &cache.get(font.id()).unwrap();
107        println!("data {:?}", &font.as_ref().data[..50]);
108
109        for name in font
110            .localized_strings()
111            .clone()
112            .filter(|name| name.id() == StringId::Manufacturer && name.is_unicode())
113        {
114            println!("{}", name);
115        }
116        font.writing_systems().for_each(|w| {
117            println!("language {:?}", w.language());
118            println!("script {:?}", w.script())
119        })
120    }
121
122    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
123    // 	let font = &cache.get(font_data.id).unwrap();
124
125    // 	println!("--------{:?}-----", font.localized_strings().find_by_id(StringId::Family, None).unwrap().to_string());
126    // 	font.writing_systems().for_each(|w| {
127    // 	    println!("language {:?}", w.language());
128    // 	    println!("script {:?}", w.script());
129    // 	})
130    // });
131
132    println!("--------Query font for script using opentype tag-----");
133    let tag = "latn";
134    let script = Script::from_opentype(swash::tag_from_str_lossy(tag)).unwrap();
135    if let Some(fallbacks) = FontIndex::global().script_map.get(&script) {
136        println!("tag: {tag}, script {script:?}, fonts {:?}", fallbacks.len());
137    }
138    println!("End--------Query font for script using opentype tag-----\n");
139
140    println!("--------Platform default fonts for script-----");
141    FontIndex::global()
142        .script_map
143        .iter()
144        .for_each(|(script, fallbacks)| {
145            println!("{:?}: {:?}", script, fallbacks.len());
146        });
147    println!("End--------Platform default fonts for script-----\n");
148
149    println!("--------List all fonts for opentype script tag-----");
150    FontIndex::global()
151        .script_tag_map
152        .iter()
153        .for_each(|(tag, fonts)| {
154            let tag = tag.to_be_bytes();
155            if let Ok(s) = core::str::from_utf8(&tag) {
156                println!("{:?}: {:?}", s, fonts.len());
157            }
158        });
159    println!("End --------List all fonts for opentype script tag-----\n");
160
161    println!("--------List all fonts for opentype language tag-----");
162    FontIndex::global()
163        .language_tag_map
164        .iter()
165        .for_each(|(tag, fonts)| {
166            let tag = tag.to_be_bytes();
167            if let Ok(s) = core::str::from_utf8(&tag) {
168                println!("{:?}: {:?}", s, fonts.len());
169            }
170        });
171    println!("End--------List all fonts for opentype language tag-----\n\n");
172
173    #[cfg(feature = "emacs")]
174    {
175        println!("--------List all fonts for Emacs charset-----");
176        FontIndex::global()
177            .emacs_charset_map
178            .iter()
179            .for_each(|(charset, fonts)| {
180                println!("{:?}: {:?}", charset, fonts.len());
181            });
182        println!("End--------List all fonts for Emacs charset-----\n\n");
183    }
184
185    // Debug lang
186    if let Some(lang) = Language::parse("zh-Hans") {
187        debug_lang(lang);
188        println!("{:?}: {:?}", lang, lang.script());
189    }
190
191    if let Some(lang) = Language::parse("zh-Hant") {
192        println!("{:?}: {:?}", lang, lang.script());
193    }
194
195    if let Some(lang) = Language::parse("zh") {
196        if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
197            println!("{lang}");
198        }
199    }
200
201    // if let Some(lang) = Language::parse("ko") {
202    //     print_lang(lang);
203    //     if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
204    //         print_lang(lang);
205    //     }
206    // }
207
208    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
209    //     let font = &cache.get(font_data.id).unwrap();
210
211    //     println!(
212    //         "--------{:?}-----",
213    //         font.localized_strings()
214    //             .find_by_id(StringId::Family, None)
215    //             .unwrap()
216    //             .to_string()
217    //     );
218    //     font.writing_systems().for_each(|w| {
219    //         if let Some(s) = w.script() {
220    //             println!("language {:?}", w.language());
221    //             println!("script {:?}", w.script());
222    //         }
223    // 	    let features = w.features();
224    // 	    features.for_each(|feature| {
225    // 		let tag = feature.tag().to_be_bytes();
226    // 		if let Ok(tag) = core::str::from_utf8(&tag) {
227    // 		    println!("tag: {}, name: {:?}, action: {:?} ", tag, feature.name(), feature.action());
228    // 		}
229
230    // 	    });
231
232    //     })
233    // });
234
235    println!("--------Source kind-----");
236    FontIndex::global()
237        .base
238        .sources
239        .iter()
240        .for_each(|source_data| match &source_data.kind {
241            SourceKind::File(file) => {
242                println!("file {:?}", file.path.as_path());
243            }
244            SourceKind::Memory(_data) => {
245                println!("Shared data");
246            }
247        });
248    println!("End--------Source kind-----");
249}
Source

pub fn match_<'a>(&'a self, spec: FontSpec) -> Option<FontEntry<'a>>

Examples found in repository?
examples/list-fonts.rs (lines 51-68)
13fn main() {
14    // List all family names
15    let family_names: Vec<String> = FontIndex::global()
16        .families
17        .clone()
18        .iter()
19        .map(|data| data.name.to_string())
20        .collect();
21    println!("family names {:?}", family_names);
22
23    let stretch = Stretch::NORMAL;
24    let style = Style::Normal;
25    let weight = Weight::NORMAL;
26
27    // Query fontconfig alias
28    if let Some(font) =
29        FontIndex::global().query("Monospace", Attributes::new(stretch, weight, style))
30    {
31        println!(
32            "monospace font {:?} {:?}",
33            font.family_name(),
34            font.attributes()
35        );
36    }
37
38    //Query localized family name
39    if let Some(font) =
40        FontIndex::global().query("文泉驛微米黑", Attributes::new(stretch, weight, style))
41    {
42        println!(
43            "Query localized family name 文泉驛微米黑: {:?} {:?}",
44            font.family_name(),
45            font.attributes()
46        );
47    }
48
49    // Match font according FontSpec
50    #[cfg(feature = "emacs")]
51    if let Some(font) = FontIndex::global().match_(FontSpec {
52        family: None,
53        foundry: None,
54        width: Some(Stretch::CONDENSED),
55        weight: Some(Weight::BOLD),
56        slant: None,
57        adstyle: None,
58        registry: Some("iso8859-1".to_string()),
59        size: None,
60        dpi: None,
61        spacing: None,
62        avgwidth: None,
63        name: None,
64        // script: Some("han".to_string()),
65        script: None,
66        lang: None,
67        otf: None,
68    }) {
69        println!("gb18030 {:?} {:?}", font.family_name(), font.attributes());
70    }
71
72    // List font according FontSpec
73    #[cfg(feature = "emacs")]
74    FontIndex::global()
75        .list(FontSpec {
76            // family: Some("Droid Sans".to_string()),
77            family: None,
78            foundry: None,
79            width: None,
80            weight: Some(Weight::BOLD),
81            slant: None,
82            adstyle: None,
83            registry: Some("iso8859-1".to_string()),
84            size: None,
85            dpi: None,
86            spacing: None,
87            avgwidth: None,
88            name: None,
89            // script: Some("han".to_string()),
90            script: None,
91            lang: None,
92            otf: None,
93        })
94        .iter()
95        .for_each(|font| {
96            println!(
97                "list gb18030 {:?} {:?}",
98                font.family_name(),
99                font.attributes()
100            );
101        });
102
103    // Query font meta using FontCache
104    let mut cache = FontCache::default();
105    if let Some(font) = cache.query("Noto Color Emoji", Attributes::new(stretch, weight, style)) {
106        let font = &cache.get(font.id()).unwrap();
107        println!("data {:?}", &font.as_ref().data[..50]);
108
109        for name in font
110            .localized_strings()
111            .clone()
112            .filter(|name| name.id() == StringId::Manufacturer && name.is_unicode())
113        {
114            println!("{}", name);
115        }
116        font.writing_systems().for_each(|w| {
117            println!("language {:?}", w.language());
118            println!("script {:?}", w.script())
119        })
120    }
121
122    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
123    // 	let font = &cache.get(font_data.id).unwrap();
124
125    // 	println!("--------{:?}-----", font.localized_strings().find_by_id(StringId::Family, None).unwrap().to_string());
126    // 	font.writing_systems().for_each(|w| {
127    // 	    println!("language {:?}", w.language());
128    // 	    println!("script {:?}", w.script());
129    // 	})
130    // });
131
132    println!("--------Query font for script using opentype tag-----");
133    let tag = "latn";
134    let script = Script::from_opentype(swash::tag_from_str_lossy(tag)).unwrap();
135    if let Some(fallbacks) = FontIndex::global().script_map.get(&script) {
136        println!("tag: {tag}, script {script:?}, fonts {:?}", fallbacks.len());
137    }
138    println!("End--------Query font for script using opentype tag-----\n");
139
140    println!("--------Platform default fonts for script-----");
141    FontIndex::global()
142        .script_map
143        .iter()
144        .for_each(|(script, fallbacks)| {
145            println!("{:?}: {:?}", script, fallbacks.len());
146        });
147    println!("End--------Platform default fonts for script-----\n");
148
149    println!("--------List all fonts for opentype script tag-----");
150    FontIndex::global()
151        .script_tag_map
152        .iter()
153        .for_each(|(tag, fonts)| {
154            let tag = tag.to_be_bytes();
155            if let Ok(s) = core::str::from_utf8(&tag) {
156                println!("{:?}: {:?}", s, fonts.len());
157            }
158        });
159    println!("End --------List all fonts for opentype script tag-----\n");
160
161    println!("--------List all fonts for opentype language tag-----");
162    FontIndex::global()
163        .language_tag_map
164        .iter()
165        .for_each(|(tag, fonts)| {
166            let tag = tag.to_be_bytes();
167            if let Ok(s) = core::str::from_utf8(&tag) {
168                println!("{:?}: {:?}", s, fonts.len());
169            }
170        });
171    println!("End--------List all fonts for opentype language tag-----\n\n");
172
173    #[cfg(feature = "emacs")]
174    {
175        println!("--------List all fonts for Emacs charset-----");
176        FontIndex::global()
177            .emacs_charset_map
178            .iter()
179            .for_each(|(charset, fonts)| {
180                println!("{:?}: {:?}", charset, fonts.len());
181            });
182        println!("End--------List all fonts for Emacs charset-----\n\n");
183    }
184
185    // Debug lang
186    if let Some(lang) = Language::parse("zh-Hans") {
187        debug_lang(lang);
188        println!("{:?}: {:?}", lang, lang.script());
189    }
190
191    if let Some(lang) = Language::parse("zh-Hant") {
192        println!("{:?}: {:?}", lang, lang.script());
193    }
194
195    if let Some(lang) = Language::parse("zh") {
196        if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
197            println!("{lang}");
198        }
199    }
200
201    // if let Some(lang) = Language::parse("ko") {
202    //     print_lang(lang);
203    //     if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
204    //         print_lang(lang);
205    //     }
206    // }
207
208    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
209    //     let font = &cache.get(font_data.id).unwrap();
210
211    //     println!(
212    //         "--------{:?}-----",
213    //         font.localized_strings()
214    //             .find_by_id(StringId::Family, None)
215    //             .unwrap()
216    //             .to_string()
217    //     );
218    //     font.writing_systems().for_each(|w| {
219    //         if let Some(s) = w.script() {
220    //             println!("language {:?}", w.language());
221    //             println!("script {:?}", w.script());
222    //         }
223    // 	    let features = w.features();
224    // 	    features.for_each(|feature| {
225    // 		let tag = feature.tag().to_be_bytes();
226    // 		if let Ok(tag) = core::str::from_utf8(&tag) {
227    // 		    println!("tag: {}, name: {:?}, action: {:?} ", tag, feature.name(), feature.action());
228    // 		}
229
230    // 	    });
231
232    //     })
233    // });
234
235    println!("--------Source kind-----");
236    FontIndex::global()
237        .base
238        .sources
239        .iter()
240        .for_each(|source_data| match &source_data.kind {
241            SourceKind::File(file) => {
242                println!("file {:?}", file.path.as_path());
243            }
244            SourceKind::Memory(_data) => {
245                println!("Shared data");
246            }
247        });
248    println!("End--------Source kind-----");
249}
Source

pub fn families_by_spec<'a>(&'a self, _: FontSpec) -> Vec<FamilyId>

Source

pub fn families_by_charset(&self, regexp: String) -> Option<&Vec<FamilyId>>

Source

pub fn families_by_emacs_script(&self, script: String) -> Option<&Vec<FamilyId>>

Source

pub fn families_by_script(&self, lang: Tag) -> Option<&Vec<FamilyId>>

Source

pub fn families_by_lang(&self, lang: Tag) -> Option<&Vec<FamilyId>>

Source

pub fn family_by_key<'a>( &'a self, key: impl Into<FamilyKey<'a>>, ) -> Option<FamilyEntry<'a>>

Returns a font family entry for the specified family key.

Source

pub fn family_by_name<'a>(&'a self, name: &str) -> Option<FamilyEntry<'a>>

Returns a font family entry for the specified name.

Source

pub fn family_by_id<'a>(&'a self, id: FamilyId) -> Option<FamilyEntry<'a>>

Returns a font family entry for the specified identifier.

Source

pub fn font_by_id<'a>(&'a self, id: FontId) -> Option<FontEntry<'a>>

Returns a font entry for the specified identifier.

Source§

impl FontIndex

Source

pub fn global() -> Arc<FontIndex>

Examples found in repository?
examples/list-fonts.rs (line 15)
13fn main() {
14    // List all family names
15    let family_names: Vec<String> = FontIndex::global()
16        .families
17        .clone()
18        .iter()
19        .map(|data| data.name.to_string())
20        .collect();
21    println!("family names {:?}", family_names);
22
23    let stretch = Stretch::NORMAL;
24    let style = Style::Normal;
25    let weight = Weight::NORMAL;
26
27    // Query fontconfig alias
28    if let Some(font) =
29        FontIndex::global().query("Monospace", Attributes::new(stretch, weight, style))
30    {
31        println!(
32            "monospace font {:?} {:?}",
33            font.family_name(),
34            font.attributes()
35        );
36    }
37
38    //Query localized family name
39    if let Some(font) =
40        FontIndex::global().query("文泉驛微米黑", Attributes::new(stretch, weight, style))
41    {
42        println!(
43            "Query localized family name 文泉驛微米黑: {:?} {:?}",
44            font.family_name(),
45            font.attributes()
46        );
47    }
48
49    // Match font according FontSpec
50    #[cfg(feature = "emacs")]
51    if let Some(font) = FontIndex::global().match_(FontSpec {
52        family: None,
53        foundry: None,
54        width: Some(Stretch::CONDENSED),
55        weight: Some(Weight::BOLD),
56        slant: None,
57        adstyle: None,
58        registry: Some("iso8859-1".to_string()),
59        size: None,
60        dpi: None,
61        spacing: None,
62        avgwidth: None,
63        name: None,
64        // script: Some("han".to_string()),
65        script: None,
66        lang: None,
67        otf: None,
68    }) {
69        println!("gb18030 {:?} {:?}", font.family_name(), font.attributes());
70    }
71
72    // List font according FontSpec
73    #[cfg(feature = "emacs")]
74    FontIndex::global()
75        .list(FontSpec {
76            // family: Some("Droid Sans".to_string()),
77            family: None,
78            foundry: None,
79            width: None,
80            weight: Some(Weight::BOLD),
81            slant: None,
82            adstyle: None,
83            registry: Some("iso8859-1".to_string()),
84            size: None,
85            dpi: None,
86            spacing: None,
87            avgwidth: None,
88            name: None,
89            // script: Some("han".to_string()),
90            script: None,
91            lang: None,
92            otf: None,
93        })
94        .iter()
95        .for_each(|font| {
96            println!(
97                "list gb18030 {:?} {:?}",
98                font.family_name(),
99                font.attributes()
100            );
101        });
102
103    // Query font meta using FontCache
104    let mut cache = FontCache::default();
105    if let Some(font) = cache.query("Noto Color Emoji", Attributes::new(stretch, weight, style)) {
106        let font = &cache.get(font.id()).unwrap();
107        println!("data {:?}", &font.as_ref().data[..50]);
108
109        for name in font
110            .localized_strings()
111            .clone()
112            .filter(|name| name.id() == StringId::Manufacturer && name.is_unicode())
113        {
114            println!("{}", name);
115        }
116        font.writing_systems().for_each(|w| {
117            println!("language {:?}", w.language());
118            println!("script {:?}", w.script())
119        })
120    }
121
122    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
123    // 	let font = &cache.get(font_data.id).unwrap();
124
125    // 	println!("--------{:?}-----", font.localized_strings().find_by_id(StringId::Family, None).unwrap().to_string());
126    // 	font.writing_systems().for_each(|w| {
127    // 	    println!("language {:?}", w.language());
128    // 	    println!("script {:?}", w.script());
129    // 	})
130    // });
131
132    println!("--------Query font for script using opentype tag-----");
133    let tag = "latn";
134    let script = Script::from_opentype(swash::tag_from_str_lossy(tag)).unwrap();
135    if let Some(fallbacks) = FontIndex::global().script_map.get(&script) {
136        println!("tag: {tag}, script {script:?}, fonts {:?}", fallbacks.len());
137    }
138    println!("End--------Query font for script using opentype tag-----\n");
139
140    println!("--------Platform default fonts for script-----");
141    FontIndex::global()
142        .script_map
143        .iter()
144        .for_each(|(script, fallbacks)| {
145            println!("{:?}: {:?}", script, fallbacks.len());
146        });
147    println!("End--------Platform default fonts for script-----\n");
148
149    println!("--------List all fonts for opentype script tag-----");
150    FontIndex::global()
151        .script_tag_map
152        .iter()
153        .for_each(|(tag, fonts)| {
154            let tag = tag.to_be_bytes();
155            if let Ok(s) = core::str::from_utf8(&tag) {
156                println!("{:?}: {:?}", s, fonts.len());
157            }
158        });
159    println!("End --------List all fonts for opentype script tag-----\n");
160
161    println!("--------List all fonts for opentype language tag-----");
162    FontIndex::global()
163        .language_tag_map
164        .iter()
165        .for_each(|(tag, fonts)| {
166            let tag = tag.to_be_bytes();
167            if let Ok(s) = core::str::from_utf8(&tag) {
168                println!("{:?}: {:?}", s, fonts.len());
169            }
170        });
171    println!("End--------List all fonts for opentype language tag-----\n\n");
172
173    #[cfg(feature = "emacs")]
174    {
175        println!("--------List all fonts for Emacs charset-----");
176        FontIndex::global()
177            .emacs_charset_map
178            .iter()
179            .for_each(|(charset, fonts)| {
180                println!("{:?}: {:?}", charset, fonts.len());
181            });
182        println!("End--------List all fonts for Emacs charset-----\n\n");
183    }
184
185    // Debug lang
186    if let Some(lang) = Language::parse("zh-Hans") {
187        debug_lang(lang);
188        println!("{:?}: {:?}", lang, lang.script());
189    }
190
191    if let Some(lang) = Language::parse("zh-Hant") {
192        println!("{:?}: {:?}", lang, lang.script());
193    }
194
195    if let Some(lang) = Language::parse("zh") {
196        if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
197            println!("{lang}");
198        }
199    }
200
201    // if let Some(lang) = Language::parse("ko") {
202    //     print_lang(lang);
203    //     if let Some(lang) = Language::from_opentype(lang.to_opentype().unwrap()) {
204    //         print_lang(lang);
205    //     }
206    // }
207
208    // FontIndex::global().base.fonts.iter().for_each(|font_data| {
209    //     let font = &cache.get(font_data.id).unwrap();
210
211    //     println!(
212    //         "--------{:?}-----",
213    //         font.localized_strings()
214    //             .find_by_id(StringId::Family, None)
215    //             .unwrap()
216    //             .to_string()
217    //     );
218    //     font.writing_systems().for_each(|w| {
219    //         if let Some(s) = w.script() {
220    //             println!("language {:?}", w.language());
221    //             println!("script {:?}", w.script());
222    //         }
223    // 	    let features = w.features();
224    // 	    features.for_each(|feature| {
225    // 		let tag = feature.tag().to_be_bytes();
226    // 		if let Ok(tag) = core::str::from_utf8(&tag) {
227    // 		    println!("tag: {}, name: {:?}, action: {:?} ", tag, feature.name(), feature.action());
228    // 		}
229
230    // 	    });
231
232    //     })
233    // });
234
235    println!("--------Source kind-----");
236    FontIndex::global()
237        .base
238        .sources
239        .iter()
240        .for_each(|source_data| match &source_data.kind {
241            SourceKind::File(file) => {
242                println!("file {:?}", file.path.as_path());
243            }
244            SourceKind::Memory(_data) => {
245                println!("Shared data");
246            }
247        });
248    println!("End--------Source kind-----");
249}

Trait Implementations§

Source§

impl Default for StaticIndex

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.