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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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.StrokingTextHandler;
  52. import org.apache.xmlgraphics.java2d.TextHandler;
  53. import org.apache.xmlgraphics.util.UnitConv;
  54. import org.apache.fop.afp.goca.GraphicsSetLineType;
  55. import org.apache.fop.afp.modca.GraphicsObject;
  56. import org.apache.fop.afp.svg.AFPGraphicsConfiguration;
  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 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); // CSOK: ConstantName
  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. /**
  498. * Draws an AWT image into a BufferedImage using an AWT Graphics2D implementation
  499. *
  500. * @param img the AWT image
  501. * @param bufferedImage the AWT buffered image
  502. * @param width the image width
  503. * @param height the image height
  504. * @param observer the image observer
  505. * @return true if the image was drawn
  506. */
  507. private boolean drawBufferedImage(Image img, BufferedImage bufferedImage,
  508. int width, int height, ImageObserver observer) {
  509. java.awt.Graphics2D g2d = bufferedImage.createGraphics();
  510. try {
  511. g2d.setComposite(AlphaComposite.SrcOver);
  512. Color color = new Color(1, 1, 1, 0);
  513. g2d.setBackground(color);
  514. g2d.setPaint(color);
  515. g2d.fillRect(0, 0, width, height);
  516. int imageWidth = bufferedImage.getWidth();
  517. int imageHeight = bufferedImage.getHeight();
  518. Rectangle clipRect = new Rectangle(0, 0, imageWidth, imageHeight);
  519. g2d.clip(clipRect);
  520. g2d.setComposite(gc.getComposite());
  521. return g2d.drawImage(img, 0, 0, imageWidth, imageHeight, observer);
  522. } finally {
  523. g2d.dispose(); //drawn so dispose immediately to free system resource
  524. }
  525. }
  526. /** {@inheritDoc} */
  527. public boolean drawImage(Image img, int x, int y, int width, int height,
  528. ImageObserver observer) {
  529. // draw with AWT Graphics2D
  530. Dimension imageSize = new Dimension(width, height);
  531. BufferedImage bufferedImage = buildBufferedImage(imageSize);
  532. boolean drawn = drawBufferedImage(img, bufferedImage, width, height, observer);
  533. if (drawn) {
  534. drawRenderedImage(bufferedImage, new AffineTransform());
  535. }
  536. return false;
  537. }
  538. /** {@inheritDoc} */
  539. public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
  540. int imgWidth = img.getWidth();
  541. int imgHeight = img.getHeight();
  542. AffineTransform gat = gc.getTransform();
  543. int graphicsObjectHeight
  544. = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight();
  545. double toMillipointFactor = UnitConv.IN2PT * 1000 / (double)paintingState.getResolution();
  546. double x = gat.getTranslateX();
  547. double y = -(gat.getTranslateY() - graphicsObjectHeight);
  548. x = toMillipointFactor * x;
  549. y = toMillipointFactor * y;
  550. double w = toMillipointFactor * imgWidth * gat.getScaleX();
  551. double h = toMillipointFactor * imgHeight * -gat.getScaleY();
  552. AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage();
  553. ImageInfo imageInfo = new ImageInfo(null, null);
  554. imageInfo.setSize(new ImageSize(
  555. img.getWidth(), img.getHeight(), paintingState.getResolution()));
  556. imageInfo.getSize().calcSizeFromPixels();
  557. ImageRendered red = new ImageRendered(imageInfo, img, null);
  558. Rectangle targetPos = new Rectangle(
  559. (int)Math.round(x),
  560. (int)Math.round(y),
  561. (int)Math.round(w),
  562. (int)Math.round(h));
  563. AFPRenderingContext context = new AFPRenderingContext(null,
  564. resourceManager, paintingState, fontInfo, null);
  565. try {
  566. handler.handleImage(context, red, targetPos);
  567. } catch (IOException ioe) {
  568. handleIOException(ioe);
  569. }
  570. }
  571. /**
  572. * Sets a custom TextHandler implementation that is responsible for painting
  573. * text. The default TextHandler paints all text as shapes. A custom
  574. * implementation can implement text painting using text painting operators.
  575. *
  576. * @param handler
  577. * the custom TextHandler implementation
  578. */
  579. public void setCustomTextHandler(TextHandler handler) {
  580. this.customTextHandler = handler;
  581. }
  582. /** {@inheritDoc} */
  583. public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
  584. log.debug("drawRenderableImage() NYI: img=" + img + ", xform=" + xform);
  585. }
  586. /** {@inheritDoc} */
  587. public FontMetrics getFontMetrics(Font f) {
  588. log.debug("getFontMetrics() NYI: f=" + f);
  589. return null;
  590. }
  591. /** {@inheritDoc} */
  592. public void setXORMode(Color col) {
  593. log.debug("setXORMode() NYI: col=" + col);
  594. }
  595. /** {@inheritDoc} */
  596. public void addNativeImage(org.apache.xmlgraphics.image.loader.Image image,
  597. float x, float y, float width, float height) {
  598. log.debug("NYI: addNativeImage() " + "image=" + image
  599. + ",x=" + x + ",y=" + y + ",width=" + width + ",height=" + height);
  600. }
  601. /** {@inheritDoc} */
  602. public void copyArea(int x, int y, int width, int height, int dx, int dy) {
  603. log.debug("copyArea() NYI: ");
  604. }
  605. }