Strong delta

This commit is contained in:
Laurenz 2022-11-29 14:57:05 +01:00
parent 0efe669278
commit 579dac3c91
7 changed files with 45 additions and 16 deletions

View File

@ -40,12 +40,15 @@ impl Behave for LinebreakNode {
} }
} }
/// Strong content, rendered in boldface by default. /// Strongly emphasizes content by increasing the font weight.
#[derive(Debug, Hash)] #[derive(Debug, Hash)]
pub struct StrongNode(pub Content); pub struct StrongNode(pub Content);
#[node(Show)] #[node(Show)]
impl StrongNode { impl StrongNode {
/// The delta to apply on the font weight.
pub const DELTA: i64 = 300;
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
Ok(Self(args.expect("body")?).pack()) Ok(Self(args.expect("body")?).pack())
} }
@ -59,12 +62,30 @@ impl StrongNode {
} }
impl Show for StrongNode { impl Show for StrongNode {
fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> Content {
self.0.clone().styled(TextNode::BOLD, Toggle) self.0.clone().styled(TextNode::DELTA, Delta(styles.get(Self::DELTA)))
} }
} }
/// Emphasized content, rendered with an italic font by default. /// A delta that is summed up when folded.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Delta(pub i64);
castable! {
Delta,
Expected: "integer",
Value::Int(delta) => Self(delta),
}
impl Fold for Delta {
type Output = i64;
fn fold(self, outer: Self::Output) -> Self::Output {
outer + self.0
}
}
/// Emphasizes content by flipping the italicness.
#[derive(Debug, Hash)] #[derive(Debug, Hash)]
pub struct EmphNode(pub Content); pub struct EmphNode(pub Content);
@ -84,7 +105,7 @@ impl EmphNode {
impl Show for EmphNode { impl Show for EmphNode {
fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content {
self.0.clone().styled(TextNode::ITALIC, Toggle) self.0.clone().styled(TextNode::EMPH, Toggle)
} }
} }

View File

@ -111,12 +111,12 @@ impl TextNode {
#[property(fold)] #[property(fold)]
pub const FEATURES: FontFeatures = FontFeatures(vec![]); pub const FEATURES: FontFeatures = FontFeatures(vec![]);
/// Whether the font weight should be increased by 300. /// A delta to apply on the font weight.
#[property(skip, fold)] #[property(skip, fold)]
pub const BOLD: Toggle = false; pub const DELTA: Delta = 0;
/// Whether the font style should be inverted. /// Whether the font style should be inverted.
#[property(skip, fold)] #[property(skip, fold)]
pub const ITALIC: Toggle = false; pub const EMPH: Toggle = false;
/// A case transformation that should be applied to the text. /// A case transformation that should be applied to the text.
#[property(skip)] #[property(skip)]
pub const CASE: Option<Case> = None; pub const CASE: Option<Case> = None;

View File

@ -537,11 +537,12 @@ pub fn variant(styles: StyleChain) -> FontVariant {
styles.get(TextNode::STRETCH), styles.get(TextNode::STRETCH),
); );
if styles.get(TextNode::BOLD) { let delta = styles.get(TextNode::DELTA);
variant.weight = variant.weight.thicken(300); variant.weight = variant
} .weight
.thicken(delta.clamp(i16::MIN as i64, i16::MAX as i64) as i16);
if styles.get(TextNode::ITALIC) { if styles.get(TextNode::EMPH) {
variant.style = match variant.style { variant.style = match variant.style {
FontStyle::Normal => FontStyle::Italic, FontStyle::Normal => FontStyle::Italic,
FontStyle::Italic => FontStyle::Normal, FontStyle::Italic => FontStyle::Normal,

View File

@ -280,10 +280,7 @@ castable! {
castable! { castable! {
FontWeight, FontWeight,
Expected: "integer or string", Expected: "integer or string",
Value::Int(v) => Value::Int(v) Value::Int(v) => Self::from_number(v.clamp(0, u16::MAX as i64) as u16),
.cast::<usize>()?
.try_into()
.map_or(Self::BLACK, Self::from_number),
Value::Str(string) => match string.as_str() { Value::Str(string) => match string.as_str() {
"thin" => Self::THIN, "thin" => Self::THIN,
"extralight" => Self::EXTRALIGHT, "extralight" => Self::EXTRALIGHT,

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -16,6 +16,16 @@ _Still [
// Inside of words can still use the functions. // Inside of words can still use the functions.
P#strong[art]ly em#emph[phas]ized. P#strong[art]ly em#emph[phas]ized.
---
// Adjusting the delta that strong applies on the weight.
Normal
#set strong(delta: 300)
*Bold*
#set strong(delta: 150)
*Medium* and *[*Bold*]*
--- ---
// Error: 13 expected underscore // Error: 13 expected underscore
#box[_Scoped] to body. #box[_Scoped] to body.