mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4272 from ruchirguitar/font-picker-indic-scripts
font-manager: Indic scripts in the font picker's script filter (#4213)
This commit is contained in:
7 files changed
+127
-5
No files matched your search
Generated
+1
@@ -2799,6 +2799,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"skrifa 0.47.0",
|
||||
"write-fonts 0.54.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -57,6 +57,8 @@ craft-fonts-build = { workspace = true }
|
||||
craft-font-manager = { path = ".", features = ["suite"] }
|
||||
# The shared preview test vectors.
|
||||
serde_json = { workspace = true }
|
||||
# Test faces built for the script filter (a script no shipped test font covers).
|
||||
write-fonts = "0.54"
|
||||
|
||||
[[test]]
|
||||
name = "it"
|
||||
|
||||
@@ -86,6 +86,15 @@ craft_ui::words! {
|
||||
script_east_asian: "fontPicker.scriptEastAsian" = "East Asian";
|
||||
script_japanese: "fontPicker.scriptJapanese" = "Japanese";
|
||||
script_thai: "fontPicker.scriptThai" = "Thai";
|
||||
script_devanagari: "fontPicker.scriptDevanagari" = "Devanagari";
|
||||
script_bengali: "fontPicker.scriptBengali" = "Bengali";
|
||||
script_gurmukhi: "fontPicker.scriptGurmukhi" = "Gurmukhi";
|
||||
script_gujarati: "fontPicker.scriptGujarati" = "Gujarati";
|
||||
script_odia: "fontPicker.scriptOdia" = "Odia";
|
||||
script_tamil: "fontPicker.scriptTamil" = "Tamil";
|
||||
script_telugu: "fontPicker.scriptTelugu" = "Telugu";
|
||||
script_kannada: "fontPicker.scriptKannada" = "Kannada";
|
||||
script_malayalam: "fontPicker.scriptMalayalam" = "Malayalam";
|
||||
any_kind: "fontPicker.anyKind" = "Any Kind";
|
||||
font_technology: "fontPicker.fontTechnology" = "Font Technology";
|
||||
preview_size: "fontPicker.previewSize" = "Font Preview Size";
|
||||
@@ -206,7 +215,7 @@ fn chosen_sample<'s, 'a>(texts: &'s [SampleText<'a>], settings: Option<&FontPick
|
||||
/// Coverage from the face: Latin, the basic Latin letters in the cmap; East Asian, the CJK Windows code pages
|
||||
/// (JIS, Simplified/Traditional Chinese, Wansung, Johab) or CJK ideographs/kana/Hangul when no code pages are declared;
|
||||
/// Japanese, the OS/2 kana or CJK unicode-range bits or the JIS code page bit, or a Japanese family name; Thai, U+0E01
|
||||
/// and U+0E32 in the cmap. Read lazily like the class.
|
||||
/// and U+0E32 in the cmap; an Indic script, its letter KA and vowel sign AA in the cmap. Read lazily like the class.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum FontScript {
|
||||
@@ -218,11 +227,43 @@ pub enum FontScript {
|
||||
Japanese,
|
||||
/// Thai.
|
||||
Thai,
|
||||
/// Devanagari.
|
||||
Devanagari,
|
||||
/// Bengali.
|
||||
Bengali,
|
||||
/// Gurmukhi.
|
||||
Gurmukhi,
|
||||
/// Gujarati.
|
||||
Gujarati,
|
||||
/// Odia.
|
||||
Odia,
|
||||
/// Tamil.
|
||||
Tamil,
|
||||
/// Telugu.
|
||||
Telugu,
|
||||
/// Kannada.
|
||||
Kannada,
|
||||
/// Malayalam.
|
||||
Malayalam,
|
||||
}
|
||||
|
||||
impl FontScript {
|
||||
/// Every script the script row can offer, in order.
|
||||
pub const ALL: [FontScript; 4] = [FontScript::Latin, FontScript::EastAsian, FontScript::Japanese, FontScript::Thai];
|
||||
pub const ALL: [FontScript; 13] = [
|
||||
FontScript::Latin,
|
||||
FontScript::EastAsian,
|
||||
FontScript::Japanese,
|
||||
FontScript::Thai,
|
||||
FontScript::Devanagari,
|
||||
FontScript::Bengali,
|
||||
FontScript::Gurmukhi,
|
||||
FontScript::Gujarati,
|
||||
FontScript::Odia,
|
||||
FontScript::Tamil,
|
||||
FontScript::Telugu,
|
||||
FontScript::Kannada,
|
||||
FontScript::Malayalam,
|
||||
];
|
||||
}
|
||||
|
||||
/// A family set the host gives (VectorCraft: Document, Recently Added), one filter toggle each. An `ordered` set that
|
||||
|
||||
@@ -200,9 +200,26 @@ const JAPANESE_RANGES: u32 = (1 << (49 - 32)) | (1 << (50 - 32)) | (1 << (59 - 3
|
||||
/// Hangul Syllables (56) plus [`JAPANESE_RANGES`].
|
||||
const EAST_ASIAN_RANGES: u32 = JAPANESE_RANGES | (1 << (56 - 32));
|
||||
|
||||
/// The Indic scripts and the first code point of their Unicode block. Every block has its letter KA and vowel sign AA
|
||||
/// at the same offsets.
|
||||
const INDIC_BLOCKS: [(FontScript, u32); 9] = [
|
||||
(FontScript::Devanagari, 0x0900),
|
||||
(FontScript::Bengali, 0x0980),
|
||||
(FontScript::Gurmukhi, 0x0A00),
|
||||
(FontScript::Gujarati, 0x0A80),
|
||||
(FontScript::Odia, 0x0B00),
|
||||
(FontScript::Tamil, 0x0B80),
|
||||
(FontScript::Telugu, 0x0C00),
|
||||
(FontScript::Kannada, 0x0C80),
|
||||
(FontScript::Malayalam, 0x0D00),
|
||||
];
|
||||
const INDIC_KA: u32 = 0x15;
|
||||
const INDIC_AA: u32 = 0x3E;
|
||||
|
||||
/// The scripts a face covers: Latin, the basic Latin letters in its `cmap`; East Asian, its East Asian Windows code
|
||||
/// pages, CJK/kana/Hangul Unicode ranges, CJK ideographs in `cmap`, or a CJK localized name; Japanese, the JIS code
|
||||
/// page, the kana or CJK Unicode ranges of its `OS/2` table, or a Japanese family name; Thai, ก and า in its `cmap`.
|
||||
/// page, the kana or CJK Unicode ranges of its `OS/2` table, or a Japanese family name; Thai, ก and า in its `cmap`;
|
||||
/// an Indic script, its letter KA and vowel sign AA in its `cmap` (no code page covers these scripts).
|
||||
fn read_scripts(catalog: &Catalog, face: &FaceInfo) -> Option<Vec<FontScript>> {
|
||||
let data = catalog.load(face).ok()?;
|
||||
let font = FontRef::from_index(&data.bytes, face.index).ok()?;
|
||||
@@ -217,7 +234,11 @@ fn read_scripts(catalog: &Catalog, face: &FaceInfo) -> Option<Vec<FontScript>> {
|
||||
let japanese = face.code_pages[0] & JIS_CODE_PAGE != 0 || ranges & JAPANESE_RANGES != 0 || japanese_name(face);
|
||||
let thai = ['\u{0E01}', '\u{0E32}'].into_iter().all(&mapped);
|
||||
let scripts = [(FontScript::Latin, latin), (FontScript::EastAsian, east_asian), (FontScript::Japanese, japanese), (FontScript::Thai, thai)];
|
||||
Some(scripts.into_iter().filter_map(|(script, covered)| covered.then_some(script)).collect())
|
||||
let indic = INDIC_BLOCKS.into_iter().map(|(script, block)| {
|
||||
let at = |offset: u32| char::from_u32(block + offset).is_some_and(&mapped);
|
||||
(script, at(INDIC_KA) && at(INDIC_AA))
|
||||
});
|
||||
Some(scripts.into_iter().chain(indic).filter_map(|(script, covered)| covered.then_some(script)).collect())
|
||||
}
|
||||
|
||||
/// The class of a family from its regular face, `None` when the catalog lacks it.
|
||||
|
||||
@@ -350,6 +350,15 @@ fn script_label(script: FontScript, words: &FontPickerWords) -> Label {
|
||||
FontScript::EastAsian => words.script_east_asian.clone(),
|
||||
FontScript::Japanese => words.script_japanese.clone(),
|
||||
FontScript::Thai => words.script_thai.clone(),
|
||||
FontScript::Devanagari => words.script_devanagari.clone(),
|
||||
FontScript::Bengali => words.script_bengali.clone(),
|
||||
FontScript::Gurmukhi => words.script_gurmukhi.clone(),
|
||||
FontScript::Gujarati => words.script_gujarati.clone(),
|
||||
FontScript::Odia => words.script_odia.clone(),
|
||||
FontScript::Tamil => words.script_tamil.clone(),
|
||||
FontScript::Telugu => words.script_telugu.clone(),
|
||||
FontScript::Kannada => words.script_kannada.clone(),
|
||||
FontScript::Malayalam => words.script_malayalam.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -232,7 +232,7 @@ fn font_picker_filter_chips(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
FamilySet::new("document", "Document", &document),
|
||||
FamilySet::new("added", "Recently Added", &added).ordered().enabled(false).hint("No fonts were added recently"),
|
||||
];
|
||||
let scripts = [FontScript::Latin, FontScript::Japanese, FontScript::Thai];
|
||||
let scripts = [FontScript::Latin, FontScript::Japanese, FontScript::Thai, FontScript::Devanagari, FontScript::Gujarati];
|
||||
let filters = FontFilters { sets: vec!["document".to_owned()], ..FontFilters::default() };
|
||||
let extras = Extras { sets: &sets, scripts: &scripts, layout: FilterLayout::Chips, ..Extras::default() };
|
||||
show_with(ui, data, demo("Hack", FontPickerSettings::default(), filters), &catalog(), &extras)
|
||||
|
||||
@@ -385,6 +385,54 @@ fn a_thai_and_a_cjk_face_each_pass_their_own_scripts_filter() {
|
||||
assert!(!thai.iter().chain(&japanese).any(|family| family.contains("Inter")), "Inter covers neither");
|
||||
}
|
||||
|
||||
/// A face named `family` whose `cmap` maps the basic Latin letters and `extra`: only the tables the catalog and the
|
||||
/// coverage read, with an empty `glyf` so it counts as drawable.
|
||||
fn face_mapping(family: &str, extra: &[char]) -> Vec<u8> {
|
||||
use write_fonts::FontBuilder;
|
||||
use write_fonts::tables::cmap::Cmap;
|
||||
use write_fonts::tables::maxp::Maxp;
|
||||
use write_fonts::tables::name::{Name, NameRecord};
|
||||
use write_fonts::tables::os2::Os2;
|
||||
use write_fonts::types::{GlyphId, NameId, Tag};
|
||||
|
||||
let chars: Vec<char> = ('A'..='Z').chain('a'..='z').chain(extra.iter().copied()).collect();
|
||||
let cmap = Cmap::from_mappings(chars.iter().zip(1u32..).map(|(&c, g)| (c, GlyphId::new(g)))).expect("a cmap");
|
||||
let mut records = vec![
|
||||
NameRecord::new(3, 1, 0x409, NameId::new(1), family.to_owned().into()),
|
||||
NameRecord::new(3, 1, 0x409, NameId::new(2), "Regular".to_owned().into()),
|
||||
NameRecord::new(3, 1, 0x409, NameId::new(4), format!("{family} Regular").into()),
|
||||
NameRecord::new(3, 1, 0x409, NameId::new(6), format!("{}-Regular", family.replace(' ', "")).into()),
|
||||
];
|
||||
records.sort_by_key(|r| r.name_id);
|
||||
let mut builder = FontBuilder::new();
|
||||
builder.add_table(&Name::new(records)).expect("name");
|
||||
builder.add_table(&Os2 { us_weight_class: 400, us_width_class: 5, ..Default::default() }).expect("OS/2");
|
||||
builder.add_table(&cmap).expect("cmap");
|
||||
builder.add_table(&Maxp::new(u16::try_from(chars.len() + 1).expect("few glyphs"))).expect("maxp");
|
||||
builder.add_raw(Tag::new(b"glyf"), vec![0u8; 4]);
|
||||
builder.build()
|
||||
}
|
||||
|
||||
/// An Indic script is read from the face's `cmap` (KA and vowel sign AA); no code page marks it. A face with
|
||||
/// Devanagari passes the Devanagari filter, not the Gujarati one, and Latin-only and Thai faces pass neither.
|
||||
#[trial]
|
||||
fn an_indic_face_passes_its_own_scripts_filter() {
|
||||
const THAI: &[u8] = include_bytes!("../../../font-subset/tests/data/sarabun/Sarabun-Regular.ttf");
|
||||
let deva = face_mapping("Test Devanagari", &['\u{0915}', '\u{093E}']);
|
||||
// A lone KA, as a symbol font might carry, is not coverage.
|
||||
let ka_only = face_mapping("Test Lone Ka", &['\u{0A95}']);
|
||||
let catalog = catalog_of(&[INTER_REGULAR, THAI, &deva, &ka_only]);
|
||||
let with = |script: FontScript| {
|
||||
let mut filters = FontFilters::default();
|
||||
filters.script = Some(script);
|
||||
listed(&catalog, &filters, &[])
|
||||
};
|
||||
let devanagari = with(FontScript::Devanagari);
|
||||
assert!(devanagari.iter().any(|family| family.contains("Test Devanagari")), "Devanagari: {devanagari:?}");
|
||||
assert!(!devanagari.iter().any(|family| family.contains("Inter") || family.contains("Sarabun")), "Devanagari: {devanagari:?}");
|
||||
assert!(with(FontScript::Gujarati).is_empty(), "nothing covers Gujarati: {:?}", with(FontScript::Gujarati));
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_host_set_keeps_only_its_families_and_an_ordered_one_lists_them_in_its_order() {
|
||||
let catalog = three_faces();
|
||||
|
||||
Reference in new issue
Block a user