diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-08 10:43:03 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-08 10:44:24 +0100 |
| commit | 1b2b53ecb91a9bd7fb3493e471ae03cd142a7c03 (patch) | |
| tree | 682124c2343db9491c84bddbdca026c676f220c6 /library/src/text | |
| parent | 25b5bd117529cd04bb789e1988eb3a3db8025a0e (diff) | |
Require font to be a named argument
Diffstat (limited to 'library/src/text')
| -rw-r--r-- | library/src/text/deco.rs | 5 | ||||
| -rw-r--r-- | library/src/text/mod.rs | 85 | ||||
| -rw-r--r-- | library/src/text/raw.rs | 8 | ||||
| -rw-r--r-- | library/src/text/shaping.rs | 2 | ||||
| -rw-r--r-- | library/src/text/shift.rs | 2 |
5 files changed, 31 insertions, 71 deletions
diff --git a/library/src/text/deco.rs b/library/src/text/deco.rs index 18145d28..d47b336b 100644 --- a/library/src/text/deco.rs +++ b/library/src/text/deco.rs @@ -31,7 +31,6 @@ pub struct UnderlineNode { /// ) /// ``` #[settable] - #[shorthand] #[resolve] #[fold] #[default] @@ -117,7 +116,6 @@ pub struct OverlineNode { /// ) /// ``` #[settable] - #[shorthand] #[resolve] #[fold] #[default] @@ -207,7 +205,6 @@ pub struct StrikeNode { /// This is #strike(stroke: 10pt)[redacted]. /// ``` #[settable] - #[shorthand] #[resolve] #[fold] #[default] @@ -219,7 +216,7 @@ pub struct StrikeNode { /// This is useful if you are unhappy with the offset your font provides. /// /// ```example - /// #set text(family: "Inria Serif") + /// #set text(font: "Inria Serif") /// This is #strike(offset: auto)[low-ish]. \ /// This is #strike(offset: -3.5pt)[on-top]. /// ``` diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index cde0163e..329ff6a9 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -39,60 +39,12 @@ use crate::prelude::*; /// ``` /// /// ## Parameters -/// - family: `FallbackList` (positional, named, variadic, settable) -/// A prioritized sequence of font families. -/// -/// When processing text, Typst tries all specified font families in order -/// until it finds a font that has the necessary glyphs. In the example below, -/// the font `Inria Serif` is preferred, but since it does not contain Arabic -/// glyphs, the arabic text uses `Noto Sans Arabic` instead. -/// -/// ```example -/// #set text( -/// "Inria Serif", -/// "Noto Sans Arabic", -/// ) -/// -/// This is Latin. \ -/// هذا عربي. -/// -/// ``` -/// /// - body: `Content` (positional, required) /// Content in which all text is styled according to the other arguments. /// /// Display: Text /// Category: text #[node(Construct)] -#[set({ - if let Some(family) = args.named("family")? { - styles.set(Self::FAMILY, family); - } else { - let mut count = 0; - let mut content = false; - for item in args.items.iter().filter(|item| item.name.is_none()) { - if EcoString::is(&item.value) { - count += 1; - } else if <Content as Cast<Spanned<Value>>>::is(&item.value) { - content = true; - } - } - - // Skip the final string if it's needed as the body. - if constructor && !content && count > 0 { - count -= 1; - } - - if count > 0 { - let mut list = Vec::with_capacity(count); - for _ in 0..count { - list.push(args.find()?.unwrap()); - } - - styles.set(Self::FAMILY, FallbackList(list)); - } - } -})] pub struct TextNode { /// The text. #[positional] @@ -101,10 +53,25 @@ pub struct TextNode { pub text: EcoString, /// A prioritized sequence of font families. + /// + /// When processing text, Typst tries all specified font families in order + /// until it finds a font that has the necessary glyphs. In the example + /// below, the font `Inria Serif` is preferred, but since it does not + /// contain Arabic glyphs, the arabic text uses `Noto Sans Arabic` instead. + /// + /// ```example + /// #set text(font: ( + /// "Inria Serif", + /// "Noto Sans Arabic", + /// )) + /// + /// This is Latin. \ + /// هذا عربي. + /// + /// ``` #[settable] - #[skip] - #[default(FallbackList(vec![FontFamily::new("Linux Libertine")]))] - pub family: FallbackList, + #[default(FontList(vec![FontFamily::new("Linux Libertine")]))] + pub font: FontList, /// Whether to allow last resort font fallback when the primary font list /// contains no match. This lets Typst search through all available fonts @@ -117,7 +84,7 @@ pub struct TextNode { /// something is up. /// /// ```example - /// #set text(family: "Inria Serif") + /// #set text(font: "Inria Serif") /// هذا عربي /// /// #set text(fallback: false) @@ -141,8 +108,8 @@ pub struct TextNode { /// style later if you change your mind about how to signify the emphasis. /// /// ```example - /// #text("Linux Libertine", style: "italic")[Italic] - /// #text("DejaVu Sans", style: "oblique")[Oblique] + /// #text(font: "Linux Libertine", style: "italic")[Italic] + /// #text(font: "DejaVu Sans", style: "oblique")[Oblique] /// ``` #[settable] #[default(FontStyle::Normal)] @@ -460,7 +427,7 @@ pub struct TextNode { /// default numbers for the font are used. /// /// ```example - /// #set text(20pt, "Noto Sans") + /// #set text(font: "Noto Sans", 20pt) /// #set text(number-type: "lining") /// Number 9. /// @@ -475,7 +442,7 @@ pub struct TextNode { /// numbers for the font are used. /// /// ```example - /// #set text(20pt, "Noto Sans") + /// #set text(font: "Noto Sans", 20pt) /// #set text(number-width: "proportional") /// A 12 B 34. \ /// A 56 B 78. @@ -609,16 +576,16 @@ cast_to_value! { /// Font family fallback list. #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] -pub struct FallbackList(pub Vec<FontFamily>); +pub struct FontList(pub Vec<FontFamily>); cast_from_value! { - FallbackList, + FontList, family: FontFamily => Self(vec![family]), values: Array => Self(values.into_iter().map(|v| v.cast()).collect::<StrResult<_>>()?), } cast_to_value! { - v: FallbackList => v.0.into() + v: FontList => v.0.into() } /// The size of text. diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index cdaefd06..8c40c6ea 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -3,8 +3,7 @@ use syntect::highlighting as synt; use typst::syntax::{self, LinkedNode}; use super::{ - FallbackList, FontFamily, Hyphenate, LinebreakNode, SmartQuoteNode, TextNode, - TextSize, + FontFamily, FontList, Hyphenate, LinebreakNode, SmartQuoteNode, TextNode, TextSize, }; use crate::layout::BlockNode; use crate::prelude::*; @@ -185,10 +184,7 @@ impl Finalize for RawNode { map.set(TextNode::OVERHANG, false); map.set(TextNode::HYPHENATE, Hyphenate(Smart::Custom(false))); map.set(TextNode::SIZE, TextSize(Em::new(0.8).into())); - map.set( - TextNode::FAMILY, - FallbackList(vec![FontFamily::new("DejaVu Sans Mono")]), - ); + map.set(TextNode::FONT, FontList(vec![FontFamily::new("DejaVu Sans Mono")])); map.set(SmartQuoteNode::ENABLED, false); realized.styled_with_map(map) } diff --git a/library/src/text/shaping.rs b/library/src/text/shaping.rs index 709cce25..e1b12120 100644 --- a/library/src/text/shaping.rs +++ b/library/src/text/shaping.rs @@ -560,7 +560,7 @@ pub fn families(styles: StyleChain) -> impl Iterator<Item = FontFamily> + Clone let tail = if styles.get(TextNode::FALLBACK) { FALLBACKS } else { &[] }; styles - .get(TextNode::FAMILY) + .get(TextNode::FONT) .0 .into_iter() .chain(tail.iter().copied().map(FontFamily::new)) diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index 105953b6..c44cc3b0 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -174,7 +174,7 @@ fn search_text(content: &Content, sub: bool) -> Option<EcoString> { /// given string. fn is_shapable(vt: &Vt, text: &str, styles: StyleChain) -> bool { let world = vt.world(); - for family in styles.get(TextNode::FAMILY).0.iter() { + for family in styles.get(TextNode::FONT).0.iter() { if let Some(font) = world .book() .select(family.as_str(), variant(styles)) |
