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

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