Change smallcaps into an element function (#3981)

Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
Yip Coekjan 2024-05-06 20:57:16 +08:00 committed by GitHub
parent 061319425b
commit 556979c83b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 14 deletions

View File

@ -66,10 +66,10 @@ pub(super) fn define(global: &mut Scope) {
global.define_elem::<OverlineElem>();
global.define_elem::<StrikeElem>();
global.define_elem::<HighlightElem>();
global.define_elem::<SmallcapsElem>();
global.define_elem::<RawElem>();
global.define_func::<lower>();
global.define_func::<upper>();
global.define_func::<smallcaps>();
global.define_func::<lorem>();
}

View File

@ -1,14 +1,10 @@
use crate::foundations::{func, Content};
use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{elem, Content, Packed, Show, StyleChain};
use crate::text::TextElem;
/// Displays text in small capitals.
///
/// _Note:_ This enables the OpenType `smcp` feature for the font. Not all fonts
/// support this feature. Sometimes smallcaps are part of a dedicated font and
/// sometimes they are not available at all. In the future, this function will
/// support selecting a dedicated smallcaps font as well as synthesizing
/// smallcaps from normal letters, but this is not yet implemented.
///
/// # Example
/// ```example
/// #set par(justify: true)
@ -23,10 +19,30 @@ use crate::text::TextElem;
/// = Introduction
/// #lorem(40)
/// ```
#[func(title = "Small Capitals")]
pub fn smallcaps(
/// The text to display to small capitals.
body: Content,
) -> Content {
body.styled(TextElem::set_smallcaps(true))
///
/// # Smallcaps fonts
/// By default, this enables the OpenType `smcp` feature for the font. Not all
/// fonts support this feature. Sometimes smallcaps are part of a dedicated
/// font. This is, for example, the case for the _Latin Modern_ family of fonts.
/// In those cases, you can use a show-set rule to customize the appearance of
/// the text in smallcaps:
///
/// ```typ
/// #show smallcaps: set text(font: "Latin Modern Roman Caps")
/// ```
///
/// In the future, this function will support synthesizing smallcaps from normal
/// letters, but this is not yet implemented.
#[elem(title = "Small Capitals", Show)]
pub struct SmallcapsElem {
/// The content to display in small capitals.
#[required]
pub body: Content,
}
impl Show for Packed<SmallcapsElem> {
#[typst_macros::time(name = "smallcaps", span = self.span())]
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
Ok(self.body().clone().styled(TextElem::set_smallcaps(true)))
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -1,3 +1,12 @@
--- smallcaps ---
// Test smallcaps.
#smallcaps[Smallcaps]
--- smallcaps-show-rule ---
// There is no dedicated smallcaps font in typst-dev-assets, so we just use some
// other font to test this show rule.
#show smallcaps: set text(font: "PT Sans")
#smallcaps[Smallcaps]
#show smallcaps: set text(fill: red)
#smallcaps[Smallcaps]