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.

AbstractPaintingState.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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.util;
  19. import java.awt.Color;
  20. import java.awt.geom.AffineTransform;
  21. import java.io.Serializable;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Stack;
  27. /**
  28. * A base class which holds information about the current rendering state.
  29. */
  30. public abstract class AbstractPaintingState implements Cloneable, Serializable {
  31. private static final long serialVersionUID = 5998356138437094188L;
  32. /** current state data */
  33. private AbstractData data = null;
  34. /** the state stack */
  35. private StateStack/*<AbstractData>*/ stateStack = new StateStack/*<AbstractData>*/();
  36. /**
  37. * Instantiates a new state data object
  38. *
  39. * @return a new state data object
  40. */
  41. protected abstract AbstractData instantiateData();
  42. /**
  43. * Instantiates a new state object
  44. *
  45. * @return a new state object
  46. */
  47. protected abstract AbstractPaintingState instantiate();
  48. /**
  49. * Returns the currently valid state
  50. *
  51. * @return the currently valid state
  52. */
  53. public AbstractData getData() {
  54. if (data == null) {
  55. data = instantiateData();
  56. }
  57. return data;
  58. }
  59. /**
  60. * Set the current color.
  61. * Check if the new color is a change and then set the current color.
  62. *
  63. * @param col the color to set
  64. * @return true if the color has changed
  65. */
  66. public boolean setColor(Color col) {
  67. if (!col.equals(getData().color)) {
  68. getData().color = col;
  69. return true;
  70. }
  71. return false;
  72. }
  73. /**
  74. * Get the color.
  75. *
  76. * @return the color
  77. */
  78. public Color getColor() {
  79. if (getData().color == null) {
  80. getData().color = Color.black;
  81. }
  82. return getData().color;
  83. }
  84. /**
  85. * Get the background color.
  86. *
  87. * @return the background color
  88. */
  89. public Color getBackColor() {
  90. if (getData().backColor == null) {
  91. getData().backColor = Color.white;
  92. }
  93. return getData().backColor;
  94. }
  95. /**
  96. * Set the current background color.
  97. * Check if the new background color is a change and then set the current background color.
  98. *
  99. * @param col the background color to set
  100. * @return true if the color has changed
  101. */
  102. public boolean setBackColor(Color col) {
  103. if (!col.equals(getData().backColor)) {
  104. getData().backColor = col;
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * Set the current font name
  111. *
  112. * @param internalFontName the internal font name
  113. * @return true if the font name has changed
  114. */
  115. public boolean setFontName(String internalFontName) {
  116. if (!internalFontName.equals(getData().fontName)) {
  117. getData().fontName = internalFontName;
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Gets the current font name
  124. *
  125. * @return the current font name
  126. */
  127. public String getFontName() {
  128. return getData().fontName;
  129. }
  130. /**
  131. * Gets the current font size
  132. *
  133. * @return the current font size
  134. */
  135. public int getFontSize() {
  136. return getData().fontSize;
  137. }
  138. /**
  139. * Set the current font size.
  140. * Check if the font size is a change and then set the current font size.
  141. *
  142. * @param size the font size to set
  143. * @return true if the font size has changed
  144. */
  145. public boolean setFontSize(int size) {
  146. if (size != getData().fontSize) {
  147. getData().fontSize = size;
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Set the current line width.
  154. *
  155. * @param width the line width in points
  156. * @return true if the line width has changed
  157. */
  158. public boolean setLineWidth(float width) {
  159. if (getData().lineWidth != width) {
  160. getData().lineWidth = width;
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Returns the current line width
  167. *
  168. * @return the current line width
  169. */
  170. public float getLineWidth() {
  171. return getData().lineWidth;
  172. }
  173. /**
  174. * Sets the dash array (line type) for the current basic stroke
  175. *
  176. * @param dash the line dash array
  177. * @return true if the dash array has changed
  178. */
  179. public boolean setDashArray(float[] dash) {
  180. if (!Arrays.equals(dash, getData().dashArray)) {
  181. getData().dashArray = dash;
  182. return true;
  183. }
  184. return false;
  185. }
  186. /**
  187. * Get the current transform.
  188. * This gets the combination of all transforms in the
  189. * current state.
  190. *
  191. * @return the calculate combined transform for the current state
  192. */
  193. public AffineTransform getTransform() {
  194. AffineTransform at = new AffineTransform();
  195. for (Iterator iter = stateStack.iterator(); iter.hasNext();) {
  196. AbstractData data = (AbstractData)iter.next();
  197. AffineTransform stackTrans = data.getTransform();
  198. at.concatenate(stackTrans);
  199. }
  200. AffineTransform currentTrans = getData().getTransform();
  201. at.concatenate(currentTrans);
  202. return at;
  203. }
  204. /**
  205. * Check the current transform.
  206. * The transform for the current state is the combination of all
  207. * transforms in the current state. The parameter is compared
  208. * against this current transform.
  209. *
  210. * @param tf the transform the check against
  211. * @return true if the new transform is different then the current transform
  212. */
  213. public boolean checkTransform(AffineTransform tf) {
  214. return !tf.equals(getData().getTransform());
  215. }
  216. /**
  217. * Get a copy of the base transform for the page. Used to translate
  218. * IPP/BPP values into X,Y positions when positioning is "fixed".
  219. *
  220. * @return the base transform, or null if the state stack is empty
  221. */
  222. public AffineTransform getBaseTransform() {
  223. if (stateStack.isEmpty()) {
  224. return null;
  225. } else {
  226. AbstractData baseData = (AbstractData)stateStack.get(0);
  227. return (AffineTransform) baseData.getTransform().clone();
  228. }
  229. }
  230. /**
  231. * Concatenates the given AffineTransform to the current one.
  232. *
  233. * @param at the transform to concatenate to the current level transform
  234. */
  235. public void concatenate(AffineTransform at) {
  236. getData().concatenate(at);
  237. }
  238. /**
  239. * Resets the current AffineTransform to the Base AffineTransform.
  240. */
  241. public void resetTransform() {
  242. getData().setTransform(getBaseTransform());
  243. }
  244. /**
  245. * Clears the current AffineTransform to the Identity AffineTransform
  246. */
  247. public void clearTransform() {
  248. getData().clearTransform();
  249. }
  250. /**
  251. * Push the current painting state onto the stack.
  252. * This call should be used when the Q operator is used
  253. * so that the state is known when popped.
  254. */
  255. public void push() {
  256. AbstractData copy = (AbstractData)getData().clone();
  257. stateStack.push(copy);
  258. }
  259. /**
  260. * Pop the painting state from the stack and set current values to popped state.
  261. * This should be called when a Q operator is used so
  262. * the state is restored to the correct values.
  263. *
  264. * @return the restored state, null if the stack is empty
  265. */
  266. public AbstractData pop() {
  267. if (!stateStack.isEmpty()) {
  268. setData((AbstractData)stateStack.pop());
  269. return this.data;
  270. } else {
  271. return null;
  272. }
  273. }
  274. /**
  275. * Pushes all painting state data in the given list to the stack
  276. *
  277. * @param dataList a state data list
  278. */
  279. public void pushAll(List/*<AbstractData>*/ dataList) {
  280. Iterator it = dataList.iterator();
  281. while (it.hasNext()) {
  282. // save current data on stack
  283. push();
  284. setData((AbstractData)it.next());
  285. }
  286. }
  287. /**
  288. * Pops all painting state data from the stack
  289. *
  290. * @return a list of state data popped from the stack
  291. */
  292. public List/*<AbstractData>*/ popAll() {
  293. List/*<AbstractData>*/ dataList = new java.util.ArrayList/*<AbstractData>*/();
  294. AbstractData data;
  295. while (true) {
  296. data = getData();
  297. if (pop() == null) {
  298. break;
  299. }
  300. // insert because of stack-popping
  301. dataList.add(0, data);
  302. }
  303. return dataList;
  304. }
  305. /**
  306. * Sets the current state data
  307. *
  308. * @param currentData state data
  309. */
  310. protected void setData(AbstractData data) {
  311. this.data = data;
  312. }
  313. /**
  314. * Clears the state stack
  315. */
  316. public void clear() {
  317. stateStack.clear();
  318. setData(null);
  319. }
  320. /**
  321. * Return the state stack
  322. *
  323. * @return the state stack
  324. */
  325. protected Stack/*<AbstractData>*/ getStateStack() {
  326. return this.stateStack;
  327. }
  328. /** {@inheritDoc} */
  329. public Object clone() {
  330. AbstractPaintingState state = instantiate();
  331. state.stateStack = new StateStack(this.stateStack);
  332. state.data = (AbstractData)this.data.clone();
  333. return state;
  334. }
  335. /** {@inheritDoc} */
  336. public String toString() {
  337. return ", stateStack=" + stateStack
  338. + ", currentData=" + data;
  339. }
  340. /**
  341. * A stack implementation which holds state objects
  342. */
  343. public class StateStack extends java.util.Stack {
  344. private static final long serialVersionUID = 4897178211223823041L;
  345. /**
  346. * Default constructor
  347. */
  348. public StateStack() {
  349. super();
  350. }
  351. /**
  352. * Copy constructor
  353. *
  354. * @param c initial contents of stack
  355. */
  356. public StateStack(Collection c) {
  357. elementCount = c.size();
  358. // 10% for growth
  359. elementData = new Object[
  360. (int)Math.min((elementCount * 110L) / 100, Integer.MAX_VALUE)];
  361. c.toArray(elementData);
  362. }
  363. }
  364. /**
  365. * A base painting state data holding object
  366. */
  367. public abstract class AbstractData implements Cloneable, Serializable {
  368. private static final long serialVersionUID = 5208418041189828624L;
  369. /** The current color */
  370. protected Color color = null;
  371. /** The current background color */
  372. protected Color backColor = null;
  373. /** The current font name */
  374. protected String fontName = null;
  375. /** The current font size */
  376. protected int fontSize = 0;
  377. /** The current line width */
  378. protected float lineWidth = 0;
  379. /** The dash array for the current basic stroke (line type) */
  380. protected float[] dashArray = null;
  381. /** The current transform */
  382. protected AffineTransform transform = null;
  383. /**
  384. * Returns a newly create data object
  385. *
  386. * @return a new data object
  387. */
  388. protected abstract AbstractData instantiate();
  389. /**
  390. * Concatenate the given AffineTransform with the current thus creating
  391. * a new viewport. Note that all concatenation operations are logged
  392. * so they can be replayed if necessary (ex. for block-containers with
  393. * "fixed" positioning.
  394. *
  395. * @param at Transformation to perform
  396. */
  397. public void concatenate(AffineTransform at) {
  398. getTransform().concatenate(at);
  399. }
  400. /**
  401. * Get the current AffineTransform.
  402. *
  403. * @return the current transform
  404. */
  405. public AffineTransform getTransform() {
  406. if (transform == null) {
  407. transform = new AffineTransform();
  408. }
  409. return transform;
  410. }
  411. /**
  412. * Sets the current AffineTransform.
  413. */
  414. public void setTransform(AffineTransform baseTransform) {
  415. this.transform = baseTransform;
  416. }
  417. /**
  418. * Resets the current AffineTransform.
  419. */
  420. public void clearTransform() {
  421. transform = new AffineTransform();
  422. }
  423. /**
  424. * Returns the derived rotation from the current transform
  425. *
  426. * @return the derived rotation from the current transform
  427. */
  428. public int getDerivedRotation() {
  429. AffineTransform at = getTransform();
  430. double sx = at.getScaleX();
  431. double sy = at.getScaleY();
  432. double shx = at.getShearX();
  433. double shy = at.getShearY();
  434. int rotation = 0;
  435. if (sx == 0 && sy == 0 && shx > 0 && shy < 0) {
  436. rotation = 270;
  437. } else if (sx < 0 && sy < 0 && shx == 0 && shy == 0) {
  438. rotation = 180;
  439. } else if (sx == 0 && sy == 0 && shx < 0 && shy > 0) {
  440. rotation = 90;
  441. } else {
  442. rotation = 0;
  443. }
  444. return rotation;
  445. }
  446. /** {@inheritDoc} */
  447. public Object clone() {
  448. AbstractData data = instantiate();
  449. data.color = this.color;
  450. data.backColor = this.backColor;
  451. data.fontName = this.fontName;
  452. data.fontSize = this.fontSize;
  453. data.lineWidth = this.lineWidth;
  454. data.dashArray = this.dashArray;
  455. data.transform = new AffineTransform(this.transform);
  456. return data;
  457. }
  458. /** {@inheritDoc} */
  459. public String toString() {
  460. return "color=" + color
  461. + ", backColor=" + backColor
  462. + ", fontName=" + fontName
  463. + ", fontSize=" + fontSize
  464. + ", lineWidth=" + lineWidth
  465. + ", dashArray=" + dashArray
  466. + ", transform=" + transform;
  467. }
  468. }
  469. }