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.

IFSerializer.java 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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.Paint;
  22. import java.awt.Point;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.AffineTransform;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import org.w3c.dom.Document;
  30. import org.xml.sax.SAXException;
  31. import org.xml.sax.helpers.AttributesImpl;
  32. import org.apache.xmlgraphics.image.loader.ImageManager;
  33. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  34. import org.apache.xmlgraphics.util.QName;
  35. import org.apache.xmlgraphics.util.XMLizable;
  36. import org.apache.fop.accessibility.StructureTreeEventHandler;
  37. import org.apache.fop.fo.extensions.InternalElementMapping;
  38. import org.apache.fop.fonts.FontInfo;
  39. import org.apache.fop.render.DefaultRendererConfigurator;
  40. import org.apache.fop.render.RenderingContext;
  41. import org.apache.fop.render.intermediate.IFRendererConfig.IFRendererConfigParser;
  42. import org.apache.fop.render.intermediate.IFStructureTreeBuilder.IFStructureTreeElement;
  43. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  44. import org.apache.fop.render.intermediate.extensions.Bookmark;
  45. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  46. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  47. import org.apache.fop.render.intermediate.extensions.Link;
  48. import org.apache.fop.render.intermediate.extensions.NamedDestination;
  49. import org.apache.fop.traits.BorderProps;
  50. import org.apache.fop.traits.RuleStyle;
  51. import org.apache.fop.util.ColorUtil;
  52. import org.apache.fop.util.DOM2SAX;
  53. import org.apache.fop.util.LanguageTags;
  54. import org.apache.fop.util.XMLConstants;
  55. import org.apache.fop.util.XMLUtil;
  56. /**
  57. * IFPainter implementation that serializes the intermediate format to XML.
  58. */
  59. public class IFSerializer extends AbstractXMLWritingIFDocumentHandler
  60. implements IFConstants, IFPainter, IFDocumentNavigationHandler {
  61. /**
  62. * Intermediate Format (IF) version, used to express an @version attribute
  63. * in the root element of the IF document, the initial value of which
  64. * is set to '2.0' to signify that something preceded it (but didn't
  65. * happen to be marked as such), and that this version is not necessarily
  66. * backwards compatible with the unmarked (<2.0) version.
  67. */
  68. public static final String VERSION = "2.0";
  69. private IFDocumentHandler mimicHandler;
  70. private int pageSequenceIndex; // used for accessibility
  71. /** Holds the intermediate format state */
  72. private IFState state;
  73. private String currentID = "";
  74. private IFStructureTreeBuilder structureTreeBuilder;
  75. public IFSerializer(IFContext context) {
  76. super(context);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. protected String getMainNamespace() {
  81. return NAMESPACE;
  82. }
  83. /** {@inheritDoc} */
  84. public boolean supportsPagesOutOfOrder() {
  85. return false;
  86. //Theoretically supported but disabled to improve performance when
  87. //rendering the IF to the final format later on
  88. }
  89. /** {@inheritDoc} */
  90. public String getMimeType() {
  91. return MIME_TYPE;
  92. }
  93. /** {@inheritDoc} */
  94. public IFDocumentHandlerConfigurator getConfigurator() {
  95. if (this.mimicHandler != null) {
  96. return getMimickedDocumentHandler().getConfigurator();
  97. } else {
  98. return new DefaultRendererConfigurator(getUserAgent(), new IFRendererConfigParser());
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public IFDocumentNavigationHandler getDocumentNavigationHandler() {
  104. return this;
  105. }
  106. /**
  107. * Tells this serializer to mimic the given document handler (mostly applies to the font set
  108. * that is used during layout).
  109. * @param targetHandler the document handler to mimic
  110. */
  111. public void mimicDocumentHandler(IFDocumentHandler targetHandler) {
  112. this.mimicHandler = targetHandler;
  113. }
  114. /**
  115. * Returns the document handler that is being mimicked by this serializer.
  116. * @return the mimicked document handler or null if no such document handler has been set
  117. */
  118. public IFDocumentHandler getMimickedDocumentHandler() {
  119. return this.mimicHandler;
  120. }
  121. /** {@inheritDoc} */
  122. public FontInfo getFontInfo() {
  123. if (this.mimicHandler != null) {
  124. return this.mimicHandler.getFontInfo();
  125. } else {
  126. return null;
  127. }
  128. }
  129. /** {@inheritDoc} */
  130. public void setFontInfo(FontInfo fontInfo) {
  131. if (this.mimicHandler != null) {
  132. this.mimicHandler.setFontInfo(fontInfo);
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. public void setDefaultFontInfo(FontInfo fontInfo) {
  137. if (this.mimicHandler != null) {
  138. this.mimicHandler.setDefaultFontInfo(fontInfo);
  139. }
  140. }
  141. @Override
  142. public StructureTreeEventHandler getStructureTreeEventHandler() {
  143. if (structureTreeBuilder == null) {
  144. structureTreeBuilder = new IFStructureTreeBuilder();
  145. }
  146. return structureTreeBuilder;
  147. }
  148. /** {@inheritDoc} */
  149. @Override
  150. public void startDocument() throws IFException {
  151. super.startDocument();
  152. try {
  153. handler.startDocument();
  154. handler.startPrefixMapping("", NAMESPACE);
  155. handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE);
  156. handler.startPrefixMapping(DocumentNavigationExtensionConstants.PREFIX,
  157. DocumentNavigationExtensionConstants.NAMESPACE);
  158. handler.startPrefixMapping(InternalElementMapping.STANDARD_PREFIX,
  159. InternalElementMapping.URI);
  160. AttributesImpl atts = new AttributesImpl();
  161. addAttribute(atts, "version", VERSION);
  162. handler.startElement(EL_DOCUMENT, atts);
  163. } catch (SAXException e) {
  164. throw new IFException("SAX error in startDocument()", e);
  165. }
  166. }
  167. @Override
  168. public void setDocumentLocale(Locale locale) {
  169. AttributesImpl atts = new AttributesImpl();
  170. atts.addAttribute(XML_NAMESPACE, "lang", "xml:lang", XMLUtil.CDATA,
  171. LanguageTags.toLanguageTag(locale));
  172. try {
  173. handler.startElement(EL_LOCALE, atts);
  174. handler.endElement(EL_LOCALE);
  175. } catch (SAXException e) {
  176. throw new RuntimeException("Unable to create the " + EL_LOCALE + " element.", e);
  177. }
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. public void startDocumentHeader() throws IFException {
  182. try {
  183. handler.startElement(EL_HEADER);
  184. } catch (SAXException e) {
  185. throw new IFException("SAX error in startDocumentHeader()", e);
  186. }
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public void endDocumentHeader() throws IFException {
  191. try {
  192. handler.endElement(EL_HEADER);
  193. } catch (SAXException e) {
  194. throw new IFException("SAX error in startDocumentHeader()", e);
  195. }
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public void startDocumentTrailer() throws IFException {
  200. try {
  201. handler.startElement(EL_TRAILER);
  202. } catch (SAXException e) {
  203. throw new IFException("SAX error in startDocumentTrailer()", e);
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public void endDocumentTrailer() throws IFException {
  209. try {
  210. handler.endElement(EL_TRAILER);
  211. } catch (SAXException e) {
  212. throw new IFException("SAX error in endDocumentTrailer()", e);
  213. }
  214. }
  215. /** {@inheritDoc} */
  216. public void endDocument() throws IFException {
  217. try {
  218. handler.endElement(EL_DOCUMENT);
  219. handler.endDocument();
  220. finishDocumentNavigation();
  221. } catch (SAXException e) {
  222. throw new IFException("SAX error in endDocument()", e);
  223. }
  224. }
  225. /** {@inheritDoc} */
  226. public void startPageSequence(String id) throws IFException {
  227. try {
  228. AttributesImpl atts = new AttributesImpl();
  229. if (id != null) {
  230. atts.addAttribute(XML_NAMESPACE, "id", "xml:id", XMLUtil.CDATA, id);
  231. }
  232. Locale lang = getContext().getLanguage();
  233. if (lang != null) {
  234. atts.addAttribute(XML_NAMESPACE, "lang", "xml:lang", XMLUtil.CDATA,
  235. LanguageTags.toLanguageTag(lang));
  236. }
  237. XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve");
  238. addForeignAttributes(atts);
  239. handler.startElement(EL_PAGE_SEQUENCE, atts);
  240. if (this.getUserAgent().isAccessibilityEnabled()) {
  241. assert (structureTreeBuilder != null);
  242. structureTreeBuilder.replayEventsForPageSequence(handler, pageSequenceIndex++);
  243. }
  244. } catch (SAXException e) {
  245. throw new IFException("SAX error in startPageSequence()", e);
  246. }
  247. }
  248. /** {@inheritDoc} */
  249. public void endPageSequence() throws IFException {
  250. try {
  251. handler.endElement(EL_PAGE_SEQUENCE);
  252. } catch (SAXException e) {
  253. throw new IFException("SAX error in endPageSequence()", e);
  254. }
  255. }
  256. /** {@inheritDoc} */
  257. public void startPage(int index, String name, String pageMasterName, Dimension size)
  258. throws IFException {
  259. try {
  260. AttributesImpl atts = new AttributesImpl();
  261. addAttribute(atts, "index", Integer.toString(index));
  262. addAttribute(atts, "name", name);
  263. if (pageMasterName != null) {
  264. //fox:external-document doesn't have a page-master
  265. addAttribute(atts, "page-master-name", pageMasterName);
  266. }
  267. addAttribute(atts, "width", Integer.toString(size.width));
  268. addAttribute(atts, "height", Integer.toString(size.height));
  269. addForeignAttributes(atts);
  270. getContext().setPageIndex(index);
  271. handler.startElement(EL_PAGE, atts);
  272. } catch (SAXException e) {
  273. throw new IFException("SAX error in startPage()", e);
  274. }
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. public void startPageHeader() throws IFException {
  279. try {
  280. handler.startElement(EL_PAGE_HEADER);
  281. if (this.getUserAgent().isAccessibilityEnabled()) {
  282. structureTreeBuilder.replayEventsForRetrievedMarkers(handler);
  283. }
  284. } catch (SAXException e) {
  285. throw new IFException("SAX error in startPageHeader()", e);
  286. }
  287. }
  288. /** {@inheritDoc} */
  289. @Override
  290. public void endPageHeader() throws IFException {
  291. try {
  292. handler.endElement(EL_PAGE_HEADER);
  293. } catch (SAXException e) {
  294. throw new IFException("SAX error in endPageHeader()", e);
  295. }
  296. }
  297. /** {@inheritDoc} */
  298. public IFPainter startPageContent() throws IFException {
  299. try {
  300. handler.startElement(EL_PAGE_CONTENT);
  301. this.state = IFState.create();
  302. return this;
  303. } catch (SAXException e) {
  304. throw new IFException("SAX error in startPageContent()", e);
  305. }
  306. }
  307. /** {@inheritDoc} */
  308. public void endPageContent() throws IFException {
  309. try {
  310. this.state = null;
  311. currentID = "";
  312. handler.endElement(EL_PAGE_CONTENT);
  313. } catch (SAXException e) {
  314. throw new IFException("SAX error in endPageContent()", e);
  315. }
  316. }
  317. /** {@inheritDoc} */
  318. @Override
  319. public void startPageTrailer() throws IFException {
  320. try {
  321. handler.startElement(EL_PAGE_TRAILER);
  322. } catch (SAXException e) {
  323. throw new IFException("SAX error in startPageTrailer()", e);
  324. }
  325. }
  326. /** {@inheritDoc} */
  327. @Override
  328. public void endPageTrailer() throws IFException {
  329. try {
  330. commitNavigation();
  331. handler.endElement(EL_PAGE_TRAILER);
  332. } catch (SAXException e) {
  333. throw new IFException("SAX error in endPageTrailer()", e);
  334. }
  335. }
  336. /** {@inheritDoc} */
  337. public void endPage() throws IFException {
  338. try {
  339. handler.endElement(EL_PAGE);
  340. getContext().setPageIndex(-1);
  341. } catch (SAXException e) {
  342. throw new IFException("SAX error in endPage()", e);
  343. }
  344. }
  345. //---=== IFPainter ===---
  346. /** {@inheritDoc} */
  347. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  348. throws IFException {
  349. startViewport(IFUtil.toString(transform), size, clipRect);
  350. }
  351. /** {@inheritDoc} */
  352. public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
  353. throws IFException {
  354. startViewport(IFUtil.toString(transforms), size, clipRect);
  355. }
  356. private void startViewport(String transform, Dimension size, Rectangle clipRect)
  357. throws IFException {
  358. try {
  359. AttributesImpl atts = new AttributesImpl();
  360. if (transform != null && transform.length() > 0) {
  361. addAttribute(atts, "transform", transform);
  362. }
  363. addAttribute(atts, "width", Integer.toString(size.width));
  364. addAttribute(atts, "height", Integer.toString(size.height));
  365. if (clipRect != null) {
  366. addAttribute(atts, "clip-rect", IFUtil.toString(clipRect));
  367. }
  368. handler.startElement(EL_VIEWPORT, atts);
  369. } catch (SAXException e) {
  370. throw new IFException("SAX error in startViewport()", e);
  371. }
  372. }
  373. /** {@inheritDoc} */
  374. public void endViewport() throws IFException {
  375. try {
  376. handler.endElement(EL_VIEWPORT);
  377. } catch (SAXException e) {
  378. throw new IFException("SAX error in endViewport()", e);
  379. }
  380. }
  381. /** {@inheritDoc} */
  382. public void startGroup(AffineTransform[] transforms, String layer) throws IFException {
  383. startGroup(IFUtil.toString(transforms), layer);
  384. }
  385. /** {@inheritDoc} */
  386. public void startGroup(AffineTransform transform, String layer) throws IFException {
  387. startGroup(IFUtil.toString(transform), layer);
  388. }
  389. private void startGroup(String transform, String layer) throws IFException {
  390. try {
  391. AttributesImpl atts = new AttributesImpl();
  392. if (transform != null && transform.length() > 0) {
  393. addAttribute(atts, "transform", transform);
  394. }
  395. if (layer != null && layer.length() > 0) {
  396. addAttribute(atts, "layer", layer);
  397. }
  398. handler.startElement(EL_GROUP, atts);
  399. } catch (SAXException e) {
  400. throw new IFException("SAX error in startGroup()", e);
  401. }
  402. }
  403. /** {@inheritDoc} */
  404. public void endGroup() throws IFException {
  405. try {
  406. handler.endElement(EL_GROUP);
  407. } catch (SAXException e) {
  408. throw new IFException("SAX error in endGroup()", e);
  409. }
  410. }
  411. /** {@inheritDoc} */
  412. public void drawImage(String uri, Rectangle rect) throws IFException {
  413. try {
  414. addID();
  415. AttributesImpl atts = new AttributesImpl();
  416. addAttribute(atts, XLINK_HREF, uri);
  417. addAttribute(atts, "x", Integer.toString(rect.x));
  418. addAttribute(atts, "y", Integer.toString(rect.y));
  419. addAttribute(atts, "width", Integer.toString(rect.width));
  420. addAttribute(atts, "height", Integer.toString(rect.height));
  421. addForeignAttributes(atts);
  422. addStructureReference(atts);
  423. handler.element(EL_IMAGE, atts);
  424. } catch (SAXException e) {
  425. throw new IFException("SAX error in startGroup()", e);
  426. } finally {
  427. ImageSessionContext session = getUserAgent().getImageSessionContext();
  428. ImageManager imageManager = getUserAgent().getImageManager();
  429. imageManager.closeImage(uri, session);
  430. }
  431. }
  432. private void addForeignAttributes(AttributesImpl atts) throws SAXException {
  433. Map foreignAttributes = getContext().getForeignAttributes();
  434. if (!foreignAttributes.isEmpty()) {
  435. for (Object o : foreignAttributes.entrySet()) {
  436. Map.Entry entry = (Map.Entry) o;
  437. addAttribute(atts, (QName) entry.getKey(), entry.getValue().toString());
  438. }
  439. }
  440. }
  441. /** {@inheritDoc} */
  442. public void drawImage(Document doc, Rectangle rect) throws IFException {
  443. try {
  444. addID();
  445. AttributesImpl atts = new AttributesImpl();
  446. addAttribute(atts, "x", Integer.toString(rect.x));
  447. addAttribute(atts, "y", Integer.toString(rect.y));
  448. addAttribute(atts, "width", Integer.toString(rect.width));
  449. addAttribute(atts, "height", Integer.toString(rect.height));
  450. addForeignAttributes(atts);
  451. addStructureReference(atts);
  452. handler.startElement(EL_IMAGE, atts);
  453. new DOM2SAX(handler).writeDocument(doc, true);
  454. handler.endElement(EL_IMAGE);
  455. } catch (SAXException e) {
  456. throw new IFException("SAX error in startGroup()", e);
  457. }
  458. }
  459. private static String toString(Paint paint) {
  460. if (paint instanceof Color) {
  461. return ColorUtil.colorToString((Color)paint);
  462. } else {
  463. throw new UnsupportedOperationException("Paint not supported: " + paint);
  464. }
  465. }
  466. /** {@inheritDoc} */
  467. public void clipRect(Rectangle rect) throws IFException {
  468. try {
  469. AttributesImpl atts = new AttributesImpl();
  470. addAttribute(atts, "x", Integer.toString(rect.x));
  471. addAttribute(atts, "y", Integer.toString(rect.y));
  472. addAttribute(atts, "width", Integer.toString(rect.width));
  473. addAttribute(atts, "height", Integer.toString(rect.height));
  474. handler.element(EL_CLIP_RECT, atts);
  475. } catch (SAXException e) {
  476. throw new IFException("SAX error in clipRect()", e);
  477. }
  478. }
  479. /** {@inheritDoc} */
  480. public void clipBackground(Rectangle rect, BorderProps bpsBefore, BorderProps bpsAfter,
  481. BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
  482. try {
  483. AttributesImpl atts = new AttributesImpl();
  484. addAttribute(atts, "x", Integer.toString(rect.x));
  485. addAttribute(atts, "y", Integer.toString(rect.y));
  486. addAttribute(atts, "width", Integer.toString(rect.width));
  487. addAttribute(atts, "height", Integer.toString(rect.height));
  488. if (hasRoundedCorners(bpsBefore, bpsAfter, bpsStart, bpsEnd)) {
  489. if (bpsBefore != null) {
  490. addAttribute(atts, "top", bpsBefore.toString());
  491. }
  492. if (bpsAfter != null) {
  493. addAttribute(atts, "bottom", bpsAfter.toString());
  494. }
  495. if (bpsStart != null) {
  496. addAttribute(atts, "left", bpsStart.toString());
  497. }
  498. if (bpsEnd != null) {
  499. addAttribute(atts, "right", bpsEnd.toString());
  500. }
  501. }
  502. handler.element(EL_CLIP_RECT, atts);
  503. } catch (SAXException e) {
  504. throw new IFException("SAX error in clipRect()", e);
  505. }
  506. }
  507. /** {@inheritDoc} */
  508. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  509. if (fill == null) {
  510. return;
  511. }
  512. try {
  513. AttributesImpl atts = new AttributesImpl();
  514. addAttribute(atts, "x", Integer.toString(rect.x));
  515. addAttribute(atts, "y", Integer.toString(rect.y));
  516. addAttribute(atts, "width", Integer.toString(rect.width));
  517. addAttribute(atts, "height", Integer.toString(rect.height));
  518. addAttribute(atts, "fill", toString(fill));
  519. handler.element(EL_RECT, atts);
  520. } catch (SAXException e) {
  521. throw new IFException("SAX error in fillRect()", e);
  522. }
  523. }
  524. //TODO create a class representing all borders should exist
  525. //with query methods like this
  526. private boolean hasRoundedCorners(BorderProps bpsBefore, BorderProps bpsAfter,
  527. BorderProps bpsStart, BorderProps bpsEnd) {
  528. boolean rtn = false;
  529. if (bpsBefore != null && bpsBefore.getRadiusStart() > 0
  530. && bpsStart != null && bpsStart.getRadiusStart() > 0) {
  531. rtn = true;
  532. }
  533. if (bpsBefore != null && bpsBefore.getRadiusEnd() > 0
  534. && bpsEnd != null && bpsEnd.getRadiusStart() > 0) {
  535. rtn = true;
  536. }
  537. if (bpsEnd != null && bpsEnd.getRadiusEnd() > 0
  538. && bpsAfter != null && bpsAfter.getRadiusEnd() > 0) {
  539. rtn = true;
  540. }
  541. if (bpsAfter != null && bpsAfter.getRadiusStart() > 0
  542. && bpsStart != null && bpsStart.getRadiusEnd() > 0) {
  543. rtn = true;
  544. }
  545. return rtn;
  546. }
  547. /** {@inheritDoc} */
  548. public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom,
  549. BorderProps left, BorderProps right, Color innerBackgroundColor) throws IFException {
  550. if (top == null && bottom == null && left == null && right == null) {
  551. return;
  552. }
  553. try {
  554. AttributesImpl atts = new AttributesImpl();
  555. addAttribute(atts, "x", Integer.toString(rect.x));
  556. addAttribute(atts, "y", Integer.toString(rect.y));
  557. addAttribute(atts, "width", Integer.toString(rect.width));
  558. addAttribute(atts, "height", Integer.toString(rect.height));
  559. if (top != null) {
  560. addAttribute(atts, "top", top.toString());
  561. }
  562. if (bottom != null) {
  563. addAttribute(atts, "bottom", bottom.toString());
  564. }
  565. if (left != null) {
  566. addAttribute(atts, "left", left.toString());
  567. }
  568. if (right != null) {
  569. addAttribute(atts, "right", right.toString());
  570. }
  571. if (innerBackgroundColor != null) {
  572. addAttribute(atts, "inner-background-color",
  573. ColorUtil.colorToString(innerBackgroundColor));
  574. }
  575. handler.element(EL_BORDER_RECT, atts);
  576. } catch (SAXException e) {
  577. throw new IFException("SAX error in drawBorderRect()", e);
  578. }
  579. }
  580. /** {@inheritDoc} */
  581. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  582. throws IFException {
  583. try {
  584. addID();
  585. AttributesImpl atts = new AttributesImpl();
  586. addAttribute(atts, "x1", Integer.toString(start.x));
  587. addAttribute(atts, "y1", Integer.toString(start.y));
  588. addAttribute(atts, "x2", Integer.toString(end.x));
  589. addAttribute(atts, "y2", Integer.toString(end.y));
  590. addAttribute(atts, "stroke-width", Integer.toString(width));
  591. addAttribute(atts, "color", ColorUtil.colorToString(color));
  592. addAttribute(atts, "style", style.getName());
  593. handler.element(EL_LINE, atts);
  594. } catch (SAXException e) {
  595. throw new IFException("SAX error in drawLine()", e);
  596. }
  597. }
  598. /** {@inheritDoc} */
  599. public void drawText(int x, int y, int letterSpacing, int wordSpacing,
  600. int[][] dp, String text) throws IFException {
  601. drawText(x, y, letterSpacing, wordSpacing, dp, text, false);
  602. }
  603. /** {@inheritDoc} */
  604. public void drawText(int x, int y, int letterSpacing, int wordSpacing,
  605. int[][] dp, String text, boolean nextIsSpace) throws IFException {
  606. try {
  607. addID();
  608. AttributesImpl atts = new AttributesImpl();
  609. addAttribute(atts, "x", Integer.toString(x));
  610. addAttribute(atts, "y", Integer.toString(y));
  611. if (nextIsSpace) {
  612. addAttribute(atts, "next-is-space", "true");
  613. }
  614. if (letterSpacing != 0) {
  615. addAttribute(atts, "letter-spacing", Integer.toString(letterSpacing));
  616. }
  617. if (wordSpacing != 0) {
  618. addAttribute(atts, "word-spacing", Integer.toString(wordSpacing));
  619. }
  620. if (dp != null) {
  621. if (IFUtil.isDPIdentity(dp)) {
  622. // don't add dx or dp attribute
  623. } else if (IFUtil.isDPOnlyDX(dp)) {
  624. // add dx attribute only
  625. int[] dx = IFUtil.convertDPToDX(dp);
  626. addAttribute(atts, "dx", IFUtil.toString(dx));
  627. } else {
  628. // add dp attribute only
  629. addAttribute(atts, "dp", XMLUtil.encodePositionAdjustments(dp));
  630. }
  631. }
  632. addStructureReference(atts);
  633. if (getContext().isHyphenated()) {
  634. addAttribute(atts, "hyphenated", "true");
  635. }
  636. handler.startElement(EL_TEXT, atts);
  637. char[] chars = text.toCharArray();
  638. handler.characters(chars, 0, chars.length);
  639. handler.endElement(EL_TEXT);
  640. } catch (SAXException e) {
  641. throw new IFException("SAX error in setFont()", e);
  642. }
  643. }
  644. /** {@inheritDoc} */
  645. public void setFont(String family, String style, Integer weight, String variant, Integer size,
  646. Color color) throws IFException {
  647. try {
  648. AttributesImpl atts = new AttributesImpl();
  649. boolean changed;
  650. if (family != null) {
  651. changed = !family.equals(state.getFontFamily());
  652. if (changed) {
  653. state.setFontFamily(family);
  654. addAttribute(atts, "family", family);
  655. }
  656. }
  657. if (style != null) {
  658. changed = !style.equals(state.getFontStyle());
  659. if (changed) {
  660. state.setFontStyle(style);
  661. addAttribute(atts, "style", style);
  662. }
  663. }
  664. if (weight != null) {
  665. changed = (weight != state.getFontWeight());
  666. if (changed) {
  667. state.setFontWeight(weight);
  668. addAttribute(atts, "weight", weight.toString());
  669. }
  670. }
  671. if (variant != null) {
  672. changed = !variant.equals(state.getFontVariant());
  673. if (changed) {
  674. state.setFontVariant(variant);
  675. addAttribute(atts, "variant", variant);
  676. }
  677. }
  678. if (size != null) {
  679. changed = (size != state.getFontSize());
  680. if (changed) {
  681. state.setFontSize(size);
  682. addAttribute(atts, "size", size.toString());
  683. }
  684. }
  685. if (color != null) {
  686. changed = !org.apache.xmlgraphics.java2d.color.ColorUtil.isSameColor(
  687. color, state.getTextColor());
  688. if (changed) {
  689. state.setTextColor(color);
  690. addAttribute(atts, "color", toString(color));
  691. }
  692. }
  693. if (atts.getLength() > 0) {
  694. handler.element(EL_FONT, atts);
  695. }
  696. } catch (SAXException e) {
  697. throw new IFException("SAX error in setFont()", e);
  698. }
  699. }
  700. /** {@inheritDoc} */
  701. public void handleExtensionObject(Object extension) throws IFException {
  702. if (extension instanceof XMLizable) {
  703. try {
  704. ((XMLizable)extension).toSAX(this.handler);
  705. } catch (SAXException e) {
  706. throw new IFException("SAX error while handling extension object", e);
  707. }
  708. } else {
  709. throw new UnsupportedOperationException(
  710. "Extension must implement XMLizable: "
  711. + extension + " (" + extension.getClass().getName() + ")");
  712. }
  713. }
  714. /**
  715. * @return a new rendering context
  716. * @throws IllegalStateException unless overridden
  717. */
  718. protected RenderingContext createRenderingContext() throws IllegalStateException {
  719. throw new IllegalStateException("Should never be called!");
  720. }
  721. private void addAttribute(AttributesImpl atts,
  722. org.apache.xmlgraphics.util.QName attribute, String value) throws SAXException {
  723. handler.startPrefixMapping(attribute.getPrefix(), attribute.getNamespaceURI());
  724. XMLUtil.addAttribute(atts, attribute, value);
  725. }
  726. private void addAttribute(AttributesImpl atts, String localName, String value) {
  727. XMLUtil.addAttribute(atts, localName, value);
  728. }
  729. private void addStructureReference(AttributesImpl atts) {
  730. IFStructureTreeElement structureTreeElement
  731. = (IFStructureTreeElement) getContext().getStructureTreeElement();
  732. if (structureTreeElement != null) {
  733. addStructRefAttribute(atts, structureTreeElement.getId());
  734. }
  735. }
  736. private void addStructRefAttribute(AttributesImpl atts, String id) {
  737. atts.addAttribute(InternalElementMapping.URI,
  738. InternalElementMapping.STRUCT_REF,
  739. InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_REF,
  740. XMLConstants.CDATA,
  741. id);
  742. }
  743. private void addID() throws SAXException {
  744. String id = getContext().getID();
  745. if (!currentID.equals(id)) {
  746. AttributesImpl atts = new AttributesImpl();
  747. addAttribute(atts, "name", id);
  748. handler.startElement(EL_ID, atts);
  749. handler.endElement(EL_ID);
  750. currentID = id;
  751. }
  752. }
  753. private Map incompleteActions = new java.util.HashMap();
  754. private List completeActions = new java.util.LinkedList();
  755. private void noteAction(AbstractAction action) {
  756. if (action == null) {
  757. throw new NullPointerException("action must not be null");
  758. }
  759. if (!action.isComplete()) {
  760. assert action.hasID();
  761. incompleteActions.put(action.getID(), action);
  762. }
  763. }
  764. /** {@inheritDoc} */
  765. public void renderNamedDestination(NamedDestination destination) throws IFException {
  766. noteAction(destination.getAction());
  767. AttributesImpl atts = new AttributesImpl();
  768. atts.addAttribute("", "name", "name", XMLConstants.CDATA, destination.getName());
  769. try {
  770. handler.startElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION, atts);
  771. serializeXMLizable(destination.getAction());
  772. handler.endElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION);
  773. } catch (SAXException e) {
  774. throw new IFException("SAX error serializing named destination", e);
  775. }
  776. }
  777. /** {@inheritDoc} */
  778. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  779. AttributesImpl atts = new AttributesImpl();
  780. try {
  781. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE, atts);
  782. for (Object o : tree.getBookmarks()) {
  783. Bookmark b = (Bookmark) o;
  784. if (b.getAction() != null) {
  785. serializeBookmark(b);
  786. }
  787. }
  788. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE);
  789. } catch (SAXException e) {
  790. throw new IFException("SAX error serializing bookmark tree", e);
  791. }
  792. }
  793. private void serializeBookmark(Bookmark bookmark) throws SAXException, IFException {
  794. noteAction(bookmark.getAction());
  795. AttributesImpl atts = new AttributesImpl();
  796. atts.addAttribute("", "title", "title", XMLUtil.CDATA, bookmark.getTitle());
  797. atts.addAttribute("", "starting-state", "starting-state",
  798. XMLUtil.CDATA, bookmark.isShown() ? "show" : "hide");
  799. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK, atts);
  800. serializeXMLizable(bookmark.getAction());
  801. for (Object o : bookmark.getChildBookmarks()) {
  802. Bookmark b = (Bookmark) o;
  803. if (b.getAction() != null) {
  804. serializeBookmark(b);
  805. }
  806. }
  807. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK);
  808. }
  809. /** {@inheritDoc} */
  810. public void renderLink(Link link) throws IFException {
  811. noteAction(link.getAction());
  812. AttributesImpl atts = new AttributesImpl();
  813. atts.addAttribute("", "rect", "rect",
  814. XMLConstants.CDATA, IFUtil.toString(link.getTargetRect()));
  815. if (getUserAgent().isAccessibilityEnabled()) {
  816. addStructRefAttribute(atts,
  817. ((IFStructureTreeElement) link.getAction().getStructureTreeElement()).getId());
  818. }
  819. try {
  820. handler.startElement(DocumentNavigationExtensionConstants.LINK, atts);
  821. serializeXMLizable(link.getAction());
  822. handler.endElement(DocumentNavigationExtensionConstants.LINK);
  823. } catch (SAXException e) {
  824. throw new IFException("SAX error serializing link", e);
  825. }
  826. }
  827. /** {@inheritDoc} */
  828. public void addResolvedAction(AbstractAction action) throws IFException {
  829. assert action.isComplete();
  830. assert action.hasID();
  831. AbstractAction noted = (AbstractAction)incompleteActions.remove(action.getID());
  832. if (noted != null) {
  833. completeActions.add(action);
  834. } else {
  835. //ignore as it was already complete when it was first used.
  836. }
  837. }
  838. public int getPageIndex() {
  839. return -1;
  840. }
  841. private void commitNavigation() throws IFException {
  842. Iterator iter = this.completeActions.iterator();
  843. while (iter.hasNext()) {
  844. AbstractAction action = (AbstractAction)iter.next();
  845. iter.remove();
  846. serializeXMLizable(action);
  847. }
  848. assert this.completeActions.size() == 0;
  849. }
  850. private void finishDocumentNavigation() {
  851. assert this.incompleteActions.size() == 0 : "Still holding incomplete actions!";
  852. }
  853. private void serializeXMLizable(XMLizable object) throws IFException {
  854. try {
  855. object.toSAX(handler);
  856. } catch (SAXException e) {
  857. throw new IFException("SAX error serializing object", e);
  858. }
  859. }
  860. /** {@inheritDoc} */
  861. public boolean isBackgroundRequired(BorderProps bpsTop, BorderProps bpsBottom,
  862. BorderProps bpsLeft, BorderProps bpsRight) {
  863. return true;
  864. }
  865. }