Fix handling of OpenType variable fonts

Fixes #3257.
This commit is contained in:
Kirill Chibisov 2020-01-31 17:54:02 +03:00 committed by GitHub
parent 2ef5e47b8e
commit 15cc07c069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 31 deletions

View File

@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Crash when using bitmap font with `embeddedbitmap` set to `false` - Crash when using bitmap font with `embeddedbitmap` set to `false`
- Inconsistent fontconfig fallback - Inconsistent fontconfig fallback
- Backspace deleting characters while IME is open on macOS - Backspace deleting characters while IME is open on macOS
- Handling of OpenType variable fonts
### Removed ### Removed

View File

@ -41,7 +41,7 @@ pub mod char_set;
pub use char_set::{CharSet, CharSetRef}; pub use char_set::{CharSet, CharSetRef};
pub mod pattern; pub mod pattern;
pub use pattern::{Pattern, PatternRef}; pub use pattern::{Pattern, PatternHash, PatternRef};
/// Find the font closest matching the provided pattern. /// Find the font closest matching the provided pattern.
/// ///

View File

@ -23,7 +23,7 @@ use libc::{c_char, c_double, c_int};
use super::ffi::FcResultMatch; use super::ffi::FcResultMatch;
use super::ffi::{FcBool, FcFontRenderPrepare, FcPatternGetBool, FcPatternGetDouble}; use super::ffi::{FcBool, FcFontRenderPrepare, FcPatternGetBool, FcPatternGetDouble};
use super::ffi::{FcChar8, FcConfigSubstitute, FcDefaultSubstitute, FcPattern}; use super::ffi::{FcChar8, FcConfigSubstitute, FcDefaultSubstitute, FcPattern, FcPatternHash};
use super::ffi::{FcPatternAddCharSet, FcPatternDestroy, FcPatternDuplicate, FcPatternGetCharSet}; use super::ffi::{FcPatternAddCharSet, FcPatternDestroy, FcPatternDuplicate, FcPatternGetCharSet};
use super::ffi::{FcPatternAddDouble, FcPatternAddString, FcPatternCreate, FcPatternGetString}; use super::ffi::{FcPatternAddDouble, FcPatternAddString, FcPatternCreate, FcPatternGetString};
use super::ffi::{FcPatternAddInteger, FcPatternGetInteger, FcPatternPrint}; use super::ffi::{FcPatternAddInteger, FcPatternGetInteger, FcPatternPrint};
@ -353,6 +353,9 @@ macro_rules! string_accessor {
} }
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct PatternHash(u32);
impl Pattern { impl Pattern {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
@ -537,6 +540,10 @@ impl PatternRef {
} }
} }
pub fn hash(&self) -> PatternHash {
unsafe { PatternHash(FcPatternHash(self.as_ptr())) }
}
/// Add charset to the pattern /// Add charset to the pattern
/// ///
/// The referenced charset is copied by fontconfig internally using /// The referenced charset is copied by fontconfig internally using

View File

