#[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_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const CASE_SENSITIVE: char = '\u{eab1}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const WHOLE_WORD: char = '\u{eb7e}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const REGEX: char = '\u{eb38}';
#[cfg_attr(
not(any(
feature = "gtk",
all(feature = "macos", target_os = "macos"),
all(feature = "win", target_os = "windows")
)),
allow(dead_code)
)]
pub(crate) const PRESERVE_CASE: char = '\u{eb2e}';
#[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,
CASE_SENSITIVE,
WHOLE_WORD,
REGEX,
PRESERVE_CASE,
];
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 glyph_constants_have_a_real_glyph_in_the_bundled_fonts_cmap() {
let glyphs = [
CHEVRON_DOWN,
CHEVRON_RIGHT,
CHEVRON_LEFT,
SEARCH,
CHECK,
CLOSE,
DIRTY,
SORT_ASCENDING,
SORT_DESCENDING,
CASE_SENSITIVE,
WHOLE_WORD,
REGEX,
PRESERVE_CASE,
];
for g in glyphs {
let glyph_id = cmap_glyph_id(FONT_BYTES, g as u32)
.unwrap_or_else(|| panic!("codicon.ttf's cmap table could not be parsed at all"));
assert_ne!(
glyph_id, 0,
"{g:?} (U+{:04X}) falls inside the PUA numeric range but has no glyph \
mapped in codicon.ttf's own cmap table — likely a transposed digit \
landing on an unmapped codepoint",
g as u32
);
}
}
fn cmap_glyph_id(bytes: &[u8], codepoint: u32) -> Option<u32> {
let num_tables = u16::from_be_bytes(bytes.get(4..6)?.try_into().ok()?) as usize;
let records_start = 12;
let mut cmap_offset = None;
for i in 0..num_tables {
let rec = records_start + i * 16;
let tag = bytes.get(rec..rec + 4)?;
if tag == b"cmap" {
let offset =
u32::from_be_bytes(bytes.get(rec + 8..rec + 12)?.try_into().ok()?) as usize;
cmap_offset = Some(offset);
break;
}
}
let cmap = cmap_offset?;
let num_subtables =
u16::from_be_bytes(bytes.get(cmap + 2..cmap + 4)?.try_into().ok()?) as usize;
let mut best_subtable_offset = None;
for i in 0..num_subtables {
let rec = cmap + 4 + i * 8;
let platform_id = u16::from_be_bytes(bytes.get(rec..rec + 2)?.try_into().ok()?);
let encoding_id = u16::from_be_bytes(bytes.get(rec + 2..rec + 4)?.try_into().ok()?);
let offset = u32::from_be_bytes(bytes.get(rec + 4..rec + 8)?.try_into().ok()?) as usize;
let is_better = match (platform_id, encoding_id) {
(3, 10) => true,
(3, 1) if best_subtable_offset.is_none() => true,
_ => false,
};
if is_better {
best_subtable_offset = Some(cmap + offset);
}
}
let sub = best_subtable_offset?;
let format = u16::from_be_bytes(bytes.get(sub..sub + 2)?.try_into().ok()?);
match format {
4 => cmap_format4_glyph_id(bytes, sub, codepoint),
12 => cmap_format12_glyph_id(bytes, sub, codepoint),
_ => None,
}
}
fn cmap_format4_glyph_id(bytes: &[u8], sub: usize, codepoint: u32) -> Option<u32> {
if codepoint > 0xFFFF {
return Some(0);
}
let c = codepoint as u16;
let seg_count_x2 =
u16::from_be_bytes(bytes.get(sub + 6..sub + 8)?.try_into().ok()?) as usize;
let seg_count = seg_count_x2 / 2;
let end_codes_start = sub + 14;
let start_codes_start = end_codes_start + seg_count_x2 + 2; let id_deltas_start = start_codes_start + seg_count_x2;
let id_range_offsets_start = id_deltas_start + seg_count_x2;
for i in 0..seg_count {
let end_code = u16::from_be_bytes(
bytes
.get(end_codes_start + i * 2..end_codes_start + i * 2 + 2)?
.try_into()
.ok()?,
);
if c > end_code {
continue;
}
let start_code = u16::from_be_bytes(
bytes
.get(start_codes_start + i * 2..start_codes_start + i * 2 + 2)?
.try_into()
.ok()?,
);
if c < start_code {
return Some(0);
}
let id_delta = i16::from_be_bytes(
bytes
.get(id_deltas_start + i * 2..id_deltas_start + i * 2 + 2)?
.try_into()
.ok()?,
);
let id_range_offset_pos = id_range_offsets_start + i * 2;
let id_range_offset = u16::from_be_bytes(
bytes
.get(id_range_offset_pos..id_range_offset_pos + 2)?
.try_into()
.ok()?,
);
if id_range_offset == 0 {
return Some(((c as i32 + id_delta as i32) as u16) as u32);
}
let glyph_index_addr =
id_range_offset_pos + id_range_offset as usize + 2 * (c - start_code) as usize;
let raw_glyph_id = u16::from_be_bytes(
bytes
.get(glyph_index_addr..glyph_index_addr + 2)?
.try_into()
.ok()?,
);
if raw_glyph_id == 0 {
return Some(0);
}
return Some(((raw_glyph_id as i32 + id_delta as i32) as u16) as u32);
}
Some(0)
}
fn cmap_format12_glyph_id(bytes: &[u8], sub: usize, codepoint: u32) -> Option<u32> {
let num_groups =
u32::from_be_bytes(bytes.get(sub + 12..sub + 16)?.try_into().ok()?) as usize;
let groups_start = sub + 16;
for i in 0..num_groups {
let rec = groups_start + i * 12;
let start_char = u32::from_be_bytes(bytes.get(rec..rec + 4)?.try_into().ok()?);
let end_char = u32::from_be_bytes(bytes.get(rec + 4..rec + 8)?.try_into().ok()?);
if codepoint < start_char || codepoint > end_char {
continue;
}
let start_glyph = u32::from_be_bytes(bytes.get(rec + 8..rec + 12)?.try_into().ok()?);
return Some(start_glyph + (codepoint - start_char));
}
Some(0)
}
#[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
}
}