oxedyne/fe2o3/fe2o3_austenite/src/emit/sinks.rs
9.9 KiB, 79 runs
created by r1870400018:63198, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The evaluator's page sinks: where the fixpoint's pages go, one at a time. |
| 2 | //! |
| 3 | //! A pass hands each finished page to a sink and drops it, so a sink keeps only what it writes. [`PdfSink`] |
| 4 | //! serialises each page's objects as they come, to chunks of bytes held until the document is done (a pass |
| 5 | //! that has not settled is discarded and begun again, so nothing may reach a file before the last), and at |
| 6 | //! `finish` writes the page tree, the outline from the final introspector, the Info dictionary from the |
| 7 | //! document's metadata, the subset fonts with their `/ToUnicode` maps and the cross-reference table. |
| 8 | //! [`VectorSink`] renders each page to an SVG document of its own. [`DeltaSink`] is the live view's: it keeps |
| 9 | //! each page's content id and the frame of only those pages the consumer does not hold, and draws them once the |
| 10 | //! pass that stands is known. [`CountSink`] counts and drops, for the heap probe and for tests. |
| 11 | |
| 12 | use crate::delta::{ |
| 13 | Builder, |
| 14 | Changed, |
| 15 | Head, |
| 16 | Kept, |
| 17 | Window, |
| 18 | }; |
| 19 | use crate::emit::pdf::PdfOptions; |
| 20 | use crate::emit::svg; |
| 21 | use crate::eval::content::ElemKind; |
| 22 | use crate::eval::fixpoint::PageSink; |
| 23 | use crate::eval::intro::Introspector; |
| 24 | use crate::eval::lib::model::lookup; |
| 25 | use crate::eval::select::Selector; |
| 26 | use crate::eval::value::Value; |
| 27 | use crate::eval::Engine; |
| 28 | use crate::page::Page; |
| 29 | |
| 30 | use oxedyne_fe2o3_core::prelude::*; |
| 31 | use oxedyne_fe2o3_graphics::pdf::{ |
| 32 | OutlineItem, |
| 33 | PdfStream, |
| 34 | }; |
| 35 | |
| 36 | use std::io::Write; |
| 37 | |
| 38 | const CHUNK: usize = 256 * 1024; // the size a chunk of output closes at |
| 39 | |
| 40 | /// Output held as a list of chunks, never one buffer that doubles as it grows and for a moment holds two |
| 41 | /// copies of the file. |
| 42 | #[derive(Debug, Default)] |
| 43 | pub struct Chunks { |
| 44 | done: Vec<Vec<u8>>, |
| 45 | cur: Vec<u8>, |
| 46 | len: usize, |
| 47 | } |
| 48 | |
| 49 | impl Chunks { |
| 50 | /// The bytes written so far. |
| 51 | pub fn len(&self) -> usize { self.len } |
| 52 | |
| 53 | pub fn is_empty(&self) -> bool { self.len == 0 } |
| 54 | |
| 55 | /// Writes every chunk, in order, to `out`. |
| 56 | pub fn write_to<W: Write>(&self, out: &mut W) -> Outcome<()> { |
| 57 | for c in &self.done { |
| 58 | res!(out.write_all(c)); |
| 59 | } |
| 60 | res!(out.write_all(&self.cur)); |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | /// The whole file as one buffer. For a test or a small document: it holds a second copy of the output. |
| 65 | pub fn to_vec(&self) -> Vec<u8> { |
| 66 | let mut v = Vec::with_capacity(self.len); |
| 67 | for c in &self.done { |
| 68 | v.extend_from_slice(c); |
| 69 | } |
| 70 | v.extend_from_slice(&self.cur); |
| 71 | v |
| 72 | } |
| 73 | |
| 74 | /// The chunks themselves, for a host that copies them out one by one. |
| 75 | pub fn chunks(&self) -> impl Iterator<Item = &[u8]> + '_ { |
| 76 | self.done.iter().map(|c| c.as_slice()).chain(std::iter::once(self.cur.as_slice())) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl Write for Chunks { |
| 81 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 82 | self.len += buf.len(); |
| 83 | self.cur.extend_from_slice(buf); |
| 84 | if self.cur.len() >= CHUNK { |
| 85 | let full = std::mem::replace(&mut self.cur, Vec::new()); |
| 86 | self.done.push(full); |
| 87 | } |
| 88 | Ok(buf.len()) |
| 89 | } |
| 90 | |
| 91 | fn flush(&mut self) -> std::io::Result<()> { Ok(()) } |
| 92 | } |
| 93 | |
| 94 | /// Streams the document's pages into a PDF. |
| 95 | pub struct PdfSink { |
| 96 | stream: Option<PdfStream<Chunks>>, |
| 97 | out: Option<Chunks>, |
| 98 | opts: PdfOptions, |
| 99 | } |
| 100 | |
| 101 | impl std::fmt::Debug for PdfSink { |
| 102 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 103 | write!(f, "PdfSink {{ open: {}, finished: {} }}", self.stream.is_some(), self.out.is_some()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl PdfSink { |
| 108 | pub fn new() -> Outcome<Self> { |
| 109 | Self::with_options(PdfOptions::default()) |
| 110 | } |
| 111 | |
| 112 | /// A sink that writes the PDF as `opts` ask. |
| 113 | pub fn with_options(opts: PdfOptions) -> Outcome<Self> { |
| 114 | Ok(Self { stream: Some(res!(Self::open(&opts))), out: None, opts }) |
| 115 | } |
| 116 | |
| 117 | fn open(opts: &PdfOptions) -> Outcome<PdfStream<Chunks>> { |
| 118 | let stream = res!(PdfStream::open_version(Chunks::default(), opts.compress, opts.minor)); |
| 119 | Ok(stream.with_colour_out(opts.colour.clone())) |
| 120 | } |
| 121 | |
| 122 | /// The finished file, once the fixpoint has run `finish`. |
| 123 | pub fn output(&self) -> Option<&Chunks> { self.out.as_ref() } |
| 124 | |
| 125 | /// Takes the finished file. |
| 126 | pub fn into_output(self) -> Option<Chunks> { self.out } |
| 127 | } |
| 128 | |
| 129 | impl PageSink for PdfSink { |
| 130 | fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> { |
| 131 | let built = res!(crate::emit::pdf::render_page(&page)); |
| 132 | match self.stream.as_mut() { |
| 133 | Some(s) => s.page(&built), |
| 134 | None => Err(err!("The PDF sink was handed a page after it finished."; Bug)), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn discard_pass(&mut self) -> Outcome<()> { |
| 139 | self.stream = Some(res!(Self::open(&self.opts))); |
| 140 | self.out = None; |
| 141 | Ok(()) |
| 142 | } |
| 143 | |
| 144 | fn finish(&mut self, engine: &mut Engine, intro: &Introspector) -> Outcome<()> { |
| 145 | let stream = match self.stream.take() { |
| 146 | Some(s) => s, |
| 147 | None => return Err(err!("The PDF sink finished twice."; Bug)), |
| 148 | }; |
| 149 | let outline = if self.opts.outline { res!(outline(engine, intro)) } else { Vec::new() }; |
| 150 | let info = self.opts.info(intro.info()); |
| 151 | // Every located element is a place a link may jump to; the writer keeps those a link names. |
| 152 | let dests = intro.iter() |
| 153 | .filter_map(|r| r.elem.location().and_then(|loc| crate::emit::pdf::named_dest(&loc.anchor(), &r.pos))) |
| 154 | .collect(); |
| 155 | self.out = Some(res!(stream.close_with_dests(outline, dests, info))); |
| 156 | Ok(()) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /// The document outline, from the final introspector: each outlined heading in document order, at its level |
| 161 | /// and the page its start tag landed on. A numbered heading is titled as Typst titles it, its number as the |
| 162 | /// heading shows it, a space and the plain text of its body; any other with its body alone. |
| 163 | pub fn outline(engine: &mut Engine, intro: &Introspector) -> Outcome<Vec<OutlineItem>> { |
| 164 | let mut items = Vec::new(); |
| 165 | for h in res!(intro.query(&Selector::Elem(ElemKind::Heading, None))) { |
| 166 | let outlined = !matches!(h.field("outlined"), Some(Value::Bool(false))); |
| 167 | let marked = match h.field("bookmarked") { |
| 168 | Some(Value::Bool(b)) => *b, |
| 169 | _ => outlined, |
| 170 | }; |
| 171 | if !marked { |
| 172 | continue; |
| 173 | } |
| 174 | let level = match h.field("level") { |
| 175 | Some(Value::Int(l)) if *l >= 1 => (*l - 1) as u8, |
| 176 | _ => 0, |
| 177 | }; |
| 178 | let body = match h.field("body") { |
| 179 | Some(Value::Content(c)) => c.plain_text(), |
| 180 | _ => String::new(), |
| 181 | }; |
| 182 | let page = match h.location().and_then(|l| intro.page(l)) { |
| 183 | Some(p) if p >= 1 => (p - 1) as usize, |
| 184 | _ => continue, |
| 185 | }; |
| 186 | let title = match (h.field("numbering"), h.location()) { |
| 187 | (None | Some(Value::None) | Some(Value::Auto), _) => body, |
| 188 | (Some(numbering), Some(loc)) => { |
| 189 | let numbering = numbering.clone(); |
| 190 | let numbers = res!(lookup::display_counter( |
| 191 | engine, &lookup::elem_counter(ElemKind::Heading), loc, &numbering, h.span())); |
| 192 | fmt!("{} {}", numbers.plain_text(), body) |
| 193 | }, |
| 194 | (Some(_), None) => body, |
| 195 | }; |
| 196 | items.push(OutlineItem { title, page, level }); |
| 197 | } |
| 198 | Ok(items) |
| 199 | } |
| 200 | |
| 201 | /// Renders each page of the pass to an SVG document, in order. A pass that has not settled is discarded |
| 202 | /// whole, so the pages held are those of the pass that did. |
| 203 | #[derive(Debug, Default)] |
| 204 | pub struct VectorSink { |
| 205 | pages: Vec<String>, |
| 206 | finished: bool, |
| 207 | } |
| 208 | |
| 209 | impl VectorSink { |
| 210 | /// The pages, once the fixpoint has run `finish`. |
| 211 | pub fn into_pages(self) -> Option<Vec<String>> { |
| 212 | if self.finished { Some(self.pages) } else { None } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | impl PageSink for VectorSink { |
| 217 | fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> { |
| 218 | self.pages.push(res!(svg::render_page(&page))); |
| 219 | Ok(()) |
| 220 | } |
| 221 | |
| 222 | fn discard_pass(&mut self) -> Outcome<()> { |
| 223 | self.pages.clear(); |
| 224 | self.finished = false; |
| 225 | Ok(()) |
| 226 | } |
| 227 | |
| 228 | fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> { |
| 229 | self.finished = true; |
| 230 | Ok(()) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// Takes each page of the pass as the live view's changed-only delta ([`crate::delta`]): the page's content |
| 235 | /// id and the page itself. Nothing is drawn while a pass runs, because the fixpoint cannot say a pass has |
| 236 | /// settled before its last page: a pass that has not is discarded whole and the builder restarts with it, |
| 237 | /// so none of its pages is ever drawn. `finish`, which runs once, after the pass that stands, draws each |
| 238 | /// page the consumer lacks inside the window and hands its SVG to `out`, and keeps every page as a |
| 239 | /// [`Kept`] for the door to draw any one of them on request. |
| 240 | #[derive(Debug)] |
| 241 | pub struct DeltaSink<C: Changed> { |
| 242 | build: Option<Builder>, // none once the delta is finished |
| 243 | out: C, |
| 244 | head: Option<(Head, Kept)>, |
| 245 | } |
| 246 | |
| 247 | impl<C: Changed> DeltaSink<C> { |
| 248 | /// A sink against the consumer's `known` ids, stepping `prior_version`, sending each changed page inside |
| 249 | /// `want` (every changed page when none) to `out`. |
| 250 | pub fn new(known: &[u64], prior_version: u32, want: Option<Window>, out: C) -> Self { |
| 251 | Self { build: Some(Builder::windowed(known, prior_version, want)), out, head: None } |
| 252 | } |
| 253 | |
| 254 | /// The delta's head, the consumer of its changed pages and the pages kept, once the fixpoint has run |
| 255 | /// `finish`. |
| 256 | pub fn into_delta(self) -> Option<(Head, C, Kept)> { |
| 257 | match self.head { |
| 258 | Some((head, kept)) => Some((head, self.out, kept)), |
| 259 | None => None, |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl<C: Changed> PageSink for DeltaSink<C> { |
| 265 | fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> { |
| 266 | match self.build.as_mut() { |
| 267 | Some(b) => { |
| 268 | b.page(page); |
| 269 | Ok(()) |
| 270 | }, |
| 271 | None => Err(err!("The delta sink was handed a page after it finished."; Bug)), |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | fn discard_pass(&mut self) -> Outcome<()> { |
| 276 | match self.build.as_mut() { |
| 277 | Some(b) => { |
| 278 | b.restart(); |
| 279 | Ok(()) |
| 280 | }, |
| 281 | None => Err(err!("The delta sink discarded a pass after it finished."; Bug)), |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> { |
| 286 | let build = match self.build.take() { |
| 287 | Some(b) => b, |
| 288 | None => return Err(err!("The delta sink finished twice."; Bug)), |
| 289 | }; |
| 290 | self.head = Some(res!(build.finish(&mut self.out))); |
| 291 | Ok(()) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Counts pages and drops them. |
| 296 | #[derive(Debug, Default)] |
| 297 | pub struct CountSink { |
| 298 | pub pages: u32, |
| 299 | pub finished: bool, |
| 300 | } |
| 301 | |
| 302 | impl PageSink for CountSink { |
| 303 | fn page(&mut self, _engine: &mut Engine, _page: Page) -> Outcome<()> { |
| 304 | self.pages += 1; |
| 305 | Ok(()) |
| 306 | } |
| 307 | |
| 308 | fn discard_pass(&mut self) -> Outcome<()> { |
| 309 | self.pages = 0; |
| 310 | Ok(()) |
| 311 | } |
| 312 | |
| 313 | fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> { |
| 314 | self.finished = true; |
| 315 | Ok(()) |
| 316 | } |
| 317 | } |