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

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