如何在xslfo中在<fo:block>和/或<fo:table>被渲染之前获取它们的尺寸?

huangapple go评论80阅读模式
英文:

How to get <fo:block> and/or <fo:table> dimensions before they are rendered in xslfo?

问题

更具体地说,这是关于块元素的高度,这些元素将在使用RenderX呈现的PDF文档中呈现。例如,假设我有以下简单的xslfo输出,其中包含一些文本和一个表格:

<?xml version="1.0" encoding="ISO-8859-1"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
  <fo:simple-page-master master-name="A4">
    <fo:region-body />
  </fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="A4">
  <fo:flow flow-name="xsl-region-body">
    <fo:block>This is some simple text. How much space will I take?</fo:block>
  </fo:flow>
</fo:page-sequence>

<fo:table-header>
  <fo:table-row>
    <fo:table-cell>
      <fo:block font-weight="bold">Car</fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block font-weight="bold">Price</fo:block>
    </fo:table-cell>
  </fo:table-row>
</fo:table-header>

<fo:table-body>
  <fo:table-row>
    <fo:table-cell>
      <fo:block>Volvo</fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block>$50000</fo:block>
    </fo:table-cell>
  </fo:table-row>
  <fo:table-row>
    <fo:table-cell>
      <fo:block>SAAB</fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block>$48000</fo:block>
    </fo:table-cell>
  </fo:table-row>
</fo:table-body>

</fo:table>
</fo:root>

是否有任何方法可以确定渲染为PDF后区域主体(即随机文本和表格)将占用多少高度,使用RenderX呈现的PDF文档?

英文:

More specifically the height of block elements that would be rendered in a pdf document using RenderX. Say for example I have this simple xslfo output with some text and a table:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;fo:root xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot;&gt;

&lt;fo:layout-master-set&gt;
  &lt;fo:simple-page-master master-name=&quot;A4&quot;&gt;
    &lt;fo:region-body /&gt;
  &lt;/fo:simple-page-master&gt;
&lt;/fo:layout-master-set&gt;

&lt;fo:page-sequence master-reference=&quot;A4&quot;&gt;
  &lt;fo:flow flow-name=&quot;xsl-region-body&quot;&gt;
    &lt;fo:block&gt;This is some simple text. How much space will I take?&lt;/fo:block&gt;
  &lt;/fo:flow&gt;
&lt;/fo:page-sequence&gt;

&lt;fo:table-header&gt;
  &lt;fo:table-row&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block font-weight=&quot;bold&quot;&gt;Car&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block font-weight=&quot;bold&quot;&gt;Price&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
  &lt;/fo:table-row&gt;
&lt;/fo:table-header&gt;

&lt;fo:table-body&gt;
  &lt;fo:table-row&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block&gt;Volvo&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block&gt;$50000&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
  &lt;/fo:table-row&gt;
  &lt;fo:table-row&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block&gt;SAAB&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
    &lt;fo:table-cell&gt;
      &lt;fo:block&gt;$48000&lt;/fo:block&gt;
    &lt;/fo:table-cell&gt;
  &lt;/fo:table-row&gt;
&lt;/fo:table-body&gt;

&lt;/fo:table&gt;
&lt;/fo:table-and-caption&gt;
&lt;/fo:root&gt;

Is there anyway that I can determine how much height the region body (i.e.: random text and table) is going to take on a rendered pdf using RenderX?

答案1

得分: 2

你不能在每种情况下都确定内容在格式化之前的高度,但你可以在格式化后计算出来。

理论上,如果你拥有所使用字体的字体度量信息(并且有很多时间),你可以编写一个程序来计算每行可以容纳多少字符,并计算页面上会有多少行。然而,如果你处理的是稍微复杂一些的现实文档,你将不得不处理诸如字距、连字、侧边浮动的干扰,以及页面编号交叉引用到另一页内容的字符数不确定等问题。你最终会编写第二个格式化器来推测第一个格式化器的行为。

在实践中,格式化器提供了其格式化文档中区域的自己的表示(或多个表示)。XSL 1.1包括格式化区域的“区域树”概念(但不包括规范)。 (请参阅https://www.w3.org/TR/xsl11/#clear)

RenderX拥有其中间输出格式,在http://www.renderx.com/reference.html#IntermediateFormatSpecification上有文档记录。

FOP拥有一种中间输出格式,文档记录在https://xmlgraphics.apache.org/fop/2.4/intermediate.html。FOP还有第二种用于测试的区域树表示,但目前我无法确定是否仍在使用中。

Antenna House拥有其Area Tree XML格式,其模式位于https://github.com/AntennaHouse/AreaTree,文档位于https://antennahouse.github.io/AreaTree/en/。

“打印和页面布局社区组”为运行XSL格式化器并在XSLT转换中获取区域树提供了一组XSLT扩展函数。代码位于https://github.com/pplcg/XSLTExtensions,示例位于https://www.w3.org/community/ppl/wiki/XSLTExtensions。不幸的是,这些扩展函数只适用于FOP和Antenna House。

英文:

You can't in every case determine the height of the content before it is formatted, but you can work it out after it's formatted.

In theory, if you had the font metrics for the fonts used (and a lot of time on your hands), you could write a program to work out how many characters would fit on each line and calculate how many lines you'd have on the page. However, if you're dealing with even moderately complex real-world documents, you'd have to deal with things like kerning, ligatures, intrusions from side floats, and the uncertain number of characters in a page-number cross-references to something on another page. You'd end up writing a second formatter to work out what the first formatter would do.

In practice, formatters make available their own representation (or representations) of the areas in the formatted document. The XSL 1.1 includes the concept (but not the specification) of an 'area tree' of the formatted areas. (See https://www.w3.org/TR/xsl11/#clear)

RenderX has its intermediate output format, documented at http://www.renderx.com/reference.html#IntermediateFormatSpecification

FOP has an intermediate output format, documented at https://xmlgraphics.apache.org/fop/2.4/intermediate.html. FOP also had a second area tree representation for use with tests, but right now I can't tell if that's still in use or not.

Antenna House has its Area Tree XML format, with schema at https://github.com/AntennaHouse/AreaTree and documentation at https://antennahouse.github.io/AreaTree/en/.

The Print and Page Layout Community Group produced a set of XSLT extension functions for running an XSL formatter and getting an area tree within your XSLT transformation. Code is at https://github.com/pplcg/XSLTExtensions and examples at https://www.w3.org/community/ppl/wiki/XSLTExtensions. Unfortunately for you, the extension functions only work with FOP and Antenna House.

huangapple
  • 本文由 发表于 2020年9月6日 00:31:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定