f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! Endnotes, which follow the text at the end of the document.

use crate::body::{NoteId, NoteMark};
use crate::note_setup::NoteLabel;

use super::Layout;

#[cfg(test)]
mod tests;

impl Layout<'_> {
    /// The label of an endnote: of the referenced one, numbered at its first reference as the
    /// endnotes of the current section are numbered, or of the endnote being laid out. A mark
    /// outside an endnote shows 1 in the settings' format.
    pub(super) fn endnote_label(&mut self, reference: Option<(NoteId, NoteMark)>) -> NoteLabel {
        let Some((id, mark)) = reference else {
            return self.endnote.unwrap_or(NoteLabel {
                value: 1,
                format: self.settings.notes.endnotes.format,
            });
        };
        if let Some(note) = self.output.endnotes.iter().find(|note| note.id == id) {
            return note.label;
        }
        let setup = self.section_layout(self.section_index).notes.endnotes;
        let note = self.numbered_note((id, mark), &self.output.endnotes, setup, None);
        let label = note.label;
        self.output.endnotes.push(note);
        label
    }

    /// Lays out the separator and then each referenced endnote after the text.
    ///
    /// Paragraph spacing does not collapse across the body-to-separator or
    /// separator-to-endnotes boundaries: the trailing space after the last body paragraph stays
    /// above the separator, the separator's leading space before is dropped from Word 2013 on
    /// (and kept in earlier modes), and the separator's trailing space after stays above the
    /// first endnote's space before.
    pub(super) fn endnotes_flow(&mut self) {
        if self.output.endnotes.is_empty() {
            return;
        }
        self.section_previous = None;
        if let Some(previous) = self.previous.take() {
            self.advance_space(previous.after);
        }
        let separator = self.context.endnotes.separator.clone();
        self.output.story_parts.current = super::StoryPart::Endnotes;
        let drop_before = self.after_break || self.drops_leading_break_space();
        if !separator.is_empty() && !self.page_has_no_line() {
            let height = self.measure_endnote_separator(&separator, drop_before);
            if self.y + height > self.body_end() {
                self.next_column();
            }
        }
        self.after_break = drop_before;
        self.blocks(&separator);
        if let Some(previous) = self.previous.take() {
            self.advance_space(previous.after);
        }
        self.after_break = false;
        let notes = self.output.endnotes.clone();
        for note in &notes {
            let Some(blocks) = self.context.endnotes.by_id.get(&note.id).cloned() else {
                continue;
            };
            self.endnote = Some(note.label);
            self.blocks(&blocks);
        }
        self.endnote = None;
        self.output.story_parts.current = super::StoryPart::Own;
    }
}