From 8469bad7487e111c8e5a0ec542f0232a0ebb4bdc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Thu, 4 Feb 2021 21:30:18 +0100 Subject: =?UTF-8?q?Add=20rectangle=20function=20=F0=9F=8E=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/library/ref/geom.png | Bin 0 -> 5087 bytes tests/library/typ/geom.typ | 23 +++++++++++++++++++++++ tests/typeset.rs | 27 ++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/library/ref/geom.png create mode 100644 tests/library/typ/geom.typ (limited to 'tests') diff --git a/tests/library/ref/geom.png b/tests/library/ref/geom.png new file mode 100644 index 00000000..37fd7d27 Binary files /dev/null and b/tests/library/ref/geom.png differ diff --git a/tests/library/typ/geom.typ b/tests/library/typ/geom.typ new file mode 100644 index 00000000..26ba7ca3 --- /dev/null +++ b/tests/library/typ/geom.typ @@ -0,0 +1,23 @@ +#[page "a5", flip: true] + +// Rectangle with width, should have paragraph height +#[rect width: 2cm, color: #9650D6][aa] + +Sometimes there is no box + +// Rectangle with height, should span line +#[rect height: 2cm, color: #734CED][bb] + +// Empty rectangle with width and height +#[rect width: 6cm, height: 12pt, color: #CB4CED] + +// This empty rectangle should not be displayed +#[rect width: 2in, color: #ff0000] + +// This one should be +#[rect height: 15mm, color: #494DE3] + +// These are in a row! +#[rect width: 2in, height: 10pt, color: #D6CD67] +#[rect width: 2in, height: 10pt, color: #EDD466] +#[rect width: 2in, height: 10pt, color: #E3BE62] diff --git a/tests/typeset.rs b/tests/typeset.rs index 807d55d9..432941ce 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,28 @@ 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 (w, h) = match &element.shape { + Shape::Rect(s) => (s.size.width.to_pt() as f32, s.size.height.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!(), + }; + + if let Some(rect) = Rect::from_xywh(x, y, w, h) { + canvas.fill_rect(rect, &paint); + } +} + fn draw_image(canvas: &mut Canvas, pos: Point, env: &Env, element: &Image) { let img = &env.resources.loaded::(element.res); -- cgit v1.2.3 From 80e076814dde330fb2136172580f11e939bc6601 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sat, 6 Feb 2021 12:30:44 +0100 Subject: =?UTF-8?q?Merge=20`rect`=20and=20`box`=20=F0=9F=A6=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/library/ref/box.png | Bin 0 -> 5087 bytes tests/library/ref/geom.png | Bin 5087 -> 0 bytes tests/library/typ/box.typ | 23 +++++++++++++++++++++++ tests/library/typ/geom.typ | 23 ----------------------- tests/typeset.rs | 15 ++++++++------- 5 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 tests/library/ref/box.png delete mode 100644 tests/library/ref/geom.png create mode 100644 tests/library/typ/box.typ delete mode 100644 tests/library/typ/geom.typ (limited to 'tests') diff --git a/tests/library/ref/box.png b/tests/library/ref/box.png new file mode 100644 index 00000000..37fd7d27 Binary files /dev/null and b/tests/library/ref/box.png differ diff --git a/tests/library/ref/geom.png b/tests/library/ref/geom.png deleted file mode 100644 index 37fd7d27..00000000 Binary files a/tests/library/ref/geom.png and /dev/null differ 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/library/typ/geom.typ b/tests/library/typ/geom.typ deleted file mode 100644 index 26ba7ca3..00000000 --- a/tests/library/typ/geom.typ +++ /dev/null @@ -1,23 +0,0 @@ -#[page "a5", flip: true] - -// Rectangle with width, should have paragraph height -#[rect width: 2cm, color: #9650D6][aa] - -Sometimes there is no box - -// Rectangle with height, should span line -#[rect height: 2cm, color: #734CED][bb] - -// Empty rectangle with width and height -#[rect width: 6cm, height: 12pt, color: #CB4CED] - -// This empty rectangle should not be displayed -#[rect width: 2in, color: #ff0000] - -// This one should be -#[rect height: 15mm, color: #494DE3] - -// These are in a row! -#[rect width: 2in, height: 10pt, color: #D6CD67] -#[rect width: 2in, height: 10pt, color: #EDD466] -#[rect width: 2in, height: 10pt, color: #E3BE62] diff --git a/tests/typeset.rs b/tests/typeset.rs index 432941ce..b1f4ede2 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -451,10 +451,6 @@ 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 (w, h) = match &element.shape { - Shape::Rect(s) => (s.size.width.to_pt() as f32, s.size.height.to_pt() as f32), - }; - let mut paint = Paint::default(); match &element.fill { @@ -464,9 +460,14 @@ fn draw_geometry(canvas: &mut Canvas, pos: Point, _: &Env, element: &Geometry) { Fill::Image(_) => todo!(), }; - if let Some(rect) = Rect::from_xywh(x, y, w, h) { - canvas.fill_rect(rect, &paint); - } + 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) { -- cgit v1.2.3 From a6cae89b47246a235ed7b1093747c6f3bcb64da4 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sat, 6 Feb 2021 12:54:44 +0100 Subject: =?UTF-8?q?Generalize=20child=20of=20NodeBackground=20=F0=9F=8D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/typeset.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/typeset.rs b/tests/typeset.rs index b1f4ede2..d431abfe 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -464,10 +464,8 @@ fn draw_geometry(canvas: &mut Canvas, pos: Point, _: &Env, element: &Geometry) { 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) { -- cgit v1.2.3