use crate::compositor::{DisplayList, DrawCommand};
use std::fmt::Write as _;
use strata_layout::Rect;
use strata_style::types::{BorderRadius, Color, FontWeight};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PdfPageSize {
A4,
Letter,
Custom { width_pt: f32, height_pt: f32 },
}
impl PdfPageSize {
pub const fn dimensions_pt(self) -> (f32, f32) {
match self {
Self::A4 => (595.28, 841.89),
Self::Letter => (612.0, 792.0),
Self::Custom {
width_pt,
height_pt,
} => (width_pt, height_pt),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PdfExportOptions {
pub page_size: PdfPageSize,
pub margin_pt: f32,
pub dpi: f32,
pub fit_to_page: bool,
pub compress_streams: bool,
pub title: String,
pub author: String,
}
impl Default for PdfExportOptions {
fn default() -> Self {
Self {
page_size: PdfPageSize::A4,
margin_pt: 24.0,
dpi: 96.0,
fit_to_page: true,
compress_streams: false,
title: "Strata Vector Export".into(),
author: "Strata Engine".into(),
}
}
}
struct PdfImageObject {
name: String,
width: u32,
height: u32,
rgb_bytes: Vec<u8>,
}
pub fn export_display_list_to_pdf(
display_list: &DisplayList,
viewport_width_px: f32,
viewport_height_px: f32,
options: &PdfExportOptions,
) -> Vec<u8> {
let (page_w, page_h) = options.page_size.dimensions_pt();
let margin = options.margin_pt.max(0.0);
let avail_w = (page_w - margin * 2.0).max(1.0);
let avail_h = (page_h - margin * 2.0).max(1.0);
let vw = viewport_width_px.max(1.0);
let vh = viewport_height_px.max(1.0);
let pt_per_px = 72.0 / options.dpi.max(1.0);
let scale = if options.fit_to_page {
(avail_w / vw).min(avail_h / vh)
} else {
pt_per_px
};
let mut content = String::with_capacity(4096);
let mut images: Vec<PdfImageObject> = Vec::new();
let mut ext_gstates: Vec<(String, f32)> = Vec::new();
let _ = writeln!(content, "q");
let _ = writeln!(
content,
"{scale:.4} 0 0 {scale:.4} {margin:.3} {:.3} cm",
page_h - margin
);
let flip_y = |y: f32| -y;
let alpha_f = |a: u8| a as f32 / 255.0;
for cmd in &display_list.commands {
match cmd {
DrawCommand::DrawRect { bounds, color } => {
if color.a == 0 || bounds.width <= 0.0 || bounds.height <= 0.0 {
continue;
}
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
emit_fill_rgb(&mut content, *color);
let _ = writeln!(
content,
"{:.3} {:.3} {:.3} {:.3} re f",
bounds.x,
flip_y(bounds.y + bounds.height),
bounds.width,
bounds.height
);
}
DrawCommand::DrawRoundedRect {
bounds,
color,
border_radius,
border_width,
border_color,
} => {
if bounds.width <= 0.0 || bounds.height <= 0.0 {
continue;
}
if color.a > 0 {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
emit_fill_rgb(&mut content, *color);
emit_rounded_rect_path(&mut content, *bounds, *border_radius);
let _ = writeln!(content, "f");
}
if let Some(bc) = border_color {
let bw = border_width
.top
.max(border_width.right)
.max(border_width.bottom)
.max(border_width.left);
if bw > 0.0 && bc.a > 0 {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(bc.a));
emit_stroke_rgb(&mut content, *bc);
let _ = writeln!(content, "{bw:.3} w");
emit_rounded_rect_path(&mut content, *bounds, *border_radius);
let _ = writeln!(content, "S");
}
}
}
DrawCommand::DrawLinearGradient {
bounds,
stops,
border_radius,
..
} => {
if let Some(first) = stops.first() {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(first.color.a));
emit_fill_rgb(&mut content, first.color);
emit_rounded_rect_path(&mut content, *bounds, *border_radius);
let _ = writeln!(content, "f");
}
}
DrawCommand::DrawBorder {
bounds,
widths,
color,
border_radius,
..
} => {
let bw = widths
.top
.max(widths.right)
.max(widths.bottom)
.max(widths.left);
if bw > 0.0 && color.a > 0 {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
emit_stroke_rgb(&mut content, *color);
let _ = writeln!(content, "{bw:.3} w");
emit_rounded_rect_path(&mut content, *bounds, *border_radius);
let _ = writeln!(content, "S");
}
}
DrawCommand::DrawText {
origin,
text,
font_size,
font_weight,
color,
is_monospace,
..
} => {
if text.is_empty() || color.a == 0 {
continue;
}
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
emit_fill_rgb(&mut content, *color);
let font_key = match (*is_monospace, *font_weight) {
(true, _) => "/F3",
(
false,
FontWeight::Bold
| FontWeight::SemiBold
| FontWeight::ExtraBold
| FontWeight::Black,
) => "/F2",
_ => "/F1",
};
let baseline_y = flip_y(origin.1 + font_size * 0.82);
let _ = writeln!(
content,
"BT {font_key} {:.2} Tf 1 0 0 1 {:.3} {:.3} Tm ({}) Tj ET",
font_size.max(1.0),
origin.0,
baseline_y,
escape_pdf_string(text)
);
}
DrawCommand::DrawTextFragments { fragments } => {
for frag in fragments {
if frag.text.is_empty() || frag.style.color.a == 0 {
continue;
}
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(frag.style.color.a));
emit_fill_rgb(&mut content, frag.style.color);
let font_key = if frag.style.is_monospace { "/F3" } else { "/F1" };
let baseline_y = flip_y(frag.bounds.y + frag.style.font_size * 0.82);
let _ = writeln!(
content,
"BT {font_key} {:.2} Tf 1 0 0 1 {:.3} {:.3} Tm ({}) Tj ET",
frag.style.font_size.max(1.0),
frag.bounds.x,
baseline_y,
escape_pdf_string(&frag.text)
);
}
}
DrawCommand::DrawSvgPath {
bounds,
path_data,
stroke_color,
fill_color,
stroke_width,
view_box,
..
} => {
let sx = if view_box.0 > 0.0 {
bounds.width / view_box.0
} else {
1.0
};
let sy = if view_box.1 > 0.0 {
bounds.height / view_box.1
} else {
1.0
};
let path_ops = svg_path_to_pdf_ops(path_data, bounds.x, bounds.y, sx, sy);
if path_ops.is_empty() {
continue;
}
if let Some(fc) = fill_color {
if fc.a > 0 {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(fc.a));
emit_fill_rgb(&mut content, *fc);
content.push_str(&path_ops);
let _ = writeln!(content, "f");
}
}
if let Some(sc) = stroke_color {
if sc.a > 0 && *stroke_width > 0.0 {
emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(sc.a));
emit_stroke_rgb(&mut content, *sc);
let _ = writeln!(content, "{:.3} w", stroke_width);
content.push_str(&path_ops);
let _ = writeln!(content, "S");
}
}
}
DrawCommand::DrawImage { bounds, image } => {
if image.width > 0 && image.height > 0 && !image.data.is_empty() {
let img_idx = images.len() + 1;
let name = format!("Im{img_idx}");
let mut rgb = Vec::with_capacity((image.width * image.height * 3) as usize);
for px in image.data.chunks_exact(4) {
rgb.push(px[0]);
rgb.push(px[1]);
rgb.push(px[2]);
}
images.push(PdfImageObject {
name: name.clone(),
width: image.width,
height: image.height,
rgb_bytes: rgb,
});
let _ = writeln!(
content,
"q {:.3} 0 0 {:.3} {:.3} {:.3} cm /{name} Do Q",
bounds.width,
bounds.height,
bounds.x,
flip_y(bounds.y + bounds.height)
);
}
}
DrawCommand::PushClip { bounds, radius } => {
let _ = writeln!(content, "q");
emit_rounded_rect_path(&mut content, *bounds, *radius);
let _ = writeln!(content, "W n");
}
DrawCommand::PopClip => {
let _ = writeln!(content, "Q");
}
DrawCommand::PushOpacity { alpha } => {
let _ = writeln!(content, "q");
emit_alpha_state(&mut content, &mut ext_gstates, *alpha);
}
DrawCommand::PopOpacity => {
let _ = writeln!(content, "Q");
}
DrawCommand::PushTransform { matrix } => {
let m = &matrix.m;
let _ = writeln!(
content,
"q {:.4} {:.4} {:.4} {:.4} {:.3} {:.3} cm",
m[0], -m[1], -m[4], m[5], m[12], -m[13]
);
}
DrawCommand::PopTransform => {
let _ = writeln!(content, "Q");
}
_ => {}
}
}
let _ = writeln!(content, "Q");
assemble_pdf_document(
page_w,
page_h,
content.as_bytes(),
&images,
&ext_gstates,
options,
)
}
fn emit_fill_rgb(out: &mut String, color: Color) {
let _ = writeln!(
out,
"{:.4} {:.4} {:.4} rg",
color.r as f32 / 255.0,
color.g as f32 / 255.0,
color.b as f32 / 255.0
);
}
fn emit_stroke_rgb(out: &mut String, color: Color) {
let _ = writeln!(
out,
"{:.4} {:.4} {:.4} RG",
color.r as f32 / 255.0,
color.g as f32 / 255.0,
color.b as f32 / 255.0
);
}
fn emit_alpha_state(out: &mut String, gstates: &mut Vec<(String, f32)>, alpha: f32) {
let a = alpha.clamp(0.0, 1.0);
if (a - 1.0).abs() < 0.005 {
return;
}
let key = if let Some((existing, _)) = gstates.iter().find(|(_, v)| (*v - a).abs() < 0.005) {
existing.clone()
} else {
let name = format!("GS{}", gstates.len() + 1);
gstates.push((name.clone(), a));
name
};
let _ = writeln!(out, "/{key} gs");
}
fn emit_rounded_rect_path(out: &mut String, b: Rect, r: BorderRadius) {
let max_r = (b.width * 0.5).min(b.height * 0.5).max(0.0);
let tl = r.top_left.clamp(0.0, max_r);
let tr = r.top_right.clamp(0.0, max_r);
let br = r.bottom_right.clamp(0.0, max_r);
let bl = r.bottom_left.clamp(0.0, max_r);
let x0 = b.x;
let x1 = b.x + b.width;
let y_top = -b.y;
let y_bot = -(b.y + b.height);
if tl < 0.01 && tr < 0.01 && br < 0.01 && bl < 0.01 {
let _ = writeln!(out, "{x0:.3} {y_bot:.3} {:.3} {:.3} re", b.width, b.height);
return;
}
const K: f32 = 0.552_284_8;
let _ = writeln!(out, "{:.3} {:.3} m", x0 + tl, y_top);
let _ = writeln!(out, "{:.3} {:.3} l", x1 - tr, y_top);
if tr > 0.01 {
let _ = writeln!(
out,
"{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
x1 - tr * (1.0 - K),
y_top,
x1,
y_top - tr * (1.0 - K),
x1,
y_top - tr
);
}
let _ = writeln!(out, "{:.3} {:.3} l", x1, y_bot + br);
if br > 0.01 {
let _ = writeln!(
out,
"{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
x1,
y_bot + br * (1.0 - K),
x1 - br * (1.0 - K),
y_bot,
x1 - br,
y_bot
);
}
let _ = writeln!(out, "{:.3} {:.3} l", x0 + bl, y_bot);
if bl > 0.01 {
let _ = writeln!(
out,
"{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
x0 + bl * (1.0 - K),
y_bot,
x0,
y_bot + bl * (1.0 - K),
x0,
y_bot + bl
);
}
let _ = writeln!(out, "{:.3} {:.3} l", x0, y_top - tl);
if tl > 0.01 {
let _ = writeln!(
out,
"{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
x0,
y_top - tl * (1.0 - K),
x0 + tl * (1.0 - K),
y_top,
x0 + tl,
y_top
);
}
let _ = writeln!(out, "h");
}
fn svg_path_to_pdf_ops(d: &str, ox: f32, oy: f32, sx: f32, sy: f32) -> String {
let mut out = String::new();
let mut tokens = Vec::new();
let mut cur = String::new();
for ch in d.chars() {
if ch.is_ascii_alphabetic() {
if !cur.is_empty() {
tokens.push(std::mem::take(&mut cur));
}
tokens.push(ch.to_string());
} else if ch.is_ascii_whitespace() || ch == ',' {
if !cur.is_empty() {
tokens.push(std::mem::take(&mut cur));
}
} else {
cur.push(ch);
}
}
if !cur.is_empty() {
tokens.push(cur);
}
let mut i = 0;
let mut cx = 0.0f32;
let mut cy = 0.0f32;
let mut cmd = ' ';
let tx = |x: f32| ox + x * sx;
let ty = |y: f32| -(oy + y * sy);
while i < tokens.len() {
if tokens[i].len() == 1 && tokens[i].chars().next().unwrap().is_ascii_alphabetic() {
cmd = tokens[i].chars().next().unwrap();
i += 1;
}
match cmd {
'M' | 'L' if i + 1 < tokens.len() => {
if let (Ok(x), Ok(y)) = (tokens[i].parse::<f32>(), tokens[i + 1].parse::<f32>()) {
cx = x;
cy = y;
let op = if cmd == 'M' { 'm' } else { 'l' };
let _ = writeln!(out, "{:.3} {:.3} {op}", tx(cx), ty(cy));
if cmd == 'M' {
cmd = 'L';
}
}
i += 2;
}
'm' | 'l' if i + 1 < tokens.len() => {
if let (Ok(dx), Ok(dy)) = (tokens[i].parse::<f32>(), tokens[i + 1].parse::<f32>()) {
cx += dx;
cy += dy;
let op = if cmd == 'm' { 'm' } else { 'l' };
let _ = writeln!(out, "{:.3} {:.3} {op}", tx(cx), ty(cy));
if cmd == 'm' {
cmd = 'l';
}
}
i += 2;
}
'H' if i < tokens.len() => {
if let Ok(x) = tokens[i].parse::<f32>() {
cx = x;
let _ = writeln!(out, "{:.3} {:.3} l", tx(cx), ty(cy));
}
i += 1;
}
'V' if i < tokens.len() => {
if let Ok(y) = tokens[i].parse::<f32>() {
cy = y;
let _ = writeln!(out, "{:.3} {:.3} l", tx(cx), ty(cy));
}
i += 1;
}
'C' if i + 5 < tokens.len() => {
if let (Ok(x1), Ok(y1), Ok(x2), Ok(y2), Ok(x), Ok(y)) = (
tokens[i].parse::<f32>(),
tokens[i + 1].parse::<f32>(),
tokens[i + 2].parse::<f32>(),
tokens[i + 3].parse::<f32>(),
tokens[i + 4].parse::<f32>(),
tokens[i + 5].parse::<f32>(),
) {
cx = x;
cy = y;
let _ = writeln!(
out,
"{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
tx(x1),
ty(y1),
tx(x2),
ty(y2),
tx(x),
ty(y)
);
}
i += 6;
}
'Z' | 'z' => {
let _ = writeln!(out, "h");
cmd = ' ';
}
_ => i += 1,
}
}
out
}
fn escape_pdf_string(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
match ch {
'\\' => out.push_str("\\\\"),
'(' => out.push_str("\\("),
')' => out.push_str("\\)"),
'\n' | '\r' => out.push(' '),
c if c.is_ascii() => out.push(c),
_ => out.push('?'),
}
}
out
}
fn assemble_pdf_document(
page_w: f32,
page_h: f32,
raw_content: &[u8],
images: &[PdfImageObject],
ext_gstates: &[(String, f32)],
options: &PdfExportOptions,
) -> Vec<u8> {
let mut pdf = Vec::with_capacity(raw_content.len() + 2048);
pdf.extend_from_slice(b"%PDF-1.7\n%\xE2\xE3\xCF\xD3\n");
let mut offsets: Vec<usize> = Vec::new();
let push_obj = |pdf: &mut Vec<u8>, offsets: &mut Vec<usize>, id: usize, body: &[u8]| {
offsets.push(pdf.len());
pdf.extend_from_slice(format!("{id} 0 obj\n").as_bytes());
pdf.extend_from_slice(body);
pdf.extend_from_slice(b"\nendobj\n");
};
let first_img_id = 9usize;
push_obj(
&mut pdf,
&mut offsets,
1,
b"<< /Type /Catalog /Pages 2 0 R >>",
);
push_obj(
&mut pdf,
&mut offsets,
2,
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
);
let mut resources =
String::from("<< /Font << /F1 4 0 R /F2 5 0 R /F3 6 0 R >>");
if !ext_gstates.is_empty() {
resources.push_str(" /ExtGState <<");
for (name, alpha) in ext_gstates {
let _ = write!(
resources,
" /{name} << /Type /ExtGState /ca {alpha:.3} /CA {alpha:.3} >>"
);
}
resources.push_str(" >>");
}
if !images.is_empty() {
resources.push_str(" /XObject <<");
for (idx, img) in images.iter().enumerate() {
let _ = write!(resources, " /{} {} 0 R", img.name, first_img_id + idx);
}
resources.push_str(" >>");
}
resources.push_str(" >>");
let page_dict = format!(
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {page_w:.2} {page_h:.2}] /Resources {resources} /Contents 8 0 R >>"
);
push_obj(&mut pdf, &mut offsets, 3, page_dict.as_bytes());
push_obj(
&mut pdf,
&mut offsets,
4,
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
);
push_obj(
&mut pdf,
&mut offsets,
5,
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>",
);
push_obj(
&mut pdf,
&mut offsets,
6,
b"<< /Type /Font /Subtype /Type1 /BaseFont /Courier >>",
);
let info_dict = format!(
"<< /Title ({}) /Author ({}) /Producer (Strata Pure-Rust Vector PDF 1.7 Engine) >>",
escape_pdf_string(&options.title),
escape_pdf_string(&options.author)
);
push_obj(&mut pdf, &mut offsets, 7, info_dict.as_bytes());
let (stream_bytes, filter_clause) = if options.compress_streams {
(
miniz_oxide::deflate::compress_to_vec_zlib(raw_content, 6),
" /Filter /FlateDecode",
)
} else {
(raw_content.to_vec(), "")
};
let mut content_obj = Vec::with_capacity(stream_bytes.len() + 96);
content_obj.extend_from_slice(
format!(
"<< /Length {}{filter_clause} >>\nstream\n",
stream_bytes.len()
)
.as_bytes(),
);
content_obj.extend_from_slice(&stream_bytes);
content_obj.extend_from_slice(b"\nendstream");
push_obj(&mut pdf, &mut offsets, 8, &content_obj);
for (idx, img) in images.iter().enumerate() {
let (img_data, img_filter) = if options.compress_streams {
(
miniz_oxide::deflate::compress_to_vec_zlib(&img.rgb_bytes, 6),
" /Filter /FlateDecode",
)
} else {
(img.rgb_bytes.clone(), "")
};
let mut obj = Vec::with_capacity(img_data.len() + 160);
obj.extend_from_slice(
format!(
"<< /Type /XObject /Subtype /Image /Width {} /Height {} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Length {}{img_filter} >>\nstream\n",
img.width,
img.height,
img_data.len()
)
.as_bytes(),
);
obj.extend_from_slice(&img_data);
obj.extend_from_slice(b"\nendstream");
push_obj(&mut pdf, &mut offsets, first_img_id + idx, &obj);
}
let xref_offset = pdf.len();
let total_objs = offsets.len() + 1;
pdf.extend_from_slice(format!("xref\n0 {total_objs}\n").as_bytes());
pdf.extend_from_slice(b"0000000000 65535 f \n");
for off in &offsets {
pdf.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
}
pdf.extend_from_slice(
format!(
"trailer\n<< /Size {total_objs} /Root 1 0 R /Info 7 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n"
)
.as_bytes(),
);
pdf
}