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:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-16 16:59:46 -04:00
1 parent de4a38e814
commit 13546ea899
26 files changed
+2544 -114

No files matched your search

+1 -1
View File
@@ -246,7 +246,7 @@ pub fn in_window<H: Host, R>(
pub fn print_later<H: Tabs>(pdf: std::path::PathBuf, cx: &mut Context<H>) {
// The panel's modal loop calls back into GPUI.
cx.spawn(async move |this, cx| {
let printed = ely_platform::print::print_pdf(&pdf);
let printed = ely_platform::print::print_pdf(&pdf).await;
// The copy may hold what a password guarded.
if let Err(error) = std::fs::remove_file(&pdf) {
log::error!("office: the print copy {} stays: {error}", pdf.display());
+1
View File
@@ -20,6 +20,7 @@ cc = "1"
[dev-dependencies]
ely-pdf-engine = { path = ".", features = ["test-support"] }
lopdf.workspace = true
tempfile.workspace = true
[lints]
+138 -1
View File
@@ -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);
+43 -4
View File
@@ -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");
}
+9
View File
@@ -12,6 +12,7 @@ arboard.workspace = true
directories.workspace = true
ely-ipc.workspace = true
env_logger.workspace = true
futures.workspace = true
getrandom.workspace = true
image.workspace = true
log.workspace = true
@@ -38,17 +39,21 @@ security-framework.workspace = true
[target.'cfg(windows)'.dependencies]
windows-sys.workspace = true
windows.workspace = true
[target.'cfg(unix)'.dependencies]
libc.workspace = true
[target.'cfg(target_os = "linux")'.dependencies]
lopdf.workspace = true
zbus.workspace = true
rfd = { workspace = true, features = ["xdg-portal"] }
[target.'cfg(not(target_os = "linux"))'.dependencies]
rfd.workspace = true
[dev-dependencies]
lopdf.workspace = true
tempfile.workspace = true
[target.'cfg(target_os = "macos")'.dev-dependencies]
@@ -60,3 +65,7 @@ workspace = true
[[test]]
name = "media"
harness = false
[[test]]
name = "print"
harness = false
+32 -73
View File
@@ -1,79 +1,32 @@
//! Prints a PDF through the system print panel.
//! Prints a PDF through the system print dialog.
use std::path::Path;
/// This build has a system print panel.
pub const BUILT: bool = cfg!(target_os = "macos");
/// Shows the print panel for `pdf`; false when cancelled.
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
pub fn print_pdf(pdf: &Path) -> anyhow::Result<bool> {
use objc2::{AllocAnyThread, MainThreadMarker};
use objc2_app_kit::{NSPaperOrientation, NSPrintInfo, NSPrintJobSavingURL, NSPrintSaveJob};
use objc2_foundation::{NSCopying, NSSize, NSString, NSURL};
use objc2_pdf_kit::{PDFDisplayBox, PDFDocument, PDFPrintScalingMode};
mod mac;
#[cfg(windows)]
mod win;
let mtm =
MainThreadMarker::new().ok_or_else(|| anyhow::anyhow!("printing needs the main thread"))?;
let url = NSURL::fileURLWithPath(&NSString::from_str(&pdf.to_string_lossy()));
let document = unsafe { PDFDocument::initWithURL(PDFDocument::alloc(), &url) }
.ok_or_else(|| anyhow::anyhow!("{} is no readable PDF", pdf.display()))?;
let sizes: Vec<(f64, f64)> = (0..unsafe { document.pageCount() })
.filter_map(|n| unsafe { document.pageAtIndex(n) })
.map(|page| {
let size = unsafe { page.boundsForBox(PDFDisplayBox::MediaBox) }.size;
// A page turned a quarter shows its other side up.
match unsafe { page.rotation() }.rem_euclid(180) == 90 {
true => (size.height, size.width),
false => (size.width, size.height),
}
})
.collect();
let (width, height) = paper_for(&sizes).ok_or_else(|| anyhow::anyhow!("{} has no pages", pdf.display()))?;
let paper = NSSize::new(width, height);
if let Some(scale) = crate::env::value("ELY_PRINT_SHARED_SCALE") {
// Tests start from a shared scale the copy must undo.
NSPrintInfo::sharedPrintInfo().setScalingFactor(scale.parse()?);
}
// The PDF already holds the document's paper, margins and scale.
let info = NSPrintInfo::sharedPrintInfo().copy();
info.setScalingFactor(1.0);
info.setOrientation(match paper.width > paper.height {
true => NSPaperOrientation::Landscape,
false => NSPaperOrientation::Portrait,
});
info.setPaperSize(paper);
info.setTopMargin(0.0);
info.setBottomMargin(0.0);
info.setLeftMargin(0.0);
info.setRightMargin(0.0);
log::info!("print: paper {}x{} points", paper.width, paper.height);
let operation = unsafe {
document.printOperationForPrintInfo_scalingMode_autoRotate(
Some(&info),
PDFPrintScalingMode::PageScaleNone,
true,
mtm,
)
}
.ok_or_else(|| anyhow::anyhow!("no print operation for {}", pdf.display()))?;
// Tests read the real job: saved to a file.
let saved = crate::env::value("ELY_PRINT_TO");
if let Some(to) = &saved {
let info = operation.printInfo();
info.setJobDisposition(unsafe { NSPrintSaveJob });
let to = NSURL::fileURLWithPath(&NSString::from_str(to));
unsafe { info.dictionary().insert(NSPrintJobSavingURL, &to) };
}
operation.setShowsPrintPanel(saved.is_none());
operation.setShowsProgressPanel(saved.is_none());
let printed = operation.runOperation();
log::info!("print: the panel closed, printed {printed}");
Ok(printed)
#[cfg(target_os = "linux")]
pub use linux::print_pdf;
#[cfg(target_os = "macos")]
pub use mac::print_pdf;
#[cfg(windows)]
pub use win::print_pdf;
/// Paper sizes round to whole points at worst.
#[cfg(not(target_os = "macos"))]
const ROUNDING: f64 = 0.5;
/// Whether a named paper, short side first, holds a page.
#[cfg(not(target_os = "macos"))]
fn holds((across, down): (f64, f64), (short, long): (f64, f64)) -> bool {
// A named paper may run 2 mm over, never under.
let suits = |paper: f64, page: f64| paper >= page - ROUNDING && paper - page <= 2.0 * 72.0 / 25.4;
suits(across, short) && suits(down, long)
}
/// One paper holding every page, turned as needed.
#[cfg(any(target_os = "macos", test))]
fn paper_for(sizes: &[(f64, f64)]) -> Option<(f64, f64)> {
let long = sizes.iter().map(|(w, h)| w.max(*h)).fold(None, |most: Option<f64>, side| Some(most.map_or(side, |m| m.max(side))))?;
let short = sizes.iter().map(|(w, h)| w.min(*h)).fold(0.0, f64::max);
@@ -87,10 +40,16 @@ fn paper_for(sizes: &[(f64, f64)]) -> Option<(f64, f64)> {
Some(if wide { (long, short) } else { (short, long) })
}
/// Printing needs a system panel this platform build lacks.
/// Runs `work` on its own thread, off the app's.
#[cfg(not(target_os = "macos"))]
pub fn print_pdf(_: &Path) -> anyhow::Result<bool> {
anyhow::bail!("printing is not built on this platform")
async fn off<T: Send + 'static>(work: impl FnOnce() -> anyhow::Result<T> + Send + 'static) -> anyhow::Result<T> {
let (sender, answer) = futures::channel::oneshot::channel();
std::thread::Builder::new().name("ely-print".into()).spawn(move || {
if sender.send(work()).is_err() {
log::warn!("print: the job ended after its caller left");
}
})?;
answer.await.map_err(|_| anyhow::anyhow!("the print thread ended without an answer"))?
}
#[cfg(test)]
+187
View File
@@ -0,0 +1,187 @@
//! Linux: the desktop portal's print dialog, over D-Bus.
use std::{
collections::HashMap,
fs::File,
io::{Seek as _, Write as _},
os::fd::{AsFd, FromRawFd as _},
path::Path,
sync::atomic::{AtomicU32, Ordering},
};
use anyhow::{Context as _, anyhow, bail};
use futures::StreamExt as _;
use lopdf::Document;
use serde::Serialize;
use zbus::{
Connection, Proxy,
zvariant::{DynamicType, Fd, OwnedObjectPath, OwnedValue, Value},
};
const TITLE: &str = "Ely Office";
const DESKTOP: &str = "org.freedesktop.portal.Desktop";
const PORTAL: &str = "/org/freedesktop/portal/desktop";
/// Standard papers: name, label, width and height in millimetres.
const FORMS: [(&str, &str, f64, f64); 6] = [
("iso_a3", "A3", 297.0, 420.0),
("iso_a4", "A4", 210.0, 297.0),
("iso_a5", "A5", 148.0, 210.0),
("iso_b5", "B5", 176.0, 250.0),
("na_letter", "US Letter", 215.9, 279.4),
("na_legal", "US Legal", 215.9, 355.6),
];
type Options = HashMap<&'static str, Value<'static>>;
mod choices;
mod sheets;
/// Shows the portal's print dialog for `pdf`; false when cancelled.
pub async fn print_pdf(pdf: &Path) -> anyhow::Result<bool> {
let sizes = {
let pdf = pdf.to_owned();
super::off(move || sizes(&pdf)).await?
};
let paper = super::paper_for(&sizes).ok_or_else(|| anyhow!("{} has no pages", pdf.display()))?;
log::info!("print: paper {}x{} points", paper.0, paper.1);
let bus = Connection::session().await.context("no session bus for the print portal")?;
let portal = Proxy::new(&bus, DESKTOP, PORTAL, "org.freedesktop.portal.Print").await?;
let settings = Options::new();
let prepared = request(&bus, &portal, "PreparePrint", |mut options| {
// We hand over PDF; the dialog must not offer others.
options.insert("supported_output_file_formats", Value::from(vec!["pdf"]));
("", TITLE, settings, page_setup(paper), options)
})
.await?;
let Some(mut prepared) = prepared else {
log::info!("print: the dialog was cancelled");
return Ok(false);
};
let token = prepared.get("token").context("the portal gave no print token")?.downcast_ref::<u32>()?;
let mut part = |key: &str| -> anyhow::Result<HashMap<String, OwnedValue>> {
Ok(prepared.remove(key).with_context(|| format!("the portal gave no {key}"))?.try_into()?)
};
let choices = choices::chosen(&part("settings")?, &part("page-setup")?)?;
log::info!("print: {} copies, {}%, {} up", choices.copies, choices.scale * 100.0, choices.up);
// The portal prints our bytes as given: we lay out.
let laid = {
let pdf = pdf.to_owned();
super::off(move || sheets::compose(&pdf, &choices)).await?
};
let file = in_memory(&laid)?;
let fd = Fd::from(file.as_fd());
let printed = request(&bus, &portal, "Print", |mut options| {
options.insert("token", Value::from(token));
("", TITLE, fd, options)
})
.await?;
log::info!("print: the portal took the job, printed {}", printed.is_some());
Ok(printed.is_some())
}
/// Calls `method`, awaits its answer; None when cancelled.
async fn request<B: Serialize + DynamicType>(
bus: &Connection,
portal: &Proxy<'_>,
method: &str,
body: impl FnOnce(Options) -> B,
) -> anyhow::Result<Option<HashMap<String, OwnedValue>>> {
static NEXT: AtomicU32 = AtomicU32::new(0);
let token = format!("ely{}_{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed));
let sender = bus.unique_name().context("the bus gave no name")?.trim_start_matches(':').replace('.', "_");
let path = format!("{PORTAL}/request/{sender}/{token}");
// Listen before asking: the answer may come first.
let request = Proxy::new(bus, DESKTOP, path.as_str(), "org.freedesktop.portal.Request").await?;
let mut answers = request.receive_signal("Response").await?;
let handle: OwnedObjectPath = portal.call(method, &body(Options::from([("handle_token", Value::from(token.clone()))]))).await?;
if handle.as_str() != path {
bail!("the portal answers at {handle}, not {path}");
}
let answer = answers.next().await.context("the portal closed the request")?;
let (code, results): (u32, HashMap<String, OwnedValue>) = answer.body().deserialize()?;
match code {
0 => Ok(Some(results)),
1 => Ok(None),
_ => bail!("the portal ended {method} ({code})"),
}
}
/// The portal's page setup for a paper, margins zero.
fn page_setup((width, height): (f64, f64)) -> Options {
let millimetres = |points: f64| points * 25.4 / 72.0;
let (short, long) = (millimetres(width.min(height)), millimetres(width.max(height)));
let points = |mm: f64| mm * 72.0 / 25.4;
let page = (width.min(height), width.max(height));
let (name, label) = match FORMS.iter().find(|(.., across, down)| super::holds((points(*across), points(*down)), page)) {
Some((name, label, ..)) => (name.to_string(), label.to_string()),
None => (format!("custom_{short:.0}x{long:.0}mm"), format!("{short:.0} × {long:.0} mm")),
};
let orientation = if width > height { "landscape" } else { "portrait" };
Options::from([
("Name", Value::from(name)),
("DisplayName", Value::from(label)),
("Width", Value::from(short)),
("Height", Value::from(long)),
("MarginTop", Value::from(0.0)),
("MarginBottom", Value::from(0.0)),
("MarginLeft", Value::from(0.0)),
("MarginRight", Value::from(0.0)),
("Orientation", Value::from(orientation)),
])
}
/// Each page's size in points, as it shows.
fn sizes(pdf: &Path) -> anyhow::Result<Vec<(f64, f64)>> {
let document = Document::load(pdf)?;
document.get_pages().into_values().map(|page| Ok(sheets::Shown::of(&document, page)?.size())).collect()
}
/// `bytes` in an unnamed file, read from its start.
fn in_memory(bytes: &[u8]) -> anyhow::Result<File> {
let fd = unsafe { libc::memfd_create(c"ely-print".as_ptr(), libc::MFD_CLOEXEC) };
if fd < 0 {
bail!("no file for the print job: {}", std::io::Error::last_os_error());
}
let mut file = unsafe { File::from_raw_fd(fd) };
file.write_all(bytes)?;
file.rewind()?;
Ok(file)
}
#[cfg(test)]
mod tests {
use super::{page_setup, sizes};
#[test]
fn a_known_paper_goes_by_its_name() {
let setup = page_setup((842.0, 595.0));
let text = |key: &str| setup[key].downcast_ref::<&str>().expect("text");
let number = |key: &str| setup[key].downcast_ref::<f64>().expect("a number").round();
assert_eq!((text("Name"), text("Orientation")), ("iso_a4", "landscape"));
assert_eq!((number("Width"), number("Height"), number("MarginLeft")), (210.0, 297.0, 0.0));
let custom = page_setup((283.5, 425.2));
assert_eq!(custom["Name"].downcast_ref::<&str>().expect("text"), "custom_100x150mm");
assert_eq!(custom["Orientation"].downcast_ref::<&str>().expect("text"), "portrait");
}
#[test]
fn page_sizes_inherit_turn_and_scale() {
use lopdf::{Document, Object, dictionary};
let mut document = Document::with_version("1.7");
let tree = document.new_object_id();
let media: Vec<Object> = vec![0.into(), 0.into(), 595.into(), 842.into()];
let upright = document.add_object(dictionary! { "Type" => "Page", "Parent" => tree });
let turned = document.add_object(dictionary! { "Type" => "Page", "Parent" => tree, "Rotate" => 90 });
let cropped = document.add_object(dictionary! { "Type" => "Page", "Parent" => tree, "CropBox" => vec![10.into(), 10.into(), 110.into(), 60.into()] });
let large = document.add_object(dictionary! { "Type" => "Page", "Parent" => tree, "UserUnit" => 2 });
let kids: Vec<Object> = vec![upright.into(), turned.into(), cropped.into(), large.into()];
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => kids, "Count" => 4, "MediaBox" => media }.into());
let catalog = document.add_object(dictionary! { "Type" => "Catalog", "Pages" => tree });
document.trailer.set("Root", catalog);
let folder = tempfile::tempdir().expect("a folder");
let pdf = folder.path().join("pages.pdf");
document.save(&pdf).expect("saves");
assert_eq!(sizes(&pdf).expect("reads"), [(595.0, 842.0), (842.0, 595.0), (100.0, 50.0), (1190.0, 1684.0)]);
}
}
@@ -0,0 +1,245 @@
//! What the portal's dialog chose: its settings and page setup.
use std::collections::HashMap;
use anyhow::{Context as _, bail};
use zbus::zvariant::OwnedValue;
type Map = HashMap<String, OwnedValue>;
/// Which sheets print, counting from one.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Set {
All,
Even,
Odd,
}
/// How several pages fill one sheet.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Order {
pub across_first: bool,
pub leftward: bool,
pub upward: bool,
}
/// The dialog's choices; the app applies every one.
#[derive(Debug, PartialEq)]
pub(super) struct Choices {
/// Pages from zero, both ends kept; None is every page.
pub ranges: Option<Vec<(u32, u32)>>,
pub set: Set,
pub reverse: bool,
pub copies: u32,
pub collate: bool,
pub scale: f64,
pub up: u32,
pub order: Order,
/// The sheet as it lies, in points.
pub sheet: (f64, f64),
pub upside_down: bool,
}
/// GTK's settings and page setup, as the portal passes them.
pub(super) fn chosen(settings: &Map, setup: &Map) -> anyhow::Result<Choices> {
let ranges = match text(settings, "print-pages")?.unwrap_or("all") {
"all" => None,
"ranges" => Some(ranges(text(settings, "page-ranges")?.context("the print dialog gave no page ranges")?)?),
other => bail!("the print dialog chose the {other} pages; Ely offers none"),
};
let set = match text(settings, "page-set")?.unwrap_or("all") {
"all" => Set::All,
"even" => Set::Even,
"odd" => Set::Odd,
other => bail!("the print dialog chose the page set {other}"),
};
// GTK's CUPS backend sends even or odd to CUPS.
let set = match settings.contains_key("output-uri") || settings.contains_key("lpr-commandline") {
true => set,
false => Set::All,
};
let scale = number(settings, "scale", 100.0)? / 100.0;
if !(scale.is_finite() && scale > 0.0) {
bail!("the print dialog chose a scale of {}%", scale * 100.0);
}
let order = order(text(settings, "number-up-layout")?.unwrap_or("lrtb"))?;
let millimetres = |key: &str| -> anyhow::Result<f64> {
let value = setup.get(key).with_context(|| format!("the page setup gives no {key}"))?;
Ok(value.downcast_ref::<f64>().with_context(|| format!("the page setup's {key} is no number"))? * 72.0 / 25.4)
};
let (width, height) = (millimetres("Width")?, millimetres("Height")?);
let way = setup.get("Orientation").context("the page setup gives no orientation")?;
let (sheet, upside_down) = match way.downcast_ref::<&str>().context("the page setup's orientation is no text")? {
"portrait" => ((width, height), false),
"landscape" => ((height, width), false),
"reverse-portrait" => ((width, height), true),
"reverse-landscape" => ((height, width), true),
other => bail!("the page setup's orientation is {other}"),
};
Ok(Choices {
ranges,
set,
reverse: flag(settings, "reverse", false)?,
copies: count(settings, "n-copies")?,
collate: flag(settings, "collate", true)?,
scale,
up: count(settings, "number-up")?,
order,
sheet,
upside_down,
})
}
/// GTK's ranges, "0-2,4" from zero; end -1 runs on.
fn ranges(text: &str) -> anyhow::Result<Vec<(u32, u32)>> {
let page = |part: &str| part.trim().parse::<u32>().with_context(|| format!("the page range {text} is unreadable"));
let ranges = text
.split(',')
.filter(|part| !part.trim().is_empty())
.map(|part| {
let (from, to) = match part.split_once('-') {
Some((from, to)) if to.trim() == "-1" => (page(from)?, u32::MAX),
Some((from, to)) => (page(from)?, page(to)?),
None => (page(part)?, page(part)?),
};
if from > to {
bail!("the page range {text} runs backwards");
}
Ok((from, to))
})
.collect::<anyhow::Result<Vec<_>>>()?;
if ranges.is_empty() {
bail!("the page ranges are empty");
}
Ok(ranges)
}
/// GTK's n-up nick, such as "lrtb": first way, then next.
fn order(nick: &str) -> anyhow::Result<Order> {
let (first, then) = nick.split_at_checked(2).with_context(|| format!("the n-up layout {nick} is unknown"))?;
let across = |way: &str| matches!(way, "lr" | "rl");
let down = |way: &str| matches!(way, "tb" | "bt");
if !(across(first) && down(then) || down(first) && across(then)) {
bail!("the n-up layout {nick} is unknown");
}
Ok(Order { across_first: across(first), leftward: nick.contains("rl"), upward: nick.contains("bt") })
}
fn text<'a>(settings: &'a Map, key: &str) -> anyhow::Result<Option<&'a str>> {
let value = settings.get(key).map(|value| value.downcast_ref::<&str>());
value.transpose().with_context(|| format!("the print setting {key} is no text"))
}
fn number(settings: &Map, key: &str, unset: f64) -> anyhow::Result<f64> {
match text(settings, key)? {
Some(text) => text.trim().parse().with_context(|| format!("the print setting {key} is {text}")),
None => Ok(unset),
}
}
/// A whole number, at least one; one when unset.
fn count(settings: &Map, key: &str) -> anyhow::Result<u32> {
let count = match text(settings, key)? {
Some(text) => text.trim().parse().with_context(|| format!("the print setting {key} is {text}"))?,
None => 1,
};
if count == 0 {
bail!("the print setting {key} is zero");
}
Ok(count)
}
fn flag(settings: &Map, key: &str, unset: bool) -> anyhow::Result<bool> {
match text(settings, key)? {
None => Ok(unset),
Some("true") => Ok(true),
Some("false") => Ok(false),
Some(other) => bail!("the print setting {key} is {other}"),
}
}
#[cfg(test)]
mod tests {
use zbus::zvariant::{OwnedValue, Value};
use super::{Choices, Map, Order, Set, chosen};
fn map(pairs: &[(&str, Value<'static>)]) -> Map {
pairs.iter().map(|(key, value)| (key.to_string(), OwnedValue::try_from(value.clone()).expect("owned"))).collect()
}
fn a4(orientation: &'static str) -> Map {
map(&[("Width", Value::from(210.0)), ("Height", Value::from(297.0)), ("Orientation", Value::from(orientation))])
}
#[test]
fn unset_settings_print_everything_once() {
let choices = chosen(&Map::new(), &a4("portrait")).expect("reads");
let order = Order { across_first: true, leftward: false, upward: false };
let (width, height) = (210.0 * 72.0 / 25.4, 297.0 * 72.0 / 25.4);
assert_eq!(
choices,
Choices {
ranges: None,
set: Set::All,
reverse: false,
copies: 1,
collate: true,
scale: 1.0,
up: 1,
order,
sheet: (width, height),
upside_down: false
}
);
}
#[test]
fn gtk_settings_read_as_given() {
let settings = map(&[
("print-pages", Value::from("ranges")),
("page-ranges", Value::from("0-1, 4,7--1")),
("page-set", Value::from("even")),
("reverse", Value::from("true")),
("n-copies", Value::from("3")),
("collate", Value::from("false")),
("scale", Value::from("50")),
("number-up", Value::from("4")),
("number-up-layout", Value::from("btrl")),
("output-uri", Value::from("file:///tmp/out.pdf")),
]);
let choices = chosen(&settings, &a4("reverse-landscape")).expect("reads");
assert_eq!(choices.ranges, Some(vec![(0, 1), (4, 4), (7, u32::MAX)]));
assert_eq!((choices.set, choices.reverse, choices.copies, choices.collate), (Set::Even, true, 3, false));
assert_eq!((choices.scale, choices.up), (0.5, 4));
assert_eq!(choices.order, Order { across_first: false, leftward: true, upward: true });
assert_eq!((choices.sheet.0.round(), choices.sheet.1.round(), choices.upside_down), (842.0, 595.0, true));
}
#[test]
fn even_pages_are_ours_only_where_cups_leaves_them() {
let set = |backend: &[(&str, Value<'static>)]| {
let mut pairs = vec![("page-set", Value::from("even"))];
pairs.extend_from_slice(backend);
chosen(&map(&pairs), &a4("portrait")).expect("reads").set
};
// GTK's CUPS backend sends cups-page-set itself.
assert_eq!(set(&[("printer", Value::from("Office"))]), Set::All);
assert_eq!(set(&[("output-uri", Value::from("file:///tmp/out.pdf"))]), Set::Even);
assert_eq!(set(&[("lpr-commandline", Value::from("lpr"))]), Set::Even);
}
#[test]
fn what_cannot_be_honoured_fails() {
let bad = |pairs: &[(&str, Value<'static>)]| chosen(&map(pairs), &a4("portrait")).is_err();
assert!(bad(&[("print-pages", Value::from("selection"))]));
assert!(bad(&[("print-pages", Value::from("ranges")), ("page-ranges", Value::from("two"))]));
assert!(bad(&[("print-pages", Value::from("ranges")), ("page-ranges", Value::from("3-1"))]));
assert!(chosen(&Map::new(), &a4("reverse_portrait")).is_err(), "GTK writes reverse-portrait");
assert!(bad(&[("scale", Value::from("0"))]));
assert!(bad(&[("n-copies", Value::from("0"))]));
assert!(bad(&[("number-up-layout", Value::from("lrrl"))]));
assert!(bad(&[("reverse", Value::from(1u32))]));
assert!(chosen(&Map::new(), &map(&[("Width", Value::from(210.0))])).is_err(), "no height");
}
}
@@ -0,0 +1,388 @@
//! The chosen pages laid on the chosen sheets, as PDF.
use std::collections::BTreeMap;
use anyhow::{Context as _, anyhow, bail};
use lopdf::{Dictionary, Document, Object, ObjectId, Stream, dictionary};
use super::choices::{Choices, Set};
use crate::print::ROUNDING;
/// Catalog entries that name pages no longer there.
const STALE: [&str; 4] = ["Outlines", "OpenAction", "AcroForm", "PageLabels"];
/// A PDF matrix, `[a b c d e f]`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Matrix(pub [f64; 6]);
impl Matrix {
/// This, then `next`.
fn then(self, next: Matrix) -> Matrix {
let ([a, b, c, d, e, f], [g, h, i, j, k, l]) = (self.0, next.0);
Matrix([a * g + b * i, a * h + b * j, c * g + d * i, c * h + d * j, e * g + f * i + k, e * h + f * j + l])
}
pub(super) fn apply(self, (x, y): (f64, f64)) -> (f64, f64) {
let [a, b, c, d, e, f] = self.0;
(a * x + c * y + e, b * x + d * y + f)
}
}
/// A page's box, unit and turn.
pub(super) struct Shown {
corners: [f64; 4],
unit: f64,
turn: i64,
}
impl Shown {
pub(super) fn of(document: &Document, page: ObjectId) -> anyhow::Result<Self> {
let corners = |bounds: &Object| -> anyhow::Result<[f64; 4]> {
let numbers = bounds.as_array()?.iter().map(|n| Ok(f64::from(n.as_float()?))).collect::<anyhow::Result<Vec<_>>>()?;
let [x0, y0, x1, y1] = numbers[..] else {
bail!("a page box has {} numbers", numbers.len());
};
Ok([x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)])
};
let media = corners(inherited(document, page, b"MediaBox")?.ok_or_else(|| anyhow!("a page has no media box"))?)?;
// A crop box is cut to the media box.
let [x0, y0, x1, y1] = match inherited(document, page, b"CropBox")? {
Some(crop) => {
let crop = corners(crop)?;
[crop[0].max(media[0]), crop[1].max(media[1]), crop[2].min(media[2]), crop[3].min(media[3])]
}
None => media,
};
if x1 <= x0 || y1 <= y0 {
bail!("a page's crop box lies outside its media box");
}
// UserUnit is the page's own, never inherited.
let unit = match document.get_dictionary(page)?.get(b"UserUnit") {
Ok(unit) => f64::from(document.dereference(unit)?.1.as_float()?),
Err(_) => 1.0,
};
let turn = inherited(document, page, b"Rotate")?.map_or(Ok(0), Object::as_i64)?.rem_euclid(360);
if !(unit > 0.0 && turn % 90 == 0) {
bail!("a page has a unit of {unit} or a turn of {turn}");
}
Ok(Self { corners: [x0, y0, x1, y1], unit, turn })
}
/// Width and height in points, as it shows.
pub(super) fn size(&self) -> (f64, f64) {
let [x0, y0, x1, y1] = self.corners;
let (width, height) = ((x1 - x0) * self.unit, (y1 - y0) * self.unit);
// A page turned a quarter shows its other side up.
if self.turn % 180 == 90 { (height, width) } else { (width, height) }
}
/// From the page's own space to its shown points.
fn matrix(&self) -> Matrix {
let [x0, y0, x1, y1] = self.corners;
let (w, h) = ((x1 - x0) * self.unit, (y1 - y0) * self.unit);
let unit = Matrix([self.unit, 0.0, 0.0, self.unit, -x0 * self.unit, -y0 * self.unit]);
// Rotate turns the page clockwise as shown.
unit.then(Matrix(match self.turn {
90 => [0.0, -1.0, 1.0, 0.0, 0.0, w],
180 => [-1.0, 0.0, 0.0, -1.0, w, h],
270 => [0.0, 1.0, -1.0, 0.0, h, 0.0],
_ => [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
}))
}
}
/// `key` on the page or its nearest tree node.
pub(super) fn inherited<'a>(document: &'a Document, page: ObjectId, key: &[u8]) -> anyhow::Result<Option<&'a Object>> {
let mut node = document.get_dictionary(page)?;
// A loop in the tree must not hang.
for _ in 0..64 {
if let Ok(value) = node.get(key) {
return Ok(Some(document.dereference(value)?.1));
}
let Ok(parent) = node.get(b"Parent") else {
return Ok(None);
};
node = document.dereference(parent)?.1.as_dict()?;
}
bail!("the page tree is deeper than 64")
}
/// The PDF at `pdf`, laid out as `choices` asks.
pub(super) fn compose(pdf: &std::path::Path, choices: &Choices) -> anyhow::Result<Vec<u8>> {
let mut document = Document::load(pdf)?;
let pages: Vec<ObjectId> = document.get_pages().into_values().collect();
let picked = picked(choices, pages.len())?;
let mut forms = BTreeMap::new();
for &n in &picked {
if let std::collections::btree_map::Entry::Vacant(slot) = forms.entry(n) {
slot.insert(form(&mut document, pages[n])?);
}
}
let shown = picked.iter().map(|&n| Shown::of(&document, pages[n])).collect::<anyhow::Result<Vec<_>>>()?;
let laid = lay(choices, &picked, &shown)?;
let tree = document.new_object_id();
let (width, height) = choices.sheet;
let mut sheets = Vec::with_capacity(laid.len());
for placed in &laid {
let mut drawn = String::new();
let mut objects = Dictionary::new();
for &(n, Matrix([a, b, c, d, e, f])) in placed {
drawn.push_str(&format!("q {a:.4} {b:.4} {c:.4} {d:.4} {e:.4} {f:.4} cm /P{n} Do Q\n"));
objects.set(format!("P{n}"), forms[&n]);
}
let mut stream = Stream::new(Dictionary::new(), drawn.into_bytes());
stream.compress()?;
let contents = document.add_object(stream);
let media: Vec<Object> = vec![0.into(), 0.into(), (width as f32).into(), (height as f32).into()];
let mut sheet = dictionary! {
"Type" => "Page",
"Parent" => tree,
"MediaBox" => media,
"Resources" => dictionary! { "XObject" => objects },
"Contents" => contents,
};
if choices.upside_down {
sheet.set("Rotate", 180);
}
sheets.push(sheet);
}
let kids: Vec<Object> = arrange(choices, sheets.len())?.into_iter().map(|n| document.add_object(sheets[n].clone()).into()).collect();
let count = kids.len() as i64;
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => kids, "Count" => count }.into());
let root = document.trailer.get(b"Root")?.as_reference()?;
let catalog = document.get_object_mut(root)?.as_dict_mut()?;
catalog.set("Pages", tree);
for key in STALE {
catalog.remove(key.as_bytes());
}
document.prune_objects();
let mut bytes = Vec::new();
document.save_to(&mut bytes)?;
Ok(bytes)
}
/// The chosen pages, from zero, in the dialog's order.
fn picked(choices: &Choices, count: usize) -> anyhow::Result<Vec<usize>> {
let last = count.checked_sub(1).context("the document has no pages")? as u32;
let picked: Vec<usize> = match &choices.ranges {
None => (0..count).collect(),
Some(ranges) => ranges.iter().filter(|(from, _)| *from <= last).flat_map(|&(from, to)| from as usize..=to.min(last) as usize).collect(),
};
if picked.is_empty() {
bail!("none of the chosen pages are in the document");
}
Ok(picked)
}
/// Sheets in print order: set, reverse, then copies.
fn arrange(choices: &Choices, sheets: usize) -> anyhow::Result<Vec<usize>> {
let mut order: Vec<usize> = (0..sheets)
.filter(|n| match choices.set {
Set::All => true,
Set::Even => n % 2 == 1,
Set::Odd => n % 2 == 0,
})
.collect();
if order.is_empty() {
bail!("the chosen page set leaves no sheet to print");
}
if choices.reverse {
order.reverse();
}
let copies = choices.copies as usize;
Ok(match choices.collate {
true => order.iter().copied().cycle().take(order.len() * copies).collect(),
false => order.into_iter().flat_map(|n| std::iter::repeat_n(n, copies)).collect(),
})
}
/// Each sheet's pages: which page, and where it goes.
fn lay(choices: &Choices, picked: &[usize], shown: &[Shown]) -> anyhow::Result<Vec<Vec<(usize, Matrix)>>> {
let (width, height) = choices.sheet;
if choices.up == 1 {
return picked
.iter()
.zip(shown)
.map(|(&n, page)| {
let (w, h) = page.size();
let k = choices.scale;
let fits = |across: f64, down: f64| k * across <= width + ROUNDING && k * down <= height + ROUNDING;
// Placed from the sheet's top left; turned left if needed.
let place = match (fits(w, h), fits(h, w)) {
(true, _) => Matrix([k, 0.0, 0.0, k, 0.0, height - k * h]),
(false, true) => Matrix([0.0, k, -k, 0.0, k * h, height - k * w]),
(false, false) => bail!(
"page {} at {:.0}% is larger than the {:.0} × {:.0} mm paper",
n + 1,
k * 100.0,
width * 25.4 / 72.0,
height * 25.4 / 72.0
),
};
Ok(vec![(n, page.matrix().then(place))])
})
.collect();
}
let (cols, rows) = grid(choices.up, choices.sheet, shown);
let (cell_w, cell_h) = (width / f64::from(cols), height / f64::from(rows));
let mut sheets = Vec::new();
for (pages, looks) in picked.chunks(choices.up as usize).zip(shown.chunks(choices.up as usize)) {
let placed = pages.iter().zip(looks).enumerate().map(|(slot, (&n, page))| {
let (col, row) = cell(slot as u32, (cols, rows), choices);
let (w, h) = page.size();
let k = (cell_w / w).min(cell_h / h) * choices.scale;
// Centred in its cell.
let left = f64::from(col) * cell_w + (cell_w - k * w) / 2.0;
let bottom = height - f64::from(row + 1) * cell_h + (cell_h - k * h) / 2.0;
(n, page.matrix().then(Matrix([k, 0.0, 0.0, k, left, bottom])))
});
sheets.push(placed.collect());
}
Ok(sheets)
}
/// Columns and rows for `up` pages; the largest fit best.
fn grid(up: u32, (width, height): (f64, f64), shown: &[Shown]) -> (u32, u32) {
let (w, h) = shown.iter().map(Shown::size).fold((0.0, 0.0), |(a, b), (w, h)| (w.max(a), h.max(b)));
let fit = |(cols, rows): (u32, u32)| (width / f64::from(cols) / w).min(height / f64::from(rows) / h);
let grids = (1..=up).filter(|&cols| up.is_multiple_of(cols)).map(|cols| (cols, up / cols));
grids.fold((up, 1), |best, grid| if fit(grid) > fit(best) { grid } else { best })
}
/// Slot `n`'s column and row, by the dialog's n-up order.
fn cell(n: u32, (cols, rows): (u32, u32), choices: &Choices) -> (u32, u32) {
let order = choices.order;
let (col, row) = if order.across_first { (n % cols, n / cols) } else { (n / rows, n % rows) };
(if order.leftward { cols - 1 - col } else { col }, if order.upward { rows - 1 - row } else { row })
}
/// The page as a form, printable annotations drawn on top.
fn form(document: &mut Document, page: ObjectId) -> anyhow::Result<ObjectId> {
let mut drawn = b"q\n".to_vec();
for content in document.get_page_contents(page) {
drawn.extend(document.get_object(content)?.as_stream()?.decompressed_content()?);
drawn.push(b'\n');
}
drawn.extend(b"Q\n");
let mut resources = match inherited(document, page, b"Resources")? {
Some(resources) => resources.as_dict()?.clone(),
None => Dictionary::new(),
};
let mut objects = match resources.get(b"XObject") {
Ok(objects) => document.dereference(objects)?.1.as_dict()?.clone(),
Err(_) => Dictionary::new(),
};
let mut n = 0;
for (look, place) in appearances(document, page)? {
// Names the page already uses stay its own.
while objects.has(format!("ElyAnnot{n}").as_bytes()) {
n += 1;
}
let [a, b, c, d, e, f] = place.0;
drawn.extend(format!("q {a:.4} {b:.4} {c:.4} {d:.4} {e:.4} {f:.4} cm /ElyAnnot{n} Do Q\n").into_bytes());
objects.set(format!("ElyAnnot{n}"), look);
let look = document.get_object_mut(look)?.as_stream_mut()?;
if look.dict.get(b"Subtype").is_err() {
look.dict.set("Subtype", "Form");
}
}
resources.set("XObject", objects);
let [x0, y0, x1, y1] = Shown::of(document, page)?.corners;
let bounds: Vec<Object> = [x0, y0, x1, y1].into_iter().map(|n| (n as f32).into()).collect();
let mut dict = dictionary! { "Type" => "XObject", "Subtype" => "Form", "BBox" => bounds, "Resources" => resources };
if let Some(group) = inherited(document, page, b"Group")? {
dict.set("Group", group.clone());
}
let mut stream = Stream::new(dict, drawn);
stream.compress()?;
Ok(document.add_object(stream))
}
/// Printable annotations' looks, and where each goes on the page.
fn appearances(document: &Document, page: ObjectId) -> anyhow::Result<Vec<(ObjectId, Matrix)>> {
let Ok(annots) = document.get_dictionary(page)?.get(b"Annots") else {
return Ok(Vec::new());
};
let turn = Shown::of(document, page)?.turn;
let mut looks = Vec::new();
for annot in document.dereference(annots)?.1.as_array()? {
let annot = document.dereference(annot)?.1.as_dict()?;
let flags = annot.get(b"F").and_then(Object::as_i64).unwrap_or(0);
// Hidden, or not marked for print: left out.
if flags & 2 != 0 || flags & 4 == 0 {
continue;
}
let Some(look) = appearance(document, annot)? else {
continue;
};
let stream = document.get_object(look)?.as_stream()?;
let numbers = |object: &Object| -> anyhow::Result<Vec<f64>> {
document.dereference(object)?.1.as_array()?.iter().map(|n| Ok(f64::from(n.as_float()?))).collect()
};
let bounds = numbers(stream.dict.get(b"BBox")?)?;
let rect = numbers(annot.get(b"Rect")?)?;
let own = match stream.dict.get(b"Matrix") {
Ok(matrix) => Matrix(numbers(matrix)?.try_into().map_err(|_| anyhow!("an appearance matrix is not six numbers"))?),
Err(_) => Matrix([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
};
let ([bx0, by0, bx1, by1], [rx0, ry0, rx1, ry1]) = (four(&bounds)?, four(&rect)?);
// The look's box, through its matrix, fills the rectangle.
let corners = [(bx0, by0), (bx1, by0), (bx0, by1), (bx1, by1)].map(|corner| own.apply(corner));
let (lx0, ly0) = corners.iter().fold((f64::MAX, f64::MAX), |(x, y), &(a, b)| (x.min(a), y.min(b)));
let (lx1, ly1) = corners.iter().fold((f64::MIN, f64::MIN), |(x, y), &(a, b)| (x.max(a), y.max(b)));
if lx1 - lx0 <= 0.0 || ly1 - ly0 <= 0.0 {
continue;
}
let (sx, sy) = ((rx1 - rx0).abs() / (lx1 - lx0), (ry1 - ry0).abs() / (ly1 - ly0));
let mut place = Matrix([sx, 0.0, 0.0, sy, rx0.min(rx1) - sx * lx0, ry0.min(ry1) - sy * ly0]);
// NoRotate: upright as shown, turned about its top left.
if flags & 16 != 0 {
place = place.then(unturned(turn, (rx0.min(rx1), ry0.max(ry1))));
}
looks.push((look, place));
}
Ok(looks)
}
/// Turns back a page's `turn` about `(x, y)`.
fn unturned(turn: i64, (x, y): (f64, f64)) -> Matrix {
let (cos, sin) = match turn {
90 => (0.0, 1.0),
180 => (-1.0, 0.0),
270 => (0.0, -1.0),
_ => (1.0, 0.0),
};
Matrix([cos, sin, -sin, cos, x - cos * x + sin * y, y - sin * x - cos * y])
}
fn four(numbers: &[f64]) -> anyhow::Result<[f64; 4]> {
numbers.try_into().map_err(|_| anyhow!("an annotation's box is not four numbers"))
}
/// An annotation's normal look in its state; None draws nothing.
fn appearance(document: &Document, annot: &Dictionary) -> anyhow::Result<Option<ObjectId>> {
let subtype = annot.get(b"Subtype")?.as_name()?;
let normal = match annot.get(b"AP") {
Ok(looks) => document.dereference(looks)?.1.as_dict()?.get(b"N").ok(),
Err(_) => None,
};
let Some(normal) = normal else {
// Links and popups have no look of their own.
if matches!(subtype, b"Link" | b"Popup") {
return Ok(None);
}
bail!("a printable {} annotation has no appearance", String::from_utf8_lossy(subtype));
};
if let Ok(id) = normal.as_reference()
&& document.get_object(id)?.as_stream().is_ok()
{
return Ok(Some(id));
}
// A look per state: the one /AS names, if any.
let states = document.dereference(normal)?.1.as_dict()?;
let state = annot.get(b"AS").and_then(Object::as_name).context("an annotation with states names none")?;
Ok(states.get(state).ok().map(Object::as_reference).transpose()?)
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,307 @@
use lopdf::{Document, Object, Stream, content::Content, dictionary};
use super::{Choices, Set, Shown, compose};
use crate::print::linux::choices::Order;
/// Pages with these boxes, each with these extra entries.
fn pages(boxes: &[([f32; 4], lopdf::Dictionary)]) -> (tempfile::TempDir, std::path::PathBuf) {
let mut document = Document::with_version("1.7");
let tree = document.new_object_id();
let kids: Vec<Object> = boxes
.iter()
.map(|(corners, extra)| {
let content = document.add_object(Stream::new(dictionary! {}, b"0 g 0 0 10 10 re f".to_vec()));
let media: Vec<Object> = corners.iter().map(|&n| n.into()).collect();
let mut page = dictionary! { "Type" => "Page", "Parent" => tree, "MediaBox" => media, "Contents" => content };
page.extend(extra);
document.add_object(page).into()
})
.collect();
let count = kids.len() as i64;
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => kids, "Count" => count }.into());
let catalog = document.add_object(dictionary! { "Type" => "Catalog", "Pages" => tree });
document.trailer.set("Root", catalog);
let folder = tempfile::tempdir().expect("a folder");
let pdf = folder.path().join("pages.pdf");
document.save(&pdf).expect("saves");
(folder, pdf)
}
fn plain(box_: [f32; 4]) -> ([f32; 4], lopdf::Dictionary) {
(box_, dictionary! {})
}
fn choices(sheet: (f64, f64)) -> Choices {
Choices {
ranges: None,
set: Set::All,
reverse: false,
copies: 1,
collate: true,
scale: 1.0,
up: 1,
order: Order { across_first: true, leftward: false, upward: false },
sheet,
upside_down: false,
}
}
/// A sheet's size; each form's width and placed corners.
type Sheet = ((i64, i64), Vec<(i64, [i64; 4])>);
fn sheets(bytes: &[u8]) -> Vec<Sheet> {
let document = Document::load_mem(bytes).expect("a PDF");
let round = |n: f64| n.round() as i64;
document
.get_pages()
.into_values()
.map(|page| {
let dict = document.get_dictionary(page).expect("a page");
let media: Vec<f64> = dict.get(b"MediaBox").and_then(Object::as_array).expect("a box").iter().map(|n| f64::from(n.as_float().expect("a number"))).collect();
let content = Content::decode(&document.get_page_content(page)).expect("content");
let objects = dict.get(b"Resources").and_then(Object::as_dict).and_then(|r| r.get(b"XObject")).and_then(Object::as_dict).expect("forms");
let mut matrix = [1.0; 6];
let mut placed = Vec::new();
for op in content.operations {
match op.operator.as_str() {
"cm" => matrix = op.operands.iter().map(|n| f64::from(n.as_float().expect("a number"))).collect::<Vec<_>>().try_into().expect("six"),
"Do" => {
let form = objects.get(op.operands[0].as_name().expect("a name")).and_then(Object::as_reference).expect("a form");
let bounds: Vec<f64> = document.get_object(form).and_then(Object::as_stream).expect("a stream").dict.get(b"BBox").and_then(Object::as_array).expect("a box").iter().map(|n| f64::from(n.as_float().expect("a number"))).collect();
let m = super::Matrix(matrix);
let corners = [(bounds[0], bounds[1]), (bounds[2], bounds[3])].map(|corner| m.apply(corner));
let (x0, x1) = (corners[0].0.min(corners[1].0), corners[0].0.max(corners[1].0));
let (y0, y1) = (corners[0].1.min(corners[1].1), corners[0].1.max(corners[1].1));
placed.push((round(bounds[2] - bounds[0]), [x0, y0, x1, y1].map(round)));
}
_ => {}
}
}
((round(media[2]), round(media[3])), placed)
})
.collect()
}
#[test]
fn a_user_unit_scales_the_page() {
let (_folder, pdf) = pages(&[([0.0, 0.0, 421.0, 297.5], dictionary! { "UserUnit" => 2 })]);
let document = Document::load(&pdf).expect("loads");
let page = document.get_pages()[&1];
assert_eq!(Shown::of(&document, page).expect("reads").size(), (842.0, 595.0));
// On A4 landscape it fills the sheet, from its corner.
let laid = sheets(&compose(&pdf, &choices((842.0, 595.0))).expect("composes"));
assert_eq!(laid, [((842, 595), vec![(421, [0, 0, 842, 595])])]);
}
#[test]
fn chosen_pages_print_scaled_from_the_top_left() {
let (_folder, pdf) = pages(&[plain([0.0, 0.0, 595.0, 842.0]), plain([0.0, 0.0, 420.0, 595.0]), plain([0.0, 0.0, 300.0, 300.0])]);
let only_second = Choices { ranges: Some(vec![(1, 1)]), scale: 0.5, ..choices((595.0, 842.0)) };
let laid = sheets(&compose(&pdf, &only_second).expect("composes"));
assert_eq!(laid, [((595, 842), vec![(420, [0, 545, 210, 842])])]);
}
#[test]
fn a_page_that_fits_only_turned_turns_left() {
let (_folder, pdf) = pages(&[plain([0.0, 0.0, 842.0, 595.0])]);
let bytes = compose(&pdf, &choices((595.0, 842.0))).expect("composes");
assert_eq!(sheets(&bytes), [((595, 842), vec![(842, [0, 0, 595, 842])])]);
// Its top left lands at the sheet's bottom left.
let document = Document::load_mem(&bytes).expect("a PDF");
let content = Content::decode(&document.get_page_content(document.get_pages()[&1])).expect("content");
let cm = content.operations.iter().find(|op| op.operator == "cm").expect("a matrix");
let m: Vec<f64> = cm.operands.iter().map(|n| f64::from(n.as_float().expect("a number"))).collect();
let corner = super::Matrix(m.try_into().expect("six")).apply((0.0, 595.0));
assert_eq!((corner.0.round(), corner.1.round()), (0.0, 0.0));
}
#[test]
fn a_crop_box_past_the_media_box_is_cut_to_it() {
let crop = |corners: [i64; 4]| -> Object { corners.map(Object::from).to_vec().into() };
let (_folder, pdf) = pages(&[([0.0, 0.0, 612.0, 792.0], dictionary! { "CropBox" => crop([0, 0, 700, 792]) })]);
// The form's box clips at 612: x 650 stays hidden.
let laid = sheets(&compose(&pdf, &choices((612.0, 792.0))).expect("composes"));
assert_eq!(laid, [((612, 792), vec![(612, [0, 0, 612, 792])])]);
let (_folder, pdf) = pages(&[([0.0, 0.0, 612.0, 792.0], dictionary! { "CropBox" => crop([700, 0, 800, 792]) })]);
assert!(compose(&pdf, &choices((612.0, 792.0))).is_err(), "nothing left to show");
}
#[test]
fn a_rotated_page_shows_turned() {
let (_folder, pdf) = pages(&[([0.0, 0.0, 595.0, 842.0], dictionary! { "Rotate" => 90 })]);
let document = Document::load(&pdf).expect("loads");
let shown = Shown::of(&document, document.get_pages()[&1]).expect("reads");
assert_eq!(shown.size(), (842.0, 595.0));
// Turned clockwise, the page's top left becomes its top right.
assert_eq!(shown.matrix().apply((0.0, 842.0)), (842.0, 595.0));
}
#[test]
fn too_large_a_page_fails() {
let (_folder, pdf) = pages(&[plain([0.0, 0.0, 1000.0, 600.0])]);
let error = compose(&pdf, &choices((595.0, 842.0))).expect_err("does not fit");
assert!(error.to_string().contains("page 1 at 100% is larger than the 210 × 297 mm paper"), "{error}");
let (_folder, pdf) = pages(&[plain([0.0, 0.0, 595.0, 842.0])]);
assert!(compose(&pdf, &Choices { scale: 2.0, ..choices((595.0, 842.0)) }).is_err(), "scaled past the paper");
// A4 is 595.28 points wide: 600 would lose its edge.
let a4 = (210.0 * 72.0 / 25.4, 297.0 * 72.0 / 25.4);
let (_folder, pdf) = pages(&[plain([0.0, 0.0, 600.0, 842.0])]);
assert!(compose(&pdf, &choices(a4)).is_err(), "a page 1.6 mm wider than A4");
// A4 in whole points still takes an A4 page.
let (_folder, pdf) = pages(&[plain([0.0, 0.0, a4.0 as f32, a4.1 as f32])]);
assert!(compose(&pdf, &choices((595.0, 842.0))).is_ok(), "rounding is no overflow");
}
#[test]
fn an_open_gtk_range_runs_to_the_last_page() {
use zbus::zvariant::{OwnedValue, Value};
let widths = [100.0, 110.0, 120.0];
let boxes: Vec<_> = widths.iter().map(|&w| plain([0.0, 0.0, w, 100.0])).collect();
let (_folder, pdf) = pages(&boxes);
let owned = |value: Value<'static>| OwnedValue::try_from(value).expect("owned");
let settings = [("print-pages", "ranges"), ("page-ranges", "1--1")].map(|(key, value)| (key.to_string(), owned(Value::from(value))));
let setup = [("Width", Value::from(210.0)), ("Height", Value::from(297.0)), ("Orientation", Value::from("portrait"))];
let setup = setup.map(|(key, value)| (key.to_string(), owned(value)));
let chosen = super::super::choices::chosen(&settings.into(), &setup.into()).expect("reads");
let printed: Vec<i64> = sheets(&compose(&pdf, &chosen).expect("composes")).iter().map(|sheet| sheet.1[0].0).collect();
assert_eq!(printed, [110, 120], "pages two and three");
}
#[test]
fn four_up_fills_cells_in_the_chosen_order() {
let sizes = [100.0, 110.0, 120.0, 130.0, 140.0];
let boxes: Vec<_> = sizes.iter().map(|&w| plain([0.0, 0.0, w, w * 1.5])).collect();
let (_folder, pdf) = pages(&boxes);
let up = Choices { up: 4, ..choices((400.0, 600.0)) };
let laid = sheets(&compose(&pdf, &up).expect("composes"));
assert_eq!(laid.len(), 2, "five pages, four a sheet");
let corner = |(width, [x0, _, _, y1]): &(i64, [i64; 4])| (*width, x0 / 200, (600 - y1) / 300);
let cells: Vec<_> = laid[0].1.iter().map(corner).collect();
assert_eq!(cells, [(100, 0, 0), (110, 1, 0), (120, 0, 1), (130, 1, 1)]);
// Every page fills its 200 × 300 cell, centred.
assert_eq!(laid[1].1, [(140, [0, 300, 200, 600])]);
let upward = Choices { order: Order { across_first: false, leftward: true, upward: true }, ..up };
let laid = sheets(&compose(&pdf, &upward).expect("composes"));
let cells: Vec<_> = laid[0].1.iter().map(corner).collect();
assert_eq!(cells, [(100, 1, 1), (110, 1, 0), (120, 0, 1), (130, 0, 0)]);
}
#[test]
fn sets_order_and_copies_choose_the_sheets() {
let widths = [100.0, 110.0, 120.0];
let boxes: Vec<_> = widths.iter().map(|&w| plain([0.0, 0.0, w, 100.0])).collect();
let (_folder, pdf) = pages(&boxes);
let order = |chosen: Choices| -> Vec<i64> { sheets(&compose(&pdf, &chosen).expect("composes")).iter().map(|sheet| sheet.1[0].0).collect() };
let base = choices((595.0, 842.0));
assert_eq!(order(Choices { set: Set::Odd, ..choices((595.0, 842.0)) }), [100, 120]);
assert_eq!(order(Choices { set: Set::Even, ..choices((595.0, 842.0)) }), [110]);
assert_eq!(order(Choices { reverse: true, ..choices((595.0, 842.0)) }), [120, 110, 100]);
assert_eq!(order(Choices { copies: 2, ..choices((595.0, 842.0)) }), [100, 110, 120, 100, 110, 120]);
assert_eq!(order(Choices { copies: 2, collate: false, ..base }), [100, 100, 110, 110, 120, 120]);
let none = compose(&pdf, &Choices { ranges: Some(vec![(5, 9)]), ..choices((595.0, 842.0)) });
assert!(none.is_err(), "no chosen page is in the document");
}
#[test]
fn printable_annotations_are_drawn_where_they_sit() {
let mut document = Document::with_version("1.7");
let look = document.add_object(Stream::new(
dictionary! { "Subtype" => "Form", "BBox" => vec![0.into(), 0.into(), 10.into(), 20.into()] },
b"0 g 0 0 10 20 re f".to_vec(),
));
let rect = |x: i64| -> Object { vec![x.into(), 100.into(), (x + 30).into(), 160.into()].into() };
let shown = dictionary! { "Subtype" => "Square", "F" => 4, "Rect" => rect(50), "AP" => dictionary! { "N" => look } };
let unprinted = dictionary! { "Subtype" => "Square", "F" => 0, "Rect" => rect(200), "AP" => dictionary! { "N" => look } };
let annots: Vec<Object> = vec![document.add_object(shown).into(), document.add_object(unprinted).into()];
let tree = document.new_object_id();
let content = document.add_object(Stream::new(dictionary! {}, Vec::new()));
let media: Vec<Object> = vec![0.into(), 0.into(), 595.into(), 842.into()];
let page = document.add_object(dictionary! { "Type" => "Page", "Parent" => tree, "MediaBox" => media, "Contents" => content, "Annots" => annots });
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => vec![page.into()], "Count" => 1 }.into());
let looks = super::appearances(&document, page).expect("reads");
assert_eq!(looks.len(), 1, "only the printable one");
let (id, place) = looks[0];
assert_eq!(id, look);
assert_eq!((place.apply((0.0, 0.0)), place.apply((10.0, 20.0))), ((50.0, 100.0), (80.0, 160.0)));
}
/// One A4 page, turned `rotate`: its annotations and own forms.
fn annotated(rotate: i64, build: impl FnOnce(&mut Document) -> (Vec<lopdf::Dictionary>, lopdf::Dictionary)) -> (Document, lopdf::ObjectId) {
let mut document = Document::with_version("1.7");
let (annots, own) = build(&mut document);
let annots: Vec<Object> = annots.into_iter().map(|annot| document.add_object(annot).into()).collect();
let tree = document.new_object_id();
let content = document.add_object(Stream::new(dictionary! {}, b"/ElyAnnot0 Do".to_vec()));
let media: Vec<Object> = vec![0.into(), 0.into(), 595.into(), 842.into()];
let resources = dictionary! { "XObject" => own };
let page = dictionary! { "Type" => "Page", "Parent" => tree, "MediaBox" => media, "Contents" => content, "Annots" => annots, "Rotate" => rotate, "Resources" => resources };
let page = document.add_object(page);
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => vec![page.into()], "Count" => 1 }.into());
(document, page)
}
fn square(document: &mut Document, flags: i64) -> lopdf::Dictionary {
let look = document.add_object(Stream::new(
dictionary! { "Subtype" => "Form", "BBox" => vec![0.into(), 0.into(), 100.into(), 80.into()] },
b"1 0 0 rg 0 0 100 80 re f".to_vec(),
));
let rect: Vec<Object> = vec![50.into(), 100.into(), 150.into(), 180.into()];
dictionary! { "Subtype" => "Square", "F" => flags, "Rect" => rect, "AP" => dictionary! { "N" => look } }
}
#[test]
fn a_no_rotate_annotation_stays_upright_about_its_top_left() {
let (document, page) = annotated(90, |document| (vec![square(document, 20)], dictionary! {}));
let (_, place) = super::appearances(&document, page).expect("reads")[0];
let shown = Shown::of(&document, page).expect("reads").matrix();
let at = |corner| {
let (x, y) = shown.apply(place.apply(corner));
(x.round(), y.round())
};
// Shown: its top left stays put, its top runs right.
assert_eq!(at((0.0, 80.0)), (180.0, 545.0));
assert_eq!(at((100.0, 80.0)), (280.0, 545.0));
assert_eq!(at((0.0, 0.0)), (180.0, 465.0));
// Without the flag, it turns with the page.
let (document, page) = annotated(90, |document| (vec![square(document, 4)], dictionary! {}));
let (_, place) = super::appearances(&document, page).expect("reads")[0];
let (x, y) = shown.apply(place.apply((0.0, 80.0)));
assert_eq!((x.round(), y.round()), (180.0, 545.0));
let (x, y) = shown.apply(place.apply((100.0, 80.0)));
assert_eq!((x.round(), y.round()), (180.0, 445.0));
}
#[test]
fn an_annotation_name_leaves_the_page_its_own() {
let mut blue = None;
let (mut document, page) = annotated(0, |document| {
let form = dictionary! { "Subtype" => "Form", "BBox" => vec![0.into(), 0.into(), 1.into(), 1.into()] };
let own = *blue.insert(document.add_object(Stream::new(form, b"0 0 1 rg".to_vec())));
(vec![square(document, 4)], dictionary! { "ElyAnnot0" => own })
});
let blue = blue.expect("made");
let form = super::form(&mut document, page).expect("a form");
let form = document.get_object(form).and_then(Object::as_stream).expect("a stream");
let objects = form.dict.get(b"Resources").and_then(Object::as_dict).and_then(|r| r.get(b"XObject")).and_then(Object::as_dict).expect("forms");
assert_eq!(objects.get(b"ElyAnnot0").and_then(Object::as_reference).expect("kept"), blue);
let drawn = String::from_utf8(form.decompressed_content().expect("content")).expect("text");
assert!(drawn.contains("/ElyAnnot0 Do") && drawn.contains("/ElyAnnot1 Do"), "{drawn}");
let added = objects.get(b"ElyAnnot1").and_then(Object::as_reference).expect("the annotation's");
assert_ne!(added, blue);
}
#[test]
fn a_printable_annotation_without_a_look_fails() {
let rect: Vec<Object> = vec![50.into(), 100.into(), 150.into(), 180.into()];
let bare = dictionary! { "Subtype" => "Square", "F" => 4, "Rect" => rect.clone() };
let (document, page) = annotated(0, |_| (vec![bare], dictionary! {}));
let error = super::appearances(&document, page).expect_err("cannot be drawn");
assert!(error.to_string().contains("a printable Square annotation has no appearance"), "{error}");
// A link draws nothing; an unset state draws nothing.
let link = dictionary! { "Subtype" => "Link", "F" => 4, "Rect" => rect.clone() };
let (mut document, page) = annotated(0, |_| (vec![link], dictionary! {}));
assert!(super::appearances(&document, page).expect("reads").is_empty());
let on = document.add_object(Stream::new(dictionary! { "BBox" => vec![0.into(), 0.into(), 1.into(), 1.into()] }, Vec::new()));
let off = dictionary! { "Subtype" => "Widget", "F" => 4, "Rect" => rect, "AS" => "Off", "AP" => dictionary! { "N" => dictionary! { "Yes" => on } } };
assert_eq!(super::appearance(&document, &off).expect("reads"), None);
}
+69
View File
@@ -0,0 +1,69 @@
//! macOS: PDFKit's print operation and panel.
use std::path::Path;
/// Shows the print panel for `pdf`; false when cancelled.
pub async fn print_pdf(pdf: &Path) -> anyhow::Result<bool> {
use objc2::{AllocAnyThread, MainThreadMarker};
use objc2_app_kit::{NSPaperOrientation, NSPrintInfo, NSPrintJobSavingURL, NSPrintSaveJob};
use objc2_foundation::{NSCopying, NSSize, NSString, NSURL};
use objc2_pdf_kit::{PDFDisplayBox, PDFDocument, PDFPrintScalingMode};
let mtm =
MainThreadMarker::new().ok_or_else(|| anyhow::anyhow!("printing needs the main thread"))?;
let url = NSURL::fileURLWithPath(&NSString::from_str(&pdf.to_string_lossy()));
let document = unsafe { PDFDocument::initWithURL(PDFDocument::alloc(), &url) }
.ok_or_else(|| anyhow::anyhow!("{} is no readable PDF", pdf.display()))?;
let sizes: Vec<(f64, f64)> = (0..unsafe { document.pageCount() })
.filter_map(|n| unsafe { document.pageAtIndex(n) })
.map(|page| {
let size = unsafe { page.boundsForBox(PDFDisplayBox::MediaBox) }.size;
// A page turned a quarter shows its other side up.
match unsafe { page.rotation() }.rem_euclid(180) == 90 {
true => (size.height, size.width),
false => (size.width, size.height),
}
})
.collect();
let (width, height) = super::paper_for(&sizes).ok_or_else(|| anyhow::anyhow!("{} has no pages", pdf.display()))?;
let paper = NSSize::new(width, height);
if let Some(scale) = crate::env::value("ELY_PRINT_SHARED_SCALE") {
// Tests start from a shared scale the copy must undo.
NSPrintInfo::sharedPrintInfo().setScalingFactor(scale.parse()?);
}
// The PDF already holds the document's paper, margins and scale.
let info = NSPrintInfo::sharedPrintInfo().copy();
info.setScalingFactor(1.0);
info.setOrientation(match paper.width > paper.height {
true => NSPaperOrientation::Landscape,
false => NSPaperOrientation::Portrait,
});
info.setPaperSize(paper);
info.setTopMargin(0.0);
info.setBottomMargin(0.0);
info.setLeftMargin(0.0);
info.setRightMargin(0.0);
log::info!("print: paper {}x{} points", paper.width, paper.height);
let operation = unsafe {
document.printOperationForPrintInfo_scalingMode_autoRotate(
Some(&info),
PDFPrintScalingMode::PageScaleNone,
true,
mtm,
)
}
.ok_or_else(|| anyhow::anyhow!("no print operation for {}", pdf.display()))?;
// Tests read the real job: saved to a file.
let saved = crate::env::value("ELY_PRINT_TO");
if let Some(to) = &saved {
let info = operation.printInfo();
info.setJobDisposition(unsafe { NSPrintSaveJob });
let to = NSURL::fileURLWithPath(&NSString::from_str(to));
unsafe { info.dictionary().insert(NSPrintJobSavingURL, &to) };
}
operation.setShowsPrintPanel(saved.is_none());
operation.setShowsProgressPanel(saved.is_none());
let printed = operation.runOperation();
log::info!("print: the panel closed, printed {printed}");
Ok(printed)
}
+375
View File
@@ -0,0 +1,375 @@
//! Windows: the system print dialog; Windows' PDF renderer draws.
use std::path::Path;
use anyhow::{Context as _, anyhow, bail};
use windows::{
Data::Pdf::{PdfDocument, PdfPage, PdfPageRenderOptions},
Foundation::{Rect, Size},
Graphics::Imaging::BitmapEncoder,
Storage::{
StorageFile,
Streams::{DataReader, InMemoryRandomAccessStream},
},
Win32::{
Foundation::ERROR_CANCELLED,
Graphics::Gdi::{
BITMAPINFO, CreateDCW, DIB_RGB_COLORS, DeleteDC, GM_ADVANCED, GetDeviceCaps, HDC, LOGPIXELSX, LOGPIXELSY,
PHYSICALHEIGHT, PHYSICALOFFSETX, PHYSICALOFFSETY, PHYSICALWIDTH, SRCCOPY, SetGraphicsMode, SetWorldTransform,
StretchDIBits, XFORM,
},
Storage::Xps::{AbortDoc, DOCINFOW, EndDoc, EndPage, StartDocW, StartPage},
System::WinRT::{RO_INIT_MULTITHREADED, RoInitialize, RoUninitialize},
UI::{
Controls::Dialogs::{
PD_NOCURRENTPAGE, PD_NOSELECTION, PD_PAGENUMS, PD_PRINTTOFILE, PD_RESULT_PRINT, PD_USEDEVMODECOPIESANDCOLLATE,
PRINTDLGEX_FLAGS, PRINTDLGEXW, PRINTPAGERANGE, PrintDlgExW, START_PAGE_GENERAL,
},
Input::KeyboardAndMouse::GetActiveWindow,
},
},
core::{HSTRING, PCWSTR},
};
mod printer;
use printer::{Devmode, default_printer, device, free, global, names, settings};
/// The printer tests write their job to a file with.
const TO_FILE: &str = "Microsoft Print to PDF";
/// Bytes one rendered band may take.
const BAND: usize = 32 << 20;
/// The probe's width, in pixels asked.
const PROBE: u32 = 200;
/// Shows the print dialog, then prints; false when cancelled.
pub async fn print_pdf(pdf: &Path) -> anyhow::Result<bool> {
let pdf = std::path::absolute(pdf)?;
let sizes = {
let pdf = pdf.clone();
super::off(move || winrt(|| sizes(&open(&pdf)?))).await?
};
let paper = super::paper_for(&sizes).ok_or_else(|| anyhow!("{} has no pages", pdf.display()))?;
log::info!("print: paper {}x{} points", paper.0, paper.1);
let every = (0..sizes.len() as u32).collect();
// Tests read the real job: written by the file printer.
let job = match crate::env::value("ELY_PRINT_TO") {
Some(to) => {
let printer = wide(TO_FILE);
let settings = settings(&printer, paper)?;
Job { printer, settings, pages: every, output: Some(wide(&to)) }
}
None => match ask(every, paper)? {
Some(job) => job,
None => {
log::info!("print: the dialog was cancelled");
return Ok(false);
}
},
};
let printed = super::off(move || winrt(|| job.print(&pdf))).await?;
log::info!("print: the job ended, printed {printed}");
Ok(printed)
}
/// What the dialog chose.
struct Job {
printer: Vec<u16>,
settings: Devmode,
pages: Vec<u32>,
output: Option<Vec<u16>>,
}
impl Job {
/// Draws the chosen pages; false when its save prompt cancels.
fn print(self, pdf: &Path) -> anyhow::Result<bool> {
let document = open(pdf)?;
let dc = unsafe { CreateDCW(PCWSTR::null(), PCWSTR(self.printer.as_ptr()), PCWSTR::null(), Some(self.settings.as_ptr())) };
if dc.is_invalid() {
bail!("the printer cannot be opened: {}", std::io::Error::last_os_error());
}
let printed = self.draw(&document, dc);
if !unsafe { DeleteDC(dc) }.as_bool() {
log::warn!("print: the printer's context stays open");
}
printed
}
fn draw(&self, document: &PdfDocument, dc: HDC) -> anyhow::Result<bool> {
// The driver may have swapped our paper; nothing is cut.
let paper = Paper::of(dc);
for &n in &self.pages {
let size = document.GetPage(n)?.Size()?;
if paper.fit(size).is_none() {
let (width, height) = paper.millimetres();
let mm = |dips: f32| f64::from(dips) * 25.4 / 96.0;
bail!(
"page {} is {:.0} × {:.0} mm; the printer's paper is {width:.0} × {height:.0} mm",
n + 1,
mm(size.Width),
mm(size.Height)
);
}
}
let name = wide("Ely Office");
let info = DOCINFOW {
cbSize: size_of::<DOCINFOW>() as i32,
lpszDocName: PCWSTR(name.as_ptr()),
lpszOutput: self.output.as_ref().map_or(PCWSTR::null(), |output| PCWSTR(output.as_ptr())),
..Default::default()
};
if unsafe { StartDocW(dc, &info) } <= 0 {
let error = std::io::Error::last_os_error();
if error.raw_os_error() == Some(ERROR_CANCELLED.0 as i32) {
return Ok(false);
}
bail!("the printer refused the job: {error}");
}
let drawn = scale(document).and_then(|scale| self.pages.iter().try_for_each(|&n| page(document, n, dc, &paper, scale)));
if let Err(error) = drawn {
unsafe { AbortDoc(dc) };
return Err(error);
}
if unsafe { EndDoc(dc) } <= 0 {
bail!("the printer did not finish the job: {}", std::io::Error::last_os_error());
}
Ok(true)
}
}
/// Shows the dialog over the active window; None when cancelled.
fn ask(every: Vec<u32>, paper: (f64, f64)) -> anyhow::Result<Option<Job>> {
let owner = unsafe { GetActiveWindow() };
if owner.is_invalid() {
bail!("no window is active to own the print dialog");
}
let printer = default_printer()?;
let settings = settings(&printer, paper)?;
let mut ranges = [PRINTPAGERANGE::default(); 32];
let mut dialog = PRINTDLGEXW {
lStructSize: size_of::<PRINTDLGEXW>() as u32,
hwndOwner: owner,
hDevMode: global(settings.bytes())?,
hDevNames: global(&names(&printer))?,
Flags: PD_NOSELECTION | PD_NOCURRENTPAGE | PD_USEDEVMODECOPIESANDCOLLATE,
nMaxPageRanges: ranges.len() as u32,
lpPageRanges: ranges.as_mut_ptr(),
nMinPage: 1,
nMaxPage: every.len() as u32,
nCopies: 1,
nStartPage: START_PAGE_GENERAL,
..Default::default()
};
let shown = unsafe { PrintDlgExW(&mut dialog) };
let chosen = shown.map_err(anyhow::Error::from).and_then(|()| {
if dialog.dwResultAction != PD_RESULT_PRINT {
return Ok(None);
}
let (pages, output) = chosen(dialog.Flags, &ranges[..dialog.nPageRanges as usize], every.len() as u32);
Ok(Some(Job { printer: device(dialog.hDevNames)?, settings: Devmode::copied(dialog.hDevMode)?, pages, output }))
});
// The dialog may replace either block; we free both.
free(dialog.hDevMode)?;
free(dialog.hDevNames)?;
chosen
}
/// The pages and output the dialog's answer chose.
fn chosen(flags: PRINTDLGEX_FLAGS, ranges: &[PRINTPAGERANGE], count: u32) -> (Vec<u32>, Option<Vec<u16>>) {
let pages = match flags.contains(PD_PAGENUMS) {
true => ranges.iter().flat_map(|range| range.nFromPage.max(1) - 1..range.nToPage.min(count)).collect(),
false => (0..count).collect(),
};
// "FILE:" makes the spooler ask for the file's name.
let output = flags.contains(PD_PRINTTOFILE).then(|| wide("FILE:"));
(pages, output)
}
/// The printer's paper, in its device pixels.
struct Paper {
dpi: (i32, i32),
size: (i32, i32),
offset: (i32, i32),
}
impl Paper {
fn of(dc: HDC) -> Self {
let caps = |index| unsafe { GetDeviceCaps(Some(dc), index) };
Self {
dpi: (caps(LOGPIXELSX), caps(LOGPIXELSY)),
size: (caps(PHYSICALWIDTH), caps(PHYSICALHEIGHT)),
offset: (caps(PHYSICALOFFSETX), caps(PHYSICALOFFSETY)),
}
}
/// Pixels, and whether it turns; None if too large.
fn fit(&self, size: Size) -> Option<((i32, i32), bool)> {
let ((dpi_x, dpi_y), (paper_x, paper_y)) = (self.dpi, self.size);
let pixels = |dips: f32, dpi: i32| (f64::from(dips) * f64::from(dpi) / 96.0).round() as i32;
// A page fits upright or turned, within rounding.
let fits = |pixels: i32, paper: i32, dpi: i32| f64::from(pixels - paper) <= f64::from(dpi) * super::ROUNDING / 72.0;
let (across, down) = (pixels(size.Width, dpi_x), pixels(size.Height, dpi_y));
if fits(across, paper_x, dpi_x) && fits(down, paper_y, dpi_y) {
return Some(((across, down), false));
}
let (across, down) = (pixels(size.Width, dpi_y), pixels(size.Height, dpi_x));
(fits(across, paper_y, dpi_y) && fits(down, paper_x, dpi_x)).then_some(((across, down), true))
}
fn millimetres(&self) -> (f64, f64) {
let mm = |pixels: i32, dpi: i32| f64::from(pixels) * 25.4 / f64::from(dpi);
(mm(self.size.0, self.dpi.0), mm(self.size.1, self.dpi.1))
}
}
/// Pixels given per pixel asked: the renderer counts DIPs.
fn scale(document: &PdfDocument) -> anyhow::Result<f64> {
let options = PdfPageRenderOptions::new()?;
options.SetDestinationWidth(PROBE)?;
options.SetDestinationHeight(PROBE)?;
let (across, _) = bitmap(&rendered(&document.GetPage(0)?, &options)?)?.size;
log::info!("print: the renderer gives {across} pixels per {PROBE} asked");
Ok(f64::from(across) / f64::from(PROBE))
}
/// Draws page `n` at its size from the paper's corner.
fn page(document: &PdfDocument, n: u32, dc: HDC, paper: &Paper, scale: f64) -> anyhow::Result<()> {
let page = document.GetPage(n)?;
let size = page.Size()?;
let ((width, height), turned) = paper.fit(size).with_context(|| format!("page {} does not fit the paper", n + 1))?;
let (offset_x, offset_y) = paper.offset;
if unsafe { StartPage(dc) } <= 0 {
bail!("the printer refused page {}: {}", n + 1, std::io::Error::last_os_error());
}
// Device pixels start at the printable area, not the paper.
let place = match turned {
// A turned page's top runs up its left edge.
true => XFORM { eM11: 0.0, eM12: -1.0, eM21: 1.0, eM22: 0.0, eDx: -offset_x as f32, eDy: (width - offset_y) as f32 },
false => XFORM { eM11: 1.0, eM12: 0.0, eM21: 0.0, eM22: 1.0, eDx: -offset_x as f32, eDy: -offset_y as f32 },
};
if unsafe { SetGraphicsMode(dc, GM_ADVANCED) } == 0 || !unsafe { SetWorldTransform(dc, &place) }.as_bool() {
bail!("the printer cannot place page {}", n + 1);
}
let rows = (BAND / (width.max(1) as usize * 4)).max(1) as i32;
let mut top = 0;
while top < height {
let band = rows.min(height - top);
let bmp = render(&page, size, (width, height), (top, band), scale)?;
draw(dc, &bmp, top, width, band)?;
top += band;
}
if unsafe { EndPage(dc) } <= 0 {
bail!("the printer did not take page {}: {}", n + 1, std::io::Error::last_os_error());
}
Ok(())
}
/// The page's band from row `top`, as a BMP file.
fn render(page: &PdfPage, size: Size, (width, height): (i32, i32), (top, rows): (i32, i32), scale: f64) -> anyhow::Result<Vec<u8>> {
let options = PdfPageRenderOptions::new()?;
let row = f64::from(size.Height) / f64::from(height);
options.SetSourceRect(Rect {
X: 0.0,
Y: (f64::from(top) * row) as f32,
Width: size.Width,
Height: (f64::from(rows) * row) as f32,
})?;
let asked = |pixels: i32| ((f64::from(pixels) / scale).round() as u32).max(1);
options.SetDestinationWidth(asked(width))?;
options.SetDestinationHeight(asked(rows))?;
rendered(page, &options)
}
/// The page as `options` asks, as a BMP file.
fn rendered(page: &PdfPage, options: &PdfPageRenderOptions) -> anyhow::Result<Vec<u8>> {
options.SetBitmapEncoderId(BitmapEncoder::BmpEncoderId()?)?;
let stream = InMemoryRandomAccessStream::new()?;
page.RenderWithOptionsToStreamAsync(&stream, options)?.join()?;
let length = u32::try_from(stream.Size()?)?;
let reader = DataReader::CreateDataReader(&stream.GetInputStreamAt(0)?)?;
reader.LoadAsync(length)?.join()?;
let mut bytes = vec![0; length as usize];
reader.ReadBytes(&mut bytes)?;
Ok(bytes)
}
/// A BMP file's pixel size and where its pixels start.
struct Bitmap {
size: (i32, i32),
start: usize,
}
fn bitmap(bmp: &[u8]) -> anyhow::Result<Bitmap> {
let word = |at: usize| bmp.get(at..at + 4).map(|bytes| u32::from_le_bytes(bytes.try_into().expect("four bytes")));
let (Some(start), Some(across), Some(down)) = (word(10), word(18), word(22)) else {
bail!("the renderer gave no bitmap");
};
if !bmp.starts_with(b"BM") || start as usize >= bmp.len() {
bail!("the renderer gave a broken bitmap");
}
// Rows run up the file when its height is positive.
Ok(Bitmap { size: (across as i32, (down as i32).abs()), start: start as usize })
}
/// Stretches one BMP band over `width` by `rows` at `top`.
fn draw(dc: HDC, bmp: &[u8], top: i32, width: i32, rows: i32) -> anyhow::Result<()> {
let Bitmap { size: (across, down), start } = bitmap(bmp)?;
// Asking in DIPs rounds; a few pixels off is that.
if (across - width).abs() > 4 || (down - rows).abs() > 4 {
bail!("the renderer gave a {across}x{down} bitmap for {width}x{rows}");
}
let info = bmp[14..].as_ptr().cast::<BITMAPINFO>();
let bits = bmp[start..].as_ptr().cast();
let copied = unsafe { StretchDIBits(dc, 0, top, width, rows, 0, 0, across, down, Some(bits), info, DIB_RGB_COLORS, SRCCOPY) };
if copied <= 0 {
bail!("the printer took none of the page: {}", std::io::Error::last_os_error());
}
Ok(())
}
/// Opens `pdf` with Windows' PDF renderer.
fn open(pdf: &Path) -> anyhow::Result<PdfDocument> {
let file = StorageFile::GetFileFromPathAsync(&HSTRING::from(pdf))?
.join()
.with_context(|| format!("{} cannot be opened", pdf.display()))?;
PdfDocument::LoadFromFileAsync(&file)?.join().with_context(|| format!("{} is no readable PDF", pdf.display()))
}
/// Each page's size in points, as it shows.
fn sizes(document: &PdfDocument) -> anyhow::Result<Vec<(f64, f64)>> {
let points = |dips: f32| f64::from(dips) * 72.0 / 96.0;
(0..document.PageCount()?)
.map(|n| {
let size = document.GetPage(n)?.Size()?;
Ok((points(size.Width), points(size.Height)))
})
.collect()
}
/// Runs `work` with the thread joined to the Windows Runtime.
fn winrt<T>(work: impl FnOnce() -> anyhow::Result<T>) -> anyhow::Result<T> {
unsafe { RoInitialize(RO_INIT_MULTITHREADED) }?;
let result = work();
unsafe { RoUninitialize() };
result
}
fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
#[cfg(test)]
mod tests {
use windows::Win32::UI::Controls::Dialogs::{PD_PAGENUMS, PD_PRINTTOFILE, PRINTDLGEX_FLAGS, PRINTPAGERANGE};
use super::{chosen, wide};
#[test]
fn the_dialog_answer_picks_pages_and_output() {
let ranges = [PRINTPAGERANGE { nFromPage: 2, nToPage: 3 }, PRINTPAGERANGE { nFromPage: 5, nToPage: 9 }];
assert_eq!(chosen(PRINTDLGEX_FLAGS(0), &ranges, 3), (vec![0, 1, 2], None));
assert_eq!(chosen(PD_PAGENUMS, &ranges, 6), (vec![1, 2, 4, 5], None));
// Print to file: the spooler asks where.
assert_eq!(chosen(PD_PAGENUMS | PD_PRINTTOFILE, &ranges[..1], 6), (vec![1, 2], Some(wide("FILE:"))));
}
}
@@ -0,0 +1,209 @@
//! The printer's settings and the global blocks the dialog trades.
use anyhow::{Context as _, bail};
use windows::{
Win32::{
Foundation::{GlobalFree, HGLOBAL, POINT},
Graphics::{
Gdi::{
DEVMODEW, DM_IN_BUFFER, DM_ORIENTATION, DM_OUT_BUFFER, DM_PAPERLENGTH, DM_PAPERSIZE, DM_PAPERWIDTH,
DMORIENT_LANDSCAPE, DMORIENT_PORTRAIT, DMPAPER_USER,
},
Printing::{ClosePrinter, DocumentPropertiesW, GetDefaultPrinterW, OpenPrinterW, PRINTER_HANDLE},
},
Storage::Xps::{DC_PAPERS, DC_PAPERSIZE, DeviceCapabilitiesW},
System::Memory::{GMEM_MOVEABLE, GlobalAlloc, GlobalLock, GlobalSize, GlobalUnlock},
UI::Controls::Dialogs::DEVNAMES,
},
core::{PCWSTR, PWSTR},
};
use super::wide;
/// `printer`'s settings, set to the document's paper.
pub(super) fn settings(printer: &[u16], (width, height): (f64, f64)) -> anyhow::Result<Devmode> {
let name = PCWSTR(printer.as_ptr());
let mut handle = PRINTER_HANDLE::default();
unsafe { OpenPrinterW(name, &mut handle, None) }
.with_context(|| format!("the printer {} cannot be opened", String::from_utf16_lossy(&printer[..printer.len() - 1])))?;
let set = || -> anyhow::Result<Devmode> {
let bytes = unsafe { DocumentPropertiesW(None, handle, name, None, None, 0) };
let mut settings = Devmode::zeroed(usize::try_from(bytes).ok().filter(|&b| b >= size_of::<DEVMODEW>()).context("the printer gives no settings")?);
answered(unsafe { DocumentPropertiesW(None, handle, name, Some(settings.as_mut_ptr()), None, DM_OUT_BUFFER.0) })?;
// Forms stand upright, in tenths of a millimetre.
let tenths = |points: f64| (points * 254.0 / 72.0).round();
let (short, long) = (tenths(width.min(height)), tenths(width.max(height)));
if long > f64::from(i16::MAX) {
bail!("the page is larger than any paper");
}
let points = |tenths: i32| f64::from(tenths) * 72.0 / 254.0;
let page = (width.min(height), width.max(height));
let form = forms(name, &settings)?.into_iter().find(|&(_, across, down)| crate::print::holds((points(across), points(down)), page));
let fields = settings.fields();
unsafe {
let paper = &mut fields.Anonymous1.Anonymous1;
match form {
Some((id, ..)) => {
paper.dmPaperSize = id as i16;
fields.dmFields = (fields.dmFields | DM_PAPERSIZE) & !(DM_PAPERWIDTH | DM_PAPERLENGTH);
}
None => {
paper.dmPaperSize = DMPAPER_USER as i16;
paper.dmPaperWidth = short as i16;
paper.dmPaperLength = long as i16;
fields.dmFields |= DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH;
}
}
paper.dmOrientation = if width > height { DMORIENT_LANDSCAPE } else { DMORIENT_PORTRAIT } as i16;
}
fields.dmFields |= DM_ORIENTATION;
// The driver settles what it cannot do.
let asked = settings.clone();
answered(unsafe {
DocumentPropertiesW(None, handle, name, Some(settings.as_mut_ptr()), Some(asked.as_ptr()), DM_IN_BUFFER.0 | DM_OUT_BUFFER.0)
})?;
Ok(settings)
};
let settings = set();
unsafe { ClosePrinter(handle) }?;
settings
}
/// The printer's forms: id, then size in tenth millimetres.
fn forms(printer: PCWSTR, settings: &Devmode) -> anyhow::Result<Vec<(u16, i32, i32)>> {
let count = unsafe { DeviceCapabilitiesW(printer, PCWSTR::null(), DC_PAPERS, None, Some(settings.as_ptr())) };
let count = usize::try_from(count).context("the printer lists no forms")?;
let mut ids = vec![0u16; count];
let mut sizes = vec![POINT::default(); count];
let listed = [
unsafe { DeviceCapabilitiesW(printer, PCWSTR::null(), DC_PAPERS, Some(PWSTR(ids.as_mut_ptr())), Some(settings.as_ptr())) },
unsafe { DeviceCapabilitiesW(printer, PCWSTR::null(), DC_PAPERSIZE, Some(PWSTR(sizes.as_mut_ptr().cast())), Some(settings.as_ptr())) },
];
if listed != [count as i32; 2] {
bail!("the printer's forms changed while read");
}
Ok(ids.into_iter().zip(sizes).map(|(id, size)| (id, size.x, size.y)).collect())
}
/// The default printer's name, ending in a nul.
pub(super) fn default_printer() -> anyhow::Result<Vec<u16>> {
let mut length = 0;
// Asked with no buffer, it fails and gives the length.
let _ = unsafe { GetDefaultPrinterW(None, &mut length) };
if length == 0 {
bail!("no default printer is set up");
}
let mut name = vec![0u16; length as usize];
if !unsafe { GetDefaultPrinterW(Some(PWSTR(name.as_mut_ptr())), &mut length) }.as_bool() {
bail!("the default printer cannot be read: {}", std::io::Error::last_os_error());
}
Ok(name)
}
/// Names `printer`, so the dialog keeps our settings.
pub(super) fn names(printer: &[u16]) -> Vec<u8> {
let driver = wide("winspool");
let header = (size_of::<DEVNAMES>() / 2) as u16;
let offsets = [header, header + driver.len() as u16, header + (driver.len() + printer.len()) as u16, 0];
let words = offsets.into_iter().chain(driver).chain(printer.iter().copied()).chain([0]);
words.flat_map(u16::to_le_bytes).collect()
}
/// A DEVNAMES block's device name, ending in a nul.
pub(super) fn device(names: HGLOBAL) -> anyhow::Result<Vec<u16>> {
let base = lock(names)?.cast::<u16>();
let offset = unsafe { (*base.cast::<DEVNAMES>()).wDeviceOffset } as usize;
let start = unsafe { base.add(offset) };
let length = (0..).take_while(|&n| unsafe { *start.add(n) } != 0).count();
let name = unsafe { std::slice::from_raw_parts(start, length + 1) }.to_vec();
unlock(names);
Ok(name)
}
/// A DEVMODE with its driver's tail, kept aligned.
#[derive(Clone)]
pub(super) struct Devmode {
words: Vec<u64>,
bytes: usize,
}
impl Devmode {
fn zeroed(bytes: usize) -> Self {
Self { words: vec![0; bytes.div_ceil(8)], bytes }
}
/// A copy of the DEVMODE in `block`.
pub(super) fn copied(block: HGLOBAL) -> anyhow::Result<Self> {
let bytes = unsafe { GlobalSize(block) };
if bytes < size_of::<DEVMODEW>() {
bail!("the dialog gave no printer settings");
}
let mut settings = Self::zeroed(bytes);
let source = lock(block)?;
unsafe { std::ptr::copy_nonoverlapping(source.cast::<u8>(), settings.words.as_mut_ptr().cast(), bytes) };
unlock(block);
Ok(settings)
}
pub(super) fn as_ptr(&self) -> *const DEVMODEW {
self.words.as_ptr().cast()
}
fn as_mut_ptr(&mut self) -> *mut DEVMODEW {
self.words.as_mut_ptr().cast()
}
fn fields(&mut self) -> &mut DEVMODEW {
unsafe { &mut *self.as_mut_ptr() }
}
pub(super) fn bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.words.as_ptr().cast(), self.bytes) }
}
}
/// A movable global block holding `bytes`.
pub(super) fn global(bytes: &[u8]) -> anyhow::Result<HGLOBAL> {
let block = unsafe { GlobalAlloc(GMEM_MOVEABLE, bytes.len()) }?;
let target = lock(block)?;
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), target.cast(), bytes.len()) };
unlock(block);
Ok(block)
}
fn lock(block: HGLOBAL) -> anyhow::Result<*mut core::ffi::c_void> {
let pointer = unsafe { GlobalLock(block) };
if pointer.is_null() {
bail!("a printer block cannot be read: {}", std::io::Error::last_os_error());
}
Ok(pointer)
}
fn unlock(block: HGLOBAL) {
// An error here only says the block is now unlocked.
if let Err(error) = unsafe { GlobalUnlock(block) }
&& error.code().is_err()
{
log::warn!("print: a printer block stays locked: {error}");
}
}
pub(super) fn free(block: HGLOBAL) -> anyhow::Result<()> {
if block.is_invalid() {
return Ok(());
}
// Freed, it answers null; windows-rs calls that an error.
match unsafe { GlobalFree(Some(block)) } {
Err(error) if error.code().is_ok() => Ok(()),
Err(error) => Err(error.into()),
Ok(_) => bail!("a printer block cannot be freed: {}", std::io::Error::last_os_error()),
}
}
/// DocumentProperties answers IDOK on success.
fn answered(code: i32) -> anyhow::Result<()> {
if code < 0 {
bail!("the printer refused its settings");
}
Ok(())
}
+339
View File
@@ -0,0 +1,339 @@
//! Printing through a Windows file printer and fake Linux portal.
#[cfg(not(target_os = "macos"))]
use std::path::Path;
/// Pages of `sizes` points, each with an inch square.
#[cfg(not(target_os = "macos"))]
fn fixture(path: &Path, sizes: &[(f32, f32)]) {
use lopdf::{Document, Object, Stream, dictionary};
let mut document = Document::with_version("1.7");
let tree = document.new_object_id();
let kids: Vec<Object> = sizes
.iter()
.map(|&(width, height)| {
let square = format!("0 g 72 {} 72 72 re f", height - 144.0);
let content = document.add_object(Stream::new(dictionary! {}, square.into_bytes()));
let media: Vec<Object> = vec![0.into(), 0.into(), width.into(), height.into()];
document
.add_object(dictionary! { "Type" => "Page", "Parent" => tree, "MediaBox" => media, "Contents" => content })
.into()
})
.collect();
let count = kids.len() as i64;
document.objects.insert(tree, dictionary! { "Type" => "Pages", "Kids" => kids, "Count" => count }.into());
let catalog = document.add_object(dictionary! { "Type" => "Catalog", "Pages" => tree });
document.trailer.set("Root", catalog);
document.save(path).expect("the fixture saves");
}
#[cfg(windows)]
fn main() {
let folder = tempfile::tempdir().expect("a folder");
unsafe { windows::Win32::System::WinRT::RoInitialize(windows::Win32::System::WinRT::RO_INIT_MULTITHREADED) }.expect("the runtime");
// A5, then wide A5: the paper stays upright.
let mixed = printed(folder.path(), "mixed", &[(420.0, 595.0), (595.0, 420.0)]);
assert_eq!(papers(&mixed), [(420, 595), (420, 595)]);
let square = |page| windows_page::dark(&mixed, page, (108, 108));
assert_eq!((square(0), square(1)), (true, false), "the upright page keeps its corner");
// Turned left, its top left lands bottom left.
assert!(windows_page::dark(&mixed, 1, (108, 487)), "the turned page's square");
assert!(!windows_page::dark(&mixed, 0, (312, 108)), "nothing at the upright page's other corner");
// A4 landscape alone turns the paper.
let wide = printed(folder.path(), "wide", &[(842.0, 595.0)]);
assert_eq!(papers(&wide), [(842, 595)]);
assert!(windows_page::dark(&wide, 0, (108, 108)), "the wide page keeps its corner");
// A paper the printer lacks: refused, nothing cut or written.
let source = folder.path().join("custom.pdf");
fixture(&source, &[(1000.0, 600.0)]);
let job = folder.path().join("custom-job.pdf");
unsafe { std::env::set_var("ELY_PRINT_TO", &job) };
let error = futures::executor::block_on(ely_platform::print::print_pdf(&source)).expect_err("no such paper");
assert!(error.to_string().contains("page 1 is 353 × 212 mm; the printer's paper is"), "{error}");
assert!(!job.exists(), "nothing printed");
// 1.6 mm past A4: refused, not cut.
fixture(&source, &[(600.0, 842.0)]);
let error = futures::executor::block_on(ely_platform::print::print_pdf(&source)).expect_err("past A4");
assert!(error.to_string().contains("page 1 is 212 × 297 mm; the printer's paper is"), "{error}");
assert!(!job.exists(), "nothing printed");
println!("print: windows ok");
}
/// Prints `sizes` through the file printer; the job's PDF.
#[cfg(windows)]
fn printed(folder: &Path, name: &str, sizes: &[(f32, f32)]) -> std::path::PathBuf {
let source = folder.join(format!("{name}.pdf"));
fixture(&source, sizes);
let job = folder.join(format!("{name}-job.pdf"));
// Before any thread starts: the job goes to a file.
unsafe { std::env::set_var("ELY_PRINT_TO", &job) };
assert!(futures::executor::block_on(ely_platform::print::print_pdf(&source)).expect("prints"), "printed");
job
}
/// Each printed page's paper in whole points.
#[cfg(windows)]
fn papers(job: &Path) -> Vec<(i64, i64)> {
let document = lopdf::Document::load(job).expect("the job is a PDF");
document
.get_pages()
.into_values()
.map(|page| {
let media = document.get_dictionary(page).and_then(|page| page.get(b"MediaBox")).expect("a media box");
let corners: Vec<f32> = media.as_array().expect("an array").iter().map(|n| n.as_float().expect("a number")).collect();
((corners[2] - corners[0]).round() as i64, (corners[3] - corners[1]).round() as i64)
})
.collect()
}
/// Reads printed pages back through Windows' own renderer.
#[cfg(windows)]
mod windows_page {
use std::path::Path;
use windows::{
Data::Pdf::{PdfDocument, PdfPageRenderOptions},
Storage::{
StorageFile,
Streams::{DataReader, InMemoryRandomAccessStream},
},
core::HSTRING,
};
/// Whether `(x, y)` points from the top left is dark.
pub fn dark(job: &Path, page: u32, (x, y): (u32, u32)) -> bool {
let file = StorageFile::GetFileFromPathAsync(&HSTRING::from(job)).and_then(|file| file.join()).expect("the job opens");
let document = PdfDocument::LoadFromFileAsync(&file).and_then(|document| document.join()).expect("the job reads");
let page = document.GetPage(page).expect("the page");
let size = page.Size().expect("its size");
let options = PdfPageRenderOptions::new().expect("options");
// Asked in DIPs; scaled back to one pixel per point.
options.SetDestinationWidth((size.Width * 0.75).round() as u32).expect("a width");
let stream = InMemoryRandomAccessStream::new().expect("a stream");
page.RenderWithOptionsToStreamAsync(&stream, &options).and_then(|render| render.join()).expect("renders");
let length = stream.Size().expect("a size") as u32;
let reader = DataReader::CreateDataReader(&stream.GetInputStreamAt(0).expect("a start")).expect("a reader");
reader.LoadAsync(length).and_then(|load| load.join()).expect("loads");
let mut png = vec![0; length as usize];
reader.ReadBytes(&mut png).expect("reads");
let image = image::load_from_memory(&png).expect("a PNG").to_luma8();
let scale = f64::from(image.width()) / f64::from(size.Width * 0.75);
let at = |point: u32| (f64::from(point) * scale) as u32;
image.get_pixel(at(x), at(y)).0[0] < 64
}
}
#[cfg(target_os = "linux")]
fn main() {
use std::io::BufRead as _;
// A private session bus: the stand-in portal and nothing else.
let bus = std::process::Command::new("dbus-daemon")
.args(["--session", "--nofork", "--nopidfile", "--print-address=1"])
.stdout(std::process::Stdio::piped())
.spawn();
let Ok(mut bus) = bus else {
println!("SKIP: no dbus-daemon");
return;
};
let mut address = String::new();
std::io::BufReader::new(bus.stdout.take().expect("its address")).read_line(&mut address).expect("reads the address");
// Before any thread starts: the portal client finds this bus.
unsafe { std::env::set_var("DBUS_SESSION_BUS_ADDRESS", address.trim()) };
let folder = tempfile::tempdir().expect("a folder");
let wide = folder.path().join("wide.pdf");
fixture(&wide, &[(842.0, 595.0)]);
let three = folder.path().join("three.pdf");
fixture(&three, &[(595.0, 842.0), (420.0, 595.0), (300.0, 300.0)]);
futures::executor::block_on(portal::check(&wide, &three));
bus.kill().expect("the bus stops");
println!("print: linux ok");
}
/// A stand-in for org.freedesktop.portal.Print.
#[cfg(target_os = "linux")]
mod portal {
use std::{
collections::HashMap,
io::Read as _,
path::Path,
sync::{Arc, Mutex},
};
use zbus::{
Connection,
message::Header,
zvariant::{OwnedFd, OwnedObjectPath, OwnedValue, Value},
};
#[derive(Default)]
struct Seen {
cancel: bool,
/// What the dialog answers the user chose.
choices: Vec<(&'static str, &'static str)>,
setup: HashMap<String, OwnedValue>,
formats: Option<Vec<String>>,
token: Option<u32>,
bytes: Option<Vec<u8>>,
}
struct Portal(Arc<Mutex<Seen>>);
/// The request object the caller listens on.
fn handle(header: &Header<'_>, options: &HashMap<String, OwnedValue>) -> OwnedObjectPath {
let sender = header.sender().expect("a sender").trim_start_matches(':').replace('.', "_");
let token: &str = options.get("handle_token").expect("a handle token").downcast_ref().expect("a string");
OwnedObjectPath::try_from(format!("/org/freedesktop/portal/desktop/request/{sender}/{token}")).expect("a path")
}
async fn answer(connection: &Connection, handle: &OwnedObjectPath, code: u32, results: HashMap<&str, Value<'_>>) {
connection
.emit_signal(None::<()>, handle, "org.freedesktop.portal.Request", "Response", &(code, results))
.await
.expect("the response goes out");
}
#[zbus::interface(name = "org.freedesktop.portal.Print")]
impl Portal {
// The portal's own signature.
#[allow(clippy::too_many_arguments)]
async fn prepare_print(
&self,
#[zbus(header)] header: Header<'_>,
#[zbus(connection)] connection: &Connection,
_parent: &str,
_title: &str,
_settings: HashMap<String, OwnedValue>,
page_setup: HashMap<String, OwnedValue>,
options: HashMap<String, OwnedValue>,
) -> OwnedObjectPath {
let handle = handle(&header, &options);
let formats = options.get("supported_output_file_formats").map(|formats| formats.try_clone().expect("a copy").try_into().expect("strings"));
let (cancel, choices) = {
let mut seen = self.0.lock().expect("the record");
(seen.setup, seen.formats) = (page_setup.clone(), formats);
(seen.cancel, seen.choices.clone())
};
// GTK hands every setting back as text.
let settings: HashMap<String, Value> = choices.into_iter().map(|(key, value)| (key.to_string(), Value::from(value))).collect();
let results = match cancel {
true => HashMap::new(),
false => HashMap::from([
("settings", Value::from(settings)),
("page-setup", Value::from(page_setup)),
("token", Value::from(7u32)),
]),
};
answer(connection, &handle, u32::from(cancel), results).await;
handle
}
async fn print(
&self,
#[zbus(header)] header: Header<'_>,
#[zbus(connection)] connection: &Connection,
_parent: &str,
_title: &str,
fd: OwnedFd,
options: HashMap<String, OwnedValue>,
) -> OwnedObjectPath {
let handle = handle(&header, &options);
let mut bytes = Vec::new();
std::fs::File::from(std::os::fd::OwnedFd::from(fd)).read_to_end(&mut bytes).expect("the job reads");
let token = options.get("token").map(|token| token.downcast_ref::<u32>().expect("a number"));
{
let mut seen = self.0.lock().expect("the record");
(seen.token, seen.bytes) = (token, Some(bytes));
}
answer(connection, &handle, 0, HashMap::new()).await;
handle
}
#[zbus(property, name = "version")]
fn version(&self) -> u32 {
3
}
}
/// A sheet's size, then each form's width and placed corners.
type Sheet = ((i64, i64), Vec<(i64, [i64; 4])>);
fn sheets(bytes: &[u8]) -> Vec<Sheet> {
use lopdf::{Document, Object, content::Content};
let document = Document::load_mem(bytes).expect("a PDF");
let numbers = |object: &Object| -> Vec<f64> { object.as_array().expect("numbers").iter().map(|n| f64::from(n.as_float().expect("a number"))).collect() };
let round = |n: f64| n.round() as i64;
let pages = document.get_pages().into_values().map(|page| {
let dict = document.get_dictionary(page).expect("a page");
let media = numbers(dict.get(b"MediaBox").expect("a box"));
let resources = dict.get(b"Resources").and_then(Object::as_dict).expect("resources");
let forms = resources.get(b"XObject").and_then(Object::as_dict).expect("forms");
let content = Content::decode(&document.get_page_content(page)).expect("content");
let mut m = vec![1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
let mut placed = Vec::new();
for op in content.operations {
match op.operator.as_str() {
"cm" => m = op.operands.iter().map(|n| f64::from(n.as_float().expect("a number"))).collect(),
"Do" => {
let form = forms.get(op.operands[0].as_name().expect("a name")).and_then(Object::as_reference).expect("a form");
let stream = document.get_object(form).and_then(Object::as_stream).expect("a stream");
let b = numbers(stream.dict.get(b"BBox").expect("a box"));
let at = |x: f64, y: f64| (m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]);
let ((x0, y0), (x1, y1)) = (at(b[0], b[1]), at(b[2], b[3]));
placed.push((round(b[2] - b[0]), [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)].map(round)));
}
_ => {}
}
}
((round(media[2]), round(media[3])), placed)
});
pages.collect()
}
pub async fn check(source: &Path, three: &Path) {
let seen = Arc::new(Mutex::new(Seen::default()));
let _portal = zbus::connection::Builder::session()
.and_then(|builder| builder.name("org.freedesktop.portal.Desktop"))
.and_then(|builder| builder.serve_at("/org/freedesktop/portal/desktop", Portal(seen.clone())))
.expect("a portal")
.build()
.await
.expect("the portal serves");
assert!(ely_platform::print::print_pdf(source).await.expect("prints"), "printed");
{
let seen = seen.lock().expect("the record");
let text = |key: &str| seen.setup.get(key).map(|value| value.downcast_ref::<String>().expect("text"));
let number = |key: &str| seen.setup.get(key).map(|value| value.downcast_ref::<f64>().expect("a number").round());
assert_eq!(text("Name").as_deref(), Some("iso_a4"));
assert_eq!(text("Orientation").as_deref(), Some("landscape"));
assert_eq!((number("Width"), number("Height"), number("MarginTop")), (Some(210.0), Some(297.0), Some(0.0)));
assert_eq!(seen.token, Some(7), "the print carries the dialog's token");
assert_eq!(seen.formats.as_deref(), Some(["pdf".to_string()].as_slice()), "PDF is all we hand over");
let printed = sheets(seen.bytes.as_deref().expect("a job"));
assert_eq!(printed, [((842, 595), vec![(842, [0, 0, 842, 595])])], "the page fills its sheet");
}
// Page two at half size, from the sheet's top left.
let choices = vec![("print-pages", "ranges"), ("page-ranges", "1"), ("scale", "50")];
*seen.lock().expect("the record") = Seen { choices, ..Seen::default() };
assert!(ely_platform::print::print_pdf(three).await.expect("prints"), "printed");
let printed = sheets(seen.lock().expect("the record").bytes.as_deref().expect("a job"));
assert_eq!(printed, [((595, 842), vec![(420, [0, 545, 210, 842])])]);
// Two copies, uncollated, of the last two pages, in reverse.
let choices = vec![("print-pages", "ranges"), ("page-ranges", "1--1"), ("reverse", "true"), ("n-copies", "2"), ("collate", "false")];
*seen.lock().expect("the record") = Seen { choices, ..Seen::default() };
assert!(ely_platform::print::print_pdf(three).await.expect("prints"), "printed");
let printed = sheets(seen.lock().expect("the record").bytes.as_deref().expect("a job"));
let widths: Vec<i64> = printed.iter().map(|(_, placed)| placed[0].0).collect();
assert_eq!(widths, [300, 300, 420, 420]);
// Cancelled, nothing prints.
*seen.lock().expect("the record") = Seen { cancel: true, ..Seen::default() };
assert!(!ely_platform::print::print_pdf(source).await.expect("cancels"), "not printed");
assert_eq!(seen.lock().expect("the record").bytes, None, "no job went out");
}
}
/// macOS prints through tests/scripts/print.sh, in a real window.
#[cfg(target_os = "macos")]
fn main() {}
+1 -7
View File
@@ -131,9 +131,6 @@ fn label(command: &Command, cx: &App) -> SharedString {
/// Why `id` cannot run; a document lifts the no-document block.
pub fn blocked(id: &str, document: bool, cx: &App) -> Option<SharedString> {
if id == PRINT.id && !ely_platform::print::BUILT {
return Some(i18n::text(cx, "command.blocked.unbuilt", &[]));
}
let command = RENAMING.iter().find(|command| command.id == id)?;
let key = command.blocked.filter(|_| !document)?;
Some(i18n::text(cx, key, &[]))
@@ -190,11 +187,8 @@ pub fn menus(
}
pub fn bind_keys(cx: &mut App) {
// Without a print panel, Print stays off the keyboard.
if ely_platform::print::BUILT {
cx.bind_keys([KeyBinding::new(PRINT.keys, Print, Some(CONTEXT))]);
}
cx.bind_keys([
KeyBinding::new(PRINT.keys, Print, Some(CONTEXT)),
KeyBinding::new(NEW.keys, New, Some(CONTEXT)),
KeyBinding::new(OPEN.keys, Open, Some(CONTEXT)),
KeyBinding::new(SAVE.keys, Save, Some(CONTEXT)),
-2
View File
@@ -41,7 +41,6 @@ const EN: &[(&str, &str)] = &[
("command.file.export", "Export…"),
("command.file.print", "Print…"),
("command.blocked.no_document", "No open document"),
("command.blocked.unbuilt", "Not built for this platform yet"),
("palette.group.file", "File"),
("gallery.ready", "Ready"),
("gallery.nav", "Navigation"),
@@ -97,7 +96,6 @@ const ZH_CN: &[(&str, &str)] = &[
("command.file.export", "导出…"),
("command.file.print", "打印…"),
("command.blocked.no_document", "没有打开的文档"),
("command.blocked.unbuilt", "此平台尚未构建"),
("palette.group.file", "文件"),
("gallery.ready", "就绪"),
("gallery.nav", "导航"),
+1 -1
View File
@@ -379,7 +379,7 @@ mod tests {
})
.expect("the window lives");
cx.run_until_parked();
// Save without a document is blocked; so is Print unbuilt.
// Save without a document is blocked.
window
.update(cx, |_, window, cx| root.update(cx, |root, cx| root.run_from_palette("file.save", window, cx)))
.expect("the window lives");