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.

IFRenderer.java 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 = (StructureTreeElement) text.getTrait(Trait.STRUCTURE_TREE_ELEMENT);
  889. establishStructureTreeElement(structElem);
  890. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  891. Typeface tf = getTypeface(fontName);
  892. FontTriplet triplet = (FontTriplet)text.getTrait(Trait.FONT);
  893. try {
  894. painter.setFont(triplet.getName(), triplet.getStyle(), new Integer(triplet.getWeight()),
  895. "normal", new Integer(size), ct);
  896. } catch (IFException e) {
  897. handleIFException(e);
  898. }
  899. int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  900. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  901. textUtil.flush();
  902. textUtil.setStartPosition(rx, bl);
  903. textUtil.setSpacing(text.getTextLetterSpaceAdjust(), text.getTextWordSpaceAdjust());
  904. super.renderText(text);
  905. textUtil.flush();
  906. renderTextDecoration(tf, size, text, bl, rx);
  907. resetStructurePointer();
  908. }
  909. /** {@inheritDoc} */
  910. protected void renderWord(WordArea word) {
  911. Font font = getFontFromArea(word.getParentArea());
  912. String s = word.getWord();
  913. renderText(s, word.getLetterAdjustArray(),
  914. font, (AbstractTextArea)word.getParentArea());
  915. super.renderWord(word);
  916. }
  917. /** {@inheritDoc} */
  918. protected void renderSpace(SpaceArea space) {
  919. Font font = getFontFromArea(space.getParentArea());
  920. String s = space.getSpace();
  921. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  922. renderText(s, null, font, textArea);
  923. if (textUtil.combined && space.isAdjustable()) {
  924. //Used for justified text, for example
  925. int tws = textArea.getTextWordSpaceAdjust()
  926. + 2 * textArea.getTextLetterSpaceAdjust();
  927. if (tws != 0) {
  928. textUtil.adjust(tws);
  929. }
  930. }
  931. super.renderSpace(space);
  932. }
  933. /**
  934. * Does low-level rendering of text.
  935. * @param s text to render
  936. * @param letterAdjust an array of widths for letter adjustment (may be null)
  937. * @param font to font in use
  938. * @param parentArea the parent text area to retrieve certain traits from
  939. */
  940. protected void renderText(String s,
  941. int[] letterAdjust,
  942. Font font, AbstractTextArea parentArea) {
  943. int l = s.length();
  944. if (l == 0) {
  945. return;
  946. }
  947. if (letterAdjust != null) {
  948. textUtil.adjust(letterAdjust[0]);
  949. }
  950. for (int i = 0; i < l; i++) {
  951. char ch = s.charAt(i);
  952. textUtil.addChar(ch);
  953. int glyphAdjust = 0;
  954. if (textUtil.combined && font.hasChar(ch)) {
  955. int tls = (i < l - 1 ? parentArea.getTextLetterSpaceAdjust() : 0);
  956. glyphAdjust += tls;
  957. }
  958. if (letterAdjust != null && i < l - 1) {
  959. glyphAdjust += letterAdjust[i + 1];
  960. }
  961. textUtil.adjust(glyphAdjust);
  962. }
  963. }
  964. private class TextUtil {
  965. private static final int INITIAL_BUFFER_SIZE = 16;
  966. private int[] dx = new int[INITIAL_BUFFER_SIZE];
  967. private int lastDXPos = 0;
  968. private final StringBuffer text = new StringBuffer();
  969. private int startx, starty;
  970. private int tls, tws;
  971. private final boolean combined = false;
  972. void addChar(char ch) {
  973. text.append(ch);
  974. }
  975. void adjust(int adjust) {
  976. if (adjust != 0) {
  977. int idx = text.length();
  978. if (idx > dx.length - 1) {
  979. int newSize = Math.max(dx.length, idx + 1) + INITIAL_BUFFER_SIZE;
  980. int[] newDX = new int[newSize];
  981. System.arraycopy(dx, 0, newDX, 0, dx.length);
  982. dx = newDX;
  983. }
  984. dx[idx] += adjust;
  985. lastDXPos = idx;
  986. }
  987. }
  988. void reset() {
  989. if (text.length() > 0) {
  990. text.setLength(0);
  991. Arrays.fill(dx, 0);
  992. lastDXPos = 0;
  993. }
  994. }
  995. void setStartPosition(int x, int y) {
  996. this.startx = x;
  997. this.starty = y;
  998. }
  999. void setSpacing(int tls, int tws) {
  1000. this.tls = tls;
  1001. this.tws = tws;
  1002. }
  1003. void flush() {
  1004. if (text.length() > 0) {
  1005. try {
  1006. int[] effDX = null;
  1007. if (lastDXPos > 0) {
  1008. int size = lastDXPos + 1;
  1009. effDX = new int[size];
  1010. System.arraycopy(dx, 0, effDX, 0, size);
  1011. }
  1012. if (combined) {
  1013. painter.drawText(startx, starty, 0, 0, effDX, text.toString());
  1014. } else {
  1015. painter.drawText(startx, starty, tls, tws, effDX, text.toString());
  1016. }
  1017. } catch (IFException e) {
  1018. handleIFException(e);
  1019. }
  1020. reset();
  1021. }
  1022. }
  1023. }
  1024. /** {@inheritDoc} */
  1025. public void renderImage(Image image, Rectangle2D pos) {
  1026. drawImage(image.getURL(), pos, image.getForeignAttributes());
  1027. }
  1028. /** {@inheritDoc} */
  1029. protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
  1030. Rectangle posInt = new Rectangle(
  1031. currentIPPosition + (int)pos.getX(),
  1032. currentBPPosition + (int)pos.getY(),
  1033. (int)pos.getWidth(),
  1034. (int)pos.getHeight());
  1035. uri = URISpecification.getURL(uri);
  1036. try {
  1037. establishForeignAttributes(foreignAttributes);
  1038. painter.drawImage(uri, posInt);
  1039. resetForeignAttributes();
  1040. } catch (IFException ife) {
  1041. handleIFException(ife);
  1042. }
  1043. }
  1044. /** {@inheritDoc} */
  1045. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  1046. endTextObject();
  1047. Rectangle posInt = new Rectangle(
  1048. currentIPPosition + (int)pos.getX(),
  1049. currentBPPosition + (int)pos.getY(),
  1050. (int)pos.getWidth(),
  1051. (int)pos.getHeight());
  1052. Document doc = fo.getDocument();
  1053. try {
  1054. establishForeignAttributes(fo.getForeignAttributes());
  1055. painter.drawImage(doc, posInt);
  1056. resetForeignAttributes();
  1057. } catch (IFException ife) {
  1058. handleIFException(ife);
  1059. }
  1060. }
  1061. /** {@inheritDoc} */
  1062. public void renderLeader(Leader area) {
  1063. renderInlineAreaBackAndBorders(area);
  1064. int style = area.getRuleStyle();
  1065. int ruleThickness = area.getRuleThickness();
  1066. int startx = currentIPPosition + area.getBorderAndPaddingWidthStart();
  1067. int starty = currentBPPosition + area.getOffset() + (ruleThickness / 2);
  1068. int endx = currentIPPosition
  1069. + area.getBorderAndPaddingWidthStart()
  1070. + area.getIPD();
  1071. Color col = (Color)area.getTrait(Trait.COLOR);
  1072. Point start = new Point(startx, starty);
  1073. Point end = new Point(endx, starty);
  1074. try {
  1075. painter.drawLine(start, end, ruleThickness, col, RuleStyle.valueOf(style));
  1076. } catch (IFException ife) {
  1077. handleIFException(ife);
  1078. }
  1079. super.renderLeader(area);
  1080. }
  1081. /** {@inheritDoc} */
  1082. protected void clip() {
  1083. throw new IllegalStateException("Not used");
  1084. }
  1085. /** {@inheritDoc} */
  1086. protected void clipRect(float x, float y, float width, float height) {
  1087. pushGroup(new IFGraphicContext.Group());
  1088. try {
  1089. painter.clipRect(toMillipointRectangle(x, y, width, height));
  1090. } catch (IFException ife) {
  1091. handleIFException(ife);
  1092. }
  1093. }
  1094. /** {@inheritDoc} */
  1095. protected void closePath() {
  1096. throw new IllegalStateException("Not used");
  1097. }
  1098. /** {@inheritDoc} */
  1099. protected void drawBorders( // CSOK: ParameterNumber
  1100. float startx, float starty,
  1101. float width, float height,
  1102. BorderProps bpsBefore, BorderProps bpsAfter,
  1103. BorderProps bpsStart, BorderProps bpsEnd) {
  1104. Rectangle rect = toMillipointRectangle(startx, starty, width, height);
  1105. try {
  1106. painter.drawBorderRect(rect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  1107. } catch (IFException ife) {
  1108. handleIFException(ife);
  1109. }
  1110. }
  1111. /** {@inheritDoc} */
  1112. protected void drawBorderLine( // CSOK: ParameterNumber
  1113. float x1, float y1, float x2, float y2, boolean horz,
  1114. boolean startOrBefore, int style, Color col) {
  1115. //Simplified implementation that is only used by renderTextDecoration()
  1116. //drawBorders() is overridden and uses the Painter's high-level method drawBorderRect()
  1117. updateColor(col, true);
  1118. fillRect(x1, y1, x2 - x1, y2 - y1);
  1119. }
  1120. private int toMillipoints(float coordinate) {
  1121. return Math.round(coordinate * 1000);
  1122. }
  1123. private Rectangle toMillipointRectangle(float x, float y, float width, float height) {
  1124. return new Rectangle(
  1125. toMillipoints(x),
  1126. toMillipoints(y),
  1127. toMillipoints(width),
  1128. toMillipoints(height));
  1129. }
  1130. /** {@inheritDoc} */
  1131. protected void fillRect(float x, float y, float width, float height) {
  1132. try {
  1133. painter.fillRect(
  1134. toMillipointRectangle(x, y, width, height),
  1135. this.graphicContext.getPaint());
  1136. } catch (IFException e) {
  1137. handleIFException(e);
  1138. }
  1139. }
  1140. /** {@inheritDoc} */
  1141. protected void moveTo(float x, float y) {
  1142. throw new IllegalStateException("Not used");
  1143. }
  1144. /** {@inheritDoc} */
  1145. protected void lineTo(float x, float y) {
  1146. throw new IllegalStateException("Not used");
  1147. }
  1148. /** {@inheritDoc} */
  1149. protected void updateColor(Color col, boolean fill) {
  1150. if (fill) {
  1151. this.graphicContext.setPaint(col);
  1152. } else {
  1153. this.graphicContext.setColor(col);
  1154. }
  1155. }
  1156. }