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.

IFPainter.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.intermediate;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Paint;
  22. import java.awt.Rectangle;
  23. import java.awt.geom.AffineTransform;
  24. import java.util.Map;
  25. import javax.xml.transform.Result;
  26. import org.w3c.dom.Document;
  27. import org.apache.fop.apps.FOUserAgent;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.traits.BorderProps;
  30. /**
  31. * Interface used to paint whole documents layouted by Apache FOP.
  32. * <p>
  33. * Call sequence:
  34. * <p>
  35. * <pre>
  36. * startDocument()
  37. * startDocumentHeader()
  38. * [handleExtension()]*
  39. * endDocumentHeader()
  40. * [
  41. * startPageSequence()
  42. * [
  43. * startPage()
  44. * startPageHeader()
  45. * [handleExtension()]*
  46. * endPageHeader()
  47. * startPageContent()
  48. * (#pageContent)+
  49. * endPageContent()
  50. * startPageTrailer()
  51. * (addTarget())*
  52. * endPageTrailer()
  53. * endPage()
  54. * ]*
  55. * endPageSequence()
  56. * ]*
  57. * startDocumentTrailer()
  58. * [handleExtension()]*
  59. * endDocumentTrailer()
  60. * endDocument()
  61. *
  62. * #box:
  63. * startBox()
  64. * (#pageContent)+
  65. * endBox()
  66. *
  67. * #pageContent:
  68. * (
  69. * setFont() |
  70. * drawText() |
  71. * drawRect() |
  72. * drawImage() |
  73. * TODO etc. etc. |
  74. * handleExtensionObject()
  75. * )
  76. * </pre>
  77. */
  78. public interface IFPainter {
  79. /**
  80. * Set the user agent.
  81. * @param userAgent The user agent
  82. */
  83. void setUserAgent(FOUserAgent userAgent);
  84. /**
  85. * Sets the JAXP Result object to receive the generated content.
  86. * @param result the JAXP Result object to receive the generated content
  87. * @throws IFException if an error occurs setting up the output
  88. */
  89. void setResult(Result result) throws IFException;
  90. /**
  91. * Sets the font set to work with.
  92. * @param fontInfo the font info object
  93. */
  94. void setFontInfo(FontInfo fontInfo);
  95. /**
  96. * Sets the default font set (with no custom configuration).
  97. */
  98. void setDefaultFontInfo();
  99. /**
  100. * Indicates whether the painter supports to handle the pages in mixed order rather than
  101. * ascending order.
  102. * @return true if out-of-order handling is supported
  103. */
  104. boolean supportsPagesOutOfOrder();
  105. /**
  106. * Returns the MIME type of the output format that is generated by this implementation.
  107. * @return the MIME type
  108. */
  109. String getMimeType();
  110. /**
  111. * Indicates the start of a document. This method may only be called once before any other
  112. * event method.
  113. * @throws IFException if an error occurs while handling this event
  114. */
  115. void startDocument() throws IFException;
  116. /**
  117. * Indicates the end of a document. This method may only be called once after the whole
  118. * document has been handled. Implementations can release resources (close streams). It is
  119. * an error to call any event method after this method.
  120. * @throws IFException if an error occurs while handling this event
  121. */
  122. void endDocument() throws IFException;
  123. /**
  124. * Indicates the start of the document header. This method is called right after the
  125. * {@code #startDocument()} method. Extensions sent to this painter between
  126. * {@code #startDocumentHeader()} and {@code #endDocumentHeader()} apply to the document as
  127. * a whole (like document metadata).
  128. * @throws IFException if an error occurs while handling this event
  129. */
  130. void startDocumentHeader() throws IFException;
  131. /**
  132. * Indicates the end of the document header. This method is called before the first
  133. * page sequence.
  134. * @throws IFException if an error occurs while handling this event
  135. */
  136. void endDocumentHeader() throws IFException;
  137. /**
  138. * Indicates the start of the document trailer. This method is called after the last
  139. * page sequence. Extensions sent to the painter between
  140. * {@code #startDocumentTrailer()} and {@code #endDocumentTrailer()} apply to the document as
  141. * a whole and is used for document-level content that is only known after all pages have
  142. * been rendered (like named destinations or the bookmark tree).
  143. * @throws IFException if an error occurs while handling this event
  144. */
  145. void startDocumentTrailer() throws IFException;
  146. /**
  147. * Indicates the end of the document trailer. This method is called right before the
  148. * {@code #endDocument()} method.
  149. * @throws IFException if an error occurs while handling this event
  150. */
  151. void endDocumentTrailer() throws IFException;
  152. /**
  153. * Indicates the start of a new page sequence.
  154. * @param id the page sequence's identifier (or null if none is available)
  155. * @throws IFException if an error occurs while handling this event
  156. */
  157. void startPageSequence(String id) throws IFException;
  158. /**
  159. * Indicates the end of a page sequence.
  160. * @throws IFException if an error occurs while handling this event
  161. */
  162. void endPageSequence() throws IFException;
  163. /**
  164. * Indicates the start of a new page.
  165. * @param index the index of the page (0-based)
  166. * @param name the page name (usually the formatted page number)
  167. * @param size the size of the page (equivalent to the MediaBox in PDF)
  168. * @throws IFException if an error occurs while handling this event
  169. */
  170. void startPage(int index, String name, Dimension size) throws IFException;
  171. /**
  172. * Indicates the end of a page
  173. * @throws IFException if an error occurs while handling this event
  174. */
  175. void endPage() throws IFException;
  176. /**
  177. * Indicates the start of the page header.
  178. * @throws IFException if an error occurs while handling this event
  179. */
  180. void startPageHeader() throws IFException;
  181. /**
  182. * Indicates the end of the page header.
  183. * @throws IFException if an error occurs while handling this event
  184. */
  185. void endPageHeader() throws IFException;
  186. /**
  187. * Indicates the start of the page content.
  188. * @throws IFException if an error occurs while handling this event
  189. */
  190. void startPageContent() throws IFException;
  191. /**
  192. * Indicates the end of the page content.
  193. * @throws IFException if an error occurs while handling this event
  194. */
  195. void endPageContent() throws IFException;
  196. /**
  197. * Indicates the start of the page trailer. The page trailer is used for writing down page
  198. * elements which are only know after handling the page itself (like PDF targets).
  199. * @throws IFException if an error occurs while handling this event
  200. */
  201. void startPageTrailer() throws IFException;
  202. /**
  203. * Indicates the end of the page trailer.
  204. * @throws IFException if an error occurs while handling this event
  205. */
  206. void endPageTrailer() throws IFException;
  207. void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect) throws IFException;
  208. void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect) throws IFException;
  209. //For transform, Batik's org.apache.batik.parser.TransformListHandler/Parser can be used
  210. void endViewport() throws IFException;
  211. void startGroup(AffineTransform[] transforms) throws IFException;
  212. void startGroup(AffineTransform transform) throws IFException;
  213. void endGroup() throws IFException;
  214. /**
  215. * Updates the current font.
  216. * @param family the font family (or null if there's no change)
  217. * @param style the font style (or null if there's no change)
  218. * @param weight the font weight (or null if there's no change)
  219. * @param variant the font variant (or null if there's no change)
  220. * @param size the font size (or null if there's no change)
  221. * @param color the text color (or null if there's no change)
  222. * @throws IFException if an error occurs while handling this event
  223. */
  224. void setFont(String family, String style, Integer weight, String variant, Integer size,
  225. Color color) throws IFException;
  226. /**
  227. * Draws text. The initial coordinates (x and y) point to the starting point at the normal
  228. * baseline of the font. The arrays (dx and dy) are optional and can be used to achieve
  229. * effects like kerning.
  230. * @param x X-coordinate of the starting point of the text
  231. * @param y Y-coordinate of the starting point of the text
  232. * @param dx an array of adjustment values for each character in X-direction
  233. * @param dy an array of adjustment values for each character in Y-direction
  234. * @param text the text
  235. * @throws IFException if an error occurs while handling this event
  236. */
  237. void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException;
  238. /**
  239. * Draws a rectangle. Either fill or stroke has to be specified.
  240. * @param rect the rectangle's coordinates and extent
  241. * @param fill the fill paint (may be null)
  242. * @param stroke the stroke color (may be null)
  243. * @throws IFException if an error occurs while handling this event
  244. */
  245. void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException;
  246. /**
  247. * Draws a border rectangle. The border segments are specified through {@code BorderProps}
  248. * instances.
  249. * @param rect the rectangle's coordinates and extent
  250. * @param before the border segment on the before-side (top)
  251. * @param after the border segment on the after-side (bottom)
  252. * @param start the border segment on the start-side (left)
  253. * @param end the border segment on the end-side (right)
  254. * @throws IFException if an error occurs while handling this event
  255. */
  256. void drawBorderRect(Rectangle rect,
  257. BorderProps before, BorderProps after,
  258. BorderProps start, BorderProps end) throws IFException;
  259. /**
  260. * Draws an image identified by a URI inside a given rectangle. This is the equivalent to
  261. * an fo:external-graphic in XSL-FO.
  262. * @param uri the image's URI
  263. * @param rect the rectangle in which the image shall be painted
  264. * @param foreignAttributes a optional Map with foreign attributes (Map<QName,String>)
  265. * @throws IFException if an error occurs while handling this event
  266. */
  267. void drawImage(String uri, Rectangle rect, Map foreignAttributes) throws IFException;
  268. /**
  269. * Draws an image (represented by a DOM document) inside a given rectangle. This is the
  270. * equivalent to an fo:instream-foreign-object in XSL-FO.
  271. * @param doc the DOM document containing the foreign object
  272. * @param rect the rectangle in which the image shall be painted
  273. * @param foreignAttributes a optional Map with foreign attributes (Map<QName,String>)
  274. * @throws IFException if an error occurs while handling this event
  275. */
  276. void drawImage(Document doc, Rectangle rect, Map foreignAttributes)
  277. throws IFException;
  278. //Note: For now, all foreign objects are handled as DOM documents. At the moment, all known
  279. //implementations use a DOM anyway, so optimizing this to work with SAX wouldn't result in
  280. //any performance benefits. The IFRenderer itself has a DOM anyway. Only the IFParser could
  281. //potentially profit if there's an image handler that can efficiently deal with the foreign
  282. //object without building a DOM.
  283. //etc. etc.
  284. /**
  285. * Handles an extension object. This can be a DOM document or any arbitrary
  286. * object. If an implementation doesn't know how to handle a particular extension it is simply
  287. * ignored.
  288. * @param extension the extension object
  289. * @throws IFException if an error occurs while handling this event
  290. */
  291. void handleExtensionObject(Object extension) throws IFException;
  292. //TODO Prototype the following:
  293. //ContentHandler handleExtension() throws Exception
  294. }