summaryrefslogtreecommitdiff
path: root/crates/typst-svg
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-svg
parent7c7b962b98a09c1baabdd03ff4ccad8f6d817b37 (diff)
Use the shaper in math (#6336)
Diffstat (limited to 'crates/typst-svg')
-rw-r--r--crates/typst-svg/src/text.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/crates/typst-svg/src/text.rs b/crates/typst-svg/src/text.rs
index e6620a59..7099a9d7 100644
--- a/crates/typst-svg/src/text.rs
+++ b/crates/typst-svg/src/text.rs
@@ -25,25 +25,32 @@ impl SVGRenderer {
self.xml.write_attribute("transform", "scale(1, -1)");
let mut x: f64 = 0.0;
+ let mut y: f64 = 0.0;
for glyph in &text.glyphs {
let id = GlyphId(glyph.id);
- let offset = x + glyph.x_offset.at(text.size).to_pt();
+ let x_offset = x + glyph.x_offset.at(text.size).to_pt();
+ let y_offset = y + glyph.y_offset.at(text.size).to_pt();
- self.render_svg_glyph(text, id, offset, scale)
- .or_else(|| self.render_bitmap_glyph(text, id, offset))
+ self.render_svg_glyph(text, id, x_offset, y_offset, scale)
+ .or_else(|| self.render_bitmap_glyph(text, id, x_offset, y_offset))
.or_else(|| {
self.render_outline_glyph(
state
.pre_concat(Transform::scale(Ratio::one(), -Ratio::one()))
- .pre_translate(Point::new(Abs::pt(offset), Abs::zero())),
+ .pre_translate(Point::new(
+ Abs::pt(x_offset),
+ Abs::pt(y_offset),
+ )),
text,
id,
- offset,
+ x_offset,
+ y_offset,
scale,
)
});
x += glyph.x_advance.at(text.size).to_pt();
+ y += glyph.y_advance.at(text.size).to_pt();
}
self.xml.end_element();
@@ -55,6 +62,7 @@ impl SVGRenderer {
text: &TextItem,
id: GlyphId,
x_offset: f64,
+ y_offset: f64,
scale: f64,
) -> Option<()> {
let data_url = convert_svg_glyph_to_base64_url(&text.font, id)?;
@@ -73,6 +81,7 @@ impl SVGRenderer {
self.xml.start_element("use");
self.xml.write_attribute_fmt("xlink:href", format_args!("#{id}"));
self.xml.write_attribute("x", &x_offset);
+ self.xml.write_attribute("y", &y_offset);
self.xml.end_element();
Some(())
@@ -84,6 +93,7 @@ impl SVGRenderer {
text: &TextItem,
id: GlyphId,
x_offset: f64,
+ y_offset: f64,
) -> Option<()> {
let (image, bitmap_x_offset, bitmap_y_offset) =
convert_bitmap_glyph_to_image(&text.font, id)?;
@@ -109,6 +119,7 @@ impl SVGRenderer {
// it.
let scale_factor = target_height / image.height();
self.xml.write_attribute("x", &(x_offset / scale_factor));
+ self.xml.write_attribute("y", &(y_offset / scale_factor));
self.xml.write_attribute_fmt(
"transform",
format_args!("scale({scale_factor} -{scale_factor})",),
@@ -125,6 +136,7 @@ impl SVGRenderer {
text: &TextItem,
glyph_id: GlyphId,
x_offset: f64,
+ y_offset: f64,
scale: f64,
) -> Option<()> {
let scale = Ratio::new(scale);
@@ -139,6 +151,7 @@ impl SVGRenderer {
self.xml.start_element("use");
self.xml.write_attribute_fmt("xlink:href", format_args!("#{id}"));
self.xml.write_attribute_fmt("x", format_args!("{x_offset}"));
+ self.xml.write_attribute_fmt("y", format_args!("{y_offset}"));
self.write_fill(
&text.fill,
FillRule::default(),