Feature/fallback fonts (#15306)

Supersedes https://github.com/zed-industries/zed/pull/12090

fixes #5180
fixes #5055

See original PR for an example of the feature at work.

This PR changes the settings interface to be backwards compatible, and
adds the `ui_font_fallbacks`, `buffer_font_fallbacks`, and
`terminal.font_fallbacks` settings.

Release Notes:

- Added support for font fallbacks via three new settings:
`ui_font_fallbacks`, `buffer_font_fallbacks`, and
`terminal.font_fallbacks`.(#5180, #5055).

---------

Co-authored-by: Junkui Zhang <364772080@qq.com>
This commit is contained in:
Mikayla Maki
2024-07-26 16:42:21 -07:00
committed by GitHub
co-authored by Junkui Zhang
parent 3e31955b7f
commit a1bd7a1297
30 changed files with 444 additions and 136 deletions
+82 -28
View File
@@ -1,10 +1,12 @@
#![allow(unused, non_upper_case_globals)]
use crate::FontFeatures;
use crate::{FontFallbacks, FontFeatures};
use cocoa::appkit::CGFloat;
use core_foundation::{
array::{
kCFTypeArrayCallBacks, CFArray, CFArrayAppendValue, CFArrayCreateMutable, CFMutableArrayRef,
kCFTypeArrayCallBacks, CFArray, CFArrayAppendArray, CFArrayAppendValue,
CFArrayCreateMutable, CFArrayGetCount, CFArrayGetValueAtIndex, CFArrayRef,
CFMutableArrayRef,
},
base::{kCFAllocatorDefault, CFRelease, TCFType},
dictionary::{
@@ -13,21 +15,88 @@ use core_foundation::{
number::CFNumber,
string::{CFString, CFStringRef},
};
use core_foundation_sys::locale::CFLocaleCopyPreferredLanguages;
use core_graphics::{display::CFDictionary, geometry::CGAffineTransform};
use core_text::{
font::{CTFont, CTFontRef},
font::{cascade_list_for_languages, CTFont, CTFontRef},
font_descriptor::{
kCTFontFeatureSettingsAttribute, CTFontDescriptor, CTFontDescriptorCopyAttributes,
CTFontDescriptorCreateCopyWithFeature, CTFontDescriptorCreateWithAttributes,
kCTFontCascadeListAttribute, kCTFontFeatureSettingsAttribute, CTFontDescriptor,
CTFontDescriptorCopyAttributes, CTFontDescriptorCreateCopyWithFeature,
CTFontDescriptorCreateWithAttributes, CTFontDescriptorCreateWithNameAndSize,
CTFontDescriptorRef,
},
};
use font_kit::font::Font;
use font_kit::font::Font as FontKitFont;
use std::ptr;
pub fn apply_features(font: &mut Font, features: &FontFeatures) {
pub fn apply_features_and_fallbacks(
font: &mut FontKitFont,
features: &FontFeatures,
fallbacks: Option<&FontFallbacks>,
) -> anyhow::Result<()> {
unsafe {
let fallback_array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if let Some(fallbacks) = fallbacks {
for user_fallback in fallbacks.fallback_list() {
let name = CFString::from(user_fallback.as_str());
let fallback_desc =
CTFontDescriptorCreateWithNameAndSize(name.as_concrete_TypeRef(), 0.0);
CFArrayAppendValue(fallback_array, fallback_desc as _);
CFRelease(fallback_desc as _);
}
}
{
let preferred_languages: CFArray<CFString> =
CFArray::wrap_under_create_rule(CFLocaleCopyPreferredLanguages());
let default_fallbacks = CTFontCopyDefaultCascadeListForLanguages(
font.native_font().as_concrete_TypeRef(),
preferred_languages.as_concrete_TypeRef(),
);
let default_fallbacks: CFArray<CTFontDescriptor> =
CFArray::wrap_under_create_rule(default_fallbacks);
default_fallbacks
.iter()
.filter(|desc| desc.font_path().is_some())
.map(|desc| {
CFArrayAppendValue(fallback_array, desc.as_concrete_TypeRef() as _);
});
}
let feature_array = generate_feature_array(features);
let keys = [kCTFontFeatureSettingsAttribute, kCTFontCascadeListAttribute];
let values = [feature_array, fallback_array];
let attrs = CFDictionaryCreate(
kCFAllocatorDefault,
keys.as_ptr() as _,
values.as_ptr() as _,
2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks,
);
CFRelease(feature_array as *const _ as _);
CFRelease(fallback_array as *const _ as _);
let new_descriptor = CTFontDescriptorCreateWithAttributes(attrs);
CFRelease(attrs as _);
let new_descriptor = CTFontDescriptor::wrap_under_create_rule(new_descriptor);
let new_font = CTFontCreateCopyWithAttributes(
font.native_font().as_concrete_TypeRef(),
0.0,
std::ptr::null(),
new_descriptor.as_concrete_TypeRef(),
);
let new_font = CTFont::wrap_under_create_rule(new_font);
*font = font_kit::font::Font::from_native_font(&new_font);
Ok(())
}
}
fn generate_feature_array(features: &FontFeatures) -> CFMutableArrayRef {
unsafe {
let native_font = font.native_font();
let mut feature_array =
CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
for (tag, value) in features.tag_value_list() {
@@ -48,26 +117,7 @@ pub fn apply_features(font: &mut Font, features: &FontFeatures) {
CFArrayAppendValue(feature_array, dict as _);
CFRelease(dict as _);
}
let attrs = CFDictionaryCreate(
kCFAllocatorDefault,
&kCTFontFeatureSettingsAttribute as *const _ as _,
&feature_array as *const _ as _,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks,
);
CFRelease(feature_array as *const _ as _);
let new_descriptor = CTFontDescriptorCreateWithAttributes(attrs);
CFRelease(attrs as _);
let new_descriptor = CTFontDescriptor::wrap_under_create_rule(new_descriptor);
let new_font = CTFontCreateCopyWithAttributes(
font.native_font().as_concrete_TypeRef(),
0.0,
ptr::null(),
new_descriptor.as_concrete_TypeRef(),
);
let new_font = CTFont::wrap_under_create_rule(new_font);
*font = Font::from_native_font(&new_font);
feature_array
}
}
@@ -82,4 +132,8 @@ extern "C" {
matrix: *const CGAffineTransform,
attributes: CTFontDescriptorRef,
) -> CTFontRef;
fn CTFontCopyDefaultCascadeListForLanguages(
font: CTFontRef,
languagePrefList: CFArrayRef,
) -> CFArrayRef;
}
+11 -7
View File
@@ -1,6 +1,6 @@
use crate::{
point, px, size, Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun,
FontStyle, FontWeight, GlyphId, LineLayout, Pixels, PlatformTextSystem, Point,
point, px, size, Bounds, DevicePixels, Font, FontFallbacks, FontFeatures, FontId, FontMetrics,
FontRun, FontStyle, FontWeight, GlyphId, LineLayout, Pixels, PlatformTextSystem, Point,
RenderGlyphParams, Result, ShapedGlyph, ShapedRun, SharedString, Size, SUBPIXEL_VARIANTS,
};
use anyhow::anyhow;
@@ -43,7 +43,7 @@ use pathfinder_geometry::{
use smallvec::SmallVec;
use std::{borrow::Cow, char, cmp, convert::TryFrom, sync::Arc};
use super::open_type;
use super::open_type::apply_features_and_fallbacks;
#[allow(non_upper_case_globals)]
const kCGImageAlphaOnly: u32 = 7;
@@ -54,6 +54,7 @@ pub(crate) struct MacTextSystem(RwLock<MacTextSystemState>);
struct FontKey {
font_family: SharedString,
font_features: FontFeatures,
font_fallbacks: Option<FontFallbacks>,
}
struct MacTextSystemState {
@@ -123,11 +124,13 @@ impl PlatformTextSystem for MacTextSystem {
let font_key = FontKey {
font_family: font.family.clone(),
font_features: font.features.clone(),
font_fallbacks: font.fallbacks.clone(),
};
let candidates = if let Some(font_ids) = lock.font_ids_by_font_key.get(&font_key) {
font_ids.as_slice()
} else {
let font_ids = lock.load_family(&font.family, &font.features)?;
let font_ids =
lock.load_family(&font.family, &font.features, font.fallbacks.as_ref())?;
lock.font_ids_by_font_key.insert(font_key.clone(), font_ids);
lock.font_ids_by_font_key[&font_key].as_ref()
};
@@ -212,6 +215,7 @@ impl MacTextSystemState {
&mut self,
name: &str,
features: &FontFeatures,
fallbacks: Option<&FontFallbacks>,
) -> Result<SmallVec<[FontId; 4]>> {
let name = if name == ".SystemUIFont" {
".AppleSystemUIFont"
@@ -227,8 +231,7 @@ impl MacTextSystemState {
for font in family.fonts() {
let mut font = font.load()?;
open_type::apply_features(&mut font, features);
apply_features_and_fallbacks(&mut font, features, fallbacks)?;
// This block contains a precautionary fix to guard against loading fonts
// that might cause panics due to `.unwrap()`s up the chain.
{
@@ -457,6 +460,7 @@ impl MacTextSystemState {
CFRange::init(utf16_start as isize, (utf16_end - utf16_start) as isize);
let font: &FontKitFont = &self.fonts[run.font_id.0];
unsafe {
string.set_attribute(
cf_range,
@@ -634,7 +638,7 @@ impl From<FontStyle> for FontkitStyle {
}
}
// Some fonts may have no attributest despite `core_text` requiring them (and panicking).
// Some fonts may have no attributes despite `core_text` requiring them (and panicking).
// This is the same version as `core_text` has without `expect` calls.
mod lenient_font_attributes {
use core_foundation::{
+115 -18
View File
@@ -30,6 +30,7 @@ struct FontInfo {
font_family: String,
font_face: IDWriteFontFace3,
features: IDWriteTypography,
fallbacks: Option<IDWriteFontFallback>,
is_system_font: bool,
is_emoji: bool,
}
@@ -287,6 +288,63 @@ impl DirectWriteState {
Ok(())
}
fn generate_font_fallbacks(
&self,
fallbacks: Option<&FontFallbacks>,
) -> Result<Option<IDWriteFontFallback>> {
if fallbacks.is_some_and(|fallbacks| fallbacks.fallback_list().is_empty()) {
return Ok(None);
}
unsafe {
let builder = self.components.factory.CreateFontFallbackBuilder()?;
let font_set = &self.system_font_collection.GetFontSet()?;
if let Some(fallbacks) = fallbacks {
for family_name in fallbacks.fallback_list() {
let Some(fonts) = font_set
.GetMatchingFonts(
&HSTRING::from(family_name),
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
)
.log_err()
else {
continue;
};
if fonts.GetFontCount() == 0 {
log::error!("No mathcing font find for {}", family_name);
continue;
}
let font = fonts.GetFontFaceReference(0)?.CreateFontFace()?;
let mut count = 0;
font.GetUnicodeRanges(None, &mut count).ok();
if count == 0 {
continue;
}
let mut unicode_ranges = vec![DWRITE_UNICODE_RANGE::default(); count as usize];
let Some(_) = font
.GetUnicodeRanges(Some(&mut unicode_ranges), &mut count)
.log_err()
else {
continue;
};
let target_family_name = HSTRING::from(family_name);
builder.AddMapping(
&unicode_ranges,
&[target_family_name.as_ptr()],
None,
None,
None,
1.0,
)?;
}
}
let system_fallbacks = self.components.factory.GetSystemFontFallback()?;
builder.AddMappings(&system_fallbacks)?;
Ok(Some(builder.CreateFontFallback()?))
}
}
unsafe fn generate_font_features(
&self,
font_features: &FontFeatures,
@@ -302,6 +360,7 @@ impl DirectWriteState {
font_weight: FontWeight,
font_style: FontStyle,
font_features: &FontFeatures,
font_fallbacks: Option<&FontFallbacks>,
is_system_font: bool,
) -> Option<FontId> {
let collection = if is_system_font {
@@ -334,11 +393,16 @@ impl DirectWriteState {
else {
continue;
};
let fallbacks = self
.generate_font_fallbacks(font_fallbacks)
.log_err()
.unwrap_or_default();
let font_info = FontInfo {
font_family: family_name.to_owned(),
font_face,
is_system_font,
features: direct_write_features,
fallbacks,
is_system_font,
is_emoji,
};
let font_id = FontId(self.fonts.len());
@@ -371,6 +435,7 @@ impl DirectWriteState {
target_font.weight,
target_font.style,
&target_font.features,
target_font.fallbacks.as_ref(),
)
.unwrap()
} else {
@@ -379,6 +444,7 @@ impl DirectWriteState {
target_font.weight,
target_font.style,
&target_font.features,
target_font.fallbacks.as_ref(),
)
.unwrap_or_else(|| {
let family = self.system_ui_font_name.clone();
@@ -388,6 +454,7 @@ impl DirectWriteState {
target_font.weight,
target_font.style,
&target_font.features,
target_font.fallbacks.as_ref(),
true,
)
.unwrap()
@@ -402,16 +469,38 @@ impl DirectWriteState {
weight: FontWeight,
style: FontStyle,
features: &FontFeatures,
fallbacks: Option<&FontFallbacks>,
) -> Option<FontId> {
// try to find target font in custom font collection first
self.get_font_id_from_font_collection(family_name, weight, style, features, false)
.or_else(|| {
self.get_font_id_from_font_collection(family_name, weight, style, features, true)
})
.or_else(|| {
self.update_system_font_collection();
self.get_font_id_from_font_collection(family_name, weight, style, features, true)
})
self.get_font_id_from_font_collection(
family_name,
weight,
style,
features,
fallbacks,
false,
)
.or_else(|| {
self.get_font_id_from_font_collection(
family_name,
weight,
style,
features,
fallbacks,
true,
)
})
.or_else(|| {
self.update_system_font_collection();
self.get_font_id_from_font_collection(
family_name,
weight,
style,
features,
fallbacks,
true,
)
})
}
fn layout_line(
@@ -440,15 +529,22 @@ impl DirectWriteState {
} else {
&self.custom_font_collection
};
let format = self.components.factory.CreateTextFormat(
&HSTRING::from(&font_info.font_family),
collection,
font_info.font_face.GetWeight(),
font_info.font_face.GetStyle(),
DWRITE_FONT_STRETCH_NORMAL,
font_size.0,
&HSTRING::from(&self.components.locale),
)?;
let format: IDWriteTextFormat1 = self
.components
.factory
.CreateTextFormat(
&HSTRING::from(&font_info.font_family),
collection,
font_info.font_face.GetWeight(),
font_info.font_face.GetStyle(),
DWRITE_FONT_STRETCH_NORMAL,
font_size.0,
&HSTRING::from(&self.components.locale),
)?
.cast()?;
if let Some(ref fallbacks) = font_info.fallbacks {
format.SetFontFallback(fallbacks)?;
}
let layout = self.components.factory.CreateTextLayout(
&text_wide,
@@ -1183,6 +1279,7 @@ fn get_font_identifier_and_font_struct(
features: FontFeatures::default(),
weight: weight.into(),
style: style.into(),
fallbacks: None,
};
let is_emoji = unsafe { font_face.IsColorFont().as_bool() };
Some((identifier, font_struct, is_emoji))
+1
View File
@@ -1,4 +1,5 @@
use derive_more::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
use std::{borrow::Borrow, sync::Arc};
use util::arc_cow::ArcCow;
+9 -3
View File
@@ -6,9 +6,9 @@ use std::{
use crate::{
black, phi, point, quad, rems, AbsoluteLength, Bounds, ContentMask, Corners, CornersRefinement,
CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font, FontFeatures, FontStyle, FontWeight,
Hsla, Length, Pixels, Point, PointRefinement, Rgba, SharedString, Size, SizeRefinement, Styled,
TextRun, WindowContext,
CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font, FontFallbacks, FontFeatures,
FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rgba, SharedString, Size,
SizeRefinement, Styled, TextRun, WindowContext,
};
use collections::HashSet;
use refineable::Refineable;
@@ -180,6 +180,9 @@ pub struct TextStyle {
/// The font features to use
pub font_features: FontFeatures,
/// The fallback fonts to use
pub font_fallbacks: Option<FontFallbacks>,
/// The font size to use, in pixels or rems.
pub font_size: AbsoluteLength,
@@ -218,6 +221,7 @@ impl Default for TextStyle {
"Helvetica".into()
},
font_features: FontFeatures::default(),
font_fallbacks: None,
font_size: rems(1.).into(),
line_height: phi(),
font_weight: FontWeight::default(),
@@ -269,6 +273,7 @@ impl TextStyle {
Font {
family: self.font_family.clone(),
features: self.font_features.clone(),
fallbacks: self.font_fallbacks.clone(),
weight: self.font_weight,
style: self.font_style,
}
@@ -286,6 +291,7 @@ impl TextStyle {
font: Font {
family: self.font_family.clone(),
features: Default::default(),
fallbacks: self.font_fallbacks.clone(),
weight: self.font_weight,
style: self.font_style,
},
+2
View File
@@ -506,6 +506,7 @@ pub trait Styled: Sized {
let Font {
family,
features,
fallbacks,
weight,
style,
} = font;
@@ -515,6 +516,7 @@ pub trait Styled: Sized {
text_style.font_features = Some(features);
text_style.font_weight = Some(weight);
text_style.font_style = Some(style);
text_style.font_fallbacks = fallbacks;
self
}
+7 -2
View File
@@ -1,8 +1,10 @@
mod font_fallbacks;
mod font_features;
mod line;
mod line_layout;
mod line_wrapper;
pub use font_fallbacks::*;
pub use font_features::*;
pub use line::*;
pub use line_layout::*;
@@ -62,8 +64,7 @@ impl TextSystem {
wrapper_pool: Mutex::default(),
font_runs_pool: Mutex::default(),
fallback_font_stack: smallvec![
// TODO: This is currently Zed-specific.
// We should allow GPUI users to provide their own fallback font stack.
// TODO: Remove this when Linux have implemented setting fallbacks.
font("Zed Plex Mono"),
font("Helvetica"),
font("Segoe UI"), // Windows
@@ -683,6 +684,9 @@ pub struct Font {
/// The font features to use.
pub features: FontFeatures,
/// The fallbacks fonts to use.
pub fallbacks: Option<FontFallbacks>,
/// The font weight.
pub weight: FontWeight,
@@ -697,6 +701,7 @@ pub fn font(family: impl Into<SharedString>) -> Font {
features: FontFeatures::default(),
weight: FontWeight::default(),
style: FontStyle::default(),
fallbacks: None,
}
}
@@ -0,0 +1,21 @@
use std::sync::Arc;
use schemars::JsonSchema;
use serde_derive::{Deserialize, Serialize};
/// The fallback fonts that can be configured for a given font.
/// Fallback fonts family names are stored here.
#[derive(Default, Clone, Eq, PartialEq, Hash, Debug, Deserialize, Serialize, JsonSchema)]
pub struct FontFallbacks(pub Arc<Vec<String>>);
impl FontFallbacks {
/// Get the fallback fonts family names
pub fn fallback_list(&self) -> &[String] {
&self.0.as_slice()
}
/// Create a font fallback from a list of strings
pub fn from_fonts(fonts: Vec<String>) -> Self {
FontFallbacks(Arc::new(fonts))
}
}