//! Type3 font dictionaries and actual PK masks; no outline replacement. use crate::{execution::FontId, pdf_device::print_decimal, pk_font::PkFontProgram}; use std::collections::BTreeMap; pub(super) fn write_font( objects: &mut Vec>, id: FontId, pk: &PkFontProgram, used: &BTreeMap, compression: u8, target: Option, ) -> usize { let font = target.unwrap_or_else(|| { objects.push(Vec::new()); objects.len() }); let first = *used.keys().next().unwrap(); let last = *used.keys().next_back().unwrap(); let mut procs = String::new(); let mut bbox = None; for slot in used.keys() { let g = pk.glyph(*slot).expect("resolved PK glyph"); let b = pk.glyph_bbox(*slot).unwrap(); bbox = Some(bbox.map_or(b, |old: [i32; 4]| { [ old[0].min(b[0]), old[1].min(b[1]), old[2].max(b[2]), old[3].max(b[3]), ] })); let [left, bottom, right, top] = b; let mut body = format!( "{} 0 {left} {bottom} {right} {top} d1\n", print_decimal(pk.widths[slot], 2) ) .into_bytes(); if g.width > 0 && g.height > 0 { body.extend_from_slice(format!("q\n{} 0 0 {} {left} {bottom} cm\nBI\n/W {}\n/H {}\n/IM true\n/BPC 1\n/D [1 0]\nID ",g.width,g.height,g.width,g.height).as_bytes()); body.extend_from_slice(&g.bitmap); body.extend_from_slice(b"\nEI\nQ\n"); } procs.push_str(&format!(" /a{slot} {} 0 R", objects.len() + 1)); objects.push(super::objects::stream("", &body, compression)); } let widths = (first..=last) .map(|slot| { print_decimal( if used.contains_key(&slot) { pk.widths[&slot] } else { 0 }, 2, ) }) .collect::>() .join(" "); let encoding = (first..=last) .map(|slot| { if used.contains_key(&slot) { format!("/a{slot}") } else { "/.notdef".into() } }) .collect::>() .join(" "); let scale = print_decimal(pk.scale, 5); let [left, bottom, right, top] = bbox.unwrap(); objects[font-1]=format!("<< /Type /Font /Subtype /Type3 /Name /N{} /FontMatrix [{scale} 0 0 {scale} 0 0] /FontBBox [{left} {bottom} {right} {top}] /Resources << /ProcSet [/PDF /ImageB] >> /FirstChar {first} /LastChar {last} /Widths [{widths}] /Encoding << /Type /Encoding /Differences [{first} {encoding}] >> /CharProcs <<{procs} >> >>",id.index()).into_bytes(); font }