docxide-pdf 0.9.0

CLI and Library for converting DOCX files to PDF, matching Microsoft Word's output as closely as possible
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::collections::HashMap;
use std::io::Read;

use crate::model::{Alignment, HorizontalPosition, LineSpacing, Paragraph, Textbox};

use super::images::{extent_dimensions, parse_anchor_position};
use super::numbering::NumberingInfo;
use super::runs::parse_runs;
use super::styles::{StylesInfo, ThemeFonts, parse_alignment, parse_line_spacing};
use super::{DML_NS, MC_NS_TOP, WML_NS, WPD_NS, WPS_NS, twips_attr, wml, wml_attr};

pub(super) fn parse_txbx_content_paragraphs<R: Read + std::io::Seek>(
    txbx_content: roxmltree::Node,
    styles: &StylesInfo,
    theme: &ThemeFonts,
    rels: &HashMap<String, String>,
    zip: &mut zip::ZipArchive<R>,
    numbering: &NumberingInfo,
) -> Vec<Paragraph> {
    let mut paragraphs = Vec::new();
    let mut counters: HashMap<(String, u8), u32> = HashMap::new();
    let mut last_seen_level: HashMap<String, u8> = HashMap::new();
    for p in txbx_content
        .children()
        .filter(|n| n.tag_name().name() == "p" && n.tag_name().namespace() == Some(WML_NS))
    {
        let parsed = parse_runs(p, styles, theme, rels, zip, numbering);
        let ppr = wml(p, "pPr");
        let para_style_id = ppr
            .and_then(|ppr| wml_attr(ppr, "pStyle"))
            .unwrap_or(&styles.default_paragraph_style_id);
        let para_style = styles.paragraph_styles.get(para_style_id);
        let alignment = ppr
            .and_then(|ppr| wml_attr(ppr, "jc"))
            .map(parse_alignment)
            .or_else(|| para_style.and_then(|s| s.alignment))
            .unwrap_or(Alignment::Left);
        let inline_spacing = ppr.and_then(|ppr| wml(ppr, "spacing"));
        let space_before = inline_spacing
            .and_then(|n| twips_attr(n, "before"))
            .or_else(|| para_style.and_then(|s| s.space_before))
            .unwrap_or(0.0);
        let space_after = inline_spacing
            .and_then(|n| twips_attr(n, "after"))
            .or_else(|| para_style.and_then(|s| s.space_after))
            .unwrap_or(0.0);
        let line_spacing = Some(
            inline_spacing
                .and_then(|n| {
                    n.attribute((WML_NS, "line"))
                        .and_then(|v| v.parse::<f32>().ok())
                        .map(|line_val| parse_line_spacing(n, line_val))
                })
                .or_else(|| para_style.and_then(|s| s.line_spacing))
                .unwrap_or(LineSpacing::Auto(1.0)),
        );
        let tab_stops = ppr.map(super::parse_tab_stops).unwrap_or_default();
        let num_pr = ppr.and_then(|ppr| wml(ppr, "numPr"));
        let (mut indent_left, mut indent_hanging, list_label, list_label_font) =
            super::numbering::parse_list_info(
                num_pr,
                numbering,
                &mut counters,
                &mut last_seen_level,
            );
        let mut indent_first_line = 0.0f32;
        let mut indent_right = 0.0f32;
        if let Some(ind) = ppr.and_then(|ppr| wml(ppr, "ind")) {
            if let Some(v) = twips_attr(ind, "left") {
                indent_left = v;
            }
            if let Some(v) = twips_attr(ind, "right") {
                indent_right = v;
            }
            if let Some(v) = twips_attr(ind, "hanging") {
                indent_hanging = v;
            }
            if let Some(v) = twips_attr(ind, "firstLine") {
                indent_first_line = v;
            }
        } else if list_label.is_empty() {
            if let Some(s) = para_style {
                if let Some(v) = s.indent_left {
                    indent_left = v;
                }
                if let Some(v) = s.indent_right {
                    indent_right = v;
                }
                if let Some(v) = s.indent_hanging {
                    indent_hanging = v;
                }
                if let Some(v) = s.indent_first_line {
                    indent_first_line = v;
                }
            }
        }
        paragraphs.push(Paragraph {
            runs: parsed.runs,
            space_before,
            space_after,
            alignment,
            indent_left,
            indent_right,
            indent_hanging,
            indent_first_line,
            list_label,
            list_label_font,
            line_spacing,
            tab_stops,
            extra_line_breaks: parsed.line_break_count,
            floating_images: parsed.floating_images,
            textboxes: parsed.textboxes,
            ..Paragraph::default()
        });
    }
    paragraphs
}

