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

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