diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-08 11:45:54 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-08 11:45:59 +0100 |
| commit | a7a4cae2948176119e8995bd8e1868f2d0e65029 (patch) | |
| tree | 123e8867c939373c1c5ce5a0d20f55192c2467c2 /library/src/text | |
| parent | 0a41844cc4e645e87fe48aa31ed3a4fd40a6ab11 (diff) | |
Less style properties
Diffstat (limited to 'library/src/text')
| -rw-r--r-- | library/src/text/link.rs | 71 | ||||
| -rw-r--r-- | library/src/text/raw.rs | 13 | ||||
| -rw-r--r-- | library/src/text/shaping.rs | 2 |
3 files changed, 29 insertions, 57 deletions
diff --git a/library/src/text/link.rs b/library/src/text/link.rs index b74ca530..acd37df6 100644 --- a/library/src/text/link.rs +++ b/library/src/text/link.rs @@ -7,31 +7,34 @@ pub struct LinkNode { /// The destination the link points to. pub dest: Destination, /// How the link is represented. - pub body: Option<Content>, + pub body: Content, } impl LinkNode { /// Create a link node from a URL with its bare text. pub fn from_url(url: EcoString) -> Self { - Self { dest: Destination::Url(url), body: None } + let mut text = url.as_str(); + for prefix in ["mailto:", "tel:"] { + text = text.trim_start_matches(prefix); + } + let shorter = text.len() < url.len(); + let body = TextNode::packed(if shorter { text.into() } else { url.clone() }); + Self { dest: Destination::Url(url), body } } } #[node(Show, Finalize)] impl LinkNode { - /// The fill color of text in the link. Just the surrounding text color - /// if `auto`. - pub const FILL: Smart<Paint> = Smart::Auto; - /// Whether to underline the link. - pub const UNDERLINE: Smart<bool> = Smart::Auto; - fn construct(_: &mut Vm, args: &mut Args) -> SourceResult<Content> { let dest = args.expect::<Destination>("destination")?; - let body = match dest { - Destination::Url(_) => args.eat()?, - Destination::Internal(_) => Some(args.expect("body")?), - }; - Ok(Self { dest, body }.pack()) + Ok(match dest { + Destination::Url(url) => match args.eat()? { + Some(body) => Self { dest: Destination::Url(url), body }, + None => Self::from_url(url), + }, + Destination::Internal(_) => Self { dest, body: args.expect("body")? }, + } + .pack()) } fn field(&self, name: &str) -> Option<Value> { @@ -40,10 +43,7 @@ impl LinkNode { Destination::Url(url) => Value::Str(url.clone().into()), Destination::Internal(loc) => Value::Dict(loc.encode()), }), - "body" => Some(match &self.body { - Some(body) => Value::Content(body.clone()), - None => Value::None, - }), + "body" => Some(Value::Content(self.body.clone())), _ => None, } } @@ -53,23 +53,13 @@ impl Show for LinkNode { fn unguard_parts(&self, id: RecipeId) -> Content { Self { dest: self.dest.clone(), - body: self.body.as_ref().map(|body| body.unguard(id)), + body: self.body.unguard(id), } .pack() } fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(self.body.clone().unwrap_or_else(|| match &self.dest { - Destination::Url(url) => { - let mut text = url.as_str(); - for prefix in ["mailto:", "tel:"] { - text = text.trim_start_matches(prefix); - } - let shorter = text.len() < url.len(); - TextNode::packed(if shorter { text.into() } else { url.clone() }) - } - Destination::Internal(_) => Content::empty(), - })) + Ok(self.body.clone()) } } @@ -77,26 +67,9 @@ impl Finalize for LinkNode { fn finalize( &self, _: Tracked<dyn World>, - styles: StyleChain, - mut realized: Content, + _: StyleChain, + realized: Content, ) -> SourceResult<Content> { - let mut map = StyleMap::new(); - map.set(TextNode::LINK, Some(self.dest.clone())); - - if let Smart::Custom(fill) = styles.get(Self::FILL) { - map.set(TextNode::FILL, fill); - } - - if match styles.get(Self::UNDERLINE) { - Smart::Auto => match &self.dest { - Destination::Url(_) => true, - Destination::Internal(_) => false, - }, - Smart::Custom(underline) => underline, - } { - realized = realized.underlined(); - } - - Ok(realized.styled_with_map(map)) + Ok(realized.styled(TextNode::LINK, Some(self.dest.clone()))) } } diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index c6229d59..2041b25e 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -6,7 +6,7 @@ use syntect::highlighting::{ use syntect::parsing::SyntaxSet; use typst::syntax; -use super::{FontFamily, Hyphenate, LinebreakNode, TextNode}; +use super::{FallbackList, FontFamily, Hyphenate, LinebreakNode, TextNode}; use crate::layout::{BlockNode, BlockSpacing}; use crate::prelude::*; @@ -24,9 +24,6 @@ impl RawNode { /// The language to syntax-highlight in. #[property(referenced)] pub const LANG: Option<EcoString> = None; - /// The raw text's font family. - #[property(referenced)] - pub const FAMILY: FontFamily = FontFamily::new("IBM Plex Mono"); /// The spacing above block-level raw. #[property(resolve, shorthand(around))] pub const ABOVE: Option<BlockSpacing> = Some(Ratio::one().into()); @@ -119,14 +116,16 @@ impl Finalize for RawNode { styles: StyleChain, mut realized: Content, ) -> SourceResult<Content> { - let mut map = StyleMap::new(); - map.set_family(styles.get(Self::FAMILY).clone(), styles); + realized = realized.styled( + TextNode::FAMILY, + FallbackList(vec![FontFamily::new("IBM Plex Mono")]), + ); if self.block { realized = realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)); } - Ok(realized.styled_with_map(map)) + Ok(realized) } } diff --git a/library/src/text/shaping.rs b/library/src/text/shaping.rs index b67ce411..ac7218a0 100644 --- a/library/src/text/shaping.rs +++ b/library/src/text/shaping.rs @@ -552,7 +552,7 @@ pub fn variant(styles: StyleChain) -> FontVariant { } /// Resolve a prioritized iterator over the font families. -fn families(styles: StyleChain) -> impl Iterator<Item = &str> + Clone { +pub fn families(styles: StyleChain) -> impl Iterator<Item = &str> + Clone { const FALLBACKS: &[&str] = &[ "ibm plex sans", "twitter color emoji", |
