diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:17:24 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:42:14 +0100 |
| commit | c38d72383d2068361d635d6c1c78ba97aa917801 (patch) | |
| tree | e758418a2d704d69dee88faf4a9a9c69b25b47ca /library/src/text | |
| parent | d7a65fa26d131179d9d82226e5ee1b562084e48a (diff) | |
Make all optional fields settable
Diffstat (limited to 'library/src/text')
| -rw-r--r-- | library/src/text/deco.rs | 72 | ||||
| -rw-r--r-- | library/src/text/misc.rs | 14 | ||||
| -rw-r--r-- | library/src/text/mod.rs | 90 | ||||
| -rw-r--r-- | library/src/text/quotes.rs | 2 | ||||
| -rw-r--r-- | library/src/text/raw.rs | 11 | ||||
| -rw-r--r-- | library/src/text/shift.rs | 38 |
6 files changed, 74 insertions, 153 deletions
diff --git a/library/src/text/deco.rs b/library/src/text/deco.rs index 1b3167e1..2a552226 100644 --- a/library/src/text/deco.rs +++ b/library/src/text/deco.rs @@ -15,11 +15,6 @@ use crate::prelude::*; /// Category: text #[node(Show)] pub struct UnderlineNode { - /// The content to underline. - #[positional] - #[required] - pub body: Content, - /// How to stroke the line. The text color and thickness are read from the /// font tables if `{auto}`. /// @@ -30,10 +25,8 @@ pub struct UnderlineNode { /// [care], /// ) /// ``` - #[settable] #[resolve] #[fold] - #[default] pub stroke: Smart<PartialStroke>, /// Position of the line relative to the baseline, read from the font tables @@ -44,9 +37,7 @@ pub struct UnderlineNode { /// The Tale Of A Faraway Line I /// ] /// ``` - #[settable] #[resolve] - #[default] pub offset: Smart<Length>, /// Amount that the line will be longer or shorter than its associated text. @@ -56,9 +47,7 @@ pub struct UnderlineNode { /// underline(extent: 2pt)[Chapter 1] /// ) /// ``` - #[settable] #[resolve] - #[default] pub extent: Length, /// Whether the line skips sections in which it would collide with the @@ -68,19 +57,23 @@ pub struct UnderlineNode { /// This #underline(evade: true)[is great]. /// This #underline(evade: false)[is less great]. /// ``` - #[settable] #[default(true)] pub evade: bool, + + /// The content to underline. + #[positional] + #[required] + pub body: Content, } impl Show for UnderlineNode { fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { Ok(self.body().styled(TextNode::set_deco(Decoration { line: DecoLine::Underline, - stroke: Self::stroke_in(styles).unwrap_or_default(), - offset: Self::offset_in(styles), - extent: Self::extent_in(styles), - evade: Self::evade_in(styles), + stroke: self.stroke(styles).unwrap_or_default(), + offset: self.offset(styles), + extent: self.extent(styles), + evade: self.evade(styles), }))) } } @@ -96,11 +89,6 @@ impl Show for UnderlineNode { /// Category: text #[node(Show)] pub struct OverlineNode { - /// The content to add a line over. - #[positional] - #[required] - pub body: Content, - /// How to stroke the line. The text color and thickness are read from the /// font tables if `{auto}`. /// @@ -112,10 +100,8 @@ pub struct OverlineNode { /// [The Forest Theme], /// ) /// ``` - #[settable] #[resolve] #[fold] - #[default] pub stroke: Smart<PartialStroke>, /// Position of the line relative to the baseline, read from the font tables @@ -126,9 +112,7 @@ pub struct OverlineNode { /// The Tale Of A Faraway Line II /// ] /// ``` - #[settable] #[resolve] - #[default] pub offset: Smart<Length>, /// Amount that the line will be longer or shorter than its associated text. @@ -138,9 +122,7 @@ pub struct OverlineNode { /// #set underline(extent: 4pt) /// #overline(underline[Typography Today]) /// ``` - #[settable] #[resolve] - #[default] pub extent: Length, /// Whether the line skips sections in which it would collide with the @@ -155,19 +137,23 @@ pub struct OverlineNode { /// [Temple], /// ) /// ``` - #[settable] #[default(true)] pub evade: bool, + + /// The content to add a line over. + #[positional] + #[required] + pub body: Content, } impl Show for OverlineNode { fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { Ok(self.body().styled(TextNode::set_deco(Decoration { line: DecoLine::Overline, - stroke: Self::stroke_in(styles).unwrap_or_default(), - offset: Self::offset_in(styles), - extent: Self::extent_in(styles), - evade: Self::evade_in(styles), + stroke: self.stroke(styles).unwrap_or_default(), + offset: self.offset(styles), + extent: self.extent(styles), + evade: self.evade(styles), }))) } } @@ -183,11 +169,6 @@ impl Show for OverlineNode { /// Category: text #[node(Show)] pub struct StrikeNode { - /// The content to strike through. - #[positional] - #[required] - pub body: Content, - /// How to stroke the line. The text color and thickness are read from the /// font tables if `{auto}`. /// @@ -198,10 +179,8 @@ pub struct StrikeNode { /// This is #strike(stroke: 1.5pt + red)[very stricken through]. \ /// This is #strike(stroke: 10pt)[redacted]. /// ``` - #[settable] #[resolve] #[fold] - #[default] pub stroke: Smart<PartialStroke>, /// Position of the line relative to the baseline, read from the font tables @@ -214,9 +193,7 @@ pub struct StrikeNode { /// This is #strike(offset: auto)[low-ish]. \ /// This is #strike(offset: -3.5pt)[on-top]. /// ``` - #[settable] #[resolve] - #[default] pub offset: Smart<Length>, /// Amount that the line will be longer or shorter than its associated text. @@ -225,19 +202,22 @@ pub struct StrikeNode { /// This #strike(extent: -2pt)[skips] parts of the word. /// This #strike(extent: 2pt)[extends] beyond the word. /// ``` - #[settable] #[resolve] - #[default] pub extent: Length, + + /// The content to strike through. + #[positional] + #[required] + pub body: Content, } impl Show for StrikeNode { fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { Ok(self.body().styled(TextNode::set_deco(Decoration { line: DecoLine::Strikethrough, - stroke: Self::stroke_in(styles).unwrap_or_default(), - offset: Self::offset_in(styles), - extent: Self::extent_in(styles), + stroke: self.stroke(styles).unwrap_or_default(), + offset: self.offset(styles), + extent: self.extent(styles), evade: false, }))) } diff --git a/library/src/text/misc.rs b/library/src/text/misc.rs index 91ac1748..64ab5bd2 100644 --- a/library/src/text/misc.rs +++ b/library/src/text/misc.rs @@ -51,7 +51,6 @@ pub struct LinebreakNode { /// line breaks in this paragraph #jb /// for an _interesting_ result. #jb /// ``` - #[named] #[default(false)] pub justify: bool, } @@ -85,25 +84,24 @@ impl Behave for LinebreakNode { /// Category: text #[node(Show)] pub struct StrongNode { - /// The content to strongly emphasize. - #[positional] - #[required] - pub body: Content, - /// The delta to apply on the font weight. /// /// ```example /// #set strong(delta: 0) /// No *effect!* /// ``` - #[settable] #[default(300)] pub delta: i64, + + /// The content to strongly emphasize. + #[positional] + #[required] + pub body: Content, } impl Show for StrongNode { fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { - Ok(self.body().styled(TextNode::set_delta(Delta(Self::delta_in(styles))))) + Ok(self.body().styled(TextNode::set_delta(Delta(self.delta(styles))))) } } diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index 2318c426..83f4e2d7 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -46,12 +46,6 @@ use crate::prelude::*; /// Category: text #[node(Construct)] pub struct TextNode { - /// The text. - #[positional] - #[required] - #[skip] - pub text: EcoString, - /// A prioritized sequence of font families. /// /// When processing text, Typst tries all specified font families in order @@ -69,7 +63,6 @@ pub struct TextNode { /// هذا عربي. /// /// ``` - #[settable] #[default(FontList(vec![FontFamily::new("Linux Libertine")]))] pub font: FontList, @@ -90,7 +83,6 @@ pub struct TextNode { /// #set text(fallback: false) /// هذا عربي /// ``` - #[settable] #[default(true)] pub fallback: bool, @@ -111,8 +103,6 @@ pub struct TextNode { /// #text(font: "Linux Libertine", style: "italic")[Italic] /// #text(font: "DejaVu Sans", style: "oblique")[Oblique] /// ``` - #[settable] - #[default(FontStyle::Normal)] pub style: FontStyle, /// The desired thickness of the font's glyphs. Accepts an integer between @@ -132,8 +122,6 @@ pub struct TextNode { /// #text(weight: 500)[Medium] \ /// #text(weight: "bold")[Bold] /// ``` - #[settable] - #[default(FontWeight::REGULAR)] pub weight: FontWeight, /// The desired width of the glyphs. Accepts a ratio between `{50%}` and @@ -144,8 +132,6 @@ pub struct TextNode { /// #text(stretch: 75%)[Condensed] \ /// #text(stretch: 100%)[Normal] /// ``` - #[settable] - #[default(FontStretch::NORMAL)] pub stretch: FontStretch, /// The size of the glyphs. This value forms the basis of the `em` unit: @@ -158,8 +144,7 @@ pub struct TextNode { /// #set text(size: 20pt) /// very #text(1.5em)[big] text /// ``` - #[settable] - #[shorthand] + #[parse(args.named_or_find("size")?)] #[fold] #[default(Abs::pt(11.0))] pub size: TextSize, @@ -170,8 +155,7 @@ pub struct TextNode { /// #set text(fill: red) /// This text is red. /// ``` - #[shorthand] - #[settable] + #[parse(args.named_or_find("fill")?)] #[default(Color::BLACK.into())] pub fill: Paint, @@ -181,9 +165,7 @@ pub struct TextNode { /// #set text(tracking: 1.5pt) /// Distant text. /// ``` - #[settable] #[resolve] - #[default(Length::zero())] pub tracking: Length, /// The amount of space between words. @@ -195,7 +177,6 @@ pub struct TextNode { /// #set text(spacing: 200%) /// Text with distant words. /// ``` - #[settable] #[resolve] #[default(Rel::one())] pub spacing: Rel<Length>, @@ -206,9 +187,7 @@ pub struct TextNode { /// A #text(baseline: 3pt)[lowered] /// word. /// ``` - #[settable] #[resolve] - #[default(Length::zero())] pub baseline: Length, /// Whether certain glyphs can hang over into the margin in justified text. @@ -231,7 +210,6 @@ pub struct TextNode { /// margin, making the paragraph's /// edge less clear. /// ``` - #[settable] #[default(true)] pub overhang: bool, @@ -248,7 +226,6 @@ pub struct TextNode { /// #set text(top-edge: "cap-height") /// #rect(fill: aqua)[Typst] /// ``` - #[settable] #[default(TextEdge::Metric(VerticalFontMetric::CapHeight))] pub top_edge: TextEdge, @@ -265,7 +242,6 @@ pub struct TextNode { /// #set text(bottom-edge: "descender") /// #rect(fill: aqua)[Typst] /// ``` - #[settable] #[default(TextEdge::Metric(VerticalFontMetric::Baseline))] pub bottom_edge: TextEdge, @@ -286,15 +262,12 @@ pub struct TextNode { /// = Einleitung /// In diesem Dokument, ... /// ``` - #[settable] #[default(Lang::ENGLISH)] pub lang: Lang, /// An [ISO 3166-1 alpha-2 region code.](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) /// /// This lets the text processing pipeline make more informed choices. - #[settable] - #[default(None)] pub region: Option<Region>, /// The dominant direction for text and inline objects. Possible values are: @@ -321,9 +294,7 @@ pub struct TextNode { /// #set text(dir: rtl) /// هذا عربي. /// ``` - #[settable] #[resolve] - #[default(HorizontalDir(Smart::Auto))] pub dir: HorizontalDir, /// Whether to hyphenate text to improve line breaking. When `{auto}`, text @@ -343,9 +314,7 @@ pub struct TextNode { /// enabling hyphenation can /// improve justification. /// ``` - #[settable] #[resolve] - #[default(Hyphenate(Smart::Auto))] pub hyphenate: Hyphenate, /// Whether to apply kerning. @@ -363,7 +332,6 @@ pub struct TextNode { /// #set text(kerning: false) /// Totally /// ``` - #[settable] #[default(true)] pub kerning: bool, @@ -380,7 +348,6 @@ pub struct TextNode { /// #set text(alternates: true) /// 0, a, g, ß /// ``` - #[settable] #[default(false)] pub alternates: bool, @@ -389,8 +356,6 @@ pub struct TextNode { /// you need to consult your font to know which sets are available. When set /// to an integer between `{1}` and `{20}`, enables the corresponding /// OpenType font feature from `ss01`, ..., `ss20`. - #[settable] - #[default(None)] pub stylistic_set: Option<StylisticSet>, /// Whether standard ligatures are active. @@ -407,19 +372,16 @@ pub struct TextNode { /// #set text(ligatures: false) /// A fine ligature. /// ``` - #[settable] #[default(true)] pub ligatures: bool, /// Whether ligatures that should be used sparingly are active. Setting this /// to `{true}` enables the OpenType `dlig` font feature. - #[settable] #[default(false)] pub discretionary_ligatures: bool, /// Whether historical ligatures are active. Setting this to `{true}` /// enables the OpenType `hlig` font feature. - #[settable] #[default(false)] pub historical_ligatures: bool, @@ -434,8 +396,6 @@ pub struct TextNode { /// #set text(number-type: "old-style") /// Number 9. /// ``` - #[settable] - #[default(Smart::Auto)] pub number_type: Smart<NumberType>, /// The width of numbers / figures. When set to `{auto}`, the default @@ -451,8 +411,6 @@ pub struct TextNode { /// A 12 B 34. \ /// A 56 B 78. /// ``` - #[settable] - #[default(Smart::Auto)] pub number_width: Smart<NumberWidth>, /// Whether to have a slash through the zero glyph. Setting this to `{true}` @@ -461,7 +419,6 @@ pub struct TextNode { /// ```example /// 0, #text(slashed-zero: true)[0] /// ``` - #[settable] #[default(false)] pub slashed_zero: bool, @@ -472,7 +429,6 @@ pub struct TextNode { /// 1/2 \ /// #text(fractions: true)[1/2] /// ``` - #[settable] #[default(false)] pub fractions: bool, @@ -488,43 +444,39 @@ pub struct TextNode { /// #set text(features: ("frac",)) /// 1/2 /// ``` - #[settable] #[fold] - #[default(FontFeatures(vec![]))] pub features: FontFeatures, + /// The text. + #[internal] + #[positional] + #[required] + pub text: EcoString, + /// A delta to apply on the font weight. - #[settable] + #[internal] #[fold] - #[skip] - #[default(0)] pub delta: Delta, /// Whether the font style should be inverted. - #[settable] + #[internal] #[fold] - #[skip] #[default(false)] pub emph: Toggle, + /// Decorative lines. + #[internal] + #[fold] + pub deco: Decoration, + /// A case transformation that should be applied to the text. - #[settable] - #[skip] - #[default(None)] + #[internal] pub case: Option<Case>, /// Whether small capital glyphs should be used. ("smcp") - #[settable] - #[skip] + #[internal] #[default(false)] pub smallcaps: bool, - - /// Decorative lines. - #[settable] - #[fold] - #[skip] - #[default(vec![])] - pub deco: Decoration, } impl TextNode { @@ -539,7 +491,9 @@ impl Construct for TextNode { // The text constructor is special: It doesn't create a text node. // Instead, it leaves the passed argument structurally unchanged, but // styles all text in it. - args.expect("body") + let styles = Self::set(args)?; + let body = args.expect::<Content>("body")?; + Ok(body.styled_with_map(styles)) } } @@ -651,7 +605,7 @@ cast_to_value! { } /// The direction of text and inline objects in their line. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct HorizontalDir(pub Smart<Dir>); cast_from_value! { @@ -680,7 +634,7 @@ impl Resolve for HorizontalDir { } /// Whether to hyphenate text. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct Hyphenate(pub Smart<bool>); cast_from_value! { diff --git a/library/src/text/quotes.rs b/library/src/text/quotes.rs index 1c602871..863cc3bd 100644 --- a/library/src/text/quotes.rs +++ b/library/src/text/quotes.rs @@ -27,7 +27,6 @@ use crate::prelude::*; #[node] pub struct SmartQuoteNode { /// Whether this should be a double quote. - #[named] #[default(true)] pub double: bool, @@ -41,7 +40,6 @@ pub struct SmartQuoteNode { /// /// These are "dumb" quotes. /// ``` - #[settable] #[default(true)] pub enabled: bool, } diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index 8526a278..36a0fc78 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -87,7 +87,6 @@ pub struct RawNode { /// rg "Hello World" /// ``` /// ```` - #[named] #[default(false)] pub block: bool, @@ -102,8 +101,6 @@ pub struct RawNode { /// This is *Typst!* /// ``` /// ```` - #[settable] - #[default] pub lang: Option<EcoString>, } @@ -114,7 +111,7 @@ impl Prepare for RawNode { mut this: Content, styles: StyleChain, ) -> SourceResult<Content> { - this.push_field("lang", Self::lang_in(styles).clone()); + this.push_field("lang", self.lang(styles).clone()); Ok(this) } } @@ -122,7 +119,7 @@ impl Prepare for RawNode { impl Show for RawNode { fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { let text = self.text(); - let lang = Self::lang_in(styles).as_ref().map(|s| s.to_lowercase()); + let lang = self.lang(styles).as_ref().map(|s| s.to_lowercase()); let foreground = THEME .settings .foreground @@ -170,7 +167,7 @@ impl Show for RawNode { TextNode::packed(text) }; - if self.block() { + if self.block(styles) { realized = BlockNode::new().with_body(Some(realized)).pack(); } @@ -179,7 +176,7 @@ impl Show for RawNode { } impl Finalize for RawNode { - fn finalize(&self, realized: Content) -> Content { + fn finalize(&self, realized: Content, _: StyleChain) -> Content { let mut map = StyleMap::new(); map.set(TextNode::set_overhang(false)); map.set(TextNode::set_hyphenate(Hyphenate(Smart::Custom(false)))); diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index 98718365..ccdb0197 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -16,11 +16,6 @@ use crate::prelude::*; /// Category: text #[node(Show)] pub struct SubNode { - /// The text to display in subscript. - #[positional] - #[required] - pub body: Content, - /// Whether to prefer the dedicated subscript characters of the font. /// /// If this is enabled, Typst first tries to transform the text to subscript @@ -31,23 +26,25 @@ pub struct SubNode { /// N#sub(typographic: true)[1] /// N#sub(typographic: false)[1] /// ``` - #[settable] #[default(true)] pub typographic: bool, /// The baseline shift for synthetic subscripts. Does not apply if /// `typographic` is true and the font has subscript codepoints for the /// given `body`. - #[settable] #[default(Em::new(0.2).into())] pub baseline: Length, /// The font size for synthetic subscripts. Does not apply if /// `typographic` is true and the font has subscript codepoints for the /// given `body`. - #[settable] #[default(TextSize(Em::new(0.6).into()))] pub size: TextSize, + + /// The text to display in subscript. + #[positional] + #[required] + pub body: Content, } impl Show for SubNode { @@ -59,7 +56,7 @@ impl Show for SubNode { ) -> SourceResult<Content> { let body = self.body(); let mut transformed = None; - if Self::typographic_in(styles) { + if self.typographic(styles) { if let Some(text) = search_text(&body, true) { if is_shapable(vt, &text, styles) { transformed = Some(TextNode::packed(text)); @@ -68,8 +65,8 @@ impl Show for SubNode { }; Ok(transformed.unwrap_or_else(|| { - body.styled(TextNode::set_baseline(Self::baseline_in(styles))) - .styled(TextNode::set_size(Self::size_in(styles))) + body.styled(TextNode::set_baseline(self.baseline(styles))) + .styled(TextNode::set_size(self.size(styles))) })) } } @@ -87,11 +84,6 @@ impl Show for SubNode { /// Category: text #[node(Show)] pub struct SuperNode { - /// The text to display in superscript. - #[positional] - #[required] - pub body: Content, - /// Whether to prefer the dedicated superscript characters of the font. /// /// If this is enabled, Typst first tries to transform the text to @@ -102,23 +94,25 @@ pub struct SuperNode { /// N#super(typographic: true)[1] /// N#super(typographic: false)[1] /// ``` - #[settable] #[default(true)] pub typographic: bool, /// The baseline shift for synthetic superscripts. Does not apply if /// `typographic` is true and the font has superscript codepoints for the /// given `body`. - #[settable] #[default(Em::new(-0.5).into())] pub baseline: Length, /// The font size for synthetic superscripts. Does not apply if /// `typographic` is true and the font has superscript codepoints for the /// given `body`. - #[settable] #[default(TextSize(Em::new(0.6).into()))] pub size: TextSize, + + /// The text to display in superscript. + #[positional] + #[required] + pub body: Content, } impl Show for SuperNode { @@ -130,7 +124,7 @@ impl Show for SuperNode { ) -> SourceResult<Content> { let body = self.body(); let mut transformed = None; - if Self::typographic_in(styles) { + if self.typographic(styles) { if let Some(text) = search_text(&body, false) { if is_shapable(vt, &text, styles) { transformed = Some(TextNode::packed(text)); @@ -139,8 +133,8 @@ impl Show for SuperNode { }; Ok(transformed.unwrap_or_else(|| { - body.styled(TextNode::set_baseline(Self::baseline_in(styles))) - .styled(TextNode::set_size(Self::size_in(styles))) + body.styled(TextNode::set_baseline(self.baseline(styles))) + .styled(TextNode::set_size(self.size(styles))) })) } } |
