Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SVGRenderer.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.render.svg;
  52. import org.apache.fop.apps.FOPException;
  53. import org.apache.fop.area.PageViewport;
  54. import org.apache.fop.area.Title;
  55. import org.apache.fop.area.inline.ForeignObject;
  56. import org.apache.fop.area.inline.Leader;
  57. import org.apache.fop.area.inline.Word;
  58. import org.apache.fop.svg.SVGUtilities;
  59. import org.apache.fop.layout.FontInfo;
  60. import org.apache.fop.fo.FOUserAgent;
  61. import org.apache.fop.fo.properties.RuleStyle;
  62. import org.w3c.dom.Node;
  63. import org.w3c.dom.svg.SVGSVGElement;
  64. import org.w3c.dom.svg.SVGDocument;
  65. import org.w3c.dom.Document;
  66. import org.w3c.dom.Element;
  67. import org.w3c.dom.DOMImplementation;
  68. import org.w3c.dom.Text;
  69. import org.apache.batik.dom.svg.SVGDOMImplementation;
  70. import org.apache.batik.dom.util.XMLSupport;
  71. import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
  72. import org.apache.batik.transcoder.TranscoderInput;
  73. import org.apache.batik.transcoder.TranscoderOutput;
  74. import org.apache.batik.transcoder.TranscoderException;
  75. import org.apache.batik.dom.util.DOMUtilities;
  76. import java.awt.Color;
  77. import java.awt.image.BufferedImage;
  78. import java.awt.geom.Rectangle2D;
  79. import java.util.HashMap;
  80. import java.io.OutputStream;
  81. import java.io.IOException;
  82. import java.io.OutputStreamWriter;
  83. import org.apache.fop.render.AbstractRenderer;
  84. import org.apache.fop.render.XMLHandler;
  85. import org.apache.fop.render.RendererContext;
  86. /**
  87. * This is the SVG renderer.
  88. */
  89. public class SVGRenderer extends AbstractRenderer implements XMLHandler {
  90. /** SVG MIME type */
  91. public static final String SVG_MIME_TYPE = "image/svg+xml";
  92. /** SVG namespace */
  93. public static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
  94. private Document svgDocument;
  95. private Element svgRoot;
  96. private Element currentPageG = null;
  97. private Element lastLink = null;
  98. private String lastViewbox = null;
  99. private Element docDefs = null;
  100. private Element pageDefs = null;
  101. private Element pagesGroup = null;
  102. // first sequence title
  103. private Title docTitle = null;
  104. private RendererContext context;
  105. private OutputStream ostream;
  106. private float totalWidth = 0;
  107. private float totalHeight = 0;
  108. private float sequenceWidth = 0;
  109. private float sequenceHeight = 0;
  110. private float pageWidth = 0;
  111. private float pageHeight = 0;
  112. private int pageNumber = 0;
  113. private HashMap fontNames = new HashMap();
  114. private HashMap fontStyles = new HashMap();
  115. private Color saveColor = null;
  116. /**
  117. * The current (internal) font name
  118. */
  119. private String currentFontName;
  120. /**
  121. * The current font size in millipoints
  122. */
  123. private int currentFontSize;
  124. /**
  125. * The current colour's red, green and blue component
  126. */
  127. private float currentRed = 0;
  128. private float currentGreen = 0;
  129. private float currentBlue = 0;
  130. /**
  131. * Creates a new SVG renderer.
  132. */
  133. public SVGRenderer() {
  134. context = new RendererContext(SVG_MIME_TYPE);
  135. }
  136. /**
  137. * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent)
  138. */
  139. public void setUserAgent(FOUserAgent agent) {
  140. super.setUserAgent(agent);
  141. userAgent.setDefaultXMLHandler(SVG_MIME_TYPE, this);
  142. userAgent.addXMLHandler(SVG_MIME_TYPE, SVG_NAMESPACE, this);
  143. }
  144. /**
  145. * @see org.apache.fop.render.Renderer#setupFontInfo(FontInfo)
  146. */
  147. public void setupFontInfo(FontInfo fontInfo) {
  148. // create a temp Image to test font metrics on
  149. BufferedImage fontImage =
  150. new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  151. org.apache.fop.render.awt.FontSetup.setup(fontInfo,
  152. fontImage.createGraphics());
  153. }
  154. /**
  155. * @see org.apache.fop.render.Renderer#setProducer(String)
  156. */
  157. public void setProducer(String producer) {
  158. }
  159. /**
  160. * @see org.apache.fop.render.Renderer#startRenderer(OutputStream)
  161. */
  162. public void startRenderer(OutputStream outputStream)
  163. throws IOException {
  164. ostream = outputStream;
  165. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  166. svgDocument = impl.createDocument(SVG_NAMESPACE, "svg", null);
  167. svgRoot = svgDocument.getDocumentElement();
  168. /*
  169. ProcessingInstruction pi =
  170. svgDocument.createProcessingInstruction("xml",
  171. " version=\"1.0\" encoding=\"ISO-8859-1\"");
  172. svgDocument.insertBefore(pi, svgRoot);
  173. */
  174. docDefs = svgDocument.createElementNS(SVG_NAMESPACE, "defs");
  175. svgRoot.appendChild(docDefs);
  176. pagesGroup = svgDocument.createElementNS(SVG_NAMESPACE, "g");
  177. pageDefs = svgDocument.createElementNS(SVG_NAMESPACE, "defs");
  178. pagesGroup.appendChild(pageDefs);
  179. svgRoot.appendChild(pagesGroup);
  180. }
  181. /**
  182. * @see org.apache.fop.render.Renderer#stopRenderer()
  183. */
  184. public void stopRenderer() throws IOException {
  185. totalWidth += sequenceWidth;
  186. if (sequenceHeight > totalHeight) {
  187. totalHeight = sequenceHeight;
  188. }
  189. svgRoot.setAttributeNS(null, "width", "" + (totalWidth + 1));
  190. svgRoot.setAttributeNS(null, "height", "" + (totalHeight + 1));
  191. //svgRoot.setAttributeNS(null, "viewBox", "0 0 " + pageWidth + " " + pageHeight);
  192. SVGTranscoder svgT = new SVGTranscoder();
  193. TranscoderInput input = new TranscoderInput(svgDocument);
  194. TranscoderOutput output =
  195. new TranscoderOutput(new OutputStreamWriter(ostream));
  196. try {
  197. svgT.transcode(input, output);
  198. } catch (TranscoderException e) {
  199. getLogger().error("could not write svg file :" + e.getMessage(), e);
  200. }
  201. ostream.flush();
  202. ostream = null;
  203. svgDocument = null;
  204. svgRoot = null;
  205. currentPageG = null;
  206. lastLink = null;
  207. totalWidth = 0;
  208. totalHeight = 0;
  209. pageNumber = 0;
  210. }
  211. /**
  212. * @see org.apache.fop.render.Renderer#startPageSequence(Title)
  213. */
  214. public void startPageSequence(Title seqTitle) {
  215. totalWidth += sequenceWidth;
  216. if (sequenceHeight > totalHeight) {
  217. totalHeight = sequenceHeight;
  218. }
  219. sequenceWidth = 0;
  220. sequenceHeight = 0;
  221. if (seqTitle != null && docTitle == null) {
  222. // convert first title to a string and set for svg document title
  223. docTitle = seqTitle;
  224. String str = convertTitleToString(seqTitle);
  225. Element svgTitle = svgDocument.createElementNS(SVG_NAMESPACE, "title");
  226. Text strNode = svgDocument.createTextNode(str);
  227. svgTitle.appendChild(strNode);
  228. svgRoot.insertBefore(svgTitle, svgRoot.getFirstChild());
  229. }
  230. }
  231. /**
  232. * @see org.apache.fop.render.Renderer#renderPage(PageViewport)
  233. */
  234. public void renderPage(PageViewport page) throws IOException, FOPException {
  235. float lastWidth = pageWidth;
  236. float lastHeight = pageHeight;
  237. Rectangle2D area = page.getViewArea();
  238. pageWidth = (float) area.getWidth() / 1000f;
  239. pageHeight = (float) area.getHeight() / 1000f;
  240. // if there is a link from the last page
  241. if (lastLink != null) {
  242. lastLink.setAttributeNS(null, "xlink:href", "#svgView(viewBox("
  243. + totalWidth + ", "
  244. + sequenceHeight + ", "
  245. + pageWidth + ", "
  246. + pageHeight + "))");
  247. pagesGroup.appendChild(lastLink);
  248. }
  249. currentPageG = svgDocument.createElementNS(SVG_NAMESPACE, "svg");
  250. currentPageG.setAttributeNS(null, "viewbox",
  251. "0 0 " + (int) pageWidth + " " + (int) pageHeight);
  252. currentPageG.setAttributeNS(null, "width",
  253. "" + ((int) pageWidth + 1));
  254. currentPageG.setAttributeNS(null, "height",
  255. "" + ((int) pageHeight + 1));
  256. currentPageG.setAttributeNS(null, "id", "Page-" + pageNumber);
  257. currentPageG.setAttributeNS(null, "style", "font-family:sanserif;font-size:12");
  258. pageDefs.appendChild(currentPageG);
  259. if (pageWidth > sequenceWidth) {
  260. sequenceWidth = pageWidth;
  261. }
  262. sequenceHeight += pageHeight;
  263. Element border =
  264. SVGUtilities.createRect(svgDocument, 0, 0, pageWidth,
  265. pageHeight);
  266. border.setAttributeNS(null, "style", "fill:none;stroke:black");
  267. currentPageG.appendChild(border);
  268. // render the page contents
  269. super.renderPage(page);
  270. Element use = svgDocument.createElementNS(SVG_NAMESPACE, "use");
  271. use.setAttributeNS(null, "xlink:href", "#Page-" + pageNumber);
  272. use.setAttributeNS(null, "x", "" + totalWidth);
  273. use.setAttributeNS(null, "y", "" + (sequenceHeight - pageHeight));
  274. pagesGroup.appendChild(use);
  275. Element lastPageLink = svgDocument.createElementNS(SVG_NAMESPACE, "a");
  276. if (lastLink != null) {
  277. lastPageLink.setAttributeNS(null, "xlink:href", lastViewbox);
  278. } else {
  279. lastPageLink.setAttributeNS(null, "xlink:href",
  280. "#svgView(viewBox("
  281. + totalWidth + ", "
  282. + (sequenceHeight - pageHeight) + ", "
  283. + pageWidth + ", "
  284. + pageHeight + "))");
  285. }
  286. pagesGroup.appendChild(lastPageLink);
  287. // setup a link to the next page, only added when the
  288. // next page is rendered
  289. Element rect = SVGUtilities.createRect(svgDocument, totalWidth,
  290. (sequenceHeight - pageHeight), pageWidth / 2, pageHeight);
  291. rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
  292. lastPageLink.appendChild(rect);
  293. lastLink = svgDocument.createElementNS(SVG_NAMESPACE, "a");
  294. rect = SVGUtilities.createRect(svgDocument,
  295. totalWidth + pageWidth / 2,
  296. (sequenceHeight - pageHeight), pageWidth / 2, pageHeight);
  297. rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
  298. lastLink.appendChild(rect);
  299. lastViewbox = "#svgView(viewBox("
  300. + totalWidth + ", "
  301. + (sequenceHeight - pageHeight) + ", "
  302. + pageWidth + ", "
  303. + pageHeight + "))";
  304. pageNumber++;
  305. }
  306. /**
  307. * Method renderForeignObject.
  308. * @param fo the foreign object
  309. */
  310. public void renderForeignObject(ForeignObject fo) {
  311. Document doc = fo.getDocument();
  312. String ns = fo.getNameSpace();
  313. userAgent.renderXML(context, doc, ns);
  314. }
  315. /**
  316. * @see org.apache.fop.render.XMLHandler#handleXML(RendererContext, Document, String)
  317. */
  318. public void handleXML(RendererContext context, Document doc,
  319. String ns) throws Exception {
  320. if (SVG_NAMESPACE.equals(ns)) {
  321. if (!(doc instanceof SVGDocument)) {
  322. DOMImplementation impl =
  323. SVGDOMImplementation.getDOMImplementation();
  324. doc = DOMUtilities.deepCloneDocument(doc, impl);
  325. }
  326. SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
  327. Element view = svgDocument.createElementNS(SVG_NAMESPACE, "svg");
  328. Node newsvg = svgDocument.importNode(svg, true);
  329. //view.setAttributeNS(null, "viewBox", "0 0 ");
  330. view.setAttributeNS(null, "x",
  331. "" + currentBlockIPPosition / 1000f);
  332. view.setAttributeNS(null, "y", "" + currentBPPosition / 1000f);
  333. // this fixes a problem where the xmlns is repeated sometimes
  334. Element ele = (Element) newsvg;
  335. ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
  336. SVG_NAMESPACE);
  337. if (ele.hasAttributeNS(null, "xmlns")) {
  338. ele.removeAttributeNS(null, "xmlns");
  339. }
  340. view.appendChild(newsvg);
  341. currentPageG.appendChild(view);
  342. }
  343. }
  344. /**
  345. * @see org.apache.fop.render.Renderer#renderLeader(Leader)
  346. */
  347. public void renderLeader(Leader area) {
  348. String style = "stroke:black;stroke-width:"
  349. + (area.getRuleThickness() / 1000) + ";";
  350. switch (area.getRuleStyle()) {
  351. case RuleStyle.DOTTED:
  352. style += "stroke-dasharray:1,1";
  353. break;
  354. case RuleStyle.DASHED:
  355. style += "stroke-dasharray:5,1";
  356. break;
  357. case RuleStyle.SOLID:
  358. break;
  359. case RuleStyle.DOUBLE:
  360. break;
  361. case RuleStyle.GROOVE:
  362. break;
  363. case RuleStyle.RIDGE:
  364. break;
  365. }
  366. Element line = SVGUtilities.createLine(svgDocument,
  367. currentBlockIPPosition / 1000,
  368. (currentBPPosition + area.getOffset()
  369. - area.getRuleThickness() / 2) / 1000,
  370. (currentBlockIPPosition + area.getWidth()) / 1000,
  371. (currentBPPosition + area.getOffset()
  372. - area.getRuleThickness() / 2) / 1000);
  373. line.setAttributeNS(null, "style", style);
  374. currentPageG.appendChild(line);
  375. super.renderLeader(area);
  376. }
  377. /**
  378. * @see org.apache.fop.render.Renderer#renderWord(Word)
  379. */
  380. public void renderWord(Word word) {
  381. Element text = SVGUtilities.createText(svgDocument,
  382. currentBlockIPPosition / 1000,
  383. (currentBPPosition + word.getOffset()) / 1000,
  384. word.getWord());
  385. currentPageG.appendChild(text);
  386. super.renderWord(word);
  387. }
  388. /**
  389. * @see org.apache.fop.render.Renderer#renderCharacter(Character)
  390. */
  391. public void renderCharacter(org.apache.fop.area.inline.Character ch) {
  392. Element text = SVGUtilities.createText(svgDocument,
  393. currentBlockIPPosition / 1000,
  394. (currentBPPosition + ch.getOffset()) / 1000,
  395. "" + ch.getChar());
  396. currentPageG.appendChild(text);
  397. super.renderCharacter(ch);
  398. }
  399. }