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.

PDFState.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.pdf;
  8. import java.awt.Shape;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.awt.geom.GeneralPath;
  13. import java.awt.geom.Area;
  14. import java.awt.Color;
  15. import java.awt.Paint;
  16. import java.awt.geom.AffineTransform;
  17. /**
  18. * This keeps information about the current state when writing to pdf.
  19. * It allows for creating new graphics states with the q operator.
  20. *
  21. * When setting the state for pdf there are three possible ways of
  22. * handling the situation.
  23. * The values can be set to override previous or default values.
  24. * A new state can be added and then the values set.
  25. * The current state can be popped and values will return to a
  26. * previous state then the necessary values can be overridden.
  27. * The current transform behaves differently to other values as the
  28. * matrix is combined with the current resolved value.
  29. * It is impossible to optimise the result without analysing the all
  30. * the possible combinations after completing.
  31. */
  32. public class PDFState {
  33. private static final String COLOR = "color";
  34. private static final String BACKCOLOR = "backcolor";
  35. private static final String PAINT = "paint";
  36. private static final String BACKPAINT = "backpaint";
  37. private static final String LINECAP = "lineCap";
  38. private static final String LINEJOIN = "lineJoin";
  39. private static final String LINEWIDTH = "lineWidth";
  40. private static final String MITERLIMIT = "miterLimit";
  41. private static final String TEXT = "text";
  42. private static final String DASHOFFSET = "dashOffset";
  43. private static final String DASHARRAY = "dashArray";
  44. private static final String TRANSFORM = "transform";
  45. private static final String FONTSIZE = "fontSize";
  46. private static final String FONTNAME = "fontName";
  47. private static final String CLIP = "clip";
  48. private static final String GSTATE = "gstate";
  49. private Color color = Color.black;
  50. private Color backcolor = Color.white;
  51. private Paint paint = null;
  52. private Paint backPaint = null;
  53. private int lineCap = 0;
  54. private int lineJoin = 0;
  55. private float lineWidth = 1;
  56. private float miterLimit = 0;
  57. private boolean text = false;
  58. private int dashOffset = 0;
  59. private int[] dashArray = new int[0];
  60. private AffineTransform transform = new AffineTransform();
  61. private float fontSize = 0;
  62. private String fontName = "";
  63. private Shape clip = null;
  64. private PDFGState gstate = null;
  65. ArrayList stateStack = new ArrayList();
  66. public PDFState() {
  67. }
  68. // this call should be used when the q operator is used
  69. // so that the state is known when popped
  70. public void push() {
  71. HashMap saveMap = new HashMap();
  72. saveMap.put(COLOR, color);
  73. saveMap.put(BACKCOLOR, backcolor);
  74. saveMap.put(PAINT, paint);
  75. saveMap.put(BACKPAINT, backPaint);
  76. saveMap.put(LINECAP, new Integer(lineCap));
  77. saveMap.put(LINEJOIN, new Integer(lineJoin));
  78. saveMap.put(LINEWIDTH, new Float(lineWidth));
  79. saveMap.put(MITERLIMIT, new Float(miterLimit));
  80. saveMap.put(TEXT, new Boolean(text));
  81. saveMap.put(DASHOFFSET, new Integer(dashOffset));
  82. saveMap.put(DASHARRAY, dashArray);
  83. saveMap.put(TRANSFORM, transform);
  84. saveMap.put(FONTSIZE, new Float(fontSize));
  85. saveMap.put(FONTNAME, fontName);
  86. saveMap.put(CLIP, clip);
  87. saveMap.put(GSTATE, gstate);
  88. stateStack.add(saveMap);
  89. transform = new AffineTransform();
  90. }
  91. public void pop() {
  92. if (getStackLevel() > 0) {
  93. HashMap saveMap = (HashMap)stateStack.get(stateStack.size() - 1);
  94. stateStack.remove(stateStack.size() - 1);
  95. color = (Color)saveMap.get(COLOR);
  96. backcolor = (Color)saveMap.get(BACKCOLOR);
  97. paint = (Paint)saveMap.get(PAINT);
  98. backPaint = (Paint)saveMap.get(BACKPAINT);
  99. lineCap = ((Integer)saveMap.get(LINECAP)).intValue();
  100. lineJoin = ((Integer)saveMap.get(LINEJOIN)).intValue();
  101. lineWidth = ((Float)saveMap.get(LINEWIDTH)).floatValue();
  102. miterLimit = ((Float)saveMap.get(MITERLIMIT)).floatValue();
  103. text = ((Boolean)saveMap.get(TEXT)).booleanValue();
  104. dashOffset = ((Integer)saveMap.get(DASHOFFSET)).intValue();
  105. dashArray = (int[])saveMap.get(DASHARRAY);
  106. transform = (AffineTransform)saveMap.get(TRANSFORM);
  107. fontSize = ((Float)saveMap.get(FONTSIZE)).floatValue();
  108. fontName = (String)saveMap.get(FONTNAME);
  109. clip = (Shape)saveMap.get(CLIP);
  110. gstate = (PDFGState)saveMap.get(GSTATE);
  111. }
  112. }
  113. public int getStackLevel() {
  114. return stateStack.size();
  115. }
  116. public void restoreLevel(int stack) {
  117. int pos = stack;
  118. while(stateStack.size() > pos + 1) {
  119. stateStack.remove(stateStack.size() - 1);
  120. }
  121. if (stateStack.size() > pos) {
  122. pop();
  123. }
  124. }
  125. public boolean setLineDash(int[] array, int offset) {
  126. return false;
  127. }
  128. public boolean setColor(Color col) {
  129. if (!col.equals(color)) {
  130. color = col;
  131. return true;
  132. }
  133. return false;
  134. }
  135. public boolean setBackColor(Color col) {
  136. if (!col.equals(backcolor)) {
  137. backcolor = col;
  138. return true;
  139. }
  140. return false;
  141. }
  142. public boolean setPaint(Paint p) {
  143. if (paint == null) {
  144. if (p != null) {
  145. paint = p;
  146. return true;
  147. }
  148. } else if (!paint.equals(p)) {
  149. paint = p;
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * For clips it can start a new state
  156. */
  157. public boolean checkClip(Shape cl) {
  158. if (clip == null) {
  159. if (cl != null) {
  160. return true;
  161. }
  162. } else if (!new Area(clip).equals(new Area(cl))) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. public void setClip(Shape cl) {
  168. if (clip != null) {
  169. Area newClip = new Area(clip);
  170. newClip.intersect(new Area(cl));
  171. clip = new GeneralPath(newClip);
  172. } else {
  173. clip = cl;
  174. }
  175. }
  176. public boolean checkTransform(AffineTransform tf) {
  177. return !tf.equals(transform);
  178. }
  179. /**
  180. * Set a new transform.
  181. * This transform is appended to the transform of
  182. * the current graphic state.
  183. */
  184. public void setTransform(AffineTransform tf) {
  185. transform.concatenate(tf);
  186. }
  187. /**
  188. * Get the current transform.
  189. * This gets the combination of all transforms in the
  190. * current state.
  191. */
  192. public AffineTransform getTransform() {
  193. AffineTransform tf;
  194. AffineTransform at = new AffineTransform();
  195. for (Iterator iter = stateStack.iterator(); iter.hasNext();) {
  196. HashMap map = (HashMap)iter.next();
  197. tf = (AffineTransform)map.get(TRANSFORM);
  198. at.concatenate(tf);
  199. }
  200. at.concatenate(transform);
  201. return at;
  202. }
  203. /**
  204. * Get the grapics state.
  205. * This gets the combination of all graphic states for
  206. * the current context.
  207. * This is the graphic state set with the gs operator not
  208. * the other graphic state changes.
  209. */
  210. public PDFGState getGState() {
  211. PDFGState defaultState = PDFGState.DEFAULT;
  212. PDFGState state;
  213. PDFGState newstate = new PDFGState(0);
  214. newstate.addValues(defaultState);
  215. for (Iterator iter = stateStack.iterator(); iter.hasNext(); ) {
  216. HashMap map = (HashMap)iter.next();
  217. state = (PDFGState)map.get(GSTATE);
  218. if (state != null) {
  219. newstate.addValues(state);
  220. }
  221. }
  222. if (gstate != null) {
  223. newstate.addValues(gstate);
  224. }
  225. return newstate;
  226. }
  227. }