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.

AFPGraphics2D.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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.afp;
  19. import java.awt.AlphaComposite;
  20. import java.awt.BasicStroke;
  21. import java.awt.Color;
  22. import java.awt.Dimension;
  23. import java.awt.Font;
  24. import java.awt.FontMetrics;
  25. import java.awt.Graphics;
  26. import java.awt.GraphicsConfiguration;
  27. import java.awt.Image;
  28. import java.awt.Paint;
  29. import java.awt.Rectangle;
  30. import java.awt.Shape;
  31. import java.awt.Stroke;
  32. import java.awt.TexturePaint;
  33. import java.awt.geom.AffineTransform;
  34. import java.awt.geom.Ellipse2D;
  35. import java.awt.geom.Line2D;
  36. import java.awt.geom.PathIterator;
  37. import java.awt.geom.Rectangle2D;
  38. import java.awt.image.BufferedImage;
  39. import java.awt.image.ImageObserver;
  40. import java.awt.image.RenderedImage;
  41. import java.awt.image.renderable.RenderableImage;
  42. import java.io.IOException;
  43. import org.apache.commons.io.output.ByteArrayOutputStream;
  44. import org.apache.commons.logging.Log;
  45. import org.apache.commons.logging.LogFactory;
  46. import org.apache.xmlgraphics.image.loader.ImageInfo;
  47. import org.apache.xmlgraphics.image.loader.ImageSize;
  48. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  49. import org.apache.xmlgraphics.java2d.AbstractGraphics2D;
  50. import org.apache.xmlgraphics.java2d.GraphicContext;
  51. import org.apache.xmlgraphics.java2d.StrokingTextHandler;
  52. import org.apache.xmlgraphics.java2d.TextHandler;
  53. import org.apache.xmlgraphics.ps.ImageEncodingHelper;
  54. import org.apache.xmlgraphics.util.MimeConstants;
  55. import org.apache.xmlgraphics.util.UnitConv;
  56. import org.apache.fop.afp.goca.GraphicsSetLineType;
  57. import org.apache.fop.afp.modca.GraphicsObject;
  58. import org.apache.fop.afp.svg.AFPGraphicsConfiguration;
  59. import org.apache.fop.fonts.FontInfo;
  60. import org.apache.fop.svg.NativeImageHandler;
  61. /**
  62. * This is a concrete implementation of <tt>AbstractGraphics2D</tt> (and
  63. * therefore of <tt>Graphics2D</tt>) which is able to generate GOCA byte
  64. * codes.
  65. *
  66. * @see org.apache.xmlgraphics.java2d.AbstractGraphics2D
  67. */
  68. public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHandler {
  69. private static final Log log = LogFactory.getLog(AFPGraphics2D.class);
  70. private static final int X = 0;
  71. private static final int Y = 1;
  72. private static final int X1 = 0;
  73. private static final int Y1 = 1;
  74. private static final int X2 = 2;
  75. private static final int Y2 = 3;
  76. private static final int X3 = 4;
  77. private static final int Y3 = 5;
  78. /** graphics object */
  79. private GraphicsObject graphicsObj = null;
  80. /** Fallback text handler */
  81. protected TextHandler fallbackTextHandler = new StrokingTextHandler();
  82. /** Custom text handler */
  83. protected TextHandler customTextHandler = null;
  84. /** AFP resource manager */
  85. private AFPResourceManager resourceManager = null;
  86. /** AFP resource info */
  87. private AFPResourceInfo resourceInfo = null;
  88. /** Current AFP state */
  89. private AFPPaintingState paintingState = null;
  90. /** AFP graphics configuration */
  91. private final AFPGraphicsConfiguration graphicsConfig = new AFPGraphicsConfiguration();
  92. /** The AFP FontInfo */
  93. private FontInfo fontInfo;
  94. /**
  95. * Main constructor
  96. *
  97. * @param textAsShapes
  98. * if true, all text is turned into shapes in the convertion. No
  99. * text is output.
  100. * @param paintingState painting state
  101. * @param resourceManager resource manager
  102. * @param resourceInfo resource info
  103. * @param fontInfo font info
  104. */
  105. public AFPGraphics2D(boolean textAsShapes, AFPPaintingState paintingState,
  106. AFPResourceManager resourceManager, AFPResourceInfo resourceInfo,
  107. FontInfo fontInfo) {
  108. super(textAsShapes);
  109. setPaintingState(paintingState);
  110. setResourceManager(resourceManager);
  111. setResourceInfo(resourceInfo);
  112. setFontInfo(fontInfo);
  113. }
  114. /**
  115. * Copy Constructor
  116. *
  117. * @param g2d
  118. * a AFPGraphics2D whose properties should be copied
  119. */
  120. public AFPGraphics2D(AFPGraphics2D g2d) {
  121. super(g2d);
  122. this.paintingState = g2d.paintingState;
  123. this.resourceManager = g2d.resourceManager;
  124. this.resourceInfo = g2d.resourceInfo;
  125. this.fontInfo = g2d.fontInfo;
  126. this.graphicsObj = g2d.graphicsObj;
  127. this.fallbackTextHandler = g2d.fallbackTextHandler;
  128. this.customTextHandler = g2d.customTextHandler;
  129. }
  130. /**
  131. * Sets the AFP resource manager
  132. *
  133. * @param resourceManager the AFP resource manager
  134. */
  135. private void setResourceManager(AFPResourceManager resourceManager) {
  136. this.resourceManager = resourceManager;
  137. }
  138. /**
  139. * Sets the AFP resource info
  140. *
  141. * @param resourceInfo the AFP resource info
  142. */
  143. private void setResourceInfo(AFPResourceInfo resourceInfo) {
  144. this.resourceInfo = resourceInfo;
  145. }
  146. /**
  147. * Returns the GOCA graphics object
  148. *
  149. * @return the GOCA graphics object
  150. */
  151. public GraphicsObject getGraphicsObject() {
  152. return this.graphicsObj;
  153. }
  154. /**
  155. * Sets the GOCA graphics object
  156. *
  157. * @param obj the GOCA graphics object
  158. */
  159. public void setGraphicsObject(GraphicsObject obj) {
  160. this.graphicsObj = obj;
  161. }
  162. /**
  163. * Sets the AFP painting state
  164. *
  165. * @param paintingState the AFP painting state
  166. */
  167. private void setPaintingState(AFPPaintingState paintingState) {
  168. this.paintingState = paintingState;
  169. }
  170. /**
  171. * Returns the AFP painting state
  172. *
  173. * @return the AFP painting state
  174. */
  175. public AFPPaintingState getPaintingState() {
  176. return this.paintingState;
  177. }
  178. /**
  179. * Sets the FontInfo
  180. *
  181. * @param fontInfo the FontInfo
  182. */
  183. private void setFontInfo(FontInfo fontInfo) {
  184. this.fontInfo = fontInfo;
  185. }
  186. /**
  187. * Returns the FontInfo
  188. *
  189. * @return the FontInfo
  190. */
  191. public FontInfo getFontInfo() {
  192. return this.fontInfo;
  193. }
  194. /**
  195. * Sets the GraphicContext
  196. *
  197. * @param gc
  198. * GraphicContext to use
  199. */
  200. public void setGraphicContext(GraphicContext gc) {
  201. this.gc = gc;
  202. }
  203. private int getResolution() {
  204. return this.paintingState.getResolution();
  205. }
  206. /**
  207. * Converts a length value to an absolute value.
  208. * Please note that this only uses the "ScaleY" factor, so this will result
  209. * in a bad value should "ScaleX" and "ScaleY" be different.
  210. * @param length the length
  211. * @return the absolute length
  212. */
  213. public double convertToAbsoluteLength(double length) {
  214. AffineTransform current = getTransform();
  215. double mult = getResolution() / (double)UnitConv.IN2PT;
  216. double factor = -current.getScaleY() / mult;
  217. return length * factor;
  218. }
  219. /** IBM's AFP Workbench paints lines that are wider than expected. We correct manually. */
  220. private static final double GUESSED_WIDTH_CORRECTION = 1.7;
  221. private static final double SPEC_NORMAL_LINE_WIDTH = UnitConv.in2pt(0.01); //"approx" 0.01 inch
  222. private static final double NORMAL_LINE_WIDTH
  223. = SPEC_NORMAL_LINE_WIDTH * GUESSED_WIDTH_CORRECTION;
  224. /**
  225. * Apply the stroke to the AFP graphics object.
  226. * This takes the java stroke and outputs the appropriate settings
  227. * to the AFP graphics object so that the stroke attributes are handled.
  228. *
  229. * @param stroke the java stroke
  230. */
  231. protected void applyStroke(Stroke stroke) {
  232. if (stroke instanceof BasicStroke) {
  233. BasicStroke basicStroke = (BasicStroke) stroke;
  234. // set line width
  235. float lineWidth = basicStroke.getLineWidth();
  236. if (false) {
  237. //Old approach. Retained until verified problems with 1440 resolution
  238. graphicsObj.setLineWidth(Math.round(lineWidth / 2));
  239. } else {
  240. double absoluteLineWidth = lineWidth * Math.abs(getTransform().getScaleY());
  241. double multiplier = absoluteLineWidth / NORMAL_LINE_WIDTH;
  242. graphicsObj.setLineWidth((int)Math.round(multiplier));
  243. //TODO Use GSFLW instead of GSLW for higher accuracy?
  244. }
  245. //No line join, miter limit and end cap support in GOCA. :-(
  246. // set line type/style (note: this is an approximation at best!)
  247. float[] dashArray = basicStroke.getDashArray();
  248. if (paintingState.setDashArray(dashArray)) {
  249. byte type = GraphicsSetLineType.DEFAULT; // normally SOLID
  250. if (dashArray != null) {
  251. type = GraphicsSetLineType.DOTTED; // default to plain DOTTED if dashed line
  252. // float offset = basicStroke.getDashPhase();
  253. if (dashArray.length == 2) {
  254. if (dashArray[0] < dashArray[1]) {
  255. type = GraphicsSetLineType.SHORT_DASHED;
  256. } else if (dashArray[0] > dashArray[1]) {
  257. type = GraphicsSetLineType.LONG_DASHED;
  258. }
  259. } else if (dashArray.length == 4) {
  260. if (dashArray[0] > dashArray[1]
  261. && dashArray[2] < dashArray[3]) {
  262. type = GraphicsSetLineType.DASH_DOT;
  263. } else if (dashArray[0] < dashArray[1]
  264. && dashArray[2] < dashArray[3]) {
  265. type = GraphicsSetLineType.DOUBLE_DOTTED;
  266. }
  267. } else if (dashArray.length == 6) {
  268. if (dashArray[0] > dashArray[1]
  269. && dashArray[2] < dashArray[3]
  270. && dashArray[4] < dashArray[5]) {
  271. type = GraphicsSetLineType.DASH_DOUBLE_DOTTED;
  272. }
  273. }
  274. }
  275. graphicsObj.setLineType(type);
  276. }
  277. } else {
  278. log.warn("Unsupported Stroke: " + stroke.getClass().getName());
  279. }
  280. }
  281. /**
  282. * Apply the java paint to the AFP.
  283. * This takes the java paint sets up the appropriate AFP commands
  284. * for the drawing with that paint.
  285. * Currently this supports the gradients and patterns from batik.
  286. *
  287. * @param paint the paint to convert to AFP
  288. * @param fill true if the paint should be set for filling
  289. * @return true if the paint is handled natively, false if the paint should be rasterized
  290. */
  291. private boolean applyPaint(Paint paint, boolean fill) {
  292. if (paint instanceof Color) {
  293. return true;
  294. }
  295. log.debug("NYI: applyPaint() " + paint + " fill=" + fill);
  296. if (paint instanceof TexturePaint) {
  297. // TexturePaint texturePaint = (TexturePaint)paint;
  298. // BufferedImage bufferedImage = texturePaint.getImage();
  299. // AffineTransform at = paintingState.getTransform();
  300. // int x = (int)Math.round(at.getTranslateX());
  301. // int y = (int)Math.round(at.getTranslateY());
  302. // drawImage(bufferedImage, x, y, null);
  303. }
  304. return false;
  305. }
  306. /**
  307. * Handle the Batik drawing event
  308. *
  309. * @param shape
  310. * the shape to draw
  311. * @param fill
  312. * true if the shape is to be drawn filled
  313. */
  314. private void doDrawing(Shape shape, boolean fill) {
  315. if (!fill) {
  316. graphicsObj.newSegment();
  317. }
  318. graphicsObj.setColor(gc.getColor());
  319. applyPaint(gc.getPaint(), fill);
  320. if (fill) {
  321. graphicsObj.beginArea();
  322. } else {
  323. applyStroke(gc.getStroke());
  324. }
  325. AffineTransform trans = gc.getTransform();
  326. PathIterator iter = shape.getPathIterator(trans);
  327. if (shape instanceof Line2D) {
  328. double[] dstPts = new double[6];
  329. iter.currentSegment(dstPts);
  330. int[] coords = new int[4];
  331. coords[X1] = (int) Math.round(dstPts[X]);
  332. coords[Y1] = (int) Math.round(dstPts[Y]);
  333. iter.next();
  334. iter.currentSegment(dstPts);
  335. coords[X2] = (int) Math.round(dstPts[X]);
  336. coords[Y2] = (int) Math.round(dstPts[Y]);
  337. graphicsObj.addLine(coords);
  338. } else if (shape instanceof Rectangle2D) {
  339. double[] dstPts = new double[6];
  340. iter.currentSegment(dstPts);
  341. int[] coords = new int[4];
  342. coords[X2] = (int) Math.round(dstPts[X]);
  343. coords[Y2] = (int) Math.round(dstPts[Y]);
  344. iter.next();
  345. iter.next();
  346. iter.currentSegment(dstPts);
  347. coords[X1] = (int) Math.round(dstPts[X]);
  348. coords[Y1] = (int) Math.round(dstPts[Y]);
  349. graphicsObj.addBox(coords);
  350. } else if (shape instanceof Ellipse2D) {
  351. double[] dstPts = new double[6];
  352. Ellipse2D elip = (Ellipse2D) shape;
  353. double scale = trans.getScaleX();
  354. double radiusWidth = elip.getWidth() / 2;
  355. double radiusHeight = elip.getHeight() / 2;
  356. graphicsObj.setArcParams(
  357. (int)Math.round(radiusWidth * scale),
  358. (int)Math.round(radiusHeight * scale),
  359. 0,
  360. 0
  361. );
  362. double[] srcPts = new double[] {elip.getCenterX(), elip.getCenterY()};
  363. trans.transform(srcPts, 0, dstPts, 0, 1);
  364. final int mh = 1;
  365. final int mhr = 0;
  366. graphicsObj.addFullArc(
  367. (int)Math.round(dstPts[X]),
  368. (int)Math.round(dstPts[Y]),
  369. mh,
  370. mhr
  371. );
  372. } else {
  373. processPathIterator(iter);
  374. }
  375. if (fill) {
  376. graphicsObj.endArea();
  377. }
  378. }
  379. /**
  380. * Processes a path iterator generating the necessary painting operations.
  381. *
  382. * @param iter PathIterator to process
  383. */
  384. private void processPathIterator(PathIterator iter) {
  385. double[] dstPts = new double[6];
  386. for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) {
  387. switch (iter.currentSegment(dstPts)) {
  388. case PathIterator.SEG_LINETO:
  389. graphicsObj.addLine(new int[] {
  390. (int)Math.round(dstPts[X]),
  391. (int)Math.round(dstPts[Y])
  392. }, true);
  393. break;
  394. case PathIterator.SEG_QUADTO:
  395. graphicsObj.addFillet(new int[] {
  396. (int)Math.round(dstPts[X1]),
  397. (int)Math.round(dstPts[Y1]),
  398. (int)Math.round(dstPts[X2]),
  399. (int)Math.round(dstPts[Y2])
  400. }, true);
  401. break;
  402. case PathIterator.SEG_CUBICTO:
  403. graphicsObj.addFillet(new int[] {
  404. (int)Math.round(dstPts[X1]),
  405. (int)Math.round(dstPts[Y1]),
  406. (int)Math.round(dstPts[X2]),
  407. (int)Math.round(dstPts[Y2]),
  408. (int)Math.round(dstPts[X3]),
  409. (int)Math.round(dstPts[Y3])
  410. }, true);
  411. break;
  412. case PathIterator.SEG_MOVETO:
  413. openingCoords = new int[] {
  414. (int)Math.round(dstPts[X]),
  415. (int)Math.round(dstPts[Y])
  416. };
  417. graphicsObj.setCurrentPosition(openingCoords);
  418. break;
  419. case PathIterator.SEG_CLOSE:
  420. graphicsObj.addLine(openingCoords, true);
  421. break;
  422. default:
  423. log.debug("Unrecognised path iterator type");
  424. break;
  425. }
  426. }
  427. }
  428. /** {@inheritDoc} */
  429. public void draw(Shape shape) {
  430. log.debug("draw() shape=" + shape);
  431. doDrawing(shape, false);
  432. }
  433. /** {@inheritDoc} */
  434. public void fill(Shape shape) {
  435. log.debug("fill() shape=" + shape);
  436. doDrawing(shape, true);
  437. }
  438. /**
  439. * Central handler for IOExceptions for this class.
  440. *
  441. * @param ioe
  442. * IOException to handle
  443. */
  444. public void handleIOException(IOException ioe) {
  445. // TODO Surely, there's a better way to do this.
  446. log.error(ioe.getMessage());
  447. ioe.printStackTrace();
  448. }
  449. /** {@inheritDoc} */
  450. public void drawString(String str, float x, float y) {
  451. try {
  452. if (customTextHandler != null && !textAsShapes) {
  453. customTextHandler.drawString(this, str, x, y);
  454. } else {
  455. fallbackTextHandler.drawString(this, str, x, y);
  456. }
  457. } catch (IOException ioe) {
  458. handleIOException(ioe);
  459. }
  460. }
  461. /** {@inheritDoc} */
  462. public GraphicsConfiguration getDeviceConfiguration() {
  463. return graphicsConfig;
  464. }
  465. /** {@inheritDoc} */
  466. public Graphics create() {
  467. return new AFPGraphics2D(this);
  468. }
  469. /** {@inheritDoc} */
  470. public void dispose() {
  471. this.graphicsObj = null;
  472. }
  473. /** {@inheritDoc} */
  474. public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
  475. return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
  476. }
  477. private BufferedImage buildBufferedImage(Dimension size) {
  478. return new BufferedImage(size.width, size.height,
  479. BufferedImage.TYPE_INT_ARGB);
  480. }
  481. private AFPImageObjectInfo createImageObjectInfo(
  482. RenderedImage img, int x, int y, int width, int height) throws IOException {
  483. ImageInfo imageInfo = new ImageInfo(null, "image/unknown");
  484. ImageSize size = new ImageSize(img.getWidth(), img.getHeight(), 72);
  485. imageInfo.setSize(size);
  486. ImageRendered imageRendered = new ImageRendered(imageInfo, img, null);
  487. RenderedImage renderedImage = imageRendered.getRenderedImage();
  488. // create image object info
  489. AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo();
  490. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
  491. int bitsPerPixel = paintingState.getBitsPerPixel();
  492. imageObjectInfo.setBitsPerPixel(bitsPerPixel);
  493. imageObjectInfo.setResourceInfo(resourceInfo);
  494. int dataHeight = renderedImage.getHeight();
  495. imageObjectInfo.setDataHeight(dataHeight);
  496. int dataWidth = renderedImage.getWidth();
  497. imageObjectInfo.setDataWidth(dataWidth);
  498. boolean colorImages = paintingState.isColorImages();
  499. imageObjectInfo.setColor(colorImages);
  500. ByteArrayOutputStream boas = new ByteArrayOutputStream();
  501. ImageEncodingHelper.encodeRenderedImageAsRGB(renderedImage, boas);
  502. byte[] imageData = boas.toByteArray();
  503. // convert to grayscale
  504. if (!colorImages) {
  505. boas.reset();
  506. imageObjectInfo.setBitsPerPixel(bitsPerPixel);
  507. ImageEncodingHelper.encodeRGBAsGrayScale(
  508. imageData, dataWidth, dataHeight, bitsPerPixel, boas);
  509. imageData = boas.toByteArray();
  510. }
  511. imageObjectInfo.setData(imageData);
  512. if (imageInfo != null) {
  513. imageObjectInfo.setUri(imageInfo.getOriginalURI());
  514. }
  515. // create object area info
  516. AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo();
  517. objectAreaInfo.setX(x);
  518. objectAreaInfo.setY(y);
  519. objectAreaInfo.setWidth(width);
  520. objectAreaInfo.setHeight(height);
  521. int resolution = paintingState.getResolution();
  522. objectAreaInfo.setWidthRes(resolution);
  523. objectAreaInfo.setHeightRes(resolution);
  524. imageObjectInfo.setObjectAreaInfo(objectAreaInfo);
  525. return imageObjectInfo;
  526. }
  527. /**
  528. * Draws an AWT image into a BufferedImage using an AWT Graphics2D implementation
  529. *
  530. * @param img the AWT image
  531. * @param bufferedImage the AWT buffered image
  532. * @param width the image width
  533. * @param height the image height
  534. * @param observer the image observer
  535. * @return true if the image was drawn
  536. */
  537. private boolean drawBufferedImage(Image img, BufferedImage bufferedImage,
  538. int width, int height, ImageObserver observer) {
  539. java.awt.Graphics2D g2d = bufferedImage.createGraphics();
  540. try {
  541. g2d.setComposite(AlphaComposite.SrcOver);
  542. Color color = new Color(1, 1, 1, 0);
  543. g2d.setBackground(color);
  544. g2d.setPaint(color);
  545. g2d.fillRect(0, 0, width, height);
  546. int imageWidth = bufferedImage.getWidth();
  547. int imageHeight = bufferedImage.getHeight();
  548. Rectangle clipRect = new Rectangle(0, 0, imageWidth, imageHeight);
  549. g2d.clip(clipRect);
  550. g2d.setComposite(gc.getComposite());
  551. return g2d.drawImage(img, 0, 0, imageWidth, imageHeight, observer);
  552. } finally {
  553. g2d.dispose(); //drawn so dispose immediately to free system resource
  554. }
  555. }
  556. /** {@inheritDoc} */
  557. public boolean drawImage(Image img, int x, int y, int width, int height,
  558. ImageObserver observer) {
  559. // draw with AWT Graphics2D
  560. Dimension imageSize = new Dimension(width, height);
  561. BufferedImage bufferedImage = buildBufferedImage(imageSize);
  562. boolean drawn = drawBufferedImage(img, bufferedImage, width, height, observer);
  563. if (drawn) {
  564. AffineTransform at = gc.getTransform();
  565. float[] srcPts = new float[] {x, y};
  566. float[] dstPts = new float[srcPts.length];
  567. at.transform(srcPts, 0, dstPts, 0, 1);
  568. x = Math.round(dstPts[X]);
  569. y = Math.round(dstPts[Y]);
  570. try {
  571. // get image object info
  572. AFPImageObjectInfo imageObjectInfo
  573. = createImageObjectInfo(bufferedImage, x, y, width, height);
  574. // create image resource
  575. resourceManager.createObject(imageObjectInfo);
  576. return true;
  577. } catch (IOException ioe) {
  578. handleIOException(ioe);
  579. }
  580. }
  581. return false;
  582. }
  583. /** {@inheritDoc} */
  584. public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
  585. int imgWidth = img.getWidth();
  586. int imgHeight = img.getHeight();
  587. AffineTransform at = paintingState.getData().getTransform();
  588. AffineTransform gat = gc.getTransform();
  589. int graphicsObjectHeight
  590. = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight();
  591. int x = (int)Math.round(at.getTranslateX() + gat.getTranslateX());
  592. int y = (int)Math.round(at.getTranslateY() - (gat.getTranslateY() - graphicsObjectHeight));
  593. int width = (int)Math.round(imgWidth * gat.getScaleX());
  594. int height = (int)Math.round(imgHeight * -gat.getScaleY());
  595. try {
  596. // get image object info
  597. AFPImageObjectInfo imageObjectInfo
  598. = createImageObjectInfo(img, x, y, width, height);
  599. // create image resource
  600. resourceManager.createObject(imageObjectInfo);
  601. } catch (IOException ioe) {
  602. handleIOException(ioe);
  603. }
  604. }
  605. /**
  606. * Sets a custom TextHandler implementation that is responsible for painting
  607. * text. The default TextHandler paints all text as shapes. A custom
  608. * implementation can implement text painting using text painting operators.
  609. *
  610. * @param handler
  611. * the custom TextHandler implementation
  612. */
  613. public void setCustomTextHandler(TextHandler handler) {
  614. this.customTextHandler = handler;
  615. }
  616. /** {@inheritDoc} */
  617. public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
  618. log.debug("drawRenderableImage() NYI: img=" + img + ", xform=" + xform);
  619. }
  620. /** {@inheritDoc} */
  621. public FontMetrics getFontMetrics(Font f) {
  622. log.debug("getFontMetrics() NYI: f=" + f);
  623. return null;
  624. }
  625. /** {@inheritDoc} */
  626. public void setXORMode(Color col) {
  627. log.debug("setXORMode() NYI: col=" + col);
  628. }
  629. /** {@inheritDoc} */
  630. public void addNativeImage(org.apache.xmlgraphics.image.loader.Image image,
  631. float x, float y, float width, float height) {
  632. log.debug("NYI: addNativeImage() " + "image=" + image
  633. + ",x=" + x + ",y=" + y + ",width=" + width + ",height=" + height);
  634. }
  635. /** {@inheritDoc} */
  636. public void copyArea(int x, int y, int width, int height, int dx, int dy) {
  637. log.debug("copyArea() NYI: ");
  638. }
  639. }