fonts: improve font name decoding
freetype can't handle a wide range of encodings for font names and can return strings like `?????` when the family name is only present in the font as a non-unicode encoding, such as Chinese. This commit improves our handling of the font name table and prefers to use results from processing that over the results returned for eg: font family directly from the freetype API. refs: https://github.com/wez/wezterm/issues/1761
This commit is contained in:
1 parent
72cbf4caeb
commit
32bf0d281e
4 files changed
+177
-31
No files matched your search
Generated
+10
@@ -1043,6 +1043,15 @@ dependencies = [
|
||||
"winreg 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7896dc8abb250ffdda33912550faa54c88ec8b998dec0b2c55ab224921ce11df"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "enum-display-derive"
|
||||
version = "0.1.1"
|
||||
@@ -4661,6 +4670,7 @@ dependencies = [
|
||||
"core-foundation 0.9.3",
|
||||
"core-text",
|
||||
"dwrote",
|
||||
"encoding_rs",
|
||||
"enum-display-derive",
|
||||
"euclid",
|
||||
"fontconfig",
|
||||
|
||||
@@ -9,6 +9,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
config = { path = "../config" }
|
||||
encoding_rs = "0.8"
|
||||
enum-display-derive = "0.1"
|
||||
euclid = "0.22"
|
||||
freetype = { path = "../deps/freetype" }
|
||||
|
||||
+57
-27
@@ -7,6 +7,7 @@ use config::{configuration, FreeTypeLoadFlags, FreeTypeLoadTarget};
|
||||
pub use freetype::*;
|
||||
use memmap2::{Mmap, MmapOptions};
|
||||
use rangeset::RangeSet;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::CStr;
|
||||
use std::fs::File;
|
||||
@@ -191,10 +192,10 @@ impl Face {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_sfnt_names(&self) -> Vec<String> {
|
||||
pub fn get_sfnt_names(&self) -> HashMap<u32, Vec<NameRecord>> {
|
||||
let num_names = unsafe { FT_Get_Sfnt_Name_Count(self.face) };
|
||||
|
||||
let mut names = vec![];
|
||||
let mut names = HashMap::new();
|
||||
|
||||
let mut sfnt_name = FT_SfntName {
|
||||
platform_id: 0,
|
||||
@@ -214,42 +215,62 @@ impl Face {
|
||||
continue;
|
||||
}
|
||||
|
||||
if sfnt_name.name_id != TT_NAME_ID_FONT_FAMILY as u16
|
||||
&& sfnt_name.name_id != TT_NAME_ID_FULL_NAME as u16
|
||||
{
|
||||
if !matches!(
|
||||
sfnt_name.name_id as u32,
|
||||
TT_NAME_ID_TYPOGRAPHIC_FAMILY
|
||||
| TT_NAME_ID_TYPOGRAPHIC_SUBFAMILY
|
||||
| TT_NAME_ID_FONT_FAMILY
|
||||
| TT_NAME_ID_FONT_SUBFAMILY
|
||||
| TT_NAME_ID_PS_NAME
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_unicode = sfnt_name.platform_id == TT_PLATFORM_APPLE_UNICODE as u16
|
||||
|| sfnt_name.encoding_id == TT_MS_ID_SYMBOL_CS as u16
|
||||
|| sfnt_name.encoding_id == TT_MS_ID_UNICODE_CS as u16;
|
||||
|
||||
if !is_unicode {
|
||||
continue;
|
||||
}
|
||||
let utf16 = unsafe {
|
||||
let bytes = unsafe {
|
||||
std::slice::from_raw_parts(
|
||||
sfnt_name.string as *const u16,
|
||||
sfnt_name.string_len as usize / 2,
|
||||
sfnt_name.string as *const u8,
|
||||
sfnt_name.string_len as usize,
|
||||
)
|
||||
};
|
||||
|
||||
match String::from_utf16(utf16) {
|
||||
Ok(name) => names.push(name),
|
||||
Err(err) => {
|
||||
let encoding = match (sfnt_name.platform_id as u32, sfnt_name.encoding_id as u32) {
|
||||
(TT_PLATFORM_MACINTOSH, TT_MAC_ID_JAPANESE)
|
||||
| (TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS) => encoding_rs::SHIFT_JIS,
|
||||
(TT_PLATFORM_MACINTOSH, TT_MAC_ID_SIMPLIFIED_CHINESE)
|
||||
| (TT_PLATFORM_MICROSOFT, TT_MS_ID_PRC) => encoding_rs::GBK,
|
||||
(TT_PLATFORM_MACINTOSH, TT_MAC_ID_TRADITIONAL_CHINESE)
|
||||
| (TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5) => encoding_rs::BIG5,
|
||||
(
|
||||
TT_PLATFORM_MICROSOFT,
|
||||
TT_MS_ID_UCS_4 | TT_MS_ID_UNICODE_CS | TT_MS_ID_SYMBOL_CS,
|
||||
) => encoding_rs::UTF_16BE,
|
||||
(TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG) => encoding_rs::EUC_KR,
|
||||
(TT_PLATFORM_APPLE_UNICODE | TT_PLATFORM_ISO, _) => encoding_rs::UTF_16BE,
|
||||
(TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN) => encoding_rs::MACINTOSH,
|
||||
_ => {
|
||||
log::trace!(
|
||||
"Failed to convert: {:#}, string_len={} (bytes), {:x?} -> {}",
|
||||
err,
|
||||
sfnt_name.string_len,
|
||||
utf16,
|
||||
String::from_utf16_lossy(utf16)
|
||||
"Skipping name_id={} because platform_id={} encoding_id={}",
|
||||
sfnt_name.name_id,
|
||||
sfnt_name.platform_id,
|
||||
sfnt_name.encoding_id
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
names.sort();
|
||||
names.dedup();
|
||||
let (name, _) = encoding.decode_with_bom_removal(bytes);
|
||||
|
||||
names
|
||||
.entry(sfnt_name.name_id as u32)
|
||||
.or_insert_with(|| vec![])
|
||||
.push(NameRecord {
|
||||
platform_id: sfnt_name.platform_id,
|
||||
encoding_id: sfnt_name.encoding_id,
|
||||
name_id: sfnt_name.name_id,
|
||||
language_id: sfnt_name.language_id,
|
||||
name: name.to_string(),
|
||||
});
|
||||
}
|
||||
names
|
||||
}
|
||||
|
||||
@@ -1082,3 +1103,12 @@ impl FreeTypeStream {
|
||||
drop(myself);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NameRecord {
|
||||
pub platform_id: u16,
|
||||
pub encoding_id: u16,
|
||||
pub language_id: u16,
|
||||
pub name_id: u16,
|
||||
pub name: String,
|
||||
}
|
||||
+109
-4
@@ -118,11 +118,110 @@ pub struct Names {
|
||||
pub aliases: Vec<String>,
|
||||
}
|
||||
|
||||
/// Returns the "best" name from a set of records.
|
||||
/// Best is English from a MS entry if available, as freetype's
|
||||
/// source claims that a number of Mac entries have somewhat
|
||||
/// broken encodings.
|
||||
fn best_name(records: &[crate::ftwrap::NameRecord]) -> String {
|
||||
let mut win = None;
|
||||
let mut uni = None;
|
||||
let mut apple = None;
|
||||
|
||||
for rec in records {
|
||||
match rec.platform_id as u32 {
|
||||
freetype::TT_PLATFORM_APPLE_UNICODE | freetype::TT_PLATFORM_ISO => {
|
||||
uni.replace(rec);
|
||||
}
|
||||
freetype::TT_PLATFORM_MACINTOSH => {
|
||||
apple.replace(rec);
|
||||
}
|
||||
freetype::TT_PLATFORM_MICROSOFT => {
|
||||
let is_english = (rec.language_id & 0x3ff) == 0x9;
|
||||
if is_english {
|
||||
return rec.name.clone();
|
||||
}
|
||||
win.replace(rec);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(rec) = apple {
|
||||
return rec.name.clone();
|
||||
}
|
||||
if let Some(rec) = win {
|
||||
return rec.name.clone();
|
||||
}
|
||||
if let Some(rec) = uni {
|
||||
return rec.name.clone();
|
||||
}
|
||||
records[0].name.clone()
|
||||
}
|
||||
|
||||
/// Return a single name from a table.
|
||||
/// The list of ids are tried in order: the first id with corresponding
|
||||
/// names is taken, and the "best" of those names is returned.
|
||||
fn name_from_table(
|
||||
names: &std::collections::HashMap<u32, Vec<crate::ftwrap::NameRecord>>,
|
||||
ids: &[u32],
|
||||
) -> Option<String> {
|
||||
for id in ids {
|
||||
if let Some(name_list) = names.get(id) {
|
||||
return Some(best_name(name_list));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns the sorted, deduplicated set of names across the list of ids
|
||||
fn names_from_table(
|
||||
names: &std::collections::HashMap<u32, Vec<crate::ftwrap::NameRecord>>,
|
||||
ids: &[u32],
|
||||
) -> Vec<String> {
|
||||
let mut result = vec![];
|
||||
|
||||
for id in ids {
|
||||
if let Some(name_list) = names.get(id) {
|
||||
for rec in name_list {
|
||||
result.push(rec.name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
result.sort();
|
||||
result.dedup();
|
||||
result
|
||||
}
|
||||
|
||||
impl Names {
|
||||
pub fn from_ft_face(face: &crate::ftwrap::Face) -> Names {
|
||||
let postscript_name = face.postscript_name();
|
||||
let family = face.family_name();
|
||||
let sub_family = face.style_name();
|
||||
// We don't simply use the freetype functions to retrieve names,
|
||||
// as freetype has a limited set of encodings that it supports.
|
||||
// We process the name table for ourselves to increase our chances
|
||||
// of returning a good version of the name.
|
||||
// See <https://github.com/wez/wezterm/issues/1761#issuecomment-1079150560>
|
||||
// for a case where freetype returns `?????` for a name.
|
||||
let names = face.get_sfnt_names();
|
||||
|
||||
let family = name_from_table(
|
||||
&names,
|
||||
&[
|
||||
freetype::TT_NAME_ID_TYPOGRAPHIC_FAMILY,
|
||||
freetype::TT_NAME_ID_FONT_FAMILY,
|
||||
],
|
||||
)
|
||||
.unwrap_or_else(|| face.family_name());
|
||||
|
||||
let sub_family = name_from_table(
|
||||
&names,
|
||||
&[
|
||||
freetype::TT_NAME_ID_TYPOGRAPHIC_SUBFAMILY,
|
||||
freetype::TT_NAME_ID_FONT_SUBFAMILY,
|
||||
],
|
||||
)
|
||||
.unwrap_or_else(|| face.style_name());
|
||||
|
||||
let postscript_name = name_from_table(&names, &[freetype::TT_NAME_ID_PS_NAME])
|
||||
.unwrap_or_else(|| face.postscript_name());
|
||||
|
||||
let full_name = if sub_family.is_empty() {
|
||||
family.to_string()
|
||||
@@ -130,7 +229,13 @@ impl Names {
|
||||
format!("{} {}", family, sub_family)
|
||||
};
|
||||
|
||||
let aliases = face.get_sfnt_names();
|
||||
let aliases = names_from_table(
|
||||
&names,
|
||||
&[
|
||||
freetype::TT_NAME_ID_TYPOGRAPHIC_FAMILY,
|
||||
freetype::TT_NAME_ID_FONT_FAMILY,
|
||||
],
|
||||
);
|
||||
|
||||
Names {
|
||||
full_name,
|
||||
|
||||
Reference in new issue
Block a user