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.

AFPDocumentHandler.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.geom.AffineTransform;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. import org.apache.fop.afp.AFPDitheredRectanglePainter;
  25. import org.apache.fop.afp.AFPPaintingState;
  26. import org.apache.fop.afp.AFPRectanglePainter;
  27. import org.apache.fop.afp.AFPResourceLevelDefaults;
  28. import org.apache.fop.afp.AFPResourceManager;
  29. import org.apache.fop.afp.AFPUnitConverter;
  30. import org.apache.fop.afp.AbstractAFPPainter;
  31. import org.apache.fop.afp.DataStream;
  32. import org.apache.fop.afp.fonts.AFPFontCollection;
  33. import org.apache.fop.afp.fonts.AFPPageFonts;
  34. import org.apache.fop.afp.modca.ResourceObject;
  35. import org.apache.fop.afp.util.DefaultFOPResourceAccessor;
  36. import org.apache.fop.afp.util.ResourceAccessor;
  37. import org.apache.fop.apps.MimeConstants;
  38. import org.apache.fop.fonts.FontCollection;
  39. import org.apache.fop.fonts.FontEventAdapter;
  40. import org.apache.fop.fonts.FontInfo;
  41. import org.apache.fop.fonts.FontManager;
  42. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  43. import org.apache.fop.render.afp.extensions.AFPIncludeFormMap;
  44. import org.apache.fop.render.afp.extensions.AFPInvokeMediumMap;
  45. import org.apache.fop.render.afp.extensions.AFPPageOverlay;
  46. import org.apache.fop.render.afp.extensions.AFPPageSegmentElement;
  47. import org.apache.fop.render.afp.extensions.AFPPageSetup;
  48. import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
  49. import org.apache.fop.render.intermediate.IFDocumentHandler;
  50. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  51. import org.apache.fop.render.intermediate.IFException;
  52. import org.apache.fop.render.intermediate.IFPainter;
  53. /**
  54. * {@link IFDocumentHandler} implementation that produces AFP (MO:DCA).
  55. */
  56. public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
  57. implements AFPCustomizable {
  58. //** logging instance */
  59. //private static Log log = LogFactory.getLog(AFPDocumentHandler.class);
  60. /** the resource manager */
  61. private AFPResourceManager resourceManager;
  62. /** the painting state */
  63. private final AFPPaintingState paintingState;
  64. /** unit converter */
  65. private final AFPUnitConverter unitConv;
  66. /** the AFP datastream */
  67. private DataStream dataStream;
  68. /** the map of page segments */
  69. private Map/*<String,PageSegmentDescriptor>*/pageSegmentMap
  70. = new java.util.HashMap/*<String,PageSegmentDescriptor>*/();
  71. /** Medium Map referenced on previous page **/
  72. private String lastMediumMap;
  73. private static final int LOC_ELSEWHERE = 0;
  74. private static final int LOC_FOLLOWING_PAGE_SEQUENCE = 1;
  75. private static final int LOC_IN_PAGE_HEADER = 2;
  76. private int location = LOC_ELSEWHERE;
  77. /** the shading mode for filled rectangles */
  78. private AFPShadingMode shadingMode = AFPShadingMode.COLOR;
  79. /**
  80. * Default constructor.
  81. */
  82. public AFPDocumentHandler() {
  83. this.resourceManager = new AFPResourceManager();
  84. this.paintingState = new AFPPaintingState();
  85. this.unitConv = paintingState.getUnitConverter();
  86. }
  87. /** {@inheritDoc} */
  88. public boolean supportsPagesOutOfOrder() {
  89. return false;
  90. }
  91. /** {@inheritDoc} */
  92. public String getMimeType() {
  93. return MimeConstants.MIME_AFP;
  94. }
  95. /** {@inheritDoc} */
  96. public IFDocumentHandlerConfigurator getConfigurator() {
  97. return new AFPRendererConfigurator(getUserAgent());
  98. }
  99. /** {@inheritDoc} */
  100. public void setDefaultFontInfo(FontInfo fontInfo) {
  101. FontManager fontManager = getUserAgent().getFactory().getFontManager();
  102. FontCollection[] fontCollections = new FontCollection[] {
  103. new AFPFontCollection(getUserAgent().getEventBroadcaster(), null)
  104. };
  105. FontInfo fi = (fontInfo != null ? fontInfo : new FontInfo());
  106. fi.setEventListener(new FontEventAdapter(getUserAgent().getEventBroadcaster()));
  107. fontManager.setup(fi, fontCollections);
  108. setFontInfo(fi);
  109. }
  110. AFPPaintingState getPaintingState() {
  111. return this.paintingState;
  112. }
  113. DataStream getDataStream() {
  114. return this.dataStream;
  115. }
  116. AFPResourceManager getResourceManager() {
  117. return this.resourceManager;
  118. }
  119. AbstractAFPPainter createRectanglePainter() {
  120. if (AFPShadingMode.DITHERED.equals(this.shadingMode)) {
  121. return new AFPDitheredRectanglePainter(
  122. getPaintingState(), getDataStream(), getResourceManager());
  123. } else {
  124. return new AFPRectanglePainter(
  125. getPaintingState(), getDataStream());
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public void startDocument() throws IFException {
  130. super.startDocument();
  131. try {
  132. paintingState.setColor(Color.WHITE);
  133. this.dataStream = resourceManager.createDataStream(paintingState, outputStream);
  134. this.dataStream.startDocument();
  135. } catch (IOException e) {
  136. throw new IFException("I/O error in startDocument()", e);
  137. }
  138. }
  139. /** {@inheritDoc} */
  140. public void endDocumentHeader() throws IFException {
  141. }
  142. /** {@inheritDoc} */
  143. public void endDocument() throws IFException {
  144. try {
  145. this.dataStream.endDocument();
  146. this.dataStream = null;
  147. this.resourceManager.writeToStream();
  148. this.resourceManager = null;
  149. } catch (IOException ioe) {
  150. throw new IFException("I/O error in endDocument()", ioe);
  151. }
  152. super.endDocument();
  153. }
  154. /** {@inheritDoc} */
  155. public void startPageSequence(String id) throws IFException {
  156. try {
  157. dataStream.startPageGroup();
  158. } catch (IOException ioe) {
  159. throw new IFException("I/O error in startPageSequence()", ioe);
  160. }
  161. this.location = LOC_FOLLOWING_PAGE_SEQUENCE;
  162. }
  163. /** {@inheritDoc} */
  164. public void endPageSequence() throws IFException {
  165. //nop
  166. }
  167. /**
  168. * Returns the base AFP transform
  169. *
  170. * @return the base AFP transform
  171. */
  172. private AffineTransform getBaseTransform() {
  173. AffineTransform baseTransform = new AffineTransform();
  174. double scale = unitConv.mpt2units(1);
  175. baseTransform.scale(scale, scale);
  176. return baseTransform;
  177. }
  178. /** {@inheritDoc} */
  179. public void startPage(int index, String name, String pageMasterName, Dimension size)
  180. throws IFException {
  181. this.location = LOC_ELSEWHERE;
  182. paintingState.clear();
  183. AffineTransform baseTransform = getBaseTransform();
  184. paintingState.concatenate(baseTransform);
  185. int pageWidth = Math.round(unitConv.mpt2units(size.width));
  186. paintingState.setPageWidth(pageWidth);
  187. int pageHeight = Math.round(unitConv.mpt2units(size.height));
  188. paintingState.setPageHeight(pageHeight);
  189. int pageRotation = paintingState.getPageRotation();
  190. int resolution = paintingState.getResolution();
  191. dataStream.startPage(pageWidth, pageHeight, pageRotation,
  192. resolution, resolution);
  193. }
  194. /** {@inheritDoc} */
  195. public void startPageHeader() throws IFException {
  196. super.startPageHeader();
  197. this.location = LOC_IN_PAGE_HEADER;
  198. }
  199. /** {@inheritDoc} */
  200. public void endPageHeader() throws IFException {
  201. this.location = LOC_ELSEWHERE;
  202. super.endPageHeader();
  203. }
  204. /** {@inheritDoc} */
  205. public IFPainter startPageContent() throws IFException {
  206. return new AFPPainter(this);
  207. }
  208. /** {@inheritDoc} */
  209. public void endPageContent() throws IFException {
  210. }
  211. /** {@inheritDoc} */
  212. public void endPage() throws IFException {
  213. try {
  214. AFPPageFonts pageFonts = paintingState.getPageFonts();
  215. if (pageFonts != null && !pageFonts.isEmpty()) {
  216. dataStream.addFontsToCurrentPage(pageFonts);
  217. }
  218. dataStream.endPage();
  219. } catch (IOException ioe) {
  220. throw new IFException("I/O error in endPage()", ioe);
  221. }
  222. }
  223. /** {@inheritDoc} */
  224. public void handleExtensionObject(Object extension) throws IFException {
  225. if (extension instanceof AFPPageSetup) {
  226. AFPPageSetup aps = (AFPPageSetup)extension;
  227. String element = aps.getElementName();
  228. if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
  229. if (this.location != LOC_IN_PAGE_HEADER
  230. && this.location != LOC_FOLLOWING_PAGE_SEQUENCE) {
  231. throw new IFException(
  232. "TLE extension must be in the page header or between page-sequence"
  233. + " and the first page: " + aps, null);
  234. }
  235. String name = aps.getName();
  236. String value = aps.getValue();
  237. dataStream.createTagLogicalElement(name, value);
  238. } else {
  239. if (this.location != LOC_IN_PAGE_HEADER) {
  240. throw new IFException(
  241. "AFP page setup extension encountered outside the page header: " + aps,
  242. null);
  243. }
  244. if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
  245. AFPPageSegmentElement.AFPPageSegmentSetup apse
  246. = (AFPPageSegmentElement.AFPPageSegmentSetup)aps;
  247. String name = apse.getName();
  248. String source = apse.getValue();
  249. String uri = apse.getResourceSrc();
  250. pageSegmentMap.put(source, new PageSegmentDescriptor(name, uri));
  251. } else if (AFPElementMapping.NO_OPERATION.equals(element)) {
  252. String content = aps.getContent();
  253. if (content != null) {
  254. dataStream.createNoOperation(content);
  255. }
  256. }
  257. }
  258. } else if (extension instanceof AFPPageOverlay) {
  259. AFPPageOverlay ipo = (AFPPageOverlay)extension;
  260. if (this.location != LOC_IN_PAGE_HEADER) {
  261. throw new IFException(
  262. "AFP page overlay extension encountered outside the page header: " + ipo,
  263. null);
  264. }
  265. String overlay = ipo.getName();
  266. if (overlay != null) {
  267. dataStream.createIncludePageOverlay(overlay, ipo.getX(), ipo.getY());
  268. }
  269. } else if (extension instanceof AFPInvokeMediumMap) {
  270. if (this.location != LOC_FOLLOWING_PAGE_SEQUENCE
  271. && this.location != LOC_IN_PAGE_HEADER) {
  272. throw new IFException(
  273. "AFP IMM extension must be between page-sequence"
  274. + " and the first page or child of page-header: "
  275. + extension, null);
  276. }
  277. AFPInvokeMediumMap imm = (AFPInvokeMediumMap)extension;
  278. String mediumMap = imm.getName();
  279. if (mediumMap != null && !mediumMap.equals(lastMediumMap)) {
  280. dataStream.createInvokeMediumMap(mediumMap);
  281. lastMediumMap = mediumMap;
  282. }
  283. } else if (extension instanceof AFPIncludeFormMap) {
  284. AFPIncludeFormMap formMap = (AFPIncludeFormMap)extension;
  285. ResourceAccessor accessor = new DefaultFOPResourceAccessor(
  286. getUserAgent(), null, null);
  287. try {
  288. getResourceManager().createIncludedResource(formMap.getName(),
  289. formMap.getSrc(), accessor,
  290. ResourceObject.TYPE_FORMDEF);
  291. } catch (IOException ioe) {
  292. throw new IFException(
  293. "I/O error while embedding form map resource: " + formMap.getName(), ioe);
  294. }
  295. }
  296. }
  297. // ---=== AFPCustomizable ===---
  298. /** {@inheritDoc} */
  299. public void setBitsPerPixel(int bitsPerPixel) {
  300. paintingState.setBitsPerPixel(bitsPerPixel);
  301. }
  302. /** {@inheritDoc} */
  303. public void setColorImages(boolean colorImages) {
  304. paintingState.setColorImages(colorImages);
  305. }
  306. /** {@inheritDoc} */
  307. public void setNativeImagesSupported(boolean nativeImages) {
  308. paintingState.setNativeImagesSupported(nativeImages);
  309. }
  310. /** {@inheritDoc} */
  311. public void setCMYKImagesSupported(boolean value) {
  312. paintingState.setCMYKImagesSupported(value);
  313. }
  314. /** {@inheritDoc} */
  315. public void setDitheringQuality(float quality) {
  316. this.paintingState.setDitheringQuality(quality);
  317. }
  318. /** {@inheritDoc} */
  319. public void setShadingMode(AFPShadingMode shadingMode) {
  320. this.shadingMode = shadingMode;
  321. }
  322. /** {@inheritDoc} */
  323. public void setResolution(int resolution) {
  324. paintingState.setResolution(resolution);
  325. }
  326. /** {@inheritDoc} */
  327. public int getResolution() {
  328. return paintingState.getResolution();
  329. }
  330. /** {@inheritDoc} */
  331. public void setDefaultResourceGroupFilePath(String filePath) {
  332. resourceManager.setDefaultResourceGroupFilePath(filePath);
  333. }
  334. /** {@inheritDoc} */
  335. public void setResourceLevelDefaults(AFPResourceLevelDefaults defaults) {
  336. resourceManager.setResourceLevelDefaults(defaults);
  337. }
  338. /**
  339. * Returns the page segment descriptor for a given URI if it actually represents a page segment.
  340. * Otherwise, it just returns null.
  341. * @param uri the URI that identifies the page segment
  342. * @return the page segment descriptor or null if there's no page segment for the given URI
  343. */
  344. PageSegmentDescriptor getPageSegmentNameFor(String uri) {
  345. return (PageSegmentDescriptor)pageSegmentMap.get(uri);
  346. }
  347. }