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.

PageObject.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.render.afp.modca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.render.afp.ioca.ImageCellPosition;
  22. import org.apache.fop.render.afp.ioca.ImageInputDescriptor;
  23. import org.apache.fop.render.afp.ioca.ImageOutputControl;
  24. import org.apache.fop.render.afp.ioca.ImageRasterData;
  25. import org.apache.fop.render.afp.ioca.ImageRasterPattern;
  26. /**
  27. * Pages contain the data objects that comprise a presentation document. Each
  28. * page has a set of data objects associated with it. Each page within a
  29. * document is independent from any other page, and each must establish its own
  30. * environment parameters.
  31. *
  32. * The page is the level in the document component hierarchy that is used for
  33. * printing or displaying a document's content. The data objects contained in
  34. * the page envelope in the data stream are presented when the page is
  35. * presented. Each data object has layout information associated with it that
  36. * directs the placement and orientation of the data on the page. In addition,
  37. * each page contains layout information that specifies the measurement units,
  38. * page width, and page depth.
  39. *
  40. * A page is initiated by a begin page structured field and terminated by an end
  41. * page structured field. Structured fields that define objects and active
  42. * environment groups or that specify attributes of the page may be encountered
  43. * in page state.
  44. *
  45. */
  46. public class PageObject extends AbstractResourceGroupContainer {
  47. /**
  48. * Construct a new page object for the specified name argument, the page
  49. * name should be an 8 character identifier.
  50. *
  51. * @param factory the resource manager
  52. *
  53. * @param name
  54. * the name of the page.
  55. * @param width
  56. * the width of the page.
  57. * @param height
  58. * the height of the page.
  59. * @param rotation
  60. * the rotation of the page.
  61. * @param widthRes
  62. * the width resolution of the page.
  63. * @param heightRes
  64. * the height resolution of the page.
  65. */
  66. public PageObject(Factory factory,
  67. String name, int width, int height, int rotation,
  68. int widthRes, int heightRes) {
  69. super(factory, name, width, height, rotation, widthRes, heightRes);
  70. }
  71. /**
  72. * Creates an IncludePageOverlay on the page.
  73. *
  74. * @param name
  75. * the name of the overlay
  76. * @param x
  77. * the x position of the overlay
  78. * @param y
  79. * the y position of the overlay
  80. * @param orientation
  81. * the orientation required for the overlay
  82. */
  83. public void createIncludePageOverlay(String name, int x, int y, int orientation) {
  84. getActiveEnvironmentGroup().createOverlay(name);
  85. IncludePageOverlay ipo = new IncludePageOverlay(name, x, y, orientation);
  86. addObject(ipo);
  87. }
  88. /**
  89. * This method will create shading on the page using the specified
  90. * coordinates (the shading contrast is controlled via the red, green blue
  91. * parameters, by converting this to grayscale).
  92. *
  93. * @param x
  94. * the x coordinate of the shading
  95. * @param y
  96. * the y coordinate of the shading
  97. * @param w
  98. * the width of the shaded area
  99. * @param h
  100. * the height of the shaded area
  101. * @param red
  102. * the red value
  103. * @param green
  104. * the green value
  105. * @param blue
  106. * the blue value
  107. */
  108. public void createShading(int x, int y, int w, int h, int red, int green, int blue) {
  109. int xCoord = 0;
  110. int yCoord = 0;
  111. int areaWidth = 0;
  112. int areaHeight = 0;
  113. switch (rotation) {
  114. case 90:
  115. xCoord = areaWidth - y - h;
  116. yCoord = x;
  117. areaWidth = h;
  118. areaHeight = w;
  119. break;
  120. case 180:
  121. xCoord = areaWidth - x - w;
  122. yCoord = areaHeight - y - h;
  123. areaWidth = w;
  124. areaHeight = h;
  125. break;
  126. case 270:
  127. xCoord = y;
  128. yCoord = areaHeight - x - w;
  129. areaWidth = h;
  130. areaHeight = w;
  131. break;
  132. default:
  133. xCoord = x;
  134. yCoord = y;
  135. areaWidth = w;
  136. areaHeight = h;
  137. break;
  138. }
  139. // Convert the color to grey scale
  140. float shade = (float) ((red * 0.3) + (green * 0.59) + (blue * 0.11));
  141. int grayscale = Math.round((shade / 255) * 16);
  142. IMImageObject imImageObject = factory.createIMImageObject();
  143. ImageOutputControl imageOutputControl = new ImageOutputControl(0, 0);
  144. ImageInputDescriptor imageInputDescriptor = new ImageInputDescriptor();
  145. ImageCellPosition imageCellPosition = new ImageCellPosition(xCoord, yCoord);
  146. imageCellPosition.setXFillSize(areaWidth);
  147. imageCellPosition.setYFillSize(areaHeight);
  148. imageCellPosition.setXSize(64);
  149. imageCellPosition.setYSize(8);
  150. //defining this as a resource
  151. byte[] rasterData = ImageRasterPattern.getRasterData(grayscale);
  152. ImageRasterData imageRasterData = factory.createImageRasterData(rasterData);
  153. imImageObject.setImageOutputControl(imageOutputControl);
  154. imImageObject.setImageInputDescriptor(imageInputDescriptor);
  155. imImageObject.setImageCellPosition(imageCellPosition);
  156. imImageObject.setImageRasterData(imageRasterData);
  157. addObject(imImageObject);
  158. }
  159. /** {@inheritDoc} */
  160. protected void writeStart(OutputStream os) throws IOException {
  161. byte[] data = new byte[17];
  162. copySF(data, Type.BEGIN, Category.PAGE);
  163. os.write(data);
  164. }
  165. /** {@inheritDoc} */
  166. protected void writeContent(OutputStream os) throws IOException {
  167. super.writeContent(os);
  168. getActiveEnvironmentGroup().writeToStream(os);
  169. writeObjects(includePageSegments, os);
  170. writeObjects(tagLogicalElements, os);
  171. writeObjects(objects, os);
  172. }
  173. /** {@inheritDoc} */
  174. protected void writeEnd(OutputStream os) throws IOException {
  175. byte[] data = new byte[17];
  176. copySF(data, Type.END, Category.PAGE);
  177. os.write(data);
  178. }
  179. /**
  180. * Adds an AFP object reference to this page
  181. *
  182. * @param obj an AFP object
  183. */
  184. public void addObject(Object obj) {
  185. endPresentationObject();
  186. super.addObject(obj);
  187. }
  188. /** {@inheritDoc} */
  189. public String toString() {
  190. return this.getName();
  191. }
  192. /** {@inheritDoc} */
  193. protected boolean canWrite(AbstractAFPObject ao) {
  194. return true;
  195. }
  196. }