docxide-pdf 0.11.0

Library and CLI for converting DOCX files to PDF, matching Microsoft Word's output as closely as possible
Documentation
use std::collections::HashMap;
use std::io::Read;

use crate::model::{ColumnDef, ColumnsConfig, DocGridType, HeaderFooter, SectionBreakType, SectionProperties};

use super::headers_footers::parse_header_footer_xml;
use super::relationships::parse_part_relationships;
use super::styles::{StylesInfo, ThemeFonts};
use super::{REL_NS, WML_NS, read_zip_text, twips_attr, twips_to_pts, wml, wml_bool};

pub(super) fn parse_section_properties<R: Read + std::io::Seek>(
    sect_node: roxmltree::Node,
    rels: &HashMap<String, String>,
    styles: &StylesInfo,
    theme: &ThemeFonts,
    zip: &mut zip::ZipArchive<R>,
    default_line_pitch: f32,
    gutter_at_top: bool,
) -> SectionProperties {
    let pg_sz = wml(sect_node, "pgSz");
    let pg_mar = wml(sect_node, "pgMar");
    let doc_grid = wml(sect_node, "docGrid");

    let page_width = pg_sz.and_then(|n| twips_attr(n, "w")).unwrap_or(612.0);
    let page_height = pg_sz.and_then(|n| twips_attr(n, "h")).unwrap_or(792.0);
    let mut margin_top = pg_mar.and_then(|n| twips_attr(n, "top")).unwrap_or(72.0);
    let margin_bottom = pg_mar.and_then(|n| twips_attr(n, "bottom")).unwrap_or(72.0);
    let mut margin_left = pg_mar.and_then(|n| twips_attr(n, "left")).unwrap_or(72.0);
    let mut margin_right = pg_mar.and_then(|n| twips_attr(n, "right")).unwrap_or(72.0);
    let header_margin = pg_mar.and_then(|n| twips_attr(n, "header")).unwrap_or(36.0);
    let footer_margin = pg_mar.and_then(|n| twips_attr(n, "footer")).unwrap_or(36.0);

    let gutter = pg_mar.and_then(|n| twips_attr(n, "gutter")).unwrap_or(0.0);
    if gutter > 0.0 {
        if gutter_at_top {
            margin_top += gutter;
        } else if wml_bool(sect_node, "rtlGutter").unwrap_or(false) {
            margin_right += gutter;
        } else {
            margin_left += gutter;
        }
    }
    let line_pitch = doc_grid
        .and_then(|n| twips_attr(n, "linePitch"))
        .unwrap_or(default_line_pitch);

    let grid_type = doc_grid
        .and_then(|n| n.attribute((WML_NS, "type")))
        .map(|v| match v {
            "lines" => DocGridType::Lines,
            "linesAndChars" => DocGridType::LinesAndChars,
            "snapToChars" => DocGridType::SnapToChars,
            _ => DocGridType::Default,
        })
        .unwrap_or(DocGridType::Default);

    let different_first_page = wml(sect_node, "titlePg").is_some();

    let pg_num_type = wml(sect_node, "pgNumType");
    let page_num_start = pg_num_type
        .and_then(|n| n.attribute((WML_NS, "start")))
        .and_then(|v| v.parse::<u32>().ok());
    let page_num_format = pg_num_type
        .and_then(|n| n.attribute((WML_NS, "fmt")))
        .map(|v| v.to_string());

    let break_type = wml(sect_node, "type")
        .and_then(|n| n.attribute((WML_NS, "val")))
        .map(|v| match v {
            "continuous" => SectionBreakType::Continuous,
            "oddPage" => SectionBreakType::OddPage,
            "evenPage" => SectionBreakType::EvenPage,
            _ => SectionBreakType::NextPage,
        })
        .unwrap_or(SectionBreakType::NextPage);

    let available = page_width - margin_left - margin_right;
    let columns = wml(sect_node, "cols").and_then(|cols_node| {
        let num: u32 = cols_node
            .attribute((WML_NS, "num"))
            .and_then(|v| v.parse().ok())
            .unwrap_or(1);
        let equal_width = cols_node
            .attribute((WML_NS, "equalWidth"))
            .map(|v| v == "1" || v == "true")
            .unwrap_or(true);
        let sep = cols_node
            .attribute((WML_NS, "sep"))
            .map(|v| v == "1" || v == "true")
            .unwrap_or(false);

        let child_cols: Vec<_> = cols_node
            .children()
            .filter(|c| c.tag_name().name() == "col" && c.tag_name().namespace() == Some(WML_NS))
            .collect();

        let col_defs: Vec<ColumnDef> = if !equal_width && !child_cols.is_empty() {
            child_cols
                .iter()
                .map(|c| ColumnDef {
                    width: twips_attr(*c, "w").unwrap_or(0.0),
                    space: twips_attr(*c, "space").unwrap_or(0.0),
                })
                .collect()
        } else if num > 1 {
            let default_space = cols_node
                .attribute((WML_NS, "space"))
                .and_then(|v| v.parse::<f32>().ok())
                .map(twips_to_pts)
                .unwrap_or(36.0);
            let col_width = (available - (num - 1) as f32 * default_space) / num as f32;
            (0..num)
                .map(|i| ColumnDef {
                    width: col_width.max(1.0),
                    space: if i < num - 1 { default_space } else { 0.0 },
                })
                .collect()
        } else {
            return None;
        };

        Some(ColumnsConfig {
            columns: col_defs,
            sep,
        })
    });

    let find_ref_rid = |tag: &str, hf_type: &str| -> Option<&str> {
        sect_node.children().find_map(|child| {
            if child.tag_name().namespace() == Some(WML_NS)
                && child.tag_name().name() == tag
                && child.attribute((WML_NS, "type")) == Some(hf_type)
            {
                child.attribute((REL_NS, "id"))
            } else {
                None
            }
        })
    };

    let resolve_hf =
        |tag: &str, hf_type: &str, zip: &mut zip::ZipArchive<R>| -> Option<HeaderFooter> {
            let rid = find_ref_rid(tag, hf_type)?;
            let target = rels.get(rid)?;
            let zip_path = if let Some(stripped) = target.strip_prefix('/') {
                stripped.to_string()
            } else {
                format!("word/{}", target)
            };
            let part_rels = parse_part_relationships(zip, &zip_path);
            let xml_text = read_zip_text(zip, &zip_path)?;
            parse_header_footer_xml(&xml_text, styles, theme, &part_rels, zip)
        };

    let header_default = resolve_hf("headerReference", "default", zip);
    let header_first = resolve_hf("headerReference", "first", zip);
    let header_even = resolve_hf("headerReference", "even", zip);
    let footer_default = resolve_hf("footerReference", "default", zip);
    let footer_first = resolve_hf("footerReference", "first", zip);
    let footer_even = resolve_hf("footerReference", "even", zip);

    SectionProperties {
        page_width,
        page_height,
        margin_top,
        margin_bottom,
        margin_left,
        margin_right,
        header_margin,
        footer_margin,
        header_default,
        header_first,
        header_even,
        footer_default,
        footer_first,
        footer_even,
        different_first_page,
        line_pitch,
        grid_type,
        break_type,
        columns,
        page_num_start,
        page_num_format,
    }
}