fn resolve_scheme_color(base: [u8; 3], fill_node: roxmltree::Node) -> [u8; 3] {
    let mut lum_mod: Option<f32> = None;
    let mut lum_off: Option<f32> = None;
    for child in fill_node.children() {
        if child.tag_name().namespace() != Some(DML_NS) {
            continue;
        }
        match child.tag_name().name() {
            "lumMod" => {
                lum_mod = child
                    .attribute("val")
                    .and_then(|v| v.parse::<f32>().ok())
                    .map(|v| v / 100_000.0);
            }
            "lumOff" => {
                lum_off = child
                    .attribute("val")
                    .and_then(|v| v.parse::<f32>().ok())
                    .map(|v| v / 100_000.0);
            }
            _ => {}
        }
    }
    if lum_mod.is_none() && lum_off.is_none() {
        return base;
    }
    let m = lum_mod.unwrap_or(1.0);
    let o = lum_off.unwrap_or(0.0);
    [
        ((base[0] as f32 / 255.0 * m + o) * 255.0).clamp(0.0, 255.0) as u8,
        ((base[1] as f32 / 255.0 * m + o) * 255.0).clamp(0.0, 255.0) as u8,
        ((base[2] as f32 / 255.0 * m + o) * 255.0).clamp(0.0, 255.0) as u8,
    ]
}

fn parse_solid_fill(sp_pr: roxmltree::Node, theme: &ThemeFonts) -> Option<[u8; 3]> {
    let fill = sp_pr
        .children()
        .find(|n| n.tag_name().name() == "solidFill" && n.tag_name().namespace() == Some(DML_NS))?;
    // Direct sRGB color
    if let Some(srgb) = fill
        .children()
        .find(|n| n.tag_name().name() == "srgbClr" && n.tag_name().namespace() == Some(DML_NS))
    {
        return srgb.attribute("val").and_then(super::parse_hex_color);
    }
    // Theme color reference
    if let Some(scheme) = fill
        .children()
        .find(|n| n.tag_name().name() == "schemeClr" && n.tag_name().namespace() == Some(DML_NS))
    {
        let val = scheme.attribute("val")?;
        // Map OOXML scheme names to theme element names
        let theme_key = match val {
            "dk1" => "dk1",
            "lt1" => "lt1",
            "dk2" => "dk2",
            "lt2" => "lt2",
            "tx1" => "dk1",
            "tx2" => "dk2",
            "bg1" => "lt1",
            "bg2" => "lt2",
            other => other,
        };
        let base = *theme.colors.get(theme_key)?;
        return Some(resolve_scheme_color(base, scheme));
    }
    None
}

fn parse_body_margins(wsp: roxmltree::Node) -> (f32, f32, f32, f32) {
    let body_pr = wsp
        .children()
        .find(|n| n.tag_name().name() == "bodyPr" && n.tag_name().namespace() == Some(WPS_NS));
    let Some(bp) = body_pr else {
        return (3.6, 7.2, 3.6, 7.2); // Word defaults: 0.05" top/bottom, 0.1" left/right
    };
    let emu_to_pt = |attr: &str, default: f32| -> f32 {
        bp.attribute(attr)
            .and_then(|v| v.parse::<f32>().ok())
            .map(|emu| emu / 12700.0)
            .unwrap_or(default)
    };
    (
        emu_to_pt("tIns", 3.6),
        emu_to_pt("lIns", 7.2),
        emu_to_pt("bIns", 3.6),
        emu_to_pt("rIns", 7.2),
    )
}

