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

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