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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.geom.AffineTransform;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.util.Map;
  27. import javax.xml.transform.dom.DOMSource;
  28. import org.w3c.dom.Document;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.xmlgraphics.image.loader.Image;
  32. import org.apache.xmlgraphics.image.loader.ImageException;
  33. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  34. import org.apache.xmlgraphics.image.loader.ImageInfo;
  35. import org.apache.xmlgraphics.image.loader.ImageManager;
  36. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  37. import org.apache.xmlgraphics.image.loader.util.ImageUtil;
  38. import org.apache.fop.ResourceEventProducer;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.render.ImageHandler;
  42. import org.apache.fop.render.ImageHandlerRegistry;
  43. import org.apache.fop.render.ImageHandlerUtil;
  44. import org.apache.fop.render.RenderingContext;
  45. import org.apache.fop.traits.BorderProps;
  46. import org.apache.fop.traits.RuleStyle;
  47. /**
  48. * Abstract base class for IFPainter implementations.
  49. */
  50. public abstract class AbstractIFPainter implements IFPainter {
  51. /** logging instance */
  52. private static Log log = LogFactory.getLog(AbstractIFPainter.class);
  53. /** non-URI that can be used in feedback messages that an image is an instream-object */
  54. protected static final String INSTREAM_OBJECT_URI = "(instream-object)";
  55. /** Holds the intermediate format state */
  56. protected IFState state;
  57. /**
  58. * Default constructor.
  59. */
  60. public AbstractIFPainter() {
  61. }
  62. /**
  63. * Returns the intermediate format context object.
  64. * @return the context object
  65. */
  66. protected abstract IFContext getContext();
  67. /**
  68. * Returns the user agent.
  69. * @return the user agent
  70. */
  71. protected FOUserAgent getUserAgent() {
  72. return getContext().getUserAgent();
  73. }
  74. /**
  75. * Returns the FOP factory.
  76. * @return the FOP factory.
  77. */
  78. protected FopFactory getFopFactory() {
  79. return getUserAgent().getFactory();
  80. }
  81. private AffineTransform combine(AffineTransform[] transforms) {
  82. AffineTransform at = new AffineTransform();
  83. for (int i = 0, c = transforms.length; i < c; i++) {
  84. at.concatenate(transforms[i]);
  85. }
  86. return at;
  87. }
  88. /** {@inheritDoc} */
  89. public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
  90. throws IFException {
  91. startViewport(combine(transforms), size, clipRect);
  92. }
  93. /** {@inheritDoc} */
  94. public void startGroup(AffineTransform[] transforms) throws IFException {
  95. startGroup(combine(transforms));
  96. }
  97. /**
  98. * Creates a new RenderingContext instance.
  99. * @return the new rendering context.
  100. */
  101. protected abstract RenderingContext createRenderingContext();
  102. /**
  103. * Loads a preloaded image and draws it using a suitable image handler.
  104. * @param info the information object of the preloaded image
  105. * @param rect the rectangle in which to paint the image
  106. * @throws ImageException if there's an error while processing the image
  107. * @throws IOException if there's an I/O error while loading the image
  108. */
  109. protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
  110. throws ImageException, IOException {
  111. ImageManager manager = getFopFactory().getImageManager();
  112. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  113. ImageHandlerRegistry imageHandlerRegistry = getFopFactory().getImageHandlerRegistry();
  114. //Load and convert the image to a supported format
  115. RenderingContext context = createRenderingContext();
  116. Map hints = createDefaultImageProcessingHints(sessionContext);
  117. context.putHints(hints);
  118. ImageFlavor[] flavors = imageHandlerRegistry.getSupportedFlavors(context);
  119. org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
  120. info, flavors,
  121. hints, sessionContext);
  122. try {
  123. drawImage(img, rect, context);
  124. } catch (IOException ioe) {
  125. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  126. getUserAgent().getEventBroadcaster());
  127. eventProducer.imageWritingError(this, ioe);
  128. }
  129. }
  130. /**
  131. * Creates the default map of processing hints for the image loading framework.
  132. * @param sessionContext the session context for access to resolution information
  133. * @return the default processing hints
  134. */
  135. protected Map createDefaultImageProcessingHints(ImageSessionContext sessionContext) {
  136. Map hints = ImageUtil.getDefaultHints(sessionContext);
  137. //Transfer common foreign attributes to hints
  138. Object conversionMode = getContext().getForeignAttribute(ImageHandlerUtil.CONVERSION_MODE);
  139. if (conversionMode != null) {
  140. hints.put(ImageHandlerUtil.CONVERSION_MODE, conversionMode);
  141. }
  142. return hints;
  143. }
  144. /**
  145. * Draws an image using a suitable image handler.
  146. * @param image the image to be painted (it needs to of a supported image flavor)
  147. * @param rect the rectangle in which to paint the image
  148. * @param context a suitable rendering context
  149. * @throws IOException in case of an I/O error while handling/writing the image
  150. * @throws ImageException if an error occurs while converting the image to a suitable format
  151. */
  152. protected void drawImage(Image image, Rectangle rect,
  153. RenderingContext context) throws IOException, ImageException {
  154. drawImage(image, rect, context, false, null);
  155. }
  156. /**
  157. * Draws an image using a suitable image handler.
  158. * @param image the image to be painted (it needs to of a supported image flavor)
  159. * @param rect the rectangle in which to paint the image
  160. * @param context a suitable rendering context
  161. * @param convert true to run the image through image conversion if that is necessary
  162. * @param additionalHints additional image processing hints
  163. * @throws IOException in case of an I/O error while handling/writing the image
  164. * @throws ImageException if an error occurs while converting the image to a suitable format
  165. */
  166. protected void drawImage(Image image, Rectangle rect,
  167. RenderingContext context, boolean convert, Map additionalHints)
  168. throws IOException, ImageException {
  169. ImageManager manager = getFopFactory().getImageManager();
  170. ImageHandlerRegistry imageHandlerRegistry = getFopFactory().getImageHandlerRegistry();
  171. Image effImage;
  172. context.putHints(additionalHints);
  173. if (convert) {
  174. Map hints = createDefaultImageProcessingHints(getUserAgent().getImageSessionContext());
  175. if (additionalHints != null) {
  176. hints.putAll(additionalHints);
  177. }
  178. effImage = manager.convertImage(image,
  179. imageHandlerRegistry.getSupportedFlavors(context), hints);
  180. } else {
  181. effImage = image;
  182. }
  183. //First check for a dynamically registered handler
  184. ImageHandler handler = imageHandlerRegistry.getHandler(context, effImage);
  185. if (handler == null) {
  186. throw new UnsupportedOperationException(
  187. "No ImageHandler available for image: "
  188. + effImage.getInfo() + " (" + effImage.getClass().getName() + ")");
  189. }
  190. if (log.isTraceEnabled()) {
  191. log.trace("Using ImageHandler: " + handler.getClass().getName());
  192. }
  193. handler.handleImage(context, effImage, rect);
  194. }
  195. /**
  196. * Returns an ImageInfo instance for the given URI. If there's an error, null is returned.
  197. * The caller can assume that any exceptions have already been handled properly. The caller
  198. * simply skips painting anything in this case.
  199. * @param uri the URI identifying the image
  200. * @return the ImageInfo instance or null if there has been an error.
  201. */
  202. protected ImageInfo getImageInfo(String uri) {
  203. ImageManager manager = getFopFactory().getImageManager();
  204. try {
  205. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  206. return manager.getImageInfo(uri, sessionContext);
  207. } catch (ImageException ie) {
  208. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  209. getUserAgent().getEventBroadcaster());
  210. eventProducer.imageError(this, uri, ie, null);
  211. } catch (FileNotFoundException fe) {
  212. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  213. getUserAgent().getEventBroadcaster());
  214. eventProducer.imageNotFound(this, uri, fe, null);
  215. } catch (IOException ioe) {
  216. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  217. getUserAgent().getEventBroadcaster());
  218. eventProducer.imageIOError(this, uri, ioe, null);
  219. }
  220. return null;
  221. }
  222. /**
  223. * Default drawing method for handling an image referenced by a URI.
  224. * @param uri the image's URI
  225. * @param rect the rectangle in which to paint the image
  226. */
  227. protected void drawImageUsingURI(String uri, Rectangle rect) {
  228. ImageManager manager = getFopFactory().getImageManager();
  229. ImageInfo info = null;
  230. try {
  231. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  232. info = manager.getImageInfo(uri, sessionContext);
  233. drawImageUsingImageHandler(info, rect);
  234. } catch (ImageException ie) {
  235. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  236. getUserAgent().getEventBroadcaster());
  237. eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
  238. } catch (FileNotFoundException fe) {
  239. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  240. getUserAgent().getEventBroadcaster());
  241. eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
  242. } catch (IOException ioe) {
  243. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  244. getUserAgent().getEventBroadcaster());
  245. eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
  246. }
  247. }
  248. /**
  249. * Default drawing method for handling a foreign object in the form of a DOM document.
  250. * @param doc the DOM document containing the foreign object
  251. * @param rect the rectangle in which to paint the image
  252. */
  253. protected void drawImageUsingDocument(Document doc, Rectangle rect) {
  254. ImageManager manager = getFopFactory().getImageManager();
  255. ImageInfo info = null;
  256. try {
  257. info = manager.preloadImage(null, new DOMSource(doc));
  258. drawImageUsingImageHandler(info, rect);
  259. } catch (ImageException ie) {
  260. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  261. getUserAgent().getEventBroadcaster());
  262. eventProducer.imageError(this,
  263. (info != null ? info.toString() : INSTREAM_OBJECT_URI), ie, null);
  264. } catch (FileNotFoundException fe) {
  265. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  266. getUserAgent().getEventBroadcaster());
  267. eventProducer.imageNotFound(this,
  268. (info != null ? info.toString() : INSTREAM_OBJECT_URI), fe, null);
  269. } catch (IOException ioe) {
  270. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  271. getUserAgent().getEventBroadcaster());
  272. eventProducer.imageIOError(this,
  273. (info != null ? info.toString() : INSTREAM_OBJECT_URI), ioe, null);
  274. }
  275. }
  276. /** {@inheritDoc} */
  277. public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
  278. BorderProps start, BorderProps end) throws IFException {
  279. if (before != null) {
  280. Rectangle b = new Rectangle(
  281. rect.x, rect.y,
  282. rect.width, before.width);
  283. fillRect(b, before.color);
  284. }
  285. if (end != null) {
  286. Rectangle b = new Rectangle(
  287. rect.x + rect.width - end.width, rect.y,
  288. end.width, rect.height);
  289. fillRect(b, end.color);
  290. }
  291. if (after != null) {
  292. Rectangle b = new Rectangle(
  293. rect.x, rect.y + rect.height - after.width,
  294. rect.width, after.width);
  295. fillRect(b, after.color);
  296. }
  297. if (start != null) {
  298. Rectangle b = new Rectangle(
  299. rect.x, rect.y,
  300. start.width, rect.height);
  301. fillRect(b, start.color);
  302. }
  303. }
  304. /** {@inheritDoc} */
  305. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  306. throws IFException {
  307. Rectangle rect = getLineBoundingBox(start, end, width);
  308. fillRect(rect, color);
  309. }
  310. /**
  311. * Calculates the bounding box for a line. Currently, only horizontal and vertical lines
  312. * are needed and supported.
  313. * @param start the starting point of the line (coordinates in mpt)
  314. * @param end the ending point of the line (coordinates in mpt)
  315. * @param width the line width (in mpt)
  316. * @return the bounding box (coordinates in mpt)
  317. */
  318. protected Rectangle getLineBoundingBox(Point start, Point end, int width) {
  319. if (start.y == end.y) {
  320. int topy = start.y - width / 2;
  321. return new Rectangle(
  322. start.x, topy,
  323. end.x - start.x, width);
  324. } else if (start.x == end.y) {
  325. int leftx = start.x - width / 2;
  326. return new Rectangle(
  327. leftx, start.x,
  328. width, end.y - start.y);
  329. } else {
  330. throw new IllegalArgumentException(
  331. "Only horizontal or vertical lines are supported at the moment.");
  332. }
  333. }
  334. /** {@inheritDoc} */
  335. public void setFont(String family, String style, Integer weight, String variant, Integer size,
  336. Color color) throws IFException {
  337. if (family != null) {
  338. state.setFontFamily(family);
  339. }
  340. if (style != null) {
  341. state.setFontStyle(style);
  342. }
  343. if (weight != null) {
  344. state.setFontWeight(weight.intValue());
  345. }
  346. if (variant != null) {
  347. state.setFontVariant(variant);
  348. }
  349. if (size != null) {
  350. state.setFontSize(size.intValue());
  351. }
  352. if (color != null) {
  353. state.setTextColor(color);
  354. }
  355. }
  356. /**
  357. * Converts a transformation matrix from millipoints to points.
  358. * @param transform the transformation matrix (in millipoints)
  359. * @return the converted transformation matrix (in points)
  360. */
  361. public static AffineTransform toPoints(AffineTransform transform) {
  362. final double[] matrix = new double[6];
  363. transform.getMatrix(matrix);
  364. //Convert from millipoints to points
  365. matrix[4] /= 1000;
  366. matrix[5] /= 1000;
  367. return new AffineTransform(matrix);
  368. }
  369. }