summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/text
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-09-11 14:40:22 +0200
committerLaurenz <laurmaedje@gmail.com>2023-09-11 14:40:22 +0200
commitb471ac7d590abd2398ce25193b4e4df373bf2e9c (patch)
treeb5f7a6fdc807ee3340a4f42b0ad3cc563fe45429 /crates/typst-library/src/text
parent8f36fca68447a5d42a3d54b5fac7e5546ee244be (diff)
First-class types
Makes types first-class values.
Diffstat (limited to 'crates/typst-library/src/text')
-rw-r--r--crates/typst-library/src/text/deco.rs66
-rw-r--r--crates/typst-library/src/text/misc.rs68
-rw-r--r--crates/typst-library/src/text/mod.rs68
-rw-r--r--crates/typst-library/src/text/quotes.rs13
-rw-r--r--crates/typst-library/src/text/raw.rs35
-rw-r--r--crates/typst-library/src/text/shift.rs14
6 files changed, 110 insertions, 154 deletions
diff --git a/crates/typst-library/src/text/deco.rs b/crates/typst-library/src/text/deco.rs
index c97ef325..e34bf363 100644
--- a/crates/typst-library/src/text/deco.rs
+++ b/crates/typst-library/src/text/deco.rs
@@ -6,19 +6,15 @@ use crate::prelude::*;
/// Underlines text.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// This is #underline[important].
/// ```
-///
-/// Display: Underline
-/// Category: text
-#[element(Show)]
+#[elem(Show)]
pub struct UnderlineElem {
- /// How to stroke the line.
+ /// How to [stroke]($stroke) the line.
///
- /// See the [line's documentation]($func/line.stroke) for more details. If
- /// set to `{auto}`, takes on the text's color and a thickness defined in
+ /// If set to `{auto}`, takes on the text's color and a thickness defined in
/// the current font.
///
/// ```example
@@ -30,7 +26,7 @@ pub struct UnderlineElem {
/// ```
#[resolve]
#[fold]
- pub stroke: Smart<PartialStroke>,
+ pub stroke: Smart<Stroke>,
/// The position of the line relative to the baseline, read from the font
/// tables if `{auto}`.
@@ -85,19 +81,15 @@ impl Show for UnderlineElem {
/// Adds a line over text.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// #overline[A line over text.]
/// ```
-///
-/// Display: Overline
-/// Category: text
-#[element(Show)]
+#[elem(Show)]
pub struct OverlineElem {
- /// How to stroke the line.
+ /// How to [stroke]($stroke) the line.
///
- /// See the [line's documentation]($func/line.stroke) for more details. If
- /// set to `{auto}`, takes on the text's color and a thickness defined in
+ /// If set to `{auto}`, takes on the text's color and a thickness defined in
/// the current font.
///
/// ```example
@@ -110,7 +102,7 @@ pub struct OverlineElem {
/// ```
#[resolve]
#[fold]
- pub stroke: Smart<PartialStroke>,
+ pub stroke: Smart<Stroke>,
/// The position of the line relative to the baseline. Read from the font
/// tables if `{auto}`.
@@ -170,23 +162,19 @@ impl Show for OverlineElem {
/// Strikes through text.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// This is #strike[not] relevant.
/// ```
-///
-/// Display: Strikethrough
-/// Category: text
-#[element(Show)]
+#[elem(title = "Strikethrough", Show)]
pub struct StrikeElem {
- /// How to stroke the line.
+ /// How to [stroke]($stroke) the line.
///
- /// See the [line's documentation]($func/line.stroke) for more details. If
- /// set to `{auto}`, takes on the text's color and a thickness defined in
+ /// If set to `{auto}`, takes on the text's color and a thickness defined in
/// the current font.
///
- /// _Note:_ Please don't use this for real redaction as you can still
- /// copy paste the text.
+ /// _Note:_ Please don't use this for real redaction as you can still copy
+ /// paste the text.
///
/// ```example
/// This is #strike(stroke: 1.5pt + red)[very stricken through]. \
@@ -194,7 +182,7 @@ pub struct StrikeElem {
/// ```
#[resolve]
#[fold]
- pub stroke: Smart<PartialStroke>,
+ pub stroke: Smart<Stroke>,
/// The position of the line relative to the baseline. Read from the font
/// tables if `{auto}`.
@@ -240,14 +228,11 @@ impl Show for StrikeElem {
/// Highlights text with a background color.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// This is #highlight[important].
/// ```
-///
-/// Display: Highlight
-/// Category: text
-#[element(Show)]
+#[elem(Show)]
pub struct HighlightElem {
/// The color to highlight the text with.
/// (Default: 0xffff5f)
@@ -316,6 +301,7 @@ impl Show for HighlightElem {
/// Defines a line-based decoration that is positioned over, under or on top of text,
/// or highlights the text with a background.
+#[ty]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Decoration {
line: DecoLine,
@@ -332,15 +318,15 @@ impl Fold for Decoration {
}
cast! {
- type Decoration: "decoration",
+ type Decoration,
}
/// A kind of decorative line.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
enum DecoLine {
- Underline { stroke: PartialStroke<Abs>, offset: Smart<Abs>, evade: bool },
- Strikethrough { stroke: PartialStroke<Abs>, offset: Smart<Abs> },
- Overline { stroke: PartialStroke<Abs>, offset: Smart<Abs>, evade: bool },
+ Underline { stroke: Stroke<Abs>, offset: Smart<Abs>, evade: bool },
+ Strikethrough { stroke: Stroke<Abs>, offset: Smart<Abs> },
+ Overline { stroke: Stroke<Abs>, offset: Smart<Abs>, evade: bool },
Highlight { fill: Paint, top_edge: TopEdge, bottom_edge: BottomEdge },
}
@@ -378,10 +364,10 @@ pub(super) fn decorate(
};
let offset = offset.unwrap_or(-metrics.position.at(text.size)) - shift;
- let stroke = stroke.clone().unwrap_or(Stroke {
+ let stroke = stroke.clone().unwrap_or(FixedStroke {
paint: text.fill.clone(),
thickness: metrics.thickness.at(text.size),
- ..Stroke::default()
+ ..FixedStroke::default()
});
let gap_padding = 0.08 * text.size;
diff --git a/crates/typst-library/src/text/misc.rs b/crates/typst-library/src/text/misc.rs
index 811b027e..73657345 100644
--- a/crates/typst-library/src/text/misc.rs
+++ b/crates/typst-library/src/text/misc.rs
@@ -2,10 +2,7 @@ use super::TextElem;
use crate::prelude::*;
/// A text space.
-///
-/// Display: Space
-/// Category: text
-#[element(Behave, Unlabellable, PlainText)]
+#[elem(Behave, Unlabellable, PlainText)]
pub struct SpaceElem {}
impl Behave for SpaceElem {
@@ -28,21 +25,18 @@ impl PlainText for SpaceElem {
/// end of a paragraph is ignored, but more than one creates additional empty
/// lines.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// *Date:* 26.12.2022 \
/// *Topic:* Infrastructure Test \
/// *Severity:* High \
/// ```
///
-/// ## Syntax { #syntax }
+/// # Syntax
/// This function also has dedicated syntax: To insert a line break, simply write
/// a backslash followed by whitespace. This always creates an unjustified
/// break.
-///
-/// Display: Line Break
-/// Category: text
-#[element(Behave)]
+#[elem(title = "Line Break", Behave)]
pub struct LinebreakElem {
/// Whether to justify the line before the break.
///
@@ -71,7 +65,7 @@ impl Behave for LinebreakElem {
///
/// Increases the current font weight by a given `delta`.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// This is *strong.* \
/// This is #strong[too.] \
@@ -80,15 +74,12 @@ impl Behave for LinebreakElem {
/// And this is *evermore.*
/// ```
///
-/// ## Syntax { #syntax }
+/// # Syntax
/// This function also has dedicated syntax: To strongly emphasize content,
/// simply enclose it in stars/asterisks (`*`). Note that this only works at
/// word boundaries. To strongly emphasize part of a word, you have to use the
/// function.
-///
-/// Display: Strong Emphasis
-/// Category: text
-#[element(Show)]
+#[elem(title = "Strong Emphasis", Show)]
pub struct StrongElem {
/// The delta to apply on the font weight.
///
@@ -131,12 +122,12 @@ impl Fold for Delta {
/// Emphasizes content by setting it in italics.
///
-/// - If the current [text style]($func/text.style) is `{"normal"}`,
-/// this turns it into `{"italic"}`.
-/// - If it is already `{"italic"}` or `{"oblique"}`,
-/// it turns it back to `{"normal"}`.
+/// - If the current [text style]($text.style) is `{"normal"}`, this turns it
+/// into `{"italic"}`.
+/// - If it is already `{"italic"}` or `{"oblique"}`, it turns it back to
+/// `{"normal"}`.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// This is _emphasized._ \
/// This is #emph[too.]
@@ -148,14 +139,11 @@ impl Fold for Delta {
/// This is _emphasized_ differently.
/// ```
///
-/// ## Syntax { #syntax }
+/// # Syntax
/// This function also has dedicated syntax: To emphasize content, simply
/// enclose it in underscores (`_`). Note that this only works at word
/// boundaries. To emphasize part of a word, you have to use the function.
-///
-/// Display: Emphasis
-/// Category: text
-#[element(Show)]
+#[elem(title = "Emphasis", Show)]
pub struct EmphElem {
/// The content to emphasize.
#[required]
@@ -189,16 +177,13 @@ impl Fold for Toggle {
/// Converts text or content to lowercase.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// #lower("ABC") \
/// #lower[*My Text*] \
/// #lower[already low]
/// ```
-///
-/// Display: Lowercase
-/// Category: text
-#[func]
+#[func(title = "Lowercase")]
pub fn lower(
/// The text to convert to lowercase.
text: Caseable,
@@ -208,16 +193,13 @@ pub fn lower(
/// Converts text or content to uppercase.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// #upper("abc") \
/// #upper[*my text*] \
/// #upper[ALREADY HIGH]
/// ```
-///
-/// Display: Uppercase
-/// Category: text
-#[func]
+#[func(title = "Uppercase")]
pub fn upper(
/// The text to convert to uppercase.
text: Caseable,
@@ -278,7 +260,7 @@ impl Case {
/// 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 heading(numbering: "I.")
@@ -292,10 +274,7 @@ impl Case {
/// = Introduction
/// #lorem(40)
/// ```
-///
-/// Display: Small Capitals
-/// Category: text
-#[func]
+#[func(title = "Small Capitals")]
pub fn smallcaps(
/// The text to display to small capitals.
body: Content,
@@ -310,7 +289,7 @@ pub fn smallcaps(
/// the same but randomly chosen. As usual for blind texts, it does not make any
/// sense. Use it as a placeholder to try layouts.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// = Blind Text
/// #lorem(30)
@@ -318,10 +297,7 @@ pub fn smallcaps(
/// = More Blind Text
/// #lorem(15)
/// ```
-///
-/// Display: Blind Text
-/// Category: text
-#[func]
+#[func(keywords = ["Blind Text"])]
pub fn lorem(
/// The length of the blind text in words.
words: usize,
diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs
index 4f3c1591..8bce5e8a 100644
--- a/crates/typst-library/src/text/mod.rs
+++ b/crates/typst-library/src/text/mod.rs
@@ -23,22 +23,23 @@ use crate::prelude::*;
/// Hook up all text definitions.
pub(super) fn define(global: &mut Scope) {
- global.define("text", TextElem::func());
- global.define("linebreak", LinebreakElem::func());
- global.define("smartquote", SmartQuoteElem::func());
- global.define("strong", StrongElem::func());
- global.define("emph", EmphElem::func());
- global.define("lower", lower_func());
- global.define("upper", upper_func());
- global.define("smallcaps", smallcaps_func());
- global.define("sub", SubElem::func());
- global.define("super", SuperElem::func());
- global.define("underline", UnderlineElem::func());
- global.define("strike", StrikeElem::func());
- global.define("highlight", HighlightElem::func());
- global.define("overline", OverlineElem::func());
- global.define("raw", RawElem::func());
- global.define("lorem", lorem_func());
+ global.category("text");
+ global.define_elem::<TextElem>();
+ global.define_elem::<LinebreakElem>();
+ global.define_elem::<SmartquoteElem>();
+ global.define_elem::<StrongElem>();
+ global.define_elem::<EmphElem>();
+ global.define_elem::<SubElem>();
+ global.define_elem::<SuperElem>();
+ global.define_elem::<UnderlineElem>();
+ global.define_elem::<OverlineElem>();
+ global.define_elem::<StrikeElem>();
+ global.define_elem::<HighlightElem>();
+ global.define_elem::<RawElem>();
+ global.define_func::<lower>();
+ global.define_func::<upper>();
+ global.define_func::<smallcaps>();
+ global.define_func::<lorem>();
}
/// Customizes the look and layout of text in a variety of ways.
@@ -47,7 +48,7 @@ pub(super) fn define(global: &mut Scope) {
/// the set rule is often the simpler choice, calling the `text` function
/// directly can be useful when passing text as an argument to another function.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// #set text(18pt)
/// With a set rule.
@@ -56,10 +57,7 @@ pub(super) fn define(global: &mut Scope) {
/// With a function call.
/// ])
/// ```
-///
-/// Display: Text
-/// Category: text
-#[element(Construct, PlainText)]
+#[elem(Construct, PlainText)]
pub struct TextElem {
/// A prioritized sequence of font families.
///
@@ -111,8 +109,8 @@ pub struct TextElem {
/// italic and oblique style is rarely observable.
///
/// If you want to emphasize your text, you should do so using the
- /// [emph]($func/emph) function instead. This makes it easy to adapt the
- /// style later if you change your mind about how to signify the emphasis.
+ /// [emph]($emph) function instead. This makes it easy to adapt the style
+ /// later if you change your mind about how to signify the emphasis.
///
/// ```example
/// #text(font: "Linux Libertine", style: "italic")[Italic]
@@ -126,7 +124,7 @@ pub struct TextElem {
/// that is closest in weight.
///
/// If you want to strongly emphasize your text, you should do so using the
- /// [strong]($func/strong) function instead. This makes it easy to adapt the
+ /// [strong]($strong) function instead. This makes it easy to adapt the
/// style later if you change your mind about how to signify the strong
/// emphasis.
///
@@ -147,7 +145,7 @@ pub struct TextElem {
/// the text if a condensed or expanded version of the font is available.
///
/// If you want to adjust the amount of space between characters instead of
- /// stretching the glyphs itself, use the [`tracking`]($func/text.tracking)
+ /// stretching the glyphs itself, use the [`tracking`]($text.tracking)
/// property instead.
///
/// ```example
@@ -196,7 +194,7 @@ pub struct TextElem {
/// the space character in the font.
///
/// If you want to adjust the amount of space between characters rather than
- /// words, use the [`tracking`]($func/text.tracking) property instead.
+ /// words, use the [`tracking`]($text.tracking) property instead.
///
/// ```example
/// #set text(spacing: 200%)
@@ -272,7 +270,7 @@ pub struct TextElem {
///
/// - The text processing pipeline can make more informed choices.
/// - Hyphenation will use the correct patterns for the language.
- /// - [Smart quotes]($func/smartquote) turns into the correct quotes for the
+ /// - [Smart quotes]($smartquote) turns into the correct quotes for the
/// language.
/// - And all other things which are language-aware.
///
@@ -327,13 +325,13 @@ pub struct TextElem {
/// - `{rtl}`: Layout text from right to left.
///
/// When writing in right-to-left scripts like Arabic or Hebrew, you should
- /// set the [text language]($func/text.lang) or direction. While individual
- /// runs of text are automatically layouted in the correct direction,
- /// setting the dominant direction gives the bidirectional reordering
- /// algorithm the necessary information to correctly place punctuation and
- /// inline objects. Furthermore, setting the direction affects the alignment
- /// values `start` and `end`, which are equivalent to `left` and `right` in
- /// `ltr` text and the other way around in `rtl` text.
+ /// set the [text language]($text.lang) or direction. While individual runs
+ /// of text are automatically layouted in the correct direction, setting the
+ /// dominant direction gives the bidirectional reordering algorithm the
+ /// necessary information to correctly place punctuation and inline objects.
+ /// Furthermore, setting the direction affects the alignment values `start`
+ /// and `end`, which are equivalent to `left` and `right` in `ltr` text and
+ /// the other way around in `rtl` text.
///
/// If you set this to `rtl` and experience bugs or in some way bad looking
/// output, please do get in touch with us through the
@@ -350,7 +348,7 @@ pub struct TextElem {
/// Whether to hyphenate text to improve line breaking. When `{auto}`, text
/// will be hyphenated if and only if justification is enabled.
///
- /// Setting the [text language]($func/text.lang) ensures that the correct
+ /// Setting the [text language]($text.lang) ensures that the correct
/// hyphenation patterns are used.
///
/// ```example
diff --git a/crates/typst-library/src/text/quotes.rs b/crates/typst-library/src/text/quotes.rs
index cf4a03d5..a47f7ed5 100644
--- a/crates/typst-library/src/text/quotes.rs
+++ b/crates/typst-library/src/text/quotes.rs
@@ -5,9 +5,9 @@ use crate::prelude::*;
/// A language-aware quote that reacts to its context.
///
/// Automatically turns into an appropriate opening or closing quote based on
-/// the active [text language]($func/text.lang).
+/// the active [text language]($text.lang).
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// "This is in quotes."
///
@@ -18,14 +18,11 @@ use crate::prelude::*;
/// "C'est entre guillemets."
/// ```
///
-/// ## Syntax { #syntax }
+/// # Syntax
/// This function also has dedicated syntax: The normal quote characters
/// (`'` and `"`). Typst automatically makes your quotes smart.
-///
-/// Display: Smart Quote
-/// Category: text
-#[element]
-pub struct SmartQuoteElem {
+#[elem]
+pub struct SmartquoteElem {
/// Whether this should be a double quote.
#[default(true)]
pub double: bool,
diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs
index a5699afd..1f46f94d 100644
--- a/crates/typst-library/src/text/raw.rs
+++ b/crates/typst-library/src/text/raw.rs
@@ -12,7 +12,7 @@ use typst::util::option_eq;
use unicode_segmentation::UnicodeSegmentation;
use super::{
- FontFamily, FontList, Hyphenate, LinebreakElem, SmartQuoteElem, TextElem, TextSize,
+ FontFamily, FontList, Hyphenate, LinebreakElem, SmartquoteElem, TextElem, TextSize,
};
use crate::layout::BlockElem;
use crate::meta::{Figurable, LocalName};
@@ -23,7 +23,7 @@ use crate::prelude::*;
/// Displays the text verbatim and in a monospace font. This is typically used
/// to embed computer code into your document.
///
-/// ## Example { #example }
+/// # Example
/// ````example
/// Adding `rbx` to `rcx` gives
/// the desired result.
@@ -43,7 +43,7 @@ use crate::prelude::*;
/// also trimmed.
/// ````
///
-/// ## Syntax { #syntax }
+/// # Syntax
/// This function also has dedicated syntax. You can enclose text in 1 or 3+
/// backticks (`` ` ``) to make it raw. Two backticks produce empty raw text.
/// When you use three or more backticks, you can additionally specify a
@@ -57,10 +57,15 @@ use crate::prelude::*;
/// needed, start the text with a single space (which will be trimmed) or use
/// the single backtick syntax. If your text should start or end with a
/// backtick, put a space before or after it (it will be trimmed).
-///
-/// Display: Raw Text / Code
-/// Category: text
-#[element(Synthesize, Show, Finalize, LocalName, Figurable, PlainText)]
+#[elem(
+ title = "Raw Text / Code",
+ Synthesize,
+ Show,
+ Finalize,
+ LocalName,
+ Figurable,
+ PlainText
+)]
pub struct RawElem {
/// The raw text.
///
@@ -153,8 +158,8 @@ pub struct RawElem {
/// code = "centered"
/// ```
/// ````
- #[default(HorizontalAlign(GenAlign::Start))]
- pub align: HorizontalAlign,
+ #[default(HAlign::Start)]
+ pub align: HAlign,
/// One or multiple additional syntax definitions to load. The syntax
/// definitions should be in the
@@ -190,10 +195,10 @@ pub struct RawElem {
/// Applying a theme only affects the color of specifically highlighted
/// text. It does not consider the theme's foreground and background
/// properties, so that you retain control over the color of raw text. You
- /// can apply the foreground color yourself with the [`text`]($func/text)
- /// function and the background with a [filled block]($func/block.fill). You
- /// could also use the [`xml`]($func/xml) function to extract these
- /// properties from the theme.
+ /// can apply the foreground color yourself with the [`text`]($text)
+ /// function and the background with a [filled block]($block.fill). You
+ /// could also use the [`xml`]($xml) function to extract these properties
+ /// from the theme.
///
/// ````example
/// #set raw(theme: "halcyon.tmTheme")
@@ -340,7 +345,7 @@ impl Show for RawElem {
if self.block(styles) {
// Align the text before inserting it into the block.
- realized = realized.aligned(Axes::with_x(Some(self.align(styles).into())));
+ realized = realized.aligned(self.align(styles).into());
realized = BlockElem::new().with_body(Some(realized)).pack();
}
@@ -356,7 +361,7 @@ impl Finalize for RawElem {
styles.set(TextElem::set_size(TextSize(Em::new(0.8).into())));
styles
.set(TextElem::set_font(FontList(vec![FontFamily::new("DejaVu Sans Mono")])));
- styles.set(SmartQuoteElem::set_enabled(false));
+ styles.set(SmartquoteElem::set_enabled(false));
realized.styled_with_map(styles)
}
}
diff --git a/crates/typst-library/src/text/shift.rs b/crates/typst-library/src/text/shift.rs
index 65e309e1..6cb4d895 100644
--- a/crates/typst-library/src/text/shift.rs
+++ b/crates/typst-library/src/text/shift.rs
@@ -5,14 +5,11 @@ use crate::prelude::*;
///
/// The text is rendered smaller and its baseline is lowered.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// Revenue#sub[yearly]
/// ```
-///
-/// Display: Subscript
-/// Category: text
-#[element(Show)]
+#[elem(title = "Subscript", Show)]
pub struct SubElem {
/// Whether to prefer the dedicated subscript characters of the font.
///
@@ -68,14 +65,11 @@ impl Show for SubElem {
///
/// The text is rendered smaller and its baseline is raised.
///
-/// ## Example { #example }
+/// # Example
/// ```example
/// 1#super[st] try!
/// ```
-///
-/// Display: Superscript
-/// Category: text
-#[element(Show)]
+#[elem(title = "Superscript", Show)]
pub struct SuperElem {
/// Whether to prefer the dedicated superscript characters of the font.
///