use super::*;
impl Default for NativeSystemFont {
fn default() -> Self {
Self::deterministic()
}
}
impl NativeSystemFont {
#[must_use]
pub fn system() -> Self {
Self {
info: probe_system_font(),
deterministic_only: false,
}
}
#[must_use]
pub fn deterministic() -> Self {
Self {
info: SystemFontInfo::new(
FontRenderBackend::DeterministicBitmap,
FONT_FAMILY_DETERMINISTIC.to_string(),
true,
false,
),
deterministic_only: true,
}
}
#[must_use]
pub fn thread_primary_font_binds() -> usize {
THREAD_PRIMARY_FONT_BINDS.with(Cell::get)
}
#[must_use]
pub fn thread_fallback_font_loads() -> usize {
THREAD_FALLBACK_FONT_LOADS.with(Cell::get)
}
}
impl SystemFontProvider for NativeSystemFont {
fn font_info(&self) -> SystemFontInfo {
self.get_info().clone()
}
fn rasterize(&self, key: GlyphKey) -> Option<RasterizedGlyph> {
if !self.get_deterministic_only()
&& let Some(native_glyph) = rasterize_native_glyph(key)
{
return Some(native_glyph);
}
Some(rasterize_glyph(key))
}
fn rasterize_batch(&self, keys: &[GlyphKey]) -> Vec<Option<RasterizedGlyph>> {
if keys.is_empty() {
return Vec::new();
}
if self.get_deterministic_only() {
let mut results: Vec<Option<RasterizedGlyph>> = Vec::with_capacity(keys.len());
for &key in keys.iter() {
results.push(Some(rasterize_glyph(key)));
}
return results;
}
let mut native_batch: Vec<Option<RasterizedGlyph>> = rasterize_native_glyph_batch(keys);
for (idx, &key) in keys.iter().enumerate() {
let needs_fallback: bool = native_batch.get(idx).is_none_or(Option::is_none);
if needs_fallback {
let fallback: RasterizedGlyph = rasterize_glyph(key);
if idx < native_batch.len() {
native_batch[idx] = Some(fallback);
} else {
native_batch.push(Some(fallback));
}
}
}
native_batch
}
fn rasterize_into(&self, key: GlyphKey, dst: &mut Vec<u8>) -> Option<(u32, u32, u32)> {
if !self.get_deterministic_only()
&& let Some(metrics) = rasterize_native_glyph_into(key, dst)
{
return Some(metrics);
}
let glyph: RasterizedGlyph = rasterize_glyph(key);
dst.clear();
dst.extend_from_slice(glyph.get_coverage().as_slice());
Some((glyph.get_width(), glyph.get_height(), glyph.get_advance()))
}
}
impl WebCssFontCache {
#[must_use]
pub const fn new() -> Self {
Self {
entries: Vec::new(),
evict_cursor: 0,
hits: 0,
misses: 0,
}
}
pub fn resolve(&mut self, key: GlyphKey) -> (u32, &str) {
let style_sig: u32 = GlyphAtlas::pack_style_sig(
key.get_size_tenths(),
key.get_bold(),
key.get_monospace(),
key.get_italic(),
);
let Self {
entries,
evict_cursor,
hits,
misses,
} = self;
let mut found_idx: Option<usize> = None;
for (idx, (sig, _)) in entries.iter().enumerate() {
if *sig == style_sig {
found_idx = Some(idx);
break;
}
}
if let Some(idx) = found_idx {
*hits = hits.saturating_add(1);
return (style_sig, entries[idx].1.as_str());
}
*misses = misses.saturating_add(1);
let formatted: String = format_web_css_font(key);
if entries.len() < WEB_FONT_CSS_CACHE_MAX {
let idx: usize = entries.len();
entries.push((style_sig, formatted));
return (style_sig, entries[idx].1.as_str());
}
let slot: usize = *evict_cursor % WEB_FONT_CSS_CACHE_MAX;
*evict_cursor = slot.wrapping_add(1) % WEB_FONT_CSS_CACHE_MAX;
entries[slot] = (style_sig, formatted);
(style_sig, entries[slot].1.as_str())
}
#[must_use]
pub fn len(&self) -> usize {
let Self { entries, .. } = self;
entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
let Self { entries, .. } = self;
entries.is_empty()
}
pub fn clear(&mut self) {
let Self {
entries,
evict_cursor,
..
} = self;
entries.clear();
*evict_cursor = 0;
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
impl CoreTextFontCacheEntry {
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
style_sig: 0,
referenced: false,
font: null_mut(),
ascent: 0.0,
descent: 0.0,
fallback_fonts: [null_mut(); CORETEXT_FALLBACK_SLOTS],
}
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
impl CoreTextFontCache {
#[must_use]
pub(crate) const fn new() -> Self {
Self {
gray_space: null_mut(),
scratch_ctx: null_mut(),
scratch_buf: Vec::new(),
scratch_w: 0,
scratch_h: 0,
count: 0,
clock_hand: 0,
index: [CORETEXT_INDEX_EMPTY; CORETEXT_FONT_INDEX_CAP],
slots: [CoreTextFontCacheEntry::empty(); CORETEXT_FONT_CACHE_SLOTS],
}
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
impl Drop for CoreTextFontCache {
fn drop(&mut self) {
let Self {
gray_space,
scratch_ctx,
count,
slots,
..
} = self;
release_coretext_cache(*gray_space, *scratch_ctx, &mut slots[..*count]);
*scratch_ctx = null_mut();
*gray_space = null_mut();
*count = 0;
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl FtLazyFace {
#[must_use]
pub(crate) const fn unprobed() -> Self {
Self {
probed: false,
face: null_mut(),
last_size_px: 0,
}
}
}
#[cfg(target_os = "android")]
impl FtAndroidFallbackFace {
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
path_hash: 0,
collection_index: 0,
referenced: false,
slot: FtLazyFace::unprobed(),
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Drop for FreeTypeRuntime {
fn drop(&mut self) {
release_freetype_runtime(self);
}
}
#[cfg(target_arch = "wasm32")]
impl WebFontScratch {
#[must_use]
pub(crate) const fn new() -> Self {
Self {
readback_canvas: None,
readback_ctx2d: None,
readback_width: 0,
readback_height: 0,
readback_last_sig: u32::MAX,
gpu_canvas: None,
gpu_ctx2d: None,
gpu_width: 0,
gpu_height: 0,
gpu_last_sig: u32::MAX,
css_cache: WebCssFontCache::new(),
total_readbacks: 0,
total_gpu_strip_batches: 0,
}
}
}
#[cfg(target_os = "windows")]
impl Win32FontCacheEntry {
#[must_use]
pub(crate) const fn empty() -> Self {
Self {
style_sig: 0,
referenced: false,
hfont: null_mut(),
ascent: 0,
descent: 0,
}
}
}
#[cfg(target_os = "windows")]
impl Win32FontCache {
#[must_use]
pub(crate) const fn new() -> Self {
Self {
hdc: null_mut(),
dwrite_attempted: false,
dwrite_factory: null_mut(),
count: 0,
clock_hand: 0,
index: [WIN32_INDEX_EMPTY; WIN32_FONT_INDEX_CAP],
slots: [Win32FontCacheEntry::empty(); WIN32_FONT_CACHE_SLOTS],
raw_buf: Vec::new(),
}
}
}
#[cfg(target_os = "windows")]
impl Drop for Win32FontCache {
fn drop(&mut self) {
let Self {
hdc,
dwrite_factory,
count,
slots,
..
} = self;
release_win32_cache(*hdc, *dwrite_factory, &mut slots[..*count]);
*hdc = null_mut();
*dwrite_factory = null_mut();
*count = 0;
}
}