ailloli_ui_devicons_font/
lib.rs

1//! License-audited file-icon font embedded as raw bytes.
2//!
3//! The font is a deterministic subset of Nerd Fonts Symbols Only 3.4.0. It
4//! contains only glyphs used by `devicons` 0.6.12 plus the canonical folder
5//! and file fallbacks. See `SOURCE_PROVENANCE` and `THIRD_PARTY_NOTICES`.
6//!
7//! # Examples
8//!
9//! ```
10//! use ailloli_ui_devicons_font::{glyph_or_fallback, FOLDER_GLYPH};
11//! assert_eq!(glyph_or_fallback(FOLDER_GLYPH), FOLDER_GLYPH);
12//! ```
13
14/// Generic-file fallback glyph at private-use codepoint `U+F15B`.
15///
16/// This glyph is included in [`SUPPORTED_GLYPHS`] and is returned by
17/// [`glyph_or_fallback`] for every unsupported character.
18///
19/// # Examples
20///
21/// ```
22/// use ailloli_ui_devicons_font::GENERIC_FILE_GLYPH;
23/// assert_eq!(GENERIC_FILE_GLYPH, '\u{f15b}');
24/// ```
25pub const GENERIC_FILE_GLYPH: char = '\u{f15b}';
26
27/// Canonical folder glyph at private-use codepoint `U+F07B`.
28///
29/// # Examples
30///
31/// ```
32/// use ailloli_ui_devicons_font::{supports_glyph, FOLDER_GLYPH};
33/// assert_eq!(FOLDER_GLYPH, '\u{f07b}');
34/// assert!(supports_glyph(FOLDER_GLYPH));
35/// ```
36pub const FOLDER_GLYPH: char = '\u{f07b}';
37
38/// Static bytes of the bundled `AilloliUIFileIcons-Regular.ttf` font.
39///
40/// The slice is embedded at compile time, is never empty in this release, and
41/// can be passed directly to a TrueType/OpenType parser or font provider. It is
42/// the font used to render `IconId::Devicon` in the wider framework.
43///
44/// # Examples
45///
46/// ```
47/// use ailloli_ui_devicons_font::DEVICON_FONT_BYTES;
48/// let bytes: &'static [u8] = DEVICON_FONT_BYTES;
49/// assert!(!bytes.is_empty());
50/// ```
51pub const DEVICON_FONT_BYTES: &[u8] = include_bytes!("../assets/AilloliUIFileIcons-Regular.ttf");
52
53/// All 198 glyphs retained by the audited font subset.
54///
55/// Entries are strictly increasing Unicode scalar values, so they are unique
56/// and suitable for binary search. Both [`FOLDER_GLYPH`] and
57/// [`GENERIC_FILE_GLYPH`] are included. The slice is exhaustive for Unicode
58/// mappings in [`DEVICON_FONT_BYTES`], as checked by the crate tests.
59///
60/// # Examples
61///
62/// ```
63/// use ailloli_ui_devicons_font::{SUPPORTED_GLYPHS, GENERIC_FILE_GLYPH};
64/// assert_eq!(SUPPORTED_GLYPHS.len(), 198);
65/// assert!(SUPPORTED_GLYPHS.binary_search(&GENERIC_FILE_GLYPH).is_ok());
66/// ```
67pub const SUPPORTED_GLYPHS: &[char] = &[
68    '\u{e21c}',
69    '\u{e271}',
70    '\u{e28b}',
71    '\u{e28c}',
72    '\u{e600}',
73    '\u{e603}',
74    '\u{e606}',
75    '\u{e607}',
76    '\u{e608}',
77    '\u{e609}',
78    '\u{e60a}',
79    '\u{e60b}',
80    '\u{e60c}',
81    '\u{e60d}',
82    '\u{e60e}',
83    '\u{e60f}',
84    '\u{e610}',
85    '\u{e611}',
86    '\u{e614}',
87    '\u{e615}',
88    '\u{e619}',
89    '\u{e61b}',
90    '\u{e61c}',
91    '\u{e61d}',
92    '\u{e61e}',
93    '\u{e61f}',
94    '\u{e620}',
95    '\u{e623}',
96    '\u{e624}',
97    '\u{e625}',
98    '\u{e627}',
99    '\u{e628}',
100    '\u{e62b}',
101    '\u{e62c}',
102    '\u{e62d}',
103    '\u{e62f}',
104    '\u{e631}',
105    '\u{e632}',
106    '\u{e633}',
107    '\u{e634}',
108    '\u{e639}',
109    '\u{e63a}',
110    '\u{e63b}',
111    '\u{e64a}',
112    '\u{e64b}',
113    '\u{e652}',
114    '\u{e655}',
115    '\u{e656}',
116    '\u{e65f}',
117    '\u{e660}',
118    '\u{e666}',
119    '\u{e667}',
120    '\u{e670}',
121    '\u{e672}',
122    '\u{e674}',
123    '\u{e677}',
124    '\u{e67a}',
125    '\u{e682}',
126    '\u{e684}',
127    '\u{e688}',
128    '\u{e68b}',
129    '\u{e691}',
130    '\u{e697}',
131    '\u{e69a}',
132    '\u{e69b}',
133    '\u{e69d}',
134    '\u{e69e}',
135    '\u{e69f}',
136    '\u{e6a0}',
137    '\u{e6a1}',
138    '\u{e6a9}',
139    '\u{e6ac}',
140    '\u{e6af}',
141    '\u{e6b2}',
142    '\u{e6b3}',
143    '\u{e6b4}',
144    '\u{e702}',
145    '\u{e706}',
146    '\u{e707}',
147    '\u{e70c}',
148    '\u{e70e}',
149    '\u{e718}',
150    '\u{e71e}',
151    '\u{e728}',
152    '\u{e736}',
153    '\u{e737}',
154    '\u{e738}',
155    '\u{e745}',
156    '\u{e749}',
157    '\u{e755}',
158    '\u{e768}',
159    '\u{e769}',
160    '\u{e76a}',
161    '\u{e772}',
162    '\u{e775}',
163    '\u{e779}',
164    '\u{e786}',
165    '\u{e791}',
166    '\u{e795}',
167    '\u{e798}',
168    '\u{e7a1}',
169    '\u{e7a7}',
170    '\u{e7a8}',
171    '\u{e7a9}',
172    '\u{e7aa}',
173    '\u{e7af}',
174    '\u{e7b1}',
175    '\u{e7b4}',
176    '\u{e7b8}',
177    '\u{e7ba}',
178    '\u{eac4}',
179    '\u{eae8}',
180    '\u{eaeb}',
181    '\u{eb9c}',
182    '\u{ebc8}',
183    '\u{ebe8}',
184    '\u{f001}',
185    '\u{f005}',
186    '\u{f019}',
187    '\u{f031}',
188    '\u{f06d}',
189    '\u{f073}',
190    '\u{f076}',
191    '\u{f07b}',
192    '\u{f0ad}',
193    '\u{f0c6}',
194    '\u{f0ec}',
195    '\u{f0fd}',
196    '\u{f108}',
197    '\u{f129}',
198    '\u{f15b}',
199    '\u{f179}',
200    '\u{f17c}',
201    '\u{f1ab}',
202    '\u{f1b2}',
203    '\u{f20e}',
204    '\u{f23e}',
205    '\u{f296}',
206    '\u{f2b8}',
207    '\u{f2d0}',
208    '\u{f2f7}',
209    '\u{f410}',
210    '\u{f462}',
211    '\u{f487}',
212    '\u{f489}',
213    '\u{f48a}',
214    '\u{f499}',
215    '\u{f49b}',
216    '\u{f4ae}',
217    '\u{f006f}',
218    '\u{f00ab}',
219    '\u{f019a}',
220    '\u{f01a7}',
221    '\u{f0219}',
222    '\u{f021b}',
223    '\u{f0227}',
224    '\u{f022c}',
225    '\u{f02a2}',
226    '\u{f031b}',
227    '\u{f032a}',
228    '\u{f0331}',
229    '\u{f035b}',
230    '\u{f042b}',
231    '\u{f0483}',
232    '\u{f04d9}',
233    '\u{f0509}',
234    '\u{f0565}',
235    '\u{f057c}',
236    '\u{f05c0}',
237    '\u{f05c6}',
238    '\u{f05ca}',
239    '\u{f0627}',
240    '\u{f0673}',
241    '\u{f06a9}',
242    '\u{f06d3}',
243    '\u{f0718}',
244    '\u{f0721}',
245    '\u{f072b}',
246    '\u{f07d4}',
247    '\u{f0858}',
248    '\u{f0868}',
249    '\u{f08c7}',
250    '\u{f099d}',
251    '\u{f0a0a}',
252    '\u{f0a16}',
253    '\u{f0aae}',
254    '\u{f0bc4}',
255    '\u{f0cb9}',
256    '\u{f0dd6}',
257    '\u{f0ebe}',
258    '\u{f0eeb}',
259    '\u{f1049}',
260    '\u{f1106}',
261    '\u{f121a}',
262    '\u{f125f}',
263    '\u{f13ff}',
264    '\u{f1997}',
265    '\u{f1998}',
266];
267
268/// Returns whether `glyph` is present in the audited embedded font.
269///
270/// The check uses binary search over 198 sorted entries and therefore takes
271/// logarithmic time without allocating. Any Unicode scalar value is accepted.
272///
273/// # Examples
274///
275/// ```
276/// use ailloli_ui_devicons_font::supports_glyph;
277/// assert!(supports_glyph('\u{e68b}')); // Rust file icon
278/// assert!(!supports_glyph('A'));
279/// ```
280pub fn supports_glyph(glyph: char) -> bool {
281    SUPPORTED_GLYPHS.binary_search(&glyph).is_ok()
282}
283
284/// Returns `glyph` when bundled, or [`GENERIC_FILE_GLYPH`] otherwise.
285///
286/// # Examples
287///
288/// ```
289/// use ailloli_ui_devicons_font::{glyph_or_fallback, GENERIC_FILE_GLYPH};
290/// assert_eq!(glyph_or_fallback('\u{e68b}'), '\u{e68b}');
291/// assert_eq!(glyph_or_fallback('A'), GENERIC_FILE_GLYPH);
292/// ```
293pub fn glyph_or_fallback(glyph: char) -> char {
294    if supports_glyph(glyph) {
295        glyph
296    } else {
297        GENERIC_FILE_GLYPH
298    }
299}
300
301#[cfg(test)]
302mod tests {
303    //! Verifies exact font cmap coverage, ordering, fallbacks, and exclusion of
304    //! known unlicensed or source-missing Font Logos glyphs.
305
306    use super::{
307        glyph_or_fallback, supports_glyph, DEVICON_FONT_BYTES, FOLDER_GLYPH, GENERIC_FILE_GLYPH,
308        SUPPORTED_GLYPHS,
309    };
310
311    #[test]
312    fn bundled_font_contains_exactly_the_audited_glyphs() {
313        let face = ttf_parser::Face::parse(DEVICON_FONT_BYTES, 0).expect("parse icon font");
314        assert_eq!(SUPPORTED_GLYPHS.len(), 198);
315        assert!(SUPPORTED_GLYPHS.windows(2).all(|pair| pair[0] < pair[1]));
316        for (label, ch) in [
317            ("rust", '\u{e68b}'),
318            ("toml", '\u{e6b2}'),
319            ("markdown", '\u{f48a}'),
320            ("json", '\u{e60b}'),
321            ("javascript", '\u{e60c}'),
322            ("typescript", '\u{e628}'),
323            ("html", '\u{e736}'),
324            ("css", '\u{e749}'),
325        ] {
326            assert!(
327                face.glyph_index(ch).is_some(),
328                "missing {label} glyph U+{:04X}",
329                ch as u32
330            );
331        }
332
333        for glyph in SUPPORTED_GLYPHS {
334            assert!(
335                face.glyph_index(*glyph).is_some(),
336                "missing audited glyph U+{:04X}",
337                *glyph as u32
338            );
339        }
340
341        let expected = SUPPORTED_GLYPHS
342            .iter()
343            .map(|glyph| *glyph as u32)
344            .collect::<std::collections::BTreeSet<_>>();
345        let mut actual = std::collections::BTreeSet::new();
346        let cmap = face.tables().cmap.expect("icon font has a cmap table");
347        for subtable in cmap
348            .subtables
349            .into_iter()
350            .filter(|table| table.is_unicode())
351        {
352            subtable.codepoints(|codepoint| {
353                if subtable.glyph_index(codepoint).is_some() {
354                    actual.insert(codepoint);
355                }
356            });
357        }
358        assert_eq!(actual, expected);
359    }
360
361    #[test]
362    fn unlicensed_and_source_missing_glyphs_use_the_generic_fallback() {
363        assert!(supports_glyph(FOLDER_GLYPH));
364        assert!(supports_glyph(GENERIC_FILE_GLYPH));
365        assert_eq!(glyph_or_fallback('\u{f303}'), GENERIC_FILE_GLYPH);
366        assert_eq!(glyph_or_fallback('\u{f381}'), GENERIC_FILE_GLYPH);
367        assert_eq!(glyph_or_fallback('\u{25b2}'), GENERIC_FILE_GLYPH);
368
369        let face = ttf_parser::Face::parse(DEVICON_FONT_BYTES, 0).expect("parse icon font");
370        for codepoint in 0xf300..=0xf381 {
371            let glyph = char::from_u32(codepoint).expect("valid private-use codepoint");
372            assert!(
373                face.glyph_index(glyph).is_none(),
374                "unlicensed Font Logos glyph U+{codepoint:04X} is bundled"
375            );
376        }
377    }
378}