diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-11-20 16:36:22 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-11-20 16:36:22 +0100 |
| commit | f105663037c44740b5aa02dea72a9b368bc003e0 (patch) | |
| tree | ee97638bfba2be3cfba8c1191919447262f181d0 /tests | |
| parent | 2e6e6244ccf73795d7d74cbc286fb0b43b404315 (diff) | |
Basic image support 🖼
- [image] function
- Image rendering in tests
- Supports JPEG and PNG
- No PDF export so far
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ref/coma.png | bin | 125591 -> 77574 bytes | |||
| -rw-r--r-- | tests/ref/image.png | bin | 0 -> 957679 bytes | |||
| -rw-r--r-- | tests/res/tiger.jpg | bin | 0 -> 116679 bytes | |||
| -rw-r--r-- | tests/typ/image.typ | 13 | ||||
| -rw-r--r-- | tests/typeset.rs | 57 |
5 files changed, 56 insertions, 14 deletions
diff --git a/tests/ref/coma.png b/tests/ref/coma.png Binary files differindex 642759a8..f84423ee 100644 --- a/tests/ref/coma.png +++ b/tests/ref/coma.png diff --git a/tests/ref/image.png b/tests/ref/image.png Binary files differnew file mode 100644 index 00000000..d532d4e9 --- /dev/null +++ b/tests/ref/image.png diff --git a/tests/res/tiger.jpg b/tests/res/tiger.jpg Binary files differnew file mode 100644 index 00000000..74dc5e0b --- /dev/null +++ b/tests/res/tiger.jpg diff --git a/tests/typ/image.typ b/tests/typ/image.typ new file mode 100644 index 00000000..b0fd4b51 --- /dev/null +++ b/tests/typ/image.typ @@ -0,0 +1,13 @@ +[page: width=10cm, height=10cm, margins=1cm] + +[image: "res/tiger.jpg"] + +[pagebreak] + +[image: "res/tiger.jpg", width=3cm] +[image: "res/tiger.jpg", height=3cm] + +[pagebreak] + +[align: center] +[image: "res/tiger.jpg", width=6cm, height=6cm] diff --git a/tests/typeset.rs b/tests/typeset.rs index 90e0d4fb..7c628879 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -15,21 +15,23 @@ use typst::eval::State; use typst::export::pdf; use typst::font::{FontLoader, SharedFontLoader}; use typst::geom::{Length, Point}; -use typst::layout::{BoxLayout, LayoutElement}; +use typst::layout::{BoxLayout, ImageElement, LayoutElement}; use typst::parse::LineMap; use typst::shaping::Shaped; use typst::typeset; -const FONT_DIR: &str = "fonts"; -const TYP_DIR: &str = "tests/typ"; -const PDF_DIR: &str = "tests/pdf"; -const PNG_DIR: &str = "tests/png"; -const REF_DIR: &str = "tests/ref"; +const FONT_DIR: &str = "../fonts"; +const TYP_DIR: &str = "typ"; +const PDF_DIR: &str = "pdf"; +const PNG_DIR: &str = "png"; +const REF_DIR: &str = "ref"; const BLACK: SolidSource = SolidSource { r: 0, g: 0, b: 0, a: 255 }; const WHITE: SolidSource = SolidSource { r: 255, g: 255, b: 255, a: 255 }; fn main() { + env::set_current_dir(env::current_dir().unwrap().join("tests")).unwrap(); + let filter = TestFilter::new(env::args().skip(1)); let mut filtered = Vec::new(); @@ -131,7 +133,7 @@ fn test(src_path: &Path, pdf_path: &Path, png_path: &Path, loader: &SharedFontLo let loader = loader.borrow(); - let surface = render(&layouts, &loader, 3.0); + let surface = render(&layouts, &loader, 2.0); surface.write_png(png_path).unwrap(); let pdf_data = pdf::export(&layouts, &loader); @@ -197,14 +199,15 @@ fn render(layouts: &[BoxLayout], loader: &FontLoader, scale: f64) -> DrawTarget ); for &(pos, ref element) in &layout.elements { + let pos = scale * pos + offset; + match element { - LayoutElement::Text(shaped) => render_shaped( - &mut surface, - loader, - shaped, - scale * pos + offset, - scale, - ), + LayoutElement::Text(shaped) => { + render_shaped(&mut surface, loader, shaped, pos, scale) + } + LayoutElement::Image(image) => { + render_image(&mut surface, image, pos, scale) + } } } @@ -244,6 +247,32 @@ fn render_shaped( } } +fn render_image(surface: &mut DrawTarget, image: &ImageElement, pos: Point, scale: f64) { + let mut data = vec![]; + for pixel in image.buf.pixels() { + let [r, g, b, a] = pixel.0; + data.push( + ((a as u32) << 24) + | ((r as u32) << 16) + | ((g as u32) << 8) + | ((b as u32) << 0), + ); + } + + surface.draw_image_with_size_at( + (scale * image.size.width.to_pt()) as f32, + (scale * image.size.height.to_pt()) as f32, + pos.x.to_pt() as f32, + pos.y.to_pt() as f32, + &raqote::Image { + width: image.buf.dimensions().0 as i32, + height: image.buf.dimensions().1 as i32, + data: &data, + }, + &Default::default(), + ); +} + struct WrappedPathBuilder(PathBuilder); impl OutlineBuilder for WrappedPathBuilder { |
