summaryrefslogtreecommitdiff
path: root/crates/typst-library/src
diff options
context:
space:
mode:
authorMax <max@mkor.je>2025-06-10 14:44:38 +0000
committerGitHub <noreply@github.com>2025-06-10 14:44:38 +0000
commit44d410dd007569227e8eca41e39fde9a932f0d02 (patch)
tree4270d55c6cdea2024c381aec45ba74997f05f325 /crates/typst-library/src
parent7c7b962b98a09c1baabdd03ff4ccad8f6d817b37 (diff)
Use the shaper in math (#6336)
Diffstat (limited to 'crates/typst-library/src')
-rw-r--r--crates/typst-library/src/text/font/mod.rs15
-rw-r--r--crates/typst-library/src/text/item.rs18
-rw-r--r--crates/typst-library/src/text/mod.rs18
3 files changed, 50 insertions, 1 deletions
diff --git a/crates/typst-library/src/text/font/mod.rs b/crates/typst-library/src/text/font/mod.rs
index afa92e77..0383bfe1 100644
--- a/crates/typst-library/src/text/font/mod.rs
+++ b/crates/typst-library/src/text/font/mod.rs
@@ -106,13 +106,26 @@ impl Font {
}
/// Look up the horizontal advance width of a glyph.
- pub fn advance(&self, glyph: u16) -> Option<Em> {
+ pub fn x_advance(&self, glyph: u16) -> Option<Em> {
self.0
.ttf
.glyph_hor_advance(GlyphId(glyph))
.map(|units| self.to_em(units))
}
+ /// Look up the vertical advance width of a glyph.
+ pub fn y_advance(&self, glyph: u16) -> Option<Em> {
+ self.0
+ .ttf
+ .glyph_ver_advance(GlyphId(glyph))
+ .map(|units| self.to_em(units))
+ }
+
+ /// Look up the width of a space.
+ pub fn space_width(&self) -> Option<Em> {
+ self.0.ttf.glyph_index(' ').and_then(|id| self.x_advance(id.0))
+ }
+
/// Lookup a name by id.
pub fn find_name(&self, id: u16) -> Option<String> {
find_name(&self.0.ttf, id)
diff --git a/crates/typst-library/src/text/item.rs b/crates/typst-library/src/text/item.rs
index ed559aec..518b082b 100644
--- a/crates/typst-library/src/text/item.rs
+++ b/crates/typst-library/src/text/item.rs
@@ -35,6 +35,11 @@ impl TextItem {
pub fn width(&self) -> Abs {
self.glyphs.iter().map(|g| g.x_advance).sum::<Em>().at(self.size)
}
+
+ /// The height of the text run.
+ pub fn height(&self) -> Abs {
+ self.glyphs.iter().map(|g| g.y_advance).sum::<Em>().at(self.size)
+ }
}
impl Debug for TextItem {
@@ -54,6 +59,10 @@ pub struct Glyph {
pub x_advance: Em,
/// The horizontal offset of the glyph.
pub x_offset: Em,
+ /// The advance height (Y-up) of the glyph.
+ pub y_advance: Em,
+ /// The vertical offset (Y-up) of the glyph.
+ pub y_offset: Em,
/// The range of the glyph in its item's text. The range's length may
/// be more than one due to multi-byte UTF-8 encoding or ligatures.
pub range: Range<u16>,
@@ -115,4 +124,13 @@ impl<'a> TextItemView<'a> {
.sum::<Em>()
.at(self.item.size)
}
+
+ /// The total height of this text slice
+ pub fn height(&self) -> Abs {
+ self.glyphs()
+ .iter()
+ .map(|g| g.y_advance)
+ .sum::<Em>()
+ .at(self.item.size)
+ }
}
diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs
index 23edc9e9..1c0696d1 100644
--- a/crates/typst-library/src/text/mod.rs
+++ b/crates/typst-library/src/text/mod.rs
@@ -30,6 +30,7 @@ pub use self::space::*;
use std::fmt::{self, Debug, Formatter};
use std::hash::Hash;
+use std::str::FromStr;
use std::sync::LazyLock;
use ecow::{eco_format, EcoString};
@@ -1283,6 +1284,12 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
feat(b"frac", 1);
}
+ match EquationElem::size_in(styles) {
+ MathSize::Script => feat(b"ssty", 1),
+ MathSize::ScriptScript => feat(b"ssty", 2),
+ _ => {}
+ }
+
for (tag, value) in TextElem::features_in(styles).0 {
tags.push(Feature::new(tag, value, ..))
}
@@ -1290,6 +1297,17 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
tags
}
+/// Process the language and region of a style chain into a
+/// rustybuzz-compatible BCP 47 language.
+pub fn language(styles: StyleChain) -> rustybuzz::Language {
+ let mut bcp: EcoString = TextElem::lang_in(styles).as_str().into();
+ if let Some(region) = TextElem::region_in(styles) {
+ bcp.push('-');
+ bcp.push_str(region.as_str());
+ }
+ rustybuzz::Language::from_str(&bcp).unwrap()
+}
+
/// A toggle that turns on and off alternatingly if folded.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ItalicToggle(pub bool);