pub fn finish(mut self) -> Vec<u8> {
        self.offsets.sort();

        let xref_len = 1 + self.offsets.last().map_or(0, |p| p.0.get());
        let xref_offset = self.buf.len();

        self.buf.extend(b"xref\n0 ");
        self.buf.push_int(xref_len);
        self.buf.push(b'\n');

        let mut idx = 0;
        let mut free = 0;

        loop {
            // Find the next free entry.
            let start = idx;
            let mut link = free + 1;
            while self.offsets.get(idx).map_or(false, |(id, _)| link == id.get()) {
                idx += 1;
                link += 1;
            }

            // A free entry links to the next free entry.
            let gen = if free == 0 { "65535" } else { "00000" };
            write!(self.buf, "{:010} {} f\r\n", link % xref_len, gen).unwrap();
            free = link;

            // A used entry contains the offset of the object in the file.
            for &(_, offset) in &self.offsets[start .. idx] {
                write!(self.buf, "{:010} 00000 n\r\n", offset).unwrap();
            }

            if idx >= self.offsets.len() {
                break;
            }
        }

        // Write the trailer dictionary.
        self.buf.extend(b"trailer\n");

        let mut trailer = Obj::direct(&mut self.buf, 0).dict();
        trailer.pair(Name(b"Size"), xref_len);

        if let Some(catalog_id) = self.catalog_id {
            trailer.pair(Name(b"Root"), catalog_id);
        }

        if let Some(info_id) = self.info_id {
            trailer.pair(Name(b"Info"), info_id);
        }

        trailer.finish();

        // Write where the cross-reference table starts.
        self.buf.extend(b"\nstartxref\n");
        write!(self.buf, "{}", xref_offset).unwrap();

        // Write the end of file marker.
        self.buf.extend(b"\n%%EOF");
        self.buf
    }
/// Write the cross-reference table and file trailer and return the
/// underlying buffer.
Write the cross-reference table and file trailer and return the
underlying buffer.
Write the cross - reference table and file trailer and return the underlying buffer .
pub fn finish(mut self) -> Vec<u8> {
        self.offsets.sort();
        let xref_len = 1 + self.offsets.last().map_or(0, |p| p.0.get());
        let xref_offset = self.buf.len();
        self.buf.extend(b"xref\n0 ");
        self.buf.push_int(xref_len);
        self.buf.push(b'\n');
        let mut idx = 0;
        let mut free = 0;
        loop {
            // Find the next free entry.
            let start = idx;
            let mut link = free + 1;
            while self.offsets.get(idx).map_or(false, |(id, _)| link == id.get()) {
                idx += 1;
                link += 1;
            }
            // A free entry links to the next free entry.
            let gen = if free == 0 { "65535" } else { "00000" };
            write!(self.buf, "{:010} {} f\r\n", link % xref_len, gen).unwrap();
            free = link;
            // A used entry contains the offset of the object in the file.
            for &(_, offset) in &self.offsets[start .. idx] {
                write!(self.buf, "{:010} 00000 n\r\n", offset).unwrap();
            }
            if idx >= self.offsets.len() {
                break;
            }
        }
        // Write the trailer dictionary.
        self.buf.extend(b"trailer\n");
        let mut trailer = Obj::direct(&mut self.buf, 0).dict();
        trailer.pair(Name(b"Size"), xref_len);
        if let Some(catalog_id) = self.catalog_id {
            trailer.pair(Name(b"Root"), catalog_id);
        }
        if let Some(info_id) = self.info_id {
            trailer.pair(Name(b"Info"), info_id);
        }
        trailer.finish();
        // Write where the cross-reference table starts.
        self.buf.extend(b"\nstartxref\n");
        write!(self.buf, "{}", xref_offset).unwrap();
        // Write the end of file marker.
        self.buf.extend(b"\n%%EOF");
        self.buf
    }
pub fn finish ( mut self ) -> Vec < u8 > { self . offsets . sort ( ) ; let xref_len = 1 + self . offsets . last ( ) . map_or ( 0 , | p | p . 0 . get ( ) ) ; let xref_offset = self . buf . len ( ) ; self . buf . extend ( b" \n " ) ; self . buf . push_int ( xref_len ) ; self . buf . push ( b'\n' ) ; let mut idx = 0 ; let mut free = 0 ; loop { // Find the next free entry. let start = idx ; let mut link = free + 1 ; while self . offsets . get ( idx ) . map_or ( false , | ( id , _ ) | link == id . get ( ) ) { idx += 1 ; link += 1 ; } // A free entry links to the next free entry. let gen = if free == 0 { " " } else { " " } ; write ! ( self buf " \r \n " link xref_len gen ) . unwrap ( ) ; free = link ; // A used entry contains the offset of the object in the file. for & ( _ , offset ) in & self . offsets [ start .. idx ] { write ! ( self buf " \r \n " offset ) . unwrap ( ) ; } if idx >= self . offsets . len ( ) { break ; } } // Write the trailer dictionary. self . buf . extend ( b" \n " ) ; let mut trailer = Obj :: direct ( & mut self . buf , 0 ) . dict ( ) ; trailer . pair ( Name ( b" " ) , xref_len ) ; if let Some ( catalog_id ) = self . catalog_id { trailer . pair ( Name ( b" " ) , catalog_id ) ; } if let Some ( info_id ) = self . info_id { trailer . pair ( Name ( b" " ) , info_id ) ; } trailer . finish ( ) ; // Write where the cross-reference table starts. self . buf . extend ( b" \n \n " ) ; write ! ( self buf " " xref_offset ) . unwrap ( ) ; // Write the end of file marker. self . buf . extend ( b" \n " ) ; self . buf }
Write the cross-reference table and file trailer and return the
underlying buffer.
Write the cross - reference table and file trailer and return the underlying buffer .
mut self
mut self