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

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