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

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