Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XMLRenderer.java 32KB

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