mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
feat(font-manager): add FontScript::EastAsian and honor bound filters in Plain picker (#3918) (#3919)
This commit is contained in:
1 parent
32cc7a13ed
commit
3d8df3f30f
4 files changed
+71
-14
No files matched your search
@@ -83,6 +83,7 @@ craft_ui::words! {
|
||||
served: "fontPicker.served" = "Not installed";
|
||||
all_scripts: "fontPicker.allScripts" = "All Scripts";
|
||||
script_latin: "fontPicker.scriptLatin" = "Latin";
|
||||
script_east_asian: "fontPicker.scriptEastAsian" = "East Asian";
|
||||
script_japanese: "fontPicker.scriptJapanese" = "Japanese";
|
||||
script_thai: "fontPicker.scriptThai" = "Thai";
|
||||
any_kind: "fontPicker.anyKind" = "Any Kind";
|
||||
@@ -202,14 +203,17 @@ fn chosen_sample<'s, 'a>(texts: &'s [SampleText<'a>], settings: Option<&FontPick
|
||||
texts.iter().find(|text| Some(text.key) == key).or_else(|| texts.first())
|
||||
}
|
||||
|
||||
/// Coverage from the face: Latin, the basic Latin letters in the cmap; 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.
|
||||
/// 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.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum FontScript {
|
||||
/// The basic Latin letters.
|
||||
Latin,
|
||||
/// Chinese, Japanese or Korean fonts.
|
||||
EastAsian,
|
||||
/// Japanese kana and CJK.
|
||||
Japanese,
|
||||
/// Thai.
|
||||
@@ -218,7 +222,7 @@ pub enum FontScript {
|
||||
|
||||
impl FontScript {
|
||||
/// Every script the script row can offer, in order.
|
||||
pub const ALL: [FontScript; 3] = [FontScript::Latin, FontScript::Japanese, FontScript::Thai];
|
||||
pub const ALL: [FontScript; 4] = [FontScript::Latin, FontScript::EastAsian, FontScript::Japanese, FontScript::Thai];
|
||||
}
|
||||
|
||||
/// A family set the host gives (VectorCraft: Document, Recently Added), one filter toggle each. An `ordered` set that
|
||||
|
||||
@@ -166,11 +166,22 @@ pub(super) fn shown_name(info: &Info, family: &str, language: &str, english: boo
|
||||
info.localized.iter().find(exact).or_else(|| info.localized.iter().find(same_language)).map_or_else(|| family.to_owned(), |n| n.name.clone())
|
||||
}
|
||||
|
||||
/// Whether a family name of the face is in Japanese.
|
||||
/// Whether a family name of the face is in any of `languages` (primary subtags, case-insensitive).
|
||||
fn has_localized_language(face: &FaceInfo, languages: &[&str]) -> bool {
|
||||
face.localized_families.iter().any(|n| {
|
||||
n.language.as_deref().and_then(|l| l.split(['-', '_']).next()).is_some_and(|l| languages.iter().any(|wanted| l.eq_ignore_ascii_case(wanted)))
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a family name of the face is in Japanese.
|
||||
fn japanese_name(face: &FaceInfo) -> bool {
|
||||
face.localized_families
|
||||
.iter()
|
||||
.any(|n| n.language.as_deref().and_then(|l| l.split(['-', '_']).next()).is_some_and(|l| l.eq_ignore_ascii_case("ja")))
|
||||
has_localized_language(face, &["ja"])
|
||||
}
|
||||
|
||||
/// Whether a family name of the face is in an East Asian language (`ja`, `zh`, or `ko`).
|
||||
fn east_asian_name(face: &FaceInfo) -> bool {
|
||||
has_localized_language(face, &["ja", "zh", "ko"])
|
||||
}
|
||||
|
||||
/// A face's class, PANOSE and weight from its `OS/2` and `post` tables, as the catalog's scan recorded them.
|
||||
@@ -181,21 +192,31 @@ fn read_traits(face: &FaceInfo) -> Traits {
|
||||
|
||||
/// The JIS/Japan bit of `ulCodePageRange1`.
|
||||
const JIS_CODE_PAGE: u32 = 1 << 17;
|
||||
/// The five East Asian Windows code pages of `ulCodePageRange1`: JIS/Japan (17),
|
||||
/// Simplified Chinese (18), Korean Wansung (19), Traditional Chinese (20), Korean Johab (21).
|
||||
const EAST_ASIAN_CODE_PAGES: u32 = 0b1_1111 << 17;
|
||||
/// The Hiragana (49), Katakana (50) and CJK Unified Ideographs (59) bits of `ulUnicodeRange2`, which holds bits 32 to 63.
|
||||
const JAPANESE_RANGES: u32 = (1 << (49 - 32)) | (1 << (50 - 32)) | (1 << (59 - 32));
|
||||
/// Hangul Syllables (56) plus [`JAPANESE_RANGES`].
|
||||
const EAST_ASIAN_RANGES: u32 = JAPANESE_RANGES | (1 << (56 - 32));
|
||||
|
||||
/// The scripts a face covers: Latin, the basic Latin letters in its `cmap`; 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`.
|
||||
/// 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`.
|
||||
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()?;
|
||||
let map = font.charmap();
|
||||
let mapped = |c: char| map.map(c).is_some();
|
||||
let latin = ('A'..='Z').chain('a'..='z').all(&mapped);
|
||||
let ranges = font.os2().ok().map_or(0, |os2| os2.ul_unicode_range_2());
|
||||
let os2 = font.os2().ok();
|
||||
let ranges = os2.as_ref().map_or(0, |t| t.ul_unicode_range_2());
|
||||
let code_page_1 = os2.as_ref().and_then(|t| t.ul_code_page_range_1()).filter(|&p| p != 0).unwrap_or(face.code_pages[0]);
|
||||
let cjk_glyphs = ['中', '国', '日'].into_iter().all(&mapped);
|
||||
let east_asian = code_page_1 & EAST_ASIAN_CODE_PAGES != 0 || ranges & EAST_ASIAN_RANGES != 0 || cjk_glyphs || east_asian_name(face);
|
||||
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::Japanese, japanese), (FontScript::Thai, thai)];
|
||||
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())
|
||||
}
|
||||
|
||||
|
||||
@@ -347,6 +347,7 @@ const PREVIEW_SIZES: [FontPreviewSize; 3] = [FontPreviewSize::Small, FontPreview
|
||||
fn script_label(script: FontScript, words: &FontPickerWords) -> Label {
|
||||
match script {
|
||||
FontScript::Latin => words.script_latin.clone(),
|
||||
FontScript::EastAsian => words.script_east_asian.clone(),
|
||||
FontScript::Japanese => words.script_japanese.clone(),
|
||||
FontScript::Thai => words.script_thai.clone(),
|
||||
}
|
||||
@@ -581,15 +582,16 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
|
||||
let family = cx.value().cloned().unwrap_or_default();
|
||||
let settings = read(&mut picker.settings, cx);
|
||||
let plain = picker.kind == FontPickerKind::Plain;
|
||||
// The plain list has no filters: neither the bar nor the column shows, and nothing narrows the list.
|
||||
let mut filters = read(&mut picker.filters, cx).filter(|_| !plain);
|
||||
let bound_filters = read(&mut picker.filters, cx);
|
||||
// The plain list hides the filter bar and class column while still applying filters bound by the host.
|
||||
let mut filters = bound_filters.clone().filter(|_| !plain);
|
||||
let actions = picker.actions;
|
||||
let has_filters = filters.is_some();
|
||||
let chips = picker.filter_layout == FilterLayout::Chips;
|
||||
let on_white = settings.as_ref().is_some_and(|s| s.samples_on_white);
|
||||
let mut preview_size = settings.as_ref().map(|s| s.preview);
|
||||
let (sets, scripts) = (picker.sets, picker.scripts);
|
||||
let narrowing = filters.clone().unwrap_or_default();
|
||||
let narrowing = bound_filters.unwrap_or_default();
|
||||
let catalog = picker.catalog;
|
||||
let infos = infos_of(&cx.response.ctx, cx.id, catalog);
|
||||
let language_code = ui_language(&cx.env);
|
||||
|
||||
@@ -1319,3 +1319,33 @@ fn the_closed_field_skips_building_stops_while_idle_and_steps_on_input() {
|
||||
assert_eq!(seen.borrow().picks.last().map(|(f, _)| f.as_str()), Some(all[2].as_str()), "wheel end commits the stepped family");
|
||||
assert_eq!(craft_font_manager::picker::stop_builds(fx.ctx()), after_wheel, "wheel end and idle frames do not rebuild stops");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn east_asian_script_filter_keeps_cjk_faces_and_narrows_a_plain_picker() {
|
||||
const THAI: &[u8] = include_bytes!("../../../font-subset/tests/data/sarabun/Sarabun-Regular.ttf");
|
||||
const CJK: &[u8] = include_bytes!("../../../ui-contract/tests/fonts/NotoSansCJKsc-test.otf");
|
||||
let catalog = catalog_of(&[INTER_REGULAR, THAI, CJK]);
|
||||
let mut filters = FontFilters::default();
|
||||
filters.script = Some(FontScript::EastAsian);
|
||||
|
||||
let filtered = listed(&catalog, &filters, &[]);
|
||||
assert!(filtered.iter().any(|family| family.contains("CJK")), "expected East Asian family in {filtered:?}");
|
||||
assert!(!filtered.iter().any(|family| family.contains("Inter") || family.contains("Sarabun")), "unexpected non-CJK family in {filtered:?}");
|
||||
|
||||
let mut family = String::new();
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
let _ = FontPicker::new("Asian text font", Bind::bound(&mut family), &catalog)
|
||||
.kind(FontPickerKind::Plain)
|
||||
.filters(Bind::given(filters.clone()))
|
||||
.show(ui);
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
open(&mut fx);
|
||||
|
||||
let words = FontPickerWords::default();
|
||||
assert!(!present(&fx, words.clear_all.as_str()), "plain picker keeps the filter bar hidden");
|
||||
assert!(present(&fx, "Noto Sans CJK SC"), "plain picker with East Asian filter lists CJK font");
|
||||
assert!(!present(&fx, "Inter"), "plain picker with East Asian filter excludes Latin font");
|
||||
assert!(!present(&fx, "Sarabun"), "plain picker with East Asian filter excludes Thai font");
|
||||
}
|
||||
Reference in new issue
Block a user