silverwolf-x's picture
download
raw
3.1 kB
use anyhow::Result;
use bincode::{Decode, Encode};
use once_cell::sync::Lazy;
use serde::Deserialize;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs};
pub(crate) struct DefaultFallbackFont {
pub name: RcStr,
pub capsize_key: RcStr,
pub az_avg_width: f64,
pub units_per_em: u32,
}
// From https://github.com/vercel/next.js/blob/a3893bf69c83fb08e88c87bf8a21d987a0448c8e/packages/font/src/utils.ts#L4
pub(crate) static DEFAULT_SANS_SERIF_FONT: Lazy<DefaultFallbackFont> =
Lazy::new(|| DefaultFallbackFont {
name: rcstr!("Arial"),
capsize_key: rcstr!("arial"),
az_avg_width: 934.5116279069767,
units_per_em: 2048,
});
pub(crate) static DEFAULT_SERIF_FONT: Lazy<DefaultFallbackFont> =
Lazy::new(|| DefaultFallbackFont {
name: rcstr!("Times New Roman"),
capsize_key: rcstr!("timesNewRoman"),
az_avg_width: 854.3953488372093,
units_per_em: 2048,
});
/// An automatically generated fallback font generated by next/font.
#[turbo_tasks::value(shared)]
pub(crate) struct AutomaticFontFallback {
/// e.g. `__Roboto_Fallback_c123b8`
pub scoped_font_family: RcStr,
/// The name of font locally, used in `src: local("{}")`
pub local_font_family: RcStr,
pub adjustment: Option<FontAdjustment>,
}
#[turbo_tasks::value(shared)]
pub(crate) enum FontFallback {
/// An automatically generated fallback font generated by next/font. May
/// include an optional [[FontAdjustment]].
Automatic(AutomaticFontFallback),
/// There was an issue preparing the font fallback. Since resolving the
/// font css cannot fail, proper Errors cannot be returned. Emit an issue,
/// return this and omit fallback information instead.
Error,
/// A list of manually provided font names to use a fallback, as-is.
Manual(Vec<RcStr>),
}
#[turbo_tasks::value_impl]
impl FontFallback {
#[turbo_tasks::function]
pub(crate) fn has_size_adjust(&self) -> Vc<bool> {
Vc::cell(matches!(self, FontFallback::Automatic(auto) if auto.adjustment.is_some()))
}
}
#[turbo_tasks::value(transparent)]
pub(crate) struct FontFallbacks(pub Vec<ResolvedVc<FontFallback>>);
#[turbo_tasks::value_impl]
impl FontFallbacks {
#[turbo_tasks::function]
pub(crate) async fn has_size_adjust(&self) -> Result<Vc<bool>> {
for fallback in &self.0 {
if *fallback.has_size_adjust().await? {
return Ok(Vc::cell(true));
}
}
Ok(Vc::cell(false))
}
}
/// An adjustment to be made to a fallback font to approximate the geometry of
/// the main webfont. Rendered as e.g. `ascent-override: 56.8%;` in the
/// stylesheet
#[derive(Debug, PartialEq, Deserialize, TraceRawVcs, NonLocalValue, Encode, Decode)]
pub(crate) struct FontAdjustment {
pub ascent: f64,
pub descent: f64,
pub line_gap: f64,
pub size_adjust: f64,
}
// Necessary since floating points in this struct don't implement Eq, but it's
// required for turbo tasks values.
impl Eq for FontAdjustment {}

Xet Storage Details

Size:
3.1 kB
·
Xet hash:
985edf0faca0d824242a54da88508d11b68d34472a3d575a009d9e4e7d91b2db

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.