summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-03-20 20:19:30 +0100
committerLaurenz <laurmaedje@gmail.com>2021-03-20 20:19:30 +0100
commit898728f260923a91444eb23b522d0abf01a4299b (patch)
tree64fdd3f78e16e6428e765a8e2d99c3cd910bd9df /tests
parent6cb9fe9064a037224b6560b69b441b72e787fa94 (diff)
Square, circle and ellipse 🔵
Diffstat (limited to 'tests')
-rw-r--r--tests/ref/library/circle.pngbin0 -> 13617 bytes
-rw-r--r--tests/ref/library/ellipse.pngbin0 -> 7638 bytes
-rw-r--r--tests/ref/library/pagebreak.pngbin803 -> 1357 bytes
-rw-r--r--tests/ref/library/rect.pngbin0 -> 2769 bytes
-rw-r--r--tests/ref/library/shapes.pngbin3136 -> 0 bytes
-rw-r--r--tests/ref/library/square.pngbin0 -> 7166 bytes
-rw-r--r--tests/typ/library/base.typ2
-rw-r--r--tests/typ/library/circle.typ41
-rw-r--r--tests/typ/library/ellipse.typ16
-rw-r--r--tests/typ/library/pagebreak.typ17
-rw-r--r--tests/typ/library/rect.typ27
-rw-r--r--tests/typ/library/shapes.typ42
-rw-r--r--tests/typ/library/square.typ31
-rw-r--r--tests/typeset.rs79
14 files changed, 189 insertions, 66 deletions
diff --git a/tests/ref/library/circle.png b/tests/ref/library/circle.png
new file mode 100644
index 00000000..7244d0dc
--- /dev/null
+++ b/tests/ref/library/circle.png
Binary files differ
diff --git a/tests/ref/library/ellipse.png b/tests/ref/library/ellipse.png
new file mode 100644
index 00000000..de178d60
--- /dev/null
+++ b/tests/ref/library/ellipse.png
Binary files differ
diff --git a/tests/ref/library/pagebreak.png b/tests/ref/library/pagebreak.png
index f0052f9e..c5ac1f54 100644
--- a/tests/ref/library/pagebreak.png
+++ b/tests/ref/library/pagebreak.png
Binary files differ
diff --git a/tests/ref/library/rect.png b/tests/ref/library/rect.png
new file mode 100644
index 00000000..81ee91d7
--- /dev/null
+++ b/tests/ref/library/rect.png
Binary files differ
diff --git a/tests/ref/library/shapes.png b/tests/ref/library/shapes.png
deleted file mode 100644
index a244feb1..00000000
--- a/tests/ref/library/shapes.png
+++ /dev/null
Binary files differ
diff --git a/tests/ref/library/square.png b/tests/ref/library/square.png
new file mode 100644
index 00000000..401b1ab2
--- /dev/null
+++ b/tests/ref/library/square.png
Binary files differ
diff --git a/tests/typ/library/base.typ b/tests/typ/library/base.typ
index 29c976c2..cc9f14a0 100644
--- a/tests/typ/library/base.typ
+++ b/tests/typ/library/base.typ
@@ -3,7 +3,7 @@
---
#test(type("hi"), "string")
-#test(repr([Hi #rect[there]]), "[Hi [<node box>]]")
+#test(repr([Hi #rect[there]]), "[Hi [<node rect>]]")
---
// Check the output.
diff --git a/tests/typ/library/circle.typ b/tests/typ/library/circle.typ
new file mode 100644
index 00000000..b395ee2b
--- /dev/null
+++ b/tests/typ/library/circle.typ
@@ -0,0 +1,41 @@
+// Test the `circle` function.
+
+---
+// Test auto sizing.
+
+Auto-sized circle. \
+#circle(fill: #eb5278, align(center, center, [But, soft!]))
+
+Center-aligned rect in auto-sized circle.
+#circle(fill: #43a127)[
+ #align(center, center)
+ #rect(fill: #9feb52, pad(5pt)[But, soft!])
+]
+
+100%-width rect in auto-sized circle. \
+#circle(fill: #43a127, rect(width: 100%, fill: #9feb52)[
+ But, soft! what light through yonder window breaks?
+])
+
+Expanded by height.
+#circle(fill: #9feb52)[A \ B \ C]
+
+---
+// Test relative sizing.
+#rect(width: 100%, height: 50pt, fill: #aaa)[
+ #align(center, center)
+ #font(color: #fff)
+ #circle(radius: 10pt, fill: #239DAD)[A]
+ #circle(height: 60%, fill: #239DAD)[B]
+ #circle(width: 20% + 20pt, fill: #239DAD)[C]
+]
+
+---
+// Radius wins over width and height.
+// Error: 2:23-2:34 unexpected argument
+// Error: 1:36-1:49 unexpected argument
+#circle(radius: 10pt, width: 50pt, height: 100pt, fill: #239DAD)
+
+// Width wins over height.
+// Error: 22-34 unexpected argument
+#circle(width: 20pt, height: 50pt, fill: #239DAD)
diff --git a/tests/typ/library/ellipse.typ b/tests/typ/library/ellipse.typ
new file mode 100644
index 00000000..06d84a11
--- /dev/null
+++ b/tests/typ/library/ellipse.typ
@@ -0,0 +1,16 @@
+// Test the `ellipse` function.
+
+---
+100% rect in 100% ellipse in fixed rect. \
+#rect(width: 3cm, height: 2cm, fill: #2a631a)[
+ #ellipse(width: 100%, height: 100%, fill: #43a127)[
+ #rect(width: 100%, height: 100%, fill: #9feb52)[
+ #align(center, center)[Stuff inside an ellipse!]
+ ]
+ ]
+]
+
+Auto-sized ellipse. \
+#ellipse(fill: #9feb52)[
+ But, soft! what light through yonder window breaks?
+]
diff --git a/tests/typ/library/pagebreak.typ b/tests/typ/library/pagebreak.typ
index 37a544cf..26629f4b 100644
--- a/tests/typ/library/pagebreak.typ
+++ b/tests/typ/library/pagebreak.typ
@@ -4,3 +4,20 @@
First of two
#pagebreak()
#page(height: 40pt)
+
+---
+// Make sure that you can't do page related stuff in a shape.
+A
+#rect[
+ B
+ // Error: 16 cannot modify page from here
+ #pagebreak()
+
+ // Error: 11-15 cannot modify page from here
+ #page("a4")
+]
+C
+
+// No consequences from the page("A4") call here.
+#pagebreak()
+D
diff --git a/tests/typ/library/rect.typ b/tests/typ/library/rect.typ
new file mode 100644
index 00000000..40713411
--- /dev/null
+++ b/tests/typ/library/rect.typ
@@ -0,0 +1,27 @@
+// Test shapes.
+
+---
+// Test the `rect` function.
+
+#page(width: 150pt)
+
+// Fit to text.
+#rect(fill: #9feb52)[Textbox]
+
+// Empty with fixed width and height.
+#rect(width: 3cm, height: 12pt, fill: #CB4CED)
+
+// Fixed width, text height.
+#rect(width: 2cm, fill: #9650D6, pad(5pt)[Fixed and padded])
+
+// Page width, fixed height.
+#rect(height: 1cm, width: 100%, fill: #734CED)[Topleft]
+
+// Not visible, but creates a gap between the boxes above and below
+// due to line spacing.
+#rect(width: 2in, fill: #ff0000)
+
+// These are in a row!
+#rect(width: 0.5in, height: 10pt, fill: #D6CD67)
+#rect(width: 0.5in, height: 10pt, fill: #EDD466)
+#rect(width: 0.5in, height: 10pt, fill: #E3BE62)
diff --git a/tests/typ/library/shapes.typ b/tests/typ/library/shapes.typ
deleted file mode 100644
index c5a8abf9..00000000
--- a/tests/typ/library/shapes.typ
+++ /dev/null
@@ -1,42 +0,0 @@
-// Test shapes.
-
----
-// Test `rect` function.
-
-#page("a8", flip: true)
-
-// Fixed width, should have text height.
-#rect(width: 2cm, fill: #9650D6)[Legal]
-
-Sometimes there is no box.
-
-// Fixed height, should span line.
-#rect(height: 1cm, width: 100%, fill: #734CED)[B]
-
-// Empty with fixed width and height.
-#rect(width: 6cm, height: 12pt, fill: #CB4CED)
-
-// Not visible, but creates a gap between the boxes above and below.
-#rect(width: 2in, fill: #ff0000)
-
-// These are in a row!
-#rect(width: 0.5in, height: 10pt, fill: #D6CD67)
-#rect(width: 0.5in, height: 10pt, fill: #EDD466)
-#rect(width: 0.5in, height: 10pt, fill: #E3BE62)
-
----
-// Make sure that you can't do page related stuff in a shape.
-A
-#rect[
- B
- // Error: 16 cannot modify page from here
- #pagebreak()
-
- // Error: 11-15 cannot modify page from here
- #page("a4")
-]
-C
-
-// No consequences from the page("A4") call here.
-#pagebreak()
-D
diff --git a/tests/typ/library/square.typ b/tests/typ/library/square.typ
new file mode 100644
index 00000000..5f224b56
--- /dev/null
+++ b/tests/typ/library/square.typ
@@ -0,0 +1,31 @@
+// Test the `square` function.
+
+---
+Auto-sized square. \
+#square(fill: #239DAD)[
+ #align(center)
+ #pad(5pt)[
+ #font(color: #fff, weight: bold)
+ Typst \
+ ]
+]
+
+---
+// Length wins over width and height.
+// Error: 2:9-2:20 unexpected argument
+// Error: 1:22-1:34 unexpected argument
+#square(width: 10cm, height: 20cm, length: 1cm, fill: #eb5278)
+
+---
+// Test height overflow.
+#page(width: 75pt, height: 100pt)
+#square(fill: #9feb52)[
+ But, soft! what light through yonder window breaks?
+]
+
+---
+// Test width overflow.
+#page(width: 100pt, height: 75pt)
+#square(fill: #9feb52)[
+ But, soft! what light through yonder window breaks?
+]
diff --git a/tests/typeset.rs b/tests/typeset.rs
index 65347033..2cf6bfb6 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -8,18 +8,19 @@ use std::rc::Rc;
use fontdock::fs::FsIndex;
use image::{GenericImageView, Rgba};
use tiny_skia::{
- Canvas, Color, ColorU8, FillRule, FilterQuality, Paint, PathBuilder, Pattern, Pixmap,
- Rect, SpreadMode, Transform,
+ Canvas, Color, ColorU8, FillRule, FilterQuality, Paint, Pattern, Pixmap, Rect,
+ SpreadMode, Transform,
};
use ttf_parser::OutlineBuilder;
use walkdir::WalkDir;
+use typst::color;
use typst::diag::{Diag, DiagSet, Level, Pass};
use typst::env::{Env, FsIndexExt, ImageResource, ResourceLoader};
use typst::eval::{EvalContext, FuncArgs, FuncValue, Scope, Value};
use typst::exec::State;
use typst::export::pdf;
-use typst::geom::{Length, Point, Sides, Size};
+use typst::geom::{self, Length, Point, Sides, Size};
use typst::layout::{Element, Fill, Frame, Geometry, Image, Shape, Shaped};
use typst::library;
use typst::parse::{LineMap, Scanner};
@@ -418,7 +419,7 @@ fn draw_text(env: &Env, canvas: &mut Canvas, pos: Point, shaped: &Shaped) {
let y = pos.y.to_pt() as f32;
let scale = (shaped.font_size / units_per_em as f64).to_pt() as f32;
- let mut builder = WrappedPathBuilder(PathBuilder::new());
+ let mut builder = WrappedPathBuilder::default();
face.outline_glyph(glyph, &mut builder);
if let Some(path) = builder.0.finish() {
@@ -426,7 +427,7 @@ fn draw_text(env: &Env, canvas: &mut Canvas, pos: Point, shaped: &Shaped) {
.transform(&Transform::from_row(scale, 0.0, 0.0, -scale, x, y).unwrap())
.unwrap();
- let mut paint = paint_from_fill(shaped.color);
+ let mut paint = convert_fill(shaped.color);
paint.anti_alias = true;
canvas.fill_path(&placed, &paint, FillRule::default());
@@ -438,28 +439,27 @@ fn draw_geometry(_: &Env, canvas: &mut Canvas, pos: Point, element: &Geometry) {
let x = pos.x.to_pt() as f32;
let y = pos.y.to_pt() as f32;
- let paint = paint_from_fill(element.fill);
+ let paint = convert_fill(element.fill);
+ let rule = FillRule::default();
- match &element.shape {
- Shape::Rect(s) => {
- let (w, h) = (s.width.to_pt() as f32, s.height.to_pt() as f32);
- canvas.fill_rect(Rect::from_xywh(x, y, w, h).unwrap(), &paint);
+ match element.shape {
+ Shape::Rect(Size { width, height }) => {
+ let w = width.to_pt() as f32;
+ let h = height.to_pt() as f32;
+ let rect = Rect::from_xywh(x, y, w, h).unwrap();
+ canvas.fill_rect(rect, &paint);
+ }
+ Shape::Ellipse(size) => {
+ let path = convert_path(x, y, &geom::ellipse_path(size));
+ canvas.fill_path(&path, &paint, rule);
+ }
+ Shape::Path(ref path) => {
+ let path = convert_path(x, y, path);
+ canvas.fill_path(&path, &paint, rule);
}
};
}
-fn paint_from_fill(fill: Fill) -> Paint<'static> {
- let mut paint = Paint::default();
- match fill {
- Fill::Color(c) => match c {
- typst::color::Color::Rgba(c) => paint.set_color_rgba8(c.r, c.g, c.b, c.a),
- },
- Fill::Image(_) => todo!(),
- }
-
- paint
-}
-
fn draw_image(env: &Env, canvas: &mut Canvas, pos: Point, element: &Image) {
let img = &env.resources.loaded::<ImageResource>(element.res);
@@ -492,7 +492,40 @@ fn draw_image(env: &Env, canvas: &mut Canvas, pos: Point, element: &Image) {
);
}
-struct WrappedPathBuilder(PathBuilder);
+fn convert_fill(fill: Fill) -> Paint<'static> {
+ let mut paint = Paint::default();
+ match fill {
+ Fill::Color(c) => match c {
+ color::Color::Rgba(c) => paint.set_color_rgba8(c.r, c.g, c.b, c.a),
+ },
+ Fill::Image(_) => todo!(),
+ }
+ paint
+}
+
+fn convert_path(x: f32, y: f32, path: &geom::Path) -> tiny_skia::Path {
+ let f = |length: Length| length.to_pt() as f32;
+ let mut builder = tiny_skia::PathBuilder::new();
+ for elem in &path.0 {
+ match elem {
+ geom::PathElement::MoveTo(p) => builder.move_to(x + f(p.x), y + f(p.y)),
+ geom::PathElement::LineTo(p) => builder.line_to(x + f(p.x), y + f(p.y)),
+ geom::PathElement::CubicTo(p1, p2, p3) => builder.cubic_to(
+ x + f(p1.x),
+ y + f(p1.y),
+ x + f(p2.x),
+ y + f(p2.y),
+ x + f(p3.x),
+ y + f(p3.y),
+ ),
+ geom::PathElement::ClosePath => builder.close(),
+ };
+ }
+ builder.finish().unwrap()
+}
+
+#[derive(Default)]
+struct WrappedPathBuilder(tiny_skia::PathBuilder);
impl OutlineBuilder for WrappedPathBuilder {
fn move_to(&mut self, x: f32, y: f32) {