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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.IFDocumentHandler;
  40. import org.apache.fop.render.intermediate.IFException;
  41. import org.apache.fop.render.intermediate.IFState;
  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<IFDocumentHandler> {
  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, new Java2DDocumentHandler());
  67. }
  68. public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFDocumentHandler documentHandler) {
  69. this(g2d, context, fontInfo, null, documentHandler);
  70. }
  71. /**
  72. * Special constructor for embedded use (when another painter uses Java2DPainter
  73. * to convert part of a document into a bitmap, for example).
  74. * @param g2d the target Graphics2D instance
  75. * @param context the IF context
  76. * @param fontInfo the font information
  77. * @param state the IF state object
  78. */
  79. public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state) {
  80. this(g2d, context, fontInfo, state, new Java2DDocumentHandler());
  81. }
  82. public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state,
  83. IFDocumentHandler documentHandler) {
  84. super(documentHandler);
  85. this.ifContext = context;
  86. if (state != null) {
  87. this.state = state.push();
  88. } else {
  89. this.state = IFState.create();
  90. }
  91. this.fontInfo = fontInfo;
  92. this.g2dState = new Java2DGraphicsState(g2d, fontInfo, g2d.getTransform());
  93. graphicsPainter = new Java2DGraphicsPainter(this);
  94. this.borderPainter = new BorderPainter(graphicsPainter);
  95. }
  96. /** {@inheritDoc} */
  97. public IFContext getContext() {
  98. return this.ifContext;
  99. }
  100. /**
  101. * Returns the associated {@link FontInfo} object.
  102. * @return the font info
  103. */
  104. protected FontInfo getFontInfo() {
  105. return this.fontInfo;
  106. }
  107. /**
  108. * Returns the Java2D graphics state.
  109. * @return the graphics state
  110. */
  111. protected Java2DGraphicsState getState() {
  112. return this.g2dState;
  113. }
  114. //----------------------------------------------------------------------------------------------
  115. /** {@inheritDoc} */
  116. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  117. throws IFException {
  118. saveGraphicsState();
  119. try {
  120. concatenateTransformationMatrix(transform);
  121. if (clipRect != null) {
  122. clipRect(clipRect);
  123. }
  124. } catch (IOException ioe) {
  125. throw new IFException("I/O error in startViewport()", ioe);
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public void endViewport() throws IFException {
  130. restoreGraphicsState();
  131. }
  132. /** {@inheritDoc} */
  133. public void startGroup(AffineTransform transform, String layer) throws IFException {
  134. saveGraphicsState();
  135. try {
  136. concatenateTransformationMatrix(transform);
  137. } catch (IOException ioe) {
  138. throw new IFException("I/O error in startGroup()", ioe);
  139. }
  140. }
  141. /** {@inheritDoc} */
  142. public void endGroup() throws IFException {
  143. restoreGraphicsState();
  144. }
  145. /** {@inheritDoc} */
  146. public void drawImage(String uri, Rectangle rect) throws IFException {
  147. drawImageUsingURI(uri, rect);
  148. }
  149. /** {@inheritDoc} */
  150. protected RenderingContext createRenderingContext() {
  151. Java2DRenderingContext java2dContext = new Java2DRenderingContext(
  152. getUserAgent(), g2dState.getGraph(), getFontInfo());
  153. return java2dContext;
  154. }
  155. /** {@inheritDoc} */
  156. public void drawImage(Document doc, Rectangle rect) throws IFException {
  157. drawImageUsingDocument(doc, rect);
  158. }
  159. /** {@inheritDoc} */
  160. public void clipRect(Rectangle rect) throws IFException {
  161. getState().updateClip(rect);
  162. }
  163. /** {@inheritDoc} */
  164. public void clipBackground(Rectangle rect, BorderProps bpsBefore, BorderProps bpsAfter,
  165. BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
  166. // TODO Auto-generated method stub
  167. }
  168. /** {@inheritDoc} */
  169. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  170. if (fill == null) {
  171. return;
  172. }
  173. if (rect.width != 0 && rect.height != 0) {
  174. g2dState.updatePaint(fill);
  175. g2dState.getGraph().fill(rect);
  176. }
  177. }
  178. /** {@inheritDoc} */
  179. public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom,
  180. BorderProps left, BorderProps right) throws IFException {
  181. if (top != null || bottom != null || left != null || right != null) {
  182. this.borderPainter.drawBorders(rect, top, bottom, left, right, null);
  183. }
  184. }
  185. /** {@inheritDoc} */
  186. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  187. throws IFException {
  188. try {
  189. this.graphicsPainter.drawLine(start, end, width, color, style);
  190. } catch (IOException ioe) {
  191. throw new IFException("Unexpected error drawing line", ioe);
  192. }
  193. }
  194. /** {@inheritDoc} */
  195. public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp, String text)
  196. throws IFException {
  197. g2dState.updateColor(state.getTextColor());
  198. FontTriplet triplet = new FontTriplet(
  199. state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
  200. //TODO Ignored: state.getFontVariant()
  201. //TODO Opportunity for font caching if font state is more heavily used
  202. Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
  203. //String fontName = font.getFontName();
  204. //float fontSize = state.getFontSize() / 1000f;
  205. g2dState.updateFont(font.getFontName(), state.getFontSize() * 1000);
  206. Graphics2D g2d = this.g2dState.getGraph();
  207. GlyphVector gv = Java2DUtil.createGlyphVector(text, g2d, font, fontInfo);
  208. Point2D cursor = new Point2D.Float(0, 0);
  209. int l = text.length();
  210. if (dp != null && dp[0] != null && (dp[0][0] != 0 || dp[0][1] != 0)) {
  211. cursor.setLocation(cursor.getX() + dp[0][0], cursor.getY() - dp[0][1]);
  212. gv.setGlyphPosition(0, cursor);
  213. }
  214. int currentIdx = 0;
  215. for (int i = 0; i < l; i++) {
  216. int orgChar = text.codePointAt(i);
  217. // The dp (GPOS/kerning adjustment) is performed over glyphs and not
  218. // characters (GlyphMapping.processWordMapping). The length of dp is
  219. // adjusted later to fit the length of the String adding trailing 0.
  220. // This means that it's probably ok to consume one of the 2 surrogate
  221. // pairs.
  222. i += CharUtilities.incrementIfNonBMP(orgChar);
  223. float xGlyphAdjust = 0;
  224. float yGlyphAdjust = 0;
  225. int cw = font.getCharWidth(orgChar);
  226. if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
  227. xGlyphAdjust += wordSpacing;
  228. }
  229. xGlyphAdjust += letterSpacing;
  230. if (dp != null && i < dp.length && dp[i] != null) {
  231. xGlyphAdjust += dp[i][2] - dp[i][0];
  232. yGlyphAdjust += dp[i][3] - dp[i][1];
  233. }
  234. if (dp != null && i < dp.length - 1 && dp[i + 1] != null) {
  235. xGlyphAdjust += dp[i + 1][0];
  236. yGlyphAdjust += dp[i + 1][1];
  237. }
  238. cursor.setLocation(cursor.getX() + cw + xGlyphAdjust, cursor.getY() - yGlyphAdjust);
  239. gv.setGlyphPosition(++currentIdx, cursor);
  240. }
  241. g2d.drawGlyphVector(gv, x, y);
  242. }
  243. /** Saves the current graphics state on the stack. */
  244. protected void saveGraphicsState() {
  245. g2dStateStack.push(g2dState);
  246. g2dState = new Java2DGraphicsState(g2dState);
  247. }
  248. /** Restores the last graphics state from the stack. */
  249. protected void restoreGraphicsState() {
  250. g2dState.dispose();
  251. g2dState = g2dStateStack.pop();
  252. }
  253. private void concatenateTransformationMatrix(AffineTransform transform) throws IOException {
  254. g2dState.transform(transform);
  255. }
  256. }