layout: Turn on synthetic small-caps for layout 2020 (#31435)

Synthetic small caps is supported by the font subsystem, but this is
disabled in Layout 2020. We can turn this on to bring support to parity
with the old layout system.

In addition to turning on synthetic small-caps this change also improves
the way that they work. Before, synthetic small caps meant that every
character was a small version of capitalized character. After this
change, capital letters are larger than small caps versions of small
letters -- matching other browsers and the common expectation of how
small caps works.
This commit is contained in:
Martin Robinson 2024-02-28 16:09:26 +01:00 committed by GitHub
parent 98bd306816
commit 5ba29c20ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 214 additions and 176 deletions

View file

@ -169,19 +169,23 @@ pub struct Font {
pub handle: FontHandle,
pub metrics: FontMetrics,
pub descriptor: FontDescriptor,
pub actual_pt_size: Au,
shaper: Option<Shaper>,
shape_cache: RefCell<HashMap<ShapeCacheEntry, Arc<GlyphStore>>>,
glyph_advance_cache: RefCell<HashMap<u32, FractionalPixel>>,
pub font_key: FontInstanceKey,
/// If this is a synthesized small caps font, then this font reference is for
/// the version of the font used to replace lowercase ASCII letters. It's up
/// to the consumer of this font to properly use this reference.
pub synthesized_small_caps: Option<FontRef>,
}
impl Font {
pub fn new(
handle: FontHandle,
descriptor: FontDescriptor,
actual_pt_size: Au,
font_key: FontInstanceKey,
synthesized_small_caps: Option<FontRef>,
) -> Font {
let metrics = handle.metrics();
@ -189,11 +193,11 @@ impl Font {
handle: handle,
shaper: None,
descriptor,
actual_pt_size,
metrics,
shape_cache: RefCell::new(HashMap::new()),
glyph_advance_cache: RefCell::new(HashMap::new()),
font_key,
synthesized_small_caps,
}
}
@ -351,7 +355,7 @@ impl Font {
#[inline]
pub fn glyph_index(&self, codepoint: char) -> Option<GlyphId> {
let codepoint = match self.descriptor.variant {
font_variant_caps::T::SmallCaps => codepoint.to_uppercase().next().unwrap(), //FIXME: #5938
font_variant_caps::T::SmallCaps => codepoint.to_ascii_uppercase(),
font_variant_caps::T::Normal => codepoint,
};
self.handle.glyph_index(codepoint)
@ -418,23 +422,33 @@ impl FontGroup {
mut font_context: &mut FontContext<S>,
codepoint: char,
) -> Option<FontRef> {
let should_look_for_small_caps = self.descriptor.variant == font_variant_caps::T::SmallCaps &&
codepoint.is_ascii_lowercase();
let font_or_synthesized_small_caps = |font: FontRef| {
if should_look_for_small_caps {
let font = font.borrow();
if font.synthesized_small_caps.is_some() {
return font.synthesized_small_caps.clone();
}
}
Some(font)
};
let has_glyph = |font: &FontRef| font.borrow().has_glyph_for(codepoint);
let font = self.find(&mut font_context, |font| has_glyph(font));
if font.is_some() {
return font;
if let Some(font) = self.find(&mut font_context, |font| has_glyph(font)) {
return font_or_synthesized_small_caps(font);
}
if let Some(ref fallback) = self.last_matching_fallback {
if has_glyph(&fallback) {
return self.last_matching_fallback.clone();
if let Some(ref last_matching_fallback) = self.last_matching_fallback {
if has_glyph(&last_matching_fallback) {
return font_or_synthesized_small_caps(last_matching_fallback.clone());
}
}
let font = self.find_fallback(&mut font_context, Some(codepoint), has_glyph);
if font.is_some() {
self.last_matching_fallback = font.clone();
return font;
if let Some(font) = self.find_fallback(&mut font_context, Some(codepoint), has_glyph) {
self.last_matching_fallback = Some(font.clone());
return font_or_synthesized_small_caps(font);
}
self.first(&mut font_context)