Move smallcaps into separate function

This commit is contained in:
Laurenz 2022-05-04 12:49:46 +02:00
parent 507c5fc925
commit ba5622b7b9
7 changed files with 38 additions and 31 deletions

View File

@ -30,6 +30,9 @@ pub fn new() -> Scope {
std.def_node::<text::OverlineNode>("overline"); std.def_node::<text::OverlineNode>("overline");
std.def_node::<text::LinkNode>("link"); std.def_node::<text::LinkNode>("link");
std.def_node::<text::RepeatNode>("repeat"); std.def_node::<text::RepeatNode>("repeat");
std.def_fn("lower", text::lower);
std.def_fn("upper", text::upper);
std.def_fn("smallcaps", text::smallcaps);
// Structure. // Structure.
std.def_node::<structure::HeadingNode>("heading"); std.def_node::<structure::HeadingNode>("heading");
@ -67,7 +70,7 @@ pub fn new() -> Scope {
// Math. // Math.
std.def_node::<math::MathNode>("math"); std.def_node::<math::MathNode>("math");
// Utility functions. // Utility.
std.def_fn("type", utility::type_); std.def_fn("type", utility::type_);
std.def_fn("assert", utility::assert); std.def_fn("assert", utility::assert);
std.def_fn("int", utility::int); std.def_fn("int", utility::int);
@ -84,8 +87,6 @@ pub fn new() -> Scope {
std.def_fn("repr", utility::repr); std.def_fn("repr", utility::repr);
std.def_fn("str", utility::str); std.def_fn("str", utility::str);
std.def_fn("regex", utility::regex); std.def_fn("regex", utility::regex);
std.def_fn("lower", utility::lower);
std.def_fn("upper", utility::upper);
std.def_fn("letter", utility::letter); std.def_fn("letter", utility::letter);
std.def_fn("roman", utility::roman); std.def_fn("roman", utility::roman);
std.def_fn("symbol", utility::symbol); std.def_fn("symbol", utility::symbol);

View File

@ -84,8 +84,6 @@ impl TextNode {
/// Whether to apply kerning ("kern"). /// Whether to apply kerning ("kern").
pub const KERNING: bool = true; pub const KERNING: bool = true;
/// Whether small capital glyphs should be used. ("smcp")
pub const SMALLCAPS: bool = false;
/// Whether to apply stylistic alternates. ("salt") /// Whether to apply stylistic alternates. ("salt")
pub const ALTERNATES: bool = false; pub const ALTERNATES: bool = false;
/// Which stylistic set to apply. ("ss01" - "ss20") /// Which stylistic set to apply. ("ss01" - "ss20")
@ -119,6 +117,9 @@ impl TextNode {
/// A case transformation that should be applied to the text. /// A case transformation that should be applied to the text.
#[property(hidden)] #[property(hidden)]
pub const CASE: Option<Case> = None; pub const CASE: Option<Case> = None;
/// Whether small capital glyphs should be used. ("smcp")
#[property(hidden)]
pub const SMALLCAPS: bool = false;
/// An URL the text should link to. /// An URL the text should link to.
#[property(hidden, referenced)] #[property(hidden, referenced)]
pub const LINK: Option<EcoString> = None; pub const LINK: Option<EcoString> = None;
@ -411,6 +412,26 @@ impl Fold for Vec<(Tag, u32)> {
} }
} }
/// Convert text to lowercase.
pub fn lower(_: &mut Context, args: &mut Args) -> TypResult<Value> {
case(Case::Lower, args)
}
/// Convert text to uppercase.
pub fn upper(_: &mut Context, args: &mut Args) -> TypResult<Value> {
case(Case::Upper, args)
}
/// Change the case of text.
fn case(case: Case, args: &mut Args) -> TypResult<Value> {
let Spanned { v, span } = args.expect("string or content")?;
Ok(match v {
Value::Str(v) => Value::Str(case.apply(&v).into()),
Value::Content(v) => Value::Content(v.styled(TextNode::CASE, Some(case))),
v => bail!(span, "expected string or content, found {}", v.type_name()),
})
}
/// A case transformation on text. /// A case transformation on text.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Case { pub enum Case {
@ -430,6 +451,12 @@ impl Case {
} }
} }
/// Display text in small capitals.
pub fn smallcaps(_: &mut Context, args: &mut Args) -> TypResult<Value> {
let body: Content = args.expect("content")?;
Ok(Value::Content(body.styled(TextNode::SMALLCAPS, true)))
}
/// A toggle that turns on and off alternatingly if folded. /// A toggle that turns on and off alternatingly if folded.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Toggle; pub struct Toggle;

