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

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