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.

AbstractIFPainter.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Dimension;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.util.Map;
  25. import javax.xml.transform.dom.DOMSource;
  26. import org.w3c.dom.Document;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.xmlgraphics.image.loader.ImageException;
  30. import org.apache.xmlgraphics.image.loader.ImageInfo;
  31. import org.apache.xmlgraphics.image.loader.ImageManager;
  32. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  33. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.events.ResourceEventProducer;
  36. import org.apache.fop.render.ImageHandler;
  37. import org.apache.fop.render.ImageHandlerRegistry;
  38. import org.apache.fop.render.RenderingContext;
  39. /**
  40. * Abstract base class for IFPainter implementations.
  41. */
  42. public abstract class AbstractIFPainter implements IFPainter {
  43. /** logging instance */
  44. private static Log log = LogFactory.getLog(AbstractIFPainter.class);
  45. /** non-URI that can be used in feedback messages that an image is an instream-object */
  46. protected static final String INSTREAM_OBJECT_URI = "(instream-object)";
  47. private FOUserAgent userAgent;
  48. /** Image handler registry */
  49. protected ImageHandlerRegistry imageHandlerRegistry = new ImageHandlerRegistry();
  50. //TODO Move reference to FOPFactory to the user has a chance to add his own implementations
  51. //and so the lookup process isn't redone for each painter instance.
  52. /**
  53. * Default constructor.
  54. */
  55. public AbstractIFPainter() {
  56. }
  57. /** {@inheritDoc} */
  58. public void setUserAgent(FOUserAgent ua) {
  59. if (this.userAgent != null) {
  60. throw new IllegalStateException("The user agent was already set");
  61. }
  62. this.userAgent = ua;
  63. }
  64. /**
  65. * Returns the user agent.
  66. * @return the user agent
  67. */
  68. protected FOUserAgent getUserAgent() {
  69. return this.userAgent;
  70. }
  71. /** {@inheritDoc} */
  72. public void startDocumentHeader() throws IFException {
  73. //nop
  74. }
  75. /** {@inheritDoc} */
  76. public void endDocumentHeader() throws IFException {
  77. //nop
  78. }
  79. /** {@inheritDoc} */
  80. public void startDocumentTrailer() throws IFException {
  81. //nop
  82. }
  83. /** {@inheritDoc} */
  84. public void endDocumentTrailer() throws IFException {
  85. //nop
  86. }
  87. /** {@inheritDoc} */
  88. public void startPageHeader() throws IFException {
  89. //nop
  90. }
  91. /** {@inheritDoc} */
  92. public void endPageHeader() throws IFException {
  93. //nop
  94. }
  95. /** {@inheritDoc} */
  96. public void startPageTrailer() throws IFException {
  97. //nop
  98. }
  99. /** {@inheritDoc} */
  100. public void endPageTrailer() throws IFException {
  101. //nop
  102. }
  103. private AffineTransform combine(AffineTransform[] transforms) {
  104. AffineTransform at = new AffineTransform();
  105. for (int i = 0, c = transforms.length; i < c; i++) {
  106. at.concatenate(transforms[i]);
  107. }
  108. return at;
  109. }
  110. /** {@inheritDoc} */
  111. public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
  112. throws IFException {
  113. startViewport(combine(transforms), size, clipRect);
  114. }
  115. /** {@inheritDoc} */
  116. public void startGroup(AffineTransform[] transforms) throws IFException {
  117. startGroup(combine(transforms));
  118. }
  119. /**
  120. * Creates a new RenderingContext instance.
  121. * @return the new rendering context.
  122. */
  123. protected abstract RenderingContext createRenderingContext();
  124. /**
  125. * Loads a preloaded image and draws it using a suitable image handler.
  126. * @param info the information object of the preloaded image
  127. * @param rect the rectangle in which to paint the image
  128. * @throws ImageException if there's an error while processing the image
  129. * @throws IOException if there's an I/O error while loading the image
  130. */
  131. protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
  132. throws ImageException, IOException {
  133. ImageManager manager = getUserAgent().getFactory().getImageManager();
  134. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  135. //Load and convert the image to a supported format
  136. RenderingContext context = createRenderingContext();
  137. Map hints = ImageUtil.getDefaultHints(sessionContext);
  138. org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
  139. info, imageHandlerRegistry.getSupportedFlavors(context),
  140. hints, sessionContext);
  141. //First check for a dynamically registered handler
  142. ImageHandler handler = imageHandlerRegistry.getHandler(context, img);
  143. if (handler == null) {
  144. throw new UnsupportedOperationException(
  145. "No ImageHandler available for image: "
  146. + info + " (" + img.getClass().getName() + ")");
  147. }
  148. if (log.isDebugEnabled()) {
  149. log.debug("Using ImageHandler: " + handler.getClass().getName());
  150. }
  151. try {
  152. //TODO foreign attributes
  153. handler.handleImage(context, img, rect);
  154. } catch (IOException ioe) {
  155. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  156. getUserAgent().getEventBroadcaster());
  157. eventProducer.imageWritingError(this, ioe);
  158. return;
  159. }
  160. }
  161. /**
  162. * Default drawing method for handling an image referenced by a URI.
  163. * @param uri the image's URI
  164. * @param rect the rectangle in which to paint the image
  165. */
  166. protected void drawImageUsingURI(String uri, Rectangle rect) {
  167. ImageManager manager = getUserAgent().getFactory().getImageManager();
  168. ImageInfo info = null;
  169. try {
  170. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  171. info = manager.getImageInfo(uri, sessionContext);
  172. drawImageUsingImageHandler(info, rect);
  173. } catch (ImageException ie) {
  174. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  175. getUserAgent().getEventBroadcaster());
  176. eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
  177. } catch (FileNotFoundException fe) {
  178. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  179. getUserAgent().getEventBroadcaster());
  180. eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
  181. } catch (IOException ioe) {
  182. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  183. getUserAgent().getEventBroadcaster());
  184. eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
  185. }
  186. }
  187. /**
  188. * Default drawing method for handling a foreign object in the form of a DOM document.
  189. * @param doc the DOM document containing the foreign object
  190. * @param rect the rectangle in which to paint the image
  191. */
  192. protected void drawImageUsingDocument(Document doc, Rectangle rect) {
  193. ImageManager manager = getUserAgent().getFactory().getImageManager();
  194. ImageInfo info = null;
  195. try {
  196. info = manager.preloadImage(null, new DOMSource(doc));
  197. drawImageUsingImageHandler(info, rect);
  198. } catch (ImageException ie) {
  199. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  200. getUserAgent().getEventBroadcaster());
  201. eventProducer.imageError(this,
  202. (info != null ? info.toString() : INSTREAM_OBJECT_URI), ie, null);
  203. } catch (FileNotFoundException fe) {
  204. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  205. getUserAgent().getEventBroadcaster());
  206. eventProducer.imageNotFound(this,
  207. (info != null ? info.toString() : INSTREAM_OBJECT_URI), fe, null);
  208. } catch (IOException ioe) {
  209. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  210. getUserAgent().getEventBroadcaster());
  211. eventProducer.imageIOError(this,
  212. (info != null ? info.toString() : INSTREAM_OBJECT_URI), ioe, null);
  213. }
  214. }
  215. }