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

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