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

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