Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PDFState.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. import java.awt.Shape;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.awt.geom.GeneralPath;
  23. import java.awt.geom.Area;
  24. import java.awt.Color;
  25. import java.awt.Paint;
  26. import java.awt.geom.AffineTransform;
  27. /**
  28. * This keeps information about the current state when writing to pdf.
  29. * It allows for creating new graphics states with the q operator.
  30. * This class is only used to store the information about the state
  31. * the caller needs to handle the actual pdf operators.
  32. *
  33. * When setting the state for pdf there are three possible ways of
  34. * handling the situation.
  35. * The values can be set to override previous or default values.
  36. * A new state can be added and then the values set.
  37. * The current state can be popped and values will return to a
  38. * previous state then the necessary values can be overridden.
  39. * The current transform behaves differently to other values as the
  40. * matrix is combined with the current resolved value.
  41. * It is impossible to optimise the result without analysing the all
  42. * the possible combinations after completing.
  43. */
  44. public class PDFState {
  45. private static final String COLOR = "color";
  46. private static final String BACKCOLOR = "backcolor";
  47. private static final String PAINT = "paint";
  48. private static final String BACKPAINT = "backpaint";
  49. private static final String LINECAP = "lineCap";
  50. private static final String LINEJOIN = "lineJoin";
  51. private static final String LINEWIDTH = "lineWidth";
  52. private static final String MITERLIMIT = "miterLimit";
  53. private static final String TEXT = "text";
  54. private static final String DASHOFFSET = "dashOffset";
  55. private static final String DASHARRAY = "dashArray";
  56. private static final String TRANSFORM = "transform";
  57. private static final String FONTSIZE = "fontSize";
  58. private static final String FONTNAME = "fontName";
  59. private static final String CLIP = "clip";
  60. private static final String GSTATE = "gstate";
  61. private Color color = Color.black;
  62. private Color backcolor = Color.white;
  63. private Paint paint = null;
  64. private Paint backPaint = null;
  65. private int lineCap = 0;
  66. private int lineJoin = 0;
  67. private float lineWidth = 1;
  68. private float miterLimit = 0;
  69. private boolean text = false;
  70. private int dashOffset = 0;
  71. private int[] dashArray = new int[0];
  72. private AffineTransform transform = new AffineTransform();
  73. private float fontSize = 0;
  74. private String fontName = "";
  75. private Shape clip = null;
  76. private PDFGState gstate = null;
  77. private ArrayList stateStack = new ArrayList();
  78. /**
  79. * PDF State for storing graphics state.
  80. */
  81. public PDFState() {
  82. }
  83. /**
  84. * Push the current state onto the stack.
  85. * This call should be used when the q operator is used
  86. * so that the state is known when popped.
  87. */
  88. public void push() {
  89. HashMap saveMap = new HashMap();
  90. saveMap.put(COLOR, color);
  91. saveMap.put(BACKCOLOR, backcolor);
  92. saveMap.put(PAINT, paint);
  93. saveMap.put(BACKPAINT, backPaint);
  94. saveMap.put(LINECAP, new Integer(lineCap));
  95. saveMap.put(LINEJOIN, new Integer(lineJoin));
  96. saveMap.put(LINEWIDTH, new Float(lineWidth));
  97. saveMap.put(MITERLIMIT, new Float(miterLimit));
  98. saveMap.put(TEXT, new Boolean(text));
  99. saveMap.put(DASHOFFSET, new Integer(dashOffset));
  100. saveMap.put(DASHARRAY, dashArray);
  101. saveMap.put(TRANSFORM, transform);
  102. saveMap.put(FONTSIZE, new Float(fontSize));
  103. saveMap.put(FONTNAME, fontName);
  104. saveMap.put(CLIP, clip);
  105. saveMap.put(GSTATE, gstate);
  106. stateStack.add(saveMap);
  107. transform = new AffineTransform();
  108. }
  109. /**
  110. * Pop the state from the stack and set current values to popped state.
  111. * This should be called when a Q operator is used so
  112. * the state is restored to the correct values.
  113. */
  114. public void pop() {
  115. if (getStackLevel() > 0) {
  116. HashMap saveMap = (HashMap)stateStack.get(stateStack.size() - 1);
  117. stateStack.remove(stateStack.size() - 1);
  118. color = (Color)saveMap.get(COLOR);
  119. backcolor = (Color)saveMap.get(BACKCOLOR);
  120. paint = (Paint)saveMap.get(PAINT);
  121. backPaint = (Paint)saveMap.get(BACKPAINT);
  122. lineCap = ((Integer)saveMap.get(LINECAP)).intValue();
  123. lineJoin = ((Integer)saveMap.get(LINEJOIN)).intValue();
  124. lineWidth = ((Float)saveMap.get(LINEWIDTH)).floatValue();
  125. miterLimit = ((Float)saveMap.get(MITERLIMIT)).floatValue();
  126. text = ((Boolean)saveMap.get(TEXT)).booleanValue();
  127. dashOffset = ((Integer)saveMap.get(DASHOFFSET)).intValue();
  128. dashArray = (int[])saveMap.get(DASHARRAY);
  129. transform = (AffineTransform)saveMap.get(TRANSFORM);
  130. fontSize = ((Float)saveMap.get(FONTSIZE)).floatValue();
  131. fontName = (String)saveMap.get(FONTNAME);
  132. clip = (Shape)saveMap.get(CLIP);
  133. gstate = (PDFGState)saveMap.get(GSTATE);
  134. }
  135. }
  136. /**
  137. * Get the current stack level.
  138. *
  139. * @return the current stack level
  140. */
  141. public int getStackLevel() {
  142. return stateStack.size();
  143. }
  144. /**
  145. * Restore the state to a particular level.
  146. * this can be used to restore to a known level without making
  147. * multiple pop calls.
  148. *
  149. * @param stack the level to restore to
  150. */
  151. public void restoreLevel(int stack) {
  152. int pos = stack;
  153. while (stateStack.size() > pos + 1) {
  154. stateStack.remove(stateStack.size() - 1);
  155. }
  156. if (stateStack.size() > pos) {
  157. pop();
  158. }
  159. }
  160. /**
  161. * Set the current line dash.
  162. * Check if setting the line dash to the given values
  163. * will make a change and then set the state to the new values.
  164. *
  165. * @param array the line dash array
  166. * @param offset the line dash start offset
  167. * @return true if the line dash has changed
  168. */
  169. public boolean setLineDash(int[] array, int offset) {
  170. return false;
  171. }
  172. /**
  173. * Set the current color.
  174. * Check if the new color is a change and then set the current color.
  175. *
  176. * @param col the color to set
  177. * @return true if the color has changed
  178. */
  179. public boolean setColor(Color col) {
  180. if (!col.equals(color)) {
  181. color = col;
  182. return true;
  183. }
  184. return false;
  185. }
  186. /**
  187. * Set the current background color.
  188. * Check if the background color will change and then set the new color.
  189. *
  190. * @param col the new background color
  191. * @return true if the background color has changed
  192. */
  193. public boolean setBackColor(Color col) {
  194. if (!col.equals(backcolor)) {
  195. backcolor = col;
  196. return true;
  197. }
  198. return false;
  199. }
  200. /**
  201. * Set the current paint.
  202. * This checks if the paint will change and then sets the current paint.
  203. *
  204. * @param p the new paint
  205. * @return true if the new paint changes the current paint
  206. */
  207. public boolean setPaint(Paint p) {
  208. if (paint == null) {
  209. if (p != null) {
  210. paint = p;
  211. return true;
  212. }
  213. } else if (!paint.equals(p)) {
  214. paint = p;
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. * Check if the clip will change the current state.
  221. * A clip is assumed to be used in a situation where it will add
  222. * to any clip in the current or parent states.
  223. * A clip cannot be cleared, this can only be achieved by going to
  224. * a parent level with the correct clip.
  225. * If the clip is different then it may start a new state so that
  226. * it can return to the previous clip.
  227. *
  228. * @param cl the clip shape to check
  229. * @return true if the clip will change the current clip.
  230. */
  231. public boolean checkClip(Shape cl) {
  232. if (clip == null) {
  233. if (cl != null) {
  234. return true;
  235. }
  236. } else if (!new Area(clip).equals(new Area(cl))) {
  237. return true;
  238. }
  239. // todo check for clips that are larger than the current
  240. return false;
  241. }
  242. /**
  243. * Set the current clip.
  244. * This either sets a new clip or sets the clip to the intersect of
  245. * the old clip and the new clip.
  246. *
  247. * @param cl the new clip in the current state
  248. */
  249. public void setClip(Shape cl) {
  250. if (clip != null) {
  251. Area newClip = new Area(clip);
  252. newClip.intersect(new Area(cl));
  253. clip = new GeneralPath(newClip);
  254. } else {
  255. clip = cl;
  256. }
  257. }
  258. /**
  259. * Check the current transform.
  260. * The transform for the current state is the combination of all
  261. * transforms in the current state. The parameter is compared
  262. * against this current transform.
  263. *
  264. * @param tf the transform the check against
  265. * @return true if the new transform is different then the current transform
  266. */
  267. public boolean checkTransform(AffineTransform tf) {
  268. return !tf.equals(transform);
  269. }
  270. /**
  271. * Set a new transform.
  272. * This transform is appended to the transform of
  273. * the current graphic state.
  274. *
  275. * @param tf the transform to concatonate to the current level transform
  276. */
  277. public void setTransform(AffineTransform tf) {
  278. transform.concatenate(tf);
  279. }
  280. /**
  281. * Get the current transform.
  282. * This gets the combination of all transforms in the
  283. * current state.
  284. *
  285. * @return the calculate combined transform for the current state
  286. */
  287. public AffineTransform getTransform() {
  288. AffineTransform tf;
  289. AffineTransform at = new AffineTransform();
  290. for (Iterator iter = stateStack.iterator(); iter.hasNext();) {
  291. HashMap map = (HashMap)iter.next();
  292. tf = (AffineTransform)map.get(TRANSFORM);
  293. at.concatenate(tf);
  294. }
  295. at.concatenate(transform);
  296. return at;
  297. }
  298. /**
  299. * Get the grapics state.
  300. * This gets the combination of all graphic states for
  301. * the current context.
  302. * This is the graphic state set with the gs operator not
  303. * the other graphic state changes.
  304. *
  305. * @return the calculated ExtGState in the current context
  306. */
  307. public PDFGState getGState() {
  308. PDFGState defaultState = PDFGState.DEFAULT;
  309. PDFGState state;
  310. PDFGState newstate = new PDFGState();
  311. newstate.addValues(defaultState);
  312. for (Iterator iter = stateStack.iterator(); iter.hasNext();) {
  313. HashMap map = (HashMap)iter.next();
  314. state = (PDFGState)map.get(GSTATE);
  315. if (state != null) {
  316. newstate.addValues(state);
  317. }
  318. }
  319. if (gstate != null) {
  320. newstate.addValues(gstate);
  321. }
  322. return newstate;
  323. }
  324. }