pub(super) struct WspResult {
    pub(super) paragraphs: Vec<Paragraph>,
    pub(super) fill_color: Option<[u8; 3]>,
    pub(super) margin_top: f32,
    pub(super) margin_left: f32,
    pub(super) margin_bottom: f32,
    pub(super) margin_right: f32,
}

pub(super) fn parse_textbox_from_wsp<R: Read + std::io::Seek>(
    anchor: roxmltree::Node,
    rels: &HashMap<String, String>,
    zip: &mut zip::ZipArchive<R>,
    styles: &StylesInfo,
    theme: &ThemeFonts,
    numbering: &NumberingInfo,
) -> Option<WspResult> {
    let wsp = anchor
        .descendants()
        .find(|n| n.tag_name().name() == "wsp" && n.tag_name().namespace() == Some(WPS_NS))?;

    // Extract fill color from spPr
    let sp_pr = wsp.children().find(|n| {
        n.tag_name().name() == "spPr" && n.tag_name().namespace() == Some(WPS_NS)
            || n.tag_name().name() == "spPr" && n.tag_name().namespace() == Some(DML_NS)
    });
    let fill_color = sp_pr.and_then(|sp| parse_solid_fill(sp, theme));
    let has_no_fill = sp_pr.is_some_and(|sp| {
        sp.children()
            .any(|n| n.tag_name().name() == "noFill" && n.tag_name().namespace() == Some(DML_NS))
    });

    let (margin_top, margin_left, margin_bottom, margin_right) = parse_body_margins(wsp);

    // Try to get textbox content
    let paragraphs = wsp
        .children()
        .find(|n| n.tag_name().name() == "txbx" && n.tag_name().namespace() == Some(WPS_NS))
        .and_then(|txbx| {
            txbx.children().find(|n| {
                n.tag_name().name() == "txbxContent" && n.tag_name().namespace() == Some(WML_NS)
            })
        })
        .map(|tc| parse_txbx_content_paragraphs(tc, styles, theme, rels, zip, numbering))
        .unwrap_or_default();

    // Return if there's text content OR a visible fill
    if paragraphs.is_empty() && (has_no_fill || fill_color.is_none()) {
        return None;
    }

    Some(WspResult {
        paragraphs,
        fill_color,
        margin_top,
        margin_left,
        margin_bottom,
        margin_right,
    })
}

const VML_NS: &str = "urn:schemas-microsoft-com:vml";

pub(super) fn parse_textbox_from_vml<R: Read + std::io::Seek>(
    pict_node: roxmltree::Node,
    rels: &HashMap<String, String>,
    zip: &mut zip::ZipArchive<R>,
    styles: &StylesInfo,
    theme: &ThemeFonts,
    numbering: &NumberingInfo,
) -> Option<Textbox> {
    let shape = pict_node.children().find(|n| {
        n.tag_name().namespace() == Some(VML_NS)
            && (n.tag_name().name() == "shape" || n.tag_name().name() == "rect")
    })?;
    let textbox = shape
        .children()
        .find(|n| n.tag_name().name() == "textbox" && n.tag_name().namespace() == Some(VML_NS))?;
    let txbx_content = textbox.children().find(|n| {
        n.tag_name().name() == "txbxContent" && n.tag_name().namespace() == Some(WML_NS)
    })?;

    let style_str = shape.attribute("style").unwrap_or("");
    let mut width = 0.0_f32;
    let mut height = 0.0_f32;
    let mut margin_left = 0.0_f32;
    let mut margin_top = 0.0_f32;
    let mut h_relative = "column";
    let mut v_relative = "paragraph";

    for part in style_str.split(';') {
        let part = part.trim();
        if let Some((key, val)) = part.split_once(':') {
            let key = key.trim();
            let val = val.trim();
            let parse_pt =
                |s: &str| -> f32 { s.trim_end_matches("pt").parse::<f32>().unwrap_or(0.0) };
            match key {
                "width" => width = parse_pt(val),
                "height" => height = parse_pt(val),
                "margin-left" => margin_left = parse_pt(val),
                "margin-top" => margin_top = parse_pt(val),
                "mso-position-horizontal-relative" => {
                    h_relative = match val {
                        "page" => "page",
                        "margin" => "margin",
                        _ => "column",
                    };
                }
                "mso-position-vertical-relative" => {
                    v_relative = match val {
                        "page" => "page",
                        "margin" => "margin",
                        _ => "paragraph",
                    };
                }
                _ => {}
            }
        }
    }

    let paragraphs =
        parse_txbx_content_paragraphs(txbx_content, styles, theme, rels, zip, numbering);
    if paragraphs.is_empty() {
        return None;
    }
    Some(Textbox {
        paragraphs,
        width_pt: width,
        height_pt: height,
        h_position: HorizontalPosition::Offset(margin_left),
        h_relative_from: h_relative,
        v_offset_pt: margin_top,
        v_relative_from: v_relative,
        fill_color: None,
        margin_left: 7.2,
        margin_right: 7.2,
        margin_top: 3.6,
        margin_bottom: 3.6,
    })
}

