#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const FONT_BYTES: &[u8] = include_bytes!("../assets/codicon.ttf");
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const FONT_FAMILY: &str = "codicon";
pub(crate) const CHEVRON_DOWN: char = '\u{eab4}';
pub(crate) const CHEVRON_RIGHT: char = '\u{eab6}';
#[cfg_attr(not(all(feature = "macos", target_os = "macos")), allow(dead_code))]
pub(crate) const CHEVRON_LEFT: char = '\u{eab5}';
#[cfg_attr(not(all(feature = "macos", target_os = "macos")), allow(dead_code))]
pub(crate) const SEARCH: char = '\u{ea6d}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const CHECK: char = '\u{eab2}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const CLOSE: char = '\u{ea76}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const DIRTY: char = '\u{ea71}';
pub(crate) const SORT_ASCENDING: char = '\u{eb71}';
pub(crate) const SORT_DESCENDING: char = '\u{eb6e}';
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn font_bytes_is_a_real_sfnt_font() {
assert!(
FONT_BYTES.len() > 1024,
"codicon.ttf looks truncated: {} bytes",
FONT_BYTES.len()
);
assert_eq!(
&FONT_BYTES[0..4],
&[0x00, 0x01, 0x00, 0x00],
"codicon.ttf's first four bytes must be the sfnt version tag"
);
}
#[test]
fn glyph_constants_are_distinct_and_in_codicons_pua_block() {
let glyphs = [
CHEVRON_DOWN,
CHEVRON_RIGHT,
CHEVRON_LEFT,
SEARCH,
CHECK,
CLOSE,
DIRTY,
SORT_ASCENDING,
SORT_DESCENDING,
];
for g in glyphs {
assert!(
('\u{ea60}'..='\u{ece7}').contains(&g),
"{g:?} (U+{:04X}) is outside the bundled font's PUA coverage",
g as u32
);
}
let mut seen = std::collections::HashSet::new();
for g in glyphs {
assert!(seen.insert(g), "duplicate glyph constant: {g:?}");
}
}
#[test]
fn font_family_matches_the_bundled_fonts_own_name_table() {
let parsed = sfnt_family_name(FONT_BYTES)
.expect("codicon.ttf must carry a Windows-platform Font Family (nameID 1) record");
assert_eq!(
parsed, FONT_FAMILY,
"FONT_FAMILY must match the bundled font's own name table"
);
}
fn sfnt_family_name(bytes: &[u8]) -> Option<String> {
let num_tables = u16::from_be_bytes(bytes.get(4..6)?.try_into().ok()?) as usize;
let records_start = 12;
let mut name_table_offset = None;
for i in 0..num_tables {
let rec = records_start + i * 16;
let tag = bytes.get(rec..rec + 4)?;
if tag == b"name" {
let offset =
u32::from_be_bytes(bytes.get(rec + 8..rec + 12)?.try_into().ok()?) as usize;
name_table_offset = Some(offset);
break;
}
}
let name_table = name_table_offset?;
let count = u16::from_be_bytes(bytes.get(name_table + 2..name_table + 4)?.try_into().ok()?)
as usize;
let storage_offset =
u16::from_be_bytes(bytes.get(name_table + 4..name_table + 6)?.try_into().ok()?)
as usize;
let records_start = name_table + 6;
for i in 0..count {
let rec = records_start + i * 12;
let platform_id = u16::from_be_bytes(bytes.get(rec..rec + 2)?.try_into().ok()?);
let name_id = u16::from_be_bytes(bytes.get(rec + 6..rec + 8)?.try_into().ok()?);
if platform_id != 3 || name_id != 1 {
continue;
}
let length =
u16::from_be_bytes(bytes.get(rec + 8..rec + 10)?.try_into().ok()?) as usize;
let str_offset =
u16::from_be_bytes(bytes.get(rec + 10..rec + 12)?.try_into().ok()?) as usize;
let start = name_table + storage_offset + str_offset;
let raw = bytes.get(start..start + length)?;
let units: Vec<u16> = raw
.chunks_exact(2)
.map(|c| u16::from_be_bytes([c[0], c[1]]))
.collect();
return String::from_utf16(&units).ok();
}
None
}
}