您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IFRenderer.java 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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.intermediate;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.geom.AffineTransform;
  24. import java.awt.geom.Rectangle2D;
  25. import java.io.IOException;
  26. import java.io.OutputStream;
  27. import java.util.Arrays;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Locale;
  31. import java.util.Map;
  32. import java.util.Stack;
  33. import javax.xml.transform.stream.StreamResult;
  34. import org.w3c.dom.Document;
  35. import org.xml.sax.SAXException;
  36. import org.apache.batik.parser.AWTTransformProducer;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.commons.logging.LogFactory;
  39. import org.apache.xmlgraphics.xmp.Metadata;
  40. import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
  41. import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
  42. import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
  43. import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
  44. import org.apache.fop.Version;
  45. import org.apache.fop.accessibility.StructureTreeElement;
  46. import org.apache.fop.apps.FOPException;
  47. import org.apache.fop.apps.FOUserAgent;
  48. import org.apache.fop.apps.MimeConstants;
  49. import org.apache.fop.area.Area;
  50. import org.apache.fop.area.AreaTreeObject;
  51. import org.apache.fop.area.Block;
  52. import org.apache.fop.area.BlockViewport;
  53. import org.apache.fop.area.BookmarkData;
  54. import org.apache.fop.area.CTM;
  55. import org.apache.fop.area.DestinationData;
  56. import org.apache.fop.area.OffDocumentExtensionAttachment;
  57. import org.apache.fop.area.OffDocumentItem;
  58. import org.apache.fop.area.PageSequence;
  59. import org.apache.fop.area.PageViewport;
  60. import org.apache.fop.area.RegionViewport;
  61. import org.apache.fop.area.Trait;
  62. import org.apache.fop.area.inline.AbstractTextArea;
  63. import org.apache.fop.area.inline.ForeignObject;
  64. import org.apache.fop.area.inline.Image;
  65. import org.apache.fop.area.inline.InlineArea;
  66. import org.apache.fop.area.inline.InlineParent;
  67. import org.apache.fop.area.inline.InlineViewport;
  68. import org.apache.fop.area.inline.Leader;
  69. import org.apache.fop.area.inline.SpaceArea;
  70. import org.apache.fop.area.inline.TextArea;
  71. import org.apache.fop.area.inline.WordArea;
  72. import org.apache.fop.datatypes.URISpecification;
  73. import org.apache.fop.fo.extensions.ExtensionAttachment;
  74. import org.apache.fop.fo.extensions.xmp.XMPMetadata;
  75. import org.apache.fop.fonts.Font;
  76. import org.apache.fop.fonts.FontInfo;
  77. import org.apache.fop.fonts.FontTriplet;
  78. import org.apache.fop.fonts.LazyFont;
  79. import org.apache.fop.fonts.Typeface;
  80. import org.apache.fop.render.AbstractPathOrientedRenderer;
  81. import org.apache.fop.render.Renderer;
  82. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  83. import org.apache.fop.render.intermediate.extensions.ActionSet;
  84. import org.apache.fop.render.intermediate.extensions.Bookmark;
  85. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  86. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  87. import org.apache.fop.render.intermediate.extensions.Link;
  88. import org.apache.fop.render.intermediate.extensions.NamedDestination;
  89. import org.apache.fop.render.intermediate.extensions.URIAction;
  90. import org.apache.fop.render.pdf.PDFEventProducer;
  91. import org.apache.fop.traits.BorderProps;
  92. import org.apache.fop.traits.RuleStyle;
  93. /**
  94. * This renderer implementation is an adapter to the {@link IFPainter} interface. It is used
  95. * to generate content using FOP's intermediate format.
  96. */
  97. public class IFRenderer extends AbstractPathOrientedRenderer {
  98. //TODO Many parts of the Renderer infrastructure are using floats (coordinates in points)
  99. //instead of ints (in millipoints). A lot of conversion to and from is performed.
  100. //When the new IF is established, the Renderer infrastructure should be revisited so check
  101. //if optimizations can be done to avoid int->float->int conversions.
  102. /** logging instance */
  103. protected static final Log log = LogFactory.getLog(IFRenderer.class);
  104. /** XML MIME type */
  105. public static final String IF_MIME_TYPE = MimeConstants.MIME_FOP_IF;
  106. private IFDocumentHandler documentHandler;
  107. private IFPainter painter;
  108. /** If not null, the XMLRenderer will mimic another renderer by using its font setup. */
  109. protected Renderer mimic;
  110. private boolean inPageSequence = false;
  111. private Stack graphicContextStack = new Stack();
  112. private Stack viewportDimensionStack = new Stack();
  113. private IFGraphicContext graphicContext = new IFGraphicContext();
  114. //private Stack groupStack = new Stack();
  115. private Metadata documentMetadata;
  116. /**
  117. * Maps XSL-FO element IDs to their on-page XY-positions
  118. * Must be used in conjunction with the page reference to fully specify the details
  119. * of a "go-to" action.
  120. */
  121. private Map idPositions = new java.util.HashMap();
  122. /**
  123. * The "go-to" actions in idGoTos that are not complete yet
  124. */
  125. private List unfinishedGoTos = new java.util.ArrayList();
  126. // can't use a Set because PDFGoTo.equals returns true if the target is the same,
  127. // even if the object number differs
  128. /** Maps unique PageViewport key to page indices (for link target handling) */
  129. protected Map pageIndices = new java.util.HashMap();
  130. private BookmarkTree bookmarkTree;
  131. private List deferredDestinations = new java.util.ArrayList();
  132. private List deferredLinks = new java.util.ArrayList();
  133. private ActionSet actionSet = new ActionSet();
  134. private TextUtil textUtil = new TextUtil();
  135. private Stack<String> ids = new Stack<String>();
  136. /**
  137. * Main constructor
  138. */
  139. public IFRenderer() {
  140. }
  141. /** {@inheritDoc} */
  142. public String getMimeType() {
  143. return IF_MIME_TYPE;
  144. }
  145. /**
  146. * Sets the {@link IFDocumentHandler} to be used by the {@link IFRenderer}.
  147. * @param documentHandler the {@link IFDocumentHandler}
  148. */
  149. public void setDocumentHandler(IFDocumentHandler documentHandler) {
  150. this.documentHandler = documentHandler;
  151. }
  152. /** {@inheritDoc} */
  153. public void setupFontInfo(FontInfo inFontInfo) throws FOPException {
  154. if (this.documentHandler == null) {
  155. this.documentHandler = createDefaultDocumentHandler();
  156. }
  157. IFUtil.setupFonts(this.documentHandler, inFontInfo);
  158. this.fontInfo = inFontInfo;
  159. }
  160. private void handleIFException(IFException ife) {
  161. if (ife.getCause() instanceof SAXException) {
  162. throw new RuntimeException(ife.getCause());
  163. } else {
  164. throw new RuntimeException(ife);
  165. }
  166. }
  167. private void handleIFExceptionWithIOException(IFException ife) throws IOException {
  168. if (ife.getCause() instanceof IOException) {
  169. throw (IOException)ife.getCause();
  170. } else {
  171. handleIFException(ife);
  172. }
  173. }
  174. /** {@inheritDoc} */
  175. public boolean supportsOutOfOrder() {
  176. return (this.documentHandler != null
  177. ? this.documentHandler.supportsPagesOutOfOrder() : false);
  178. }
  179. /**
  180. * Returns the document navigation handler if available/supported.
  181. * @return the document navigation handler or null if not supported
  182. */
  183. protected IFDocumentNavigationHandler getDocumentNavigationHandler() {
  184. return this.documentHandler.getDocumentNavigationHandler();
  185. }
  186. /**
  187. * Indicates whether document navigation features are supported by the document handler.
  188. * @return true if document navigation features are available
  189. */
  190. protected boolean hasDocumentNavigation() {
  191. return getDocumentNavigationHandler() != null;
  192. }
  193. /**
  194. * Creates a default {@link IFDocumentHandler} when none has been set.
  195. * @return the default IFDocumentHandler
  196. */
  197. protected IFDocumentHandler createDefaultDocumentHandler() {
  198. IFSerializer serializer = new IFSerializer();
  199. FOUserAgent userAgent = getUserAgent();
  200. serializer.setContext(new IFContext(userAgent));
  201. if (userAgent.isAccessibilityEnabled()) {
  202. userAgent.setStructureTreeEventHandler(serializer.getStructureTreeEventHandler());
  203. }
  204. return serializer;
  205. }
  206. /** {@inheritDoc} */
  207. public void startRenderer(OutputStream outputStream)
  208. throws IOException {
  209. try {
  210. if (outputStream != null) {
  211. StreamResult result = new StreamResult(outputStream);
  212. if (getUserAgent().getOutputFile() != null) {
  213. result.setSystemId(
  214. getUserAgent().getOutputFile().toURI().toURL().toExternalForm());
  215. }
  216. if (this.documentHandler == null) {
  217. this.documentHandler = createDefaultDocumentHandler();
  218. }
  219. this.documentHandler.setResult(result);
  220. }
  221. super.startRenderer(null);
  222. if (log.isDebugEnabled()) {
  223. log.debug("Rendering areas via IF document handler ("
  224. + this.documentHandler.getClass().getName() + ")...");
  225. }
  226. documentHandler.startDocument();
  227. documentHandler.startDocumentHeader();
  228. } catch (IFException e) {
  229. handleIFExceptionWithIOException(e);
  230. }
  231. }
  232. /** {@inheritDoc} */
  233. public void stopRenderer() throws IOException {
  234. try {
  235. if (this.inPageSequence) {
  236. documentHandler.endPageSequence();
  237. this.inPageSequence = false;
  238. }
  239. documentHandler.startDocumentTrailer();
  240. //Wrap up document navigation
  241. if (hasDocumentNavigation()) {
  242. finishOpenGoTos();
  243. Iterator iter = this.deferredDestinations.iterator();
  244. while (iter.hasNext()) {
  245. NamedDestination dest = (NamedDestination)iter.next();
  246. iter.remove();
  247. getDocumentNavigationHandler().renderNamedDestination(dest);
  248. }
  249. if (this.bookmarkTree != null) {
  250. getDocumentNavigationHandler().renderBookmarkTree(this.bookmarkTree);
  251. }
  252. }
  253. documentHandler.endDocumentTrailer();
  254. documentHandler.endDocument();
  255. } catch (IFException e) {
  256. handleIFExceptionWithIOException(e);
  257. }
  258. pageIndices.clear();
  259. idPositions.clear();
  260. actionSet.clear();
  261. super.stopRenderer();
  262. log.debug("Rendering finished.");
  263. }
  264. @Override
  265. public void setDocumentLocale(Locale locale) {
  266. documentHandler.setDocumentLocale(locale);
  267. }
  268. /** {@inheritDoc} */
  269. public void processOffDocumentItem(OffDocumentItem odi) {
  270. if (odi instanceof DestinationData) {
  271. // render Destinations
  272. renderDestination((DestinationData) odi);
  273. } else if (odi instanceof BookmarkData) {
  274. // render Bookmark-Tree
  275. renderBookmarkTree((BookmarkData) odi);
  276. } else if (odi instanceof OffDocumentExtensionAttachment) {
  277. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)odi).getAttachment();
  278. if (XMPMetadata.CATEGORY.equals(attachment.getCategory())) {
  279. renderXMPMetadata((XMPMetadata)attachment);
  280. } else {
  281. try {
  282. this.documentHandler.handleExtensionObject(attachment);
  283. } catch (IFException ife) {
  284. handleIFException(ife);
  285. }
  286. }
  287. }
  288. }
  289. private void renderDestination(DestinationData dd) {
  290. if (!hasDocumentNavigation()) {
  291. return;
  292. }
  293. String targetID = dd.getIDRef();
  294. if (targetID == null || targetID.length() == 0) {
  295. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  296. }
  297. PageViewport pv = dd.getPageViewport();
  298. if (pv != null) {
  299. GoToXYAction action = getGoToActionForID(targetID, pv.getPageIndex());
  300. NamedDestination namedDestination = new NamedDestination(targetID, action);
  301. this.deferredDestinations.add(namedDestination);
  302. } else {
  303. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  304. log.debug("Unresolved destination item received: " + dd.getIDRef());
  305. }
  306. }
  307. /**
  308. * Renders a Bookmark-Tree object
  309. * @param bookmarks the BookmarkData object containing all the Bookmark-Items
  310. */
  311. protected void renderBookmarkTree(BookmarkData bookmarks) {
  312. assert this.bookmarkTree == null;
  313. if (!hasDocumentNavigation()) {
  314. return;
  315. }
  316. this.bookmarkTree = new BookmarkTree();
  317. for (int i = 0; i < bookmarks.getCount(); i++) {
  318. BookmarkData ext = bookmarks.getSubData(i);
  319. Bookmark b = renderBookmarkItem(ext);
  320. bookmarkTree.addBookmark(b);
  321. }
  322. }
  323. private Bookmark renderBookmarkItem(BookmarkData bookmarkItem) {
  324. String targetID = bookmarkItem.getIDRef();
  325. if (targetID == null || targetID.length() == 0) {
  326. throw new IllegalArgumentException("DestinationData must contain a ID reference");
  327. }
  328. GoToXYAction action = null;
  329. PageViewport pv = bookmarkItem.getPageViewport();
  330. if (pv != null) {
  331. action = getGoToActionForID(targetID, pv.getPageIndex());
  332. } else {
  333. //Warning already issued by AreaTreeHandler (debug level is sufficient)
  334. log.debug("Bookmark with IDRef \"" + targetID + "\" has a null PageViewport.");
  335. }
  336. Bookmark b = new Bookmark(
  337. bookmarkItem.getBookmarkTitle(),
  338. bookmarkItem.showChildItems(),
  339. action);
  340. for (int i = 0; i < bookmarkItem.getCount(); i++) {
  341. b.addChildBookmark(renderBookmarkItem(bookmarkItem.getSubData(i)));
  342. }
  343. return b;
  344. }
  345. private void renderXMPMetadata(XMPMetadata metadata) {
  346. this.documentMetadata = metadata.getMetadata();
  347. }
  348. private GoToXYAction getGoToActionForID(String targetID, int pageIndex) {
  349. // Already a GoToXY present for this target? If not, create.
  350. GoToXYAction action = (GoToXYAction)actionSet.get(targetID);
  351. //GoToXYAction action = (GoToXYAction)idGoTos.get(targetID);
  352. if (action == null) {
  353. if (pageIndex < 0) {
  354. //pageIndex = page
  355. }
  356. Point position = (Point)idPositions.get(targetID);
  357. // can the GoTo already be fully filled in?
  358. if (pageIndex >= 0 && position != null) {
  359. action = new GoToXYAction(targetID, pageIndex, position);
  360. } else {
  361. // Not complete yet, can't use getPDFGoTo:
  362. action = new GoToXYAction(targetID, pageIndex, null);
  363. unfinishedGoTos.add(action);
  364. }
  365. action = (GoToXYAction)actionSet.put(action);
  366. //idGoTos.put(targetID, action);
  367. }
  368. return action;
  369. }
  370. private void finishOpenGoTos() {
  371. int count = unfinishedGoTos.size();
  372. if (count > 0) {
  373. Point defaultPos = new Point(0, 0); // top-o-page
  374. while (!unfinishedGoTos.isEmpty()) {
  375. GoToXYAction action = (GoToXYAction)unfinishedGoTos.get(0);
  376. noteGoToPosition(action, defaultPos);
  377. }
  378. PDFEventProducer eventProducer = PDFEventProducer.Provider.get(
  379. getUserAgent().getEventBroadcaster());
  380. eventProducer.nonFullyResolvedLinkTargets(this, count);
  381. // dysfunctional if pageref is null
  382. }
  383. }
  384. private void noteGoToPosition(GoToXYAction action, Point position) {
  385. action.setTargetLocation(position);
  386. try {
  387. getDocumentNavigationHandler().addResolvedAction(action);
  388. } catch (IFException ife) {
  389. handleIFException(ife);
  390. }
  391. unfinishedGoTos.remove(action);
  392. }
  393. private void noteGoToPosition(GoToXYAction action, PageViewport pv, Point position) {
  394. action.setPageIndex(pv.getPageIndex());
  395. noteGoToPosition(action, position);
  396. }
  397. private void saveAbsolutePosition(String id, PageViewport pv,
  398. int relativeIPP, int relativeBPP, AffineTransform tf) {
  399. Point position = new Point(relativeIPP, relativeBPP);
  400. tf.transform(position, position);
  401. idPositions.put(id, position);
  402. // is there already a GoTo action waiting to be completed?
  403. GoToXYAction action = (GoToXYAction)actionSet.get(id);
  404. if (action != null) {
  405. noteGoToPosition(action, pv, position);
  406. }
  407. }
  408. private void saveAbsolutePosition(String id, int relativeIPP, int relativeBPP) {
  409. saveAbsolutePosition(id, this.currentPageViewport,
  410. relativeIPP, relativeBPP, graphicContext.getTransform());
  411. }
  412. private void saveBlockPosIfTargetable(Block block) {
  413. String id = getTargetableID(block);
  414. if (hasDocumentNavigation() && id != null) {
  415. // FIXME: Like elsewhere in the renderer code, absolute and relative
  416. // directions are happily mixed here. This makes sure that the
  417. // links point to the right location, but it is not correct.
  418. int ipp = block.getXOffset();
  419. int bpp = block.getYOffset() + block.getSpaceBefore();
  420. int positioning = block.getPositioning();
  421. if (!(positioning == Block.FIXED || positioning == Block.ABSOLUTE)) {
  422. ipp += currentIPPosition;
  423. bpp += currentBPPosition;
  424. }
  425. saveAbsolutePosition(id, currentPageViewport, ipp, bpp, graphicContext.getTransform());
  426. }
  427. }
  428. private void saveInlinePosIfTargetable(InlineArea inlineArea) {
  429. String id = getTargetableID(inlineArea);
  430. if (hasDocumentNavigation() && id != null) {
  431. int extraMarginBefore = 5000; // millipoints
  432. int ipp = currentIPPosition;
  433. int bpp = currentBPPosition + inlineArea.getOffset() - extraMarginBefore;
  434. saveAbsolutePosition(id, ipp, bpp);
  435. }
  436. }
  437. private String getTargetableID(Area area) {
  438. String id = (String) area.getTrait(Trait.PROD_ID);
  439. if (id == null || id.length() == 0
  440. || !currentPageViewport.isFirstWithID(id)
  441. || idPositions.containsKey(id)) {
  442. return null;
  443. } else {
  444. return id;
  445. }
  446. }
  447. /** {@inheritDoc} */
  448. public void startPageSequence(PageSequence pageSequence) {
  449. try {
  450. if (this.inPageSequence) {
  451. documentHandler.endPageSequence();
  452. documentHandler.getContext().setLanguage(null);
  453. } else {
  454. if (this.documentMetadata == null) {
  455. this.documentMetadata = createDefaultDocumentMetadata();
  456. }
  457. documentHandler.handleExtensionObject(this.documentMetadata);
  458. documentHandler.endDocumentHeader();
  459. this.inPageSequence = true;
  460. }
  461. establishForeignAttributes(pageSequence.getForeignAttributes());
  462. documentHandler.getContext().setLanguage(toLocale(pageSequence));
  463. documentHandler.startPageSequence(null);
  464. resetForeignAttributes();
  465. processExtensionAttachments(pageSequence);
  466. } catch (IFException e) {
  467. handleIFException(e);
  468. }
  469. }
  470. private Locale toLocale(PageSequence pageSequence) {
  471. if (pageSequence.getLanguage() != null) {
  472. if (pageSequence.getCountry() != null) {
  473. return new Locale(pageSequence.getLanguage(), pageSequence.getCountry());
  474. } else {
  475. return new Locale(pageSequence.getLanguage());
  476. }
  477. }
  478. return null;
  479. }
  480. private Metadata createDefaultDocumentMetadata() {
  481. Metadata xmp = new Metadata();
  482. DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp);
  483. if (getUserAgent().getTitle() != null) {
  484. dc.setTitle(getUserAgent().getTitle());
  485. }
  486. if (getUserAgent().getAuthor() != null) {
  487. dc.addCreator(getUserAgent().getAuthor());
  488. }
  489. if (getUserAgent().getKeywords() != null) {
  490. dc.addSubject(getUserAgent().getKeywords());
  491. }
  492. XMPBasicAdapter xmpBasic = XMPBasicSchema.getAdapter(xmp);
  493. if (getUserAgent().getProducer() != null) {
  494. xmpBasic.setCreatorTool(getUserAgent().getProducer());
  495. } else {
  496. xmpBasic.setCreatorTool(Version.getVersion());
  497. }
  498. xmpBasic.setMetadataDate(new java.util.Date());
  499. if (getUserAgent().getCreationDate() != null) {
  500. xmpBasic.setCreateDate(getUserAgent().getCreationDate());
  501. } else {
  502. xmpBasic.setCreateDate(xmpBasic.getMetadataDate());
  503. }
  504. return xmp;
  505. }
  506. /** {@inheritDoc} */
  507. public void preparePage(PageViewport page) {
  508. super.preparePage(page);
  509. }
  510. /** {@inheritDoc} */
  511. public void renderPage(PageViewport page) throws IOException, FOPException {
  512. if (log.isTraceEnabled()) {
  513. log.trace("renderPage() " + page);
  514. }
  515. try {
  516. pageIndices.put(page.getKey(), new Integer(page.getPageIndex()));
  517. Rectangle viewArea = page.getViewArea();
  518. Dimension dim = new Dimension(viewArea.width, viewArea.height);
  519. establishForeignAttributes(page.getForeignAttributes());
  520. documentHandler.startPage(page.getPageIndex(), page.getPageNumberString(),
  521. page.getSimplePageMasterName(), dim);
  522. resetForeignAttributes();
  523. documentHandler.startPageHeader();
  524. //Add page attachments to page header
  525. processExtensionAttachments(page);
  526. documentHandler.endPageHeader();
  527. this.painter = documentHandler.startPageContent();
  528. super.renderPage(page);
  529. this.painter = null;
  530. documentHandler.endPageContent();
  531. documentHandler.startPageTrailer();
  532. if (hasDocumentNavigation()) {
  533. Iterator iter = this.deferredLinks.iterator();
  534. while (iter.hasNext()) {
  535. Link link = (Link)iter.next();
  536. iter.remove();
  537. getDocumentNavigationHandler().renderLink(link);
  538. }
  539. }
  540. documentHandler.endPageTrailer();
  541. establishForeignAttributes(page.getForeignAttributes());
  542. documentHandler.endPage();
  543. resetForeignAttributes();
  544. } catch (IFException e) {
  545. handleIFException(e);
  546. }
  547. }
  548. private void processExtensionAttachments(AreaTreeObject area) throws IFException {
  549. if (area.hasExtensionAttachments()) {
  550. for (Iterator iter = area.getExtensionAttachments().iterator();
  551. iter.hasNext();) {
  552. ExtensionAttachment attachment = (ExtensionAttachment) iter.next();
  553. this.documentHandler.handleExtensionObject(attachment);
  554. }
  555. }
  556. }
  557. private void establishForeignAttributes(Map foreignAttributes) {
  558. documentHandler.getContext().setForeignAttributes(foreignAttributes);
  559. }
  560. private void resetForeignAttributes() {
  561. documentHandler.getContext().resetForeignAttributes();
  562. }
  563. private void establishStructureTreeElement(StructureTreeElement structureTreeElement) {
  564. documentHandler.getContext().setStructureTreeElement(structureTreeElement);
  565. }
  566. private void resetStructurePointer() {
  567. documentHandler.getContext().resetStructureTreeElement();
  568. }
  569. /** {@inheritDoc} */
  570. protected void saveGraphicsState() {
  571. graphicContextStack.push(graphicContext);
  572. graphicContext = (IFGraphicContext)graphicContext.clone();
  573. }
  574. /** {@inheritDoc} */
  575. protected void restoreGraphicsState() {
  576. while (graphicContext.getGroupStackSize() > 0) {
  577. IFGraphicContext.Group[] groups = graphicContext.dropGroups();
  578. for (int i = groups.length - 1; i >= 0; i--) {
  579. try {
  580. groups[i].end(painter);
  581. } catch (IFException ife) {
  582. handleIFException(ife);
  583. }
  584. }
  585. }
  586. graphicContext = (IFGraphicContext)graphicContextStack.pop();
  587. }
  588. private void pushGroup(IFGraphicContext.Group group) {
  589. graphicContext.pushGroup(group);
  590. try {
  591. group.start(painter);
  592. } catch (IFException ife) {
  593. handleIFException(ife);
  594. }
  595. }
  596. /** {@inheritDoc} */
  597. protected List breakOutOfStateStack() {
  598. log.debug("Block.FIXED --> break out");
  599. List breakOutList = new java.util.ArrayList();
  600. while (!this.graphicContextStack.empty()) {
  601. //Handle groups
  602. IFGraphicContext.Group[] groups = graphicContext.getGroups();
  603. for (int j = groups.length - 1; j >= 0; j--) {
  604. try {
  605. groups[j].end(painter);
  606. } catch (IFException ife) {
  607. handleIFException(ife);
  608. }
  609. }
  610. breakOutList.add(0, this.graphicContext);
  611. graphicContext = (IFGraphicContext)graphicContextStack.pop();
  612. }
  613. return breakOutList;
  614. }
  615. /** {@inheritDoc} */
  616. protected void restoreStateStackAfterBreakOut(List breakOutList) {
  617. log.debug("Block.FIXED --> restoring context after break-out");
  618. for (int i = 0, c = breakOutList.size(); i < c; i++) {
  619. graphicContextStack.push(graphicContext);
  620. this.graphicContext = (IFGraphicContext)breakOutList.get(i);
  621. //Handle groups
  622. IFGraphicContext.Group[] groups = graphicContext.getGroups();
  623. for (int j = 0, jc = groups.length; j < jc; j++) {
  624. try {
  625. groups[j].start(painter);
  626. } catch (IFException ife) {
  627. handleIFException(ife);
  628. }
  629. }
  630. }
  631. log.debug("restored.");
  632. }
  633. /** {@inheritDoc} */
  634. protected void concatenateTransformationMatrix(AffineTransform at) {
  635. if (!at.isIdentity()) {
  636. concatenateTransformationMatrixMpt(ptToMpt(at), false);
  637. }
  638. }
  639. private void concatenateTransformationMatrixMpt(AffineTransform at, boolean force) {
  640. if (force || !at.isIdentity()) {
  641. if (log.isTraceEnabled()) {
  642. log.trace("-----concatenateTransformationMatrix: " + at);
  643. }
  644. IFGraphicContext.Group group = new IFGraphicContext.Group(at);
  645. pushGroup(group);
  646. }
  647. }
  648. /** {@inheritDoc} */
  649. protected void beginTextObject() {
  650. //nop - Ignore, handled by painter internally
  651. }
  652. /** {@inheritDoc} */
  653. protected void endTextObject() {
  654. //nop - Ignore, handled by painter internally
  655. }
  656. /** {@inheritDoc} */
  657. protected void renderRegionViewport(RegionViewport viewport) {
  658. Dimension dim = new Dimension(viewport.getIPD(), viewport.getBPD());
  659. viewportDimensionStack.push(dim);
  660. super.renderRegionViewport(viewport);
  661. viewportDimensionStack.pop();
  662. }
  663. /** {@inheritDoc} */
  664. protected void renderBlockViewport(BlockViewport bv, List children) {
  665. //Essentially the same code as in the super class but optimized for the IF
  666. //This is the content-rect
  667. Dimension dim = new Dimension(bv.getIPD(), bv.getBPD());
  668. viewportDimensionStack.push(dim);
  669. // save positions
  670. int saveIP = currentIPPosition;
  671. int saveBP = currentBPPosition;
  672. CTM ctm = bv.getCTM();
  673. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  674. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  675. if (bv.getPositioning() == Block.ABSOLUTE
  676. || bv.getPositioning() == Block.FIXED) {
  677. //For FIXED, we need to break out of the current viewports to the
  678. //one established by the page. We save the state stack for restoration
  679. //after the block-container has been painted. See below.
  680. List breakOutList = null;
  681. if (bv.getPositioning() == Block.FIXED) {
  682. breakOutList = breakOutOfStateStack();
  683. }
  684. AffineTransform positionTransform = new AffineTransform();
  685. positionTransform.translate(bv.getXOffset(), bv.getYOffset());
  686. //"left/"top" (bv.getX/YOffset()) specify the position of the content rectangle
  687. positionTransform.translate(-borderPaddingStart, -borderPaddingBefore);
  688. //Free transformation for the block-container viewport
  689. String transf;
  690. transf = bv.getForeignAttributeValue(FOX_TRANSFORM);
  691. if (transf != null) {
  692. AffineTransform freeTransform = AWTTransformProducer.createAffineTransform(transf);
  693. positionTransform.concatenate(freeTransform);
  694. }
  695. saveGraphicsState();
  696. //Viewport position
  697. concatenateTransformationMatrixMpt(positionTransform, false);
  698. //Background and borders
  699. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd());
  700. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter());
  701. drawBackAndBorders(bv, 0, 0,
  702. (dim.width + bpwidth) / 1000f, (dim.height + bpheight) / 1000f);
  703. //Shift to content rectangle after border painting
  704. AffineTransform contentRectTransform = new AffineTransform();
  705. contentRectTransform.translate(borderPaddingStart, borderPaddingBefore);
  706. concatenateTransformationMatrixMpt(contentRectTransform, false);
  707. //saveGraphicsState();
  708. //Set up coordinate system for content rectangle
  709. AffineTransform contentTransform = ctm.toAffineTransform();
  710. //concatenateTransformationMatrixMpt(contentTransform);
  711. startViewport(contentTransform, bv.getClipRectangle());
  712. currentIPPosition = 0;
  713. currentBPPosition = 0;
  714. renderBlocks(bv, children);
  715. endViewport();
  716. //restoreGraphicsState();
  717. restoreGraphicsState();
  718. if (breakOutList != null) {
  719. restoreStateStackAfterBreakOut(breakOutList);
  720. }
  721. currentIPPosition = saveIP;
  722. currentBPPosition = saveBP;
  723. } else {
  724. currentBPPosition += bv.getSpaceBefore();
  725. //borders and background in the old coordinate system
  726. handleBlockTraits(bv);
  727. //Advance to start of content area
  728. currentIPPosition += bv.getStartIndent();
  729. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  730. ctm = tempctm.multiply(ctm);
  731. //Now adjust for border/padding
  732. currentBPPosition += borderPaddingBefore;
  733. startVParea(ctm, bv.getClipRectangle());
  734. currentIPPosition = 0;
  735. currentBPPosition = 0;
  736. renderBlocks(bv, children);
  737. endVParea();
  738. currentIPPosition = saveIP;
  739. currentBPPosition = saveBP;
  740. currentBPPosition += bv.getAllocBPD();
  741. }
  742. viewportDimensionStack.pop();
  743. }
  744. /** {@inheritDoc} */
  745. public void renderInlineViewport(InlineViewport viewport) {
  746. StructureTreeElement structElem
  747. = (StructureTreeElement) viewport.getTrait(Trait.STRUCTURE_TREE_ELEMENT);
  748. establishStructureTreeElement(structElem);
  749. pushdID(viewport);
  750. Dimension dim = new Dimension(viewport.getIPD(), viewport.getBPD());
  751. viewportDimensionStack.push(dim);
  752. super.renderInlineViewport(viewport);
  753. viewportDimensionStack.pop();
  754. resetStructurePointer();
  755. popID(viewport);
  756. }
  757. /** {@inheritDoc} */
  758. protected void startVParea(CTM ctm, Rectangle clippingRect) {
  759. if (log.isTraceEnabled()) {
  760. log.trace("startVParea() ctm=" + ctm + ", clippingRect=" + clippingRect);
  761. }
  762. AffineTransform at = new AffineTransform(ctm.toArray());
  763. startViewport(at, clippingRect);
  764. if (log.isTraceEnabled()) {
  765. log.trace("startVPArea: " + at + " --> " + graphicContext.getTransform());
  766. }
  767. }
  768. private void startViewport(AffineTransform at, Rectangle clipRect) {
  769. saveGraphicsState();
  770. try {
  771. IFGraphicContext.Viewport viewport = new IFGraphicContext.Viewport(
  772. at, (Dimension)viewportDimensionStack.peek(), clipRect);
  773. graphicContext.pushGroup(viewport);
  774. viewport.start(painter);
  775. } catch (IFException e) {
  776. handleIFException(e);
  777. }
  778. }
  779. /** {@inheritDoc} */
  780. protected void endVParea() {
  781. log.trace("endVParea()");
  782. endViewport();
  783. if (log.isTraceEnabled()) {
  784. log.trace("endVPArea() --> " + graphicContext.getTransform());
  785. }
  786. }
  787. private void endViewport() {
  788. restoreGraphicsState();
  789. }
  790. /** {@inheritDoc} */
  791. protected void renderInlineArea(InlineArea inlineArea) {
  792. saveInlinePosIfTargetable(inlineArea);
  793. pushdID(inlineArea);
  794. super.renderInlineArea(inlineArea);
  795. popID(inlineArea);
  796. }
  797. /** {@inheritDoc} */
  798. public void renderInlineParent(InlineParent ip) {
  799. // stuff we only need if a link must be created:
  800. Rectangle ipRect = null;
  801. AbstractAction action = null;
  802. // make sure the rect is determined *before* calling super!
  803. int ipp = currentIPPosition;
  804. int bpp = currentBPPosition + ip.getOffset();
  805. ipRect = new Rectangle(ipp, bpp, ip.getIPD(), ip.getBPD());
  806. AffineTransform transform = graphicContext.getTransform();
  807. ipRect = transform.createTransformedShape(ipRect).getBounds();
  808. // render contents
  809. super.renderInlineParent(ip);
  810. boolean linkTraitFound = false;
  811. // try INTERNAL_LINK first
  812. Trait.InternalLink intLink = (Trait.InternalLink) ip.getTrait(Trait.INTERNAL_LINK);
  813. if (intLink != null) {
  814. linkTraitFound = true;
  815. String pvKey = intLink.getPVKey();
  816. String idRef = intLink.getIDRef();
  817. boolean pvKeyOK = pvKey != null && pvKey.length() > 0;
  818. boolean idRefOK = idRef != null && idRef.length() > 0;
  819. if (pvKeyOK && idRefOK) {
  820. Integer pageIndex = (Integer)pageIndices.get(pvKey);
  821. action = getGoToActionForID(idRef, (pageIndex != null ? pageIndex.intValue() : -1));
  822. } else {
  823. //Warnings already issued by AreaTreeHandler
  824. }
  825. }
  826. // no INTERNAL_LINK, look for EXTERNAL_LINK
  827. if (!linkTraitFound) {
  828. Trait.ExternalLink extLink = (Trait.ExternalLink) ip.getTrait(Trait.EXTERNAL_LINK);
  829. if (extLink != null) {
  830. String extDest = extLink.getDestination();
  831. if (extDest != null && extDest.length() > 0) {
  832. linkTraitFound = true;
  833. action = new URIAction(extDest, extLink.newWindow());
  834. action = actionSet.put(action);
  835. }
  836. }
  837. }
  838. // warn if link trait found but not allowed, else create link
  839. if (linkTraitFound) {
  840. StructureTreeElement structElem
  841. = (StructureTreeElement) ip.getTrait(Trait.STRUCTURE_TREE_ELEMENT);
  842. action.setStructureTreeElement(structElem);
  843. Link link = new Link(action, ipRect);
  844. this.deferredLinks.add(link);
  845. }
  846. }
  847. /** {@inheritDoc} */
  848. protected void renderBlock(Block block) {
  849. if (log.isTraceEnabled()) {
  850. log.trace("renderBlock() " + block);
  851. }
  852. saveBlockPosIfTargetable(block);
  853. pushdID(block);
  854. super.renderBlock(block);
  855. popID(block);
  856. }
  857. private void pushdID(Area area) {
  858. String prodID = (String) area.getTrait(Trait.PROD_ID);
  859. if (prodID != null) {
  860. ids.push(prodID);
  861. documentHandler.getContext().setID(prodID);
  862. }
  863. }
  864. private void popID(Area area) {
  865. String prodID = (String) area.getTrait(Trait.PROD_ID);
  866. if (prodID != null) {
  867. ids.pop();
  868. documentHandler.getContext().setID(ids.empty() ? "" : ids.peek());
  869. }
  870. }
  871. private Typeface getTypeface(String fontName) {
  872. Typeface tf = (Typeface) fontInfo.getFonts().get(fontName);
  873. if (tf instanceof LazyFont) {
  874. tf = ((LazyFont)tf).getRealFont();
  875. }
  876. return tf;
  877. }
  878. /** {@inheritDoc} */
  879. protected void renderText(TextArea text) {
  880. if (log.isTraceEnabled()) {
  881. log.trace("renderText() " + text);
  882. }
  883. renderInlineAreaBackAndBorders(text);
  884. Color ct = (Color) text.getTrait(Trait.COLOR);
  885. beginTextObject();
  886. String fontName = getInternalFontNameForArea(text);
  887. int size = ((Integer) text.getTrait(Trait.FONT_SIZE)).intValue();
  888. StructureTreeElement structElem
  889. = (StructureTreeElement) text.getTrait(Trait.STRUCTURE_TREE_ELEMENT);
  890. establishStructureTreeElement(structElem);
  891. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  892. Typeface tf = getTypeface(fontName);
  893. FontTriplet triplet = (FontTriplet)text.getTrait(Trait.FONT);
  894. try {
  895. painter.setFont(triplet.getName(), triplet.getStyle(), new Integer(triplet.getWeight()),
  896. "normal", new Integer(size), ct);
  897. } catch (IFException e) {
  898. handleIFException(e);
  899. }
  900. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  901. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  902. textUtil.flush();
  903. textUtil.setStartPosition(rx, bl);
  904. textUtil.setSpacing(text.getTextLetterSpaceAdjust(), text.getTextWordSpaceAdjust());
  905. super.renderText(text);
  906. textUtil.flush();
  907. renderTextDecoration(tf, size, text, bl, rx);
  908. resetStructurePointer();
  909. }
  910. /** {@inheritDoc} */
  911. protected void renderWord(WordArea word) {
  912. Font font = getFontFromArea(word.getParentArea());
  913. String s = word.getWord();
  914. renderText(s, word.getLetterAdjustArray(),
  915. font, (AbstractTextArea)word.getParentArea());
  916. super.renderWord(word);
  917. }
  918. /** {@inheritDoc} */
  919. protected void renderSpace(SpaceArea space) {
  920. Font font = getFontFromArea(space.getParentArea());
  921. String s = space.getSpace();
  922. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  923. renderText(s, null, font, textArea);
  924. if (textUtil.combined && space.isAdjustable()) {
  925. //Used for justified text, for example
  926. int tws = textArea.getTextWordSpaceAdjust()
  927. + 2 * textArea.getTextLetterSpaceAdjust();
  928. if (tws != 0) {
  929. textUtil.adjust(tws);
  930. }
  931. }
  932. super.renderSpace(space);
  933. }
  934. /**
  935. * Does low-level rendering of text.
  936. * @param s text to render
  937. * @param letterAdjust an array of widths for letter adjustment (may be null)
  938. * @param font to font in use
  939. * @param parentArea the parent text area to retrieve certain traits from
  940. */
  941. protected void renderText(String s,
  942. int[] letterAdjust,
  943. Font font, AbstractTextArea parentArea) {
  944. int l = s.length();
  945. if (l == 0) {
  946. return;
  947. }
  948. if (letterAdjust != null) {
  949. textUtil.adjust(letterAdjust[0]);
  950. }
  951. for (int i = 0; i < l; i++) {
  952. char ch = s.charAt(i);
  953. textUtil.addChar(ch);
  954. int glyphAdjust = 0;
  955. if (textUtil.combined && font.hasChar(ch)) {
  956. int tls = (i < l - 1 ? parentArea.getTextLetterSpaceAdjust() : 0);
  957. glyphAdjust += tls;
  958. }
  959. if (letterAdjust != null && i < l - 1) {
  960. glyphAdjust += letterAdjust[i + 1];
  961. }
  962. textUtil.adjust(glyphAdjust);
  963. }
  964. }
  965. private class TextUtil {
  966. private static final int INITIAL_BUFFER_SIZE = 16;
  967. private int[] dx = new int[INITIAL_BUFFER_SIZE];
  968. private int lastDXPos = 0;
  969. private final StringBuffer text = new StringBuffer();
  970. private int startx, starty;
  971. private int tls, tws;
  972. private final boolean combined = false;
  973. void addChar(char ch) {
  974. text.append(ch);
  975. }
  976. void adjust(int adjust) {
  977. if (adjust != 0) {
  978. int idx = text.length();
  979. if (idx > dx.length - 1) {
  980. int newSize = Math.max(dx.length, idx + 1) + INITIAL_BUFFER_SIZE;
  981. int[] newDX = new int[newSize];
  982. System.arraycopy(dx, 0, newDX, 0, dx.length);
  983. dx = newDX;
  984. }
  985. dx[idx] += adjust;
  986. lastDXPos = idx;
  987. }
  988. }
  989. void reset() {
  990. if (text.length() > 0) {
  991. text.setLength(0);
  992. Arrays.fill(dx, 0);
  993. lastDXPos = 0;
  994. }
  995. }
  996. void setStartPosition(int x, int y) {
  997. this.startx = x;
  998. this.starty = y;
  999. }
  1000. void setSpacing(int tls, int tws) {
  1001. this.tls = tls;
  1002. this.tws = tws;
  1003. }
  1004. void flush() {
  1005. if (text.length() > 0) {
  1006. try {
  1007. int[] effDX = null;
  1008. if (lastDXPos > 0) {
  1009. int size = lastDXPos + 1;
  1010. effDX = new int[size];
  1011. System.arraycopy(dx, 0, effDX, 0, size);
  1012. }
  1013. if (combined) {
  1014. painter.drawText(startx, starty, 0, 0, effDX, text.toString());
  1015. } else {
  1016. painter.drawText(startx, starty, tls, tws, effDX, text.toString());
  1017. }
  1018. } catch (IFException e) {
  1019. handleIFException(e);
  1020. }
  1021. reset();
  1022. }
  1023. }
  1024. }
  1025. /** {@inheritDoc} */
  1026. public void renderImage(Image image, Rectangle2D pos) {
  1027. drawImage(image.getURL(), pos, image.getForeignAttributes());
  1028. }
  1029. /** {@inheritDoc} */
  1030. protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
  1031. Rectangle posInt = new Rectangle(
  1032. currentIPPosition + (int)pos.getX(),
  1033. currentBPPosition + (int)pos.getY(),
  1034. (int)pos.getWidth(),
  1035. (int)pos.getHeight());
  1036. uri = URISpecification.getURL(uri);
  1037. try {
  1038. establishForeignAttributes(foreignAttributes);
  1039. painter.drawImage(uri, posInt);
  1040. resetForeignAttributes();
  1041. } catch (IFException ife) {
  1042. handleIFException(ife);
  1043. }
  1044. }
  1045. /** {@inheritDoc} */
  1046. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  1047. endTextObject();
  1048. Rectangle posInt = new Rectangle(
  1049. currentIPPosition + (int)pos.getX(),
  1050. currentBPPosition + (int)pos.getY(),
  1051. (int)pos.getWidth(),
  1052. (int)pos.getHeight());
  1053. Document doc = fo.getDocument();
  1054. try {
  1055. establishForeignAttributes(fo.getForeignAttributes());
  1056. painter.drawImage(doc, posInt);
  1057. resetForeignAttributes();
  1058. } catch (IFException ife) {
  1059. handleIFException(ife);
  1060. }
  1061. }
  1062. /** {@inheritDoc} */
  1063. public void renderLeader(Leader area) {
  1064. renderInlineAreaBackAndBorders(area);
  1065. int style = area.getRuleStyle();
  1066. int ruleThickness = area.getRuleThickness();
  1067. int startx = currentIPPosition + area.getBorderAndPaddingWidthStart();
  1068. int starty = currentBPPosition + area.getOffset() + (ruleThickness / 2);
  1069. int endx = currentIPPosition
  1070. + area.getBorderAndPaddingWidthStart()
  1071. + area.getIPD();
  1072. Color col = (Color)area.getTrait(Trait.COLOR);
  1073. Point start = new Point(startx, starty);
  1074. Point end = new Point(endx, starty);
  1075. try {
  1076. painter.drawLine(start, end, ruleThickness, col, RuleStyle.valueOf(style));
  1077. } catch (IFException ife) {
  1078. handleIFException(ife);
  1079. }
  1080. super.renderLeader(area);
  1081. }
  1082. /** {@inheritDoc} */
  1083. protected void clip() {
  1084. throw new IllegalStateException("Not used");
  1085. }
  1086. /** {@inheritDoc} */
  1087. protected void clipRect(float x, float y, float width, float height) {
  1088. pushGroup(new IFGraphicContext.Group());
  1089. try {
  1090. painter.clipRect(toMillipointRectangle(x, y, width, height));
  1091. } catch (IFException ife) {
  1092. handleIFException(ife);
  1093. }
  1094. }
  1095. /** {@inheritDoc} */
  1096. protected void closePath() {
  1097. throw new IllegalStateException("Not used");
  1098. }
  1099. /** {@inheritDoc} */
  1100. protected void drawBorders( // CSOK: ParameterNumber
  1101. float startx, float starty,
  1102. float width, float height,
  1103. BorderProps bpsBefore, BorderProps bpsAfter,
  1104. BorderProps bpsStart, BorderProps bpsEnd) {
  1105. Rectangle rect = toMillipointRectangle(startx, starty, width, height);
  1106. try {
  1107. painter.drawBorderRect(rect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  1108. } catch (IFException ife) {
  1109. handleIFException(ife);
  1110. }
  1111. }
  1112. /** {@inheritDoc} */
  1113. protected void drawBorderLine( // CSOK: ParameterNumber
  1114. float x1, float y1, float x2, float y2, boolean horz,
  1115. boolean startOrBefore, int style, Color col) {
  1116. //Simplified implementation that is only used by renderTextDecoration()
  1117. //drawBorders() is overridden and uses the Painter's high-level method drawBorderRect()
  1118. updateColor(col, true);
  1119. fillRect(x1, y1, x2 - x1, y2 - y1);
  1120. }
  1121. private int toMillipoints(float coordinate) {
  1122. return Math.round(coordinate * 1000);
  1123. }
  1124. private Rectangle toMillipointRectangle(float x, float y, float width, float height) {
  1125. return new Rectangle(
  1126. toMillipoints(x),
  1127. toMillipoints(y),
  1128. toMillipoints(width),
  1129. toMillipoints(height));
  1130. }
  1131. /** {@inheritDoc} */
  1132. protected void fillRect(float x, float y, float width, float height) {
  1133. try {
  1134. painter.fillRect(
  1135. toMillipointRectangle(x, y, width, height),
  1136. this.graphicContext.getPaint());
  1137. } catch (IFException e) {
  1138. handleIFException(e);
  1139. }
  1140. }
  1141. /** {@inheritDoc} */
  1142. protected void moveTo(float x, float y) {
  1143. throw new IllegalStateException("Not used");
  1144. }
  1145. /** {@inheritDoc} */
  1146. protected void lineTo(float x, float y) {
  1147. throw new IllegalStateException("Not used");
  1148. }
  1149. /** {@inheritDoc} */
  1150. protected void updateColor(Color col, boolean fill) {
  1151. if (fill) {
  1152. this.graphicContext.setPaint(col);
  1153. } else {
  1154. this.graphicContext.setColor(col);
  1155. }
  1156. }
  1157. }