diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-24 17:12:34 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-24 17:12:34 +0100 |
| commit | 73615f7e3ce23f2ea656d04ea9f96184f5ebdc0a (patch) | |
| tree | 7691b792e1e4b33469a72c40fc76854f1de0814e /tests | |
| parent | 6720520ec06dd0718f81049b2b11e81664f7ef62 (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.png | bin | 2723 -> 2715 bytes | |||
| -rw-r--r-- | tests/ref/control/if.png | bin | 1637 -> 1644 bytes | |||
| -rw-r--r-- | tests/ref/control/let.png | bin | 1961 -> 1974 bytes | |||
| -rw-r--r-- | tests/ref/expr/call.png | bin | 5991 -> 6000 bytes | |||
| -rw-r--r-- | tests/ref/expr/ops.png | bin | 776 -> 770 bytes | |||
| -rw-r--r-- | tests/ref/full/coma.png | bin | 59286 -> 59402 bytes | |||
| -rw-r--r-- | tests/ref/library/circle.png | bin | 13617 -> 13642 bytes | |||
| -rw-r--r-- | tests/ref/library/ellipse.png | bin | 7638 -> 7588 bytes | |||
| -rw-r--r-- | tests/ref/library/font.png | bin | 21246 -> 21305 bytes | |||
| -rw-r--r-- | tests/ref/library/pad.png | bin | 1226 -> 1224 bytes | |||
| -rw-r--r-- | tests/ref/library/page.png | bin | 8290 -> 8180 bytes | |||
| -rw-r--r-- | tests/ref/library/pagebreak.png | bin | 1357 -> 1360 bytes | |||
| -rw-r--r-- | tests/ref/library/paragraph.png | bin | 4170 -> 4162 bytes | |||
| -rw-r--r-- | tests/ref/library/rect.png | bin | 2769 -> 2760 bytes | |||
| -rw-r--r-- | tests/ref/library/spacing.png | bin | 3833 -> 3845 bytes | |||
| -rw-r--r-- | tests/ref/library/square.png | bin | 7166 -> 7184 bytes | |||
| -rw-r--r-- | tests/ref/markup/emph.png | bin | 3387 -> 3372 bytes | |||
| -rw-r--r-- | tests/ref/markup/escape.png | bin | 6530 -> 6517 bytes | |||
| -rw-r--r-- | tests/ref/markup/heading.png | bin | 5205 -> 5177 bytes | |||
| -rw-r--r-- | tests/ref/markup/linebreak.png | bin | 4308 -> 4292 bytes | |||
| -rw-r--r-- | tests/ref/markup/nbsp.png | bin | 1763 -> 1769 bytes | |||
| -rw-r--r-- | tests/ref/markup/parbreak.png | bin | 1654 -> 1682 bytes | |||
| -rw-r--r-- | tests/ref/markup/raw.png | bin | 8299 -> 8261 bytes | |||
| -rw-r--r-- | tests/ref/markup/strong.png | bin | 3374 -> 3385 bytes | |||
| -rw-r--r-- | tests/ref/repr.png | bin | 10828 -> 10833 bytes | |||
| -rw-r--r-- | tests/ref/text.png | bin | 36910 -> 0 bytes | |||
| -rw-r--r-- | tests/ref/text/basic.png | bin | 0 -> 37041 bytes | |||
| -rw-r--r-- | tests/ref/text/complex.png | bin | 0 -> 12273 bytes | |||
| -rw-r--r-- | tests/typ/text/basic.typ (renamed from tests/typ/text.typ) | 0 | ||||
| -rw-r--r-- | tests/typ/text/complex.typ | 38 | ||||
| -rw-r--r-- | tests/typeset.rs | 55 |
31 files changed, 60 insertions, 33 deletions
diff --git a/tests/ref/control/for.png b/tests/ref/control/for.png Binary files differindex b3757692..1c7dfd42 100644 --- a/tests/ref/control/for.png +++ b/tests/ref/control/for.png diff --git a/tests/ref/control/if.png b/tests/ref/control/if.png Binary files differindex e38cf7c4..b30e63f8 100644 --- a/tests/ref/control/if.png +++ b/tests/ref/control/if.png diff --git a/tests/ref/control/let.png b/tests/ref/control/let.png Binary files differindex 20aad953..8a5c5fc6 100644 --- a/tests/ref/control/let.png +++ b/tests/ref/control/let.png diff --git a/tests/ref/expr/call.png b/tests/ref/expr/call.png Binary files differindex 26f1cb03..f05fb835 100644 --- a/tests/ref/expr/call.png +++ b/tests/ref/expr/call.png diff --git a/tests/ref/expr/ops.png b/tests/ref/expr/ops.png Binary files differindex 2f3a28ea..e6f2edab 100644 --- a/tests/ref/expr/ops.png +++ b/tests/ref/expr/ops.png diff --git a/tests/ref/full/coma.png b/tests/ref/full/coma.png Binary files differindex 5fd6d801..d869d57e 100644 --- a/tests/ref/full/coma.png +++ b/tests/ref/full/coma.png diff --git a/tests/ref/library/circle.png b/tests/ref/library/circle.png Binary files differindex 9e89d98b..f1128642 100644 --- a/tests/ref/library/circle.png +++ b/tests/ref/library/circle.png diff --git a/tests/ref/library/ellipse.png b/tests/ref/library/ellipse.png Binary files differindex de178d60..c78c9272 100644 --- a/tests/ref/library/ellipse.png +++ b/tests/ref/library/ellipse.png diff --git a/tests/ref/library/font.png b/tests/ref/library/font.png Binary files differindex 57a45006..07c65726 100644 --- a/tests/ref/library/font.png +++ b/tests/ref/library/font.png diff --git a/tests/ref/library/pad.png b/tests/ref/library/pad.png Binary files differindex 3536db5d..3bc0ddd0 100644 --- a/tests/ref/library/pad.png +++ b/tests/ref/library/pad.png diff --git a/tests/ref/library/page.png b/tests/ref/library/page.png Binary files differindex 7d1ff96f..8e5a83ff 100644 --- a/tests/ref/library/page.png +++ b/tests/ref/library/page.png diff --git a/tests/ref/library/pagebreak.png b/tests/ref/library/pagebreak.png Binary files differindex d513c963..ab990c69 100644 --- a/tests/ref/library/pagebreak.png +++ b/tests/ref/library/pagebreak.png diff --git a/tests/ref/library/paragraph.png b/tests/ref/library/paragraph.png Binary files differindex bf38bdf8..37898017 100644 --- a/tests/ref/library/paragraph.png +++ b/tests/ref/library/paragraph.png diff --git a/tests/ref/library/rect.png b/tests/ref/library/rect.png Binary files differindex 81ee91d7..56f1003f 100644 --- a/tests/ref/library/rect.png +++ b/tests/ref/library/rect.png diff --git a/tests/ref/library/spacing.png b/tests/ref/library/spacing.png Binary files differindex 6a234a8a..c266b9fa 100644 --- a/tests/ref/library/spacing.png +++ b/tests/ref/library/spacing.png diff --git a/tests/ref/library/square.png b/tests/ref/library/square.png Binary files differindex 401b1ab2..26469d20 100644 --- a/tests/ref/library/square.png +++ b/tests/ref/library/square.png diff --git a/tests/ref/markup/emph.png b/tests/ref/markup/emph.png Binary files differindex f43eeecb..9b6bda5c 100644 --- a/tests/ref/markup/emph.png +++ b/tests/ref/markup/emph.png diff --git a/tests/ref/markup/escape.png b/tests/ref/markup/escape.png Binary files differindex 561b5f1e..9c6f1f59 100644 --- a/tests/ref/markup/escape.png +++ b/tests/ref/markup/escape.png diff --git a/tests/ref/markup/heading.png b/tests/ref/markup/heading.png Binary files differindex f95b8c2c..a32229e7 100644 --- a/tests/ref/markup/heading.png +++ b/tests/ref/markup/heading.png diff --git a/tests/ref/markup/linebreak.png b/tests/ref/markup/linebreak.png Binary files differindex 4dfca22f..512fa0f5 100644 --- a/tests/ref/markup/linebreak.png +++ b/tests/ref/markup/linebreak.png diff --git a/tests/ref/markup/nbsp.png b/tests/ref/markup/nbsp.png Binary files differindex 89f75f14..cc920776 100644 --- a/tests/ref/markup/nbsp.png +++ b/tests/ref/markup/nbsp.png diff --git a/tests/ref/markup/parbreak.png b/tests/ref/markup/parbreak.png Binary files differindex 008afca2..f100b9d7 100644 --- a/tests/ref/markup/parbreak.png +++ b/tests/ref/markup/parbreak.png diff --git a/tests/ref/markup/raw.png b/tests/ref/markup/raw.png Binary files differindex 1aebc02d..a20ca999 100644 --- a/tests/ref/markup/raw.png +++ b/tests/ref/markup/raw.png diff --git a/tests/ref/markup/strong.png b/tests/ref/markup/strong.png Binary files differindex 20e29b1f..4bbf6ac0 100644 --- a/tests/ref/markup/strong.png +++ b/tests/ref/markup/strong.png diff --git a/tests/ref/repr.png b/tests/ref/repr.png Binary files differindex 390c2cab..71f415ef 100644 --- a/tests/ref/repr.png +++ b/tests/ref/repr.png diff --git a/tests/ref/text.png b/tests/ref/text.png Binary files differdeleted file mode 100644 index 1dd70c1c..00000000 --- a/tests/ref/text.png +++ /dev/null diff --git a/tests/ref/text/basic.png b/tests/ref/text/basic.png Binary files differnew file mode 100644 index 00000000..a06c8763 --- /dev/null +++ b/tests/ref/text/basic.png diff --git a/tests/ref/text/complex.png b/tests/ref/text/complex.png Binary files differnew file mode 100644 index 00000000..9af49f16 --- /dev/null +++ b/tests/ref/text/complex.png 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); } |
