Skip to content

Commit

Permalink
First attempt at checking ref images
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 21, 2024
1 parent ca0229b commit 21611ff
Show file tree
Hide file tree
Showing 18 changed files with 695 additions and 3 deletions.
589 changes: 588 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tiny-skia-path = {git = "https://github.com/RazrFalcon/tiny-skia", rev="eec0a47"

[dev-dependencies]
difference = "2.0.0"
image = "0.25.1"
paste = "1.0.15"
sitro = {git = "https://github.com/LaurenzV/sitro", rev = "666b4ee"}

Expand Down
Binary file added tests/refs/linear_gradient/ghostscript.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/mupdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/pdfbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/pdfium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/pdfjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/poppler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/quartz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/linear_gradient/xpdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/ghostscript.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/mupdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/pdfbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/pdfium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/pdfjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/poppler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/refs/text_rendering/quartz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 106 additions & 2 deletions tests/visreg.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping};
use fontdb::Source;
use image::{load_from_memory, Rgba};
use krilla::document::Document;
use krilla::rgb::Rgb;
use krilla::serialize::SerializeSettings;
use krilla::stream::TestGlyph;
use krilla::{rgb, Fill, LinearGradient, Paint, SpreadMethod, Stop};
use sitro::{render_mupdf, render_pdfbox, render_pdfium, render_pdfjs, render_quartz, RenderOptions, RenderedDocument, Renderer, render_poppler, render_ghostscript};
use sitro::{
render_ghostscript, render_mupdf, render_pdfbox, render_pdfium, render_pdfjs, render_poppler,
render_quartz, RenderOptions, RenderedDocument, Renderer,
};
use skrifa::GlyphId;
use std::cmp::max;
use std::path::PathBuf;
use std::sync::Arc;
use tiny_skia_path::{PathBuilder, Rect, Size, Transform};
use usvg::NormalizedF32;

Expand All @@ -17,7 +27,7 @@ pub fn render_doc(doc: &[u8], renderer: &Renderer) -> RenderedDocument {
Renderer::Quartz => render_quartz(doc, &options).unwrap(),
Renderer::Pdfjs => render_pdfjs(doc, &options).unwrap(),
Renderer::Pdfbox => render_pdfbox(doc, &options).unwrap(),
Renderer::Ghostscript => render_ghostscript(doc, &options).unwrap()
Renderer::Ghostscript => render_ghostscript(doc, &options).unwrap(),
}
}

Expand All @@ -32,6 +42,42 @@ pub fn save_refs(name: &str, renderer: &Renderer, document: RenderedDocument) {
panic!("empty document");
} else if document.len() == 1 {
let ref_path = refs_path.join(format!("{}.png", renderer.name()));

let reference = load_from_memory(&std::fs::read(&ref_path).unwrap()).unwrap().into_rgba8();
let actual = load_from_memory(&document[0]).unwrap().into_rgba8();

let width = max(reference.width(), actual.width());
let height = max(reference.height(), actual.height());

let mut pixel_diff = 0;

for x in 0..width {
for y in 0..height {
let actual_pixel = actual.get_pixel_checked(x, y);
let expected_pixel = reference.get_pixel_checked(x, y);



match (actual_pixel, expected_pixel) {
(Some(actual), Some(expected)) => {
if is_pix_diff(expected, actual) {
pixel_diff += 1;
}
}
(Some(actual), None) => {
pixel_diff += 1;
}
(None, Some(expected)) => {
pixel_diff += 1;
}
_ => unreachable!(),
}
}
}

assert_eq!(pixel_diff, 0);


std::fs::write(&ref_path, &document[0]).unwrap();
} else {
for (index, page) in document.iter().enumerate() {
Expand All @@ -41,6 +87,17 @@ pub fn save_refs(name: &str, renderer: &Renderer, document: RenderedDocument) {
}
}

fn is_pix_diff(pixel1: &Rgba<u8>, pixel2: &Rgba<u8>) -> bool {
if pixel1.0[3] == 0 && pixel2.0[3] == 0 {
return false;
}

pixel1.0[0] != pixel2.0[0]
|| pixel1.0[1] != pixel2.0[1]
|| pixel1.0[2] != pixel2.0[2]
|| pixel1.0[3] != pixel2.0[3]
}

macro_rules! generate_renderer_tests {
($test_name:ident, $test_body:expr) => {
paste::item! {
Expand Down Expand Up @@ -140,3 +197,50 @@ generate_renderer_tests!(linear_gradient, |renderer| {
let rendered = render_doc(&pdf, &renderer);
save_refs(stringify!(linear_gradient), &renderer, rendered);
});

generate_renderer_tests!(cosmic_text, |renderer| {
let mut font_system = FontSystem::new_with_fonts([Source::Binary(Arc::new(include_bytes!(
"fonts/NotoSans-Regular.ttf"
)))]);
let metrics = Metrics::new(14.0, 20.0);
let mut buffer = Buffer::new(&mut font_system, metrics);
buffer.set_size(&mut font_system, Some(200.0), None);
let attrs = Attrs::new();
let text = "Some text here. Let's make it a bit longer so that line wrapping kicks in";
buffer.set_text(&mut font_system, text, attrs, Shaping::Advanced);
buffer.shape_until_scroll(&mut font_system, false);

let page_size = tiny_skia_path::Size::from_wh(200.0, 400.0).unwrap();
let mut document_builder = Document::new(SerializeSettings::default());
let mut builder = document_builder.start_page(page_size);
let mut surface = builder.surface();

let font_map = surface.convert_fontdb(font_system.db_mut(), None);

// Inspect the output runs
for run in buffer.layout_runs() {
let y_offset = run.line_y;
let iter = run
.glyphs
.iter()
.map(|g| {
TestGlyph::new(
font_map.get(&g.font_id).unwrap().clone(),
GlyphId::new(g.glyph_id as u32),
g.w,
g.x_offset,
g.font_size,
run.text[g.start..g.end].to_string(),
)
})
.peekable();
surface.fill_glyph_run(0.0, y_offset, Fill::<Rgb>::default(), iter);
}

surface.finish();
builder.finish();

let pdf = document_builder.finish();
let rendered = render_doc(&pdf, &renderer);
save_refs(stringify!(text_rendering), &renderer, rendered);
});

0 comments on commit 21611ff

Please sign in to comment.