You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

intermediate.xml 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <!-- $Id$ -->
  17. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
  18. <document>
  19. <header>
  20. <title>Apache™ FOP: Intermediate Format</title>
  21. <version>$Revision$</version>
  22. </header>
  23. <body>
  24. <note>
  25. Please note that the intermediate formats described here are
  26. <strong>advanced features</strong> and can be ignored by most users of Apache FOP.
  27. </note>
  28. <section id="introduction">
  29. <title>Introduction</title>
  30. <p>
  31. Apache™ FOP now provides two different so-called intermediate formats. The first one
  32. (let's call it the area tree XML format) is basically a 1:1 XML representation of FOP's
  33. area tree as generated by the layout engine. The area tree is conceptually defined in the
  34. <a href="http://www.w3.org/TR/2001/REC-xsl-20011015/slice1.html#section-N742-Formatting">XSL-FO specification in chapter 1.1.2</a>.
  35. Even though the area tree is mentioned in the XSL-FO specification, this part is not
  36. standardized. Therefore, the area tree XML format is a FOP-proprietary XML file format.
  37. The area tree XML can be generated through the area tree XML Renderer (the XMLRenderer).
  38. </p>
  39. <p>
  40. The second intermediate format (which we shall name exactly like this: the intermediate
  41. format)
  42. is a recent addition which tries to meet a slightly different set of goals. It is highly
  43. optimized for speed.
  44. </p>
  45. <p>
  46. The intermediate format can be used to generate intermediate documents that are modified
  47. before they are finally rendered to their ultimate output format. Modifications include
  48. adjusting and changing trait values, adding or modifying area objects, inserting prefabricated
  49. pages, overlays, imposition (n-up, rotation, scaling etc.). Multiple IF files can be combined
  50. to a single output file.
  51. </p>
  52. </section>
  53. <section id="which-if">
  54. <title>Which Intermediate Format to choose?</title>
  55. <p>
  56. Both formats have their use cases, so the choice you will make will depend on your
  57. particular situation. Here is a list of strengths and use cases for both formats:
  58. </p>
  59. <section id="strengths-at">
  60. <title>Area Tree XML (AT XML)</title>
  61. <ul>
  62. <li>1:1 representation of FOP's area tree in XML.</li>
  63. <li>Contains more structure information than the new intermediate format.</li>
  64. <li>Used in FOP's layout engine test suite for regression testing.</li>
  65. </ul>
  66. </section>
  67. <section id="strengths-if">
  68. <title>Intermediate Format (IF)</title>
  69. <ul>
  70. <li>Highly optimized for speed.</li>
  71. <li>Smaller XML files.</li>
  72. <li>Easier to post-process.</li>
  73. <li>XML Schema is available.</li>
  74. <li>
  75. Recommended for use cases where documents are formatted concurrently and later
  76. concatenated to a single print job.
  77. </li>
  78. </ul>
  79. </section>
  80. <p>
  81. More technical information about the two formats can be found on the
  82. <a href="http://wiki.apache.org/xmlgraphics-fop/AreaTreeIntermediateXml/NewDesign">FOP Wiki</a>.
  83. </p>
  84. </section>
  85. <section id="architecture">
  86. <title>Architectural Overview</title>
  87. <figure src="images/if-architecture-overview.png"
  88. alt="Diagram with an architectural overview over the intermediate formats"/>
  89. </section>
  90. <section id="usage">
  91. <title>Usage of the Area Tree XML format (AT XML)</title>
  92. <p>
  93. As already mentioned, the area tree XML format is generated by using the
  94. <strong>XMLRenderer</strong> (MIME type: <strong>application/X-fop-areatree</strong>).
  95. So, you basically set the right MIME type for the output format and process your FO files
  96. as if you would create a PDF file.
  97. </p>
  98. <p>
  99. However, there is an important detail to consider: The
  100. various Renderers don't all use the same font sources. To be able to create the right
  101. area tree for the ultimate output format, you need to create the area tree XML file using
  102. the right font setup. This is achieved by telling the XMLRenderer to mimic another
  103. renderer. This is done by calling the XMLRenderer's mimicRenderer() method with an
  104. instance of the ultimate target renderer as the single parameter. This has a consequence:
  105. An area tree XML file rendered with the Java2DRenderer may not look as expected when it
  106. was actually generated for the PDF renderer. For renderers that use the same font setup,
  107. this restriction does not apply (PDF and PS, for example). Generating the area tree XML
  108. format file is the first step.
  109. </p>
  110. <p>
  111. The second step is to reparse the file using the <strong>AreaTreeParser</strong> which is
  112. found in the org.apache.fop.area package. The pages retrieved from the area tree XML file
  113. are added to an AreaTreeModel instance from where they are normally rendered using one of
  114. the available Renderer implementations. You can find examples for the area tree XML
  115. processing in the
  116. <a href="http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/intermediate/"><code>examples/embedding</code></a>
  117. directory in the FOP distribution.
  118. </p>
  119. <p>
  120. The basic pattern to parse the area tree XML format looks like this:
  121. </p>
  122. <source><![CDATA[
  123. FopFactory fopFactory = FopFactory.newInstance();
  124. // Setup output
  125. OutputStream out = new java.io.FileOutputStream(pdffile);
  126. out = new java.io.BufferedOutputStream(out);
  127. try {
  128. //Setup fonts and user agent
  129. FontInfo fontInfo = new FontInfo();
  130. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  131. //Construct the AreaTreeModel that will received the individual pages
  132. AreaTreeModel treeModel = new RenderPagesModel(userAgent,
  133. MimeConstants.MIME_PDF, fontInfo, out);
  134. //Parse the area tree file into the area tree
  135. AreaTreeParser parser = new AreaTreeParser();
  136. Source src = new StreamSource(myIFFile);
  137. parser.parse(src, treeModel, userAgent);
  138. //Signal the end of the processing. The renderer can finalize the target document.
  139. treeModel.endDocument();
  140. } finally {
  141. out.close();
  142. }]]></source>
  143. <p>
  144. This example simply reads an area tree file and renders it to a PDF file. Please note, that in normal
  145. FOP operation you're shielded from having to instantiate the FontInfo object yourself. This
  146. is normally a task of the AreaTreeHandler which is not present in this scenario. The same
  147. applies to the AreaTreeModel instance, in this case an instance of a subclass called
  148. RenderPagesModel. RenderPagesModel is ideal in this case as it has very little overhead
  149. processing the individual pages. An important line in the example is the call to
  150. <code>endDocument()</code> on the AreaTreeModel. This lets the Renderer know that the processing
  151. is now finished.
  152. </p>
  153. <p>
  154. The area tree XML format can also be used from the <a href="running.html#standalone-start">command-line</a>
  155. by using the "-atin" parameter for specifying the area tree XML as input file. You can also
  156. specify a "mimic renderer" by inserting a MIME type between "-at" and the output file.
  157. </p>
  158. <section id="concat">
  159. <title>Concatenating Documents</title>
  160. <p>
  161. This initial example is obviously not very useful. It would be faster to create the PDF file
  162. directly. As the <a href="http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/examples/embedding/java/embedding/atxml/ExampleConcat.java">ExampleConcat.java</a>
  163. example shows you can easily parse multiple area tree files in a row and add the parsed pages to the
  164. same AreaTreeModel instance which essentially concatenates all the input document to one single
  165. output document.
  166. </p>
  167. </section>
  168. <section id="modifying">
  169. <title>Modifying Documents</title>
  170. <p>
  171. One of the most important use cases for this format is obviously modifying the area
  172. tree XML before finally rendering it to the target format. You can easily use XSLT to process
  173. the AT XML file according to your needs. Please note, that we will currently not formally describe
  174. the area tree XML format. You need to have a good understanding its structure so you don't
  175. create any non-parseable files. We may add an XML Schema and more detailed documentation at a
  176. later time. You're invited to help us with that.
  177. </p>
  178. <note>
  179. The area tree XML format is sensitive to changes in whitespace. If you're not careful,
  180. the modified file may not render correctly.
  181. </note>
  182. </section>
  183. <section id="advanced">
  184. <title>Advanced Use</title>
  185. <p>
  186. The generation of the area tree format as well as it parsing process has been designed to allow
  187. for maximum flexibility and optimization. Please note that you can call <code>setTransformerHandler()</code> on
  188. XMLRenderer to give the XMLRenderer your own TransformerHandler instance in case you would like to
  189. do custom serialization (to a W3C DOM, for example) and/or to directly modify the area tree using
  190. XSLT. The AreaTreeParser on the other side allows you to retrieve a ContentHandler instance where
  191. you can manually send SAX events to to start the parsing process (see <code>getContentHandler()</code>).
  192. </p>
  193. </section>
  194. </section>
  195. <section id="usage-if">
  196. <title>Usage of the Intermediate Format (IF)</title>
  197. <p>
  198. The Intermediate Format (IF) is generated by the <strong>IFSerializer</strong>
  199. (MIME type: <strong>application/X-fop-intermediate-format</strong>).
  200. So, you basically set the right MIME type for the output format and process your FO files
  201. as if you would create a PDF file.
  202. </p>
  203. <p>
  204. The IFSerializer is an implementation of the <strong>IFDocumentHandler</strong> and
  205. <strong>IFPainter</strong> interfaces. The <strong>IFRenderer</strong> class is responsible
  206. for converting FOP's area tree into calls against these two interfaces.
  207. </p>
  208. <ul>
  209. <li>
  210. IFDocumentHandler: This interface is used on the document-level and defines the
  211. overall structure of the Intermediate Format.
  212. </li>
  213. <li>
  214. IFPainter: This interface is used to generate graphical page content like text, images
  215. and borders.
  216. </li>
  217. </ul>
  218. <p>
  219. As with the AT XML, there is an important detail to consider: The various output
  220. implementations don't all use the same font sources. To be able
  221. to create the right IF for the ultimate output file, you need to create the IF file using
  222. the right font setup. This is achieved by telling the IFRenderer (responsible for
  223. converting the area tree into calls to the IFDocumentHandler and IFPainter interfaces)
  224. to mimic another renderer. This is done by calling the IFSerializer's
  225. mimicDocumentHandler() method with an instance of the ultimate target document handler
  226. as the single parameter. This has a consequence: An IF file rendered with the
  227. Java2DDocumentHandler may not look as expected when it was actually generated for the PDF
  228. implementation. For implementations that use the same font setup,
  229. this restriction does not apply (PDF and PS, for example). Generating the Intermediate
  230. Format file is the first step.
  231. </p>
  232. <p>
  233. The second step is to reparse the file using the <strong>IFParser</strong> which is
  234. found in the org.apache.fop.render.intermediate package. The IFParser simply takes an
  235. IFDocumentHandler instance against which it generates the appropriate calls. The IFParser
  236. is implemented as a SAX ContentHandler so you're free to choose the method for
  237. post-processing the IF file(s). You can use XSLT or write SAX- or DOM-based code to
  238. manipulate the contents. You can find examples for the Intermediate Format
  239. processing in the
  240. <a href="http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/intermediate/"><code>examples/embedding</code></a>
  241. directory in the FOP distribution.
  242. </p>
  243. <p>
  244. The basic pattern to parse the intermediate format looks like this:
  245. </p>
  246. <source><![CDATA[
  247. FopFactory fopFactory = FopFactory.newInstance();
  248. // Setup output
  249. OutputStream out = new java.io.FileOutputStream(pdffile);
  250. out = new java.io.BufferedOutputStream(out);
  251. try {
  252. //Setup user agent
  253. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  254. //Create IFDocumentHandler instance
  255. IFDocumentHandler targetHandler;
  256. String mime = MimeConstants.MIME_PDF;
  257. targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
  258. userAgent, mime);
  259. //Setup fonts
  260. IFUtil.setupFonts(targetHandler);
  261. //Tell the target handler where to write the PDF to
  262. targetHandler.setResult(new StreamResult(pdffile));
  263. //Parse the IF file
  264. IFParser parser = new IFParser();
  265. Source src = new StreamSource(myIFFile);
  266. parser.parse(src, targetHandler, userAgent);
  267. } finally {
  268. out.close();
  269. }]]></source>
  270. <p>
  271. This example simply reads an intermediate file and renders it to a PDF file. Here
  272. IFParser.parse() is used, but you can also just get a SAX ContentHandler by using the
  273. IFParser.getContentHandler() method.
  274. </p>
  275. <section id="concat-if">
  276. <title>Concatenating Documents</title>
  277. <p>
  278. This initial example is obviously not very useful. It would be faster to create the PDF file
  279. directly (without the intermediate step). As the
  280. <a href="http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/examples/embedding/java/embedding/intermediate/ExampleConcat.java">ExampleConcat.java</a>
  281. example shows you can easily parse multiple intermediate files in a row and use the
  282. IFConcatenator class to concatenate page sequences from multiple source files to a single
  283. output file. This particular example does the concatenation on the level of the
  284. IFDocumentHandler interface. You could also do this in XSLT or using SAX on the XML level.
  285. Whatever suits your process best.
  286. </p>
  287. </section>
  288. <section id="modifying-if">
  289. <title>Modifying Documents</title>
  290. <p>
  291. One of the most important use cases for this format is obviously modifying the
  292. intermediate format before finally rendering it to the target format. You can easily use
  293. XSLT to process the IF file according to your needs.
  294. </p>
  295. <p>
  296. There is an XML Schema (located under
  297. <a href="http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/documentation/intermediate-format-ng/">src/documentation/intermediate-format-ng</a>)
  298. that helps you verify that your modified content is correct.
  299. </p>
  300. <p>
  301. For certain output formats there's a caveat: Formats like AFP and PCL do not support
  302. arbitrary transformations on the IF's "viewport" and "g" elements. Possible are
  303. only rotations in 90 degree steps and translations.
  304. </p>
  305. </section>
  306. <section id="advanced-if">
  307. <title>Advanced Use</title>
  308. <p>
  309. The generation of the intermediate format as well as it parsing process has been
  310. designed to allow for maximum flexibility and optimization. So rather than just passing
  311. in a StreamResult to IFSerializer's setResult() method, you can also use a SAXResult
  312. or a DOMResult. And as you've already seen , the IFParser on the other side allows you
  313. to retrieve a ContentHandler instance where you can manually send SAX events to
  314. start the parsing process (see <code>getContentHandler()</code>).
  315. </p>
  316. </section>
  317. </section>
  318. </body>
  319. </document>