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.

Java2DPainter.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.render.java2d;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics2D;
  22. import java.awt.Paint;
  23. import java.awt.Point;
  24. import java.awt.Rectangle;
  25. import java.awt.font.GlyphVector;
  26. import java.awt.geom.AffineTransform;
  27. import java.awt.geom.Point2D;
  28. import java.io.IOException;
  29. import java.util.Stack;
  30. import org.w3c.dom.Document;
  31. import org.apache.fop.fonts.Font;
  32. import org.apache.fop.fonts.FontInfo;
  33. import org.apache.fop.fonts.FontTriplet;
  34. import org.apache.fop.render.RenderingContext;
  35. import org.apache.fop.render.intermediate.AbstractIFPainter;
  36. import org.apache.fop.render.intermediate.BorderPainter;
  37. import org.apache.fop.render.intermediate.GraphicsPainter;
  38. import org.apache.fop.render.intermediate.IFContext;
  39. import org.apache.fop.render.intermediate.IFException;
  40. import org.apache.fop.render.intermediate.IFState;
  41. import org.apache.fop.render.intermediate.IFUtil;
  42. import org.apache.fop.traits.BorderProps;
  43. import org.apache.fop.traits.RuleStyle;
  44. import org.apache.fop.util.CharUtilities;
  45. /**
  46. * {@link org.apache.fop.render.intermediate.IFPainter} implementation that paints on a Graphics2D
  47. * instance.
  48. */
  49. public class Java2DPainter extends AbstractIFPainter<Java2DDocumentHandler> {
  50. /** the IF context */
  51. protected IFContext ifContext;
  52. /** The font information */
  53. protected FontInfo fontInfo;
  54. private final GraphicsPainter graphicsPainter;
  55. private final BorderPainter borderPainter;
  56. /** The current state, holds a Graphics2D and its context */
  57. protected Java2DGraphicsState g2dState;
  58. private Stack<Java2DGraphicsState> g2dStateStack = new Stack<Java2DGraphicsState>();
  59. /**
  60. * Main constructor.
  61. * @param g2d the target Graphics2D instance
  62. * @param context the IF context
  63. * @param fontInfo the font information
  64. */
  65. public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo) {
  66. this(g2d, context, fontInfo, null);
  67. }
  68. /**
  69. * Special constructor for embedded use (when another painter uses Java2DPainter
  70. * to convert part of a document into a bitmap, for example).
  71. * @param g2d the target Graphics2D instance
  72. * @param context the IF context
  73. * @param fontInfo the font information
  74. * @param state the IF state object
  75. */
  76. public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state) {
  77. super(new Java2DDocumentHandler());
  78. this.ifContext = context;
  79. if (state != null) {
  80. this.state = state.push();
  81. } else {
  82. this.state = IFState.create();
  83. }
  84. this.fontInfo = fontInfo;
  85. this.g2dState = new Java2DGraphicsState(g2d, fontInfo, g2d.getTransform());
  86. graphicsPainter = new Java2DGraphicsPainter(this);
  87. this.borderPainter = new BorderPainter(graphicsPainter);
  88. }
  89. /** {@inheritDoc} */
  90. public IFContext getContext() {
  91. return this.ifContext;
  92. }
  93. /**
  94. * Returns the associated {@link FontInfo} object.
  95. * @return the font info
  96. */
  97. protected FontInfo getFontInfo() {
  98. return this.fontInfo;
  99. }
  100. /**
  101. * Returns the Java2D graphics state.
  102. * @return the graphics state
  103. */
  104. protected Java2DGraphicsState getState() {
  105. return this.g2dState;
  106. }
  107. //----------------------------------------------------------------------------------------------
  108. /** {@inheritDoc} */
  109. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  110. throws IFException {
  111. saveGraphicsState();
  112. try {
  113. concatenateTransformationMatrix(transform);
  114. clipRect(clipRect);
  115. } catch (IOException ioe) {
  116. throw new IFException("I/O error in startViewport()", ioe);
  117. }
  118. }
  119. /** {@inheritDoc} */
  120. public void endViewport() throws IFException {
  121. restoreGraphicsState();
  122. }
  123. /** {@inheritDoc} */
  124. public void startGroup(AffineTransform transform) throws IFException {
  125. saveGraphicsState();
  126. try {
  127. concatenateTransformationMatrix(transform);
  128. } catch (IOException ioe) {
  129. throw new IFException("I/O error in startGroup()", ioe);
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. public void endGroup() throws IFException {
  134. restoreGraphicsState();
  135. }
  136. /** {@inheritDoc} */
  137. public void drawImage(String uri, Rectangle rect) throws IFException {
  138. drawImageUsingURI(uri, rect);
  139. }
  140. /** {@inheritDoc} */
  141. protected RenderingContext createRenderingContext() {
  142. Java2DRenderingContext java2dContext = new Java2DRenderingContext(
  143. getUserAgent(), g2dState.getGraph(), getFontInfo());
  144. return java2dContext;
  145. }
  146. /** {@inheritDoc} */
  147. public void drawImage(Document doc, Rectangle rect) throws IFException {
  148. drawImageUsingDocument(doc, rect);
  149. }
  150. /** {@inheritDoc} */
  151. public void clipRect(Rectangle rect) throws IFException {
  152. getState().updateClip(rect);
  153. }
  154. /** {@inheritDoc} */
  155. public void clipBackground(Rectangle rect, BorderProps bpsBefore, BorderProps bpsAfter,
  156. BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
  157. // TODO Auto-generated method stub
  158. }
  159. /** {@inheritDoc} */
  160. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  161. if (fill == null) {
  162. return;
  163. }
  164. if (rect.width != 0 && rect.height != 0) {
  165. g2dState.updatePaint(fill);
  166. g2dState.getGraph().fill(rect);
  167. }
  168. }
  169. /** {@inheritDoc} */
  170. public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom,
  171. BorderProps left, BorderProps right) throws IFException {
  172. if (top != null || bottom != null || left != null || right != null) {
  173. this.borderPainter.drawBorders(rect, top, bottom, left, right, null);
  174. }
  175. }
  176. /** {@inheritDoc} */
  177. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  178. throws IFException {
  179. try {
  180. this.graphicsPainter.drawLine(start, end, width, color, style);
  181. } catch (IOException ioe) {
  182. throw new IFException("Unexpected error drawing line", ioe);
  183. }
  184. }
  185. /** {@inheritDoc} */
  186. public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp, String text)
  187. throws IFException {
  188. g2dState.updateColor(state.getTextColor());
  189. FontTriplet triplet = new FontTriplet(
  190. state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
  191. //TODO Ignored: state.getFontVariant()
  192. //TODO Opportunity for font caching if font state is more heavily used
  193. Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
  194. //String fontName = font.getFontName();
  195. //float fontSize = state.getFontSize() / 1000f;
  196. g2dState.updateFont(font.getFontName(), state.getFontSize() * 1000);
  197. Graphics2D g2d = this.g2dState.getGraph();
  198. GlyphVector gv = g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), text);
  199. Point2D cursor = new Point2D.Float(0, 0);
  200. int l = text.length();
  201. int[] dx = IFUtil.convertDPToDX ( dp );
  202. int dxl = (dx != null ? dx.length : 0);
  203. if (dx != null && dxl > 0 && dx[0] != 0) {
  204. cursor.setLocation(cursor.getX() - (dx[0] / 10f), cursor.getY());
  205. gv.setGlyphPosition(0, cursor);
  206. }
  207. for (int i = 0; i < l; i++) {
  208. char orgChar = text.charAt(i);
  209. float glyphAdjust = 0;
  210. int cw = font.getCharWidth(orgChar);
  211. if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
  212. glyphAdjust += wordSpacing;
  213. }
  214. glyphAdjust += letterSpacing;
  215. if (dx != null && i < dxl - 1) {
  216. glyphAdjust += dx[i + 1];
  217. }
  218. cursor.setLocation(cursor.getX() + cw + glyphAdjust, cursor.getY());
  219. gv.setGlyphPosition(i + 1, cursor);
  220. }
  221. g2d.drawGlyphVector(gv, x, y);
  222. }
  223. /** Saves the current graphics state on the stack. */
  224. protected void saveGraphicsState() {
  225. g2dStateStack.push(g2dState);
  226. g2dState = new Java2DGraphicsState(g2dState);
  227. }
  228. /** Restores the last graphics state from the stack. */
  229. protected void restoreGraphicsState() {
  230. g2dState.dispose();
  231. g2dState = g2dStateStack.pop();
  232. }
  233. private void concatenateTransformationMatrix(AffineTransform transform) throws IOException {
  234. g2dState.transform(transform);
  235. }
  236. }