diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-02-06 13:07:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-06 13:07:25 +0100 |
| commit | bfc2f5aefc6c407de0b699b31dafd835fc2c9be3 (patch) | |
| tree | 67c23ec9df3b9f535faf5fbd443e85d9a7813d37 /tests | |
| parent | dacd7dadc04d4538f1063a86afd676695c7471ab (diff) | |
| parent | a6cae89b47246a235ed7b1093747c6f3bcb64da4 (diff) | |
Merge pull request #17 from typst/rects
Add rectangle function 🎛
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/library/ref/box.png | bin | 0 -> 5087 bytes | |||
| -rw-r--r-- | tests/library/typ/box.typ | 23 | ||||
| -rw-r--r-- | tests/typeset.rs | 26 |
3 files changed, 48 insertions, 1 deletions
diff --git a/tests/library/ref/box.png b/tests/library/ref/box.png Binary files differnew file mode 100644 index 00000000..37fd7d27 --- /dev/null +++ b/tests/library/ref/box.png diff --git a/tests/library/typ/box.typ b/tests/library/typ/box.typ new file mode 100644 index 00000000..03e5da54 --- /dev/null +++ b/tests/library/typ/box.typ @@ -0,0 +1,23 @@ +#[page "a5", flip: true] + +// Rectangle with width, should have paragraph height +#[box width: 2cm, color: #9650D6][aa] + +Sometimes there is no box + +// Rectangle with height, should span line +#[box height: 2cm, width: 100%, color: #734CED][bb] + +// Empty rectangle with width and height +#[box width: 6cm, height: 12pt, color: #CB4CED] + +// This empty rectangle should not be displayed +#[box width: 2in, color: #ff0000] + +// This one should be +#[box height: 15mm, width: 100%, color: #494DE3] + +// These are in a row! +#[box width: 2in, height: 10pt, color: #D6CD67] +#[box width: 2in, height: 10pt, color: #EDD466] +#[box width: 2in, height: 10pt, color: #E3BE62] diff --git a/tests/typeset.rs b/tests/typeset.rs index 807d55d9..d431abfe 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -20,7 +20,7 @@ use typst::eval::{Args, EvalContext, Scope, State, Value, ValueFunc}; use typst::export::pdf; use typst::font::FsIndexExt; use typst::geom::{Length, Point, Sides, Size, Spec}; -use typst::layout::{Element, Expansion, Frame, Image}; +use typst::layout::{Element, Expansion, Fill, Frame, Geometry, Image, Shape}; use typst::library; use typst::parse::{LineMap, Scanner}; use typst::pretty::{Pretty, Printer}; @@ -409,6 +409,9 @@ fn draw(frames: &[Frame], env: &Env, pixel_per_pt: f32) -> Canvas { Element::Image(image) => { draw_image(&mut canvas, pos, env, image); } + Element::Geometry(geom) => { + draw_geometry(&mut canvas, pos, env, geom); + } } } @@ -444,6 +447,27 @@ fn draw_text(canvas: &mut Canvas, pos: Point, env: &Env, shaped: &Shaped) { } } +fn draw_geometry(canvas: &mut Canvas, pos: Point, _: &Env, element: &Geometry) { + let x = pos.x.to_pt() as f32; + let y = pos.y.to_pt() as f32; + + let mut paint = Paint::default(); + + match &element.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!(), + }; + + 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); + } + }; +} + fn draw_image(canvas: &mut Canvas, pos: Point, env: &Env, element: &Image) { let img = &env.resources.loaded::<ImageResource>(element.res); |
