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

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