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 14KB

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