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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. Iterator iter = foreignAttributes.entrySet().iterator();
  436. while (iter.hasNext()) {
  437. Map.Entry entry = (Map.Entry)iter.next();
  438. addAttribute(atts, (QName)entry.getKey(), entry.getValue().toString());
  439. }
  440. }
  441. }
  442. /** {@inheritDoc} */
  443. public void drawImage(Document doc, Rectangle rect) throws IFException {
  444. try {
  445. addID();
  446. AttributesImpl atts = new AttributesImpl();
  447. addAttribute(atts, "x", Integer.toString(rect.x));
  448. addAttribute(atts, "y", Integer.toString(rect.y));
  449. addAttribute(atts, "width", Integer.toString(rect.width));
  450. addAttribute(atts, "height", Integer.toString(rect.height));
  451. addForeignAttributes(atts);
  452. addStructureReference(atts);
  453. handler.startElement(EL_IMAGE, atts);
  454. new DOM2SAX(handler).writeDocument(doc, true);
  455. handler.endElement(EL_IMAGE);
  456. } catch (SAXException e) {
  457. throw new IFException("SAX error in startGroup()", e);
  458. }
  459. }
  460. private static String toString(Paint paint) {
  461. if (paint instanceof Color) {
  462. return ColorUtil.colorToString((Color)paint);
  463. } else {
  464. throw new UnsupportedOperationException("Paint not supported: " + paint);
  465. }
  466. }
  467. /** {@inheritDoc} */
  468. public void clipRect(Rectangle rect) throws IFException {
  469. try {
  470. AttributesImpl atts = new AttributesImpl();
  471. addAttribute(atts, "x", Integer.toString(rect.x));
  472. addAttribute(atts, "y", Integer.toString(rect.y));
  473. addAttribute(atts, "width", Integer.toString(rect.width));
  474. addAttribute(atts, "height", Integer.toString(rect.height));
  475. handler.element(EL_CLIP_RECT, atts);
  476. } catch (SAXException e) {
  477. throw new IFException("SAX error in clipRect()", e);
  478. }
  479. }
  480. /** {@inheritDoc} */
  481. public void clipBackground(Rectangle rect, BorderProps bpsBefore, BorderProps bpsAfter,
  482. BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
  483. try {
  484. AttributesImpl atts = new AttributesImpl();
  485. addAttribute(atts, "x", Integer.toString(rect.x));
  486. addAttribute(atts, "y", Integer.toString(rect.y));
  487. addAttribute(atts, "width", Integer.toString(rect.width));
  488. addAttribute(atts, "height", Integer.toString(rect.height));
  489. if (hasRoundedCorners(bpsBefore, bpsAfter, bpsStart, bpsEnd)) {
  490. if (bpsBefore != null) {
  491. addAttribute(atts, "top", bpsBefore.toString());
  492. }
  493. if (bpsAfter != null) {
  494. addAttribute(atts, "bottom", bpsAfter.toString());
  495. }
  496. if (bpsStart != null) {
  497. addAttribute(atts, "left", bpsStart.toString());
  498. }
  499. if (bpsEnd != null) {
  500. addAttribute(atts, "right", bpsEnd.toString());
  501. }
  502. }
  503. handler.element(EL_CLIP_RECT, atts);
  504. } catch (SAXException e) {
  505. throw new IFException("SAX error in clipRect()", e);
  506. }
  507. }
  508. /** {@inheritDoc} */
  509. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  510. if (fill == null) {
  511. return;
  512. }
  513. try {
  514. AttributesImpl atts = new AttributesImpl();
  515. addAttribute(atts, "x", Integer.toString(rect.x));
  516. addAttribute(atts, "y", Integer.toString(rect.y));
  517. addAttribute(atts, "width", Integer.toString(rect.width));
  518. addAttribute(atts, "height", Integer.toString(rect.height));
  519. addAttribute(atts, "fill", toString(fill));
  520. handler.element(EL_RECT, atts);
  521. } catch (SAXException e) {
  522. throw new IFException("SAX error in fillRect()", e);
  523. }
  524. }
  525. //TODO create a class representing all borders should exist
  526. //with query methods like this
  527. private boolean hasRoundedCorners(BorderProps bpsBefore, BorderProps bpsAfter,
  528. BorderProps bpsStart, BorderProps bpsEnd) {
  529. boolean rtn = false;
  530. if (bpsBefore != null && bpsBefore.getRadiusStart() > 0
  531. && bpsStart != null && bpsStart.getRadiusStart() > 0) {
  532. rtn = true;
  533. }
  534. if (bpsBefore != null && bpsBefore.getRadiusEnd() > 0
  535. && bpsEnd != null && bpsEnd.getRadiusStart() > 0) {
  536. rtn = true;
  537. }
  538. if (bpsEnd != null && bpsEnd.getRadiusEnd() > 0
  539. && bpsAfter != null && bpsAfter.getRadiusEnd() > 0) {
  540. rtn = true;
  541. }
  542. if (bpsAfter != null && bpsAfter.getRadiusStart() > 0
  543. && bpsStart != null && bpsStart.getRadiusEnd() > 0) {
  544. rtn = true;
  545. }
  546. return rtn;
  547. }
  548. /** {@inheritDoc} */
  549. public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom,
  550. BorderProps left, BorderProps right, Color innerBackgroundColor) throws IFException {
  551. if (top == null && bottom == null && left == null && right == null) {
  552. return;
  553. }
  554. try {
  555. AttributesImpl atts = new AttributesImpl();
  556. addAttribute(atts, "x", Integer.toString(rect.x));
  557. addAttribute(atts, "y", Integer.toString(rect.y));
  558. addAttribute(atts, "width", Integer.toString(rect.width));
  559. addAttribute(atts, "height", Integer.toString(rect.height));
  560. if (top != null) {
  561. addAttribute(atts, "top", top.toString());
  562. }
  563. if (bottom != null) {
  564. addAttribute(atts, "bottom", bottom.toString());
  565. }
  566. if (left != null) {
  567. addAttribute(atts, "left", left.toString());
  568. }
  569. if (right != null) {
  570. addAttribute(atts, "right", right.toString());
  571. }
  572. if (innerBackgroundColor != null) {
  573. addAttribute(atts, "inner-background-color",
  574. ColorUtil.colorToString(innerBackgroundColor));
  575. }
  576. handler.element(EL_BORDER_RECT, atts);
  577. } catch (SAXException e) {
  578. throw new IFException("SAX error in drawBorderRect()", e);
  579. }
  580. }
  581. /** {@inheritDoc} */
  582. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  583. throws IFException {
  584. try {
  585. addID();
  586. AttributesImpl atts = new AttributesImpl();
  587. addAttribute(atts, "x1", Integer.toString(start.x));
  588. addAttribute(atts, "y1", Integer.toString(start.y));
  589. addAttribute(atts, "x2", Integer.toString(end.x));
  590. addAttribute(atts, "y2", Integer.toString(end.y));
  591. addAttribute(atts, "stroke-width", Integer.toString(width));
  592. addAttribute(atts, "color", ColorUtil.colorToString(color));
  593. addAttribute(atts, "style", style.getName());
  594. handler.element(EL_LINE, atts);
  595. } catch (SAXException e) {
  596. throw new IFException("SAX error in drawLine()", e);
  597. }
  598. }
  599. /** {@inheritDoc} */
  600. public void drawText(int x, int y, int letterSpacing, int wordSpacing,
  601. int[][] dp, String text) throws IFException {
  602. try {
  603. addID();
  604. AttributesImpl atts = new AttributesImpl();
  605. addAttribute(atts, "x", Integer.toString(x));
  606. addAttribute(atts, "y", Integer.toString(y));
  607. if (letterSpacing != 0) {
  608. addAttribute(atts, "letter-spacing", Integer.toString(letterSpacing));
  609. }
  610. if (wordSpacing != 0) {
  611. addAttribute(atts, "word-spacing", Integer.toString(wordSpacing));
  612. }
  613. if (dp != null) {
  614. if (IFUtil.isDPIdentity(dp)) {
  615. // don't add dx or dp attribute
  616. } else if (IFUtil.isDPOnlyDX(dp)) {
  617. // add dx attribute only
  618. int[] dx = IFUtil.convertDPToDX(dp);
  619. addAttribute(atts, "dx", IFUtil.toString(dx));
  620. } else {
  621. // add dp attribute only
  622. addAttribute(atts, "dp", XMLUtil.encodePositionAdjustments(dp));
  623. }
  624. }
  625. addStructureReference(atts);
  626. if (getContext().isHyphenated()) {
  627. addAttribute(atts, "hyphenated", "true");
  628. }
  629. handler.startElement(EL_TEXT, atts);
  630. char[] chars = text.toCharArray();
  631. handler.characters(chars, 0, chars.length);
  632. handler.endElement(EL_TEXT);
  633. } catch (SAXException e) {
  634. throw new IFException("SAX error in setFont()", e);
  635. }
  636. }
  637. /** {@inheritDoc} */
  638. public void setFont(String family, String style, Integer weight, String variant, Integer size,
  639. Color color) throws IFException {
  640. try {
  641. AttributesImpl atts = new AttributesImpl();
  642. boolean changed;
  643. if (family != null) {
  644. changed = !family.equals(state.getFontFamily());
  645. if (changed) {
  646. state.setFontFamily(family);
  647. addAttribute(atts, "family", family);
  648. }
  649. }
  650. if (style != null) {
  651. changed = !style.equals(state.getFontStyle());
  652. if (changed) {
  653. state.setFontStyle(style);
  654. addAttribute(atts, "style", style);
  655. }
  656. }
  657. if (weight != null) {
  658. changed = (weight != state.getFontWeight());
  659. if (changed) {
  660. state.setFontWeight(weight);
  661. addAttribute(atts, "weight", weight.toString());
  662. }
  663. }
  664. if (variant != null) {
  665. changed = !variant.equals(state.getFontVariant());
  666. if (changed) {
  667. state.setFontVariant(variant);
  668. addAttribute(atts, "variant", variant);
  669. }
  670. }
  671. if (size != null) {
  672. changed = (size != state.getFontSize());
  673. if (changed) {
  674. state.setFontSize(size);
  675. addAttribute(atts, "size", size.toString());
  676. }
  677. }
  678. if (color != null) {
  679. changed = !org.apache.xmlgraphics.java2d.color.ColorUtil.isSameColor(
  680. color, state.getTextColor());
  681. if (changed) {
  682. state.setTextColor(color);
  683. addAttribute(atts, "color", toString(color));
  684. }
  685. }
  686. if (atts.getLength() > 0) {
  687. handler.element(EL_FONT, atts);
  688. }
  689. } catch (SAXException e) {
  690. throw new IFException("SAX error in setFont()", e);
  691. }
  692. }
  693. /** {@inheritDoc} */
  694. public void handleExtensionObject(Object extension) throws IFException {
  695. if (extension instanceof XMLizable) {
  696. try {
  697. ((XMLizable)extension).toSAX(this.handler);
  698. } catch (SAXException e) {
  699. throw new IFException("SAX error while handling extension object", e);
  700. }
  701. } else {
  702. throw new UnsupportedOperationException(
  703. "Extension must implement XMLizable: "
  704. + extension + " (" + extension.getClass().getName() + ")");
  705. }
  706. }
  707. /**
  708. * @return a new rendering context
  709. * @throws IllegalStateException unless overridden
  710. */
  711. protected RenderingContext createRenderingContext() throws IllegalStateException {
  712. throw new IllegalStateException("Should never be called!");
  713. }
  714. private void addAttribute(AttributesImpl atts,
  715. org.apache.xmlgraphics.util.QName attribute, String value) throws SAXException {
  716. handler.startPrefixMapping(attribute.getPrefix(), attribute.getNamespaceURI());
  717. XMLUtil.addAttribute(atts, attribute, value);
  718. }
  719. private void addAttribute(AttributesImpl atts, String localName, String value) {
  720. XMLUtil.addAttribute(atts, localName, value);
  721. }
  722. private void addStructureReference(AttributesImpl atts) {
  723. IFStructureTreeElement structureTreeElement
  724. = (IFStructureTreeElement) getContext().getStructureTreeElement();
  725. if (structureTreeElement != null) {
  726. addStructRefAttribute(atts, structureTreeElement.getId());
  727. }
  728. }
  729. private void addStructRefAttribute(AttributesImpl atts, String id) {
  730. atts.addAttribute(InternalElementMapping.URI,
  731. InternalElementMapping.STRUCT_REF,
  732. InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_REF,
  733. XMLConstants.CDATA,
  734. id);
  735. }
  736. private void addID() throws SAXException {
  737. String id = getContext().getID();
  738. if (!currentID.equals(id)) {
  739. AttributesImpl atts = new AttributesImpl();
  740. addAttribute(atts, "name", id);
  741. handler.startElement(EL_ID, atts);
  742. handler.endElement(EL_ID);
  743. currentID = id;
  744. }
  745. }
  746. private Map incompleteActions = new java.util.HashMap();
  747. private List completeActions = new java.util.LinkedList();
  748. private void noteAction(AbstractAction action) {
  749. if (action == null) {
  750. throw new NullPointerException("action must not be null");
  751. }
  752. if (!action.isComplete()) {
  753. assert action.hasID();
  754. incompleteActions.put(action.getID(), action);
  755. }
  756. }
  757. /** {@inheritDoc} */
  758. public void renderNamedDestination(NamedDestination destination) throws IFException {
  759. noteAction(destination.getAction());
  760. AttributesImpl atts = new AttributesImpl();
  761. atts.addAttribute(null, "name", "name", XMLConstants.CDATA, destination.getName());
  762. try {
  763. handler.startElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION, atts);
  764. serializeXMLizable(destination.getAction());
  765. handler.endElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION);
  766. } catch (SAXException e) {
  767. throw new IFException("SAX error serializing named destination", e);
  768. }
  769. }
  770. /** {@inheritDoc} */
  771. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  772. AttributesImpl atts = new AttributesImpl();
  773. try {
  774. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE, atts);
  775. Iterator iter = tree.getBookmarks().iterator();
  776. while (iter.hasNext()) {
  777. Bookmark b = (Bookmark)iter.next();
  778. if (b.getAction() != null) {
  779. serializeBookmark(b);
  780. }
  781. }
  782. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE);
  783. } catch (SAXException e) {
  784. throw new IFException("SAX error serializing bookmark tree", e);
  785. }
  786. }
  787. private void serializeBookmark(Bookmark bookmark) throws SAXException, IFException {
  788. noteAction(bookmark.getAction());
  789. AttributesImpl atts = new AttributesImpl();
  790. atts.addAttribute(null, "title", "title", XMLUtil.CDATA, bookmark.getTitle());
  791. atts.addAttribute(null, "starting-state", "starting-state",
  792. XMLUtil.CDATA, bookmark.isShown() ? "show" : "hide");
  793. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK, atts);
  794. serializeXMLizable(bookmark.getAction());
  795. Iterator iter = bookmark.getChildBookmarks().iterator();
  796. while (iter.hasNext()) {
  797. Bookmark b = (Bookmark)iter.next();
  798. if (b.getAction() != null) {
  799. serializeBookmark(b);
  800. }
  801. }
  802. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK);
  803. }
  804. /** {@inheritDoc} */
  805. public void renderLink(Link link) throws IFException {
  806. noteAction(link.getAction());
  807. AttributesImpl atts = new AttributesImpl();
  808. atts.addAttribute(null, "rect", "rect",
  809. XMLConstants.CDATA, IFUtil.toString(link.getTargetRect()));
  810. if (getUserAgent().isAccessibilityEnabled()) {
  811. addStructRefAttribute(atts,
  812. ((IFStructureTreeElement) link.getAction().getStructureTreeElement()).getId());
  813. }
  814. try {
  815. handler.startElement(DocumentNavigationExtensionConstants.LINK, atts);
  816. serializeXMLizable(link.getAction());
  817. handler.endElement(DocumentNavigationExtensionConstants.LINK);
  818. } catch (SAXException e) {
  819. throw new IFException("SAX error serializing link", e);
  820. }
  821. }
  822. /** {@inheritDoc} */
  823. public void addResolvedAction(AbstractAction action) throws IFException {
  824. assert action.isComplete();
  825. assert action.hasID();
  826. AbstractAction noted = (AbstractAction)incompleteActions.remove(action.getID());
  827. if (noted != null) {
  828. completeActions.add(action);
  829. } else {
  830. //ignore as it was already complete when it was first used.
  831. }
  832. }
  833. private void commitNavigation() throws IFException {
  834. Iterator iter = this.completeActions.iterator();
  835. while (iter.hasNext()) {
  836. AbstractAction action = (AbstractAction)iter.next();
  837. iter.remove();
  838. serializeXMLizable(action);
  839. }
  840. assert this.completeActions.size() == 0;
  841. }
  842. private void finishDocumentNavigation() {
  843. assert this.incompleteActions.size() == 0 : "Still holding incomplete actions!";
  844. }
  845. private void serializeXMLizable(XMLizable object) throws IFException {
  846. try {
  847. object.toSAX(handler);
  848. } catch (SAXException e) {
  849. throw new IFException("SAX error serializing object", e);
  850. }
  851. }
  852. /** {@inheritDoc} */
  853. public boolean isBackgroundRequired(BorderProps bpsTop, BorderProps bpsBottom,
  854. BorderProps bpsLeft, BorderProps bpsRight) {
  855. return true;
  856. }
  857. }