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

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