Skip to content

Anchored images ignore relativeFrom and land in the page corner #10

Description

@mantissaman

Split out of #3.

An anchored (floating) image is drawn in the top left corner of the page instead of next to its paragraph, which in practice means it disappears off the printable area.

Cause

resolve_anchor_images in crates/rdocx-layout/src/engine.rs takes the raw offsets and uses them as absolute page coordinates:

let x = anchor.pos_h_offset.to_pt();
let y = anchor.pos_v_offset.to_pt();

It never looks at relativeFrom. In the file attached to #3 the anchor is:

<wp:positionH relativeFrom="column"><wp:posOffset>60325</wp:posOffset></wp:positionH>
<wp:positionV relativeFrom="paragraph"><wp:posOffset>635</wp:posOffset></wp:positionV>

Those offsets are relative to the column and to the paragraph, and they are tiny, about 4.75pt and 0.05pt. Treated as page coordinates they put the image in the very corner of the sheet.

CT_Anchor already parses pos_h_relative_from and pos_v_relative_from. The values are simply not used.

Fix

Resolve the offset against the frame named by relativeFrom (page, margin, column, paragraph, character and so on) rather than against the page origin. That needs the paragraph's laid out position, so it has to happen after pagination, which is where resolve_anchor_images already runs.

Worth noting that PR #2 touches this same function and adds alignment based positioning (pos_h_align, pos_v_align). Whoever picks this up should coordinate with that PR rather than work around it.

Reproduce

cargo run -p rdocx-cli -- render office2pdf_test.docx -o out

The image in word/media/image1.png does not appear anywhere on the page.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions