aboutsummaryrefslogtreecommitdiffstats
path: root/src/sandbox/org/apache/fop/render/svg/SVGPrintPainter.java
blob: db6a96afe83f5b387168a8730aa5cdf2179fcc0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.svg;

import java.awt.Dimension;

import javax.xml.transform.Result;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.apache.fop.render.intermediate.IFConstants;
import org.apache.fop.render.intermediate.IFException;

/**
 * IFPainter implementation that writes SVG Print.
 */
public class SVGPrintPainter extends AbstractSVGPainter {

    /**
     * Default constructor.
     */
    public SVGPrintPainter() {
        //nop
    }

    /**
     * Creates a new SVGPrintPainter that sends the XML content it generates to the given
     * SAX ContentHandler.
     * @param result the JAXP Result object to receive the generated content
     * @throws IFException if an error occurs setting up the output
     */
    public SVGPrintPainter(Result result) throws IFException {
        setResult(result);
    }

    /** {@inheritDoc} */
    public boolean supportsPagesOutOfOrder() {
        return false;
    }

    /** {@inheritDoc} */
    public void startDocument() throws IFException {
        try {
            handler.startDocument();
            handler.startPrefixMapping("", NAMESPACE);
            handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE);
            handler.startPrefixMapping("if", IFConstants.NAMESPACE);
            AttributesImpl atts = new AttributesImpl();
            atts.addAttribute("", "version", "version", CDATA, "1.2"); //SVG Print is SVG 1.2
            startElement("svg", atts);
        } catch (SAXException e) {
            throw new IFException("SAX error in startDocument()", e);
        }
    }

    /** {@inheritDoc} */
    public void endDocument() throws IFException {
        try {
            endElement("svg");
            handler.endDocument();
        } catch (SAXException e) {
            throw new IFException("SAX error in endDocument()", e);
        }
    }

    /** {@inheritDoc} */
    public void startPageSequence(String id) throws IFException {
        try {
            AttributesImpl atts = new AttributesImpl();
            if (id != null) {
                atts.addAttribute(XML_NAMESPACE, "id", "xml:id", CDATA, id);
            }
            startElement("pageSet", atts);
        } catch (SAXException e) {
            throw new IFException("SAX error in startPageSequence()", e);
        }
    }

    /** {@inheritDoc} */
    public void endPageSequence() throws IFException {
        try {
            endElement("pageSet");
        } catch (SAXException e) {
            throw new IFException("SAX error in endPageSequence()", e);
        }
    }

    /** {@inheritDoc} */
    public void startPage(int index, String name, Dimension size) throws IFException {
        try {
            AttributesImpl atts = new AttributesImpl();
            /*
            atts.addAttribute("", "index", "index", CDATA, Integer.toString(index));
            atts.addAttribute("", "name", "name", CDATA, name);
            */
            //NOTE: SVG Print doesn't support individual page sizes for each page
            atts.addAttribute(IFConstants.NAMESPACE, "width", "if:width",
                    CDATA, Integer.toString(size.width));
            atts.addAttribute(IFConstants.NAMESPACE, "height", "if:height",
                    CDATA, Integer.toString(size.height));
            atts.addAttribute(IFConstants.NAMESPACE, "viewBox", "if:viewBox", CDATA,
                    "0 0 " + Integer.toString(size.width) + " " + Integer.toString(size.height));
            startElement("page", atts);
        } catch (SAXException e) {
            throw new IFException("SAX error in startPage()", e);
        }
    }

    /** {@inheritDoc} */
    public void startPageHeader() throws IFException {
    }

    /** {@inheritDoc} */
    public void endPageHeader() throws IFException {
    }

    /** {@inheritDoc} */
    public void startPageContent() throws IFException {
        super.startPageContent();
        try {
            startElement("g");
        } catch (SAXException e) {
            throw new IFException("SAX error in startPageContent()", e);
        }
    }

    /** {@inheritDoc} */
    public void endPageContent() throws IFException {
        try {
            endElement("g");
        } catch (SAXException e) {
            throw new IFException("SAX error in endPageContent()", e);
        }
        super.endPageContent();
    }

    /** {@inheritDoc} */
    public void startPageTrailer() throws IFException {
    }

    /** {@inheritDoc} */
    public void endPageTrailer() throws IFException {
    }

    /** {@inheritDoc} */
    public void endPage() throws IFException {
        try {
            endElement("page");
        } catch (SAXException e) {
            throw new IFException("SAX error in endPage()", e);
        }
    }

}