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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.AFPPaintingState;
  25. import org.apache.fop.afp.AFPResourceLevelDefaults;
  26. import org.apache.fop.afp.AFPResourceManager;
  27. import org.apache.fop.afp.AFPUnitConverter;
  28. import org.apache.fop.afp.DataStream;
  29. import org.apache.fop.afp.fonts.AFPFontCollection;
  30. import org.apache.fop.afp.fonts.AFPPageFonts;
  31. import org.apache.fop.apps.MimeConstants;
  32. import org.apache.fop.fonts.FontCollection;
  33. import org.apache.fop.fonts.FontEventAdapter;
  34. import org.apache.fop.fonts.FontInfo;
  35. import org.apache.fop.fonts.FontManager;
  36. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  37. import org.apache.fop.render.afp.extensions.AFPPageSetup;
  38. import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
  39. import org.apache.fop.render.intermediate.IFContext;
  40. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  41. import org.apache.fop.render.intermediate.IFException;
  42. import org.apache.fop.render.intermediate.IFPainter;
  43. /**
  44. * {@code IFDocumentHandler} implementation that produces AFP (MO:DCA).
  45. */
  46. public class AFPDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
  47. implements AFPCustomizable {
  48. //** logging instance */
  49. //private static Log log = LogFactory.getLog(AFPDocumentHandler.class);
  50. /** the resource manager */
  51. private AFPResourceManager resourceManager;
  52. /** the painting state */
  53. private final AFPPaintingState paintingState;
  54. /** unit converter */
  55. private final AFPUnitConverter unitConv;
  56. /** the AFP datastream */
  57. private DataStream dataStream;
  58. /** the map of page segments */
  59. private Map/*<String,String>*/pageSegmentMap
  60. = new java.util.HashMap/*<String,String>*/();
  61. private boolean inPageHeader;
  62. /**
  63. * Default constructor.
  64. */
  65. public AFPDocumentHandler() {
  66. this.resourceManager = new AFPResourceManager();
  67. this.paintingState = new AFPPaintingState();
  68. this.unitConv = paintingState.getUnitConverter();
  69. }
  70. /** {@inheritDoc} */
  71. public boolean supportsPagesOutOfOrder() {
  72. return false;
  73. }
  74. /** {@inheritDoc} */
  75. public String getMimeType() {
  76. return MimeConstants.MIME_AFP;
  77. }
  78. /** {@inheritDoc} */
  79. public void setContext(IFContext context) {
  80. super.setContext(context);
  81. }
  82. /** {@inheritDoc} */
  83. public IFDocumentHandlerConfigurator getConfigurator() {
  84. return new AFPRendererConfigurator(getUserAgent());
  85. }
  86. /** {@inheritDoc} */
  87. public void setDefaultFontInfo(FontInfo fontInfo) {
  88. FontManager fontManager = getUserAgent().getFactory().getFontManager();
  89. FontCollection[] fontCollections = new FontCollection[] {
  90. new AFPFontCollection(getUserAgent().getEventBroadcaster(), null)
  91. };
  92. FontInfo fi = (fontInfo != null ? fontInfo : new FontInfo());
  93. fi.setEventListener(new FontEventAdapter(getUserAgent().getEventBroadcaster()));
  94. fontManager.setup(fi, fontCollections);
  95. setFontInfo(fi);
  96. }
  97. AFPPaintingState getPaintingState() {
  98. return this.paintingState;
  99. }
  100. DataStream getDataStream() {
  101. return this.dataStream;
  102. }
  103. AFPResourceManager getResourceManager() {
  104. return this.resourceManager;
  105. }
  106. /** {@inheritDoc} */
  107. public void startDocument() throws IFException {
  108. try {
  109. if (getUserAgent() == null) {
  110. throw new IllegalStateException(
  111. "User agent must be set before starting PostScript generation");
  112. }
  113. if (this.outputStream == null) {
  114. throw new IllegalStateException("OutputStream hasn't been set through setResult()");
  115. }
  116. paintingState.setColor(Color.WHITE);
  117. this.dataStream = resourceManager.createDataStream(paintingState, outputStream);
  118. this.dataStream.startDocument();
  119. } catch (IOException e) {
  120. throw new IFException("I/O error in startDocument()", e);
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. public void endDocumentHeader() throws IFException {
  125. }
  126. /** {@inheritDoc} */
  127. public void endDocument() throws IFException {
  128. try {
  129. this.dataStream.endDocument();
  130. this.dataStream = null;
  131. this.resourceManager.writeToStream();
  132. this.resourceManager = null;
  133. } catch (IOException ioe) {
  134. throw new IFException("I/O error in endDocument()", ioe);
  135. }
  136. super.endDocument();
  137. }
  138. /** {@inheritDoc} */
  139. public void startPageSequence(String id) throws IFException {
  140. try {
  141. dataStream.startPageGroup();
  142. } catch (IOException ioe) {
  143. throw new IFException("I/O error in startPageSequence()", ioe);
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. public void endPageSequence() throws IFException {
  148. //nop
  149. }
  150. /**
  151. * Returns the base AFP transform
  152. *
  153. * @return the base AFP transform
  154. */
  155. private AffineTransform getBaseTransform() {
  156. AffineTransform baseTransform = new AffineTransform();
  157. double scale = unitConv.mpt2units(1);
  158. baseTransform.scale(scale, scale);
  159. return baseTransform;
  160. }
  161. /** {@inheritDoc} */
  162. public void startPage(int index, String name, String pageMasterName, Dimension size)
  163. throws IFException {
  164. paintingState.clear();
  165. pageSegmentMap.clear();
  166. AffineTransform baseTransform = getBaseTransform();
  167. paintingState.concatenate(baseTransform);
  168. int pageWidth = Math.round(unitConv.mpt2units(size.width));
  169. paintingState.setPageWidth(pageWidth);
  170. int pageHeight = Math.round(unitConv.mpt2units(size.height));
  171. paintingState.setPageHeight(pageHeight);
  172. int pageRotation = paintingState.getPageRotation();
  173. int resolution = paintingState.getResolution();
  174. dataStream.startPage(pageWidth, pageHeight, pageRotation,
  175. resolution, resolution);
  176. }
  177. /** {@inheritDoc} */
  178. public void startPageHeader() throws IFException {
  179. super.startPageHeader();
  180. this.inPageHeader = true;
  181. }
  182. /** {@inheritDoc} */
  183. public void endPageHeader() throws IFException {
  184. this.inPageHeader = false;
  185. super.endPageHeader();
  186. }
  187. /** {@inheritDoc} */
  188. public IFPainter startPageContent() throws IFException {
  189. return new AFPPainter(this);
  190. }
  191. /** {@inheritDoc} */
  192. public void endPageContent() throws IFException {
  193. }
  194. /** {@inheritDoc} */
  195. public void endPage() throws IFException {
  196. try {
  197. AFPPageFonts pageFonts = paintingState.getPageFonts();
  198. if (pageFonts != null && !pageFonts.isEmpty()) {
  199. dataStream.addFontsToCurrentPage(pageFonts);
  200. }
  201. dataStream.endPage();
  202. } catch (IOException ioe) {
  203. throw new IFException("I/O error in endPage()", ioe);
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. public void handleExtensionObject(Object extension) throws IFException {
  208. if (extension instanceof AFPPageSetup) {
  209. AFPPageSetup aps = (AFPPageSetup)extension;
  210. if (!inPageHeader) {
  211. throw new IFException(
  212. "AFP page setup extension encountered outside the page header: " + aps, null);
  213. }
  214. String element = aps.getElementName();
  215. if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(element)) {
  216. String overlay = aps.getName();
  217. if (overlay != null) {
  218. dataStream.createIncludePageOverlay(overlay);
  219. }
  220. } else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
  221. String name = aps.getName();
  222. String source = aps.getValue();
  223. pageSegmentMap.put(source, name);
  224. } else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
  225. String name = aps.getName();
  226. String value = aps.getValue();
  227. dataStream.createTagLogicalElement(name, value);
  228. } else if (AFPElementMapping.NO_OPERATION.equals(element)) {
  229. String content = aps.getContent();
  230. if (content != null) {
  231. dataStream.createNoOperation(content);
  232. }
  233. }
  234. }
  235. }
  236. // ---=== AFPCustomizable ===---
  237. /** {@inheritDoc} */
  238. public void setBitsPerPixel(int bitsPerPixel) {
  239. paintingState.setBitsPerPixel(bitsPerPixel);
  240. }
  241. /** {@inheritDoc} */
  242. public void setColorImages(boolean colorImages) {
  243. paintingState.setColorImages(colorImages);
  244. }
  245. /** {@inheritDoc} */
  246. public void setNativeImagesSupported(boolean nativeImages) {
  247. paintingState.setNativeImagesSupported(nativeImages);
  248. }
  249. /** {@inheritDoc} */
  250. public void setResolution(int resolution) {
  251. paintingState.setResolution(resolution);
  252. }
  253. /** {@inheritDoc} */
  254. public int getResolution() {
  255. return paintingState.getResolution();
  256. }
  257. /** {@inheritDoc} */
  258. public void setDefaultResourceGroupFilePath(String filePath) {
  259. resourceManager.setDefaultResourceGroupFilePath(filePath);
  260. }
  261. /** {@inheritDoc} */
  262. public void setResourceLevelDefaults(AFPResourceLevelDefaults defaults) {
  263. resourceManager.setResourceLevelDefaults(defaults);
  264. }
  265. /**
  266. * Returns the page segment name for a given URI if it actually represents a page segment.
  267. * Otherwise, it just returns null.
  268. * @param uri the URI that identifies the page segment
  269. * @return the page segment name or null if there's no page segment for the given URI
  270. */
  271. String getPageSegmentNameFor(String uri) {
  272. return (String)pageSegmentMap.get(uri);
  273. }
  274. }