mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
202 upstream commits since 6cb00467a. Resolutions:
- craft-window, craft-platform: ours. Upstream's frame policy v2, restore
store and GNOME light/wait_first are the versions carried on 28 Oct;
ours moved place() to craft-behavior and kept LayoutWatch and the
appearance/portal.rs split.
- craft-timeline: ours. craft-time (crates/time) lands as upstream wrote
it and is not yet used by craft-timeline.
- Old crates/ui modules stay removed. Upstream's frosted backdrops, shape
gallery, dialog tabs and the iconic flyout top-edge rendering are not
carried. The #1094 model half is in craft-behavior's dock:
Group::flyout_offset and Action::MoveFlyoutTop.
- craft-update: #586 Save and Update, the install probe on the check
thread and quiet per-user MSI self-updates, carried into updater.rs,
stack_dialogs.rs, install/windows.rs and self_update/helper.rs.
- craft-jpeg: progressive Huffman decoding, carried into huffman.rs,
parse.rs and scan.rs.
- craft-print: the driver settings dialog's new tests and the bounded
XML parse's tests, as trials.
- Test-only conflicts: upstream tests from before the base that our side
cut stay cut; tests new upstream are kept.
194 lines
9.7 KiB
Rust
194 lines
9.7 KiB
Rust
//! Integration tests of craft-pdf: interpretation.
|
|
|
|
#![cfg(feature = "read")]
|
|
#![allow(clippy::unwrap_used, clippy::panic)]
|
|
|
|
#[cfg(feature = "interpret")]
|
|
mod device {
|
|
use craft_pdf::interpret::{
|
|
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, SoftMask,
|
|
TransformExt, font::Glyph,
|
|
};
|
|
use kurbo::{Affine, BezPath, Rect};
|
|
|
|
/// Records the clips the interpreter applies, alongside the paths painted under them.
|
|
#[derive(Default)]
|
|
struct ClipDevice {
|
|
clips: Vec<BezPath>,
|
|
paths: usize,
|
|
}
|
|
|
|
impl Device<'_> for ClipDevice {
|
|
fn set_soft_mask(&mut self, _: Option<SoftMask<'_>>) {}
|
|
fn set_blend_mode(&mut self, _: BlendMode) {}
|
|
fn draw_path(&mut self, _: &BezPath, _: Affine, _: &Paint<'_>, _: &PathDrawMode) {
|
|
self.paths += 1;
|
|
}
|
|
fn push_clip_path(&mut self, clip_path: &ClipPath) {
|
|
self.clips.push(clip_path.path.clone());
|
|
}
|
|
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'_>>, _: BlendMode) {}
|
|
fn draw_glyph(&mut self, _: &Glyph<'_>, _: Affine, _: Affine, _: &Paint<'_>, _: &GlyphDrawMode) {}
|
|
fn draw_image(&mut self, _: Image<'_, '_>, _: Affine) {}
|
|
fn pop_clip_path(&mut self) {}
|
|
fn pop_transparency_group(&mut self) {}
|
|
}
|
|
|
|
fn clips_of(content: &str) -> ClipDevice {
|
|
let bytes = format!(
|
|
"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 20] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >> endobj\n4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n5 0 obj << /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >> endobj\ntrailer << /Root 1 0 R >>\n%%EOF",
|
|
content.len()
|
|
);
|
|
let pdf = craft_pdf::open_slice(bytes.as_bytes(), None).unwrap();
|
|
let page = &pdf.pages()[0];
|
|
let cache = InterpreterCache::new();
|
|
let mut context = Context::new(
|
|
page.initial_transform(true).to_kurbo(),
|
|
Rect::new(0.0, 0.0, 40.0, 20.0),
|
|
&cache,
|
|
page.xref(),
|
|
InterpreterSettings::default(),
|
|
);
|
|
let mut device = ClipDevice::default();
|
|
craft_pdf::interpret_page(page, &mut context, &mut device).unwrap();
|
|
device
|
|
}
|
|
|
|
/// A glyph shown in a clipping render mode adds its outline to the clipping path, and that
|
|
/// clip applies to everything painted after `ET`. When the glyphs shown have no outline -- a
|
|
/// space, say -- the clip is empty and nothing paints through it. The interpreter applied a
|
|
/// clip only once a glyph had contributed an outline, so such a text object clipped nothing
|
|
/// and what followed painted unmasked.
|
|
///
|
|
/// A text object that shows no glyph at all is a different case: there the clipping path is
|
|
/// unchanged, as readers leave it.
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
|
fn a_glyph_shown_in_a_clipping_mode_clips_even_with_no_outline() {
|
|
// `7 Tr` is clip-only, and the glyph shown has no outline: the clip is empty.
|
|
let empty = clips_of("q BT 7 Tr /F1 12 Tf 4 4 Td ( ) Tj ET 1 0 0 rg 0 0 40 20 re f Q");
|
|
assert_eq!(empty.clips.len(), 1, "the empty clip is applied");
|
|
assert!(empty.clips[0].is_empty(), "and it clips everything away: {:?}", empty.clips[0]);
|
|
|
|
// Visible glyphs in the same mode clip to their outlines, as they always did: one clip, not
|
|
// empty, and the fill still painted beneath it (noted in review).
|
|
let visible = clips_of("q BT 7 Tr /F1 12 Tf 4 4 Td (H) Tj ET 1 0 0 rg 0 0 40 20 re f Q");
|
|
assert_eq!(visible.clips.len(), 1, "the glyph outline is applied as the clip");
|
|
assert!(!visible.clips[0].is_empty(), "and it holds the outline");
|
|
assert_eq!(visible.paths, 1);
|
|
|
|
// A clipping mode with no glyph shown at all leaves the clipping path alone.
|
|
let none = clips_of("q BT 7 Tr /F1 12 Tf 4 4 Td ET 1 0 0 rg 0 0 40 20 re f Q");
|
|
assert!(none.clips.is_empty(), "nothing was shown, so nothing clips");
|
|
assert_eq!(none.paths, 1);
|
|
|
|
// No text object: the fill is unclipped, as it always was.
|
|
let plain = clips_of("q 1 0 0 rg 0 0 40 20 re f Q");
|
|
assert!(plain.clips.is_empty(), "nothing clips the fill");
|
|
assert_eq!(plain.paths, 1);
|
|
|
|
// A painting mode never clips, outline or not.
|
|
let painting = clips_of("q BT 0 Tr /F1 12 Tf 4 4 Td ( ) Tj ET 1 0 0 rg 0 0 40 20 re f Q");
|
|
assert!(painting.clips.is_empty(), "a painting text mode never clips");
|
|
assert_eq!(painting.paths, 1);
|
|
}
|
|
}
|
|
|
|
/// What the clip does to the page, rendered: the device test above shows the clip the interpreter
|
|
/// sets, this shows the pixels it leaves (noted in review).
|
|
#[cfg(all(feature = "raster", not(target_arch = "wasm32")))]
|
|
mod rendered {
|
|
use craft_pdf::raster::{self, Options, RenderCache};
|
|
|
|
/// The share of a 40 x 20 pt page painted red (the fill), after `text` and then the fill.
|
|
fn red_share(text: &str) -> f64 {
|
|
let content = format!("q {text} 1 0 0 rg 0 0 40 20 re f Q");
|
|
let bytes = format!(
|
|
"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 20] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >> endobj\n4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n5 0 obj << /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >> endobj\ntrailer << /Root 1 0 R >>\n%%EOF",
|
|
content.len()
|
|
);
|
|
let pdf = craft_pdf::open_slice(bytes.as_bytes(), None).unwrap();
|
|
let page = raster::render(&pdf.pages()[0], &RenderCache::new(), &Default::default(), &Options { background: [255; 4], ..Default::default() })
|
|
.unwrap();
|
|
let pixels = page.bytes().as_chunks::<4>().0;
|
|
let red = pixels.iter().filter(|p| p[0] > 200 && p[1] < 80 && p[2] < 80).count();
|
|
red as f64 / pixels.len() as f64
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_text_clip_hides_what_follows_and_a_visible_one_shows_its_glyphs() {
|
|
assert!(red_share("") > 0.99, "no text object: the fill covers the page");
|
|
assert_eq!(red_share("BT 7 Tr /F1 12 Tf 4 4 Td ( ) Tj ET"), 0.0, "an outline-less glyph in 7 Tr: nothing paints");
|
|
let through = red_share("BT 7 Tr /F1 12 Tf 4 4 Td (H) Tj ET");
|
|
assert!(through > 0.02 && through < 0.5, "visible glyphs in 7 Tr: the fill shows through the H only ({through})");
|
|
assert!(red_share("BT 7 Tr /F1 12 Tf 4 4 Td ET") > 0.99, "no glyph shown: the clip is unchanged");
|
|
}
|
|
}
|
|
|
|
/// A path reaches the device as a rectangle only when it is one. A five-point open polyline whose
|
|
/// points reach every corner of its own bounding box (VectorCraft #2920, a zigzag in an `.ai`
|
|
/// file) used to be drawn as that box.
|
|
#[cfg(feature = "interpret")]
|
|
mod rectangles {
|
|
use craft_pdf::interpret::{
|
|
BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, InterpreterSettings, Paint, PathDrawMode, SoftMask,
|
|
TransformExt, font::Glyph,
|
|
};
|
|
use kurbo::{Affine, BezPath, Rect};
|
|
|
|
#[derive(Default)]
|
|
struct Shapes {
|
|
rects: usize,
|
|
paths: Vec<BezPath>,
|
|
clips: usize,
|
|
}
|
|
|
|
impl Device<'_> for Shapes {
|
|
fn set_soft_mask(&mut self, _: Option<SoftMask<'_>>) {}
|
|
fn set_blend_mode(&mut self, _: BlendMode) {}
|
|
fn draw_path(&mut self, path: &BezPath, _: Affine, _: &Paint<'_>, _: &PathDrawMode) {
|
|
self.paths.push(path.clone());
|
|
}
|
|
fn draw_rect(&mut self, _: &Rect, _: Affine, _: &Paint<'_>, _: &PathDrawMode) {
|
|
self.rects += 1;
|
|
}
|
|
fn push_clip_path(&mut self, _: &ClipPath) {
|
|
self.clips += 1;
|
|
}
|
|
fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'_>>, _: BlendMode) {}
|
|
fn draw_glyph(&mut self, _: &Glyph<'_>, _: Affine, _: Affine, _: &Paint<'_>, _: &GlyphDrawMode) {}
|
|
fn draw_image(&mut self, _: Image<'_, '_>, _: Affine) {}
|
|
fn pop_clip_path(&mut self) {}
|
|
fn pop_transparency_group(&mut self) {}
|
|
}
|
|
|
|
fn draw(content: &str) -> Shapes {
|
|
let bytes = format!(
|
|
"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 120 60] /Contents 4 0 R >> endobj\n4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF",
|
|
content.len()
|
|
);
|
|
let pdf = craft_pdf::open_slice(bytes.as_bytes(), None).unwrap();
|
|
let page = &pdf.pages()[0];
|
|
let cache = InterpreterCache::new();
|
|
let mut context = Context::new(
|
|
page.initial_transform(true).to_kurbo(),
|
|
Rect::new(0.0, 0.0, 120.0, 60.0),
|
|
&cache,
|
|
page.xref(),
|
|
InterpreterSettings::default(),
|
|
);
|
|
let mut shapes = Shapes::default();
|
|
craft_pdf::interpret_page(page, &mut context, &mut shapes).unwrap();
|
|
shapes
|
|
}
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
|
#[cfg_attr(not(target_arch = "wasm32"), test)]
|
|
fn a_closed_path_through_the_corners_out_of_order_is_not_a_rectangle() {
|
|
// A bow tie: every point is a corner of the bounding box, but two sides are diagonals.
|
|
let shapes = draw("0 0 0 RG 10 10 m 50 40 l 10 40 l 50 10 l h S");
|
|
assert_eq!(shapes.rects, 0);
|
|
assert_eq!(shapes.paths.len(), 1);
|
|
}
|
|
}
|