mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Make font fallback disableable
This commit is contained in:
parent
7eeafbd464
commit
17ea0d4ba9
@ -119,13 +119,6 @@ impl Default for ParState {
|
|||||||
/// Defines font properties.
|
/// Defines font properties.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||||
pub struct FontState {
|
pub struct FontState {
|
||||||
/// Whether 300 extra font weight should be added to what is defined by the
|
|
||||||
/// `variant`.
|
|
||||||
pub strong: bool,
|
|
||||||
/// Whether the the font style defined by the `variant` should be inverted.
|
|
||||||
pub emph: bool,
|
|
||||||
/// Whether a monospace font should be preferred.
|
|
||||||
pub monospace: bool,
|
|
||||||
/// The font size.
|
/// The font size.
|
||||||
pub size: Length,
|
pub size: Length,
|
||||||
/// The selected font variant (the final variant also depends on `strong`
|
/// The selected font variant (the final variant also depends on `strong`
|
||||||
@ -140,6 +133,15 @@ pub struct FontState {
|
|||||||
/// A list of font families with generic class definitions (the final
|
/// A list of font families with generic class definitions (the final
|
||||||
/// family list also depends on `monospace`).
|
/// family list also depends on `monospace`).
|
||||||
pub families: Rc<FamilyState>,
|
pub families: Rc<FamilyState>,
|
||||||
|
/// Whether 300 extra font weight should be added to what is defined by the
|
||||||
|
/// `variant`.
|
||||||
|
pub strong: bool,
|
||||||
|
/// Whether the the font style defined by the `variant` should be inverted.
|
||||||
|
pub emph: bool,
|
||||||
|
/// Whether a monospace font should be preferred.
|
||||||
|
pub monospace: bool,
|
||||||
|
/// Whether font fallback to a base list should occur.
|
||||||
|
pub fallback: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FontState {
|
impl FontState {
|
||||||
@ -164,10 +166,11 @@ impl FontState {
|
|||||||
|
|
||||||
/// The resolved family iterator.
|
/// The resolved family iterator.
|
||||||
pub fn families(&self) -> impl Iterator<Item = &str> + Clone {
|
pub fn families(&self) -> impl Iterator<Item = &str> + Clone {
|
||||||
let head = self
|
let head = if self.monospace {
|
||||||
.monospace
|
self.families.monospace.as_slice()
|
||||||
.then(|| self.families.monospace.as_slice())
|
} else {
|
||||||
.unwrap_or_default();
|
&[]
|
||||||
|
};
|
||||||
|
|
||||||
let core = self.families.list.iter().flat_map(move |family| {
|
let core = self.families.list.iter().flat_map(move |family| {
|
||||||
match family {
|
match family {
|
||||||
@ -178,10 +181,13 @@ impl FontState {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
head.iter()
|
let tail = if self.fallback {
|
||||||
.chain(core)
|
self.families.base.as_slice()
|
||||||
.chain(self.families.base.iter())
|
} else {
|
||||||
.map(String::as_str)
|
&[]
|
||||||
|
};
|
||||||
|
|
||||||
|
head.iter().chain(core).chain(tail).map(String::as_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Access the `families` state mutably.
|
/// Access the `families` state mutably.
|
||||||
@ -193,19 +199,20 @@ impl FontState {
|
|||||||
impl Default for FontState {
|
impl Default for FontState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
families: Rc::new(FamilyState::default()),
|
size: Length::pt(11.0),
|
||||||
variant: FontVariant {
|
variant: FontVariant {
|
||||||
style: FontStyle::Normal,
|
style: FontStyle::Normal,
|
||||||
weight: FontWeight::REGULAR,
|
weight: FontWeight::REGULAR,
|
||||||
stretch: FontStretch::NORMAL,
|
stretch: FontStretch::NORMAL,
|
||||||
},
|
},
|
||||||
strong: false,
|
|
||||||
emph: false,
|
|
||||||
monospace: false,
|
|
||||||
size: Length::pt(11.0),
|
|
||||||
top_edge: VerticalFontMetric::CapHeight,
|
top_edge: VerticalFontMetric::CapHeight,
|
||||||
bottom_edge: VerticalFontMetric::Baseline,
|
bottom_edge: VerticalFontMetric::Baseline,
|
||||||
fill: Paint::Color(Color::Rgba(RgbaColor::BLACK)),
|
fill: Paint::Color(Color::Rgba(RgbaColor::BLACK)),
|
||||||
|
families: Rc::new(FamilyState::default()),
|
||||||
|
strong: false,
|
||||||
|
emph: false,
|
||||||
|
monospace: false,
|
||||||
|
fallback: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
|||||||
let serif = args.named("serif")?;
|
let serif = args.named("serif")?;
|
||||||
let sans_serif = args.named("sans-serif")?;
|
let sans_serif = args.named("sans-serif")?;
|
||||||
let monospace = args.named("monospace")?;
|
let monospace = args.named("monospace")?;
|
||||||
|
let fallback = args.named("fallback")?;
|
||||||
|
|
||||||
ctx.template.modify(move |state| {
|
ctx.template.modify(move |state| {
|
||||||
let font = state.font_mut();
|
let font = state.font_mut();
|
||||||
@ -66,6 +67,10 @@ pub fn font(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
|||||||
if let Some(FamilyDef(monospace)) = &monospace {
|
if let Some(FamilyDef(monospace)) = &monospace {
|
||||||
font.families_mut().monospace = monospace.clone();
|
font.families_mut().monospace = monospace.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(fallback) = fallback {
|
||||||
|
font.fallback = fallback;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Value::None)
|
Ok(Value::None)
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
@ -33,6 +33,18 @@ Emoji: 🐪, 🌋, 🏞
|
|||||||
This is [#font(fill: rgb("FA644B")) way more] colorful.
|
This is [#font(fill: rgb("FA644B")) way more] colorful.
|
||||||
]
|
]
|
||||||
|
|
||||||
|
// Disable font fallback beyond the user-specified list.
|
||||||
|
// Without disabling, Latin Modern Math would come to the rescue.
|
||||||
|
#font("PT Sans", "Twitter Color Emoji", fallback: false)
|
||||||
|
2π = 𝛼 + 𝛽. ✅
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test class definitions.
|
||||||
|
#font(sans-serif: "PT Sans")
|
||||||
|
[#font(family: sans-serif) Sans-serif.] \
|
||||||
|
[#font(monospace) Monospace.] \
|
||||||
|
[#font(monospace, monospace: ("Nope", "Latin Modern Math")) Math.]
|
||||||
|
|
||||||
---
|
---
|
||||||
// Test top and bottom edge.
|
// Test top and bottom edge.
|
||||||
|
|
||||||
@ -47,13 +59,6 @@ Emoji: 🐪, 🌋, 🏞
|
|||||||
#try(cap-height, baseline)
|
#try(cap-height, baseline)
|
||||||
#try(x-height, baseline)
|
#try(x-height, baseline)
|
||||||
|
|
||||||
---
|
|
||||||
// Test class definitions.
|
|
||||||
#font(sans-serif: "PT Sans")
|
|
||||||
[#font(family: sans-serif) Sans-serif.] \
|
|
||||||
[#font(monospace) Monospace.] \
|
|
||||||
[#font(monospace, monospace: ("Nope", "Latin Modern Math")) Math.]
|
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 7-12 unexpected argument
|
// Error: 7-12 unexpected argument
|
||||||
#font(false)
|
#font(false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user