mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Change smallcaps
into an element function (#3981)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
061319425b
commit
556979c83b
@ -66,10 +66,10 @@ pub(super) fn define(global: &mut Scope) {
|
|||||||
global.define_elem::<OverlineElem>();
|
global.define_elem::<OverlineElem>();
|
||||||
global.define_elem::<StrikeElem>();
|
global.define_elem::<StrikeElem>();
|
||||||
global.define_elem::<HighlightElem>();
|
global.define_elem::<HighlightElem>();
|
||||||
|
global.define_elem::<SmallcapsElem>();
|
||||||
global.define_elem::<RawElem>();
|
global.define_elem::<RawElem>();
|
||||||
global.define_func::<lower>();
|
global.define_func::<lower>();
|
||||||
global.define_func::<upper>();
|
global.define_func::<upper>();
|
||||||
global.define_func::<smallcaps>();
|
|
||||||
global.define_func::<lorem>();
|
global.define_func::<lorem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
use crate::text::TextElem;
|
||||||
|
|
||||||
/// Displays text in small capitals.
|
/// 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
|
||||||
/// ```example
|
/// ```example
|
||||||
/// #set par(justify: true)
|
/// #set par(justify: true)
|
||||||
@ -23,10 +19,30 @@ use crate::text::TextElem;
|
|||||||
/// = Introduction
|
/// = Introduction
|
||||||
/// #lorem(40)
|
/// #lorem(40)
|
||||||
/// ```
|
/// ```
|
||||||
#[func(title = "Small Capitals")]
|
///
|
||||||
pub fn smallcaps(
|
/// # Smallcaps fonts
|
||||||
/// The text to display to small capitals.
|
/// By default, this enables the OpenType `smcp` feature for the font. Not all
|
||||||
body: Content,
|
/// fonts support this feature. Sometimes smallcaps are part of a dedicated
|
||||||
) -> Content {
|
/// font. This is, for example, the case for the _Latin Modern_ family of fonts.
|
||||||
body.styled(TextElem::set_smallcaps(true))
|
/// 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)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
BIN
tests/ref/smallcaps-show-rule.png
Normal file
BIN
tests/ref/smallcaps-show-rule.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
@ -1,3 +1,12 @@
|
|||||||
--- smallcaps ---
|
--- smallcaps ---
|
||||||
// Test smallcaps.
|
// Test smallcaps.
|
||||||
#smallcaps[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]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user