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.

XMLRenderer.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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.xml;
  19. // Java
  20. import java.awt.Color;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.xml.transform.TransformerConfigurationException;
  28. import javax.xml.transform.sax.SAXTransformerFactory;
  29. import javax.xml.transform.sax.TransformerHandler;
  30. import javax.xml.transform.stream.StreamResult;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.NodeList;
  34. import org.xml.sax.SAXException;
  35. import org.apache.xmlgraphics.util.QName;
  36. import org.apache.xmlgraphics.util.XMLizable;
  37. import org.apache.fop.apps.FOPException;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.area.Area;
  41. import org.apache.fop.area.AreaTreeObject;
  42. import org.apache.fop.area.BeforeFloat;
  43. import org.apache.fop.area.Block;
  44. import org.apache.fop.area.BlockViewport;
  45. import org.apache.fop.area.BodyRegion;
  46. import org.apache.fop.area.BookmarkData;
  47. import org.apache.fop.area.CTM;
  48. import org.apache.fop.area.DestinationData;
  49. import org.apache.fop.area.Footnote;
  50. import org.apache.fop.area.LineArea;
  51. import org.apache.fop.area.MainReference;
  52. import org.apache.fop.area.NormalFlow;
  53. import org.apache.fop.area.OffDocumentExtensionAttachment;
  54. import org.apache.fop.area.OffDocumentItem;
  55. import org.apache.fop.area.PageSequence;
  56. import org.apache.fop.area.PageViewport;
  57. import org.apache.fop.area.RegionReference;
  58. import org.apache.fop.area.RegionViewport;
  59. import org.apache.fop.area.Span;
  60. import org.apache.fop.area.Trait;
  61. import org.apache.fop.area.Trait.Background;
  62. import org.apache.fop.area.Trait.InternalLink;
  63. import org.apache.fop.area.inline.Container;
  64. import org.apache.fop.area.inline.ForeignObject;
  65. import org.apache.fop.area.inline.Image;
  66. import org.apache.fop.area.inline.InlineArea;
  67. import org.apache.fop.area.inline.InlineBlockParent;
  68. import org.apache.fop.area.inline.InlineParent;
  69. import org.apache.fop.area.inline.Leader;
  70. import org.apache.fop.area.inline.Space;
  71. import org.apache.fop.area.inline.SpaceArea;
  72. import org.apache.fop.area.inline.TextArea;
  73. import org.apache.fop.area.inline.Viewport;
  74. import org.apache.fop.area.inline.WordArea;
  75. import org.apache.fop.fo.Constants;
  76. import org.apache.fop.fo.extensions.ExtensionAttachment;
  77. import org.apache.fop.fonts.FontInfo;
  78. import org.apache.fop.fonts.FontTriplet;
  79. import org.apache.fop.render.Renderer;
  80. import org.apache.fop.render.RendererContext;
  81. import org.apache.fop.render.XMLHandler;
  82. import org.apache.fop.util.ColorUtil;
  83. import org.apache.fop.util.DOM2SAX;
  84. /**
  85. * Renderer that renders areas to XML for debugging purposes.
  86. * This creates an xml that contains the information of the area
  87. * tree. It does not output any state or derived information.
  88. * The output can be used to build a new area tree which can be
  89. * rendered to any renderer.
  90. */
  91. public class XMLRenderer extends AbstractXMLRenderer {
  92. /** XML MIME type */
  93. public static final String XML_MIME_TYPE = MimeConstants.MIME_FOP_AREA_TREE;
  94. private boolean startedSequence = false;
  95. private boolean compactFormat = false;
  96. /** If not null, the XMLRenderer will mimic another renderer by using its font setup. */
  97. protected Renderer mimic;
  98. private int pageSequenceIndex;
  99. /**
  100. * Creates a new XML renderer.
  101. */
  102. public XMLRenderer() {
  103. context = new RendererContext(this, XML_MIME_TYPE);
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public void setUserAgent(FOUserAgent agent) {
  109. super.setUserAgent(agent);
  110. XMLHandler xmlHandler = new XMLXMLHandler();
  111. userAgent.getXMLHandlerRegistry().addXMLHandler(xmlHandler);
  112. Boolean b = (Boolean)userAgent.getRendererOptions().get("compact-format");
  113. if (b != null) {
  114. setCompactFormat(b.booleanValue());
  115. }
  116. }
  117. /**
  118. * Call this method to make the XMLRenderer mimic a different renderer by using its font
  119. * setup. This is useful when working with the intermediate format parser.
  120. * @param renderer the renderer to mimic
  121. */
  122. public void mimicRenderer(Renderer renderer) {
  123. this.mimic = renderer;
  124. }
  125. /** {@inheritDoc} */
  126. public void setupFontInfo(FontInfo inFontInfo) throws FOPException {
  127. if (mimic != null) {
  128. mimic.setupFontInfo(inFontInfo);
  129. } else {
  130. super.setupFontInfo(inFontInfo);
  131. }
  132. }
  133. public void setCompactFormat(boolean compact) {
  134. this.compactFormat = compact;
  135. }
  136. private boolean isDetailedFormat() {
  137. return !this.compactFormat;
  138. }
  139. /**
  140. * Adds the general Area attributes.
  141. * @param area Area to extract attributes from
  142. */
  143. protected void addAreaAttributes(Area area) {
  144. addAttribute("ipd", area.getIPD());
  145. addAttribute("bpd", area.getBPD());
  146. if (isDetailedFormat()) {
  147. if (area.getIPD() != 0) {
  148. addAttribute("ipda", area.getAllocIPD());
  149. }
  150. if (area.getBPD() != 0) {
  151. addAttribute("bpda", area.getAllocBPD());
  152. }
  153. addAttribute("bap", area.getBorderAndPaddingWidthStart() + " "
  154. + area.getBorderAndPaddingWidthEnd() + " "
  155. + area.getBorderAndPaddingWidthBefore() + " "
  156. + area.getBorderAndPaddingWidthAfter());
  157. }
  158. }
  159. /**
  160. * Adds attributes from traits of an Area.
  161. * @param area Area to extract traits from
  162. */
  163. protected void addTraitAttributes(Area area) {
  164. Map traitMap = area.getTraits();
  165. if (traitMap != null) {
  166. Iterator iter = traitMap.entrySet().iterator();
  167. while (iter.hasNext()) {
  168. Map.Entry traitEntry = (Map.Entry) iter.next();
  169. Object key = traitEntry.getKey();
  170. String name = Trait.getTraitName(key);
  171. Class clazz = Trait.getTraitClass(key);
  172. if ("break-before".equals(name) || "break-after".equals(name)) {
  173. continue;
  174. }
  175. Object value = traitEntry.getValue();
  176. if (key == Trait.FONT) {
  177. FontTriplet triplet = (FontTriplet)value;
  178. addAttribute("font-name", triplet.getName());
  179. addAttribute("font-style", triplet.getStyle());
  180. addAttribute("font-weight", triplet.getWeight());
  181. } else if (clazz.equals(InternalLink.class)) {
  182. InternalLink iLink = (InternalLink)value;
  183. addAttribute(name, iLink.xmlAttribute());
  184. } else if (clazz.equals(Background.class)) {
  185. Background bkg = (Background)value;
  186. //TODO Remove the following line (makes changes in the test checks necessary)
  187. addAttribute(name, bkg.toString());
  188. if (bkg.getColor() != null) {
  189. addAttribute("bkg-color", bkg.getColor().toString());
  190. }
  191. if (bkg.getURL() != null) {
  192. addAttribute("bkg-img", bkg.getURL());
  193. String repString;
  194. int repeat = bkg.getRepeat();
  195. switch (repeat) {
  196. case Constants.EN_REPEAT:
  197. repString = "repeat";
  198. break;
  199. case Constants.EN_REPEATX:
  200. repString = "repeat-x";
  201. break;
  202. case Constants.EN_REPEATY:
  203. repString = "repeat-y";
  204. break;
  205. case Constants.EN_NOREPEAT:
  206. repString = "no-repeat";
  207. break;
  208. default:
  209. throw new IllegalStateException(
  210. "Illegal value for repeat encountered: " + repeat);
  211. }
  212. addAttribute("bkg-repeat", repString);
  213. addAttribute("bkg-horz-offset", bkg.getHoriz());
  214. addAttribute("bkg-vert-offset", bkg.getVertical());
  215. }
  216. } else if (clazz.equals(Color.class)) {
  217. Color c = (Color)value;
  218. addAttribute(name, ColorUtil.colorToString(c));
  219. } else if (key == Trait.START_INDENT || key == Trait.END_INDENT) {
  220. if (((Integer)value).intValue() != 0) {
  221. addAttribute(name, value.toString());
  222. }
  223. } else {
  224. addAttribute(name, value.toString());
  225. }
  226. }
  227. }
  228. transferForeignObjects(area);
  229. }
  230. private void transferForeignObjects(AreaTreeObject ato) {
  231. Map prefixes = new java.util.HashMap();
  232. Iterator iter = ato.getForeignAttributes().entrySet().iterator();
  233. while (iter.hasNext()) {
  234. Map.Entry entry = (Map.Entry)iter.next();
  235. QName qname = (QName)entry.getKey();
  236. prefixes.put(qname.getPrefix(), qname.getNamespaceURI());
  237. addAttribute(qname, (String)entry.getValue());
  238. }
  239. //Namespace declarations
  240. iter = prefixes.entrySet().iterator();
  241. while (iter.hasNext()) {
  242. Map.Entry entry = (Map.Entry)iter.next();
  243. String qn = "xmlns:" + (String)entry.getKey();
  244. atts.addAttribute("", (String)entry.getKey(), qn,
  245. CDATA, (String)entry.getValue());
  246. }
  247. }
  248. /** {@inheritDoc} */
  249. public void processOffDocumentItem(OffDocumentItem oDI) {
  250. if (oDI instanceof BookmarkData) {
  251. renderBookmarkTree((BookmarkData) oDI);
  252. } else if (oDI instanceof DestinationData) {
  253. renderDestination((DestinationData) oDI);
  254. } else if (oDI instanceof OffDocumentExtensionAttachment) {
  255. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)oDI).getAttachment();
  256. if (extensionAttachments == null) {
  257. extensionAttachments = new java.util.ArrayList();
  258. }
  259. extensionAttachments.add(attachment);
  260. } else {
  261. String warn = "Ignoring OffDocumentItem: " + oDI;
  262. log.warn(warn);
  263. }
  264. }
  265. /**
  266. * Renders a BookmarkTree object
  267. * @param bookmarkRoot the BookmarkData object representing the top of the tree
  268. */
  269. protected void renderBookmarkTree(BookmarkData bookmarkRoot) {
  270. if (bookmarkRoot.getWhenToProcess() == OffDocumentItem.END_OF_DOC) {
  271. endPageSequence();
  272. }
  273. /* If this kind of handling is also necessary for other renderers, then
  274. better add endPageSequence to the Renderer interface and call it
  275. explicitly from model.endDocument() */
  276. startElement("bookmarkTree");
  277. for (int i = 0; i < bookmarkRoot.getCount(); i++) {
  278. renderBookmarkItem(bookmarkRoot.getSubData(i));
  279. }
  280. endElement("bookmarkTree");
  281. }
  282. private void renderBookmarkItem(BookmarkData bm) {
  283. atts.clear();
  284. addAttribute("title", bm.getBookmarkTitle());
  285. addAttribute("show-children", String.valueOf(bm.showChildItems()));
  286. PageViewport pv = bm.getPageViewport();
  287. String pvKey = pv == null ? null : pv.getKey();
  288. addAttribute("internal-link",
  289. InternalLink.makeXMLAttribute(pvKey, bm.getIDRef()));
  290. startElement("bookmark", atts);
  291. for (int i = 0; i < bm.getCount(); i++) {
  292. renderBookmarkItem(bm.getSubData(i));
  293. }
  294. endElement("bookmark");
  295. }
  296. /**
  297. * Renders a DestinationData object (named destination)
  298. * @param destination the destination object
  299. */
  300. protected void renderDestination(DestinationData destination) {
  301. if (destination.getWhenToProcess() == OffDocumentItem.END_OF_DOC) {
  302. endPageSequence();
  303. }
  304. atts.clear();
  305. PageViewport pv = destination.getPageViewport();
  306. String pvKey = pv == null ? null : pv.getKey();
  307. addAttribute("internal-link",
  308. InternalLink.makeXMLAttribute(pvKey, destination.getIDRef()));
  309. startElement("destination", atts);
  310. endElement("destination");
  311. }
  312. /** {@inheritDoc} */
  313. public void startRenderer(OutputStream outputStream)
  314. throws IOException {
  315. log.debug("Rendering areas to Area Tree XML");
  316. if (this.handler == null) {
  317. SAXTransformerFactory factory
  318. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  319. try {
  320. TransformerHandler transformerHandler = factory.newTransformerHandler();
  321. this.handler = transformerHandler;
  322. StreamResult res = new StreamResult(outputStream);
  323. transformerHandler.setResult(res);
  324. } catch (TransformerConfigurationException tce) {
  325. throw new RuntimeException(tce.getMessage());
  326. }
  327. this.out = outputStream;
  328. }
  329. try {
  330. handler.startDocument();
  331. } catch (SAXException saxe) {
  332. handleSAXException(saxe);
  333. }
  334. if (userAgent.getProducer() != null) {
  335. comment("Produced by " + userAgent.getProducer());
  336. }
  337. startElement("areaTree");
  338. }
  339. /** {@inheritDoc} */
  340. public void stopRenderer() throws IOException {
  341. endPageSequence();
  342. endElement("areaTree");
  343. try {
  344. handler.endDocument();
  345. } catch (SAXException saxe) {
  346. handleSAXException(saxe);
  347. }
  348. if (this.out != null) {
  349. this.out.flush();
  350. }
  351. log.debug("Written out Area Tree XML");
  352. }
  353. /** {@inheritDoc} */
  354. public void renderPage(PageViewport page) throws IOException, FOPException {
  355. atts.clear();
  356. addAttribute("bounds", page.getViewArea());
  357. addAttribute("key", page.getKey());
  358. addAttribute("nr", page.getPageNumber());
  359. addAttribute("formatted-nr", page.getPageNumberString());
  360. if (page.getSimplePageMasterName() != null) {
  361. addAttribute("simple-page-master-name", page.getSimplePageMasterName());
  362. }
  363. if (page.isBlank()) {
  364. addAttribute("blank", "true");
  365. }
  366. transferForeignObjects(page);
  367. startElement("pageViewport", atts);
  368. startElement("page");
  369. handlePageExtensionAttachments(page);
  370. super.renderPage(page);
  371. endElement("page");
  372. endElement("pageViewport");
  373. }
  374. /** {@inheritDoc} */
  375. protected void handleExtensionAttachments(List attachments) {
  376. if (attachments != null && attachments.size() > 0) {
  377. startElement("extension-attachments");
  378. Iterator i = attachments.iterator();
  379. while (i.hasNext()) {
  380. ExtensionAttachment attachment = (ExtensionAttachment)i.next();
  381. if (attachment instanceof XMLizable) {
  382. try {
  383. ((XMLizable)attachment).toSAX(this.handler);
  384. } catch (SAXException e) {
  385. log.error("Error while serializing Extension Attachment", e);
  386. }
  387. } else {
  388. String warn = "Ignoring non-XMLizable ExtensionAttachment: " + attachment;
  389. log.warn(warn);
  390. }
  391. }
  392. endElement("extension-attachments");
  393. }
  394. }
  395. /** {@inheritDoc} */
  396. public void startPageSequence(PageSequence pageSequence) {
  397. handleDocumentExtensionAttachments();
  398. endPageSequence(); // move this before handleDocumentExtensionAttachments() ?
  399. startedSequence = true;
  400. atts.clear();
  401. if (pageSequence.getLanguage() != null) {
  402. addAttribute("language", pageSequence.getLanguage());
  403. }
  404. if (pageSequence.getCountry() != null) {
  405. addAttribute("country", pageSequence.getCountry());
  406. }
  407. transferForeignObjects(pageSequence);
  408. startElement("pageSequence", atts);
  409. if (this.getUserAgent().isAccessibilityEnabled()) {
  410. String structureTreeElement = "structureTree";
  411. startElement(structureTreeElement);
  412. NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
  413. for (int i = 0, n = nodes.getLength(); i < n; i++) {
  414. Node node = nodes.item(i);
  415. try {
  416. new DOM2SAX(handler).writeFragment(node);
  417. } catch (SAXException e) {
  418. handleSAXException(e);
  419. }
  420. }
  421. endElement(structureTreeElement);
  422. }
  423. handleExtensionAttachments(pageSequence.getExtensionAttachments());
  424. LineArea seqTitle = pageSequence.getTitle();
  425. if (seqTitle != null) {
  426. startElement("title");
  427. List children = seqTitle.getInlineAreas();
  428. for (int count = 0; count < children.size(); count++) {
  429. InlineArea inline = (InlineArea) children.get(count);
  430. renderInlineArea(inline);
  431. }
  432. endElement("title");
  433. }
  434. }
  435. /**
  436. * Tells the renderer to finish the current PageSequence
  437. */
  438. public void endPageSequence() {
  439. if (startedSequence) {
  440. endElement("pageSequence");
  441. }
  442. startedSequence = false;
  443. }
  444. /**
  445. * {@inheritDoc}
  446. */
  447. protected void renderRegionViewport(RegionViewport port) {
  448. if (port != null) {
  449. atts.clear();
  450. addAreaAttributes(port);
  451. addTraitAttributes(port);
  452. addAttribute("rect", port.getViewArea());
  453. if (port.isClip()) {
  454. addAttribute("clipped", "true");
  455. }
  456. startElement("regionViewport", atts);
  457. RegionReference region = port.getRegionReference();
  458. atts.clear();
  459. addAreaAttributes(region);
  460. addTraitAttributes(region);
  461. addAttribute("name", region.getRegionName());
  462. addAttribute("ctm", region.getCTM().toString());
  463. if (region.getRegionClass() == FO_REGION_BEFORE) {
  464. startElement("regionBefore", atts);
  465. renderRegion(region);
  466. endElement("regionBefore");
  467. } else if (region.getRegionClass() == FO_REGION_START) {
  468. startElement("regionStart", atts);
  469. renderRegion(region);
  470. endElement("regionStart");
  471. } else if (region.getRegionClass() == FO_REGION_BODY) {
  472. BodyRegion body = (BodyRegion)region;
  473. if (body.getColumnCount() != 1) {
  474. addAttribute("columnGap", body.getColumnGap());
  475. addAttribute("columnCount", body.getColumnCount());
  476. }
  477. startElement("regionBody", atts);
  478. renderBodyRegion(body);
  479. endElement("regionBody");
  480. } else if (region.getRegionClass() == FO_REGION_END) {
  481. startElement("regionEnd", atts);
  482. renderRegion(region);
  483. endElement("regionEnd");
  484. } else if (region.getRegionClass() == FO_REGION_AFTER) {
  485. startElement("regionAfter", atts);
  486. renderRegion(region);
  487. endElement("regionAfter");
  488. }
  489. endElement("regionViewport");
  490. }
  491. }
  492. /** {@inheritDoc} */
  493. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  494. //only necessary for graphical output
  495. }
  496. /** {@inheritDoc} */
  497. protected void endVParea() {
  498. //only necessary for graphical output
  499. }
  500. /**
  501. * {@inheritDoc}
  502. * org.apache.fop.area.inline.InlineArea)
  503. */
  504. protected void renderInlineAreaBackAndBorders(InlineArea area) {
  505. //only necessary for graphical output
  506. }
  507. /**
  508. * {@inheritDoc}
  509. */
  510. protected void renderBeforeFloat(BeforeFloat bf) {
  511. startElement("beforeFloat");
  512. super.renderBeforeFloat(bf);
  513. endElement("beforeFloat");
  514. }
  515. /**
  516. * {@inheritDoc}
  517. */
  518. protected void renderFootnote(Footnote footnote) {
  519. atts.clear();
  520. addAttribute("top-offset", footnote.getTop());
  521. startElement("footnote", atts);
  522. super.renderFootnote(footnote);
  523. endElement("footnote");
  524. }
  525. /**
  526. * {@inheritDoc}
  527. */
  528. protected void renderMainReference(MainReference mr) {
  529. atts.clear();
  530. addAreaAttributes(mr);
  531. addTraitAttributes(mr);
  532. if (mr.getColumnCount() != 1) {
  533. addAttribute("columnGap", mr.getColumnGap());
  534. }
  535. startElement("mainReference", atts);
  536. Span span = null;
  537. List spans = mr.getSpans();
  538. for (int count = 0; count < spans.size(); count++) {
  539. span = (Span) spans.get(count);
  540. atts.clear();
  541. if (span.getColumnCount() != 1) {
  542. addAttribute("columnCount", span.getColumnCount());
  543. }
  544. addAreaAttributes(span);
  545. addTraitAttributes(span);
  546. startElement("span", atts);
  547. for (int c = 0; c < span.getColumnCount(); c++) {
  548. NormalFlow flow = (NormalFlow) span.getNormalFlow(c);
  549. renderFlow(flow);
  550. }
  551. endElement("span");
  552. }
  553. endElement("mainReference");
  554. }
  555. /**
  556. * {@inheritDoc}
  557. */
  558. protected void renderFlow(NormalFlow flow) {
  559. // the normal flow reference area contains stacked blocks
  560. atts.clear();
  561. addAreaAttributes(flow);
  562. addTraitAttributes(flow);
  563. startElement("flow", atts);
  564. super.renderFlow(flow);
  565. endElement("flow");
  566. }
  567. /** {@inheritDoc} */
  568. protected void renderReferenceArea(Block block) {
  569. handleBlockTraits(block);
  570. List children = block.getChildAreas();
  571. if (children != null) {
  572. renderBlocks(block, children);
  573. }
  574. }
  575. /** {@inheritDoc} */
  576. protected void renderBlock(Block block) {
  577. atts.clear();
  578. addAreaAttributes(block);
  579. addTraitAttributes(block);
  580. int positioning = block.getPositioning();
  581. if (block instanceof BlockViewport) {
  582. BlockViewport bvp = (BlockViewport)block;
  583. boolean abspos = false;
  584. if (bvp.getPositioning() == Block.ABSOLUTE
  585. || bvp.getPositioning() == Block.FIXED) {
  586. abspos = true;
  587. }
  588. if (abspos) {
  589. addAttribute("left-position", bvp.getXOffset());
  590. addAttribute("top-position", bvp.getYOffset());
  591. }
  592. addAttribute("ctm", bvp.getCTM().toString());
  593. if (bvp.getClip()) {
  594. addAttribute("clipped", "true");
  595. }
  596. } else {
  597. if (block.getXOffset() != 0) {
  598. addAttribute("left-offset", block.getXOffset());
  599. }
  600. if (block.getYOffset() != 0) {
  601. addAttribute("top-offset", block.getYOffset());
  602. }
  603. }
  604. switch (positioning) {
  605. case Block.RELATIVE:
  606. addAttribute("positioning", "relative");
  607. break;
  608. case Block.ABSOLUTE:
  609. addAttribute("positioning", "absolute");
  610. break;
  611. case Block.FIXED:
  612. addAttribute("positioning", "fixed");
  613. break;
  614. default: //nop
  615. }
  616. startElement("block", atts);
  617. super.renderBlock(block);
  618. endElement("block");
  619. }
  620. /**
  621. * {@inheritDoc}
  622. */
  623. protected void renderLineArea(LineArea line) {
  624. atts.clear();
  625. addAreaAttributes(line);
  626. addTraitAttributes(line);
  627. startElement("lineArea", atts);
  628. super.renderLineArea(line);
  629. endElement("lineArea");
  630. }
  631. /**
  632. * {@inheritDoc}
  633. */
  634. protected void renderInlineArea(InlineArea inlineArea) {
  635. atts.clear();
  636. if (inlineArea.getClass() == InlineArea.class) {
  637. // Generic inline area. This is implemented to allow the 0x0 "dummy"
  638. // area generated by fo:wrapper to pass its id.
  639. addAreaAttributes(inlineArea);
  640. addTraitAttributes(inlineArea);
  641. startElement("inline", atts);
  642. endElement("inline");
  643. } else {
  644. super.renderInlineArea(inlineArea);
  645. // calls specific renderers for Text, Space, Viewport, etc. etc.
  646. }
  647. }
  648. /**
  649. * {@inheritDoc}
  650. */
  651. protected void renderViewport(Viewport viewport) {
  652. atts.clear();
  653. addAreaAttributes(viewport);
  654. addTraitAttributes(viewport);
  655. addAttribute("offset", viewport.getOffset());
  656. addAttribute("pos", viewport.getContentPosition());
  657. if (viewport.getClip()) {
  658. addAttribute("clip", "true");
  659. }
  660. startElement("viewport", atts);
  661. super.renderViewport(viewport);
  662. endElement("viewport");
  663. }
  664. /**
  665. * {@inheritDoc}
  666. */
  667. public void renderImage(Image image, Rectangle2D pos) {
  668. atts.clear();
  669. addAreaAttributes(image);
  670. addTraitAttributes(image);
  671. addAttribute("url", image.getURL());
  672. //addAttribute("pos", pos);
  673. startElement("image", atts);
  674. endElement("image");
  675. }
  676. /**
  677. * {@inheritDoc}
  678. */
  679. public void renderContainer(Container cont) {
  680. startElement("container");
  681. super.renderContainer(cont);
  682. endElement("container");
  683. }
  684. /**
  685. * Renders an fo:foreing-object.
  686. * @param fo the foreign object
  687. * @param pos the position of the foreign object
  688. * @see org.apache.fop.render.AbstractRenderer#renderForeignObject(ForeignObject, Rectangle2D)
  689. */
  690. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  691. atts.clear();
  692. addAreaAttributes(fo);
  693. addTraitAttributes(fo);
  694. String ns = fo.getNameSpace();
  695. addAttribute("ns", ns);
  696. startElement("foreignObject", atts);
  697. Document doc = fo.getDocument();
  698. context.setProperty(XMLXMLHandler.HANDLER, handler);
  699. renderXML(context, doc, ns);
  700. endElement("foreignObject");
  701. }
  702. /**
  703. * {@inheritDoc}
  704. */
  705. protected void renderInlineSpace(Space space) {
  706. atts.clear();
  707. addAreaAttributes(space);
  708. addTraitAttributes(space);
  709. addAttribute("offset", space.getOffset());
  710. startElement("space", atts);
  711. endElement("space");
  712. }
  713. /**
  714. * {@inheritDoc}
  715. */
  716. protected void renderText(TextArea text) {
  717. atts.clear();
  718. if (text.getTextWordSpaceAdjust() != 0) {
  719. addAttribute("twsadjust", text.getTextWordSpaceAdjust());
  720. }
  721. if (text.getTextLetterSpaceAdjust() != 0) {
  722. addAttribute("tlsadjust", text.getTextLetterSpaceAdjust());
  723. }
  724. addAttribute("offset", text.getOffset());
  725. addAttribute("baseline", text.getBaselineOffset());
  726. addAreaAttributes(text);
  727. addTraitAttributes(text);
  728. startElement("text", atts);
  729. super.renderText(text);
  730. endElement("text");
  731. }
  732. /**
  733. * {@inheritDoc}
  734. */
  735. protected void renderWord(WordArea word) {
  736. atts.clear();
  737. addAttribute("offset", word.getOffset());
  738. int[] letterAdjust = word.getLetterAdjustArray();
  739. if (letterAdjust != null) {
  740. StringBuffer sb = new StringBuffer(64);
  741. boolean nonZeroFound = false;
  742. for (int i = 0, c = letterAdjust.length; i < c; i++) {
  743. if (i > 0) {
  744. sb.append(' ');
  745. }
  746. sb.append(letterAdjust[i]);
  747. nonZeroFound |= (letterAdjust[i] != 0);
  748. }
  749. if (nonZeroFound) {
  750. addAttribute("letter-adjust", sb.toString());
  751. }
  752. }
  753. startElement("word", atts);
  754. characters(word.getWord());
  755. endElement("word");
  756. super.renderWord(word);
  757. }
  758. /**
  759. * {@inheritDoc}
  760. */
  761. protected void renderSpace(SpaceArea space) {
  762. atts.clear();
  763. addAttribute("offset", space.getOffset());
  764. if (!space.isAdjustable()) {
  765. addAttribute("adj", "false"); //default is true
  766. }
  767. startElement("space", atts);
  768. characters(space.getSpace());
  769. endElement("space");
  770. super.renderSpace(space);
  771. }
  772. /**
  773. * {@inheritDoc}
  774. */
  775. protected void renderInlineParent(InlineParent ip) {
  776. atts.clear();
  777. addAreaAttributes(ip);
  778. addTraitAttributes(ip);
  779. addAttribute("offset", ip.getOffset());
  780. startElement("inlineparent", atts);
  781. super.renderInlineParent(ip);
  782. endElement("inlineparent");
  783. }
  784. /**
  785. * {@inheritDoc}
  786. */
  787. protected void renderInlineBlockParent(InlineBlockParent ibp) {
  788. atts.clear();
  789. addAreaAttributes(ibp);
  790. addTraitAttributes(ibp);
  791. addAttribute("offset", ibp.getOffset());
  792. startElement("inlineblockparent", atts);
  793. super.renderInlineBlockParent(ibp);
  794. endElement("inlineblockparent");
  795. }
  796. /**
  797. * {@inheritDoc}
  798. */
  799. protected void renderLeader(Leader area) {
  800. atts.clear();
  801. addAreaAttributes(area);
  802. addTraitAttributes(area);
  803. addAttribute("offset", area.getOffset());
  804. addAttribute("ruleStyle", area.getRuleStyleAsString());
  805. addAttribute("ruleThickness", area.getRuleThickness());
  806. startElement("leader", atts);
  807. endElement("leader");
  808. super.renderLeader(area);
  809. }
  810. /** {@inheritDoc} */
  811. public String getMimeType() {
  812. return XML_MIME_TYPE;
  813. }
  814. }