View File

@ -2,7 +2,6 @@ use lipsum::lipsum_from_seed;
use crate::eval::Regex; use crate::eval::Regex;
use crate::library::prelude::*; use crate::library::prelude::*;
use crate::library::text::{Case, TextNode};
/// The string representation of a value. /// The string representation of a value.
pub fn repr(_: &mut Context, args: &mut Args) -> TypResult<Value> { pub fn repr(_: &mut Context, args: &mut Args) -> TypResult<Value> {
@ -20,26 +19,6 @@ pub fn str(_: &mut Context, args: &mut Args) -> TypResult<Value> {
})) }))
} }
/// Convert a string to lowercase.
pub fn lower(_: &mut Context, args: &mut Args) -> TypResult<Value> {
case(Case::Lower, args)
}
/// Convert a string to uppercase.
pub fn upper(_: &mut Context, args: &mut Args) -> TypResult<Value> {
case(Case::Upper, args)
}
/// Change the case of a string or content.
fn case(case: Case, args: &mut Args) -> TypResult<Value> {
let Spanned { v, span } = args.expect("string or content")?;
Ok(match v {
Value::Str(v) => Value::Str(case.apply(&v).into()),
Value::Content(v) => Value::Content(v.styled(TextNode::CASE, Some(case))),
v => bail!(span, "expected string or content, found {}", v.type_name()),
})
}
/// Create blind text. /// Create blind text.
pub fn lorem(_: &mut Context, args: &mut Args) -> TypResult<Value> { pub fn lorem(_: &mut Context, args: &mut Args) -> TypResult<Value> {
let words: usize = args.expect("number of words")?; let words: usize = args.expect("number of words")?;

View File

@ -24,5 +24,5 @@
#set page("a4") #set page("a4")
#set page("a5") #set page("a5")
#set page("a11", flipped: true, fill: eastern) #set page("a11", flipped: true, fill: eastern)
#set text("Roboto", white, smallcaps: true) #set text("Roboto", white)
Typst #smallcaps[Typst]

View File

@ -23,7 +23,7 @@
--- ---
// Test page fill. // Test page fill.
#set page(width: 80pt, height: 40pt, fill: eastern) #set page(width: 80pt, height: 40pt, fill: eastern)
#text(15pt, "Roboto", fill: white, smallcaps: true)[Typst] #text(15pt, "Roboto", fill: white, smallcaps[Typst])
#page(width: 40pt, fill: none, margins: (top: 10pt, rest: auto))[Hi] #page(width: 40pt, fill: none, margins: (top: 10pt, rest: auto))[Hi]
--- ---

View File

@ -3,7 +3,7 @@
--- ---
// Test classic example. // Test classic example.
#set text("Roboto") #set text("Roboto")
#show phrase: "Der Spiegel" as text(smallcaps: true, [#phrase]) #show phrase: "Der Spiegel" as smallcaps[#phrase]
Die Zeitung Der Spiegel existiert. Die Zeitung Der Spiegel existiert.
--- ---

View File

@ -8,7 +8,7 @@
--- ---
// Test smallcaps. // Test smallcaps.
#set text("Roboto") #set text("Roboto")
#text(smallcaps: true)[Smallcaps] #smallcaps[Smallcaps]
--- ---
// Test alternates and stylistic sets. // Test alternates and stylistic sets.