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

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