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

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