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.

AFPPaintingState.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.afp;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.util.AbstractPaintingState;
  22. /**
  23. * This keeps information about the current painting state when writing to an AFP datastream.
  24. */
  25. public class AFPPaintingState extends org.apache.fop.util.AbstractPaintingState implements Cloneable {
  26. private static final long serialVersionUID = 8206711712452344473L;
  27. private static Log log = LogFactory.getLog("org.apache.xmlgraphics.afp");
  28. /** the portrait rotation */
  29. private int portraitRotation = 0;
  30. /** the landscape rotation */
  31. private int landscapeRotation = 270;
  32. /** color image support */
  33. private boolean colorImages = false;
  34. /** images are supported in this AFP environment */
  35. private boolean nativeImages = false;
  36. /** default value for image depth */
  37. private int bitsPerPixel = 8;
  38. /** the output resolution */
  39. private int resolution = 240; // 240 dpi
  40. /** the current page */
  41. private AFPPagePaintingState pagePaintingState = new AFPPagePaintingState();
  42. // /** reference orientation */
  43. // private int orientation = 0;
  44. /** a unit converter */
  45. private final transient AFPUnitConverter unitConv = new AFPUnitConverter(this);
  46. /**
  47. * Sets the rotation to be used for portrait pages, valid values are 0
  48. * (default), 90, 180, 270.
  49. *
  50. * @param rotation
  51. * The rotation in degrees.
  52. */
  53. public void setPortraitRotation(int rotation) {
  54. if (rotation == 0 || rotation == 90 || rotation == 180
  55. || rotation == 270) {
  56. portraitRotation = rotation;
  57. } else {
  58. throw new IllegalArgumentException(
  59. "The portrait rotation must be one"
  60. + " of the values 0, 90, 180, 270");
  61. }
  62. }
  63. /**
  64. * Returns the rotation to be used for portrait pages
  65. *
  66. * @return the rotation to be used for portrait pages
  67. */
  68. protected int getPortraitRotation() {
  69. return this.portraitRotation;
  70. }
  71. /**
  72. * Sets the rotation to be used for landscape pages, valid values are 0, 90,
  73. * 180, 270 (default).
  74. *
  75. * @param rotation
  76. * The rotation in degrees.
  77. */
  78. public void setLandscapeRotation(int rotation) {
  79. if (rotation == 0 || rotation == 90 || rotation == 180
  80. || rotation == 270) {
  81. landscapeRotation = rotation;
  82. } else {
  83. throw new IllegalArgumentException(
  84. "The landscape rotation must be one"
  85. + " of the values 0, 90, 180, 270");
  86. }
  87. }
  88. /**
  89. * Returns the landscape rotation
  90. *
  91. * @return the landscape rotation
  92. */
  93. protected int getLandscapeRotation() {
  94. return this.landscapeRotation;
  95. }
  96. /**
  97. * Sets the number of bits used per pixel
  98. *
  99. * @param bitsPerPixel
  100. * number of bits per pixel
  101. */
  102. public void setBitsPerPixel(int bitsPerPixel) {
  103. switch (bitsPerPixel) {
  104. case 1:
  105. case 4:
  106. case 8:
  107. this.bitsPerPixel = bitsPerPixel;
  108. break;
  109. default:
  110. log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
  111. this.bitsPerPixel = 8;
  112. break;
  113. }
  114. }
  115. /**
  116. * Returns the number of bits per pixel
  117. *
  118. * @return the number of bits per pixel
  119. */
  120. public int getBitsPerPixel() {
  121. return this.bitsPerPixel;
  122. }
  123. /**
  124. * Sets whether images are color or not
  125. *
  126. * @param colorImages
  127. * color image output
  128. */
  129. public void setColorImages(boolean colorImages) {
  130. this.colorImages = colorImages;
  131. }
  132. /**
  133. * Returns true if color images are to be used
  134. *
  135. * @return true if color images are to be used
  136. */
  137. public boolean isColorImages() {
  138. return this.colorImages;
  139. }
  140. /**
  141. * Sets whether images are natively supported or not in the AFP environment
  142. *
  143. * @param nativeImages true if images are natively supported in this AFP environment
  144. */
  145. public void setNativeImages(boolean nativeImages) {
  146. this.nativeImages = nativeImages;
  147. }
  148. /**
  149. * Returns true if images are supported natively in this AFP environment
  150. *
  151. * @return true if images are supported natively in this AFP environment
  152. */
  153. public boolean isNativeImages() {
  154. return this.nativeImages;
  155. }
  156. /**
  157. * Sets the output/device resolution
  158. *
  159. * @param resolution
  160. * the output resolution (dpi)
  161. */
  162. public void setResolution(int resolution) {
  163. if (log.isDebugEnabled()) {
  164. log.debug("renderer-resolution set to: " + resolution + "dpi");
  165. }
  166. this.resolution = resolution;
  167. }
  168. /**
  169. * Returns the output/device resolution.
  170. *
  171. * @return the resolution in dpi
  172. */
  173. public int getResolution() {
  174. return this.resolution;
  175. }
  176. /** {@inheritDoc} */
  177. protected AbstractData instantiateData() {
  178. return new AFPData();
  179. }
  180. /** {@inheritDoc} */
  181. protected AbstractPaintingState instantiate() {
  182. return new AFPPaintingState();
  183. }
  184. /**
  185. * Returns the painting state of the current page
  186. *
  187. * @return the painting state of the current page
  188. */
  189. protected AFPPagePaintingState getPagePaintingState() {
  190. return this.pagePaintingState;
  191. }
  192. /**
  193. * Sets if the current painted shape is to be filled
  194. *
  195. * @param fill true if the current painted shape is to be filled
  196. * @return true if the fill value has changed
  197. */
  198. protected boolean setFill(boolean fill) {
  199. if (fill != ((AFPData)getData()).filled) {
  200. ((AFPData)getData()).filled = fill;
  201. return true;
  202. }
  203. return false;
  204. }
  205. /**
  206. * Gets the current page fonts
  207. *
  208. * @return the current page fonts
  209. */
  210. public AFPPageFonts getPageFonts() {
  211. return pagePaintingState.getFonts();
  212. }
  213. /**
  214. * Increments and returns the page font count
  215. *
  216. * @return the page font count
  217. */
  218. public int incrementPageFontCount() {
  219. return pagePaintingState.incrementFontCount();
  220. }
  221. /**
  222. * Sets the page width
  223. *
  224. * @param pageWidth the page width
  225. */
  226. public void setPageWidth(int pageWidth) {
  227. pagePaintingState.setWidth(pageWidth);
  228. }
  229. /**
  230. * Returns the page width
  231. *
  232. * @return the page width
  233. */
  234. public int getPageWidth() {
  235. return pagePaintingState.getWidth();
  236. }
  237. /**
  238. * Sets the page height
  239. *
  240. * @param pageHeight the page height
  241. */
  242. public void setPageHeight(int pageHeight) {
  243. pagePaintingState.setHeight(pageHeight);
  244. }
  245. /**
  246. * Returns the page height
  247. *
  248. * @return the page height
  249. */
  250. public int getPageHeight() {
  251. return pagePaintingState.getHeight();
  252. }
  253. /**
  254. * Returns the page rotation
  255. *
  256. * @return the page rotation
  257. */
  258. public int getPageRotation() {
  259. return pagePaintingState.getOrientation();
  260. }
  261. /**
  262. * Sets the uri of the current image
  263. *
  264. * @param uri the uri of the current image
  265. */
  266. public void setImageUri(String uri) {
  267. ((AFPData)getData()).imageUri = uri;
  268. }
  269. /**
  270. * Gets the uri of the current image
  271. *
  272. * @return the uri of the current image
  273. */
  274. public String getImageUri() {
  275. return ((AFPData)getData()).imageUri;
  276. }
  277. /**
  278. * Returns the currently derived rotation
  279. *
  280. * @return the currently derived rotation
  281. */
  282. public int getRotation() {
  283. return getData().getDerivedRotation();
  284. }
  285. /**
  286. * Returns the unit converter
  287. *
  288. * @return the unit converter
  289. */
  290. public AFPUnitConverter getUnitConverter() {
  291. return this.unitConv;
  292. }
  293. /** {@inheritDoc} */
  294. public Object clone() {
  295. AFPPaintingState state = (AFPPaintingState)super.clone();
  296. state.pagePaintingState = (AFPPagePaintingState)this.pagePaintingState.clone();
  297. state.portraitRotation = this.portraitRotation;
  298. state.landscapeRotation = this.landscapeRotation;
  299. state.bitsPerPixel = this.bitsPerPixel;
  300. state.colorImages = this.colorImages;
  301. state.resolution = this.resolution;
  302. return state;
  303. }
  304. /** {@inheritDoc} */
  305. public String toString() {
  306. return "AFPPaintingState{" + "portraitRotation=" + portraitRotation
  307. + ", landscapeRotation=" + landscapeRotation
  308. + ", colorImages=" + colorImages
  309. + ", bitsPerPixel=" + bitsPerPixel
  310. + ", resolution=" + resolution
  311. + ", pageState=" + pagePaintingState
  312. + super.toString()
  313. + "}";
  314. }
  315. /**
  316. * Page level state data
  317. */
  318. private class AFPPagePaintingState implements Cloneable {
  319. /** page width */
  320. private int width = 0;
  321. /** page height */
  322. private int height = 0;
  323. /** page fonts */
  324. private AFPPageFonts fonts = new AFPPageFonts();
  325. /** page font count */
  326. private int fontCount = 0;
  327. /** page orientation */
  328. private int orientation = 0;
  329. /**
  330. * Returns the page width
  331. *
  332. * @return the page width
  333. */
  334. protected int getWidth() {
  335. return width;
  336. }
  337. /**
  338. * Sets the page width
  339. *
  340. * @param width the page width
  341. */
  342. protected void setWidth(int width) {
  343. this.width = width;
  344. }
  345. /**
  346. * Returns the page height
  347. *
  348. * @return the page height
  349. */
  350. protected int getHeight() {
  351. return height;
  352. }
  353. /**
  354. * Sets the page height
  355. *
  356. * @param height the page height
  357. */
  358. protected void setHeight(int height) {
  359. this.height = height;
  360. }
  361. /**
  362. * Returns the page fonts
  363. *
  364. * @return the page fonts
  365. */
  366. protected AFPPageFonts getFonts() {
  367. return fonts;
  368. }
  369. /**
  370. * Sets the current page fonts
  371. *
  372. * @param fonts the current page fonts
  373. */
  374. protected void setFonts(AFPPageFonts fonts) {
  375. this.fonts = fonts;
  376. }
  377. /**
  378. * Increments and returns the current page font count
  379. *
  380. * @return increment and return the current page font count
  381. */
  382. protected int incrementFontCount() {
  383. return ++fontCount;
  384. }
  385. /**
  386. * Returns the current page orientation
  387. *
  388. * @return the current page orientation
  389. */
  390. protected int getOrientation() {
  391. return orientation;
  392. }
  393. /**
  394. * Sets the current page orientation
  395. *
  396. * @param orientation the current page orientation
  397. */
  398. protected void setOrientation(int orientation) {
  399. this.orientation = orientation;
  400. }
  401. /** {@inheritDoc} */
  402. public Object clone() {
  403. AFPPagePaintingState state = new AFPPagePaintingState();
  404. state.width = this.width;
  405. state.height = this.height;
  406. state.orientation = this.orientation;
  407. state.fonts = new AFPPageFonts(this.fonts);
  408. state.fontCount = this.fontCount;
  409. return state;
  410. }
  411. /** {@inheritDoc} */
  412. public String toString() {
  413. return "AFPPagePaintingState{width=" + width
  414. + ", height=" + height
  415. + ", orientation=" + orientation
  416. + ", fonts=" + fonts
  417. + ", fontCount=" + fontCount
  418. + "}";
  419. }
  420. }
  421. /**
  422. * Block level state data
  423. */
  424. private class AFPData extends org.apache.fop.util.AbstractPaintingState.AbstractData {
  425. private static final long serialVersionUID = -1789481244175275686L;
  426. /** The current fill status */
  427. private boolean filled = false;
  428. private String imageUri = null;
  429. /** {@inheritDoc} */
  430. public Object clone() {
  431. AFPData obj = (AFPData)super.clone();
  432. obj.filled = this.filled;
  433. obj.imageUri = this.imageUri;
  434. return obj;
  435. }
  436. /** {@inheritDoc} */
  437. public String toString() {
  438. return "AFPData{" + super.toString()
  439. + ", filled=" + filled
  440. + ", imageUri=" + imageUri
  441. + "}";
  442. }
  443. /** {@inheritDoc} */
  444. protected AbstractData instantiate() {
  445. return new AFPData();
  446. }
  447. }
  448. }