summaryrefslogtreecommitdiff
path: root/crates/typst-layout
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-layout')
-rw-r--r--crates/typst-layout/Cargo.toml1
-rw-r--r--crates/typst-layout/src/math/text.rs254
2 files changed, 27 insertions, 228 deletions
diff --git a/crates/typst-layout/Cargo.toml b/crates/typst-layout/Cargo.toml
index cc355a3d..2c314e5c 100644
--- a/crates/typst-layout/Cargo.toml
+++ b/crates/typst-layout/Cargo.toml
@@ -21,6 +21,7 @@ typst-timing = { workspace = true }
typst-utils = { workspace = true }
az = { workspace = true }
bumpalo = { workspace = true }
+codex = { workspace = true }
comemo = { workspace = true }
ecow = { workspace = true }
hypher = { workspace = true }
diff --git a/crates/typst-layout/src/math/text.rs b/crates/typst-layout/src/math/text.rs
index 53f88f2b..634969cd 100644
--- a/crates/typst-layout/src/math/text.rs
+++ b/crates/typst-layout/src/math/text.rs
@@ -1,10 +1,11 @@
use std::f64::consts::SQRT_2;
+use codex::styling::{to_style, MathStyle};
use ecow::EcoString;
use typst_library::diag::SourceResult;
use typst_library::foundations::{Packed, StyleChain, SymbolElem};
use typst_library::layout::{Abs, Size};
-use typst_library::math::{EquationElem, MathSize, MathVariant};
+use typst_library::math::{EquationElem, MathSize};
use typst_library::text::{
BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric,
};
@@ -64,12 +65,21 @@ fn layout_inline_text(
ctx: &mut MathContext,
styles: StyleChain,
) -> SourceResult<FrameFragment> {
+ let variant = styles.get(EquationElem::variant);
+ let bold = styles.get(EquationElem::bold);
+ // Disable auto-italic.
+ let italic = styles.get(EquationElem::italic).or(Some(false));
+
if text.chars().all(|c| c.is_ascii_digit() || c == '.') {
// Small optimization for numbers. Note that this lays out slightly
// differently to normal text and is worth re-evaluating in the future.
let mut fragments = vec![];
for unstyled_c in text.chars() {
- let c = styled_char(styles, unstyled_c, false);
+ // This is fine as ascii digits and '.' can never end up as more
+ // than a single char after styling.
+ let style = MathStyle::select(unstyled_c, variant, bold, italic);
+ let c = to_style(unstyled_c, style).next().unwrap();
+
let glyph = GlyphFragment::new_char(ctx.font, styles, c, span)?;
fragments.push(glyph.into());
}
@@ -83,8 +93,10 @@ fn layout_inline_text(
.map(|p| p.wrap());
let styles = styles.chain(&local);
- let styled_text: EcoString =
- text.chars().map(|c| styled_char(styles, c, false)).collect();
+ let styled_text: EcoString = text
+ .chars()
+ .flat_map(|c| to_style(c, MathStyle::select(c, variant, bold, italic)))
+ .collect();
let spaced = styled_text.graphemes(true).nth(1).is_some();
let elem = TextElem::packed(styled_text).spanned(span);
@@ -124,9 +136,16 @@ pub fn layout_symbol(
Some(c) if has_dtls_feat(ctx.font) => (c, styles.chain(&dtls)),
_ => (elem.text, styles),
};
- let c = styled_char(styles, unstyled_c, true);
+
+ let variant = styles.get(EquationElem::variant);
+ let bold = styles.get(EquationElem::bold);
+ let italic = styles.get(EquationElem::italic);
+
+ let style = MathStyle::select(unstyled_c, variant, bold, italic);
+ let text: EcoString = to_style(unstyled_c, style).collect();
+
let fragment: MathFragment =
- match GlyphFragment::new_char(ctx.font, symbol_styles, c, elem.span()) {
+ match GlyphFragment::new(ctx.font, symbol_styles, &text, elem.span()) {
Ok(mut glyph) => {
adjust_glyph_layout(&mut glyph, ctx, styles);
glyph.into()
@@ -134,8 +153,7 @@ pub fn layout_symbol(
Err(_) => {
// Not in the math font, fallback to normal inline text layout.
// TODO: Should replace this with proper fallback in [`GlyphFragment::new`].
- layout_inline_text(c.encode_utf8(&mut [0; 4]), elem.span(), ctx, styles)?
- .into()
+ layout_inline_text(&text, elem.span(), ctx, styles)?.into()
}
};
ctx.push(fragment);
@@ -161,226 +179,6 @@ fn adjust_glyph_layout(
}
}
-/// Style the character by selecting the unicode codepoint for italic, bold,
-/// caligraphic, etc.
-///
-/// <https://www.w3.org/TR/mathml-core/#new-text-transform-mappings>
-/// <https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols>
-fn styled_char(styles: StyleChain, c: char, auto_italic: bool) -> char {
- use MathVariant::*;
-
- let variant = styles.get(EquationElem::variant);
- let bold = styles.get(EquationElem::bold);
- let italic = styles.get(EquationElem::italic).unwrap_or(
- auto_italic
- && matches!(
- c,
- 'a'..='z' | 'ħ' | 'ı' | 'ȷ' | 'A'..='Z' |
- 'α'..='ω' | '∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ'
- )
- && matches!(variant, Sans | Serif),
- );
-
- if let Some(c) = basic_exception(c) {
- return c;
- }
-
- if let Some(c) = latin_exception(c, variant, bold, italic) {
- return c;
- }
-
- if let Some(c) = greek_exception(c, variant, bold, italic) {
- return c;
- }
-
- let base = match c {
- 'A'..='Z' => 'A',
- 'a'..='z' => 'a',
- 'Α'..='Ω' => 'Α',
- 'α'..='ω' => 'α',
- '0'..='9' => '0',
- // Hebrew Alef -> Dalet.
- '\u{05D0}'..='\u{05D3}' => '\u{05D0}',
- _ => return c,
- };
-
- let tuple = (variant, bold, italic);
- let start = match c {
- // Latin upper.
- 'A'..='Z' => match tuple {
- (Serif, false, false) => 0x0041,
- (Serif, true, false) => 0x1D400,
- (Serif, false, true) => 0x1D434,
- (Serif, true, true) => 0x1D468,
- (Sans, false, false) => 0x1D5A0,
- (Sans, true, false) => 0x1D5D4,
- (Sans, false, true) => 0x1D608,
- (Sans, true, true) => 0x1D63C,
- (Cal, false, _) => 0x1D49C,
- (Cal, true, _) => 0x1D4D0,
- (Frak, false, _) => 0x1D504,
- (Frak, true, _) => 0x1D56C,
- (Mono, _, _) => 0x1D670,
- (Bb, _, _) => 0x1D538,
- },
-
- // Latin lower.
- 'a'..='z' => match tuple {
- (Serif, false, false) => 0x0061,
- (Serif, true, false) => 0x1D41A,
- (Serif, false, true) => 0x1D44E,
- (Serif, true, true) => 0x1D482,
- (Sans, false, false) => 0x1D5BA,
- (Sans, true, false) => 0x1D5EE,
- (Sans, false, true) => 0x1D622,
- (Sans, true, true) => 0x1D656,
- (Cal, false, _) => 0x1D4B6,
- (Cal, true, _) => 0x1D4EA,
- (Frak, false, _) => 0x1D51E,
- (Frak, true, _) => 0x1D586,
- (Mono, _, _) => 0x1D68A,
- (Bb, _, _) => 0x1D552,
- },
-
- // Greek upper.
- 'Α'..='Ω' => match tuple {
- (Serif, false, false) => 0x0391,
- (Serif, true, false) => 0x1D6A8,
- (Serif, false, true) => 0x1D6E2,
- (Serif, true, true) => 0x1D71C,
- (Sans, _, false) => 0x1D756,
- (Sans, _, true) => 0x1D790,
- (Cal | Frak | Mono | Bb, _, _) => return c,
- },
-
- // Greek lower.
- 'α'..='ω' => match tuple {
- (Serif, false, false) => 0x03B1,
- (Serif, true, false) => 0x1D6C2,
- (Serif, false, true) => 0x1D6FC,
- (Serif, true, true) => 0x1D736,
- (Sans, _, false) => 0x1D770,
- (Sans, _, true) => 0x1D7AA,
- (Cal | Frak | Mono | Bb, _, _) => return c,
- },
-
- // Hebrew Alef -> Dalet.
- '\u{05D0}'..='\u{05D3}' => 0x2135,
-
- // Numbers.
- '0'..='9' => match tuple {
- (Serif, false, _) => 0x0030,
- (Serif, true, _) => 0x1D7CE,
- (Bb, _, _) => 0x1D7D8,
- (Sans, false, _) => 0x1D7E2,
- (Sans, true, _) => 0x1D7EC,
- (Mono, _, _) => 0x1D7F6,
- (Cal | Frak, _, _) => return c,
- },
-
- _ => unreachable!(),
- };
-
- std::char::from_u32(start + (c as u32 - base as u32)).unwrap()
-}
-
-fn basic_exception(c: char) -> Option<char> {
- Some(match c {
- '〈' => '⟨',
- '〉' => '⟩',
- '《' => '⟪',
- '》' => '⟫',
- _ => return None,
- })
-}
-
-fn latin_exception(
- c: char,
- variant: MathVariant,
- bold: bool,
- italic: bool,
-) -> Option<char> {
- use MathVariant::*;
- Some(match (c, variant, bold, italic) {
- ('B', Cal, false, _) => 'ℬ',
- ('E', Cal, false, _) => 'ℰ',
- ('F', Cal, false, _) => 'ℱ',
- ('H', Cal, false, _) => 'ℋ',
- ('I', Cal, false, _) => 'ℐ',
- ('L', Cal, false, _) => 'ℒ',
- ('M', Cal, false, _) => 'ℳ',
- ('R', Cal, false, _) => 'ℛ',
- ('C', Frak, false, _) => 'ℭ',
- ('H', Frak, false, _) => 'ℌ',
- ('I', Frak, false, _) => 'ℑ',
- ('R', Frak, false, _) => 'ℜ',
- ('Z', Frak, false, _) => 'ℨ',
- ('C', Bb, ..) => 'ℂ',
- ('H', Bb, ..) => 'ℍ',
- ('N', Bb, ..) => 'ℕ',
- ('P', Bb, ..) => 'ℙ',
- ('Q', Bb, ..) => 'ℚ',
- ('R', Bb, ..) => 'ℝ',
- ('Z', Bb, ..) => 'ℤ',
- ('D', Bb, _, true) => 'ⅅ',
- ('d', Bb, _, true) => 'ⅆ',
- ('e', Bb, _, true) => 'ⅇ',
- ('i', Bb, _, true) => 'ⅈ',
- ('j', Bb, _, true) => 'ⅉ',
- ('h', Serif, false, true) => 'ℎ',
- ('e', Cal, false, _) => 'ℯ',
- ('g', Cal, false, _) => 'ℊ',
- ('o', Cal, false, _) => 'ℴ',
- ('ħ', Serif, .., true) => 'ℏ',
- ('ı', Serif, .., true) => '𝚤',
- ('ȷ', Serif, .., true) => '𝚥',
- _ => return None,
- })
-}
-
-fn greek_exception(
- c: char,
- variant: MathVariant,
- bold: bool,
- italic: bool,
-) -> Option<char> {
- use MathVariant::*;
- if c == 'Ϝ' && variant == Serif && bold {
- return Some('𝟊');
- }
- if c == 'ϝ' && variant == Serif && bold {
- return Some('𝟋');
- }
-
- let list = match c {
- 'ϴ' => ['𝚹', '𝛳', '𝜭', '𝝧', '𝞡', 'ϴ'],
- '∇' => ['𝛁', '𝛻', '𝜵', '𝝯', '𝞩', '∇'],
- '∂' => ['𝛛', '𝜕', '𝝏', '𝞉', '𝟃', '∂'],
- 'ϵ' => ['𝛜', '𝜖', '𝝐', '𝞊', '𝟄', 'ϵ'],
- 'ϑ' => ['𝛝', '𝜗', '𝝑', '𝞋', '𝟅', 'ϑ'],
- 'ϰ' => ['𝛞', '𝜘', '𝝒', '𝞌', '𝟆', 'ϰ'],
- 'ϕ' => ['𝛟', '𝜙', '𝝓', '𝞍', '𝟇', 'ϕ'],
- 'ϱ' => ['𝛠', '𝜚', '𝝔', '𝞎', '𝟈', 'ϱ'],
- 'ϖ' => ['𝛡', '𝜛', '𝝕', '𝞏', '𝟉', 'ϖ'],
- 'Γ' => ['𝚪', '𝛤', '𝜞', '𝝘', '𝞒', 'ℾ'],
- 'γ' => ['𝛄', '𝛾', '𝜸', '𝝲', '𝞬', 'ℽ'],
- 'Π' => ['𝚷', '𝛱', '𝜫', '𝝥', '𝞟', 'ℿ'],
- 'π' => ['𝛑', '𝜋', '𝝅', '𝝿', '𝞹', 'ℼ'],
- '∑' => ['∑', '∑', '∑', '∑', '∑', '⅀'],
- _ => return None,
- };
-
- Some(match (variant, bold, italic) {
- (Serif, true, false) => list[0],
- (Serif, false, true) => list[1],
- (Serif, true, true) => list[2],
- (Sans, _, false) => list[3],
- (Sans, _, true) => list[4],
- (Bb, ..) => list[5],
- _ => return None,
- })
-}
-
/// The non-dotless version of a dotless character that can be used with the
/// `dtls` OpenType feature.
pub fn try_dotless(c: char) -> Option<char> {