diff --git a/crates/text/src/fontdb/scan.rs b/crates/text/src/fontdb/scan.rs index e25891fb32..f410353573 100644 --- a/crates/text/src/fontdb/scan.rs +++ b/crates/text/src/fontdb/scan.rs @@ -3,6 +3,8 @@ use super::build::{style_italic, style_weight}; use super::dirs::sfnt_of; use super::face::{MAX_AXES, MAX_INSTANCES}; use super::names::{all_names, name, norm}; +#[cfg(not(target_arch = "wasm32"))] +use super::traits::may_set_thai; use super::traits::{FaceKeys, FaceStyle, FontTraits, face_styles}; use super::{FileRef, MetadataProvider, Path, StringId, Tag}; @@ -126,40 +128,51 @@ pub(super) fn font_files(path: &Path) -> Vec> { const OUTLINE_TABLES: [&[u8; 4]; 6] = [b"glyf", b"CFF ", b"CFF2", b"VARC", b"sbix", b"CBDT"]; /// The styles of every face in the font file at `path` ([`face_styles`]), reading only its table -/// directories and `name`, `fvar` and `OS/2` tables: a scan opens hundreds of font files, many of +/// directories and `name`, `fvar` and `OS/2` tables (and the `cmap` table of a face that may set +/// Thai, [`may_set_thai`]): a scan opens hundreds of font files, many of /// them megabytes long. Faces without outlines the font engine draws (no [`OUTLINE_TABLES`] table) /// are left out. #[cfg(not(target_arch = "wasm32"))] pub(super) fn file_face_names(path: &Path) -> Vec { - file_face_names_within(path, MAX_FACES, u64::MAX) + file_face_names_reading(path, MAX_FACES, u64::MAX, true) } -/// [`file_face_names`], reading at most `max_faces` faces and `max_read` bytes of the file in all. -/// A suitcase font's resource fork is read whole when it is at most `max_read` bytes long, and the -/// limits then apply to each of its fonts; a longer fork gives none. +/// [`file_face_names`], reading at most `max_faces` faces and `max_read` bytes of the file in all, +/// and no `cmap` table: a face's [`FontTraits::thai`] is false. A suitcase font's resource fork is +/// read whole when it is at most `max_read` bytes long, and the limits then apply to each of its +/// fonts; a longer fork gives none. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn file_face_names_within(path: &Path, max_faces: u32, max_read: u64) -> Vec { + file_face_names_reading(path, max_faces, max_read, false) +} + +/// [`file_face_names_within`], reading the `cmap` table of a face that may set Thai when +/// `read_cmap` is true. +#[cfg(not(target_arch = "wasm32"))] +fn file_face_names_reading(path: &Path, max_faces: u32, max_read: u64, read_cmap: bool) -> Vec { if crate::suitcase::is_suitcase(path) { if crate::suitcase::fork_len(path) > max_read { return vec![]; } return crate::suitcase::read_fonts(path) .into_iter() - .flat_map(|font| reader_face_names_within(std::io::Cursor::new(font), max_faces, max_read)) + .flat_map(|font| reader_face_names_within(std::io::Cursor::new(font), max_faces, max_read, read_cmap)) .collect(); } match std::fs::File::open(path) { - Ok(file) => reader_face_names_within(file, max_faces, max_read), + Ok(file) => reader_face_names_within(file, max_faces, max_read, read_cmap), Err(_) => vec![], } } -/// [`file_face_names_within`] for the font file `file` reads. +/// [`file_face_names_reading`] for the font file `file` reads. #[cfg(not(target_arch = "wasm32"))] -fn reader_face_names_within(mut file: impl std::io::Read + std::io::Seek, max_faces: u32, max_read: u64) -> Vec { +fn reader_face_names_within(mut file: impl std::io::Read + std::io::Seek, max_faces: u32, max_read: u64, read_cmap: bool) -> Vec { use std::io::SeekFrom; - /// The largest table read from a (possibly damaged) file. + /// The largest table read from a (possibly damaged) file, other than `cmap`. const MAX_NAME_TABLE: u32 = 1 << 20; + /// The largest `cmap` table read. + const MAX_CMAP_TABLE: u32 = 16 << 20; /// The `post` table's header: version, italic angle, underline and fixed-pitch fields. const POST_HEADER: u32 = 32; let mut read = 0u64; @@ -188,11 +201,12 @@ fn reader_face_names_within(mut file: impl std::io::Read + std::io::Seek, max_fa if !records.as_chunks::<16>().0.iter().any(|r| OUTLINE_TABLES.iter().any(|t| r.starts_with(*t))) { return None; } - // A table's first `most` bytes at most; a table read whole longer than `MAX_NAME_TABLE` gives nothing. + // A table's first `most` bytes at most; a table read whole longer than `MAX_NAME_TABLE` + // (`MAX_CMAP_TABLE` for `cmap`) gives nothing. let mut table_within = |tag: &[u8; 4], most: u32| -> Option> { let rec: &[u8] = records.as_chunks::<16>().0.iter().find(|r| r.starts_with(tag))?; let (offset, len) = (be32(rec, 8)?, be32(rec, 12)?.min(most)); - if len > MAX_NAME_TABLE { + if len > if tag == b"cmap" { MAX_CMAP_TABLE } else { MAX_NAME_TABLE } { return None; } read_at(offset.into(), len as usize) @@ -217,7 +231,13 @@ fn reader_face_names_within(mut file: impl std::io::Read + std::io::Seek, max_fa tables.push((tag, data.as_slice())); } } - let font = sfnt_of(&tables)?; + let mut font = sfnt_of(&tables)?; + // Its character map too when it may set Thai, to tell whether it does. + let cmap = if read_cmap && may_set_thai(&skrifa::FontRef::new(&font).ok()?) { table_within(b"cmap", u32::MAX) } else { None }; + if let Some(cmap) = &cmap { + tables.push((b"cmap", cmap.as_slice())); + font = sfnt_of(&tables)?; + } Some(face_styles(&skrifa::FontRef::new(&font).ok()?)) }) .flatten() diff --git a/crates/text/src/fontdb/traits.rs b/crates/text/src/fontdb/traits.rs index deff6ce054..c25ce20c4f 100644 --- a/crates/text/src/fontdb/traits.rs +++ b/crates/text/src/fontdb/traits.rs @@ -95,7 +95,7 @@ impl FontClass { #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct FontTraits { pub japanese: bool, - /// Its character map has Thai (ก and า). + /// Its OS/2 table allows Thai ([`may_set_thai`]) and its character map has Thai (ก and า). pub thai: bool, pub class: FontClass, /// The face has variation axes (an `fvar` table of at most 64 axes); for a family, one of @@ -216,8 +216,21 @@ impl FontLikeness { } } +/// Whether a face may set Thai by its OS/2 table: it has none, or the table's Unicode ranges have +/// Thai (bit 24) or its code pages have Thai (bit 16, code page 874). Only such a face has its +/// character map checked for Thai ([`FontTraits::thai`]); the system font scan reads a face's +/// `cmap` table only then ([`super::scan::file_face_names`]). +pub(super) fn may_set_thai(font: &skrifa::FontRef<'_>) -> bool { + use skrifa::raw::TableProvider; + match font.os2() { + Ok(o) => o.ul_unicode_range_1() & (1 << 24) != 0 || o.ul_code_page_range_1().unwrap_or(0) & (1 << 16) != 0, + Err(_) => true, + } +} + impl FontTraits { - /// From a face's OS/2 and `fvar` tables (when it has them) and names. + /// From a face's OS/2 and `fvar` tables (when it has them), names and, when it may set Thai + /// ([`may_set_thai`]), character map. fn of(font: &skrifa::FontRef<'_>, keys: &FaceKeys) -> Self { use skrifa::raw::TableProvider; let os2 = font.os2().ok(); @@ -230,7 +243,7 @@ impl FontTraits { }; // Hiragana (bit 49) or Katakana (bit 50) in the Unicode ranges, or the JIS code page. let japanese = ranges2 & (0b11 << 17) != 0 || codepages & (1 << 17) != 0 || has_japanese_name; - let thai = ['\u{0E01}', '\u{0E32}'].iter().all(|c| font.charmap().map(*c).is_some()); + let thai = may_set_thai(font) && ['\u{0E01}', '\u{0E32}'].iter().all(|c| font.charmap().map(*c).is_some()); let family_type = panose.first().copied().unwrap_or(0); let serif_style = panose.get(1).copied().unwrap_or(0); let proportion = panose.get(3).copied().unwrap_or(0); diff --git a/crates/text/src/test_fonts.rs b/crates/text/src/test_fonts.rs index 1ba4fe3f99..d8f91f8d50 100644 --- a/crates/text/src/test_fonts.rs +++ b/crates/text/src/test_fonts.rs @@ -705,6 +705,51 @@ fn repeat_font(family: &str, chars: &[(char, char)]) -> Option> { Some(builder.build()) } +/// The families [`thai_font`] can be named, two fonts that set Thai. +pub const THAI_FAMILY: &str = "Thaitest Sans"; +pub const THAI_OTHER_FAMILY: &str = "Thaiothertest Sans"; + +/// Bundled Source Sans 3 Regular as `family`, with a cmap that maps ก and า to the glyphs of A and +/// B beside the Latin letters and the space. With `claimed`, its OS/2 table has the Thai Unicode +/// range (bit 24) set; without, neither that range nor the Thai code page (bit 16). `None` when it +/// can't be built. +pub fn thai_font(family: &str, claimed: bool) -> Option> { + use write_fonts::tables::cmap::Cmap; + + const BASE: &[u8] = include_bytes!("../../../assets/fonts/SourceSans3-Regular.ttf"); + let font = skrifa::FontRef::new(BASE).ok()?; + let charmap = font.charmap(); + let glyph = |c| Some(write_fonts::types::GlyphId::new(charmap.map(c)?.to_u32())); + let mut mappings: Vec<(char, write_fonts::types::GlyphId)> = vec![('\u{0E01}', glyph('A')?), ('\u{0E32}', glyph('B')?)]; + for c in ('A'..='Z').chain('a'..='z').chain([' ']) { + mappings.push((c, glyph(c)?)); + } + let cmap = Cmap::from_mappings(mappings).ok()?; + let full = format!("{family} Regular"); + let postscript = format!("{}-Regular", family.replace(' ', "")); + let name = name_table(&[(1, family), (2, "Regular"), (4, &full), (6, &postscript)])?; + let mut builder = FontBuilder::new(); + builder.add_table(&cmap).ok()?; + builder.add_raw(Tag::new(b"name"), name); + for r in font.table_directory.table_records() { + let tag = Tag::new(&r.tag().to_be_bytes()); + if tag == Tag::new(b"GSUB") || builder.contains(tag) { + continue; + } + let mut data = font.table_data(r.tag())?.as_bytes().to_vec(); + if tag == Tag::new(b"OS/2") { + // ulUnicodeRange1 at bytes 42 to 46, ulCodePageRange1 at bytes 78 to 82. + let ranges = u32::from_be_bytes(data.get(42..46)?.try_into().ok()?); + let ranges = if claimed { ranges | 1 << 24 } else { ranges & !(1 << 24) }; + data.get_mut(42..46)?.copy_from_slice(&ranges.to_be_bytes()); + let pages = u32::from_be_bytes(data.get(78..82)?.try_into().ok()?) & !(1 << 16); + data.get_mut(78..82)?.copy_from_slice(&pages.to_be_bytes()); + } + builder.add_raw(tag, data); + } + Some(builder.build()) +} + /// The family of [`half_form_font`]. pub const HALF_FORM_FAMILY: &str = "Halftest Sans"; diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index 06c4db899c..08f6584186 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -590,6 +590,39 @@ fn the_scan_reads_a_familys_likeness_without_loading_it() { let _ = std::fs::remove_dir_all(&dir); } +/// Font menus' Thai filter and Show Similar Fonts: the scan tells whether a family sets Thai as +/// loading it does. A face whose OS/2 table names neither the Thai range nor the Thai code page does +/// not set Thai, loaded or scanned, whatever its character map holds. +#[test] +fn the_scan_tells_whether_a_family_sets_thai_without_loading_it() { + use crate::test_fonts::{THAI_FAMILY, THAI_OTHER_FAMILY, thai_font}; + let dir = std::env::temp_dir().join(format!("vc-sysfonts-{}-thai", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + for claimed in [true, false] { + std::fs::write(dir.join("Thaitest-Regular.ttf"), thai_font(THAI_FAMILY, claimed).unwrap()).unwrap(); + let scanned = FontDb::with_font_dirs(vec![dir.clone()]); + let thai = scanned.family_traits(THAI_FAMILY).thai; + assert!(!scanned.is_loaded(THAI_FAMILY), "the scan's traits, nothing loaded"); + let loaded = FontDb::with_font_dirs(vec![]); + assert_eq!(loaded.add_font(thai_font(THAI_FAMILY, claimed).unwrap()), 1); + assert_eq!(thai, loaded.family_traits(THAI_FAMILY).thai, "claimed: {claimed}"); + assert_eq!(thai, claimed); + } + // A loaded Thai family lists a scanned Thai family before a Latin family that looks the same, + // and the order stays when the scanned family is loaded. + std::fs::write(dir.join("Thaitest-Regular.ttf"), thai_font(THAI_FAMILY, true).unwrap()).unwrap(); + let db = FontDb::with_font_dirs(vec![dir.clone()]); + assert_eq!(db.add_font(thai_font(THAI_OTHER_FAMILY, true).unwrap()), 1); + let families: Vec = ["Source Sans 3", THAI_FAMILY, THAI_OTHER_FAMILY].map(String::from).to_vec(); + let expected: Vec = [THAI_OTHER_FAMILY, THAI_FAMILY, "Source Sans 3"].map(String::from).to_vec(); + assert_eq!(db.similar_families(THAI_OTHER_FAMILY, &families), expected); + assert!(!db.is_loaded(THAI_FAMILY)); + assert_eq!(db.add_font(thai_font(THAI_FAMILY, true).unwrap()), 1); + assert_eq!(db.similar_families(THAI_OTHER_FAMILY, &families), expected); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn registered_fonts_outside_the_font_folders_are_their_full_paths() { let dir = std::env::temp_dir().join("Fonts");