R7.6: Windows and Linux print (#3)
Windows prints through the system dialog at the document's paper; Linux through the desktop portal, the dialog's choices laid out by Ely. Round 3 findings fixed without a further review, as the rules say. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
de4a38e814
commit
13546ea899
26 files changed
+2544
-114
No files matched your search
@@ -20,6 +20,7 @@ cc = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
ely-pdf-engine = { path = ".", features = ["test-support"] }
|
||||
lopdf.workspace = true
|
||||
tempfile.workspace = true
|
||||
|
||||
[lints]
|
||||
|
||||
@@ -143,6 +143,143 @@ int ely_write_repaired(fz_context *ctx, fz_document *doc, const char *path)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Marked for print and not hidden. */
|
||||
static int prints(fz_context *ctx, pdf_annot *annot)
|
||||
{
|
||||
int flags = pdf_annot_flags(ctx, annot);
|
||||
return (flags & PDF_ANNOT_IS_PRINT) && !(flags & (PDF_ANNOT_IS_HIDDEN | PDF_ANNOT_IS_INVISIBLE));
|
||||
}
|
||||
|
||||
/* Asks MuPDF to make and keep a printable annotation's look. */
|
||||
static void ask_look(fz_context *ctx, pdf_annot *annot)
|
||||
{
|
||||
if (prints(ctx, annot))
|
||||
pdf_annot_request_synthesis(ctx, annot);
|
||||
}
|
||||
|
||||
/* Draws a printable look into `buf`, named in `xobj`. */
|
||||
static void draw_look(fz_context *ctx, pdf_annot *annot, int page, pdf_obj *xobj, fz_buffer *buf)
|
||||
{
|
||||
pdf_obj *type = pdf_dict_get(ctx, pdf_annot_obj(ctx, annot), PDF_NAME(Subtype));
|
||||
pdf_obj *ap;
|
||||
fz_matrix m;
|
||||
char name[32];
|
||||
int n = 0;
|
||||
/* Links and popups have no look of their own. */
|
||||
if (!prints(ctx, annot) || type == PDF_NAME(Link) || type == PDF_NAME(Popup))
|
||||
return;
|
||||
if (!pdf_dict_getp(ctx, pdf_annot_obj(ctx, annot), "AP/N"))
|
||||
fz_throw(ctx, FZ_ERROR_FORMAT, "a %s annotation on page %d cannot be drawn", pdf_to_name(ctx, type), page + 1);
|
||||
/* A state with no look of its own draws nothing. */
|
||||
ap = pdf_annot_ap(ctx, annot);
|
||||
if (!ap)
|
||||
return;
|
||||
do
|
||||
fz_snprintf(name, sizeof name, "ElyAnnot%d", n++);
|
||||
while (pdf_dict_gets(ctx, xobj, name));
|
||||
pdf_dict_puts(ctx, xobj, name, ap);
|
||||
pdf_dict_put(ctx, ap, PDF_NAME(Type), PDF_NAME(XObject));
|
||||
pdf_dict_put(ctx, ap, PDF_NAME(Subtype), PDF_NAME(Form));
|
||||
m = pdf_annot_transform(ctx, annot);
|
||||
fz_append_printf(ctx, buf, "q %g %g %g %g %g %g cm /%s Do Q\n", m.a, m.b, m.c, m.d, m.e, m.f, name);
|
||||
}
|
||||
|
||||
/* Draws the printable looks in; drops all but links. */
|
||||
static void flatten(fz_context *ctx, pdf_document *pdf, pdf_page *page, int number)
|
||||
{
|
||||
pdf_obj *annots = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
|
||||
pdf_obj *res, *xobj, *contents, *links = NULL, *prologue = NULL;
|
||||
fz_buffer *buf = NULL;
|
||||
pdf_annot *annot;
|
||||
int prepend, append, i;
|
||||
if (pdf_array_len(ctx, annots) == 0)
|
||||
return;
|
||||
res = pdf_dict_get(ctx, page->obj, PDF_NAME(Resources));
|
||||
if (!res)
|
||||
{
|
||||
res = pdf_dict_get_inheritable(ctx, page->obj, PDF_NAME(Resources));
|
||||
if (res)
|
||||
pdf_dict_put(ctx, page->obj, PDF_NAME(Resources), res);
|
||||
else
|
||||
res = pdf_dict_put_dict(ctx, page->obj, PDF_NAME(Resources), 4);
|
||||
}
|
||||
xobj = pdf_dict_get(ctx, res, PDF_NAME(XObject));
|
||||
if (!xobj)
|
||||
xobj = pdf_dict_put_dict(ctx, res, PDF_NAME(XObject), 8);
|
||||
fz_var(buf);
|
||||
fz_var(links);
|
||||
fz_var(prologue);
|
||||
fz_try(ctx)
|
||||
{
|
||||
/* The page's own state is closed before the looks. */
|
||||
contents = pdf_dict_get(ctx, page->obj, PDF_NAME(Contents));
|
||||
pdf_count_q_balance(ctx, pdf, res, contents, &prepend, &append);
|
||||
buf = fz_new_buffer(ctx, 1024);
|
||||
while (prepend-- > 0)
|
||||
fz_append_string(ctx, buf, "q\n");
|
||||
prologue = pdf_add_stream(ctx, pdf, buf, NULL, 0);
|
||||
fz_clear_buffer(ctx, buf);
|
||||
while (append-- > 0)
|
||||
fz_append_string(ctx, buf, "Q\n");
|
||||
for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
|
||||
draw_look(ctx, annot, number, xobj, buf);
|
||||
for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
|
||||
draw_look(ctx, annot, number, xobj, buf);
|
||||
links = pdf_new_array(ctx, pdf, 4);
|
||||
for (i = 0; i < pdf_array_len(ctx, annots); i++)
|
||||
if (pdf_dict_get(ctx, pdf_array_get(ctx, annots, i), PDF_NAME(Subtype)) == PDF_NAME(Link))
|
||||
pdf_array_push(ctx, links, pdf_array_get(ctx, annots, i));
|
||||
pdf_dict_put(ctx, page->obj, PDF_NAME(Annots), links);
|
||||
if (!pdf_is_array(ctx, contents))
|
||||
{
|
||||
pdf_obj *all = pdf_dict_put_array(ctx, page->obj, PDF_NAME(Contents), 3);
|
||||
if (contents)
|
||||
pdf_array_push(ctx, all, contents);
|
||||
contents = all;
|
||||
}
|
||||
pdf_array_insert(ctx, contents, prologue, 0);
|
||||
pdf_array_push_drop(ctx, contents, pdf_add_stream(ctx, pdf, buf, NULL, 0));
|
||||
}
|
||||
fz_always(ctx)
|
||||
{
|
||||
fz_drop_buffer(ctx, buf);
|
||||
pdf_drop_obj(ctx, links);
|
||||
pdf_drop_obj(ctx, prologue);
|
||||
}
|
||||
fz_catch(ctx)
|
||||
fz_rethrow(ctx);
|
||||
}
|
||||
|
||||
/* Every page as it prints; no form is left. */
|
||||
static void flatten_all(fz_context *ctx, fz_document *copy, void *arg)
|
||||
{
|
||||
pdf_document *pdf = ely_pdf_of(ctx, copy);
|
||||
pdf_page *page = NULL;
|
||||
pdf_annot *annot;
|
||||
int i, n = pdf_count_pages(ctx, pdf);
|
||||
fz_var(page);
|
||||
fz_try(ctx)
|
||||
{
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
page = pdf_load_page(ctx, pdf, i);
|
||||
for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
|
||||
ask_look(ctx, annot);
|
||||
for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
|
||||
ask_look(ctx, annot);
|
||||
pdf_update_page(ctx, page);
|
||||
flatten(ctx, pdf, page, i);
|
||||
fz_drop_page(ctx, &page->super);
|
||||
page = NULL;
|
||||
}
|
||||
pdf_dict_del(ctx, pdf_dict_get(ctx, pdf_trailer(ctx, pdf), PDF_NAME(Root)), PDF_NAME(AcroForm));
|
||||
}
|
||||
fz_always(ctx)
|
||||
fz_drop_page(ctx, (fz_page *)page);
|
||||
fz_catch(ctx)
|
||||
fz_rethrow(ctx);
|
||||
}
|
||||
|
||||
/* Leave to print was checked; the printer gets no lock. */
|
||||
int ely_print_copy(fz_context *ctx, fz_document *doc, const char *raw, const char *out, const char *password)
|
||||
{
|
||||
@@ -150,7 +287,7 @@ int ely_print_copy(fz_context *ctx, fz_document *doc, const char *raw, const cha
|
||||
{
|
||||
pdf_write_options opts = pdf_default_write_options;
|
||||
opts.do_encrypt = PDF_ENCRYPT_NONE;
|
||||
ely_save_copy(ctx, ely_pdf_of(ctx, doc), raw, out, password, &opts, NULL, NULL);
|
||||
ely_save_copy(ctx, ely_pdf_of(ctx, doc), raw, out, password, &opts, flatten_all, NULL);
|
||||
}
|
||||
fz_catch(ctx)
|
||||
return ely_fail(ctx);
|
||||
|
||||
@@ -171,6 +171,45 @@ fn a_print_copy_carries_no_lock() {
|
||||
assert_eq!(third.pages(opened.doc).expect("pages"), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_print_copy_shows_what_prints_to_any_screen_renderer() {
|
||||
let dir = tempfile::tempdir().expect("a temp dir");
|
||||
// No looks of their own: MuPDF makes them.
|
||||
let square = |flags: u8, rect: &str, color: &str| format!("<< /Type /Annot /Subtype /Square /F {flags} /Rect [{rect}] /C [{color}] /IC [{color}] >>");
|
||||
let annots = [
|
||||
square(4, "50 100 150 180", "1 0 0"),
|
||||
square(0, "200 100 300 180", "0 1 0"),
|
||||
// Print and NoView: printed, never shown.
|
||||
square(36, "350 100 450 180", "0 0 1"),
|
||||
// Print and Hidden: neither.
|
||||
square(6, "50 300 150 380", "0 1 0"),
|
||||
"<< /Type /Annot /Subtype /Link /F 4 /Rect [200 300 300 380] /A << /S /URI /URI (https://example.com) >> >>".to_string(),
|
||||
];
|
||||
let path = dir.path().join("doc.pdf");
|
||||
std::fs::write(&path, one_page("", " /Annots [5 0 R 6 0 R 7 0 R 8 0 R 9 0 R]", "", &annots)).expect("writes");
|
||||
let (mut worker, doc) = opened(&path);
|
||||
let print = dir.path().join("print.pdf");
|
||||
worker.print_copy(doc, &print).expect("writes");
|
||||
// Only the link stays an annotation.
|
||||
let copy = lopdf::Document::load(&print).expect("a PDF");
|
||||
let left = copy.get_dictionary(copy.get_pages()[&1]).and_then(|page| page.get(b"Annots")).and_then(lopdf::Object::as_array).expect("annotations");
|
||||
let kinds: Vec<&[u8]> = left.iter().map(|annot| copy.dereference(annot).and_then(|(_, a)| a.as_dict()?.get(b"Subtype")?.as_name()).expect("a kind")).collect();
|
||||
assert_eq!(kinds, [b"Link".as_slice()]);
|
||||
assert!(copy.catalog().expect("a catalog").get(b"AcroForm").is_err(), "no form left");
|
||||
// MuPDF draws by screen rules, as Windows' renderer does.
|
||||
let mut third = self::worker();
|
||||
let printed = third.open(&print).expect("opens").doc;
|
||||
let bgra = third.render(printed, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws").bgra;
|
||||
let at = |x: usize, y: usize| -> [u8; 3] {
|
||||
let p = &bgra[((792 - y) * 612 + x) * 4..][..3];
|
||||
[p[2], p[1], p[0]]
|
||||
};
|
||||
assert_eq!(at(100, 140), [255, 0, 0], "printable");
|
||||
assert_eq!(at(250, 140), [255, 255, 255], "not for print");
|
||||
assert_eq!(at(400, 140), [0, 0, 255], "print, not view");
|
||||
assert_eq!(at(100, 340), [255, 255, 255], "hidden");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_broken_picture_shows_when_drawn_or_checked() {
|
||||
let dir = tempfile::tempdir().expect("a temp dir");
|
||||
@@ -299,18 +338,18 @@ fn a_broken_picture_read_before_is_still_damage() {
|
||||
// Reading caches the pictures; their repeat waits.
|
||||
let read: [fn(&mut Worker, Doc); 2] = [|w, d| drop(w.text(d, 0).expect("reads")), |w, d| drop(w.search(d, 0, "x").expect("searches"))];
|
||||
for (path, first) in files.iter().flat_map(|p| read.map(|r| (p, r))) {
|
||||
let (mut worker, doc) = opened(&path);
|
||||
let (mut worker, doc) = opened(path);
|
||||
first(&mut worker, doc);
|
||||
let tile = worker.render(doc, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws");
|
||||
assert!(tile.damaged, "the picture draws as damage");
|
||||
assert!(worker.repair_report(doc).expect("asks").is_some());
|
||||
let (mut worker, doc) = opened(&path);
|
||||
let (mut worker, doc) = opened(path);
|
||||
first(&mut worker, doc);
|
||||
assert!(worker.export_image(doc, 0, 72.0, &dir.path().join("page.png")).is_err(), "its page read whole");
|
||||
let (mut worker, doc) = opened(&path);
|
||||
let (mut worker, doc) = opened(path);
|
||||
first(&mut worker, doc);
|
||||
assert!(worker.export_text(doc, &dir.path().join("doc.txt")).is_err(), "its text read whole");
|
||||
let (mut worker, doc) = opened(&path);
|
||||
let (mut worker, doc) = opened(path);
|
||||
first(&mut worker, doc);
|
||||
assert!(worker.check_pictures(doc).expect("checks"), "the check finds it");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user