summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-03-24 17:12:34 +0100
committerLaurenz <laurmaedje@gmail.com>2021-03-24 17:12:34 +0100
commit73615f7e3ce23f2ea656d04ea9f96184f5ebdc0a (patch)
tree7691b792e1e4b33469a72c40fc76854f1de0814e /tests
parent6720520ec06dd0718f81049b2b11e81664f7ef62 (diff)
Text shaping πŸš€
- Shapes text with rustybuzz - Font fallback with family list - Tofus are shown in the first font Co-Authored-By: Martin <mhaug@live.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/ref/control/for.pngbin2723 -> 2715 bytes
-rw-r--r--tests/ref/control/if.pngbin1637 -> 1644 bytes
-rw-r--r--tests/ref/control/let.pngbin1961 -> 1974 bytes
-rw-r--r--tests/ref/expr/call.pngbin5991 -> 6000 bytes
-rw-r--r--tests/ref/expr/ops.pngbin776 -> 770 bytes
-rw-r--r--tests/ref/full/coma.pngbin59286 -> 59402 bytes
-rw-r--r--tests/ref/library/circle.pngbin13617 -> 13642 bytes
-rw-r--r--tests/ref/library/ellipse.pngbin7638 -> 7588 bytes
-rw-r--r--tests/ref/library/font.pngbin21246 -> 21305 bytes
-rw-r--r--tests/ref/library/pad.pngbin1226 -> 1224 bytes
-rw-r--r--tests/ref/library/page.pngbin8290 -> 8180 bytes
-rw-r--r--tests/ref/library/pagebreak.pngbin1357 -> 1360 bytes
-rw-r--r--tests/ref/library/paragraph.pngbin4170 -> 4162 bytes
-rw-r--r--tests/ref/library/rect.pngbin2769 -> 2760 bytes
-rw-r--r--tests/ref/library/spacing.pngbin3833 -> 3845 bytes
-rw-r--r--tests/ref/library/square.pngbin7166 -> 7184 bytes
-rw-r--r--tests/ref/markup/emph.pngbin3387 -> 3372 bytes
-rw-r--r--tests/ref/markup/escape.pngbin6530 -> 6517 bytes
-rw-r--r--tests/ref/markup/heading.pngbin5205 -> 5177 bytes
-rw-r--r--tests/ref/markup/linebreak.pngbin4308 -> 4292 bytes
-rw-r--r--tests/ref/markup/nbsp.pngbin1763 -> 1769 bytes
-rw-r--r--tests/ref/markup/parbreak.pngbin1654 -> 1682 bytes
-rw-r--r--tests/ref/markup/raw.pngbin8299 -> 8261 bytes
-rw-r--r--tests/ref/markup/strong.pngbin3374 -> 3385 bytes
-rw-r--r--tests/ref/repr.pngbin10828 -> 10833 bytes
-rw-r--r--tests/ref/text.pngbin36910 -> 0 bytes
-rw-r--r--tests/ref/text/basic.pngbin0 -> 37041 bytes
-rw-r--r--tests/ref/text/complex.pngbin0 -> 12273 bytes
-rw-r--r--tests/typ/text/basic.typ (renamed from tests/typ/text.typ)0
-rw-r--r--tests/typ/text/complex.typ38
-rw-r--r--tests/typeset.rs55
31 files changed, 60 insertions, 33 deletions
diff --git a/tests/ref/control/for.png b/tests/ref/control/for.png
index b3757692..1c7dfd42 100644
--- a/tests/ref/control/for.png
+++ b/tests/ref/control/for.png
Binary files differ
diff --git a/tests/ref/control/if.png b/tests/ref/control/if.png
index e38cf7c4..b30e63f8 100644
--- a/tests/ref/control/if.png
+++ b/tests/ref/control/if.png
Binary files differ
diff --git a/tests/ref/control/let.png b/tests/ref/control/let.png
index 20aad953..8a5c5fc6 100644
--- a/tests/ref/control/let.png
+++ b/tests/ref/control/let.png
Binary files differ
diff --git a/tests/ref/expr/call.png b/tests/ref/expr/call.png
index 26f1cb03..f05fb835 100644
--- a/tests/ref/expr/call.png
+++ b/tests/ref/expr/call.png
Binary files differ
diff --git a/tests/ref/expr/ops.png b/tests/ref/expr/ops.png
index 2f3a28ea..e6f2edab 100644
--- a/tests/ref/expr/ops.png
+++ b/tests/ref/expr/ops.png
Binary files differ
diff --git a/tests/ref/full/coma.png b/tests/ref/full/coma.png
index 5fd6d801..d869d57e 100644
--- a/tests/ref/full/coma.png
+++ b/tests/ref/full/coma.png
Binary files differ
diff --git a/tests/ref/library/circle.png b/tests/ref/library/circle.png
index 9e89d98b..f1128642 100644
--- a/tests/ref/library/circle.png
+++ b/tests/ref/library/circle.png
Binary files differ
diff --git a/tests/ref/library/ellipse.png b/tests/ref/library/ellipse.png
index de178d60..c78c9272 100644
--- a/tests/ref/library/ellipse.png
+++ b/tests/ref/library/ellipse.png
Binary files differ
diff --git a/tests/ref/library/font.png b/tests/ref/library/font.png
index 57a45006..07c65726 100644
--- a/tests/ref/library/font.png
+++ b/tests/ref/library/font.png
Binary files differ
diff --git a/tests/ref/library/pad.png b/tests/ref/library/pad.png
index 3536db5d..3bc0ddd0 100644
--- a/tests/ref/library/pad.png
+++ b/tests/ref/library/pad.png
Binary files differ
diff --git a/tests/ref/library/page.png b/tests/ref/library/page.png
index 7d1ff96f..8e5a83ff 100644
--- a/tests/ref/library/page.png
+++ b/tests/ref/library/page.png
Binary files differ
diff --git a/tests/ref/library/pagebreak.png b/tests/ref/library/pagebreak.png
index d513c963..ab990c69 100644
--- a/tests/ref/library/pagebreak.png
+++ b/tests/ref/library/pagebreak.png
Binary files differ
diff --git a/tests/ref/library/paragraph.png b/tests/ref/library/paragraph.png
index bf38bdf8..37898017 100644
--- a/tests/ref/library/paragraph.png
+++ b/tests/ref/library/paragraph.png
Binary files differ
diff --git a/tests/ref/library/rect.png b/tests/ref/library/rect.png
index 81ee91d7..56f1003f 100644
--- a/tests/ref/library/rect.png
+++ b/tests/ref/library/rect.png
Binary files differ
diff --git a/tests/ref/library/spacing.png b/tests/ref/library/spacing.png
index 6a234a8a..c266b9fa 100644
--- a/tests/ref/library/spacing.png
+++ b/tests/ref/library/spacing.png
Binary files differ
diff --git a/tests/ref/library/square.png b/tests/ref/library/square.png
index 401b1ab2..26469d20 100644
--- a/tests/ref/library/square.png
+++ b/tests/ref/library/square.png
Binary files differ
diff --git a/tests/ref/markup/emph.png b/tests/ref/markup/emph.png
index f43eeecb..9b6bda5c 100644
--- a/tests/ref/markup/emph.png
+++ b/tests/ref/markup/emph.png
Binary files differ
diff --git a/tests/ref/markup/escape.png b/tests/ref/markup/escape.png
index 561b5f1e..9c6f1f59 100644
--- a/tests/ref/markup/escape.png
+++ b/tests/ref/markup/escape.png
Binary files differ
diff --git a/tests/ref/markup/heading.png b/tests/ref/markup/heading.png
index f95b8c2c..a32229e7 100644
--- a/tests/ref/markup/heading.png
+++ b/tests/ref/markup/heading.png
Binary files differ
diff --git a/tests/ref/markup/linebreak.png b/tests/ref/markup/linebreak.png
index 4dfca22f..512fa0f5 100644
--- a/tests/ref/markup/linebreak.png
+++ b/tests/ref/markup/linebreak.png
Binary files differ
diff --git a/tests/ref/markup/nbsp.png b/tests/ref/markup/nbsp.png
index 89f75f14..cc920776 100644
--- a/tests/ref/markup/nbsp.png
+++ b/tests/ref/markup/nbsp.png
Binary files differ
diff --git a/tests/ref/markup/parbreak.png b/tests/ref/markup/parbreak.png
index 008afca2..f100b9d7 100644
--- a/tests/ref/markup/parbreak.png
+++ b/tests/ref/markup/parbreak.png
Binary files differ
diff --git a/tests/ref/markup/raw.png b/tests/ref/markup/raw.png
index 1aebc02d..a20ca999 100644
--- a/tests/ref/markup/raw.png
+++ b/tests/ref/markup/raw.png
Binary files differ
diff --git a/tests/ref/markup/strong.png b/tests/ref/markup/strong.png
index 20e29b1f..4bbf6ac0 100644
--- a/tests/ref/markup/strong.png
+++ b/tests/ref/markup/strong.png
Binary files differ
diff --git a/tests/ref/repr.png b/tests/ref/repr.png
index 390c2cab..71f415ef 100644
--- a/tests/ref/repr.png
+++ b/tests/ref/repr.png
Binary files differ
diff --git a/tests/ref/text.png b/tests/ref/text.png
deleted file mode 100644
index 1dd70c1c..00000000
--- a/tests/ref/text.png
+++ /dev/null
Binary files differ
diff --git a/tests/ref/text/basic.png b/tests/ref/text/basic.png
new file mode 100644
index 00000000..a06c8763
--- /dev/null
+++ b/tests/ref/text/basic.png
Binary files differ
diff --git a/tests/ref/text/complex.png b/tests/ref/text/complex.png
new file mode 100644
index 00000000..9af49f16
--- /dev/null
+++ b/tests/ref/text/complex.png
Binary files differ
diff --git a/tests/typ/text.typ b/tests/typ/text/basic.typ
index b424cee8..b424cee8 100644
--- a/tests/typ/text.typ
+++ b/tests/typ/text/basic.typ
diff --git a/tests/typ/text/complex.typ b/tests/typ/text/complex.typ
new file mode 100644
index 00000000..567a208d
--- /dev/null
+++ b/tests/typ/text/complex.typ
@@ -0,0 +1,38 @@
+// Test complex text shaping.
+
+---
+// Test ligatures.
+
+// This should create an "fi" ligature.
+Le fira
+
+// This should just shape nicely.
+#font("Noto Sans Arabic")
+Ω…Ω†Ψ΄ Ψ₯Ω„Ψ§ Ψ¨Ψ³Ω… Ψ§Ω„Ω„Ω‡
+
+// This should form a three-member family.
+#font("Twitter Color Emoji")
+πŸ‘©β€πŸ‘©β€πŸ‘¦ 🀚🏿
+
+// These two shouldn't be affected by a zero-width joiner.
+πŸžβ€πŸŒ‹
+
+---
+// Test font fallback.
+
+#font("EB Garamond", "Noto Sans Arabic", "Twitter Color Emoji")
+
+// Font fallback for emoji.
+AπŸ˜€B
+
+// Font fallback for entire text.
+Ω…Ω†Ψ΄ Ψ₯Ω„Ψ§ Ψ¨Ψ³Ω… Ψ§Ω„Ω„Ω‡
+
+// Font fallback in right-to-left text.
+Ψ¨πŸˆπŸ˜€Ψ³Ω…
+
+// Multi-layer font fallback.
+AΨ¨πŸ˜€πŸžΨ³Ω…B
+
+// Tofus are rendered with the first font.
+AπŸˆδΈ­ζ–‡B
diff --git a/tests/typeset.rs b/tests/typeset.rs
index c5ec01d2..b38311aa 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -21,7 +21,7 @@ use typst::eval::{EvalContext, FuncArgs, FuncValue, Scope, Value};
use typst::exec::State;
use typst::export::pdf;
use typst::geom::{self, Length, Point, Sides, Size};
-use typst::layout::{Element, Fill, Frame, Geometry, Image, Shape, Shaped};
+use typst::layout::{Element, Fill, Frame, Geometry, Image, Shape, ShapedText};
use typst::library;
use typst::parse::{LineMap, Scanner};
use typst::pretty::pretty;
@@ -391,15 +391,18 @@ fn draw(env: &Env, frames: &[Frame], pixel_per_pt: f32) -> Pixmap {
for &(pos, ref element) in &frame.elements {
let pos = origin + pos;
+ let x = pos.x.to_pt() as f32;
+ let y = pos.y.to_pt() as f32;
+ let ts = ts.pre_translate(x, y);
match element {
Element::Text(shaped) => {
- draw_text(&mut canvas, env, ts, pos, shaped);
+ draw_text(&mut canvas, env, ts, shaped);
}
Element::Image(image) => {
- draw_image(&mut canvas, env, ts, pos, image);
+ draw_image(&mut canvas, env, ts, image);
}
Element::Geometry(geom) => {
- draw_geometry(&mut canvas, ts, pos, geom);
+ draw_geometry(&mut canvas, ts, geom);
}
}
}
@@ -410,18 +413,18 @@ fn draw(env: &Env, frames: &[Frame], pixel_per_pt: f32) -> Pixmap {
canvas
}
-fn draw_text(canvas: &mut Pixmap, env: &Env, ts: Transform, pos: Point, shaped: &Shaped) {
- let face = env.fonts.face(shaped.face).get();
+fn draw_text(canvas: &mut Pixmap, env: &Env, ts: Transform, shaped: &ShapedText) {
+ let ttf = env.fonts.face(shaped.face).ttf();
for (&glyph, &offset) in shaped.glyphs.iter().zip(&shaped.offsets) {
- let units_per_em = face.units_per_em().unwrap_or(1000);
+ let units_per_em = ttf.units_per_em().unwrap_or(1000);
- let x = (pos.x + offset).to_pt() as f32;
- let y = pos.y.to_pt() as f32;
- let scale = (shaped.font_size / units_per_em as f64).to_pt() as f32;
+ let x = offset.to_pt() as f32;
+ let s = (shaped.size / units_per_em as f64).to_pt() as f32;
+ let ts = ts.pre_translate(x, 0.0);
// Try drawing SVG if present.
- if let Some(tree) = face
+ if let Some(tree) = ttf
.glyph_svg_image(glyph)
.and_then(|data| std::str::from_utf8(data).ok())
.map(|svg| {
@@ -433,11 +436,9 @@ fn draw_text(canvas: &mut Pixmap, env: &Env, ts: Transform, pos: Point, shaped:
for child in tree.root().children() {
if let usvg::NodeKind::Path(node) = &*child.borrow() {
let path = convert_usvg_path(&node.data);
- let transform = convert_usvg_transform(node.transform);
- let ts = transform
- .post_concat(Transform::from_row(scale, 0.0, 0.0, scale, x, y))
+ let ts = convert_usvg_transform(node.transform)
+ .post_scale(s, s)
.post_concat(ts);
-
if let Some(fill) = &node.fill {
let (paint, fill_rule) = convert_usvg_fill(fill);
canvas.fill_path(&path, &paint, fill_rule, ts, None);
@@ -450,9 +451,9 @@ fn draw_text(canvas: &mut Pixmap, env: &Env, ts: Transform, pos: Point, shaped:
// Otherwise, draw normal outline.
let mut builder = WrappedPathBuilder(tiny_skia::PathBuilder::new());
- if face.outline_glyph(glyph, &mut builder).is_some() {
+ if ttf.outline_glyph(glyph, &mut builder).is_some() {
let path = builder.0.finish().unwrap();
- let ts = Transform::from_row(scale, 0.0, 0.0, -scale, x, y).post_concat(ts);
+ let ts = ts.pre_scale(s, -s);
let mut paint = convert_typst_fill(shaped.color);
paint.anti_alias = true;
canvas.fill_path(&path, &paint, FillRule::default(), ts, None);
@@ -460,11 +461,7 @@ fn draw_text(canvas: &mut Pixmap, env: &Env, ts: Transform, pos: Point, shaped:
}
}
-fn draw_geometry(canvas: &mut Pixmap, ts: Transform, pos: Point, element: &Geometry) {
- let x = pos.x.to_pt() as f32;
- let y = pos.y.to_pt() as f32;
- let ts = Transform::from_translate(x, y).post_concat(ts);
-
+fn draw_geometry(canvas: &mut Pixmap, ts: Transform, element: &Geometry) {
let paint = convert_typst_fill(element.fill);
let rule = FillRule::default();
@@ -486,13 +483,7 @@ fn draw_geometry(canvas: &mut Pixmap, ts: Transform, pos: Point, element: &Geome
};
}
-fn draw_image(
- canvas: &mut Pixmap,
- env: &Env,
- ts: Transform,
- pos: Point,
- element: &Image,
-) {
+fn draw_image(canvas: &mut Pixmap, env: &Env, ts: Transform, element: &Image) {
let img = &env.resources.loaded::<ImageResource>(element.res);
let mut pixmap = Pixmap::new(img.buf.width(), img.buf.height()).unwrap();
@@ -501,8 +492,6 @@ fn draw_image(
*dest = ColorU8::from_rgba(r, g, b, a).premultiply();
}
- let x = pos.x.to_pt() as f32;
- let y = pos.y.to_pt() as f32;
let view_width = element.size.width.to_pt() as f32;
let view_height = element.size.height.to_pt() as f32;
let scale_x = view_width as f32 / pixmap.width() as f32;
@@ -514,10 +503,10 @@ fn draw_image(
SpreadMode::Pad,
FilterQuality::Bilinear,
1.0,
- Transform::from_row(scale_x, 0.0, 0.0, scale_y, x, y),
+ Transform::from_row(scale_x, 0.0, 0.0, scale_y, 0.0, 0.0),
);
- let rect = Rect::from_xywh(x, y, view_width, view_height).unwrap();
+ let rect = Rect::from_xywh(0.0, 0.0, view_width, view_height).unwrap();
canvas.fill_rect(rect, &paint, ts, None);
}