1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
use super::family::parse_families;
use super::index::*;
use super::library::FontLibrary;
use super::types::{FamilyId, FamilyKey, FontId, FontKey, SourceId};
use super::{shared_data::SharedData, Font};
use crate::util::fxhash::FxHashMap;
use std::sync::Arc;
use swash::proxy::CharmapProxy;
use swash::text::{
cluster::{CharCluster, Status},
Cjk, Language, Script,
};
use swash::{Attributes, Synthesis};
pub type FontGroupKey = (u64, Attributes);
type Epoch = u64;
/// Identifier for a cached font group.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct FontGroupId(pub u64);
const MAX_INLINE: usize = 6;
pub struct FontContext {
library: FontLibrary,
fonts: FontCache,
groups: GroupCache,
}
impl FontContext {
pub fn new(library: FontLibrary) -> Self {
let index = library.inner.index.read().unwrap().clone();
let fonts = FontCache {
index,
sources: FxHashMap::default(),
epoch: 0,
};
Self {
library,
fonts,
groups: GroupCache::default(),
}
}
/// Returns the underlying font library.
pub fn library(&self) -> &FontLibrary {
&self.library
}
/// Resets font group caches and state. This should be called at the end
/// of every layout session.
pub fn reset_group_state(&mut self) {
self.groups.reset();
}
/// Registers a font group.
pub fn register_group(&mut self, families: &str, key: u64, attrs: Attributes) -> FontGroupId {
self.groups.get(&self.fonts, families, key, attrs)
}
/// Selects a font group for subsequent cluster mapping operations.
pub fn select_group(&mut self, descriptor: FontGroupId) {
self.groups.select(descriptor);
}
/// Selects fallback fonts for the specified writing system.
pub fn select_fallbacks(&mut self, script: Script, language: Option<&Language>) {
self.groups
.select_fallbacks(script, language.map(|l| l.cjk()).unwrap_or(Cjk::None))
}
/// Maps the characters in a cluster to nominal glyph identifiers and
/// returns the most suitable font based on the currently selected
/// descriptor and fallback configuration.
pub fn map_cluster(
&mut self,
cluster: &mut CharCluster,
synthesis: &mut Synthesis,
) -> Option<Font> {
let mut best = None;
let list = &self.groups.state.fonts;
for entry in self.groups.fonts.get_mut(list.start..list.end)?.iter_mut() {
match entry.map_cluster(&mut self.fonts, cluster, synthesis, best.is_none()) {
Some((font, status)) => {
if status == Status::Complete {
return Some(font);
}
best = Some(font);
}
None => continue,
}
}
let attrs = list.attributes;
// We don't have a complete mapping at this point, so time to check
// fallback fonts.
if cluster.info().is_emoji() {
if let Some(entry) = self.groups.emoji(&self.fonts, attrs) {
match entry.map_cluster(&mut self.fonts, cluster, synthesis, best.is_none()) {
Some((font, status)) => {
if status == Status::Complete {
return Some(font);
}
best = Some(font);
}
None => {}
}
}
}
if !self.groups.state.fallbacks_ready {
self.groups.fill_fallbacks(&self.fonts);
}
for &family in &self.groups.state.fallbacks {
let entry = match self.groups.state.fallback_map.get_mut(&(family, attrs)) {
Some(entry) => entry,
_ => match self.fonts.query(family, attrs) {
Some(font) => {
self.groups
.state
.fallback_map
.insert((family, attrs), font.selector(attrs).into());
self.groups
.state
.fallback_map
.get_mut(&(family, attrs))
.unwrap()
}
_ => continue,
},
};
match entry.map_cluster(&mut self.fonts, cluster, synthesis, best.is_none()) {
Some((font, status)) => {
if status == Status::Complete {
return Some(font);
}
best = Some(font);
}
None => continue,
}
}
best
}
}
pub struct FontCache {
pub index: Arc<StaticIndex>,
sources: FxHashMap<SourceId, Option<(SharedData, Epoch)>>,
epoch: Epoch,
}
impl FontCache {
pub fn default() -> Self {
let index = StaticIndex::global().clone();
FontCache {
index,
sources: FxHashMap::default(),
epoch: 0,
}
}
/// Returns a font entry that matches the specified family and
/// attributes.
pub fn query<'a>(
&'a self,
family: impl Into<FamilyKey<'a>>,
attributes: impl Into<Attributes>,
) -> Option<FontEntry<'a>> {
self.index.query(family, attributes)
}
/// Returns a font entry for the specified identifier.
pub fn font_by_id<'a>(&'a self, id: FontId) -> Option<FontEntry<'a>> {
self.index.font_by_id(id)
}
/// Returns a font matching the specified key.
pub fn get<'k>(&mut self, key: impl Into<FontKey<'k>>) -> Option<Font> {
let (source_id, offset, attributes, key) = match key.into() {
FontKey::Id(id) => {
let font = self.font_by_id(id)?;
(
font.source().id(),
font.offset(),
font.attributes(),
font.cache_key(),
)
}
FontKey::Descriptor(family, attrs) => {
let font = self.query(family, attrs)?;
(
font.source().id(),
font.offset(),
font.attributes(),
font.cache_key(),
)
}
};
let epoch = self.epoch;
match self.sources.get_mut(&source_id) {
Some(data) => {
return data.as_mut().map(|d| {
d.1 = epoch;
Font {
data: d.0.clone(),
offset,
attributes,
key,
}
})
}
_ => {}
}
let source = self.index.base.sources.get(source_id.to_usize())?;
match source.get() {
Some(data) => {
self.sources.insert(source_id, Some((data.clone(), epoch)));
Some(Font {
data,
offset,
attributes,
key,
})
}
_ => {
self.sources.insert(source_id, None);
None
}
}
}
}
/// Internal cache of font groups.
///
/// The strategy here uses a two layer caching system that maps user font
/// groups to a list of resolved font identifiers and a group
/// identifier. The group identifier is then mapped to a transient
/// list of cached fonts. This structure provides reasonably fast lookup
/// while also allowing group invalidation and eviction without the
/// need for notifying user code or for a messy observer/listener style
/// system. Essentially, this is more complex than desired, but the complexity
/// is entirely encapsulated here.
#[derive(Default)]
struct GroupCache {
/// Maps from a user font descriptor key to a list of font
/// identifiers.
key_map: FxHashMap<FontGroupKey, CachedGroup>,
/// Temporary storage for parsing a user font descriptor.
tmp: Vec<(FontId, Attributes)>,
/// Next descriptor identifier.
next_id: u64,
/// Map from descriptor identifier to the list of cached fonts. This
/// is semi-transient: usually per layout session.
font_map: FxHashMap<FontGroupId, CachedFontList>,
/// Fonts referenced by the ranges in `font_map`.
fonts: Vec<CachedFont>,
/// Currently selected descriptor/script/language state for mapping
/// clusters.
state: GroupCacheState,
}
/// Current mapping state for a descriptor cache.
struct GroupCacheState {
/// Selected identifier.
id: FontGroupId,
/// Selected font list.
fonts: CachedFontList,
/// Fallback state.
fallback: Option<(Script, Cjk)>,
/// True if the fallbacks list is current.
fallbacks_ready: bool,
/// Transient fallback cache to avoid excessive queries.
fallback_map: FxHashMap<(FamilyId, Attributes), CachedFont>,
/// Current list of fallback families.
fallbacks: Vec<FamilyId>,
/// True if we've attempted to load an emoji font.
emoji_ready: bool,
/// Cached emoji font.
emoji: Option<CachedFont>,
}
impl Default for GroupCacheState {
fn default() -> Self {
Self {
id: FontGroupId(!0),
fonts: CachedFontList::default(),
fallback: None,
fallbacks_ready: true,
fallback_map: FxHashMap::default(),
fallbacks: Vec::new(),
emoji_ready: false,
emoji: None,
}
}
}
impl GroupCacheState {
fn reset(&mut self) {
self.id = FontGroupId(!0);
self.fonts = CachedFontList::default();
self.fallback = None;
self.fallbacks_ready = true;
self.fallback_map.clear();
self.fallbacks.clear();
self.emoji_ready = false;
self.emoji = None;
}
}
impl GroupCache {
/// Returns a font group identifier for the specified families and attributes.
fn get(&mut self, fonts: &FontCache, names: &str, key: u64, attrs: Attributes) -> FontGroupId {
use std::collections::hash_map::Entry;
let key = (key, attrs);
// Fast path for a descriptor we've already seen.
match self.key_map.get_mut(&key) {
Some(item) => {
item.epoch = fonts.epoch;
match self.font_map.entry(item.id) {
Entry::Occupied(..) => {}
Entry::Vacant(e) => {
let start = self.fonts.len();
self.fonts.extend(
item.data
.get()
.iter()
.map(|&sel| (sel.0, sel.1, attrs).into()),
);
let end = self.fonts.len();
e.insert(CachedFontList {
attributes: attrs,
start,
end,
});
}
}
return item.id;
}
_ => {}
}
// Parse the descriptor and collect the font identifiers.
self.tmp.clear();
for family in parse_families(names) {
match fonts.query(family, attrs).map(|f| f.selector(attrs)) {
Some(sel) => self.tmp.push((sel.0, sel.1)),
_ => {}
}
}
// Slow path: linear search.
for (item_key, item) in &self.key_map {
if item_key.1 != attrs {
continue;
}
let existing = item.data.get();
if existing == &self.tmp {
match self.font_map.entry(item.id) {
Entry::Occupied(..) => {}
Entry::Vacant(e) => {
let start = self.fonts.len();
self.fonts.extend(
item.data
.get()
.iter()
.map(|&sel| (sel.0, sel.1, attrs).into()),
);
let end = self.fonts.len();
e.insert(CachedFontList {
attributes: attrs,
start,
end,
});
}
}
return item.id;
}
}
// Insert a new entry.
let id = FontGroupId(self.next_id);
self.next_id += 1;
let mut data = GroupData::Inline(0, [(FontId(0), Attributes::default()); MAX_INLINE]);
for font in &self.tmp {
data.push(font.0, font.1);
}
let start = self.fonts.len();
self.fonts
.extend(self.tmp.iter().map(|&sel| (sel.0, sel.1, attrs).into()));
let end = self.fonts.len();
self.font_map.insert(
id,
CachedFontList {
attributes: attrs,
start,
end,
},
);
let desc = CachedGroup {
id,
epoch: fonts.epoch,
data,
};
self.key_map.insert(key, desc);
id
}
/// Selects a descriptor for mapping clusters.
fn select(&mut self, id: FontGroupId) {
if self.state.id == id {
return;
}
match self.font_map.get(&id) {
Some(fonts) => self.state.fonts = *fonts,
_ => self.state.fonts = CachedFontList::default(),
}
self.state.id = id;
}
/// Selects a fallback state.
fn select_fallbacks(&mut self, script: Script, cjk: Cjk) {
if self.state.fallback != Some((script, cjk)) {
self.state.fallback = Some((script, cjk));
self.state.fallbacks_ready = false;
self.state.fallbacks.clear();
}
}
fn fill_fallbacks(&mut self, fonts: &FontCache) {
self.state.fallbacks.clear();
self.state.fallbacks_ready = true;
match self.state.fallback {
Some((script, cjk)) => {
self.state
.fallbacks
.extend_from_slice(fonts.index.fallbacks(script, cjk));
}
_ => {}
}
}
fn emoji(&mut self, fonts: &FontCache, attrs: Attributes) -> Option<&mut CachedFont> {
if !self.state.emoji_ready {
self.state.emoji_ready = true;
let family = fonts.index.emoji_family()?;
let sel = fonts.query(family, ())?.selector(attrs);
self.state.emoji = Some(sel.into());
}
self.state.emoji.as_mut()
}
/// Clears all transient state.
fn reset(&mut self) {
self.state.reset();
self.font_map.clear();
self.fonts.clear();
}
// fn prune(&mut self, epoch: Epoch, target_size: usize) {
// if self.key_map.len() <= target_size {
// return;
// }
// let mut count = self.key_map.len() - target_size;
// self.key_map.retain(|_, v| {
// if count != 0 && v.epoch < epoch {
// count -= 1;
// false
// } else {
// true
// }
// });
// if count != 0 {
// self.key_map.retain(|_, _| {
// if count != 0 {
// count -= 1;
// false
// } else {
// true
// }
// });
// }
// }
}
struct CachedGroup {
id: FontGroupId,
epoch: Epoch,
data: GroupData,
}
#[derive(Clone)]
enum GroupData {
Inline(u8, [(FontId, Attributes); MAX_INLINE]),
Heap(Vec<(FontId, Attributes)>),
}
impl GroupData {
// fn clear(&mut self) {
// match self {
// Self::Inline(len, _) => {
// *len = 0;
// }
// Self::Heap(vec) => {
// vec.clear();
// }
// }
// }
fn push(&mut self, font: FontId, attrs: Attributes) {
match self {
Self::Inline(len, ids) => {
if *len as usize == ids.len() {
let mut vec = Vec::from(&ids[..]);
vec.push((font, attrs));
*self = Self::Heap(vec);
} else {
ids[*len as usize] = (font, attrs);
*len += 1;
}
}
Self::Heap(vec) => {
vec.push((font, attrs));
}
}
}
fn get(&self) -> &[(FontId, Attributes)] {
match self {
Self::Inline(len, ids) => &ids[..*len as usize],
Self::Heap(vec) => &vec,
}
}
}
#[derive(Copy, Clone, Default)]
struct CachedFontList {
/// Attributes are necessary for fallback font selection.
attributes: Attributes,
/// Range of cached fonts.
start: usize,
/// ... ditto
end: usize,
}
struct CachedFont {
id: FontId,
font: Option<Font>,
charmap: CharmapProxy,
synth: Synthesis,
error: bool,
}
impl CachedFont {
#[inline]
fn map_cluster(
&mut self,
fonts: &mut FontCache,
cluster: &mut CharCluster,
synth: &mut Synthesis,
first: bool,
) -> Option<(Font, Status)> {
if self.error {
return None;
}
let font = match &self.font {
Some(font) => font,
None => {
let font = fonts.get(self.id);
let font = match font {
Some(f) => f,
_ => {
self.error = true;
return None;
}
};
self.charmap = CharmapProxy::from_font(&font.as_ref());
self.font = Some(font);
self.font.as_ref().unwrap()
}
};
let charmap = self.charmap.materialize(&font.as_ref());
let status = cluster.map(|ch| charmap.map(ch));
if status != Status::Discard || first {
*synth = self.synth;
Some((font.clone(), status))
} else {
None
}
}
}
impl From<(FontId, Attributes, Attributes)> for CachedFont {
fn from(v: (FontId, Attributes, Attributes)) -> Self {
let synth = v.1.synthesize(v.2);
Self {
id: v.0,
font: None,
charmap: CharmapProxy::default(),
synth,
error: false,
}
}
}