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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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.geom.AffineTransform;
  33. import java.awt.geom.Ellipse2D;
  34. import java.awt.geom.Line2D;
  35. import java.awt.geom.PathIterator;
  36. import java.awt.geom.Rectangle2D;
  37. import java.awt.image.BufferedImage;
  38. import java.awt.image.ImageObserver;
  39. import java.awt.image.RenderedImage;
  40. import java.awt.image.renderable.RenderableImage;
  41. import java.io.IOException;
  42. import org.apache.commons.logging.Log;
  43. import org.apache.commons.logging.LogFactory;
  44. import org.apache.xmlgraphics.image.loader.ImageInfo;
  45. import org.apache.xmlgraphics.image.loader.ImageSize;
  46. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  47. import org.apache.xmlgraphics.java2d.AbstractGraphics2D;
  48. import org.apache.xmlgraphics.java2d.GraphicContext;
  49. import org.apache.xmlgraphics.java2d.GraphicsConfigurationWithTransparency;
  50. import org.apache.xmlgraphics.java2d.StrokingTextHandler;
  51. import org.apache.xmlgraphics.java2d.TextHandler;
  52. import org.apache.xmlgraphics.util.UnitConv;
  53. import org.apache.fop.afp.goca.GraphicsSetLineType;
  54. import org.apache.fop.afp.modca.GraphicsObject;
  55. import org.apache.fop.afp.modca.ResourceObject;
  56. import org.apache.fop.afp.util.CubicBezierApproximator;
  57. import org.apache.fop.fonts.FontInfo;
  58. import org.apache.fop.render.afp.AFPImageHandlerRenderedImage;
  59. import org.apache.fop.render.afp.AFPRenderingContext;
  60. import org.apache.fop.svg.NativeImageHandler;
  61. /**
  62. * This is a concrete implementation of {@link AbstractGraphics2D} (and
  63. * therefore of {@link java.awt.Graphics2D}) 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;
  80. /** Fallback text handler */
  81. protected TextHandler fallbackTextHandler = new StrokingTextHandler();
  82. /** Custom text handler */
  83. protected TextHandler customTextHandler;
  84. /** AFP resource manager */
  85. private AFPResourceManager resourceManager;
  86. /** AFP resource info */
  87. private AFPResourceInfo resourceInfo;
  88. /** Current AFP state */
  89. private AFPPaintingState paintingState;
  90. /** AFP graphics configuration */
  91. private final GraphicsConfigurationWithTransparency graphicsConfig = new GraphicsConfigurationWithTransparency();
  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. * Returns the AFP resource manager associated with this {@link java.awt.Graphics2D} instance.
  140. * @return the resource manager
  141. */
  142. public AFPResourceManager getResourceManager() {
  143. return this.resourceManager;
  144. }
  145. /**
  146. * Sets the AFP resource info
  147. *
  148. * @param resourceInfo the AFP resource info
  149. */
  150. private void setResourceInfo(AFPResourceInfo resourceInfo) {
  151. this.resourceInfo = resourceInfo;
  152. }
  153. /**
  154. * Returns the GOCA graphics object
  155. *
  156. * @return the GOCA graphics object
  157. */
  158. public GraphicsObject getGraphicsObject() {
  159. return this.graphicsObj;
  160. }
  161. /**
  162. * Sets the GOCA graphics object
  163. *
  164. * @param obj the GOCA graphics object
  165. */
  166. public void setGraphicsObject(GraphicsObject obj) {
  167. this.graphicsObj = obj;
  168. }
  169. /**
  170. * Sets the AFP painting state
  171. *
  172. * @param paintingState the AFP painting state
  173. */
  174. private void setPaintingState(AFPPaintingState paintingState) {
  175. this.paintingState = paintingState;
  176. }
  177. /**
  178. * Returns the AFP painting state
  179. *
  180. * @return the AFP painting state
  181. */
  182. public AFPPaintingState getPaintingState() {
  183. return this.paintingState;
  184. }
  185. /**
  186. * Sets the FontInfo
  187. *
  188. * @param fontInfo the FontInfo
  189. */
  190. private void setFontInfo(FontInfo fontInfo) {
  191. this.fontInfo = fontInfo;
  192. }
  193. /**
  194. * Returns the FontInfo
  195. *
  196. * @return the FontInfo
  197. */
  198. public FontInfo getFontInfo() {
  199. return this.fontInfo;
  200. }
  201. /**
  202. * Sets the GraphicContext
  203. *
  204. * @param gc
  205. * GraphicContext to use
  206. */
  207. public void setGraphicContext(GraphicContext gc) {
  208. this.gc = gc;
  209. }
  210. private int getResolution() {
  211. return this.paintingState.getResolution();
  212. }
  213. /**
  214. * Converts a length value to an absolute value.
  215. * Please note that this only uses the "ScaleY" factor, so this will result
  216. * in a bad value should "ScaleX" and "ScaleY" be different.
  217. * @param length the length
  218. * @return the absolute length
  219. */
  220. public double convertToAbsoluteLength(double length) {
  221. AffineTransform current = getTransform();
  222. double mult = getResolution() / (double)UnitConv.IN2PT;
  223. double factor = -current.getScaleY() / mult;
  224. return length * factor;
  225. }
  226. /**
  227. * Apply the stroke to the AFP graphics object.
  228. * This takes the java stroke and outputs the appropriate settings
  229. * to the AFP graphics object so that the stroke attributes are handled.
  230. *
  231. * @param stroke the java stroke
  232. */
  233. protected void applyStroke(Stroke stroke) {
  234. if (stroke instanceof BasicStroke) {
  235. BasicStroke basicStroke = (BasicStroke) stroke;
  236. // set line width and correct it; NOTE: apparently we need to correct the width so that the
  237. // output looks OK since the default with depends on the output device
  238. float lineWidth = basicStroke.getLineWidth();
  239. float correction = paintingState.getLineWidthCorrection();
  240. graphicsObj.setLineWidth(lineWidth * correction);
  241. //No line join, miter limit and end cap support in GOCA. :-(
  242. // set line type/style (note: this is an approximation at best!)
  243. float[] dashArray = basicStroke.getDashArray();
  244. if (paintingState.setDashArray(dashArray)) {
  245. byte type = GraphicsSetLineType.DEFAULT; // normally SOLID
  246. if (dashArray != null) {
  247. type = GraphicsSetLineType.DOTTED; // default to plain DOTTED if dashed line
  248. // float offset = basicStroke.getDashPhase();
  249. if (dashArray.length == 2) {
  250. if (dashArray[0] < dashArray[1]) {
  251. type = GraphicsSetLineType.SHORT_DASHED;
  252. } else if (dashArray[0] > dashArray[1]) {
  253. type = GraphicsSetLineType.LONG_DASHED;
  254. }
  255. } else if (dashArray.length == 4) {
  256. if (dashArray[0] > dashArray[1]
  257. && dashArray[2] < dashArray[3]) {
  258. type = GraphicsSetLineType.DASH_DOT;
  259. } else if (dashArray[0] < dashArray[1]
  260. && dashArray[2] < dashArray[3]) {
  261. type = GraphicsSetLineType.DOUBLE_DOTTED;
  262. }
  263. } else if (dashArray.length == 6) {
  264. if (dashArray[0] > dashArray[1]
  265. && dashArray[2] < dashArray[3]
  266. && dashArray[4] < dashArray[5]) {
  267. type = GraphicsSetLineType.DASH_DOUBLE_DOTTED;
  268. }
  269. }
  270. }
  271. graphicsObj.setLineType(type);
  272. }
  273. } else {
  274. LOG.warn("Unsupported Stroke: " + stroke.getClass().getName());
  275. }
  276. }
  277. /**
  278. * Apply the java paint to the AFP.
  279. * This takes the java paint sets up the appropriate AFP commands
  280. * for the drawing with that paint.
  281. * Currently this supports the gradients and patterns from batik.
  282. *
  283. * @param paint the paint to convert to AFP
  284. * @param fill true if the paint should be set for filling
  285. * @return true if the paint is handled natively, false if the paint should be rasterized
  286. */
  287. private boolean applyPaint(Paint paint, boolean fill) {
  288. if (paint instanceof Color) {
  289. return true;
  290. }
  291. LOG.debug("NYI: applyPaint() " + paint + " fill=" + fill);
  292. // if (paint instanceof TexturePaint) {
  293. // TexturePaint texturePaint = (TexturePaint)paint;
  294. // BufferedImage bufferedImage = texturePaint.getImage();
  295. // AffineTransform at = paintingState.getTransform();
  296. // int x = (int)Math.round(at.getTranslateX());
  297. // int y = (int)Math.round(at.getTranslateY());
  298. // drawImage(bufferedImage, x, y, null);
  299. // }
  300. return false;
  301. }
  302. /**
  303. * Handle the Batik drawing event
  304. *
  305. * @param shape
  306. * the shape to draw
  307. * @param fill
  308. * true if the shape is to be drawn filled
  309. */
  310. private void doDrawing(Shape shape, boolean fill) {
  311. if (!fill) {
  312. graphicsObj.newSegment();
  313. }
  314. graphicsObj.setColor(gc.getColor());
  315. applyPaint(gc.getPaint(), fill);
  316. if (fill) {
  317. graphicsObj.beginArea();
  318. } else {
  319. applyStroke(gc.getStroke());
  320. }
  321. AffineTransform trans = gc.getTransform();
  322. PathIterator iter = shape.getPathIterator(trans);
  323. if (shape instanceof Line2D) {
  324. double[] dstPts = new double[6];
  325. iter.currentSegment(dstPts);
  326. int[] coords = new int[4];
  327. coords[X1] = (int) Math.round(dstPts[X]);
  328. coords[Y1] = (int) Math.round(dstPts[Y]);
  329. iter.next();
  330. iter.currentSegment(dstPts);
  331. coords[X2] = (int) Math.round(dstPts[X]);
  332. coords[Y2] = (int) Math.round(dstPts[Y]);
  333. graphicsObj.addLine(coords);
  334. } else if (shape instanceof Rectangle2D) {
  335. double[] dstPts = new double[6];
  336. iter.currentSegment(dstPts);
  337. int[] coords = new int[4];
  338. coords[X2] = (int) Math.round(dstPts[X]);
  339. coords[Y2] = (int) Math.round(dstPts[Y]);
  340. iter.next();
  341. iter.next();
  342. iter.currentSegment(dstPts);
  343. coords[X1] = (int) Math.round(dstPts[X]);
  344. coords[Y1] = (int) Math.round(dstPts[Y]);
  345. graphicsObj.addBox(coords);
  346. } else if (shape instanceof Ellipse2D) {
  347. double[] dstPts = new double[6];
  348. Ellipse2D elip = (Ellipse2D) shape;
  349. double scale = trans.getScaleX();
  350. double radiusWidth = elip.getWidth() / 2;
  351. double radiusHeight = elip.getHeight() / 2;
  352. graphicsObj.setArcParams(
  353. (int)Math.round(radiusWidth * scale),
  354. (int)Math.round(radiusHeight * scale),
  355. 0,
  356. 0
  357. );
  358. double[] srcPts = new double[] {elip.getCenterX(), elip.getCenterY()};
  359. trans.transform(srcPts, 0, dstPts, 0, 1);
  360. final int mh = 1;
  361. final int mhr = 0;
  362. graphicsObj.addFullArc(
  363. (int)Math.round(dstPts[X]),
  364. (int)Math.round(dstPts[Y]),
  365. mh,
  366. mhr
  367. );
  368. } else {
  369. processPathIterator(iter);
  370. }
  371. if (fill) {
  372. graphicsObj.endArea();
  373. }
  374. }
  375. /**
  376. * Processes a path iterator generating the necessary painting operations.
  377. *
  378. * @param iter PathIterator to process
  379. */
  380. private void processPathIterator(PathIterator iter) {
  381. double[] dstPts = new double[6];
  382. double[] currentPosition = new double[2];
  383. for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) {
  384. switch (iter.currentSegment(dstPts)) {
  385. case PathIterator.SEG_LINETO:
  386. graphicsObj.addLine(new int[] {
  387. (int)Math.round(dstPts[X]),
  388. (int)Math.round(dstPts[Y])
  389. }, true);
  390. currentPosition = new double[]{dstPts[X], dstPts[Y]};
  391. break;
  392. case PathIterator.SEG_QUADTO:
  393. graphicsObj.addFillet(new int[] {
  394. (int)Math.round(dstPts[X1]),
  395. (int)Math.round(dstPts[Y1]),
  396. (int)Math.round(dstPts[X2]),
  397. (int)Math.round(dstPts[Y2])
  398. }, true);
  399. currentPosition = new double[]{dstPts[X2], dstPts[Y2]};
  400. break;
  401. case PathIterator.SEG_CUBICTO:
  402. double[] cubicCoords = new double[] {currentPosition[0], currentPosition[1],
  403. dstPts[X1], dstPts[Y1], dstPts[X2], dstPts[Y2], dstPts[X3], dstPts[Y3]};
  404. double[][] quadParts = CubicBezierApproximator.fixedMidPointApproximation(
  405. cubicCoords);
  406. if (quadParts.length >= 4) {
  407. for (double[] quadPts : quadParts) {
  408. if (quadPts != null && quadPts.length == 4) {
  409. graphicsObj.addFillet(new int[]{
  410. (int) Math.round(quadPts[X1]),
  411. (int) Math.round(quadPts[Y1]),
  412. (int) Math.round(quadPts[X2]),
  413. (int) Math.round(quadPts[Y2])
  414. }, true);
  415. currentPosition = new double[]{quadPts[X2], quadPts[Y2]};
  416. }
  417. }
  418. }
  419. break;
  420. case PathIterator.SEG_MOVETO:
  421. openingCoords = new int[] {
  422. (int)Math.round(dstPts[X]),
  423. (int)Math.round(dstPts[Y])
  424. };
  425. currentPosition = new double[]{dstPts[X], dstPts[Y]};
  426. graphicsObj.setCurrentPosition(openingCoords);
  427. break;
  428. case PathIterator.SEG_CLOSE:
  429. graphicsObj.addLine(openingCoords, true);
  430. currentPosition = new double[]{openingCoords[0], openingCoords[1]};
  431. break;
  432. default:
  433. LOG.debug("Unrecognised path iterator type");
  434. break;
  435. }
  436. }
  437. }
  438. /** {@inheritDoc} */
  439. @Override
  440. public void draw(Shape shape) {
  441. LOG.debug("draw() shape=" + shape);
  442. doDrawing(shape, false);
  443. }
  444. /** {@inheritDoc} */
  445. @Override
  446. public void fill(Shape shape) {
  447. LOG.debug("fill() shape=" + shape);
  448. doDrawing(shape, true);
  449. }
  450. /**
  451. * Central handler for IOExceptions for this class.
  452. *
  453. * @param ioe
  454. * IOException to handle
  455. */
  456. public void handleIOException(IOException ioe) {
  457. // TODO Surely, there's a better way to do this.
  458. LOG.error(ioe.getMessage());
  459. ioe.printStackTrace();
  460. }
  461. /** {@inheritDoc} */
  462. @Override
  463. public void drawString(String str, float x, float y) {
  464. try {
  465. if (customTextHandler != null && !textAsShapes) {
  466. customTextHandler.drawString(this, str, x, y);
  467. } else {
  468. fallbackTextHandler.drawString(this, str, x, y);
  469. }
  470. } catch (IOException ioe) {
  471. handleIOException(ioe);
  472. }
  473. }
  474. /** {@inheritDoc} */
  475. @Override
  476. public GraphicsConfiguration getDeviceConfiguration() {
  477. return graphicsConfig;
  478. }
  479. /** {@inheritDoc} */
  480. @Override
  481. public Graphics create() {
  482. return new AFPGraphics2D(this);
  483. }
  484. /** {@inheritDoc} */
  485. @Override
  486. public void dispose() {
  487. this.graphicsObj = null;
  488. }
  489. /** {@inheritDoc} */
  490. @Override
  491. public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
  492. return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
  493. }
  494. private BufferedImage buildBufferedImage(Dimension size) {
  495. return new BufferedImage(size.width, size.height,
  496. BufferedImage.TYPE_INT_ARGB);
  497. }
  498. /**
  499. * Draws an AWT image into a BufferedImage using an AWT Graphics2D implementation
  500. *
  501. * @param img the AWT image
  502. * @param bufferedImage the AWT buffered image
  503. * @param width the image width
  504. * @param height the image height
  505. * @param observer the image observer
  506. * @return true if the image was drawn
  507. */
  508. private boolean drawBufferedImage(Image img, BufferedImage bufferedImage,
  509. int width, int height, ImageObserver observer) {
  510. java.awt.Graphics2D g2d = bufferedImage.createGraphics();
  511. try {
  512. g2d.setComposite(AlphaComposite.SrcOver);
  513. Color color = new Color(1, 1, 1, 0);
  514. g2d.setBackground(color);
  515. g2d.setPaint(color);
  516. g2d.fillRect(0, 0, width, height);
  517. int imageWidth = bufferedImage.getWidth();
  518. int imageHeight = bufferedImage.getHeight();
  519. Rectangle clipRect = new Rectangle(0, 0, imageWidth, imageHeight);
  520. g2d.clip(clipRect);
  521. g2d.setComposite(gc.getComposite());
  522. return g2d.drawImage(img, 0, 0, imageWidth, imageHeight, observer);
  523. } finally {
  524. g2d.dispose(); //drawn so dispose immediately to free system resource
  525. }
  526. }
  527. /** {@inheritDoc} */
  528. @Override
  529. public boolean drawImage(Image img, int x, int y, int width, int height,
  530. ImageObserver observer) {
  531. // draw with AWT Graphics2D
  532. Dimension imageSize = new Dimension(width, height);
  533. BufferedImage bufferedImage = buildBufferedImage(imageSize);
  534. boolean drawn = drawBufferedImage(img, bufferedImage, width, height, observer);
  535. if (drawn) {
  536. drawRenderedImage(bufferedImage, new AffineTransform());
  537. }
  538. return false;
  539. }
  540. /** {@inheritDoc} */
  541. @Override
  542. public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
  543. int imgWidth = img.getWidth();
  544. int imgHeight = img.getHeight();
  545. AffineTransform gat = gc.getTransform();
  546. int graphicsObjectHeight
  547. = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight();
  548. double toMillipointFactor = UnitConv.IN2PT * 1000 / (double)paintingState.getResolution();
  549. double x = gat.getTranslateX();
  550. double y = -(gat.getTranslateY() - graphicsObjectHeight);
  551. x = toMillipointFactor * x;
  552. y = toMillipointFactor * y;
  553. double w = toMillipointFactor * imgWidth * gat.getScaleX();
  554. double h = toMillipointFactor * imgHeight * -gat.getScaleY();
  555. AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage();
  556. String uri = null;
  557. if (resourceManager.getResourceLevelDefaults()
  558. .getDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC).isPrintFile()) {
  559. uri = resourceInfo.getUri();
  560. }
  561. ImageInfo imageInfo = new ImageInfo(uri, null);
  562. imageInfo.setSize(new ImageSize(
  563. img.getWidth(), img.getHeight(), paintingState.getResolution()));
  564. imageInfo.getSize().calcSizeFromPixels();
  565. ImageRendered red = new ImageRendered(imageInfo, img, null);
  566. Rectangle targetPos = new Rectangle(
  567. (int)Math.round(x),
  568. (int)Math.round(y),
  569. (int)Math.round(w),
  570. (int)Math.round(h));
  571. AFPRenderingContext context = new AFPRenderingContext(null,
  572. resourceManager, paintingState, fontInfo, null);
  573. resourceManager.includeCached = false;
  574. try {
  575. handler.handleImage(context, red, targetPos);
  576. } catch (IOException ioe) {
  577. handleIOException(ioe);
  578. }
  579. resourceManager.includeCached = true;
  580. }
  581. /**
  582. * Sets a custom TextHandler implementation that is responsible for painting
  583. * text. The default TextHandler paints all text as shapes. A custom
  584. * implementation can implement text painting using text painting operators.
  585. *
  586. * @param handler
  587. * the custom TextHandler implementation
  588. */
  589. public void setCustomTextHandler(TextHandler handler) {
  590. this.customTextHandler = handler;
  591. }
  592. /** {@inheritDoc} */
  593. @Override
  594. public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
  595. LOG.debug("drawRenderableImage() NYI: img=" + img + ", xform=" + xform);
  596. }
  597. /** {@inheritDoc} */
  598. @Override
  599. public FontMetrics getFontMetrics(Font f) {
  600. LOG.debug("getFontMetrics() NYI: f=" + f);
  601. return null;
  602. }
  603. /** {@inheritDoc} */
  604. @Override
  605. public void setXORMode(Color col) {
  606. LOG.debug("setXORMode() NYI: col=" + col);
  607. }
  608. /** {@inheritDoc} */
  609. public void addNativeImage(org.apache.xmlgraphics.image.loader.Image image,
  610. float x, float y, float width, float height) {
  611. LOG.debug("NYI: addNativeImage() " + "image=" + image
  612. + ",x=" + x + ",y=" + y + ",width=" + width + ",height=" + height);
  613. }
  614. /** {@inheritDoc} */
  615. @Override
  616. public void copyArea(int x, int y, int width, int height, int dx, int dy) {
  617. LOG.debug("copyArea() NYI: ");
  618. }
  619. public void clearRect(int x, int y, int width, int height) {
  620. }
  621. }