pub(super) fn collect_textboxes_from_paragraph<R: Read + std::io::Seek>(
    para_node: roxmltree::Node,
    rels: &HashMap<String, String>,
    zip: &mut zip::ZipArchive<R>,
    styles: &StylesInfo,
    theme: &ThemeFonts,
    numbering: &NumberingInfo,
) -> Vec<Textbox> {
    let mut textboxes = Vec::new();

    for child in para_node.children() {
        let ns = child.tag_name().namespace();
        let name = child.tag_name().name();
        if ns == Some(MC_NS_TOP) && name == "AlternateContent" {
            let choice = child.children().find(|n| {
                n.tag_name().namespace() == Some(MC_NS_TOP) && n.tag_name().name() == "Choice"
            });
            let fallback = child.children().find(|n| {
                n.tag_name().namespace() == Some(MC_NS_TOP) && n.tag_name().name() == "Fallback"
            });

            if let Some(branch) = choice {
                // DrawingML path: mc:Choice → w:drawing → wp:anchor → wps:wsp → wps:txbx
                for drawing in branch.children().filter(|n| {
                    n.tag_name().namespace() == Some(WML_NS) && n.tag_name().name() == "drawing"
                }) {
                    for container in drawing.children().filter(|n| {
                        n.tag_name().namespace() == Some(WPD_NS) && n.tag_name().name() == "anchor"
                    }) {
                        let (display_w, display_h) = extent_dimensions(container);

                        if let Some(wsp) =
                            parse_textbox_from_wsp(container, rels, zip, styles, theme, numbering)
                        {
                            let (h_position, h_relative, v_offset, v_relative) =
                                parse_anchor_position(container);
                            textboxes.push(Textbox {
                                paragraphs: wsp.paragraphs,
                                width_pt: display_w,
                                height_pt: display_h,
                                h_position,
                                h_relative_from: h_relative,
                                v_offset_pt: v_offset,
                                v_relative_from: v_relative,
                                fill_color: wsp.fill_color,
                                margin_left: wsp.margin_left,
                                margin_right: wsp.margin_right,
                                margin_top: wsp.margin_top,
                                margin_bottom: wsp.margin_bottom,
                            });
                        }
                    }
                }
            } else if let Some(branch) = fallback {
                // VML fallback: mc:Fallback → w:pict → v:shape → v:textbox
                for pict in branch.children().filter(|n| {
                    n.tag_name().namespace() == Some(WML_NS) && n.tag_name().name() == "pict"
                }) {
                    if let Some(tb) =
                        parse_textbox_from_vml(pict, rels, zip, styles, theme, numbering)
                    {
                        textboxes.push(tb);
                    }
                }
                // Also check for w:r/w:pict inside fallback
                for r in branch.children().filter(|n| {
                    n.tag_name().namespace() == Some(WML_NS) && n.tag_name().name() == "r"
                }) {
                    for pict in r.children().filter(|n| {
                        n.tag_name().namespace() == Some(WML_NS) && n.tag_name().name() == "pict"
                    }) {
                        if let Some(tb) =
                            parse_textbox_from_vml(pict, rels, zip, styles, theme, numbering)
                        {
                            textboxes.push(tb);
                        }
                    }
                }
            }
        }
    }
    textboxes
}