Everything in Rust on Servo 0.7 (trimmed: no WebXR, WebGPU, multiprocess): tabs and windows, address bar with suggestions, history, bookmarks and reading list, downloads with resume and external download managers, encrypted password vault with autofill, ad blocker (adblock-rust, 22 lists), VPN through Tor or a proxy and secure DNS via a local bridge proxy, picture in picture, Discord Rich Presence, media player and visualizer, spell check, site apps, user scripts and styles, import from other browsers, gamepads, Web Bluetooth, and every setting of the original Nacre in a plain egui interface. Not compiled yet; docs/HANDOFF.md has the first build checklist. Co-Authored-By: Claude Opus 5.5 <[email protected]>
46 lines
2.4 KiB
Rust
46 lines
2.4 KiB
Rust
//! A one-page PDF holding a picture of the page ("Save as PDF"; Servo 0.7 can't print).
|
|
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
|
|
/// Write `image` as a single JPEG-compressed page sized to the image (72 dpi points).
|
|
pub fn write_image_pdf(path: &Path, image: &image::RgbaImage) -> std::io::Result<()> {
|
|
let rgb = image::DynamicImage::ImageRgba8(image.clone()).to_rgb8();
|
|
let mut jpeg = Vec::new();
|
|
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 90).encode_image(&rgb).map_err(std::io::Error::other)?;
|
|
let (w, h) = (image.width(), image.height());
|
|
// Page size in points: fit an A4-ish width at 96 dpi so text keeps a sensible size.
|
|
let scale = 72.0 / 96.0;
|
|
let (pw, ph) = (w as f64 * scale, h as f64 * scale);
|
|
let content = format!("q {pw:.2} 0 0 {ph:.2} 0 0 cm /Im0 Do Q");
|
|
|
|
let mut out: Vec<u8> = Vec::new();
|
|
let mut offsets = Vec::new();
|
|
out.extend_from_slice(b"%PDF-1.4\n%\xE2\xE3\xCF\xD3\n");
|
|
let mut object = |out: &mut Vec<u8>, body: &[u8]| {
|
|
offsets.push(out.len());
|
|
let n = offsets.len();
|
|
out.extend_from_slice(format!("{n} 0 obj\n").as_bytes());
|
|
out.extend_from_slice(body);
|
|
out.extend_from_slice(b"\nendobj\n");
|
|
};
|
|
object(&mut out, b"<< /Type /Catalog /Pages 2 0 R >>");
|
|
object(&mut out, b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
|
|
object(&mut out, format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {pw:.2} {ph:.2}] /Resources << /XObject << /Im0 4 0 R >> >> /Contents 5 0 R >>").as_bytes());
|
|
let mut img = format!("<< /Type /XObject /Subtype /Image /Width {w} /Height {h} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode /Length {} >>\nstream\n", jpeg.len()).into_bytes();
|
|
img.extend_from_slice(&jpeg);
|
|
img.extend_from_slice(b"\nendstream");
|
|
object(&mut out, &img);
|
|
let mut stream = format!("<< /Length {} >>\nstream\n", content.len()).into_bytes();
|
|
stream.extend_from_slice(content.as_bytes());
|
|
stream.extend_from_slice(b"\nendstream");
|
|
object(&mut out, &stream);
|
|
let xref = out.len();
|
|
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", offsets.len() + 1).as_bytes());
|
|
for offset in &offsets {
|
|
out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
|
|
}
|
|
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", offsets.len() + 1).as_bytes());
|
|
let mut file = std::fs::File::create(path)?;
|
|
file.write_all(&out)
|
|
}
|