Skip to content

Commit

Permalink
Change smallcaps into an element function (#3981)
Browse files Browse the repository at this point in the history
Co-authored-by: Laurenz <laurmaedje@gmail.com>
  • Loading branch information
Coekjan and laurmaedje committed May 6, 2024
1 parent 0613194 commit 556979c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/typst/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}

Expand Down
42 changes: 29 additions & 13 deletions crates/typst/src/text/smallcaps.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 added tests/ref/smallcaps-show-rule.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/suite/text/smallcaps.typ
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 556979c

Please sign in to comment.