@ -16,7 +16,6 @@
use std::cmp::{min, Ordering}; use std::cmp::{min, Ordering};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::path::PathBuf;
use freetype::freetype_sys; use freetype::freetype_sys;
use freetype::tt_os2::TrueTypeOS2Table; use freetype::tt_os2::TrueTypeOS2Table;
@ -26,7 +25,7 @@ use log::{debug, trace};
pub mod fc; pub mod fc;
use fc::{CharSet, Pattern, PatternRef}; use fc::{CharSet, Pattern, PatternHash, PatternRef};
use super::{ use super::{
BitmapBuffer, FontDesc, FontKey, GlyphKey, Metrics, Rasterize, RasterizedGlyph, Size, Slant, BitmapBuffer, FontDesc, FontKey, GlyphKey, Metrics, Rasterize, RasterizedGlyph, Size, Slant,
@ -37,9 +36,21 @@ struct FixedSize {
pixelsize: f64, pixelsize: f64,
} }
struct FallbackFont {
pattern: Pattern,
hash: PatternHash,
}
impl FallbackFont {
fn new(pattern: Pattern) -> FallbackFont {
let hash = pattern.hash();
Self { pattern, hash }
}
}
#[derive(Default)] #[derive(Default)]
struct FallbackList { struct FallbackList {
list: Vec<Pattern>, list: Vec<FallbackFont>,
coverage: CharSet, coverage: CharSet,
} }
@ -77,7 +88,7 @@ impl fmt::Debug for Face {
pub struct FreeTypeRasterizer { pub struct FreeTypeRasterizer {
faces: HashMap<FontKey, Face>, faces: HashMap<FontKey, Face>,
library: Library, library: Library,
keys: HashMap<PathBuf, FontKey>, keys: HashMap<PatternHash, FontKey>,
fallback_lists: HashMap<FontKey, FallbackList>, fallback_lists: HashMap<FontKey, FallbackList>,
device_pixel_ratio: f32, device_pixel_ratio: f32,
pixel_size: f64, pixel_size: f64,
@ -261,23 +272,16 @@ impl FreeTypeRasterizer {
let base_font = font_iter.next().ok_or_else(|| Error::MissingFont(desc.to_owned()))?; let base_font = font_iter.next().ok_or_else(|| Error::MissingFont(desc.to_owned()))?;
let base_font = pattern.render_prepare(config, base_font); let base_font = pattern.render_prepare(config, base_font);
let font_path = base_font.file(0).ok_or_else(|| Error::MissingFont(desc.to_owned()))?;
// Reload already loaded faces and drop their fallback faces // Reload already loaded faces and drop their fallback faces
let font_key = if let Some(font_key) = self.keys.remove(&font_path) { let font_key = if let Some(font_key) = self.keys.remove(&base_font.hash()) {
let fallback_list = self.fallback_lists.remove(&font_key).unwrap_or_default(); let fallback_list = self.fallback_lists.remove(&font_key).unwrap_or_default();
for font_pattern in &fallback_list.list { for fallback_font in &fallback_list.list {
let path = match font_pattern.file(0) { if let Some(ff_key) = self.keys.get(&fallback_font.hash) {
Some(path) => path,
None => continue,
};
if let Some(ff_key) = self.keys.get(&path) {
// Skip primary fonts, since these are all reloaded later // Skip primary fonts, since these are all reloaded later
if !self.fallback_lists.contains_key(&ff_key) { if !self.fallback_lists.contains_key(&ff_key) {
self.faces.remove(ff_key); self.faces.remove(ff_key);
self.keys.remove(&path); self.keys.remove(&fallback_font.hash);
} }
} }
} }
@ -297,11 +301,11 @@ impl FreeTypeRasterizer {
let coverage = CharSet::new(); let coverage = CharSet::new();
let empty_charset = CharSet::new(); let empty_charset = CharSet::new();
// Load fallback list // Load fallback list
let list: Vec<Pattern> = font_iter let list: Vec<FallbackFont> = font_iter
.map(|font| { .map(|font| {
let charset = font.get_charset().unwrap_or(&empty_charset); let charset = font.get_charset().unwrap_or(&empty_charset);
let _ = coverage.merge(&charset); let _ = coverage.merge(&charset);
font.to_owned() FallbackFont::new(font.to_owned())
}) })
.collect(); .collect();
@ -316,7 +320,8 @@ impl FreeTypeRasterizer {
key: Option<FontKey>, key: Option<FontKey>,
) -> Result<Option<FontKey>, Error> { ) -> Result<Option<FontKey>, Error> {
if let (Some(path), Some(index)) = (pattern.file(0), pattern.index().next()) { if let (Some(path), Some(index)) = (pattern.file(0), pattern.index().next()) {
if let Some(key) = self.keys.get(&path) { let font_hash = pattern.hash();
if let Some(key) = self.keys.get(&font_hash) {
return Ok(Some(*key)); return Ok(Some(*key));
} }
@ -361,7 +366,7 @@ impl FreeTypeRasterizer {
let key = face.key; let key = face.key;
self.faces.insert(key, face); self.faces.insert(key, face);
self.keys.insert(path, key); self.keys.insert(font_hash, key);
Ok(Some(key)) Ok(Some(key))
} else { } else {
Ok(None) Ok(None)
@ -373,10 +378,8 @@ impl FreeTypeRasterizer {
glyph_key: GlyphKey, glyph_key: GlyphKey,
have_recursed: bool, have_recursed: bool,
) -> Result<FontKey, Error> { ) -> Result<FontKey, Error> {
let c = glyph_key.c;
let use_initial_face = if let Some(face) = self.faces.get(&glyph_key.font_key) { let use_initial_face = if let Some(face) = self.faces.get(&glyph_key.font_key) {
let index = face.ft_face.get_char_index(c as usize); let index = face.ft_face.get_char_index(glyph_key.c as usize);
index != 0 || have_recursed index != 0 || have_recursed
} else { } else {
@ -614,13 +617,9 @@ impl FreeTypeRasterizer {
return Ok(glyph.font_key); return Ok(glyph.font_key);
} }
for font_pattern in &fallback_list.list { for fallback_font in &fallback_list.list {
let path = match font_pattern.file(0) { let font_pattern = &fallback_font.pattern;
Some(path) => path, match self.keys.get(&fallback_font.hash) {
None => continue,
};
match self.keys.get(&path) {
Some(&key) => { Some(&key) => {
let face = match self.faces.get(&key) { let face = match self.faces.get(&key) {
Some(face) => face, Some(face) => face,