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.

SVGRenderer.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.svg;
  18. import org.apache.fop.apps.FOPException;
  19. import org.apache.fop.area.PageViewport;
  20. import org.apache.fop.area.LineArea;
  21. import org.apache.fop.area.inline.ForeignObject;
  22. import org.apache.fop.area.inline.Leader;
  23. import org.apache.fop.area.inline.TextArea;
  24. import org.apache.fop.svg.SVGUtilities;
  25. import org.apache.fop.fonts.FontInfo;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.w3c.dom.Node;
  28. import org.w3c.dom.svg.SVGSVGElement;
  29. import org.w3c.dom.svg.SVGDocument;
  30. /* org.w3c.dom.Document is not imported to avoid conflict with
  31. org.apache.fop.control.Document */
  32. import org.w3c.dom.DOMImplementation;
  33. import org.w3c.dom.Element;
  34. import org.w3c.dom.Text;
  35. import org.apache.batik.dom.svg.SVGDOMImplementation;
  36. import org.apache.batik.dom.util.XMLSupport;
  37. import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
  38. import org.apache.batik.transcoder.TranscoderInput;
  39. import org.apache.batik.transcoder.TranscoderOutput;
  40. import org.apache.batik.transcoder.TranscoderException;
  41. import org.apache.batik.dom.util.DOMUtilities;
  42. import java.awt.Color;
  43. import java.awt.image.BufferedImage;
  44. import java.awt.geom.Rectangle2D;
  45. import java.util.HashMap;
  46. import java.io.OutputStream;
  47. import java.io.IOException;
  48. import java.io.OutputStreamWriter;
  49. import org.apache.fop.render.AbstractRenderer;
  50. import org.apache.fop.render.XMLHandler;
  51. import org.apache.fop.render.RendererContext;
  52. /**
  53. * This is the SVG renderer.
  54. */
  55. public class SVGRenderer extends AbstractRenderer implements XMLHandler {
  56. /** SVG MIME type */
  57. public static final String SVG_MIME_TYPE = "image/svg+xml";
  58. /** SVG namespace */
  59. public static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
  60. private org.w3c.dom.Document svgDocument;
  61. private Element svgRoot;
  62. private Element currentPageG = null;
  63. private Element lastLink = null;
  64. private String lastViewbox = null;
  65. private Element docDefs = null;
  66. private Element pageDefs = null;
  67. private Element pagesGroup = null;
  68. // first sequence title
  69. private LineArea docTitle = null;
  70. private RendererContext context;
  71. private OutputStream ostream;
  72. private float totalWidth = 0;
  73. private float totalHeight = 0;
  74. private float sequenceWidth = 0;
  75. private float sequenceHeight = 0;
  76. private float pageWidth = 0;
  77. private float pageHeight = 0;
  78. private int pageNumber = 0;
  79. private HashMap fontNames = new HashMap();
  80. private HashMap fontStyles = new HashMap();
  81. private Color saveColor = null;
  82. /**
  83. * The current (internal) font name
  84. */
  85. private String currentFontName;
  86. /**
  87. * The current font size in millipoints
  88. */
  89. private int currentFontSize;
  90. /**
  91. * The current colour's red, green and blue component
  92. */
  93. private float currentRed = 0;
  94. private float currentGreen = 0;
  95. private float currentBlue = 0;
  96. /**
  97. * Creates a new SVG renderer.
  98. */
  99. public SVGRenderer() {
  100. context = new RendererContext(this, SVG_MIME_TYPE);
  101. }
  102. /**
  103. * @see org.apache.fop.render.Renderer#setUserAgent(FOUserAgent)
  104. */
  105. public void setUserAgent(FOUserAgent agent) {
  106. super.setUserAgent(agent);
  107. userAgent.getXMLHandlerRegistry().addXMLHandler(this);
  108. }
  109. /**
  110. * @see org.apache.fop.render.Renderer#setupFontInfo(FontInfo)
  111. */
  112. public void setupFontInfo(FontInfo fontInfo) {
  113. // create a temp Image to test font metrics on
  114. BufferedImage fontImage =
  115. new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  116. org.apache.fop.render.java2d.FontSetup.setup(fontInfo,
  117. fontImage.createGraphics());
  118. }
  119. /**
  120. * @see org.apache.fop.render.Renderer#startRenderer(OutputStream)
  121. */
  122. public void startRenderer(OutputStream outputStream)
  123. throws IOException {
  124. ostream = outputStream;
  125. DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  126. svgDocument = impl.createDocument(SVG_NAMESPACE, "svg", null);
  127. svgRoot = svgDocument.getDocumentElement();
  128. /*
  129. ProcessingInstruction pi =
  130. svgDocument.createProcessingInstruction("xml",
  131. " version=\"1.0\" encoding=\"ISO-8859-1\"");
  132. svgDocument.insertBefore(pi, svgRoot);
  133. */
  134. docDefs = svgDocument.createElementNS(SVG_NAMESPACE, "defs");
  135. svgRoot.appendChild(docDefs);
  136. pagesGroup = svgDocument.createElementNS(SVG_NAMESPACE, "g");
  137. pageDefs = svgDocument.createElementNS(SVG_NAMESPACE, "defs");
  138. pagesGroup.appendChild(pageDefs);
  139. svgRoot.appendChild(pagesGroup);
  140. }
  141. /**
  142. * @see org.apache.fop.render.Renderer#stopRenderer()
  143. */
  144. public void stopRenderer() throws IOException {
  145. totalWidth += sequenceWidth;
  146. if (sequenceHeight > totalHeight) {
  147. totalHeight = sequenceHeight;
  148. }
  149. svgRoot.setAttributeNS(null, "width", "" + (totalWidth + 1));
  150. svgRoot.setAttributeNS(null, "height", "" + (totalHeight + 1));
  151. //svgRoot.setAttributeNS(null, "viewBox", "0 0 " + pageWidth + " " + pageHeight);
  152. SVGTranscoder svgT = new SVGTranscoder();
  153. TranscoderInput input = new TranscoderInput(svgDocument);
  154. TranscoderOutput output =
  155. new TranscoderOutput(new OutputStreamWriter(ostream));
  156. try {
  157. svgT.transcode(input, output);
  158. } catch (TranscoderException e) {
  159. getLogger().error("could not write svg file :" + e.getMessage(), e);
  160. }
  161. ostream.flush();
  162. ostream = null;
  163. svgDocument = null;
  164. svgRoot = null;
  165. currentPageG = null;
  166. lastLink = null;
  167. totalWidth = 0;
  168. totalHeight = 0;
  169. pageNumber = 0;
  170. }
  171. /**
  172. * @see org.apache.fop.render.Renderer#startPageSequence(Title)
  173. */
  174. public void startPageSequence(LineArea seqTitle) {
  175. totalWidth += sequenceWidth;
  176. if (sequenceHeight > totalHeight) {
  177. totalHeight = sequenceHeight;
  178. }
  179. sequenceWidth = 0;
  180. sequenceHeight = 0;
  181. if (seqTitle != null && docTitle == null) {
  182. // convert first title to a string and set for svg document title
  183. docTitle = seqTitle;
  184. String str = convertTitleToString(seqTitle);
  185. Element svgTitle = svgDocument.createElementNS(SVG_NAMESPACE, "title");
  186. Text strNode = svgDocument.createTextNode(str);
  187. svgTitle.appendChild(strNode);
  188. svgRoot.insertBefore(svgTitle, svgRoot.getFirstChild());
  189. }
  190. }
  191. /**
  192. * @see org.apache.fop.render.Renderer#renderPage(PageViewport)
  193. */
  194. public void renderPage(PageViewport page) throws IOException, FOPException {
  195. float lastWidth = pageWidth;
  196. float lastHeight = pageHeight;
  197. Rectangle2D area = page.getViewArea();
  198. pageWidth = (float) area.getWidth() / 1000f;
  199. pageHeight = (float) area.getHeight() / 1000f;
  200. // if there is a link from the last page
  201. if (lastLink != null) {
  202. lastLink.setAttributeNS(null, "xlink:href", "#svgView(viewBox("
  203. + totalWidth + ", "
  204. + sequenceHeight + ", "
  205. + pageWidth + ", "
  206. + pageHeight + "))");
  207. pagesGroup.appendChild(lastLink);
  208. }
  209. currentPageG = svgDocument.createElementNS(SVG_NAMESPACE, "svg");
  210. currentPageG.setAttributeNS(null, "viewbox",
  211. "0 0 " + (int) pageWidth + " " + (int) pageHeight);
  212. currentPageG.setAttributeNS(null, "width",
  213. "" + ((int) pageWidth + 1));
  214. currentPageG.setAttributeNS(null, "height",
  215. "" + ((int) pageHeight + 1));
  216. currentPageG.setAttributeNS(null, "id", "Page-" + pageNumber);
  217. currentPageG.setAttributeNS(null, "style", "font-family:sanserif;font-size:12");
  218. pageDefs.appendChild(currentPageG);
  219. if (pageWidth > sequenceWidth) {
  220. sequenceWidth = pageWidth;
  221. }
  222. sequenceHeight += pageHeight;
  223. Element border =
  224. SVGUtilities.createRect(svgDocument, 0, 0, pageWidth,
  225. pageHeight);
  226. border.setAttributeNS(null, "style", "fill:none;stroke:black");
  227. currentPageG.appendChild(border);
  228. // render the page contents
  229. super.renderPage(page);
  230. Element use = svgDocument.createElementNS(SVG_NAMESPACE, "use");
  231. use.setAttributeNS(null, "xlink:href", "#Page-" + pageNumber);
  232. use.setAttributeNS(null, "x", "" + totalWidth);
  233. use.setAttributeNS(null, "y", "" + (sequenceHeight - pageHeight));
  234. pagesGroup.appendChild(use);
  235. Element lastPageLink = svgDocument.createElementNS(SVG_NAMESPACE, "a");
  236. if (lastLink != null) {
  237. lastPageLink.setAttributeNS(null, "xlink:href", lastViewbox);
  238. } else {
  239. lastPageLink.setAttributeNS(null, "xlink:href",
  240. "#svgView(viewBox("
  241. + totalWidth + ", "
  242. + (sequenceHeight - pageHeight) + ", "
  243. + pageWidth + ", "
  244. + pageHeight + "))");
  245. }
  246. pagesGroup.appendChild(lastPageLink);
  247. // setup a link to the next page, only added when the
  248. // next page is rendered
  249. Element rect = SVGUtilities.createRect(svgDocument, totalWidth,
  250. (sequenceHeight - pageHeight), pageWidth / 2, pageHeight);
  251. rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
  252. lastPageLink.appendChild(rect);
  253. lastLink = svgDocument.createElementNS(SVG_NAMESPACE, "a");
  254. rect = SVGUtilities.createRect(svgDocument,
  255. totalWidth + pageWidth / 2,
  256. (sequenceHeight - pageHeight), pageWidth / 2, pageHeight);
  257. rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
  258. lastLink.appendChild(rect);
  259. lastViewbox = "#svgView(viewBox("
  260. + totalWidth + ", "
  261. + (sequenceHeight - pageHeight) + ", "
  262. + pageWidth + ", "
  263. + pageHeight + "))";
  264. pageNumber++;
  265. }
  266. /**
  267. * Method renderForeignObject.
  268. * @param fo the foreign object
  269. */
  270. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  271. org.w3c.dom.Document doc = fo.getDocument();
  272. String ns = fo.getNameSpace();
  273. renderXML(context, doc, ns);
  274. }
  275. /** @see org.apache.fop.render.XMLHandler */
  276. public void handleXML(RendererContext context,
  277. org.w3c.dom.Document doc, String ns) throws Exception {
  278. if (SVG_NAMESPACE.equals(ns)) {
  279. if (!(doc instanceof SVGDocument)) {
  280. DOMImplementation impl =
  281. SVGDOMImplementation.getDOMImplementation();
  282. doc = DOMUtilities.deepCloneDocument(doc, impl);
  283. }
  284. SVGSVGElement svg = ((SVGDocument) doc).getRootElement();
  285. Element view = svgDocument.createElementNS(SVG_NAMESPACE, "svg");
  286. Node newsvg = svgDocument.importNode(svg, true);
  287. //view.setAttributeNS(null, "viewBox", "0 0 ");
  288. view.setAttributeNS(null, "x", "" + currentIPPosition / 1000f);
  289. view.setAttributeNS(null, "y", "" + currentBPPosition / 1000f);
  290. // this fixes a problem where the xmlns is repeated sometimes
  291. Element ele = (Element) newsvg;
  292. ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns",
  293. SVG_NAMESPACE);
  294. if (ele.hasAttributeNS(null, "xmlns")) {
  295. ele.removeAttributeNS(null, "xmlns");
  296. }
  297. view.appendChild(newsvg);
  298. currentPageG.appendChild(view);
  299. }
  300. }
  301. /**
  302. * @see org.apache.fop.render.Renderer#renderLeader(Leader)
  303. */
  304. public void renderLeader(Leader area) {
  305. String style = "stroke:black;stroke-width:"
  306. + (area.getRuleThickness() / 1000) + ";";
  307. switch (area.getRuleStyle()) {
  308. case EN_DOTTED:
  309. style += "stroke-dasharray:1,1";
  310. break;
  311. case EN_DASHED:
  312. style += "stroke-dasharray:5,1";
  313. break;
  314. case EN_SOLID:
  315. break;
  316. case EN_DOUBLE:
  317. break;
  318. case EN_GROOVE:
  319. break;
  320. case EN_RIDGE:
  321. break;
  322. }
  323. Element line = SVGUtilities.createLine(svgDocument,
  324. currentIPPosition / 1000,
  325. (currentBPPosition + area.getOffset()
  326. - area.getRuleThickness() / 2) / 1000,
  327. (currentIPPosition + area.getIPD()) / 1000,
  328. (currentBPPosition + area.getOffset()
  329. - area.getRuleThickness() / 2) / 1000);
  330. line.setAttributeNS(null, "style", style);
  331. currentPageG.appendChild(line);
  332. super.renderLeader(area);
  333. }
  334. /**
  335. * @see org.apache.fop.render.Renderer#renderText(TextArea)
  336. */
  337. public void renderText(TextArea text) {
  338. Element textElement = SVGUtilities.createText(svgDocument,
  339. currentIPPosition / 1000,
  340. (currentBPPosition + text.getOffset()) / 1000,
  341. text.getTextArea());
  342. currentPageG.appendChild(textElement);
  343. super.renderText(text);
  344. }
  345. /**
  346. * @see org.apache.fop.render.Renderer#renderCharacter(Character)
  347. */
  348. public void renderCharacter(org.apache.fop.area.inline.Character ch) {
  349. Element text = SVGUtilities.createText(svgDocument,
  350. currentIPPosition / 1000,
  351. (currentBPPosition + ch.getOffset()) / 1000,
  352. "" + ch.getChar());
  353. currentPageG.appendChild(text);
  354. super.renderCharacter(ch);
  355. }
  356. /** @see org.apache.fop.render.AbstractRenderer */
  357. public String getMimeType() {
  358. return SVG_MIME_TYPE;
  359. }
  360. /** @see org.apache.fop.render.XMLHandler#getNamespace() */
  361. public String getNamespace() {
  362. return SVG_NAMESPACE;
  363. }
  364. }