XSL-FO:分配矩形而不是内容矩形的绝对定位

XSL-FO: Absolute positioning of allocation rectangle rather than content rectangle

我正在使用 XSL-FO(使用 Apache FOP 作为呈现引擎)实现 'dashboards' 的 PDF 导出。这些仪表板被定义为在特定大小的区域内呈现的一组矩形元素,其位置由百分比 top/left 偏移量和百分比 widths/heights 定义。使用 XSL-FO 绝对定位呈现它似乎是显而易见的解决方案,并且在大多数情况下效果很好。

以下 XSL-FO:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="10pt" id="sequence-id">
<fo:layout-master-set>
<fo:simple-page-master master-name="page-layout" page-width=" 4in " page-height=" 4in ">
    <fo:region-body region-name="document-content" margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page-layout" initial-page-number="1">
<fo:flow flow-name="document-content">
<fo:block-container border="1px solid green" height="3in">
<fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
</fo:block-container>
</fo:flow>
</fo:page-sequence>
</fo:root>

...导致以下输出:

表示仪表板元素内容的绿色区域相互对接,很难看。所以,我想在内容周围添加一些填充。如果我向表示第一个仪表板元素的 block-container 添加填充:

<fo:block-container absolute-position="absolute" padding="5px" width="50%" left="0" top="0" height="40%" border="1px solid red">

...仪表板元素的(红色)边框向外扩展而不是向内添加填充:

这是因为,与 CSS 相比,XSL-FO 中的绝对定位偏移处理的是内容矩形(即填充内),而不是包含填充和边框的矩形。每 the spec:

These properties set the position of the content-rectangle of the associated area.

我想通过绝对定位属性在分配给仪表板元素的区域内添加 padding/a 边框。我已经尝试过将内容放入另一个嵌套的 block-containerblock 中的各种方法,但我尝试过的任何方法都无法正常工作。作为最后的手段,我可​​以计算 offsets/dimensions 以有效地在元素之间留下一些空白 space ,但这感觉应该由渲染引擎自动处理。有什么建议吗?

适用于所有维度的最简单方法...在块容器内添加一个块,您可以在其中添加所有内容。这个块应该有你想要的保证金。所以:

    <fo:flow flow-name="document-content">
        <fo:block-container border="1px solid green" height="3in">
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
                <fo:block margin="5px">
                    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
        </fo:block-container>
    </fo:flow>

产生这个: