summaryrefslogtreecommitdiff
path: root/library/src/text
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-15 22:51:55 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-15 23:11:20 +0100
commitb6202b646a0d5ecced301d9bac8bfcaf977d7ee4 (patch)
tree7d42cb50f9e66153e7e8b2217009684e25f54f42 /library/src/text
parentf3980c704544a464f9729cc8bc9f97d3a7454769 (diff)
Reflection for castables
Diffstat (limited to 'library/src/text')
-rw-r--r--library/src/text/deco.rs2
-rw-r--r--library/src/text/misc.rs17
-rw-r--r--library/src/text/mod.rs93
-rw-r--r--library/src/text/quotes.rs2
-rw-r--r--library/src/text/raw.rs2
-rw-r--r--library/src/text/shift.rs2
-rw-r--r--library/src/text/symbol.rs2
7 files changed, 70 insertions, 50 deletions
diff --git a/library/src/text/deco.rs b/library/src/text/deco.rs
index fceb4cfd..86866715 100644
--- a/library/src/text/deco.rs
+++ b/library/src/text/deco.rs
@@ -5,6 +5,8 @@ use super::TextNode;
use crate::prelude::*;
/// Typeset underline, stricken-through or overlined text.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]
diff --git a/library/src/text/misc.rs b/library/src/text/misc.rs
index 1c5a32b4..fc4f7d73 100644
--- a/library/src/text/misc.rs
+++ b/library/src/text/misc.rs
@@ -2,6 +2,8 @@ use super::TextNode;
use crate::prelude::*;
/// A text space.
+///
+/// Tags: text.
#[func]
#[capable(Unlabellable, Behave)]
#[derive(Debug, Hash)]
@@ -23,6 +25,8 @@ impl Behave for SpaceNode {
}
/// A line break.
+///
+/// Tags: text.
#[func]
#[capable(Behave)]
#[derive(Debug, Hash)]
@@ -45,6 +49,8 @@ impl Behave for LinebreakNode {
}
/// Strongly emphasizes content by increasing the font weight.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]
@@ -79,8 +85,7 @@ pub struct Delta(pub i64);
castable! {
Delta,
- Expected: "integer",
- Value::Int(delta) => Self(delta),
+ v: i64 => Self(v),
}
impl Fold for Delta {
@@ -92,6 +97,8 @@ impl Fold for Delta {
}
/// Emphasizes content by flipping the italicness.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]
@@ -130,12 +137,16 @@ impl Fold for Toggle {
}
/// Convert a string or content to lowercase.
+///
+/// Tags: text.
#[func]
pub fn lower(args: &mut Args) -> SourceResult<Value> {
case(Case::Lower, args)
}
/// Convert a string or content to uppercase.
+///
+/// Tags: text.
#[func]
pub fn upper(args: &mut Args) -> SourceResult<Value> {
case(Case::Upper, args)
@@ -171,6 +182,8 @@ impl Case {
}
/// Display text in small capitals.
+///
+/// Tags: text.
#[func]
pub fn smallcaps(args: &mut Args) -> SourceResult<Value> {
let body: Content = args.expect("content")?;
diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs
index d09d8f28..7340e5de 100644
--- a/library/src/text/mod.rs
+++ b/library/src/text/mod.rs
@@ -26,6 +26,8 @@ use crate::layout::ParNode;
use crate::prelude::*;
/// A single run of text with the same style.
+///
+/// Tags: text.
#[func]
#[capable]
#[derive(Clone, Hash)]
@@ -206,8 +208,7 @@ impl Debug for FontFamily {
castable! {
FontFamily,
- Expected: "string",
- Value::Str(string) => Self::new(&string),
+ string: EcoString => Self::new(&string),
}
/// Font family fallback list.
@@ -216,12 +217,10 @@ pub struct FallbackList(pub Vec<FontFamily>);
castable! {
FallbackList,
- Expected: "string or array of strings",
- Value::Str(string) => Self(vec![FontFamily::new(&string)]),
- Value::Array(values) => Self(values
+ family: FontFamily => Self(vec![family]),
+ values: Array => Self(values
.into_iter()
.filter_map(|v| v.cast().ok())
- .map(|string: EcoString| FontFamily::new(&string))
.collect()),
}
@@ -237,7 +236,10 @@ impl Fold for TextSize {
}
}
-castable!(TextSize: Length);
+castable! {
+ TextSize,
+ v: Length => Self(v),
+}
/// Specifies the bottom or top edge of text.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
@@ -260,16 +262,17 @@ impl TextEdge {
castable! {
TextEdge,
- Expected: "string or length",
- Value::Length(v) => Self::Length(v),
- Value::Str(string) => Self::Metric(match string.as_str() {
- "ascender" => VerticalFontMetric::Ascender,
- "cap-height" => VerticalFontMetric::CapHeight,
- "x-height" => VerticalFontMetric::XHeight,
- "baseline" => VerticalFontMetric::Baseline,
- "descender" => VerticalFontMetric::Descender,
- _ => Err("unknown font metric")?,
- }),
+ v: Length => Self::Length(v),
+ /// The distance from the baseline to the ascender.
+ "ascender" => Self::Metric(VerticalFontMetric::Ascender),
+ /// The approximate height of uppercase letters.
+ "cap-height" => Self::Metric(VerticalFontMetric::CapHeight),
+ /// The approximate height of non-ascending lowercase letters.
+ "x-height" => Self::Metric(VerticalFontMetric::XHeight),
+ /// The baseline on which the letters rest.
+ "baseline" => Self::Metric(VerticalFontMetric::Baseline),
+ /// The distance from the baseline to the descender.
+ "descender" => Self::Metric(VerticalFontMetric::Descender),
}
/// The direction of text and inline objects in their line.
@@ -278,10 +281,9 @@ pub struct HorizontalDir(pub Smart<Dir>);
castable! {
HorizontalDir,
- Expected: "direction or auto",
- Value::Auto => Self(Smart::Auto),
- @dir: Dir => match dir.axis() {
- Axis::X => Self(Smart::Custom(*dir)),
+ _: AutoValue => Self(Smart::Auto),
+ dir: Dir => match dir.axis() {
+ Axis::X => Self(Smart::Custom(dir)),
Axis::Y => Err("must be horizontal")?,
},
}
@@ -303,9 +305,8 @@ pub struct Hyphenate(pub Smart<bool>);
castable! {
Hyphenate,
- Expected: "boolean or auto",
- Value::Auto => Self(Smart::Auto),
- Value::Bool(v) => Self(Smart::Custom(v)),
+ _: AutoValue => Self(Smart::Auto),
+ v: bool => Self(Smart::Custom(v)),
}
impl Resolve for Hyphenate {
@@ -337,8 +338,7 @@ impl StylisticSet {
castable! {
StylisticSet,
- Expected: "integer",
- Value::Int(v) => match v {
+ v: i64 => match v {
1 ..= 20 => Self::new(v as u8),
_ => Err("must be between 1 and 20")?,
},
@@ -355,12 +355,10 @@ pub enum NumberType {
castable! {
NumberType,
- Expected: "string",
- Value::Str(string) => match string.as_str() {
- "lining" => Self::Lining,
- "old-style" => Self::OldStyle,
- _ => Err(r#"expected "lining" or "old-style""#)?,
- },
+ /// Numbers that fit well with capital text.
+ "lining" => Self::Lining,
+ /// Numbers that fit well into a flow of upper- and lowercase text.
+ "old-style" => Self::OldStyle,
}
/// The width of numbers / figures.
@@ -374,12 +372,10 @@ pub enum NumberWidth {
castable! {
NumberWidth,
- Expected: "string",
- Value::Str(string) => match string.as_str() {
- "proportional" => Self::Proportional,
- "tabular" => Self::Tabular,
- _ => Err(r#"expected "proportional" or "tabular""#)?,
- },
+ /// Number widths are glyph specific.
+ "proportional" => Self::Proportional,
+ /// All numbers are of equal width / monospaced.
+ "tabular" => Self::Tabular,
}
/// OpenType font features settings.
@@ -388,20 +384,21 @@ pub struct FontFeatures(pub Vec<(Tag, u32)>);
castable! {
FontFeatures,
- Expected: "array of strings or dictionary mapping tags to integers",
- Value::Array(values) => Self(values
+ values: Array => Self(values
.into_iter()
- .filter_map(|v| v.cast().ok())
- .map(|string: EcoString| (Tag::from_bytes_lossy(string.as_bytes()), 1))
- .collect()),
- Value::Dict(values) => Self(values
+ .map(|v| {
+ let tag = v.cast::<EcoString>()?;
+ Ok((Tag::from_bytes_lossy(tag.as_bytes()), 1))
+ })
+ .collect::<StrResult<_>>()?),
+ values: Dict => Self(values
.into_iter()
- .filter_map(|(k, v)| {
+ .map(|(k, v)| {
+ let num = v.cast::<u32>()?;
let tag = Tag::from_bytes_lossy(k.as_bytes());
- let num = v.cast::<i64>().ok()?.try_into().ok()?;
- Some((tag, num))
+ Ok((tag, num))
})
- .collect()),
+ .collect::<StrResult<_>>()?),
}
impl Fold for FontFeatures {
diff --git a/library/src/text/quotes.rs b/library/src/text/quotes.rs
index 0f678de3..ab6f166c 100644
--- a/library/src/text/quotes.rs
+++ b/library/src/text/quotes.rs
@@ -3,6 +3,8 @@ use typst::syntax::is_newline;
use crate::prelude::*;
/// A smart quote.
+///
+/// Tags: text.
#[func]
#[capable]
#[derive(Debug, Hash)]
diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs
index 4ad70654..125a5da1 100644
--- a/library/src/text/raw.rs
+++ b/library/src/text/raw.rs
@@ -7,6 +7,8 @@ use crate::layout::BlockNode;
use crate::prelude::*;
/// Raw text with optional syntax highlighting.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]
diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs
index 65adc027..ad4a6cd9 100644
--- a/library/src/text/shift.rs
+++ b/library/src/text/shift.rs
@@ -10,6 +10,8 @@ use crate::prelude::*;
/// typography possible, we first try to transform the text to superscript
/// codepoints. If that fails, we fall back to rendering shrunk normal letters
/// in a raised way.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]
diff --git a/library/src/text/symbol.rs b/library/src/text/symbol.rs
index fc746eb2..eece81ab 100644
--- a/library/src/text/symbol.rs
+++ b/library/src/text/symbol.rs
@@ -2,6 +2,8 @@ use crate::prelude::*;
use crate::text::TextNode;
/// A symbol identified by symmie notation.
+///
+/// Tags: text.
#[func]
#[capable(Show)]
#[derive(Debug, Hash)]