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

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