| 1 |
/* |
| 2 |
* fontconfig/fc-fontations/lang.rs |
| 3 |
* |
| 4 |
* Copyright 2025 Google LLC. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 7 |
* documentation for any purpose is hereby granted without fee, provided that |
| 8 |
* the above copyright notice appear in all copies and that both that |
| 9 |
* copyright notice and this permission notice appear in supporting |
| 10 |
* documentation, and that the name of the author(s) not be used in |
| 11 |
* advertising or publicity pertaining to distribution of the software without |
| 12 |
* specific, written prior permission. The authors make no |
| 13 |
* representations about the suitability of this software for any purpose. It |
| 14 |
* is provided "as is" without express or implied warranty. |
| 15 |
* |
| 16 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 22 |
* PERFORMANCE OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
use read_fonts::TableProvider; |
| 26 |
use skrifa::FontRef; |
| 27 |
use std::ffi::CString; |
| 28 |
|
| 29 |
// See logic and comment in FcFreeTypeLangSet, check for Han charsets to make fonts |
| 30 |
// which advertise support for a single language not support other Han languages. |
| 31 |
fn exclusive_lang_in_codepage_range(codepage_ranges: u64) -> Option<String> { |
| 32 |
// u32 is the position of the bit in the OS/2 codepage range. |
| 33 |
const LANG_MAP: [(u32, &str); 4] = [(17, "ja"), (18, "zh-cn"), (19, "ko"), (20, "zh-tw")]; |
| 34 |
|
| 35 |
let mut found_languages = LANG_MAP |
| 36 |
.iter() |
| 37 |
.filter(move |bit_lang_pair| codepage_ranges & (1 << bit_lang_pair.0) != 0); |
| 38 |
|
| 39 |
match (found_languages.next(), found_languages.next()) { |
| 40 |
(Some(bit_lang_pair), None) => Some(bit_lang_pair.1.to_string()), |
| 41 |
_ => None, |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Returns the language identifier string if according to OS/2 codepage ranges |
| 46 |
// the font advertises support for only one Han font, otherwise none. |
| 47 |
pub fn exclusive_lang(font: &FontRef) -> Option<CString> { |
| 48 |
let os2 = font.os2().ok()?; |
| 49 |
let codepage_ranges: u64 = |
| 50 |
((os2.ul_code_page_range_2()? as u64) << 32) + os2.ul_code_page_range_1()? as u64; |
| 51 |
|
| 52 |
exclusive_lang_in_codepage_range(codepage_ranges).and_then(|string| CString::new(string).ok()) |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(test)] |
| 56 |
mod test { |
| 57 |
use super::exclusive_lang_in_codepage_range; |
| 58 |
#[test] |
| 59 |
fn exclusive_lang() { |
| 60 |
let non_exclusive = 1 << 17 + 1 << 20; |
| 61 |
assert!(exclusive_lang_in_codepage_range(non_exclusive).is_none()); |
| 62 |
let exclusive = 1 << 17; |
| 63 |
assert_eq!(exclusive_lang_in_codepage_range(exclusive).unwrap(), "ja"); |
| 64 |
let unmatched_non_han = 1 << 4 + 1 << 21; |
| 65 |
assert!(exclusive_lang_in_codepage_range(unmatched_non_han).is_none()); |
| 66 |
} |
| 67 |
} |