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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. super.startDocument();
  109. try {
  110. paintingState.setColor(Color.WHITE);
  111. this.dataStream = resourceManager.createDataStream(paintingState, outputStream);
  112. this.dataStream.startDocument();
  113. } catch (IOException e) {
  114. throw new IFException("I/O error in startDocument()", e);
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. public void endDocumentHeader() throws IFException {
  119. }
  120. /** {@inheritDoc} */
  121. public void endDocument() throws IFException {
  122. try {
  123. this.dataStream.endDocument();
  124. this.dataStream = null;
  125. this.resourceManager.writeToStream();
  126. this.resourceManager = null;
  127. } catch (IOException ioe) {
  128. throw new IFException("I/O error in endDocument()", ioe);
  129. }
  130. super.endDocument();
  131. }
  132. /** {@inheritDoc} */
  133. public void startPageSequence(String id) throws IFException {
  134. try {
  135. dataStream.startPageGroup();
  136. } catch (IOException ioe) {
  137. throw new IFException("I/O error in startPageSequence()", ioe);
  138. }
  139. }
  140. /** {@inheritDoc} */
  141. public void endPageSequence() throws IFException {
  142. //nop
  143. }
  144. /**
  145. * Returns the base AFP transform
  146. *
  147. * @return the base AFP transform
  148. */
  149. private AffineTransform getBaseTransform() {
  150. AffineTransform baseTransform = new AffineTransform();
  151. double scale = unitConv.mpt2units(1);
  152. baseTransform.scale(scale, scale);
  153. return baseTransform;
  154. }
  155. /** {@inheritDoc} */
  156. public void startPage(int index, String name, String pageMasterName, Dimension size)
  157. throws IFException {
  158. paintingState.clear();
  159. pageSegmentMap.clear();
  160. AffineTransform baseTransform = getBaseTransform();
  161. paintingState.concatenate(baseTransform);
  162. int pageWidth = Math.round(unitConv.mpt2units(size.width));
  163. paintingState.setPageWidth(pageWidth);
  164. int pageHeight = Math.round(unitConv.mpt2units(size.height));
  165. paintingState.setPageHeight(pageHeight);
  166. int pageRotation = paintingState.getPageRotation();
  167. int resolution = paintingState.getResolution();
  168. dataStream.startPage(pageWidth, pageHeight, pageRotation,
  169. resolution, resolution);
  170. }
  171. /** {@inheritDoc} */
  172. public void startPageHeader() throws IFException {
  173. super.startPageHeader();
  174. this.inPageHeader = true;
  175. }
  176. /** {@inheritDoc} */
  177. public void endPageHeader() throws IFException {
  178. this.inPageHeader = false;
  179. super.endPageHeader();
  180. }
  181. /** {@inheritDoc} */
  182. public IFPainter startPageContent() throws IFException {
  183. return new AFPPainter(this);
  184. }
  185. /** {@inheritDoc} */
  186. public void endPageContent() throws IFException {
  187. }
  188. /** {@inheritDoc} */
  189. public void endPage() throws IFException {
  190. try {
  191. AFPPageFonts pageFonts = paintingState.getPageFonts();
  192. if (pageFonts != null && !pageFonts.isEmpty()) {
  193. dataStream.addFontsToCurrentPage(pageFonts);
  194. }
  195. dataStream.endPage();
  196. } catch (IOException ioe) {
  197. throw new IFException("I/O error in endPage()", ioe);
  198. }
  199. }
  200. /** {@inheritDoc} */
  201. public void handleExtensionObject(Object extension) throws IFException {
  202. if (extension instanceof AFPPageSetup) {
  203. AFPPageSetup aps = (AFPPageSetup)extension;
  204. if (!inPageHeader) {
  205. throw new IFException(
  206. "AFP page setup extension encountered outside the page header: " + aps, null);
  207. }
  208. String element = aps.getElementName();
  209. if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(element)) {
  210. String overlay = aps.getName();
  211. if (overlay != null) {
  212. dataStream.createIncludePageOverlay(overlay);
  213. }
  214. } else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
  215. String name = aps.getName();
  216. String source = aps.getValue();
  217. pageSegmentMap.put(source, name);
  218. } else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
  219. String name = aps.getName();
  220. String value = aps.getValue();
  221. dataStream.createTagLogicalElement(name, value);
  222. } else if (AFPElementMapping.NO_OPERATION.equals(element)) {
  223. String content = aps.getContent();
  224. if (content != null) {
  225. dataStream.createNoOperation(content);
  226. }
  227. }
  228. }
  229. }
  230. // ---=== AFPCustomizable ===---
  231. /** {@inheritDoc} */
  232. public void setBitsPerPixel(int bitsPerPixel) {
  233. paintingState.setBitsPerPixel(bitsPerPixel);
  234. }
  235. /** {@inheritDoc} */
  236. public void setColorImages(boolean colorImages) {
  237. paintingState.setColorImages(colorImages);
  238. }
  239. /** {@inheritDoc} */
  240. public void setNativeImagesSupported(boolean nativeImages) {
  241. paintingState.setNativeImagesSupported(nativeImages);
  242. }
  243. /** {@inheritDoc} */
  244. public void setResolution(int resolution) {
  245. paintingState.setResolution(resolution);
  246. }
  247. /** {@inheritDoc} */
  248. public int getResolution() {
  249. return paintingState.getResolution();
  250. }
  251. /** {@inheritDoc} */
  252. public void setDefaultResourceGroupFilePath(String filePath) {
  253. resourceManager.setDefaultResourceGroupFilePath(filePath);
  254. }
  255. /** {@inheritDoc} */
  256. public void setResourceLevelDefaults(AFPResourceLevelDefaults defaults) {
  257. resourceManager.setResourceLevelDefaults(defaults);
  258. }
  259. /**
  260. * Returns the page segment name for a given URI if it actually represents a page segment.
  261. * Otherwise, it just returns null.
  262. * @param uri the URI that identifies the page segment
  263. * @return the page segment name or null if there's no page segment for the given URI
  264. */
  265. String getPageSegmentNameFor(String uri) {
  266. return (String)pageSegmentMap.get(uri);
  267. }
  268. }