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

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