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.

SVGPrintDocumentHandler.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.svg;
  19. import java.awt.Dimension;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.helpers.AttributesImpl;
  22. import org.apache.fop.render.intermediate.IFConstants;
  23. import org.apache.fop.render.intermediate.IFContext;
  24. import org.apache.fop.render.intermediate.IFException;
  25. import org.apache.fop.render.intermediate.IFPainter;
  26. import org.apache.fop.util.XMLUtil;
  27. /**
  28. * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation
  29. * that writes SVG Print.
  30. */
  31. public class SVGPrintDocumentHandler extends AbstractSVGDocumentHandler {
  32. /**
  33. * Default constructor.
  34. */
  35. public SVGPrintDocumentHandler(IFContext context) {
  36. super(context);
  37. }
  38. /** {@inheritDoc} */
  39. public boolean supportsPagesOutOfOrder() {
  40. return false;
  41. }
  42. /** {@inheritDoc} */
  43. public String getMimeType() {
  44. return MIME_SVG_PRINT;
  45. }
  46. /** {@inheritDoc} */
  47. public void startDocument() throws IFException {
  48. super.startDocument();
  49. try {
  50. handler.startDocument();
  51. handler.startPrefixMapping("", NAMESPACE);
  52. handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE);
  53. handler.startPrefixMapping("if", IFConstants.NAMESPACE);
  54. AttributesImpl atts = new AttributesImpl();
  55. XMLUtil.addAttribute(atts, "version", "1.2"); //SVG Print is SVG 1.2
  56. handler.startElement("svg", atts);
  57. } catch (SAXException e) {
  58. throw new IFException("SAX error in startDocument()", e);
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. public void endDocument() throws IFException {
  63. try {
  64. handler.endElement("svg");
  65. handler.endDocument();
  66. } catch (SAXException e) {
  67. throw new IFException("SAX error in endDocument()", e);
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. public void startPageSequence(String id) throws IFException {
  72. try {
  73. AttributesImpl atts = new AttributesImpl();
  74. if (id != null) {
  75. atts.addAttribute(XML_NAMESPACE, "id", "xml:id", CDATA, id);
  76. }
  77. handler.startElement("pageSet", atts);
  78. } catch (SAXException e) {
  79. throw new IFException("SAX error in startPageSequence()", e);
  80. }
  81. }
  82. /** {@inheritDoc} */
  83. public void endPageSequence() throws IFException {
  84. try {
  85. handler.endElement("pageSet");
  86. } catch (SAXException e) {
  87. throw new IFException("SAX error in endPageSequence()", e);
  88. }
  89. }
  90. /** {@inheritDoc} */
  91. public void startPage(int index, String name, String pageMasterName, Dimension size)
  92. throws IFException {
  93. try {
  94. AttributesImpl atts = new AttributesImpl();
  95. /*
  96. XMLUtil.addAttribute(atts, "index", Integer.toString(index));
  97. XMLUtil.addAttribute(atts, "name", name);
  98. */
  99. //NOTE: SVG Print doesn't support individual page sizes for each page
  100. atts.addAttribute(IFConstants.NAMESPACE, "width", "if:width",
  101. CDATA, Integer.toString(size.width));
  102. atts.addAttribute(IFConstants.NAMESPACE, "height", "if:height",
  103. CDATA, Integer.toString(size.height));
  104. atts.addAttribute(IFConstants.NAMESPACE, "viewBox", "if:viewBox", CDATA,
  105. "0 0 " + Integer.toString(size.width) + " " + Integer.toString(size.height));
  106. handler.startElement("page", atts);
  107. } catch (SAXException e) {
  108. throw new IFException("SAX error in startPage()", e);
  109. }
  110. }
  111. /** {@inheritDoc} */
  112. public void startPageHeader() throws IFException {
  113. }
  114. /** {@inheritDoc} */
  115. public void endPageHeader() throws IFException {
  116. }
  117. /** {@inheritDoc} */
  118. public IFPainter startPageContent() throws IFException {
  119. try {
  120. handler.startElement("g");
  121. } catch (SAXException e) {
  122. throw new IFException("SAX error in startPageContent()", e);
  123. }
  124. return new SVGPainter(this, handler);
  125. }
  126. /** {@inheritDoc} */
  127. public void endPageContent() throws IFException {
  128. try {
  129. handler.endElement("g");
  130. } catch (SAXException e) {
  131. throw new IFException("SAX error in endPageContent()", e);
  132. }
  133. }
  134. /** {@inheritDoc} */
  135. public void startPageTrailer() throws IFException {
  136. }
  137. /** {@inheritDoc} */
  138. public void endPageTrailer() throws IFException {
  139. }
  140. /** {@inheritDoc} */
  141. public void endPage() throws IFException {
  142. try {
  143. handler.endElement("page");
  144. } catch (SAXException e) {
  145. throw new IFException("SAX error in endPage()", e);
  146. }
  147. }
  148. }